24 RCSID(
"$Id: 85f5f81147491e1804e70bc00d64f096cb6e15de $")
26 #include <freeradius-devel/libradius.h>
30 #if !defined(HAVE_CLOCK_GETTIME) && defined(__MACH__)
31 # include <mach/mach_time.h>
42 #ifndef HAVE_STRNCASECMP
46 unsigned char *p1, *p2;
49 p1 = (
unsigned char *)s1;
50 p2 = (
unsigned char *)s2;
54 if (*p1 == 0 && *p2 == 0)
59 if (islower(c1)) c1 = toupper(c1);
60 if (islower(c2)) c2 = toupper(c2);
62 if ((dif = c1 - c2) != 0)
72 #ifndef HAVE_STRCASECMP
85 #ifndef HAVE_INET_ATON
90 if (sscanf(cp,
"%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4)
93 inp->s_addr = htonl((a1 << 24) + (a2 << 16) + (a3 << 8) + a4);
112 strsep(
char **stringp,
char const *delim)
119 if ((s = *stringp) == NULL)
126 if ((sc = *spanp++) == c) {
141 #ifndef HAVE_LOCALTIME_R
154 memcpy(result, localtime(l_clock),
sizeof(*result));
171 char *
ctime_r(time_t
const *l_clock,
char *l_buf)
173 strcpy(l_buf, ctime(l_clock));
179 #ifndef HAVE_GMTIME_R
190 struct tm *
gmtime_r(time_t
const *l_clock,
struct tm *result)
192 memcpy(result, gmtime(l_clock),
sizeof(*result));
198 #ifndef HAVE_VDPRINTF
199 int vdprintf (
int fd,
char const *format, va_list args)
206 if (dup_fd < 0)
return -1;
208 fp = fdopen(fd,
"w");
214 ret = vfprintf(fp, format, args);
221 #ifndef HAVE_GETTIMEOFDAY
229 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
230 #define DELTA_EPOCH_IN_USEC 11644473600000000Ui64
232 #define DELTA_EPOCH_IN_USEC 11644473600000000ULL
235 static uint64_t filetime_to_unix_epoch (FILETIME
const *ft)
237 uint64_t res = (uint64_t) ft->dwHighDateTime << 32;
239 res |= ft->dwLowDateTime;
241 res -= DELTA_EPOCH_IN_USEC;
245 int gettimeofday (
struct timeval *tv,
UNUSED void *tz)
254 GetSystemTimeAsFileTime (&ft);
255 tim = filetime_to_unix_epoch (&ft);
256 tv->tv_sec = (long) (tim / 1000000L);
257 tv->tv_usec = (long) (tim % 1000000L);
263 #if !defined(HAVE_CLOCK_GETTIME) && defined(__MACH__)
264 int clock_gettime(
int clk_id,
struct timespec *t)
266 static mach_timebase_info_data_t timebase;
267 static bool done_init =
false;
269 #ifdef HAVE_PTHREAD_H
270 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
274 #ifdef HAVE_PTHREAD_H
278 mach_timebase_info(&timebase);
279 #ifdef HAVE_PTHREAD_H
290 case CLOCK_MONOTONIC:
293 time = mach_absolute_time();
294 double nseconds = ((double)time * (
double)timebase.numer)/((
double)timebase.denom);
295 double seconds = ((double)time * (
double)timebase.numer)/((
double)timebase.denom * 1e9);
297 t->tv_nsec = nseconds;
308 #define NTP_EPOCH_OFFSET 2208988800ULL
320 usec = tv->tv_usec * 4295;
321 usec -= ((tv->tv_usec * 2143) >> 16);
326 memcpy(ntp, &sec,
sizeof(sec));
327 memcpy(ntp +
sizeof(sec), &usec,
sizeof(usec));
338 memcpy(&sec, ntp,
sizeof(sec));
339 memcpy(&usec, ntp +
sizeof(sec),
sizeof(usec));
345 tv->tv_usec = usec / 4295;
348 #if !defined(HAVE_128BIT_INTEGERS) && !defined(WORDS_BIGENDIAN)
356 uint64_t
const *p = (uint64_t
const *) #
360 ret[1] = ntohll(p[0]);
361 ret[0] = ntohll(p[1]);
363 return *(uint128_t *)ret;
370 #ifndef HAVE_INET_PTON
377 static char const digits[] =
"0123456789";
383 while (*p && ((off = strchr(digits, *p)) != NULL)) {
385 num += (off - digits);
387 if (num > 255)
return 0;
415 memcpy(dst, &tmp,
sizeof(tmp));
420 # ifdef HAVE_STRUCT_SOCKADDR_IN6
434 static int inet_pton6(
char const *src,
unsigned char *dst)
436 static char const xdigits_l[] =
"0123456789abcdef",
437 xdigits_u[] =
"0123456789ABCDEF";
438 uint8_t tmp[IN6ADDRSZ], *tp, *endp, *colonp;
439 char const *xdigits, *curtok;
443 memset((tp = tmp), 0, IN6ADDRSZ);
444 endp = tp + IN6ADDRSZ;
453 while ((ch = *src++) !=
'\0') {
456 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
457 pch = strchr((xdigits = xdigits_u), ch);
460 val |= (pch - xdigits);
474 if (tp + INT16SZ > endp)
476 *tp++ = (uint8_t) (val >> 8) & 0xff;
477 *tp++ = (uint8_t) val & 0xff;
482 if (ch ==
'.' && ((tp + INADDRSZ) <= endp) &&
483 inet_pton4(curtok, (
struct in_addr *) tp) > 0) {
491 if (tp + INT16SZ > endp)
493 *tp++ = (uint8_t) (val >> 8) & 0xff;
494 *tp++ = (uint8_t) val & 0xff;
496 if (colonp != NULL) {
501 int const n = tp - colonp;
504 for (i = 1; i <= n; i++) {
505 endp[- i] = colonp[n - i];
513 memcpy(dst, tmp, IN6ADDRSZ);
524 if (af == AF_INET)
return inet_pton4(src, dst);
526 # ifdef HAVE_STRUCT_SOCKADDR_IN6
527 if (af == AF_INET6)
return inet_pton6(src, dst);
533 #ifndef HAVE_INET_NTOP
538 char const *
inet_ntop(
int af,
void const *src,
char *dst,
size_t cnt)
541 uint8_t
const *ipaddr = src;
543 if (cnt <= INET_ADDRSTRLEN)
return NULL;
546 ipaddr[0], ipaddr[1],
547 ipaddr[2], ipaddr[3]);
555 if (af == AF_INET6) {
556 struct in6_addr const *ipaddr = src;
558 if (cnt <= INET6_ADDRSTRLEN)
return NULL;
560 snprintf(dst, cnt,
"%x:%x:%x:%x:%x:%x:%x:%x",
561 (ipaddr->s6_addr[0] << 8) | ipaddr->s6_addr[1],
562 (ipaddr->s6_addr[2] << 8) | ipaddr->s6_addr[3],
563 (ipaddr->s6_addr[4] << 8) | ipaddr->s6_addr[5],
564 (ipaddr->s6_addr[6] << 8) | ipaddr->s6_addr[7],
565 (ipaddr->s6_addr[8] << 8) | ipaddr->s6_addr[9],
566 (ipaddr->s6_addr[10] << 8) | ipaddr->s6_addr[11],
567 (ipaddr->s6_addr[12] << 8) | ipaddr->s6_addr[13],
568 (ipaddr->s6_addr[14] << 8) | ipaddr->s6_addr[15]);
592 n = talloc_strdup(t, p);
594 talloc_set_type(n,
char);
617 n = talloc_vasprintf(t, fmt, ap);
620 talloc_set_type(n,
char);
636 p = talloc_array(t,
char, inlen + 1);
638 memcpy(p, in, inlen);
uint128_t ntohlll(uint128_t const num)
Swap byte order of 128 bit integer.
char const * inet_ntop(int af, void const *src, char *dst, size_t cnt)
static int inet_pton4(char const *src, struct in_addr *dst)
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
int inet_aton(char const *cp, struct in_addr *inp)
char * crypt(UNUSED char *key, char *salt)
#define pthread_mutex_unlock(_x)
void ntp2timeval(struct timeval *tv, char const *ntp)
int vdprintf(int fd, char const *format, va_list args)
char * talloc_typed_asprintf(void const *t, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
int inet_pton(int af, char const *src, void *dst)
int strcasecmp(char *s1, char *s2)
char * talloc_bstrndup(void const *t, char const *in, size_t inlen)
Binary safe strndup function.
char * ctime_r(time_t const *l_clock, char *l_buf)
int strncasecmp(char *s1, char *s2, int n)
void timeval2ntp(struct timeval const *tv, uint8_t *ntp)
#define pthread_mutex_lock(_x)
char * strsep(char **stringp, char const *delim)
struct tm * localtime_r(time_t const *l_clock, struct tm *result)
char * talloc_typed_strdup(void const *t, char const *p)
Call talloc strdup, setting the type on the new chunk correctly.
static const uchar sc[16]
struct tm * gmtime_r(time_t const *l_clock, struct tm *result)