]> git.proxmox.com Git - mirror_ovs.git/blobdiff - lib/uuid.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / uuid.c
index 9aaa91590d9a1bc0c025831f8674328c9dea1bbe..13d20ac64977ab89b784c0ffe10045ff21742960 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008, 2009, 2010 Nicira Networks
+/* Copyright (c) 2008, 2009, 2010, 2011, 2013, 2016, 2017 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <unistd.h>
 
 #include "aes128.h"
+#include "entropy.h"
+#include "ovs-thread.h"
 #include "sha1.h"
-#include "socket-util.h"
+#include "timeval.h"
 #include "util.h"
 
 static struct aes128 key;
@@ -34,7 +36,6 @@ static uint64_t counter[2];
 BUILD_ASSERT_DECL(sizeof counter == 16);
 
 static void do_init(void);
-static void read_urandom(void *buffer, size_t n);
 
 /*
  * Initialize the UUID module.  Aborts the program with an error message if
@@ -49,11 +50,8 @@ static void read_urandom(void *buffer, size_t n);
 void
 uuid_init(void)
 {
-    static bool inited;
-    if (!inited) {
-        do_init();
-        inited = true;
-    }
+    static pthread_once_t once = PTHREAD_ONCE_INIT;
+    pthread_once(&once, do_init);
 }
 
 /* Generates a new random UUID in 'uuid'.
@@ -83,16 +81,37 @@ uuid_init(void)
 void
 uuid_generate(struct uuid *uuid)
 {
+    static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
+    uint64_t copy[2];
+
     uuid_init();
 
-    /* Increment the counter. */
+    /* Copy out the counter's current value, then increment it. */
+    ovs_mutex_lock(&mutex);
+    copy[0] = counter[0];
+    copy[1] = counter[1];
     if (++counter[1] == 0) {
         counter[0]++;
     }
+    ovs_mutex_unlock(&mutex);
 
     /* AES output is exactly 16 bytes, so we encrypt directly into 'uuid'. */
-    aes128_encrypt(&key, counter, uuid);
+    aes128_encrypt(&key, copy, uuid);
+
+    uuid_set_bits_v4(uuid);
+}
+
+struct uuid
+uuid_random(void)
+{
+    struct uuid uuid;
+    uuid_generate(&uuid);
+    return uuid;
+}
 
+void
+uuid_set_bits_v4(struct uuid *uuid)
+{
     /* Set bits to indicate a random UUID.  See RFC 4122 section 4.4. */
     uuid->parts[2] &= ~0xc0000000;
     uuid->parts[2] |=  0x80000000;
@@ -104,7 +123,7 @@ uuid_generate(struct uuid *uuid)
 void
 uuid_zero(struct uuid *uuid)
 {
-    uuid->parts[0] = uuid->parts[1] = uuid->parts[2] = uuid->parts[3] = 0;
+    *uuid = UUID_ZERO;
 }
 
 /* Returns true if 'uuid' is all zero, otherwise false. */
@@ -157,48 +176,96 @@ uuid_from_string(struct uuid *uuid, const char *s)
 bool
 uuid_from_string_prefix(struct uuid *uuid, const char *s)
 {
-    static const char template[] = "00000000-1111-1111-2222-222233333333";
-    const char *t;
+    /* 0         1         2         3      */
+    /* 012345678901234567890123456789012345 */
+    /* ------------------------------------ */
+    /* 00000000-1111-1111-2222-222233333333 */
 
-    uuid_zero(uuid);
-    for (t = template; ; t++, s++) {
-        if (*t >= '0' && *t <= '3') {
-            uint32_t *part = &uuid->parts[*t - '0'];
-            if (!isxdigit(*s)) {
-                goto error;
-            }
-            *part = (*part << 4) + hexit_value(*s);
-        } else if (*t == 0) {
-            return true;
-        } else if (*t != *s) {
-            goto error;
-        }
+    bool ok;
+
+    uuid->parts[0] = hexits_value(s, 8, &ok);
+    if (!ok || s[8] != '-') {
+        goto error;
+    }
+
+    uuid->parts[1] = hexits_value(s + 9, 4, &ok) << 16;
+    if (!ok || s[13] != '-') {
+        goto error;
+    }
+
+    uuid->parts[1] += hexits_value(s + 14, 4, &ok);
+    if (!ok || s[18] != '-') {
+        goto error;
+    }
+
+    uuid->parts[2] = hexits_value(s + 19, 4, &ok) << 16;
+    if (!ok || s[23] != '-') {
+        goto error;
+    }
+
+    uuid->parts[2] += hexits_value(s + 24, 4, &ok);
+    if (!ok) {
+        goto error;
     }
 
+    uuid->parts[3] = hexits_value(s + 28, 8, &ok);
+    if (!ok) {
+        goto error;
+    }
+    return true;
+
 error:
     uuid_zero(uuid);
     return false;
 }
-\f
-static void
-read_urandom(void *buffer, size_t n)
+
+/* If 's' is a string representation of a UUID, or the beginning of one,
+ * returns strlen(s), otherwise 0.
+ *
+ * For example:
+ *
+ *     "123" yields 3
+ *     "xyzzy" yields 0
+ *     "123xyzzy" yields 0
+ *     "e66250bb-9531-491b-b9c3-5385cabb0080" yields 36
+ *     "e66250bb-9531-491b-b9c3-5385cabb0080xyzzy" yields 0
+ */
+int
+uuid_is_partial_string(const char *s)
 {
-    static const char urandom[] = "/dev/urandom";
-    size_t bytes_read;
-    int error;
-    int fd;
-
-    fd = open(urandom, O_RDONLY);
-    if (fd < 0) {
-        ovs_fatal(errno, "%s: open failed", urandom);
+    static const char tmpl[UUID_LEN] = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
+    size_t i;
+    for (i = 0; i < UUID_LEN; i++) {
+        if (s[i] == '\0') {
+            return i;
+        } else if (tmpl[i] == 'x'
+                   ? hexit_value(s[i]) < 0
+                   : s[i] != '-') {
+            return 0;
+        }
     }
-    error = read_fully(fd, buffer, n, &bytes_read);
-    if (error == EOF) {
-        ovs_fatal(0, "%s: unexpected end of file", urandom);
-    } else if (error) {
-        ovs_fatal(error, "%s: read error", urandom);
+    if (s[i] != '\0') {
+        return 0;
     }
-    close(fd);
+    return i;
+}
+
+/* Compares 'match' to the string representation of 'uuid'.  If 'match' equals
+ * or is a prefix of this string representation, returns strlen(match);
+ * otherwise, returns 0. */
+int
+uuid_is_partial_match(const struct uuid *uuid, const char *match)
+{
+    char uuid_s[UUID_LEN + 1];
+    snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
+    size_t match_len = strlen(match);
+    return !strncmp(uuid_s, match, match_len) ? match_len : 0;
+}
+\f
+static void
+sha1_update_int(struct sha1_ctx *sha1_ctx, uintmax_t x)
+{
+   sha1_update(sha1_ctx, &x, sizeof x);
 }
 
 static void
@@ -208,27 +275,21 @@ do_init(void)
     struct sha1_ctx sha1_ctx;
     uint8_t random_seed[16];
     struct timeval now;
-    pid_t pid, ppid;
-    uid_t uid;
-    gid_t gid;
 
     /* Get seed data. */
-    read_urandom(random_seed, sizeof random_seed);
-    if (gettimeofday(&now, NULL)) {
-        ovs_fatal(errno, "gettimeofday failed");
-    }
-    pid = getpid();
-    ppid = getppid();
-    uid = getuid();
-    gid = getgid();
+    get_entropy_or_die(random_seed, sizeof random_seed);
+    xgettimeofday(&now);
 
     /* Convert seed into key. */
     sha1_init(&sha1_ctx);
     sha1_update(&sha1_ctx, random_seed, sizeof random_seed);
-    sha1_update(&sha1_ctx, &pid, sizeof pid);
-    sha1_update(&sha1_ctx, &ppid, sizeof ppid);
-    sha1_update(&sha1_ctx, &uid, sizeof uid);
-    sha1_update(&sha1_ctx, &gid, sizeof gid);
+    sha1_update(&sha1_ctx, &now, sizeof now);
+    sha1_update_int(&sha1_ctx, getpid());
+#ifndef _WIN32
+    sha1_update_int(&sha1_ctx, getppid());
+    sha1_update_int(&sha1_ctx, getuid());
+    sha1_update_int(&sha1_ctx, getgid());
+#endif
     sha1_final(&sha1_ctx, sha1);
 
     /* Generate key. */
@@ -236,5 +297,5 @@ do_init(void)
     aes128_schedule(&key, sha1);
 
     /* Generate initial counter. */
-    read_urandom(counter, sizeof counter);
+    get_entropy_or_die(counter, sizeof counter);
 }