#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
#include <ctype.h>
#ifndef HAVE_CRYPT
char *crypt(UNUSED char *key, char *salt)
{
return salt;
}
#endif
#ifndef HAVE_STRNCASECMP
int strncasecmp(char *s1, char *s2, int n)
{
int dif;
unsigned char *p1, *p2;
int c1, c2;
p1 = (unsigned char *)s1;
p2 = (unsigned char *)s2;
dif = 0;
while (n != 0) {
if (*p1 == 0 && *p2 == 0)
break;
c1 = *p1;
c2 = *p2;
if (islower(c1)) c1 = toupper(c1);
if (islower(c2)) c2 = toupper(c2);
if ((dif = c1 - c2) != 0)
break;
p1++;
p2++;
n--;
}
return dif;
}
#endif
#ifndef HAVE_STRCASECMP
int strcasecmp(char *s1, char *s2)
{
int l1, l2;
l1 = strlen(s1);
l2 = strlen(s2);
if (l2 > l1) l1 = l2;
return strncasecmp(s1, s2, l1);
}
#endif
#ifndef HAVE_INET_ATON
int inet_aton(const char *cp, struct in_addr *inp)
{
int a1, a2, a3, a4;
if (sscanf(cp, "%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4)
return 0;
inp->s_addr = htonl((a1 << 24) + (a2 << 16) + (a3 << 8) + a4);
return 1;
}
#endif
#ifndef HAVE_STRSEP
char *
strsep(char **stringp, const char *delim)
{
char *s;
const char *spanp;
int c, sc;
char *tok;
if ((s = *stringp) == NULL)
return (NULL);
for (tok = s;;) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
return NULL;
}
#endif
#ifndef HAVE_LOCALTIME_R
struct tm *localtime_r(const time_t *l_clock, struct tm *result)
{
memcpy(result, localtime(l_clock), sizeof(*result));
return result;
}
#endif
#ifndef HAVE_CTIME_R
char *ctime_r(const time_t *l_clock, char *l_buf)
{
strcpy(l_buf, ctime(l_clock));
return l_buf;
}
#endif
#ifndef HAVE_GMTIME_R
struct tm *gmtime_r(const time_t *l_clock, struct tm *result)
{
memcpy(result, gmtime(l_clock), sizeof(*result));
return result;
}
#endif
#ifndef HAVE_GETTIMEOFDAY
#ifdef WIN32
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
#define DELTA_EPOCH_IN_USEC 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_USEC 11644473600000000ULL
#endif
static uint64_t filetime_to_unix_epoch (const FILETIME *ft)
{
uint64_t res = (uint64_t) ft->dwHighDateTime << 32;
res |= ft->dwLowDateTime;
res /= 10;
res -= DELTA_EPOCH_IN_USEC;
return (res);
}
int gettimeofday (struct timeval *tv, UNUSED void *tz)
{
FILETIME ft;
uint64_t tim;
if (!tv) {
errno = EINVAL;
return (-1);
}
GetSystemTimeAsFileTime (&ft);
tim = filetime_to_unix_epoch (&ft);
tv->tv_sec = (long) (tim / 1000000L);
tv->tv_usec = (long) (tim % 1000000L);
return (0);
}
#endif
#endif
#define NTP_EPOCH_OFFSET 2208988800ULL
void
timeval2ntp(const struct timeval *tv, uint8_t *ntp)
{
uint32_t sec, usec;
sec = tv->tv_sec + NTP_EPOCH_OFFSET;
usec = tv->tv_usec * 4295;
usec -= ((tv->tv_usec * 2143) >> 16);
sec = htonl(sec);
usec = htonl(usec);
memcpy(ntp, &sec, sizeof(sec));
memcpy(ntp + sizeof(sec), &usec, sizeof(usec));
}
void
ntp2timeval(struct timeval *tv, const char *ntp)
{
uint32_t sec, usec;
memcpy(&sec, ntp, sizeof(sec));
memcpy(&usec, ntp + sizeof(sec), sizeof(usec));
sec = ntohl(sec);
usec = ntohl(usec);
tv->tv_sec = sec - NTP_EPOCH_OFFSET;
tv->tv_usec = usec / 4295;
}