* Add support for other operating systems (kFreeBSD, FreeBSD, and OSX for now).

* Switch to autoconf from genconfig.sh.
This commit is contained in:
Justin Maggard
2011-09-16 23:39:58 +00:00
parent 715af3519b
commit 773e1f6566
40 changed files with 1952 additions and 533 deletions

53
uuid.c
View File

@ -28,22 +28,27 @@
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>
#if HAVE_MACH_MACH_TIME_H
#include <mach/mach_time.h>
#elif HAVE_CLOCK_GETTIME_SYSCALL
#include <sys/syscall.h>
#endif
#include "getifaddr.h"
#include "log.h"
#define ETH_ALEN 6
#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000L
#endif
#define NSEC_PER_MSEC 1000000L
static u_int32_t clock_seq;
static const u_int32_t clock_seq_max = 0x3fff; /* 14 bits */
static uint32_t clock_seq;
static const uint32_t clock_seq_max = 0x3fff; /* 14 bits */
static int clock_seq_initialized;
unsigned long long
@ -51,7 +56,17 @@ monotonic_us(void)
{
struct timespec ts;
#if defined(HAVE_LIBRT)
clock_gettime(CLOCK_MONOTONIC, &ts);
#elif HAVE_CLOCK_GETTIME_SYSCALL
syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts);
#elif HAVE_MACH_MACH_TIME_H
return mach_absolute_time();
#else
struct timeval tv;
gettimeofday(&tv, 0);
TIMEVAL_TO_TIMESPEC(&tv, &ts);
#endif
return ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000;
}
@ -120,12 +135,12 @@ init_clockseq(void)
int
generate_uuid(unsigned char uuid_out[16])
{
static u_int64_t last_time_all;
static uint64_t last_time_all;
static unsigned int clock_seq_started;
static char last_node[6] = { 0, 0, 0, 0, 0, 0 };
struct timespec ts;
u_int64_t time_all;
uint64_t time_all;
int inc_clock_seq = 0;
unsigned char mac[6];
@ -162,8 +177,16 @@ generate_uuid(unsigned char uuid_out[16])
* nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of
* Gregorian reform to the Christian calendar).
*/
#if HAVE_LIBRT
clock_gettime(CLOCK_REALTIME, &ts);
#elif HAVE_CLOCK_GETTIME_SYSCALL
syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts);
time_all = ((u_int64_t)ts.tv_sec) * (NSEC_PER_SEC / 100);
#else
struct timeval tv;
gettimeofday(&tv, 0);
TIMEVAL_TO_TIMESPEC(&tv, &ts);
#endif
time_all = ((uint64_t)ts.tv_sec) * (NSEC_PER_SEC / 100);
time_all += ts.tv_nsec / 100;
/* add offset from Gregorian Calendar to Jan 1 1970 */
@ -192,14 +215,14 @@ generate_uuid(unsigned char uuid_out[16])
last_time_all = time_all;
/* Fill in timestamp and clock_seq values */
uuid_out[3] = (u_int8_t)time_all;
uuid_out[2] = (u_int8_t)(time_all >> 8);
uuid_out[1] = (u_int8_t)(time_all >> 16);
uuid_out[0] = (u_int8_t)(time_all >> 24);
uuid_out[5] = (u_int8_t)(time_all >> 32);
uuid_out[4] = (u_int8_t)(time_all >> 40);
uuid_out[7] = (u_int8_t)(time_all >> 48);
uuid_out[6] = (u_int8_t)(time_all >> 56);
uuid_out[3] = (uint8_t)time_all;
uuid_out[2] = (uint8_t)(time_all >> 8);
uuid_out[1] = (uint8_t)(time_all >> 16);
uuid_out[0] = (uint8_t)(time_all >> 24);
uuid_out[5] = (uint8_t)(time_all >> 32);
uuid_out[4] = (uint8_t)(time_all >> 40);
uuid_out[7] = (uint8_t)(time_all >> 48);
uuid_out[6] = (uint8_t)(time_all >> 56);
uuid_out[8] = clock_seq >> 8;
uuid_out[9] = clock_seq & 0xff;