summaryrefslogtreecommitdiff
path: root/src/uuid.cpp
blob: 02f716eb06280ab11b18c32412823d3f721bcb16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file

    This file is part of 0MQ.

    0MQ 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 3 of the License, or
    (at your option) any later version.

    0MQ 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 program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>

#include "uuid.hpp"
#include "err.hpp"
#include "stdint.hpp"
#include "platform.hpp"

#if defined ZMQ_HAVE_WINDOWS

#include <rpc.h>

void zmq::generate_uuid (void *buf_)
{
    RPC_STATUS ret = UuidCreate ((::UUID*) buf_);
    zmq_assert (ret == RPC_S_OK);
}

#elif defined ZMQ_HAVE_FREEBSD || defined ZMQ_HAVE_NETBSD

#include <uuid.h>

void zmq::generate_uuid (void *buf_)
{
    uint32_t status;
    uuid_create ((::uuid_t*) buf_, &status);
    zmq_assert (status == uuid_s_ok);
}

#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_SOLARIS ||\
      defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_CYGWIN

#include <uuid/uuid.h>

void zmq::generate_uuid (void *buf_)
{
    uuid_generate ((unsigned char*) buf_);
}

#elif defined ZMQ_HAVE_OPENVMS

#include <starlet.h>

void zmq::generate_uuid (void *buf_)
{
    sys$create_uid(buf_);
}

#else

#include <openssl/rand.h>

void zmq::generate_uuid (void *buf_)
{
    unsigned char *buf = (unsigned char*) buf_;

    //  Generate random value.
    int ret = RAND_bytes (buf, 16);
    zmq_assert (ret == 1);

    //  Set UUID variant to 2 (UUID as specified in RFC4122).
    const unsigned char variant = 2;
    buf [8] = (buf [8] & 0x3f) | (variant << 6);

    //  Set UUID version to 4 (randomly or pseudo-randomly generated UUID).
    const unsigned char version = 4;
    buf [6] = (buf [6] & 0x0f) | (version << 4);
}

#endif