uuid.c 4.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2007, 2008 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

40 41
#define qemudLog(level, msg...) fprintf(stderr, msg)

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
    if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN)))
103
        qemudLog(QEMUD_WARN,
104 105
                 _("Falling back to pseudorandom UUID,"
                   " failed to generate random bytes: %s"), strerror(err));
106

107
    return virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
108 109
}

J
Jim Meyering 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* 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;
    }
}

126 127
/**
 * virUUIDParse:
128 129
 * @uuidstr: zero terminated string representation of the UUID
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
130 131 132 133 134 135
 *
 * 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.
 */
136
int
137
virUUIDParse(const char *uuidstr, unsigned char *uuid) {
138 139 140
    const char *cur;
    int i;

141
    if ((uuidstr == NULL) || (uuid == NULL))
142 143
        return(-1);

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

    return 0;

 error:
    return -1;
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/**
 * 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';
}