#include <ctype.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fcntl.h>
#include <unistd.h>
#include <synch.h>
#include <sys/mman.h>
#include "uuid_misc.h"
shared_buffer_t *data;
static uuid_node_t node_id_cache;
static int node_init;
static int file_type;
static int fd;
uint16_t get_random(void);
void get_current_time(uuid_time_t *);
void struct_to_string(uuid_t, struct uuid *);
void string_to_struct(struct uuid *, uuid_t);
int get_ethernet_address(uuid_node_t *);
static int map_state();
static void format_uuid(struct uuid *, uint16_t, uuid_time_t,
uuid_node_t);
static int uuid_create(struct uuid *);
static void gen_ethernet_address(uuid_node_t *);
static void revalidate_data(uuid_node_t *);
static int
uuid_create(struct uuid *uuid)
{
uuid_time_t timestamp;
uuid_node_t system_node;
int ret, non_unique = 0;
if (node_init) {
bcopy(&node_id_cache, &system_node, sizeof (uuid_node_t));
} else {
gen_ethernet_address(&system_node);
bcopy(&system_node, &node_id_cache, sizeof (uuid_node_t));
node_init = 1;
}
if (map_state() == -1)
return (-1);
for (;;) {
if ((ret = mutex_lock(&data->lock)) == 0)
break;
else
switch (ret) {
case EOWNERDEAD:
revalidate_data(&system_node);
(void) mutex_consistent(&data->lock);
(void) mutex_unlock(&data->lock);
break;
case ENOTRECOVERABLE:
return (ret);
}
}
if (data->state.clock == 0) {
data->state.clock = get_random();
non_unique++;
}
if (memcmp(&system_node, &data->state.node, sizeof (uuid_node_t)) != 0)
data->state.clock++;
get_current_time(×tamp);
if ((data->state.ts == 0) || (data->state.ts >= timestamp)) {
data->state.clock++;
data->state.ts = timestamp;
}
if (non_unique)
system_node.nodeID[0] |= 0x80;
format_uuid(uuid, data->state.clock, timestamp, system_node);
(void) mutex_unlock(&data->lock);
return (0);
}
static void
gen_ethernet_address(uuid_node_t *system_node)
{
uchar_t node[6];
if (get_ethernet_address(system_node) != 0) {
arc4random_buf(node, 6);
(void) memcpy(system_node->nodeID, node, 6);
system_node->nodeID[0] = 0x88;
system_node->nodeID[1] = 0x00;
system_node->nodeID[2] = 0x20;
}
}
static void
format_uuid(struct uuid *uuid, uint16_t clock_seq,
uuid_time_t timestamp, uuid_node_t node)
{
uuid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF);
uuid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF);
uuid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 0x0FFF);
uuid->time_hi_and_version |= (1 << 12);
uuid->clock_seq_low = clock_seq & 0xFF;
uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
uuid->clock_seq_hi_and_reserved |= 0x80;
(void) memcpy(&uuid->node_addr, &node, sizeof (uuid->node_addr));
}
static int
map_state()
{
FILE *tmp;
if (file_type != 0)
return (1);
if ((fd = open(STATE_LOCATION, O_RDWR)) < 0) {
file_type = TEMP_FILE;
if ((tmp = tmpfile()) == NULL)
return (-1);
else
fd = fileno(tmp);
} else {
file_type = STATE_FILE;
}
(void) ftruncate(fd, (off_t)sizeof (shared_buffer_t));
data = (shared_buffer_t *)mmap(NULL, sizeof (shared_buffer_t),
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
return (-1);
(void) mutex_init(&data->lock, USYNC_PROCESS|LOCK_ROBUST, 0);
(void) close(fd);
return (1);
}
static void
revalidate_data(uuid_node_t *node)
{
int i;
data->state.ts = 0;
for (i = 0; i < sizeof (data->state.node.nodeID); i++)
data->state.node.nodeID[i] = 0;
data->state.clock = 0;
gen_ethernet_address(node);
bcopy(node, &node_id_cache, sizeof (uuid_node_t));
node_init = 1;
}
void
uuid_print(struct uuid u)
{
int i;
(void) printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
u.time_hi_and_version, u.clock_seq_hi_and_reserved,
u.clock_seq_low);
for (i = 0; i < 6; i++)
(void) printf("%2.2x", u.node_addr[i]);
(void) printf("\n");
}
void
struct_to_string(uuid_t ptr, struct uuid *uu)
{
uint_t tmp;
uchar_t *out = ptr;
tmp = uu->time_low;
out[3] = (uchar_t)tmp;
tmp >>= 8;
out[2] = (uchar_t)tmp;
tmp >>= 8;
out[1] = (uchar_t)tmp;
tmp >>= 8;
out[0] = (uchar_t)tmp;
tmp = uu->time_mid;
out[5] = (uchar_t)tmp;
tmp >>= 8;
out[4] = (uchar_t)tmp;
tmp = uu->time_hi_and_version;
out[7] = (uchar_t)tmp;
tmp >>= 8;
out[6] = (uchar_t)tmp;
tmp = uu->clock_seq_hi_and_reserved;
out[8] = (uchar_t)tmp;
tmp = uu->clock_seq_low;
out[9] = (uchar_t)tmp;
(void) memcpy(out+10, uu->node_addr, 6);
}
void
string_to_struct(struct uuid *uuid, uuid_t in)
{
uchar_t *ptr;
uint_t tmp;
ptr = in;
tmp = *ptr++;
tmp = (tmp << 8) | *ptr++;
tmp = (tmp << 8) | *ptr++;
tmp = (tmp << 8) | *ptr++;
uuid->time_low = tmp;
tmp = *ptr++;
tmp = (tmp << 8) | *ptr++;
uuid->time_mid = tmp;
tmp = *ptr++;
tmp = (tmp << 8) | *ptr++;
uuid->time_hi_and_version = tmp;
tmp = *ptr++;
uuid->clock_seq_hi_and_reserved = tmp;
tmp = *ptr++;
uuid->clock_seq_low = tmp;
(void) memcpy(uuid->node_addr, ptr, 6);
}
void
uuid_generate_random(uuid_t uu)
{
struct uuid uuid;
if (uu == NULL)
return;
(void) memset(uu, 0, sizeof (uuid_t));
(void) memset(&uuid, 0, sizeof (struct uuid));
arc4random_buf(uu, sizeof (uuid_t));
string_to_struct(&uuid, uu);
uuid.time_hi_and_version &= 0xfff;
uuid.time_hi_and_version |= (1 << 14);
uuid.clock_seq_hi_and_reserved &= 0x3f;
uuid.clock_seq_hi_and_reserved |= 0x80;
struct_to_string(uu, &uuid);
}
void
uuid_generate_time(uuid_t uu)
{
struct uuid uuid;
if (uu == NULL)
return;
if (uuid_create(&uuid) < 0) {
uuid_generate_random(uu);
return;
}
struct_to_string(uu, &uuid);
}
void
uuid_generate(uuid_t uu)
{
uuid_generate_random(uu);
}
void
uuid_copy(uuid_t dst, uuid_t src)
{
(void) memcpy(dst, src, UUID_LEN);
}
void
uuid_clear(uuid_t uu)
{
(void) memset(uu, 0, UUID_LEN);
}
static void
uuid_unparse_common(uuid_t uu, char *out, boolean_t upper)
{
struct uuid uuid;
uint16_t clock_seq;
char etheraddr[13];
int index = 0, i;
if (uu == NULL) {
return;
}
string_to_struct(&uuid, uu);
clock_seq = uuid.clock_seq_hi_and_reserved;
clock_seq = (clock_seq << 8) | uuid.clock_seq_low;
for (i = 0; i < 6; i++) {
(void) sprintf(ðeraddr[index++], upper ? "%.2X" : "%.2x",
uuid.node_addr[i]);
index++;
}
etheraddr[index] = '\0';
(void) snprintf(out, 25,
upper ? "%08X-%04X-%04X-%04X-" : "%08x-%04x-%04x-%04x-",
uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq);
(void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH);
}
void
uuid_unparse_upper(uuid_t uu, char *out)
{
uuid_unparse_common(uu, out, B_TRUE);
}
void
uuid_unparse_lower(uuid_t uu, char *out)
{
uuid_unparse_common(uu, out, B_FALSE);
}
void
uuid_unparse(uuid_t uu, char *out)
{
uuid_unparse_common(uu, out, B_FALSE);
}
int
uuid_is_null(uuid_t uu)
{
int i;
uuid_t null_uu;
(void) memset(null_uu, 0, sizeof (uuid_t));
i = memcmp(uu, null_uu, sizeof (uuid_t));
if (i == 0) {
return (1);
} else {
return (0);
}
}
int
uuid_parse(char *in, uuid_t uu)
{
char *ptr, buf[3];
int i;
struct uuid uuid;
uint16_t clock_seq;
if ((strlen(in) != 36) || (uu == NULL) || (in[36] != '\0')) {
return (-1);
}
ptr = in;
for (i = 0; i < 36; i++, ptr++) {
if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
if (*ptr != '-') {
return (-1);
}
} else {
if (!isxdigit(*ptr)) {
return (-1);
}
}
}
uuid.time_low = strtoul(in, NULL, 16);
uuid.time_mid = strtoul(in+9, NULL, 16);
uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
clock_seq = strtoul(in+19, NULL, 16);
uuid.clock_seq_hi_and_reserved = (clock_seq & 0xFF00) >> 8;
uuid.clock_seq_low = (clock_seq & 0xFF);
ptr = in+24;
buf[2] = '\0';
for (i = 0; i < 6; i++) {
buf[0] = *ptr++;
buf[1] = *ptr++;
uuid.node_addr[i] = strtoul(buf, NULL, 16);
}
struct_to_string(uu, &uuid);
return (0);
}
time_t
uuid_time(uuid_t uu, struct timeval *ret_tv)
{
struct uuid uuid;
uint_t high;
struct timeval tv;
u_longlong_t clock_reg;
uint_t tmp;
uint8_t clk;
string_to_struct(&uuid, uu);
tmp = (uuid.time_hi_and_version & 0xF000) >> 12;
clk = uuid.clock_seq_hi_and_reserved;
if ((uu == NULL) || ((tmp & 0x01) != 0x01) || ((clk & 0x80) != 0x80)) {
return (-1);
}
high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
clock_reg = uuid.time_low | ((u_longlong_t)high << 32);
clock_reg -= (((u_longlong_t)0x01B21DD2) << 32) + 0x13814000;
tv.tv_sec = clock_reg / 10000000;
tv.tv_usec = (clock_reg % 10000000) / 10;
if (ret_tv) {
*ret_tv = tv;
}
return (tv.tv_sec);
}