gen_uuid.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. return;
  112. }
  113. /*
  114. * Get the ethernet hardware address, if we can find it...
  115. */
  116. static int get_node_id(unsigned char *node_id)
  117. {
  118. #ifdef HAVE_NET_IF_H
  119. int sd;
  120. struct ifreq ifr, *ifrp;
  121. struct ifconf ifc;
  122. char buf[1024];
  123. int n, i;
  124. unsigned char *a;
  125. #ifdef HAVE_NET_IF_DL_H
  126. struct sockaddr_dl *sdlp;
  127. #endif
  128. /*
  129. * BSD 4.4 defines the size of an ifreq to be
  130. * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
  131. * However, under earlier systems, sa_len isn't present, so the size is
  132. * just sizeof(struct ifreq)
  133. */
  134. #ifdef HAVE_SA_LEN
  135. #ifndef max
  136. #define max(a,b) ((a) > (b) ? (a) : (b))
  137. #endif
  138. #define ifreq_size(i) max(sizeof(struct ifreq),\
  139. sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
  140. #else
  141. #define ifreq_size(i) sizeof(struct ifreq)
  142. #endif /* HAVE_SA_LEN*/
  143. sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  144. if (sd < 0) {
  145. return -1;
  146. }
  147. memset(buf, 0, sizeof(buf));
  148. ifc.ifc_len = sizeof(buf);
  149. ifc.ifc_buf = buf;
  150. if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
  151. close(sd);
  152. return -1;
  153. }
  154. n = ifc.ifc_len;
  155. for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
  156. ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
  157. strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
  158. #ifdef SIOCGIFHWADDR
  159. if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
  160. continue;
  161. a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
  162. #else
  163. #ifdef SIOCGENADDR
  164. if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
  165. continue;
  166. a = (unsigned char *) ifr.ifr_enaddr;
  167. #else
  168. #ifdef HAVE_NET_IF_DL_H
  169. sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
  170. if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
  171. continue;
  172. a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
  173. #else
  174. /*
  175. * XXX we don't have a way of getting the hardware
  176. * address
  177. */
  178. close(sd);
  179. return 0;
  180. #endif /* HAVE_NET_IF_DL_H */
  181. #endif /* SIOCGENADDR */
  182. #endif /* SIOCGIFHWADDR */
  183. if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
  184. continue;
  185. if (node_id) {
  186. memcpy(node_id, a, 6);
  187. close(sd);
  188. return 1;
  189. }
  190. }
  191. close(sd);
  192. #endif
  193. return 0;
  194. }
  195. /* Assume that the gettimeofday() has microsecond granularity */
  196. #define MAX_ADJUSTMENT 10
  197. static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
  198. {
  199. static int adjustment = 0;
  200. static struct timeval last = {0, 0};
  201. static uint16_t clock_seq;
  202. struct timeval tv;
  203. unsigned long long clock_reg;
  204. try_again:
  205. gettimeofday(&tv, 0);
  206. if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
  207. get_random_bytes(&clock_seq, sizeof(clock_seq));
  208. clock_seq &= 0x3FFF;
  209. last = tv;
  210. last.tv_sec--;
  211. }
  212. if ((tv.tv_sec < last.tv_sec) ||
  213. ((tv.tv_sec == last.tv_sec) &&
  214. (tv.tv_usec < last.tv_usec))) {
  215. clock_seq = (clock_seq+1) & 0x3FFF;
  216. adjustment = 0;
  217. last = tv;
  218. } else if ((tv.tv_sec == last.tv_sec) &&
  219. (tv.tv_usec == last.tv_usec)) {
  220. if (adjustment >= MAX_ADJUSTMENT)
  221. goto try_again;
  222. adjustment++;
  223. } else {
  224. adjustment = 0;
  225. last = tv;
  226. }
  227. clock_reg = tv.tv_usec*10 + adjustment;
  228. clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
  229. clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
  230. *clock_high = clock_reg >> 32;
  231. *clock_low = clock_reg;
  232. *ret_clock_seq = clock_seq;
  233. return 0;
  234. }
  235. void uuid_generate_time(uuid_t out)
  236. {
  237. static unsigned char node_id[6];
  238. static int has_init = 0;
  239. struct uuid uu;
  240. uint32_t clock_mid;
  241. if (!has_init) {
  242. if (get_node_id(node_id) <= 0) {
  243. get_random_bytes(node_id, 6);
  244. /*
  245. * Set multicast bit, to prevent conflicts
  246. * with IEEE 802 addresses obtained from
  247. * network cards
  248. */
  249. node_id[0] |= 0x01;
  250. }
  251. has_init = 1;
  252. }
  253. get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
  254. uu.clock_seq |= 0x8000;
  255. uu.time_mid = (uint16_t) clock_mid;
  256. uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
  257. memcpy(uu.node, node_id, 6);
  258. uuid_pack(&uu, out);
  259. }
  260. void uuid_generate_random(uuid_t out)
  261. {
  262. uuid_t buf;
  263. struct uuid uu;
  264. get_random_bytes(buf, sizeof(buf));
  265. uuid_unpack(buf, &uu);
  266. uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
  267. uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
  268. uuid_pack(&uu, out);
  269. }
  270. /*
  271. * This is the generic front-end to uuid_generate_random and
  272. * uuid_generate_time. It uses uuid_generate_random only if
  273. * /dev/urandom is available, since otherwise we won't have
  274. * high-quality randomness.
  275. */
  276. void uuid_generate(uuid_t out)
  277. {
  278. if (get_random_fd() >= 0)
  279. uuid_generate_random(out);
  280. else
  281. uuid_generate_time(out);
  282. }