23 RCSID(
"$Id: 65cd304c9cb8c036bd5195a8459ba4874c20ebde $")
25 #include <freeradius-devel/missing.h>
31 #if !defined(HAVE_CLOCK_GETTIME) && defined(__MACH__)
32 # include <mach/mach_time.h>
35 #ifndef HAVE_STRNCASECMP
39 unsigned char *p1, *p2;
42 p1 = (
unsigned char *)s1;
43 p2 = (
unsigned char *)s2;
47 if (*p1 == 0 && *p2 == 0)
52 if (islower(c1)) c1 = toupper(c1);
53 if (islower(c2)) c2 = toupper(c2);
55 if ((dif = c1 - c2) != 0)
65 #ifndef HAVE_STRCASECMP
87 if (
n == 0)
return NULL;
89 memcpy(&p, &s,
sizeof(p));
90 for (p += (
n - 1); p >= (
uint8_t const *)s; p--)
if (*p == (
uint8_t)c)
return (
void *)p;
96 #ifndef HAVE_INET_ATON
101 if (sscanf(cp,
"%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4)
104 inp->s_addr = htonl((a1 << 24) + (a2 << 16) + (a3 << 8) + a4);
123 strsep(
char **stringp,
char const *delim)
130 if ((s = *stringp) == NULL)
137 if ((
sc = *spanp++) == c) {
152 #ifndef HAVE_LOCALTIME_R
165 memcpy(result, localtime(l_clock),
sizeof(*result));
182 char *
ctime_r(time_t
const *l_clock,
char *l_buf)
184 strcpy(l_buf, ctime(l_clock));
190 #ifndef HAVE_GMTIME_R
201 struct tm *
gmtime_r(time_t
const *l_clock,
struct tm *result)
203 memcpy(result, gmtime(l_clock),
sizeof(*result));
209 #ifndef HAVE_VDPRINTF
217 if (dup_fd < 0)
return -1;
219 fp = fdopen(fd,
"w");
233 #if !defined(HAVE_CLOCK_GETTIME) && defined(__MACH__)
234 int clock_gettime(
int clk_id,
struct timespec *t)
236 static mach_timebase_info_data_t timebase;
237 static bool done_init =
false;
242 pthread_mutex_lock(&mutex);
244 mach_timebase_info(&timebase);
247 pthread_mutex_unlock(&mutex);
254 case CLOCK_MONOTONIC:
257 time = mach_absolute_time();
258 double nanoseconds = ((double)time * (
double)timebase.numer)/((
double)timebase.denom);
259 double seconds = ((double)time * (
double)timebase.numer)/((
double)timebase.denom * 1e9);
261 t->tv_nsec = nanoseconds;
275 #ifndef HAVE_INET_PTON
282 static char const digits[] =
"0123456789";
288 while (*p && ((off = strchr(digits, *p)) != NULL)) {
290 num += (off - digits);
292 if (num > 255)
return 0;
320 memcpy(dst, &tmp,
sizeof(tmp));
325 # ifdef HAVE_STRUCT_SOCKADDR_IN6
339 static int inet_pton6(
char const *src,
unsigned char *dst)
341 static char const xdigits_l[] =
"0123456789abcdef",
342 xdigits_u[] =
"0123456789ABCDEF";
343 uint8_t tmp[IN6ADDRSZ], *tp, *endp, *colonp;
344 char const *xdigits, *curtok;
348 memset((tp = tmp), 0, IN6ADDRSZ);
349 endp = tp + IN6ADDRSZ;
358 while ((ch = *src++) !=
'\0') {
361 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
362 pch = strchr((xdigits = xdigits_u), ch);
365 val |= (pch - xdigits);
379 if (tp + INT16SZ > endp)
381 *tp++ = (
uint8_t) (val >> 8) & 0xff;
387 if (ch ==
'.' && ((tp + INADDRSZ) <= endp) &&
388 inet_pton4(curtok, (
struct in_addr *) tp) > 0) {
396 if (tp + INT16SZ > endp)
398 *tp++ = (
uint8_t) (val >> 8) & 0xff;
401 if (colonp != NULL) {
406 int const n = tp - colonp;
409 for (i = 1; i <=
n; i++) {
410 endp[- i] = colonp[
n - i];
418 memcpy(dst, tmp, IN6ADDRSZ);
429 if (af == AF_INET)
return inet_pton4(src, dst);
431 # ifdef HAVE_STRUCT_SOCKADDR_IN6
432 if (af == AF_INET6)
return inet_pton6(src, dst);
438 #ifndef HAVE_INET_NTOP
443 char const *
inet_ntop(
int af,
void const *src,
char *dst,
size_t cnt)
448 if (cnt <= INET_ADDRSTRLEN)
return NULL;
451 ipaddr[0], ipaddr[1],
452 ipaddr[2], ipaddr[3]);
460 if (af == AF_INET6) {
461 struct in6_addr const *ipaddr = src;
463 if (cnt <= INET6_ADDRSTRLEN)
return NULL;
465 snprintf(dst, cnt,
"%x:%x:%x:%x:%x:%x:%x:%x",
481 #ifndef HAVE_SENDMMSG
504 for (i = 0; i < vlen; i++) {
507 slen = sendmsg(
sockfd, &msgvec[i].msg_hdr, flags);
509 msgvec[i].msg_len = 0;
516 if (i == 0)
return -1;
519 msgvec[i].msg_len = (
unsigned int)slen;
529 #ifndef HAVE_CLOSEFROM
543 # define CLOSEFROM_DIR "/proc/self/fd"
544 # elif defined(__APPLE__)
545 # define CLOSEFROM_DIR "/dev/fd"
547 # undef HAVE_DIRENT_H
555 # ifdef HAVE_DIRENT_H
560 if (fcntl(fd, F_CLOSEM) == 0)
return;
564 maxfd = fcntl(fd, F_F_MAXFD);
565 if (maxfd >= 0)
goto do_close;
569 maxfd = sysconf(_SC_OPEN_MAX);
575 # ifdef HAVE_DIRENT_H
579 dir = opendir(CLOSEFROM_DIR);
585 while ((dp = readdir(dir)) != NULL) {
586 my_fd = strtol(dp->d_name, &endp, 10);
587 if (my_fd <= 0)
continue;
591 if (my_fd == dirfd(dir))
continue;
593 if ((my_fd >= fd) && (my_fd <= maxfd)) {
594 (void)
close((
int) my_fd);
597 (void) closedir(dir);
606 if (fd > maxfd)
return;
611 for (i = fd; i < maxfd; i++) {
619 #ifndef HAVE_MEMSET_EXPLICIT
621 #ifdef HAVE_EXPLICIT_BZERO
627 if (!len)
return ptr;
629 #ifdef HAVE_EXPLICIT_BZERO
630 explicit_bzero(ptr, len);
633 volatile unsigned char *
volatile p = (
volatile unsigned char *
volatile) ptr;
strcpy(log_entry->msg, buffer)
void * memset_explicit(void *ptr, int ch, size_t len)
struct tm * gmtime_r(time_t const *l_clock, struct tm *result)
int inet_pton(int af, char const *src, void *dst)
int strncasecmp(char *s1, char *s2, int n)
void * memrchr(void const *s, int c, size_t n)
GNU libc extension on some platforms.
int vdprintf(int fd, char const *format, va_list args)
char * ctime_r(time_t const *l_clock, char *l_buf)
int strcasecmp(char *s1, char *s2)
char const * inet_ntop(int af, void const *src, char *dst, size_t cnt)
struct tm * localtime_r(time_t const *l_clock, struct tm *result)
int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags)
Emulates the real sendmmsg in userland.
int inet_aton(char const *cp, struct in_addr *inp)
static int inet_pton4(char const *src, struct in_addr *dst)
char * strsep(char **stringp, char const *delim)
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
static const uchar sc[16]
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
int format(printf, 5, 0))