]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/uuid/gen_uuid.c
2 * gen_uuid.c --- generate a DCE-compatible uuid
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU
8 * Library General Public License.
13 * Force inclusion of SVID stuff since we need it if we're compiling in
27 #include <sys/types.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
37 #ifdef HAVE_SYS_SOCKIO_H
38 #include <sys/sockio.h>
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
50 #define srand(x) srandom(x)
51 #define rand() random()
54 static int get_random_fd(void)
62 fd
= open("/dev/urandom", O_RDONLY
);
64 fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
);
65 srand((getpid() << 16) ^ getuid() ^ tv
.tv_sec
^ tv
.tv_usec
);
67 /* Crank the random number generator a few times */
69 for (i
= (tv
.tv_sec
^ tv
.tv_usec
) & 0x1F; i
> 0; i
--)
76 * Generate a series of random bytes. Use /dev/urandom if possible,
77 * and if not, use srandom/random.
79 static void get_random_bytes(void *buf
, int nbytes
)
81 int i
, n
= nbytes
, fd
= get_random_fd();
83 unsigned char *cp
= (unsigned char *) buf
;
89 if (lose_counter
++ > 16)
100 * We do this all the time, but this is the only source of
101 * randomness if /dev/random/urandom is out to lunch.
103 for (cp
= buf
, i
= 0; i
< nbytes
; i
++)
104 *cp
++ ^= (rand() >> 7) & 0xFF;
109 * Get the ethernet hardware address, if we can find it...
111 static int get_node_id(unsigned char *node_id
)
115 struct ifreq ifr
, *ifrp
;
122 * BSD 4.4 defines the size of an ifreq to be
123 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
124 * However, under earlier systems, sa_len isn't present, so the size is
125 * just sizeof(struct ifreq)
129 #define max(a,b) ((a) > (b) ? (a) : (b))
131 #define ifreq_size(i) max(sizeof(struct ifreq),\
132 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
134 #define ifreq_size(i) sizeof(struct ifreq)
135 #endif /* HAVE_SA_LEN*/
137 sd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
141 memset(buf
, 0, sizeof(buf
));
142 ifc
.ifc_len
= sizeof(buf
);
144 if (ioctl (sd
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
149 for (i
= 0; i
< n
; i
+= ifreq_size(*ifr
) ) {
150 ifrp
= (struct ifreq
*)((char *) ifc
.ifc_buf
+i
);
151 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
, IFNAMSIZ
);
153 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) < 0)
155 a
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
158 if (ioctl(sd
, SIOCGENADDR
, &ifr
) < 0)
160 a
= (unsigned char *) ifr
.ifr_enaddr
;
163 * XXX we don't have a way of getting the hardware
168 #endif /* SIOCGENADDR */
169 #endif /* SIOCGIFHWADDR */
170 if (!a
[0] && !a
[1] && !a
[2] && !a
[3] && !a
[4] && !a
[5])
173 memcpy(node_id
, a
, 6);
183 /* Assume that the gettimeofday() has microsecond granularity */
184 #define MAX_ADJUSTMENT 10
186 static int get_clock(__u32
*clock_high
, __u32
*clock_low
, __u16
*ret_clock_seq
)
188 static int adjustment
= 0;
189 static struct timeval last
= {0, 0};
190 static __u16 clock_seq
;
192 unsigned long long clock_reg
;
195 gettimeofday(&tv
, 0);
196 if ((last
.tv_sec
== 0) && (last
.tv_usec
== 0)) {
197 get_random_bytes(&clock_seq
, sizeof(clock_seq
));
202 if ((tv
.tv_sec
< last
.tv_sec
) ||
203 ((tv
.tv_sec
== last
.tv_sec
) &&
204 (tv
.tv_usec
< last
.tv_usec
))) {
205 clock_seq
= (clock_seq
+1) & 0x1FFF;
208 } else if ((tv
.tv_sec
== last
.tv_sec
) &&
209 (tv
.tv_usec
== last
.tv_usec
)) {
210 if (adjustment
>= MAX_ADJUSTMENT
)
218 clock_reg
= tv
.tv_usec
*10 + adjustment
;
219 clock_reg
+= ((unsigned long long) tv
.tv_sec
)*10000000;
220 clock_reg
+= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
222 *clock_high
= clock_reg
>> 32;
223 *clock_low
= clock_reg
;
224 *ret_clock_seq
= clock_seq
;
228 void uuid_generate_time(uuid_t out
)
230 static unsigned char node_id
[6];
231 static int has_init
= 0;
236 if (get_node_id(node_id
) <= 0) {
237 get_random_bytes(node_id
, 6);
239 * Set multicast bit, to prevent conflicts
240 * with IEEE 802 addresses obtained from
247 get_clock(&clock_mid
, &uu
.time_low
, &uu
.clock_seq
);
248 uu
.clock_seq
|= 0x8000;
249 uu
.time_mid
= (__u16
) clock_mid
;
250 uu
.time_hi_and_version
= (clock_mid
>> 16) | 0x1000;
251 memcpy(uu
.node
, node_id
, 6);
255 void uuid_generate_random(uuid_t out
)
260 get_random_bytes(buf
, sizeof(buf
));
261 uuid_unpack(buf
, &uu
);
263 uu
.clock_seq
= (uu
.clock_seq
& 0x3FFF) | 0x8000;
264 uu
.time_hi_and_version
= (uu
.time_hi_and_version
& 0x0FFF) | 0x4000;
269 * This is the generic front-end to uuid_generate_random and
270 * uuid_generate_time. It uses uuid_generate_random only if
271 * /dev/urandom is available, since otherwise we won't have
272 * high-quality randomness.
274 void uuid_generate(uuid_t out
)
276 if (get_random_fd() >= 0)
277 uuid_generate_random(out
);
279 uuid_generate_time(out
);