17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5cc1a9a89SRafael Vanoni Polanczyk * Common Development and Distribution License (the "License"). 6cc1a9a89SRafael Vanoni Polanczyk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2178b6ed60Scraigm 227c478bd9Sstevel@tonic-gate /* 23cc1a9a89SRafael Vanoni Polanczyk * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*9d12795fSRobert Mustacchi * Copyright (c) 2015, Joyent, Inc. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * The copyright in this file is taken from the original Leach 307c478bd9Sstevel@tonic-gate * & Salz UUID specification, from which this implementation 317c478bd9Sstevel@tonic-gate * is derived. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc. 367c478bd9Sstevel@tonic-gate * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. & 377c478bd9Sstevel@tonic-gate * Digital Equipment Corporation, Maynard, Mass. Copyright (c) 1998 387c478bd9Sstevel@tonic-gate * Microsoft. To anyone who acknowledges that this file is provided 397c478bd9Sstevel@tonic-gate * "AS IS" without any express or implied warranty: permission to use, 407c478bd9Sstevel@tonic-gate * copy, modify, and distribute this file for any purpose is hereby 417c478bd9Sstevel@tonic-gate * granted without fee, provided that the above copyright notices and 427c478bd9Sstevel@tonic-gate * this notice appears in all source code copies, and that none of the 437c478bd9Sstevel@tonic-gate * names of Open Software Foundation, Inc., Hewlett-Packard Company, 447c478bd9Sstevel@tonic-gate * or Digital Equipment Corporation be used in advertising or 457c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the software without 467c478bd9Sstevel@tonic-gate * specific, written prior permission. Neither Open Software 477c478bd9Sstevel@tonic-gate * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital 487c478bd9Sstevel@tonic-gate * Equipment Corporation makes any representations about the 497c478bd9Sstevel@tonic-gate * suitability of this software for any purpose. 507c478bd9Sstevel@tonic-gate */ 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #include <uuid/uuid.h> 537c478bd9Sstevel@tonic-gate #include <stdlib.h> 547c478bd9Sstevel@tonic-gate #include <strings.h> 557c478bd9Sstevel@tonic-gate #include "uuid_misc.h" 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define UUCMP(u1, u2) if (u1 != u2) return ((u1 < u2) ? -1 : 1) 587c478bd9Sstevel@tonic-gate #define UUIDS_PER_TOD_CALL 10 /* tv_usec is multiplied by 10 */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate void struct_to_string(uuid_t, struct uuid *); 617c478bd9Sstevel@tonic-gate void string_to_struct(struct uuid *, uuid_t); 627c478bd9Sstevel@tonic-gate void get_system_time(uuid_time_t *); 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* 65cc1a9a89SRafael Vanoni Polanczyk * Name: get_current_time 667c478bd9Sstevel@tonic-gate * 677c478bd9Sstevel@tonic-gate * Description: get-current_time -- get time as 60 bit 100ns ticks 687c478bd9Sstevel@tonic-gate * since the beginning of unix time. 697c478bd9Sstevel@tonic-gate * Compensate for the fact that real clock resolution is 707c478bd9Sstevel@tonic-gate * less than 100ns. 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * Returns: None. 737c478bd9Sstevel@tonic-gate * 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate void 76cc1a9a89SRafael Vanoni Polanczyk get_current_time(uuid_time_t *timestamp) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate uuid_time_t time_now; 797c478bd9Sstevel@tonic-gate static uuid_time_t time_last = 0; 807c478bd9Sstevel@tonic-gate static uint16_t uuids_this_tick = 0; 817c478bd9Sstevel@tonic-gate int done; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate done = 0; 847c478bd9Sstevel@tonic-gate while (!done) { 857c478bd9Sstevel@tonic-gate get_system_time(&time_now); 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * if clock reading changed since last UUID generated... 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate if (time_last != time_now) { 917c478bd9Sstevel@tonic-gate /* 927c478bd9Sstevel@tonic-gate * reset count of uuids generated with 937c478bd9Sstevel@tonic-gate * this clock reading 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate uuids_this_tick = 0; 967c478bd9Sstevel@tonic-gate done = 1; 977c478bd9Sstevel@tonic-gate } else { 987c478bd9Sstevel@tonic-gate uuids_this_tick++; 997c478bd9Sstevel@tonic-gate if (uuids_this_tick < UUIDS_PER_TOD_CALL) 1007c478bd9Sstevel@tonic-gate done = 1; 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate /* 1037c478bd9Sstevel@tonic-gate * too many UUIDs for this gettimeofday call; spin 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate time_last = time_now; 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * add the count of uuids to low order bits of the clock reading 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate *timestamp = time_now + uuids_this_tick; 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* 114cc1a9a89SRafael Vanoni Polanczyk * Name: get_random 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * Description: Gets a random number. 1177c478bd9Sstevel@tonic-gate * 1187c478bd9Sstevel@tonic-gate * Returns: nbytes of random information. 1197c478bd9Sstevel@tonic-gate * 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate uint16_t 122cc1a9a89SRafael Vanoni Polanczyk get_random(void) 1237c478bd9Sstevel@tonic-gate { 124*9d12795fSRobert Mustacchi return (arc4random_uniform(UINT16_MAX)); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Name: uuid_compare 1297c478bd9Sstevel@tonic-gate * 1307c478bd9Sstevel@tonic-gate * Description: Compares 2 uuid strings 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * Returns: -1 if u1 < u2, 1 if u1 > u2 and 0 if both are equal 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate int 1357c478bd9Sstevel@tonic-gate uuid_compare(uuid_t uu1, uuid_t uu2) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate struct uuid uuid1, uuid2; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate string_to_struct(&uuid1, uu1); 1417c478bd9Sstevel@tonic-gate string_to_struct(&uuid2, uu2); 1427c478bd9Sstevel@tonic-gate UUCMP(uuid1.time_low, uuid2.time_low); 1437c478bd9Sstevel@tonic-gate UUCMP(uuid1.time_mid, uuid2.time_mid); 1447c478bd9Sstevel@tonic-gate UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version); 1457c478bd9Sstevel@tonic-gate UUCMP(uuid1.clock_seq_hi_and_reserved, uuid2.clock_seq_hi_and_reserved); 1467c478bd9Sstevel@tonic-gate UUCMP(uuid1.clock_seq_low, uuid2.clock_seq_low); 1477c478bd9Sstevel@tonic-gate return (memcmp(uuid1.node_addr, uuid2.node_addr, 6)); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Name: get_system_time 1527c478bd9Sstevel@tonic-gate * 1537c478bd9Sstevel@tonic-gate * Description: system dependent call to get the current system time. 1547c478bd9Sstevel@tonic-gate * Returned as 100ns ticks since Oct 15, 1582, but 1557c478bd9Sstevel@tonic-gate * resolution may be less than 100ns. 1567c478bd9Sstevel@tonic-gate * 1577c478bd9Sstevel@tonic-gate * Returns: None 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate void 1607c478bd9Sstevel@tonic-gate get_system_time(uuid_time_t *uuid_time) 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate struct timeval tp; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate (void) gettimeofday(&tp, (struct timezone *)0); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * Offset between UUID formatted times and Unix formatted times. 1687c478bd9Sstevel@tonic-gate * UUID UTC base time is October 15, 1582. 1697c478bd9Sstevel@tonic-gate * Unix base time is January 1, 1970. 1707c478bd9Sstevel@tonic-gate */ 1717c478bd9Sstevel@tonic-gate *uuid_time = (uint64_t)tp.tv_sec * 10000000; 1727c478bd9Sstevel@tonic-gate *uuid_time += tp.tv_usec * 10; 17378b6ed60Scraigm *uuid_time += 0x01B21DD213814000ULL; 1747c478bd9Sstevel@tonic-gate } 175