2
* Copyright (C) 2007, 2008, 2009 Red Hat, Inc.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Mark McLoughlin <markmc@redhat.com>
31
#include <sys/types.h>
39
#include "virterror_internal.h"
47
virUUIDGenerateRandomBytes(unsigned char *buf,
52
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
58
if ((n = read(fd, buf, buflen)) <= 0) {
62
return n < 0 ? errno : ENODATA;
75
virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
79
*buf = virRandom(256);
88
* @uuid: array of VIR_UUID_BUFLEN bytes to store the new UUID
90
* Generates a randomized unique identifier.
92
* Returns 0 in case of success and -1 in case of failure
95
virUUIDGenerate(unsigned char *uuid)
102
if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
104
VIR_WARN(_("Falling back to pseudorandom UUID,"
105
" failed to generate random bytes: %s"),
106
virStrerror(err, ebuf, sizeof ebuf));
109
return virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
112
/* Convert C from hexadecimal character to integer. */
114
hextobin (unsigned char c)
118
default: return c - '0';
119
case 'a': case 'A': return 10;
120
case 'b': case 'B': return 11;
121
case 'c': case 'C': return 12;
122
case 'd': case 'D': return 13;
123
case 'e': case 'E': return 14;
124
case 'f': case 'F': return 15;
130
* @uuidstr: zero terminated string representation of the UUID
131
* @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
133
* Parses the external string representation, allowing spaces and '-'
134
* character in the sequence, and storing the result as a raw UUID
136
* Returns 0 in case of success and -1 in case of error.
139
virUUIDParse(const char *uuidstr, unsigned char *uuid) {
143
if ((uuidstr == NULL) || (uuid == NULL))
147
* do a liberal scan allowing '-' and ' ' anywhere between character
148
* pairs as long as there is 32 of them in the end.
151
for (i = 0;i < VIR_UUID_BUFLEN;) {
155
if ((*cur == '-') || (*cur == ' ')) {
159
if (!c_isxdigit(*cur))
161
uuid[i] = hextobin(*cur);
166
if (!c_isxdigit(*cur))
168
uuid[i] += hextobin(*cur);
181
* @uuid: array of VIR_UUID_RAW_LEN bytes to store the raw UUID
182
* @uuidstr: array of VIR_UUID_STRING_BUFLEN bytes to store the
183
* string representation of the UUID in. The resulting string
184
* will be NULL terminated.
186
* Converts the raw UUID into printable format, with embedded '-'
188
* Returns 0 in case of success and -1 in case of error.
190
void virUUIDFormat(const unsigned char *uuid, char *uuidstr)
192
snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
193
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
194
uuid[0], uuid[1], uuid[2], uuid[3],
195
uuid[4], uuid[5], uuid[6], uuid[7],
196
uuid[8], uuid[9], uuid[10], uuid[11],
197
uuid[12], uuid[13], uuid[14], uuid[15]);
198
uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';