uuid.c revision 6be356c5780a1ccb886bba08d6eb56b61f021564
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The copyright in this file is taken from the original Leach & Salz
* UUID specification, from which this implementation is derived.
*/
/*
* Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
* Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
* Digital Equipment Corporation, Maynard, Mass. Copyright (c) 1998
* Microsoft. To anyone who acknowledges that this file is provided
* "AS IS" without any express or implied warranty: permission to use,
* copy, modify, and distribute this file for any purpose is hereby
* granted without fee, provided that the above copyright notices and
* this notice appears in all source code copies, and that none of the
* names of Open Software Foundation, Inc., Hewlett-Packard Company,
* or Digital Equipment Corporation be used in advertising or
* publicity pertaining to distribution of the software without
* specific, written prior permission. Neither Open Software
* Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
* Equipment Corporation makes any representations about the
* suitability of this software for any purpose.
*/
/*
* Module: uuid.c
*
* Description: This module is the workhorse for generating abstract
* UUIDs. It delegates system-specific tasks (such
* as obtaining the node identifier or system time)
* to the sysdep module.
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fcntl.h>
#include <unistd.h>
#include <thread.h>
#include <synch.h>
#include "uuid_misc.h"
#define STATE_LOCATION "/var/sadm/system/uuid_state"
#define URANDOM_PATH "/dev/urandom"
#define MAX_RETRY 8
#define VER1_MASK 0xefff
uint16_t _get_random(void);
void _get_current_time(uuid_time_t *);
int get_ethernet_address(uuid_node_t *);
/*
* local functions
*/
static int _lock_state(char *);
static void _unlock_state(int);
uuid_node_t *);
static void fill_random_bytes(uchar_t *, int);
static int uuid_create(struct uuid *);
static void gen_ethernet_address(uuid_node_t *);
/*
* Name: uuid_create.
*
* Description: Generates a uuid based on Version 1 format
*
* Returns: 0 on success, -1 on Error
*/
static int
{
int locked_state_fd;
int non_unique = 0;
if (mutex_lock(&ulock) != 0) {
return (-1);
}
/*
* acquire system wide lock so we're alone
*/
if (locked_state_fd < 0) {
non_unique++;
} else {
/* read saved state from disk */
&last_node);
}
if (clockseq == 0) {
/* couldn't read clock sequence; generate a random one */
clockseq = _get_random();
non_unique++;
}
clockseq++;
}
/*
* get current time
*/
/*
* If timestamp is not set or is not in the past,
* increment clock sequence.
*/
clockseq++;
}
if (non_unique)
/*
* stuff fields into the UUID
*/
if ((locked_state_fd >= 0) &&
system_node) == -1)) {
(void) mutex_unlock(&ulock);
return (-1);
}
/*
* Unlock system-wide lock
*/
(void) mutex_unlock(&ulock);
return (0);
}
/*
* Name: gen_ethernet_address
*
* Description: Fills system_node with Ethernet address if available,
* else fills random numbers
*
* Returns: Nothing
*/
static void
{
if (get_ethernet_address(system_node) != 0) {
/*
* use 8:0:20 with the multicast bit set
* to avoid namespace collisions.
*/
}
}
/*
* Name: _format_uuid
*
* Description: Formats a UUID, given the clock_seq timestamp,
* and node address. Fills in passed-in pointer with
* the resulting uuid.
*
* Returns: None.
*/
static void
{
/*
* First set up the first 60 bits from the timestamp
*/
0x0FFF);
/*
* This is version 1, so say so in the UUID version field (4 bits)
*/
/*
* Now do the clock sequence
*/
/*
* We must save the most-significant 2 bits for the reserved field
*/
/*
* The variant for this format is the 2 high bits set to 10,
* so here it is
*/
/*
* write result to passed-in pointer
*/
}
/*
* Name: _read_state
*
* Description: Reads non-volatile state from a (possibly) saved statefile.
* For each non-null pointer passed-in, the corresponding
* information from the statefile is filled in.
* the resulting uuid.
*
* Returns: Nothing.
*/
static void
{
*timestamp = 0;
*clockseq = 0;
sizeof (uuid_state_t)) {
/* This file is being accessed the first time */
}
}
/*
* Name: _write_state
*
* Description: Writes non-volatile state from the passed-in information.
*
* Returns: -1 on error, 0 otherwise.
*/
static int
{
/*
* seek to beginning of file and write data
*/
return (0);
}
}
return (-1);
}
/*
* Name: _uuid_print
*
* Description: Prints a nicely-formatted uuid to stdout.
*
* Returns: None.
*
*/
void
uuid_print(struct uuid u)
{
int i;
u.clock_seq_low);
for (i = 0; i < 6; i++)
(void) printf("\n");
}
/*
* Name: _lock_state
*
* Description: Locks down the statefile, by first creating the file
* if it doesn't exist.
*
* Returns: A non-negative file descriptor referring to the locked
* or -1 otherwise.
*/
static int
_lock_state(char *loc)
{
int fd;
if (fd < 0) {
return (-1);
}
/*
* File could not be locked, bail
*/
return (-1);
}
return (fd);
}
/*
* Name: _unlock_state
*
* Description: Unlocks a locked statefile, and close()'s the file.
*
* Returns: Nothing.
*/
void
_unlock_state(int fd)
{
}
/*
* Name: fill_random_bytes
*
* Description: fills buf with random numbers - nbytes is the number of bytes
* if that fails for some reason, it retries MAX_RETRY times. If
* it still fails then it uses srand48(3C)
*
* Returns: Nothing.
*/
static void
{
if (fd >= 0) {
while (nbytes > 0) {
continue;
}
if (i <= 0) {
break;
continue;
}
nbytes -= i;
buf += i;
retries = 0;
}
if (nbytes == 0) {
return;
}
}
for (i = 0; i < nbytes; i++) {
}
if (fd >= 0) {
}
}
/*
* Name: struct_to_string
*
* Description: Unpacks the structure members in "struct uuid" to a char
* string "uuid_t".
*
* Returns: Nothing.
*/
void
{
tmp >>= 8;
tmp >>= 8;
tmp >>= 8;
tmp >>= 8;
tmp >>= 8;
}
/*
* Name: string_to_struct
*
* Description: Packs the values in the "uuid_t" string into "struct uuid".
*
* Returns: Nothing
*/
void
{
}
/*
* Name: uuid_generate_random
*
* Description: Generates UUID based on DCE Version 4
*
* Returns: Nothing. uu contains the newly generated UUID
*/
void
{
return;
/*
* This is version 4, so say so in the UUID version field (4 bits)
*/
/*
* we don't want the bit 1 to be set also which is for version 1
*/
/*
* The variant for this format is the 2 high bits set to 10,
* so here it is
*/
/*
* Set MSB of Ethernet address to 1 to indicate that it was generated
* randomly
*/
}
/*
* Name: uuid_generate_time
*
* Description: Generates UUID based on DCE Version 1.
*
* Returns: Nothing. uu contains the newly generated UUID.
*/
void
{
return;
return;
}
}
/*
* Name: uuid_generate
*
* Description: Creates a new UUID. The uuid will be generated based on
* calling uuid_generate_random. If it failed to generate UUID
* then uuid_generate will call uuid_generate_time.
*
* Returns: Nothing. uu contains the newly generated UUID.
*/
void
{
int fd;
return;
}
if (fd >= 0) {
} else {
(void) uuid_generate_time(uu);
}
}
/*
* Name: uuid_copy
*
* Description: The uuid_copy function copies the UUID variable src to dst
*
* Returns: Nothing
*/
void
{
}
/*
* Name: uuid_clear
*
* Description: The uuid_clear function sets the value of the supplied uuid
* variable uu, to the NULL value.
*
* Returns: Nothing
*/
void
{
}
/*
* Name: uuid_unparse
*
* Description: This function converts the supplied UUID uu from the internal
* binary format into a 36-byte string (plus trailing null char)
* and stores this value in the character string pointed to by out
*
* Returns: Nothing.
*/
void
{
char etheraddr[13];
int index = 0, i;
/* basic sanity checking */
return;
}
/* XXX user should have allocated enough memory */
/*
* if (strlen(out) < UUID_PRINTABLE_STRING_LENGTH) {
* return;
* }
*/
for (i = 0; i < 6; i++) {
index++;
}
}
/*
* Name: uuid_is_null
*
* Description: The uuid_is_null function compares the value of the supplied
* UUID variable uu to the NULL value. If the value is equal
* to the NULL UUID, 1 is returned, otherwise 0 is returned.
*
* Returns: 0 if uu is NOT null, 1 if uu is NULL.
*/
int
{
int i;
if (i == 0) {
/* uu is NULL uuid */
return (1);
} else {
return (0);
}
}
/*
* Name: uuid_parse
*
* Description: uuid_parse converts the UUID string given by 'in' into the
* internal uuid_t format. The input UUID is a string of the form
* cefa7a9c-1dd2-11b2-8350-880020adbeef in printf(3C) format.
* Upon successfully parsing the input string, UUID is stored
* in the location pointed to by uu
*
* Returns: 0 if the UUID is successfully stored, -1 otherwise.
*/
int
{
int i;
/* do some sanity checking */
return (-1);
}
for (i = 0; i < 36; i++, ptr++) {
if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
if (*ptr != '-') {
return (-1);
}
} else {
return (-1);
}
}
}
for (i = 0; i < 6; i++) {
}
return (0);
}
/*
* Name: uuid_time
*
* Description: uuid_time extracts the time at which the supplied UUID uu
* was created. This function can only extract the creation
* time for UUIDs created with the uuid_generate_time function.
* The time at which the UUID was created, in seconds and
* microseconds since the epoch is stored in the location
* pointed to by ret_tv.
*
* Returns: The time at which the UUID was created, in seconds since
* January 1, 1970 GMT (the epoch). -1 otherwise.
*/
{
/* check if uu is NULL, Version = 1 of DCE and Variant = 0b10x */
return (-1);
}
if (ret_tv) {
}
}