⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uuidgenerator.cpp

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 CPP
字号:
//
// UUIDGenerator.cpp
//
// $Id: //poco/Main/Foundation/src/UUIDGenerator.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#include "Foundation/UUIDGenerator.h"
#include "Foundation/Thread.h"
#include "Foundation/RandomStream.h"
#include "Foundation/DigestEngine.h"
#include "Foundation/MD5Engine.h"
#include "Foundation/SingletonHolder.h"
#include <string.h>


Foundation_BEGIN


UUIDGenerator::UUIDGenerator(): _ticks(0), _haveNode(false)
{
}


UUIDGenerator::~UUIDGenerator()
{
}


UUID UUIDGenerator::create()
{
	FastMutex::ScopedLock lock(_mutex);

	if (!_haveNode)
	{
		getNode();
		_haveNode = true;
	}
	Timestamp::UtcTimeVal tv = timeStamp();
	UInt32 timeLow = UInt32(tv & 0xFFFFFFFF);
	UInt16 timeMid = UInt16((tv >> 32) & 0xFFFF);
	UInt16 timeHiAndVersion = UInt16((tv >> 48) & 0x0FFF) + (UUID::UUID_TIME_BASED << 12);
	UInt16 clockSeq = (UInt16(_random.next() >> 4) & 0x3FFF) | 0x8000;
	return UUID(timeLow, timeMid, timeHiAndVersion, clockSeq, _node);
}


UUID UUIDGenerator::createFromName(const UUID& nsid, const std::string& name)
{
	MD5Engine md5;
	return createFromName(nsid, name, md5);
}


UUID UUIDGenerator::createFromName(const UUID& nsid, const std::string& name, DigestEngine& de)
{
	poco_assert_dbg (de.digestLength() >= 16);

	UUID netNsid = nsid;
	netNsid.toNetwork();
	de.reset();
	de.update(&netNsid, sizeof(netNsid));
	de.update(name);
	char buffer[16];
	DigestEngine::Digest d = de.digest();
	for (int i = 0; i < 16; ++i)
	{
		buffer[i] = d[i];
	}
	return UUID(buffer, UUID::UUID_NAME_BASED);
}

	
UUID UUIDGenerator::createRandom()
{
	char buffer[16];
	RandomInputStream ris;
	ris.read(buffer, sizeof(buffer));
	return UUID(buffer, UUID::UUID_RANDOM);
}


Timestamp::UtcTimeVal UUIDGenerator::timeStamp()
{
	Timestamp now;
	for (;;)
	{
		if (now != _lastTime)
		{
			_lastTime = now;
			_ticks = 0;
			break;
		}
		if (_ticks < 100)
		{
			++_ticks;
			break;
		}
		now.update();
	}
	Timestamp::UtcTimeVal tv = now.utcTime();
	return tv + _ticks;
}


UUIDGenerator& UUIDGenerator::defaultGenerator()
{
	static SingletonHolder<UUIDGenerator> sh;
	return *sh.get();
}


Foundation_END


//
// platform-specific code below
//


#if defined(POCO_OS_FAMILY_WINDOWS)
//
// Windows
//
// The following code is based on the example code
// from the MSDN knowledge base article ID 118623:
// "How To Get the MAC Address for an Ethernet Adapter".
//
#include <windows.h>
#include <wincon.h>
#include <nb30.h>


Foundation_BEGIN


struct ASTAT
{
	ADAPTER_STATUS adapt;
	NAME_BUFFER    nameBuffer[30];
};


void UUIDGenerator::getNode()
{
	ASTAT adapter;
	NCB ncb;
	LANA_ENUM lenum;

	memset(&ncb, 0, sizeof(ncb));
	ncb.ncb_command = NCBENUM;
	ncb.ncb_buffer  = (UCHAR*) &lenum;
	ncb.ncb_length  = sizeof(lenum);
	UCHAR rc = Netbios(&ncb);
	if (rc) throw SystemException("cannot enumerate network adapters");
	for (int i = 0; i < lenum.length; i++)
	{
		memset(&ncb, 0, sizeof(ncb));
		ncb.ncb_command  = NCBRESET;
		ncb.ncb_lana_num = lenum.lana[i];
		rc = Netbios(&ncb);
		if (rc) throw SystemException("cannot determine MAC address (NCBRESET failed)");

		memset(&ncb, 0, sizeof(ncb));
		ncb.ncb_command  = NCBASTAT;
		ncb.ncb_lana_num = lenum.lana[i];
		ncb.ncb_buffer   = (UCHAR*) &adapter;
		ncb.ncb_length   = sizeof(adapter);
		strcpy((char*) ncb.ncb_callname,  "*");
		rc = Netbios(&ncb);
		if (rc) throw SystemException("cannot determine MAC address (NCBASTAT failed)");
		if (adapter.adapt.adapter_type == 0xFE)
		{
			memcpy(_node, adapter.adapt.adapter_address, sizeof(_node));
			break;
		}
	}
}


Foundation_END


#elif defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX
//
// BSD variants
//
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if_dl.h>


Foundation_BEGIN


void UUIDGenerator::getNode()
{
	struct ifaddrs* ifaphead;
	int rc = getifaddrs(&ifaphead);
	if (rc) throw SystemException("cannot get network adapter list");

	for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next) 
	{
		if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK) 
		{
			struct sockaddr_dl* sdl = (struct sockaddr_dl*) ifap->ifa_addr;
			caddr_t ap = (caddr_t) (sdl->sdl_data + sdl->sdl_nlen);
			int alen = sdl->sdl_alen;
			if (ap && alen > 0) 
			{
				memcpy(_node, ap, sizeof(_node));
				break;
			}
		}
	}
	freeifaddrs(ifaphead);
}


Foundation_END


#elif POCO_OS == POCO_OS_LINUX
//
// Linux
//
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <unistd.h>


Foundation_BEGIN


void UUIDGenerator::getNode()
{
	struct ifreq ifr;

	int s = socket(PF_INET, SOCK_DGRAM, 0);
	if (s == -1) throw SystemException("cannot open socket");

	strcpy(ifr.ifr_name, "eth0");
	int rc = ioctl(s, SIOCGIFHWADDR, &ifr);
	close(s);
	if (rc < 0) throw SystemException("cannot get MAC address");
	struct sockaddr* sa = (struct sockaddr*) &ifr.ifr_addr;
	memcpy(_node, sa->sa_data, sizeof(_node));
}


Foundation_END


#elif defined(POCO_OS_FAMILY_UNIX) || defined(POCO_OS_FAMILY_VMS)
//
// Unix/VMS
//
#if defined(__VMS)
#include <ioctl.h>
#else
#include <sys/ioctl.h>
#endif
#if defined(sun) || defined(__sun)
#include <sys/sockio.h>
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <net/if.h>
#if defined(__VMS)
#include <inet.h>
#else
#include <arpa/inet.h>
#endif
#include <netdb.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <unistd.h>


#if defined(__VMS)
#define MAXHOSTNAMELEN 64
#endif


Foundation_BEGIN


void UUIDGenerator::getNode()
{
	char name[MAXHOSTNAMELEN];
	if (gethostname(name, sizeof(name)))
		throw SystemException("cannot get host name");

	struct hostent* pHost = gethostbyname(name);
	if (!pHost) throw SystemException("cannot get host IP address");

	int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (s == -1) throw SystemException("cannot open socket");

	struct arpreq ar;
	memset(&ar, 0, sizeof(ar));
	struct sockaddr_in* pAddr = (struct sockaddr_in*) &ar.arp_pa;
	pAddr->sin_family = AF_INET;
	memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
	int rc = ioctl(s, SIOCGARP, &ar);
	close(s);
	if (rc < 0) throw SystemException("cannot get MAC address");
	memcpy(_node, ar.arp_ha.sa_data, sizeof(_node));
}


Foundation_END


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -