2 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright 2000, John E. Schimmel, All rights reserved.
7 * This software is not subject to any license of Mirapoint, Inc.
9 * This is free software; you can redistribute and use it
10 * under the same terms as OpenLDAP itself.
12 /* Adapted for incorporatation into OpenLDAP by Kurt Zeilenga */
15 ** Sorry this file is so scary, but it needs to run on a wide range of
16 ** platforms. The only exported routine is lutil_uuidstr() which is all
17 ** that LDAP cares about. It generates a new uuid and returns it in
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_UUID_H
26 # include <sys/uuid.h>
28 # include <ac/socket.h>
29 # include <sys/sockio.h>
30 # include <net/if_types.h>
32 # include <net/if_dl.h>
33 # ifdef HAVE_SYS_SYSCTL_H
34 # include <sys/sysctl.h>
35 # include <net/route.h>
39 /* 100 usec intervals from 10/10/1582 to 1/1/1970 */
40 #define UUID_TPLUS 0x01B21DD2138140LL
42 #ifndef HAVE_SYS_UUID_H
43 static unsigned char *
46 static unsigned char zero[6];
47 static unsigned char eaddr[6];
49 #ifdef HAVE_SYS_SYSCTL_H
52 char *buf, *next, *lim;
53 struct if_msghdr *ifm;
54 struct sockaddr_dl *sdl;
56 if (memcmp(eaddr, zero, sizeof(eaddr))) {
64 mib[4] = NET_RT_IFLIST;
67 if (sysctl(mib, sizeof(mib), NULL, &needed, NULL, 0) < 0) {
72 if( buf == NULL ) return NULL;
74 if (sysctl(mib, sizeof(mib), buf, &needed, NULL, 0) < 0) {
80 for (next = buf; next < lim; next += ifm->ifm_msglen) {
81 ifm = (struct if_msghdr *)next;
82 sdl = (struct sockaddr_dl *)(ifm + 1);
84 if ( sdl->sdl_family != AF_LINK || sdl->sdl_alen == 6 ) {
86 (unsigned char *)sdl->sdl_data + sdl->sdl_nlen,
96 #elif defined (SIOCGIFADDR)
97 char buf[sizeof(struct ifreq) * 32];
101 struct sockaddr_dl *sdl;
105 if (memcmp(eaddr, zero, sizeof(eaddr))) {
109 s = socket( AF_INET, SOCK_DGRAM, 0 );
114 ifc.ifc_len = sizeof( buf );
116 memset( buf, 0, sizeof( buf ) );
118 i = ioctl( s, SIOCGIFCONF, (char *)&ifc );
125 for ( i = 0; i < ifc.ifc_len; ) {
126 ifr = (struct ifreq *)&ifc.ifc_buf[i];
129 if ( sa->sa_len > sizeof( ifr->ifr_addr ) ) {
130 i += sizeof( ifr->ifr_name ) + sa->sa_len;
135 if ( sa->sa_family != AF_LINK ) {
139 sdl = (struct sockaddr_dl *)sa;
141 if ( sdl->sdl_alen == 6 ) {
143 (unsigned char *)sdl->sdl_data + sdl->sdl_nlen,
152 if (memcmp(eaddr, zero, sizeof(eaddr)) == 0) {
153 /* XXX - who knows? */
154 lutil_entropy( eaddr, sizeof(eaddr) );
163 ** All we really care about is an ISO UUID string. The format of a UUID is:
165 ** time_low 0-3 low field of the timestamp
166 ** time_mid 4-5 middle field of timestamp
167 ** time_hi_and_version 6-7 high field of timestamp and
169 ** clock_seq_hi_and_resv 8 high field of clock sequence
171 ** clock_seq_low 9 low field of clock sequence
172 ** node 10-15 spacially unique identifier
174 ** We use DCE version one, and the DCE variant. Our unique identifier is
175 ** the first ethernet address on the system.
178 lutil_uuidstr( char *buf, size_t len )
180 #ifdef HAVE_SYS_UUID_H
185 uuid_create( &uu, &rc );
186 if ( rc != uuid_s_ok ) {
190 uuid_to_str( &uu, &s, &rc );
191 if ( rc != uuid_s_ok ) {
194 if ( strlen( s ) >= len ) {
199 strncpy( buf, s, len );
206 unsigned long long tl;
208 unsigned short t2, t3, s1, n1, n2, n3;
212 * Theoretically we should delay if seq wraps within 100usec but for now
213 * systems are not fast enough to worry about it.
215 static int inited = 0;
216 static unsigned short seq;
219 lutil_entropy( &seq, sizeof(seq) );
223 #ifdef HAVE_GETTIMEOFDAY
224 gettimeofday( &tv, 0 );
230 tl = ( tv.tv_sec * 10000000LL ) + ( tv.tv_usec * 10LL ) + UUID_TPLUS;
233 t1 = tl & 0xffffffff; /* time_low */
234 t2 = ( tl >> 32 ) & 0xffff; /* time_mid */
235 t3 = ( tl >> 48 ) & 0x0fff | 0x1000; /* time_hi_and_version */
236 s1 = ( ++seq & 0x1fff ) | 0x8000; /* clock_seq_and_reserved */
238 t1 = snprintf( buf, len,
239 "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
240 t1, (unsigned) t2, (unsigned) t3, (unsigned) s1,
241 (unsigned) nl[0], (unsigned) nl[1],
242 (unsigned) nl[2], (unsigned) nl[3],
243 (unsigned) nl[4], (unsigned) nl[5] );
245 return (t1 < len) ? 1 : 0;
251 main(int argc, char **argv)
253 char buf1[8], buf2[64];
255 #ifndef HAVE_SYS_UUID_H
256 unsigned char *p = lutil_eaddr();
259 printf( "Ethernet Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
260 (unsigned) p[0], (unsigned) p[1], (unsigned) p[2],
261 (unsigned) p[3], (unsigned) p[4], (unsigned) p[5]);
265 if ( lutil_uuidstr( buf1, sizeof( buf1 ) ) ) {
266 printf( "UUID: %s\n", buf1 );
268 fprintf( stderr, "too short: %ld\n", (long) sizeof( buf1 ) );
271 if ( lutil_uuidstr( buf2, sizeof( buf2 ) ) ) {
272 printf( "UUID: %s\n", buf2 );
274 fprintf( stderr, "too short: %ld\n", (long) sizeof( buf2 ) );
277 if ( lutil_uuidstr( buf2, sizeof( buf2 ) ) ) {
278 printf( "UUID: %s\n", buf2 );
280 fprintf( stderr, "too short: %ld\n", (long) sizeof( buf2 ) );