gen_uuid.c 7.7 KB

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