]> git.saurik.com Git - apple/xnu.git/blobdiff - libkern/uuid/uuid.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libkern / uuid / uuid.c
index ffc5c80592cd4dae8251e5c36af114cdf4ecdf52..6bdfcec9de481072209563b9d17dadd16fb1240c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
+ * Copyright (c) 2004-2011 Apple Inc. All rights reserved.
  *
  * %Begin-Header%
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  * 3. The name of the author may not be used to endorse or promote
  *    products derived from this software without specific prior
  *    written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 #include <sys/systm.h>
 #include <sys/time.h>
 
-#include <net/if.h>
-#include <net/if_dl.h>
-#include <net/if_types.h>
-
-UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+extern int uuid_get_ethernet(u_int8_t *);
 
 static void
 read_node(uint8_t *node)
 {
 #if NETWORKING
-       struct ifnet *ifp;
-       struct sockaddr_dl *sdl;
-
-       ifnet_head_lock_shared();
-       TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
-               ifnet_lock_shared(ifp);
-               IFA_LOCK_SPIN(ifp->if_lladdr);
-               sdl = (struct sockaddr_dl *)ifp->if_lladdr->ifa_addr;
-               if (sdl->sdl_type == IFT_ETHER) {
-                       memcpy(node, LLADDR(sdl), 6);
-                       IFA_UNLOCK(ifp->if_lladdr);
-                       ifnet_lock_done(ifp);
-                       ifnet_head_done();
-                       return;
-               }
-               IFA_UNLOCK(ifp->if_lladdr);
-               ifnet_lock_done(ifp);
+       if (uuid_get_ethernet(node) == 0) {
+               return;
        }
-       ifnet_head_done();
 #endif /* NETWORKING */
 
        read_random(node, 6);
@@ -103,13 +83,25 @@ uuid_copy(uuid_t dst, const uuid_t src)
        memcpy(dst, src, sizeof(uuid_t));
 }
 
+static void
+uuid_random_setflags(uuid_t out)
+{
+       out[6] = (out[6] & 0x0F) | 0x40;
+       out[8] = (out[8] & 0x3F) | 0x80;
+}
+
 void
 uuid_generate_random(uuid_t out)
 {
        read_random(out, sizeof(uuid_t));
+       uuid_random_setflags(out);
+}
 
-       out[6] = (out[6] & 0x0F) | 0x40;
-       out[8] = (out[8] & 0x3F) | 0x80;
+void
+uuid_generate_early_random(uuid_t out)
+{
+       read_frandom(out, sizeof(uuid_t));
+       uuid_random_setflags(out);
 }
 
 void
@@ -129,7 +121,7 @@ uuid_generate_time(uuid_t out)
        out[5] = (uint8_t)(time >> 32);
        out[6] = (uint8_t)(time >> 56);
        out[7] = (uint8_t)(time >> 48);
+
        out[6] = (out[6] & 0x0F) | 0x10;
        out[8] = (out[8] & 0x3F) | 0x80;
 }
@@ -152,52 +144,52 @@ uuid_parse(const uuid_string_t in, uuid_t uu)
        int n = 0;
 
        sscanf(in,
-               "%2hhx%2hhx%2hhx%2hhx-"
-               "%2hhx%2hhx-"
-               "%2hhx%2hhx-"
-               "%2hhx%2hhx-"
-               "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%n",
-               &uu[0], &uu[1], &uu[2], &uu[3],
-               &uu[4], &uu[5],
-               &uu[6], &uu[7],
-               &uu[8], &uu[9],
-               &uu[10], &uu[11], &uu[12], &uu[13], &uu[14], &uu[15], &n);
-
-       return (n != 36 || in[n] != '\0' ? -1 : 0);
+           "%2hhx%2hhx%2hhx%2hhx-"
+           "%2hhx%2hhx-"
+           "%2hhx%2hhx-"
+           "%2hhx%2hhx-"
+           "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%n",
+           &uu[0], &uu[1], &uu[2], &uu[3],
+           &uu[4], &uu[5],
+           &uu[6], &uu[7],
+           &uu[8], &uu[9],
+           &uu[10], &uu[11], &uu[12], &uu[13], &uu[14], &uu[15], &n);
+
+       return n != 36 || in[n] != '\0' ? -1 : 0;
 }
 
 void
 uuid_unparse_lower(const uuid_t uu, uuid_string_t out)
 {
        snprintf(out,
-               sizeof(uuid_string_t),
-               "%02x%02x%02x%02x-"
-               "%02x%02x-"
-               "%02x%02x-"
-               "%02x%02x-"
-               "%02x%02x%02x%02x%02x%02x",
-               uu[0], uu[1], uu[2], uu[3],
-               uu[4], uu[5],
-               uu[6], uu[7],
-               uu[8], uu[9],
-               uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
+           sizeof(uuid_string_t),
+           "%02x%02x%02x%02x-"
+           "%02x%02x-"
+           "%02x%02x-"
+           "%02x%02x-"
+           "%02x%02x%02x%02x%02x%02x",
+           uu[0], uu[1], uu[2], uu[3],
+           uu[4], uu[5],
+           uu[6], uu[7],
+           uu[8], uu[9],
+           uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
 }
 
 void
 uuid_unparse_upper(const uuid_t uu, uuid_string_t out)
 {
        snprintf(out,
-               sizeof(uuid_string_t),
-               "%02X%02X%02X%02X-"
-               "%02X%02X-"
-               "%02X%02X-"
-               "%02X%02X-"
-               "%02X%02X%02X%02X%02X%02X",
-               uu[0], uu[1], uu[2], uu[3],
-               uu[4], uu[5],
-               uu[6], uu[7],
-               uu[8], uu[9],
-               uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
+           sizeof(uuid_string_t),
+           "%02X%02X%02X%02X-"
+           "%02X%02X-"
+           "%02X%02X-"
+           "%02X%02X-"
+           "%02X%02X%02X%02X%02X%02X",
+           uu[0], uu[1], uu[2], uu[3],
+           uu[4], uu[5],
+           uu[6], uu[7],
+           uu[8], uu[9],
+           uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
 }
 
 void