X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fuuid.c;h=2aac4c7201f661ec1f168dac820b8ff5f45def85;hb=a23aab1fc2f66b63ba9b7e4b9c9a8f6d58c367d0;hp=264d9bf5afb113f0dc1813d08ca9f90447a66c6f;hpb=58fda1dab104041fc693032475ec4662c1a52849;p=openvswitch diff --git a/lib/uuid.c b/lib/uuid.c index 264d9bf5..2aac4c72 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008, 2009 Nicira Networks +/* Copyright (c) 2008, 2009, 2010 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,8 +25,8 @@ #include #include "aes128.h" +#include "entropy.h" #include "sha1.h" -#include "socket-util.h" #include "util.h" static struct aes128 key; @@ -34,7 +34,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 @@ -107,6 +106,14 @@ uuid_zero(struct uuid *uuid) uuid->parts[0] = uuid->parts[1] = uuid->parts[2] = uuid->parts[3] = 0; } +/* Returns true if 'uuid' is all zero, otherwise false. */ +bool +uuid_is_zero(const struct uuid *uuid) +{ + return (!uuid->parts[0] && !uuid->parts[1] + && !uuid->parts[2] && !uuid->parts[3]); +} + /* Compares 'a' and 'b'. Returns a negative value if 'a < b', zero if 'a == * b', or positive if 'a > b'. The ordering is lexicographical order of the * conventional way of writing out UUIDs as strings. */ @@ -132,6 +139,22 @@ uuid_compare_3way(const struct uuid *a, const struct uuid *b) * be set to all-zero-bits. */ bool uuid_from_string(struct uuid *uuid, const char *s) +{ + if (!uuid_from_string_prefix(uuid, s)) { + return false; + } else if (s[UUID_LEN] != '\0') { + uuid_zero(uuid); + return false; + } else { + return true; + } +} + +/* Same as uuid_from_string() but s[UUID_LEN] is not required to be a null byte + * to succeed; that is, 's' need only begin with UUID syntax, not consist + * entirely of it. */ +bool +uuid_from_string_prefix(struct uuid *uuid, const char *s) { static const char template[] = "00000000-1111-1111-2222-222233333333"; const char *t; @@ -144,10 +167,10 @@ uuid_from_string(struct uuid *uuid, const char *s) goto error; } *part = (*part << 4) + hexit_value(*s); - } else if (*t != *s) { - goto error; } else if (*t == 0) { return true; + } else if (*t != *s) { + goto error; } } @@ -156,27 +179,6 @@ error: return false; } -static void -read_urandom(void *buffer, size_t n) -{ - 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); - } - 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); - } - close(fd); -} - static void do_init(void) { @@ -189,7 +191,7 @@ do_init(void) gid_t gid; /* Get seed data. */ - read_urandom(random_seed, sizeof random_seed); + get_entropy_or_die(random_seed, sizeof random_seed); if (gettimeofday(&now, NULL)) { ovs_fatal(errno, "gettimeofday failed"); } @@ -212,5 +214,5 @@ do_init(void) aes128_schedule(&key, sha1); /* Generate initial counter. */ - read_urandom(counter, sizeof counter); + get_entropy_or_die(counter, sizeof counter); }