uuid.cpp 3.1 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU 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
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

Martin Sustrik's avatar
Martin Sustrik committed
24
#if defined ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
25

Martin Sustrik's avatar
Martin Sustrik committed
26
zmq::uuid_t::uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
27 28
{
    RPC_STATUS ret = UuidCreate (&uuid);
Martin Sustrik's avatar
Martin Sustrik committed
29
    zmq_assert (ret == RPC_S_OK);
Martin Sustrik's avatar
Martin Sustrik committed
30
    ret = UuidToString (&uuid, &uuid_str);
Martin Sustrik's avatar
Martin Sustrik committed
31
    zmq_assert (ret == RPC_S_OK);
unknown's avatar
unknown committed
32 33 34 35 36 37 38

	/*
	HRESULT hr = CoCreateGUID (&uuid);
	zmq_assert (hr == S_OK);
	int rc = StringFromGUID2 (uuid, uuid_str, 40);
	zmq_assert (rc != 0);
	*/
Martin Sustrik's avatar
Martin Sustrik committed
39 40
}

Martin Sustrik's avatar
Martin Sustrik committed
41
zmq::uuid_t::~uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
42 43 44
{
}

Martin Sustrik's avatar
Martin Sustrik committed
45
const char *zmq::uuid_t::to_string ()
Martin Sustrik's avatar
Martin Sustrik committed
46
{
unknown's avatar
unknown committed
47
    return (char*) uuid_str;
Martin Sustrik's avatar
Martin Sustrik committed
48 49
}

Martin Sustrik's avatar
Martin Sustrik committed
50
#elif defined ZMQ_HAVE_FREEBSD
Martin Sustrik's avatar
Martin Sustrik committed
51 52 53 54

#include <stdlib.h>
#include <uuid.h>

Martin Sustrik's avatar
Martin Sustrik committed
55
zmq::uuid_t::uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
56 57 58
{
    uint32_t status;
    uuid_create (&uuid, &status);
Martin Sustrik's avatar
Martin Sustrik committed
59
    zmq_assert (status == uuid_s_ok);
Martin Sustrik's avatar
Martin Sustrik committed
60
    uuid_to_string (&uuid, &uuid_str, &status);
Martin Sustrik's avatar
Martin Sustrik committed
61
    zmq_assert (status == uuid_s_ok);
Martin Sustrik's avatar
Martin Sustrik committed
62 63
}

Martin Sustrik's avatar
Martin Sustrik committed
64
zmq::uuid_t::~uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
65 66 67 68
{
    free (uuid_str);
}

Martin Sustrik's avatar
Martin Sustrik committed
69
const char *zmq::uuid_t::to_string ()
Martin Sustrik's avatar
Martin Sustrik committed
70 71 72 73
{
    return uuid_str;
}

Martin Sustrik's avatar
Martin Sustrik committed
74
#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_OSX
Martin Sustrik's avatar
Martin Sustrik committed
75 76 77

#include <uuid/uuid.h>

Martin Sustrik's avatar
Martin Sustrik committed
78
zmq::uuid_t::uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
79 80 81 82 83
{
    uuid_generate (uuid);
    uuid_unparse (uuid, uuid_buf);
}

Martin Sustrik's avatar
Martin Sustrik committed
84
zmq::uuid_t::~uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
85 86 87
{
}

Martin Sustrik's avatar
Martin Sustrik committed
88
const char *zmq::uuid_t::to_string ()
Martin Sustrik's avatar
Martin Sustrik committed
89 90 91 92 93 94 95 96 97 98
{
    return uuid_buf;
}

#else

#include <stdio.h>
#include <string.h>
#include <openssl/rand.h>

Martin Sustrik's avatar
Martin Sustrik committed
99
zmq::uuid_t::uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
100 101 102
{
    unsigned char rand_buf [16];
    int ret = RAND_bytes (rand_buf, sizeof rand_buf);
Martin Sustrik's avatar
Martin Sustrik committed
103
    zmq_assert (ret == 1);
Martin Sustrik's avatar
Martin Sustrik committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    //  Read in UUID fields.
    memcpy (&time_low, rand_buf, sizeof time_low);
    memcpy (&time_mid, rand_buf + 4, sizeof time_mid);
    memcpy (&time_hi_and_version, rand_buf + 6, sizeof time_hi_and_version);
    memcpy (&clock_seq_hi_and_reserved, rand_buf + 8,
        sizeof clock_seq_hi_and_reserved);
    memcpy (&clock_seq_low, rand_buf + 9, sizeof clock_seq_low);
    memcpy (&node [0], rand_buf + 10, sizeof node);

    //  Store UUID version number.
    time_hi_and_version = (time_hi_and_version & 0x0fff) | 4 << 12;

    //  Store UUID type.
    clock_seq_hi_and_reserved = (clock_seq_hi_and_reserved & 0x3f) | 0x80;

    snprintf (uuid_buf, sizeof uuid_buf,
        "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
        time_low,
        time_mid,
        time_hi_and_version,
        clock_seq_hi_and_reserved,
        clock_seq_low,
        node [0], node [1], node [2], node [3], node [4], node [5]);
}

Martin Sustrik's avatar
Martin Sustrik committed
130
zmq::uuid_t::~uuid_t ()
Martin Sustrik's avatar
Martin Sustrik committed
131 132 133
{
}

Martin Sustrik's avatar
Martin Sustrik committed
134
const char *zmq::uuid_t::to_string ()
Martin Sustrik's avatar
Martin Sustrik committed
135 136 137 138 139
{
    return uuid_buf;
}

#endif