]> git.saurik.com Git - apple/libc.git/blobdiff - uuid/uuidsrc/gen_uuid.c
Libc-1158.1.2.tar.gz
[apple/libc.git] / uuid / uuidsrc / gen_uuid.c
index 30d6202e3b5712a3e4ae516b09e249e7abc75f08..c56c8c9aa73bff3b1ffcf7e0c4929e4050f39928 100644 (file)
 
 #include "uuidP.h"
 
-#ifdef HAVE_SRANDOM
-#define srand(x)       srandom(x)
-#define rand()                 random()
-#endif
-
-static int get_random_fd(void)
-{
-       struct timeval  tv;
-       static int      fd = -2;
-       int             i;
-
-       if (fd == -2) {
-               gettimeofday(&tv, 0);
-               fd = open("/dev/urandom", O_RDONLY);
-               if (fd == -1)
-                       fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
-               srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
-       }
-       /* Crank the random number generator a few times */
-       gettimeofday(&tv, 0);
-       for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
-               rand();
-       return fd;
-}
-
-
-/*
- * Generate a series of random bytes.  Use /dev/urandom if possible,
- * and if not, use srandom/random.
- */
-static void get_random_bytes(void *buf, int nbytes)
-{
-       int i, n = nbytes, fd = get_random_fd();
-       int lose_counter = 0;
-       unsigned char *cp = (unsigned char *) buf;
-
-       if (fd >= 0) {
-               while (n > 0) {
-                       i = read(fd, cp, n);
-                       if (i <= 0) {
-                               if (lose_counter++ > 16)
-                                       break;
-                               continue;
-                       }
-                       n -= i;
-                       cp += i;
-                       lose_counter = 0;
-               }
-       }
-       
-       /*
-        * We do this all the time, but this is the only source of
-        * randomness if /dev/random/urandom is out to lunch.
-        */
-       for (cp = buf, i = 0; i < nbytes; i++)
-               *cp++ ^= (rand() >> 7) & 0xFF;
-       return;
-}
+#define        get_random_bytes(a,b)   arc4random_buf((a),(b))
 
 /*
  * Get the ethernet hardware address, if we can find it...
@@ -203,7 +146,11 @@ static int get_node_id(unsigned char *node_id)
 #endif /* AF_LINK */
 #endif /* SIOCGENADDR */
 #endif /* SIOCGIFHWADDR */
-               if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
+               /*
+                * Skip interfaces that return either 00:00:00:00:00:00 or
+                * 02:00:00:00:00:00.
+                */
+               if ((!a[0] || a[0] == 0x02) && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
                        continue;
                if (node_id) {
                        memcpy(node_id, a, 6);
@@ -302,15 +249,9 @@ void uuid_generate_random(uuid_t out)
 }
 
 /*
- * This is the generic front-end to uuid_generate_random and
- * uuid_generate_time.  It uses uuid_generate_random only if
- * /dev/urandom is available, since otherwise we won't have
- * high-quality randomness.
+ * This is the generic front-end
  */
 void uuid_generate(uuid_t out)
 {
-       if (get_random_fd() >= 0)
-               uuid_generate_random(out);
-       else
-               uuid_generate_time(out);
+       uuid_generate_random(out);
 }