Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / uuid / gen_uuid.c
1 /*
2  * gen_uuid.c --- generate a DCE-compatible uuid
3  */
4
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #ifdef HAVE_STDLIB_H
9 #include <stdlib.h>
10 #endif
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/stat.h>
16 #include <sys/file.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #ifdef HAVE_SYS_SOCKIO_H
20 #include <sys/sockio.h>
21 #endif
22 #ifdef HAVE_NET_IF_H
23 #include <net/if.h>
24 #endif
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28
29 #include "uuidP.h"
30
31 #ifdef HAVE_SRANDOM
32 #define srand(x)        srandom(x)
33 #define rand()          random()
34 #endif
35
36 /*
37  * Generate a series of random bytes.  Use /dev/urandom if possible,
38  * and if not, use srandom/random.
39  */
40 static void get_random_bytes(void *buf, int nbytes)
41 {
42         static int fd = -2;
43         int i;
44         char *cp = (char *) buf;
45
46         if (fd == -2) {
47                 fd = open("/dev/urandom", O_RDONLY);
48                 srand((getpid() << 16) ^ getuid() ^ time(0));
49         }
50         if (fd > 0) {
51                 i = read(fd, cp, nbytes);
52                 if (i == nbytes)
53                         return;
54                 if (i > 0) {
55                         nbytes -= i;
56                         cp += i;
57                 }
58         }
59         for (i=0; i < nbytes; i++)
60                 *cp++ = rand() & 0xFF;
61 }
62
63 /*
64  * Get the ethernet hardware address, if we can find it...
65  */
66 static int get_node_id(unsigned char *node_id)
67 {
68 #ifdef HAVE_NET_IF_H
69         int             sd;
70         struct ifreq    ifr, *ifrp;
71         struct ifconf   ifc;
72         char buf[1024];
73         int             n, i;
74         unsigned char   *a;
75         
76 /*
77  * BSD 4.4 defines the size of an ifreq to be
78  * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
79  * However, under earlier systems, sa_len isn't present, so the size is 
80  * just sizeof(struct ifreq)
81  */
82 #ifdef HAVE_SA_LEN
83 #ifndef max
84 #define max(a,b) ((a) > (b) ? (a) : (b))
85 #endif
86 #define ifreq_size(i) max(sizeof(struct ifreq),\
87      sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
88 #else
89 #define ifreq_size(i) sizeof(struct ifreq)
90 #endif /* HAVE_SA_LEN*/
91
92         sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
93         if (sd < 0) {
94                 return -1;
95         }
96         memset(buf, 0, sizeof(buf));
97         ifc.ifc_len = sizeof(buf);
98         ifc.ifc_buf = buf;
99         if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
100                 close(sd);
101                 return -1;
102         }
103         n = ifc.ifc_len;
104         for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
105                 ifrp = (struct ifreq *)((caddr_t) ifc.ifc_buf+i);
106                 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
107 #ifdef SIOCGIFHWADDR
108                 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
109                         continue;
110                 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
111 #else
112 #ifdef SIOCGENADDR
113                 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
114                         continue;
115                 a = (unsigned char *) ifr.ifr_enaddr;
116 #else
117                 /*
118                  * XXX we don't have a way of getting the hardware
119                  * address
120                  */
121                 close(sd);
122                 return 0;
123 #endif /* SIOCGENADDR */
124 #endif /* SIOCGIFHWADDR */
125                 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
126                         continue;
127                 if (node_id) {
128                         memcpy(node_id, a, 6);
129                         close(sd);
130                         return 1;
131                 }
132         }
133         close(sd);
134 #endif
135         return 0;
136 }
137
138 /* Assume that the gettimeofday() has microsecond granularity */
139 #define MAX_ADJUSTMENT 10
140
141 static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq)
142 {
143         static int                      adjustment = 0;
144         static struct timeval           last = {0, 0};
145         static __u16                    clock_seq;
146         struct timeval                  tv;
147         unsigned long long              clock;
148         
149 try_again:
150         gettimeofday(&tv, 0);
151         if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
152                 get_random_bytes(&clock_seq, sizeof(clock_seq));
153                 clock_seq &= 0x1FFF;
154                 last = tv;
155                 last.tv_sec--;
156         }
157         if ((tv.tv_sec < last.tv_sec) ||
158             ((tv.tv_sec == last.tv_sec) &&
159              (tv.tv_usec < last.tv_usec))) {
160                 clock_seq = (clock_seq+1) & 0x1FFF;
161                 adjustment = 0;
162         } else if ((tv.tv_sec == last.tv_sec) &&
163             (tv.tv_usec == last.tv_usec)) {
164                 if (adjustment >= MAX_ADJUSTMENT)
165                         goto try_again;
166                 adjustment++;
167         } else
168                 adjustment = 0;
169         
170         clock = tv.tv_usec*10 + adjustment;
171         clock += ((unsigned long long) tv.tv_sec)*10000000;
172         clock += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
173
174         *clock_high = clock >> 32;
175         *clock_low = clock;
176         *ret_clock_seq = clock_seq;
177         return 0;
178 }
179
180 void uuid_generate(uuid_t out)
181 {
182         static unsigned char node_id[6];
183         static int has_init = 0;
184         struct uuid uu;
185         __u32   clock_mid;
186
187         if (!has_init) {
188                 if (get_node_id(node_id) <= 0)
189                         get_random_bytes(node_id, 6);
190                 has_init = 1;
191         }
192         get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
193         uu.clock_seq |= 0x8000;
194         uu.time_mid = (__u16) clock_mid;
195         uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
196         memcpy(uu.node, node_id, 6);
197         uuid_pack(&uu, out);
198 }