X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fuuid.c;h=cfaf0c5825ffe91df2143730483ce1eb40f4a1e7;hb=305b76debf72120672a8ba81d3356b6dccb1da9a;hp=9aaa91590d9a1bc0c025831f8674328c9dea1bbe;hpb=a0bc29a541fc7dc6e20137d5558e2094d614e6ab;p=sliver-openvswitch.git diff --git a/lib/uuid.c b/lib/uuid.c index 9aaa91590..cfaf0c582 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008, 2009, 2010 Nicira Networks +/* Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc. * * 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,9 @@ #include #include "aes128.h" +#include "entropy.h" #include "sha1.h" -#include "socket-util.h" +#include "timeval.h" #include "util.h" static struct aes128 key; @@ -34,7 +35,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 @@ -157,50 +157,49 @@ 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; } -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) { @@ -213,10 +212,8 @@ do_init(void) gid_t gid; /* Get seed data. */ - read_urandom(random_seed, sizeof random_seed); - if (gettimeofday(&now, NULL)) { - ovs_fatal(errno, "gettimeofday failed"); - } + get_entropy_or_die(random_seed, sizeof random_seed); + xgettimeofday(&now); pid = getpid(); ppid = getppid(); uid = getuid(); @@ -236,5 +233,5 @@ do_init(void) aes128_schedule(&key, sha1); /* Generate initial counter. */ - read_urandom(counter, sizeof counter); + get_entropy_or_die(counter, sizeof counter); }