4
// $Id: //poco/1.2/Foundation/src/UUIDGenerator.cpp#1 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/UUIDGenerator.h"
38
#include "Poco/Thread.h"
39
#include "Poco/RandomStream.h"
40
#include "Poco/DigestEngine.h"
41
#include "Poco/MD5Engine.h"
42
#include "Poco/SingletonHolder.h"
49
UUIDGenerator::UUIDGenerator(): _ticks(0), _haveNode(false)
54
UUIDGenerator::~UUIDGenerator()
59
UUID UUIDGenerator::create()
61
FastMutex::ScopedLock lock(_mutex);
68
Timestamp::UtcTimeVal tv = timeStamp();
69
UInt32 timeLow = UInt32(tv & 0xFFFFFFFF);
70
UInt16 timeMid = UInt16((tv >> 32) & 0xFFFF);
71
UInt16 timeHiAndVersion = UInt16((tv >> 48) & 0x0FFF) + (UUID::UUID_TIME_BASED << 12);
72
UInt16 clockSeq = (UInt16(_random.next() >> 4) & 0x3FFF) | 0x8000;
73
return UUID(timeLow, timeMid, timeHiAndVersion, clockSeq, _node);
77
UUID UUIDGenerator::createFromName(const UUID& nsid, const std::string& name)
80
return createFromName(nsid, name, md5);
84
UUID UUIDGenerator::createFromName(const UUID& nsid, const std::string& name, DigestEngine& de)
86
poco_assert_dbg (de.digestLength() >= 16);
91
de.update(&netNsid, sizeof(netNsid));
94
const DigestEngine::Digest& d = de.digest();
95
for (int i = 0; i < 16; ++i)
99
return UUID(buffer, UUID::UUID_NAME_BASED);
103
UUID UUIDGenerator::createRandom()
106
RandomInputStream ris;
107
ris.read(buffer, sizeof(buffer));
108
return UUID(buffer, UUID::UUID_RANDOM);
112
Timestamp::UtcTimeVal UUIDGenerator::timeStamp()
117
if (now != _lastTime)
130
Timestamp::UtcTimeVal tv = now.utcTime();
135
UUID UUIDGenerator::createOne()
143
return createRandom();
148
UUIDGenerator& UUIDGenerator::defaultGenerator()
150
static SingletonHolder<UUIDGenerator> sh;
159
// platform-specific code below
163
#if defined(POCO_OS_FAMILY_WINDOWS)
168
#include <iphlpapi.h>
174
void UUIDGenerator::getNode()
176
PIP_ADAPTER_INFO pAdapterInfo;
177
PIP_ADAPTER_INFO pAdapter = 0;
178
ULONG len = sizeof(IP_ADAPTER_INFO);
179
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
180
// Make an initial call to GetAdaptersInfo to get
181
// the necessary size into len
182
DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
183
if (rc == ERROR_BUFFER_OVERFLOW)
185
delete [] reinterpret_cast<char*>(pAdapterInfo);
186
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
188
else if (rc != ERROR_SUCCESS)
190
throw SystemException("cannot get network adapter list");
195
if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
197
pAdapter = pAdapterInfo;
198
while (pAdapter && !found)
200
if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(_node))
202
memcpy(_node, pAdapter->Address, pAdapter->AddressLength);
207
else throw SystemException("cannot get network adapter list");
208
if (!found) throw SystemException("no Ethernet adapter found");
212
delete [] reinterpret_cast<char*>(pAdapterInfo);
215
delete [] reinterpret_cast<char*>(pAdapterInfo);
222
#elif defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX
226
#include <sys/types.h>
227
#include <sys/socket.h>
229
#include <net/if_dl.h>
235
void UUIDGenerator::getNode()
237
struct ifaddrs* ifaphead;
238
int rc = getifaddrs(&ifaphead);
239
if (rc) throw SystemException("cannot get network adapter list");
241
bool foundAdapter = false;
242
for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next)
244
if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK)
246
struct sockaddr_dl* sdl = reinterpret_cast<struct sockaddr_dl*>(ifap->ifa_addr);
247
caddr_t ap = (caddr_t) (sdl->sdl_data + sdl->sdl_nlen);
248
int alen = sdl->sdl_alen;
251
memcpy(_node, ap, sizeof(_node));
257
freeifaddrs(ifaphead);
258
if (!foundAdapter) throw SystemException("cannot determine MAC address (no suitable network adapter found)");
265
#elif defined(__CYGWIN__) || POCO_OS == POCO_OS_LINUX
269
#include <sys/ioctl.h>
270
#include <sys/socket.h>
271
#include <netinet/in.h>
273
#include <arpa/inet.h>
280
void UUIDGenerator::getNode()
284
int s = socket(PF_INET, SOCK_DGRAM, 0);
285
if (s == -1) throw SystemException("cannot open socket");
287
strcpy(ifr.ifr_name, "eth0");
288
int rc = ioctl(s, SIOCGIFHWADDR, &ifr);
290
if (rc < 0) throw SystemException("cannot get MAC address");
291
struct sockaddr* sa = reinterpret_cast<struct sockaddr*>(&ifr.ifr_addr);
292
memcpy(_node, sa->sa_data, sizeof(_node));
299
#elif defined(POCO_OS_FAMILY_UNIX) || defined(POCO_OS_FAMILY_VMS)
306
#include <sys/ioctl.h>
308
#if defined(sun) || defined(__sun)
309
#include <sys/sockio.h>
311
#include <sys/socket.h>
312
#include <sys/types.h>
313
#include <netinet/in.h>
318
#include <arpa/inet.h>
322
#include <net/if_arp.h>
327
#define MAXHOSTNAMELEN 64
334
void UUIDGenerator::getNode()
336
char name[MAXHOSTNAMELEN];
337
if (gethostname(name, sizeof(name)))
338
throw SystemException("cannot get host name");
340
struct hostent* pHost = gethostbyname(name);
341
if (!pHost) throw SystemException("cannot get host IP address");
343
int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
344
if (s == -1) throw SystemException("cannot open socket");
347
memset(&ar, 0, sizeof(ar));
348
struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa);
349
pAddr->sin_family = AF_INET;
350
memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
351
int rc = ioctl(s, SIOCGARP, &ar);
353
if (rc < 0) throw SystemException("cannot get MAC address");
354
memcpy(_node, ar.arp_ha.sa_data, sizeof(_node));