gen_uuid.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * gen_uuid.c --- generate a DCE-compatible uuid
  4. *
  5. * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, and the entire permission notice in its entirety,
  13. * including the disclaimer of warranties.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote
  18. * products derived from this software without specific prior
  19. * written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  24. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  27. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  31. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  32. * DAMAGE.
  33. * %End-Header%
  34. */
  35. #include <unistd.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <fcntl.h>
  39. #include <errno.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <sys/file.h>
  43. #include <sys/time.h>
  44. #ifdef HAVE_SYS_IOCTL_H
  45. #include <sys/ioctl.h>
  46. #endif
  47. #include <sys/socket.h>
  48. #ifdef HAVE_SYS_SOCKIO_H
  49. #include <sys/sockio.h>
  50. #endif
  51. #ifdef HAVE_NET_IF_H
  52. #include <net/if.h>
  53. #endif
  54. #ifdef HAVE_NETINET_IN_H
  55. #include <netinet/in.h>
  56. #endif
  57. #ifdef HAVE_NET_IF_DL_H
  58. #include <net/if_dl.h>
  59. #endif
  60. #include "uuidP.h"
  61. #ifdef HAVE_SRANDOM
  62. #define srand(x) srandom(x)
  63. #define rand() random()
  64. #endif
  65. static int get_random_fd(void)
  66. {
  67. struct timeval tv;
  68. static int fd = -2;
  69. int i;
  70. if (fd == -2) {
  71. gettimeofday(&tv, 0);
  72. fd = open("/dev/urandom", O_RDONLY);
  73. if (fd == -1)
  74. fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
  75. srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
  76. }
  77. /* Crank the random number generator a few times */
  78. gettimeofday(&tv, 0);
  79. for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
  80. rand();
  81. return fd;
  82. }
  83. /*
  84. * Generate a series of random bytes. Use /dev/urandom if possible,
  85. * and if not, use srandom/random.
  86. */
  87. static void get_random_bytes(void *buf, int nbytes)
  88. {
  89. int i, n = nbytes, fd = get_random_fd();
  90. int lose_counter = 0;
  91. unsigned char *cp = (unsigned char *) buf;
  92. if (fd >= 0) {
  93. while (n > 0) {
  94. i = read(fd, cp, n);
  95. if (i <= 0) {
  96. if (lose_counter++ > 16)
  97. break;
  98. continue;
  99. }
  100. n -= i;
  101. cp += i;
  102. lose_counter = 0;
  103. }
  104. }
  105. /*
  106. * We do this all the time, but this is the only source of
  107. * randomness if /dev/random/urandom is out to lunch.
  108. */
  109. for (cp = buf, i = 0; i < nbytes; i++)
  110. *cp++ ^= (rand() >> 7) & 0xFF;
  111. }
  112. /*
  113. * Get the ethernet hardware address, if we can find it...
  114. */
  115. static int get_node_id(unsigned char *node_id)
  116. {
  117. #ifdef HAVE_NET_IF_H
  118. int sd;
  119. struct ifreq ifr, *ifrp;
  120. struct ifconf ifc;
  121. char buf[1024];
  122. int n, i;
  123. unsigned char *a;
  124. #ifdef HAVE_NET_IF_DL_H
  125. struct sockaddr_dl *sdlp;
  126. #endif
  127. /*
  128. * BSD 4.4 defines the size of an ifreq to be
  129. * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
  130. * However, under earlier systems, sa_len isn't present, so the size is
  131. * just sizeof(struct ifreq)
  132. */
  133. #ifdef HAVE_SA_LEN
  134. #ifndef max
  135. #define max(a,b) ((a) > (b) ? (a) : (b))
  136. #endif
  137. #define ifreq_size(i) max(sizeof(struct ifreq),\
  138. sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
  139. #else
  140. #define ifreq_size(i) sizeof(struct ifreq)
  141. #endif /* HAVE_SA_LEN*/
  142. sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  143. if (sd < 0) {
  144. return -1;
  145. }
  146. memset(buf, 0, sizeof(buf));
  147. ifc.ifc_len = sizeof(buf);
  148. ifc.ifc_buf = buf;
  149. if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
  150. close(sd);
  151. return -1;
  152. }
  153. n = ifc.ifc_len;
  154. for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
  155. ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
  156. strncpy_IFNAMSIZ(ifr.ifr_name, ifrp->ifr_name);
  157. #ifdef SIOCGIFHWADDR
  158. if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
  159. continue;
  160. a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
  161. #else
  162. #ifdef SIOCGENADDR
  163. if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
  164. continue;
  165. a = (unsigned char *) ifr.ifr_enaddr;
  166. #else
  167. #ifdef HAVE_NET_IF_DL_H
  168. sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
  169. if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
  170. continue;
  171. a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
  172. #else
  173. /*
  174. * XXX we don't have a way of getting the hardware
  175. * address
  176. */
  177. close(sd);
  178. return 0;
  179. #endif /* HAVE_NET_IF_DL_H */
  180. #endif /* SIOCGENADDR */
  181. #endif /* SIOCGIFHWADDR */
  182. if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
  183. continue;
  184. if (node_id) {
  185. memcpy(node_id, a, 6);
  186. close(sd);
  187. return 1;
  188. }
  189. }
  190. close(sd);
  191. #endif
  192. return 0;
  193. }
  194. /* Assume that the gettimeofday() has microsecond granularity */
  195. #define MAX_ADJUSTMENT 10
  196. static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
  197. {
  198. static int adjustment = 0;
  199. static struct timeval last = {0, 0};
  200. static uint16_t clock_seq;
  201. struct timeval tv;
  202. unsigned long long clock_reg;
  203. try_again:
  204. gettimeofday(&tv, 0);
  205. if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
  206. get_random_bytes(&clock_seq, sizeof(clock_seq));
  207. clock_seq &= 0x3FFF;
  208. last = tv;
  209. last.tv_sec--;
  210. }
  211. if ((tv.tv_sec < last.tv_sec) ||
  212. ((tv.tv_sec == last.tv_sec) &&
  213. (tv.tv_usec < last.tv_usec))) {
  214. clock_seq = (clock_seq+1) & 0x3FFF;
  215. adjustment = 0;
  216. last = tv;
  217. } else if ((tv.tv_sec == last.tv_sec) &&
  218. (tv.tv_usec == last.tv_usec)) {
  219. if (adjustment >= MAX_ADJUSTMENT)
  220. goto try_again;
  221. adjustment++;
  222. } else {
  223. adjustment = 0;
  224. last = tv;
  225. }
  226. clock_reg = tv.tv_usec*10 + adjustment;
  227. clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
  228. clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
  229. *clock_high = clock_reg >> 32;
  230. *clock_low = clock_reg;
  231. *ret_clock_seq = clock_seq;
  232. return 0;
  233. }
  234. void uuid_generate_time(uuid_t out)
  235. {
  236. static unsigned char node_id[6];
  237. static int has_init = 0;
  238. struct uuid uu;
  239. uint32_t clock_mid;
  240. if (!has_init) {
  241. if (get_node_id(node_id) <= 0) {
  242. get_random_bytes(node_id, 6);
  243. /*
  244. * Set multicast bit, to prevent conflicts
  245. * with IEEE 802 addresses obtained from
  246. * network cards
  247. */
  248. node_id[0] |= 0x01;
  249. }
  250. has_init = 1;
  251. }
  252. get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
  253. uu.clock_seq |= 0x8000;
  254. uu.time_mid = (uint16_t) clock_mid;
  255. uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
  256. memcpy(uu.node, node_id, 6);
  257. uuid_pack(&uu, out);
  258. }
  259. void uuid_generate_random(uuid_t out)
  260. {
  261. uuid_t buf;
  262. struct uuid uu;
  263. get_random_bytes(buf, sizeof(buf));
  264. uuid_unpack(buf, &uu);
  265. uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
  266. uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
  267. uuid_pack(&uu, out);
  268. }
  269. /*
  270. * This is the generic front-end to uuid_generate_random and
  271. * uuid_generate_time. It uses uuid_generate_random only if
  272. * /dev/urandom is available, since otherwise we won't have
  273. * high-quality randomness.
  274. */
  275. void uuid_generate(uuid_t out)
  276. {
  277. if (get_random_fd() >= 0)
  278. uuid_generate_random(out);
  279. else
  280. uuid_generate_time(out);
  281. }