uuid.c 4.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2007, 2008, 2009 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Authors:
 *     Mark McLoughlin <markmc@redhat.com>
 */

22
#include <config.h>
23 24 25 26 27 28

#include "uuid.h"

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
29
#include <stdio.h>
30 31 32 33 34 35
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

J
Jim Meyering 已提交
36
#include "c-ctype.h"
37
#include "internal.h"
38
#include "util.h"
39
#include "virterror_internal.h"
40
#include "logging.h"
41

42 43 44 45
#ifndef ENODATA
#define ENODATA EIO
#endif

46
static int
D
Daniel P. Berrange 已提交
47 48
virUUIDGenerateRandomBytes(unsigned char *buf,
                           int buflen)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{
    int fd;

    if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
        return errno;

    while (buflen > 0) {
        int n;

        if ((n = read(fd, buf, buflen)) <= 0) {
            if (errno == EINTR)
                continue;
            close(fd);
            return n < 0 ? errno : ENODATA;
        }

        buf += n;
        buflen -= n;
    }

    close(fd);

    return 0;
}

static int
D
Daniel P. Berrange 已提交
75 76
virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
                                 int buflen)
77 78
{
    while (buflen > 0) {
79
        *buf = virRandom(256);
80 81 82 83 84 85
        buflen--;
    }

    return 0;
}

86 87
/**
 * virUUIDGenerate:
88
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the new UUID
89 90 91 92 93
 *
 * Generates a randomized unique identifier.
 *
 * Returns 0 in case of success and -1 in case of failure
 */
94
int
D
Daniel P. Berrange 已提交
95
virUUIDGenerate(unsigned char *uuid)
96 97 98
{
    int err;

99 100 101
    if (uuid == NULL)
        return(-1);

102 103
    if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
        char ebuf[1024];
104
        VIR_WARN(_("Falling back to pseudorandom UUID,"
105 106 107
                   " failed to generate random bytes: %s"),
                 virStrerror(err, ebuf, sizeof ebuf));
    }
108

109
    return virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
110 111
}

J
Jim Meyering 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/* Convert C from hexadecimal character to integer.  */
static int
hextobin (unsigned char c)
{
  switch (c)
    {
    default: return c - '0';
    case 'a': case 'A': return 10;
    case 'b': case 'B': return 11;
    case 'c': case 'C': return 12;
    case 'd': case 'D': return 13;
    case 'e': case 'E': return 14;
    case 'f': case 'F': return 15;
    }
}

128 129
/**
 * virUUIDParse:
130 131
 * @uuidstr: zero terminated string representation of the UUID
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
132 133 134 135 136 137
 *
 * Parses the external string representation, allowing spaces and '-'
 * character in the sequence, and storing the result as a raw UUID
 *
 * Returns 0 in case of success and -1 in case of error.
 */
138
int
139
virUUIDParse(const char *uuidstr, unsigned char *uuid) {
140 141 142
    const char *cur;
    int i;

143
    if ((uuidstr == NULL) || (uuid == NULL))
144 145
        return(-1);

146 147 148 149
    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
     * pairs as long as there is 32 of them in the end.
     */
150 151 152
    cur = uuidstr;
    for (i = 0;i < VIR_UUID_BUFLEN;) {
        uuid[i] = 0;
153 154 155 156 157 158
        if (*cur == 0)
            goto error;
        if ((*cur == '-') || (*cur == ' ')) {
            cur++;
            continue;
        }
J
Jim Meyering 已提交
159
        if (!c_isxdigit(*cur))
160
            goto error;
J
Jim Meyering 已提交
161
        uuid[i] = hextobin(*cur);
162
        uuid[i] *= 16;
163 164 165
        cur++;
        if (*cur == 0)
            goto error;
J
Jim Meyering 已提交
166
        if (!c_isxdigit(*cur))
167
            goto error;
J
Jim Meyering 已提交
168
        uuid[i] += hextobin(*cur);
169 170 171 172 173 174 175 176 177 178
        i++;
        cur++;
    }

    return 0;

 error:
    return -1;
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/**
 * virUUIDFormat:
 * @uuid: array of VIR_UUID_RAW_LEN bytes to store the raw UUID
 * @uuidstr: array of VIR_UUID_STRING_BUFLEN bytes to store the
 * string representation of the UUID in. The resulting string
 * will be NULL terminated.
 *
 * Converts the raw UUID into printable format, with embedded '-'
 *
 * Returns 0 in case of success and -1 in case of error.
 */
void virUUIDFormat(const unsigned char *uuid, char *uuidstr)
{
    snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
             "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
             uuid[0], uuid[1], uuid[2], uuid[3],
             uuid[4], uuid[5], uuid[6], uuid[7],
             uuid[8], uuid[9], uuid[10], uuid[11],
             uuid[12], uuid[13], uuid[14], uuid[15]);
    uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';
}