From 5ae8225ece70197649068b9d07ca01b98e02562d Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Thu, 18 Sep 2008 15:49:05 +0000 Subject: [PATCH] added Environment::nodeId() --- Foundation/include/Poco/Environment.h | 16 +- Foundation/include/Poco/Environment_UNIX.h | 5 +- Foundation/include/Poco/Environment_VMS.h | 5 +- Foundation/include/Poco/Environment_WIN32.h | 5 +- Foundation/include/Poco/Environment_WIN32U.h | 5 +- Foundation/include/Poco/File.h | 8 +- Foundation/include/Poco/UUIDGenerator.h | 15 +- Foundation/src/Environment.cpp | 25 ++- Foundation/src/Environment_UNIX.cpp | 134 +++++++++++- Foundation/src/Environment_VMS.cpp | 39 +++- Foundation/src/Environment_WIN32.cpp | 49 ++++- Foundation/src/Environment_WIN32U.cpp | 49 ++++- Foundation/src/File_UNIX.cpp | 18 +- Foundation/src/File_VMS.cpp | 4 +- Foundation/src/UUIDGenerator.cpp | 211 +------------------ Foundation/testsuite/src/CoreTest.cpp | 3 +- Foundation/testsuite/src/PathTest.cpp | 4 +- 17 files changed, 359 insertions(+), 236 deletions(-) diff --git a/Foundation/include/Poco/Environment.h b/Foundation/include/Poco/Environment.h index bf2dcdaf5..9676aec91 100644 --- a/Foundation/include/Poco/Environment.h +++ b/Foundation/include/Poco/Environment.h @@ -1,7 +1,7 @@ // // Environment.h // -// $Id: //poco/svn/Foundation/include/Poco/Environment.h#2 $ +// $Id: //poco/1.3/Foundation/include/Poco/Environment.h#2 $ // // Library: Foundation // Package: Core @@ -51,6 +51,8 @@ class Foundation_API Environment /// and some general system information. { public: + typedef UInt8 NodeId[6]; /// Ethernet address. + static std::string get(const std::string& name); /// Returns the value of the environment variable /// with the given name. Throws a NotFoundException @@ -80,6 +82,18 @@ public: static std::string nodeName(); /// Returns the node (or host) name. + + static void nodeId(NodeId& id); + /// Returns the Ethernet address of the first Ethernet + /// adapter found on the system. + /// + /// Throws a SystemException if no Ethernet adapter is available. + + static std::string nodeId(); + /// Returns the Ethernet address (format "xx:xx:xx:xx:xx:xx") + /// of the first Ethernet adapter found on the system. + /// + /// Throws a SystemException if no Ethernet adapter is available. }; diff --git a/Foundation/include/Poco/Environment_UNIX.h b/Foundation/include/Poco/Environment_UNIX.h index 285f6d88a..cb097324b 100644 --- a/Foundation/include/Poco/Environment_UNIX.h +++ b/Foundation/include/Poco/Environment_UNIX.h @@ -1,7 +1,7 @@ // // Environment_UNIX.h // -// $Id: //poco/svn/Foundation/include/Poco/Environment_UNIX.h#2 $ +// $Id: //poco/1.3/Foundation/include/Poco/Environment_UNIX.h#2 $ // // Library: Foundation // Package: Core @@ -51,6 +51,8 @@ namespace Poco { class Foundation_API EnvironmentImpl { public: + typedef UInt8 NodeId[6]; /// Ethernet address. + static std::string getImpl(const std::string& name); static bool hasImpl(const std::string& name); static void setImpl(const std::string& name, const std::string& value); @@ -58,6 +60,7 @@ public: static std::string osVersionImpl(); static std::string osArchitectureImpl(); static std::string nodeNameImpl(); + static void nodeIdImpl(NodeId& id); private: typedef std::map StringMap; diff --git a/Foundation/include/Poco/Environment_VMS.h b/Foundation/include/Poco/Environment_VMS.h index 769c5e9e2..799572e64 100644 --- a/Foundation/include/Poco/Environment_VMS.h +++ b/Foundation/include/Poco/Environment_VMS.h @@ -1,7 +1,7 @@ // // Environment_VMS.h // -// $Id: //poco/svn/Foundation/include/Poco/Environment_VMS.h#2 $ +// $Id: //poco/1.3/Foundation/include/Poco/Environment_VMS.h#2 $ // // Library: Foundation // Package: Core @@ -50,6 +50,8 @@ namespace Poco { class Foundation_API EnvironmentImpl { public: + typedef UInt8 NodeId[6]; /// Ethernet address. + static std::string getImpl(const std::string& name); static bool hasImpl(const std::string& name); static void setImpl(const std::string& name, const std::string& value); @@ -57,6 +59,7 @@ public: static std::string osVersionImpl(); static std::string osArchitectureImpl(); static std::string nodeNameImpl(); + static void nodeIdImpl(NodeId& id); static std::string getsyi(unsigned short code); /// a wrapper for $GETSYIW diff --git a/Foundation/include/Poco/Environment_WIN32.h b/Foundation/include/Poco/Environment_WIN32.h index 64962d32e..b3730c8b7 100644 --- a/Foundation/include/Poco/Environment_WIN32.h +++ b/Foundation/include/Poco/Environment_WIN32.h @@ -1,7 +1,7 @@ // // Environment_WIN32.h // -// $Id: //poco/svn/Foundation/include/Poco/Environment_WIN32.h#2 $ +// $Id: //poco/1.3/Foundation/include/Poco/Environment_WIN32.h#2 $ // // Library: Foundation // Package: Core @@ -49,6 +49,8 @@ namespace Poco { class Foundation_API EnvironmentImpl { public: + typedef UInt8 NodeId[6]; /// Ethernet address. + static std::string getImpl(const std::string& name); static bool hasImpl(const std::string& name); static void setImpl(const std::string& name, const std::string& value); @@ -56,6 +58,7 @@ public: static std::string osVersionImpl(); static std::string osArchitectureImpl(); static std::string nodeNameImpl(); + static void nodeIdImpl(NodeId& id); }; diff --git a/Foundation/include/Poco/Environment_WIN32U.h b/Foundation/include/Poco/Environment_WIN32U.h index 83eb23dc5..23afe3bda 100644 --- a/Foundation/include/Poco/Environment_WIN32U.h +++ b/Foundation/include/Poco/Environment_WIN32U.h @@ -1,7 +1,7 @@ // // Environment_WIN32U.h // -// $Id: //poco/svn/Foundation/include/Poco/Environment_WIN32U.h#2 $ +// $Id: //poco/1.3/Foundation/include/Poco/Environment_WIN32U.h#2 $ // // Library: Foundation // Package: Core @@ -49,6 +49,8 @@ namespace Poco { class Foundation_API EnvironmentImpl { public: + typedef UInt8 NodeId[6]; /// Ethernet address. + static std::string getImpl(const std::string& name); static bool hasImpl(const std::string& name); static void setImpl(const std::string& name, const std::string& value); @@ -56,6 +58,7 @@ public: static std::string osVersionImpl(); static std::string osArchitectureImpl(); static std::string nodeNameImpl(); + static void nodeIdImpl(NodeId& id); }; diff --git a/Foundation/include/Poco/File.h b/Foundation/include/Poco/File.h index 3284d04a3..62fb8d856 100644 --- a/Foundation/include/Poco/File.h +++ b/Foundation/include/Poco/File.h @@ -1,7 +1,7 @@ // // File.h // -// $Id: //poco/svn/Foundation/include/Poco/File.h#3 $ +// $Id: //poco/1.3/Foundation/include/Poco/File.h#6 $ // // Library: Foundation // Package: Filesystem @@ -141,6 +141,12 @@ public: Timestamp created() const; /// Returns the creation date of the file. + /// + /// Not all platforms or filesystems (e.g. Linux and most Unix + /// platforms with the exception of FreeBSD and Mac OS X) + /// maintain the creation date of a file. + /// On such platforms, created() returns + /// the time of the last inode modification. Timestamp getLastModified() const; /// Returns the modification date of the file. diff --git a/Foundation/include/Poco/UUIDGenerator.h b/Foundation/include/Poco/UUIDGenerator.h index b03a01069..fcff505c0 100644 --- a/Foundation/include/Poco/UUIDGenerator.h +++ b/Foundation/include/Poco/UUIDGenerator.h @@ -1,7 +1,7 @@ // // UUIDGenerator.h // -// $Id: //poco/svn/Foundation/include/Poco/UUIDGenerator.h#2 $ +// $Id: //poco/1.3/Foundation/include/Poco/UUIDGenerator.h#3 $ // // Library: Foundation // Package: UUID @@ -45,6 +45,7 @@ #include "Poco/Mutex.h" #include "Poco/Random.h" #include "Poco/Timestamp.h" +#include "Poco/Environment.h" namespace Poco { @@ -100,12 +101,12 @@ protected: void getNode(); private: - FastMutex _mutex; - Random _random; - Timestamp _lastTime; - int _ticks; - UInt8 _node[6]; - bool _haveNode; + FastMutex _mutex; + Random _random; + Timestamp _lastTime; + int _ticks; + Environment::NodeId _node; + bool _haveNode; UUIDGenerator(const UUIDGenerator&); UUIDGenerator& operator = (const UUIDGenerator&); diff --git a/Foundation/src/Environment.cpp b/Foundation/src/Environment.cpp index 45133354a..fcc805c3a 100644 --- a/Foundation/src/Environment.cpp +++ b/Foundation/src/Environment.cpp @@ -1,7 +1,7 @@ // // Environment.cpp // -// $Id: //poco/svn/Foundation/src/Environment.cpp#2 $ +// $Id: //poco/1.3/Foundation/src/Environment.cpp#2 $ // // Library: Foundation // Package: Core @@ -35,6 +35,7 @@ #include "Poco/Environment.h" +#include #if defined(POCO_OS_FAMILY_VMS) @@ -102,4 +103,26 @@ std::string Environment::nodeName() } +std::string Environment::nodeId() +{ + NodeId id; + nodeId(id); + char result[18]; + std::sprintf(result, "%02x:%02x:%02x:%02x:%02x:%02x", + id[0], + id[1], + id[2], + id[3], + id[4], + id[5]); + return std::string(result); +} + + +void Environment::nodeId(NodeId& id) +{ + return EnvironmentImpl::nodeIdImpl(id); +} + + } // namespace Poco diff --git a/Foundation/src/Environment_UNIX.cpp b/Foundation/src/Environment_UNIX.cpp index 13f3b808d..8526eb20b 100644 --- a/Foundation/src/Environment_UNIX.cpp +++ b/Foundation/src/Environment_UNIX.cpp @@ -1,7 +1,7 @@ // // Environment_UNIX.cpp // -// $Id: //poco/svn/Foundation/src/Environment_UNIX.cpp#2 $ +// $Id: //poco/1.3/Foundation/src/Environment_UNIX.cpp#3 $ // // Library: Foundation // Package: Core @@ -118,3 +118,135 @@ std::string EnvironmentImpl::nodeNameImpl() } // namespace Poco + + +// +// nodeIdImpl +// +#if defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX +// +// BSD variants +// +#include +#include +#include +#include + + +namespace Poco { + + +void EnvironmentImpl::nodeIdImpl(NodeId& id) +{ + struct ifaddrs* ifaphead; + int rc = getifaddrs(&ifaphead); + if (rc) throw SystemException("cannot get network adapter list"); + + bool foundAdapter = false; + 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 = reinterpret_cast(ifap->ifa_addr); + caddr_t ap = (caddr_t) (sdl->sdl_data + sdl->sdl_nlen); + int alen = sdl->sdl_alen; + if (ap && alen > 0) + { + std::memcpy(&id, ap, sizeof(id)); + foundAdapter = true; + break; + } + } + } + freeifaddrs(ifaphead); + if (!foundAdapter) throw SystemException("cannot determine MAC address (no suitable network adapter found)"); +} + + +} // namespace Poco + + +#elif defined(__CYGWIN__) || POCO_OS == POCO_OS_LINUX +// +// Linux, Cygwin +// +#include +#include +#include +#include +#include +#include + + +namespace Poco { + + +void EnvironmentImpl::nodeIdImpl(NodeId& id) +{ + 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 = reinterpret_cast(&ifr.ifr_addr); + std::memcpy(&id, sa->sa_data, sizeof(id)); +} + + +} // namespace Poco + + +#elif defined(POCO_OS_FAMILY_UNIX) +// +// General Unix +// +#include +#if defined(sun) || defined(__sun) +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace Poco { + + +void EnvironmentImpl::nodeIdImpl(NodeId& id) +{ + 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; + std::memset(&ar, 0, sizeof(ar)); + struct sockaddr_in* pAddr = reinterpret_cast(&ar.arp_pa); + pAddr->sin_family = AF_INET; + std::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"); + std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id)); +} + + +} // namespace Poco + + +#endif diff --git a/Foundation/src/Environment_VMS.cpp b/Foundation/src/Environment_VMS.cpp index d30eee9a7..6aa31fb5b 100644 --- a/Foundation/src/Environment_VMS.cpp +++ b/Foundation/src/Environment_VMS.cpp @@ -1,7 +1,7 @@ // // Environment_VMS.cpp // -// $Id: //poco/svn/Foundation/src/Environment_VMS.cpp#2 $ +// $Id: //poco/1.3/Foundation/src/Environment_VMS.cpp#2 $ // // Library: Foundation // Package: Core @@ -43,6 +43,19 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define MAXHOSTNAMELEN 64 namespace Poco { @@ -108,6 +121,30 @@ std::string EnvironmentImpl::nodeNameImpl() } +void EnvironmentImpl::nodeIdImpl(NodeId& id) +{ + 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; + std::memset(&ar, 0, sizeof(ar)); + struct sockaddr_in* pAddr = reinterpret_cast(&ar.arp_pa); + pAddr->sin_family = AF_INET; + std::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"); + std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id)); +} + + std::string EnvironmentImpl::getsyi(unsigned short code) { #pragma pointer_size save diff --git a/Foundation/src/Environment_WIN32.cpp b/Foundation/src/Environment_WIN32.cpp index 7454f77cb..774dc5e21 100644 --- a/Foundation/src/Environment_WIN32.cpp +++ b/Foundation/src/Environment_WIN32.cpp @@ -1,7 +1,7 @@ // // Environment_WIN32.cpp // -// $Id: //poco/svn/Foundation/src/Environment_WIN32.cpp#2 $ +// $Id: //poco/1.3/Foundation/src/Environment_WIN32.cpp#4 $ // // Library: Foundation // Package: Core @@ -38,6 +38,7 @@ #include "Poco/Exception.h" #include #include "Poco/UnWindows.h" +#include namespace Poco { @@ -144,4 +145,50 @@ std::string EnvironmentImpl::nodeNameImpl() } +void EnvironmentImpl::nodeIdImpl(NodeId& id) +{ + PIP_ADAPTER_INFO pAdapterInfo; + PIP_ADAPTER_INFO pAdapter = 0; + ULONG len = sizeof(IP_ADAPTER_INFO); + pAdapterInfo = reinterpret_cast(new char[len]); + // Make an initial call to GetAdaptersInfo to get + // the necessary size into len + DWORD rc = GetAdaptersInfo(pAdapterInfo, &len); + if (rc == ERROR_BUFFER_OVERFLOW) + { + delete [] reinterpret_cast(pAdapterInfo); + pAdapterInfo = reinterpret_cast(new char[len]); + } + else if (rc != ERROR_SUCCESS) + { + throw SystemException("cannot get network adapter list"); + } + try + { + bool found = false; + if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR) + { + pAdapter = pAdapterInfo; + while (pAdapter && !found) + { + if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id)) + { + std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength); + found = true; + } + pAdapter = pAdapter->Next; + } + } + else throw SystemException("cannot get network adapter list"); + if (!found) throw SystemException("no Ethernet adapter found"); + } + catch (Exception&) + { + delete [] reinterpret_cast(pAdapterInfo); + throw; + } + delete [] reinterpret_cast(pAdapterInfo); +} + + } // namespace Poco diff --git a/Foundation/src/Environment_WIN32U.cpp b/Foundation/src/Environment_WIN32U.cpp index 57311ac20..42d87f523 100644 --- a/Foundation/src/Environment_WIN32U.cpp +++ b/Foundation/src/Environment_WIN32U.cpp @@ -1,7 +1,7 @@ // // Environment_WIN32U.cpp // -// $Id: //poco/svn/Foundation/src/Environment_WIN32U.cpp#2 $ +// $Id: //poco/1.3/Foundation/src/Environment_WIN32U.cpp#4 $ // // Library: Foundation // Package: Core @@ -41,6 +41,7 @@ #include #include #include "Poco/UnWindows.h" +#include namespace Poco { @@ -159,4 +160,50 @@ std::string EnvironmentImpl::nodeNameImpl() } +void EnvironmentImpl::nodeIdImpl(NodeId& id) +{ + PIP_ADAPTER_INFO pAdapterInfo; + PIP_ADAPTER_INFO pAdapter = 0; + ULONG len = sizeof(IP_ADAPTER_INFO); + pAdapterInfo = reinterpret_cast(new char[len]); + // Make an initial call to GetAdaptersInfo to get + // the necessary size into len + DWORD rc = GetAdaptersInfo(pAdapterInfo, &len); + if (rc == ERROR_BUFFER_OVERFLOW) + { + delete [] reinterpret_cast(pAdapterInfo); + pAdapterInfo = reinterpret_cast(new char[len]); + } + else if (rc != ERROR_SUCCESS) + { + throw SystemException("cannot get network adapter list"); + } + try + { + bool found = false; + if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR) + { + pAdapter = pAdapterInfo; + while (pAdapter && !found) + { + if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id)) + { + std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength); + found = true; + } + pAdapter = pAdapter->Next; + } + } + else throw SystemException("cannot get network adapter list"); + if (!found) throw SystemException("no Ethernet adapter found"); + } + catch (Exception&) + { + delete [] reinterpret_cast(pAdapterInfo); + throw; + } + delete [] reinterpret_cast(pAdapterInfo); +} + + } // namespace Poco diff --git a/Foundation/src/File_UNIX.cpp b/Foundation/src/File_UNIX.cpp index 62032bc43..91c64ae94 100644 --- a/Foundation/src/File_UNIX.cpp +++ b/Foundation/src/File_UNIX.cpp @@ -1,7 +1,7 @@ // // File_UNIX.cpp // -// $Id: //poco/svn/Foundation/src/File_UNIX.cpp#3 $ +// $Id: //poco/1.3/Foundation/src/File_UNIX.cpp#10 $ // // Library: Foundation // Package: Filesystem @@ -38,7 +38,6 @@ #include "Poco/Buffer.h" #include "Poco/Exception.h" #include -#include #include #include #include @@ -46,6 +45,7 @@ #include #include #include +#include namespace Poco { @@ -97,7 +97,7 @@ bool FileImpl::canReadImpl() const { poco_assert (!_path.empty()); - if(geteuid() == 0) + if (geteuid() == 0) return true; struct stat st; @@ -119,7 +119,7 @@ bool FileImpl::canWriteImpl() const { poco_assert (!_path.empty()); - if(geteuid() == 0) + if (geteuid() == 0) return true; struct stat st; @@ -209,9 +209,19 @@ Timestamp FileImpl::createdImpl() const { poco_assert (!_path.empty()); +#if defined(__APPLE__) + struct stat64 st; + if (stat64(_path.c_str(), &st) == 0) + return Timestamp::fromEpochTime(st.st_birthtime); +#elif defined(__FreeBSD__) + struct stat st; + if (stat(_path.c_str(), &st) == 0) + return Timestamp::fromEpochTime(st.st_birthtime); +#else struct stat st; if (stat(_path.c_str(), &st) == 0) return Timestamp::fromEpochTime(st.st_ctime); +#endif else handleLastErrorImpl(_path); return 0; diff --git a/Foundation/src/File_VMS.cpp b/Foundation/src/File_VMS.cpp index 3864c9943..17992f709 100644 --- a/Foundation/src/File_VMS.cpp +++ b/Foundation/src/File_VMS.cpp @@ -1,7 +1,7 @@ // // File_VMS.cpp // -// $Id: //poco/svn/Foundation/src/File_VMS.cpp#3 $ +// $Id: //poco/1.3/Foundation/src/File_VMS.cpp#4 $ // // Library: Foundation // Package: Filesystem @@ -186,7 +186,7 @@ Timestamp FileImpl::createdImpl() const struct stat st; if (stat(_path.c_str(), &st) == 0) - return Timestamp(st.st_mtime); + return Timestamp(st.st_ctime); else handleLastErrorImpl(_path); return 0; diff --git a/Foundation/src/UUIDGenerator.cpp b/Foundation/src/UUIDGenerator.cpp index aeb3d2839..2b7b0d3ae 100644 --- a/Foundation/src/UUIDGenerator.cpp +++ b/Foundation/src/UUIDGenerator.cpp @@ -1,7 +1,7 @@ // // UUIDGenerator.cpp // -// $Id: //poco/svn/Foundation/src/UUIDGenerator.cpp#2 $ +// $Id: //poco/1.3/Foundation/src/UUIDGenerator.cpp#4 $ // // Library: Foundation // Package: UUID @@ -62,7 +62,7 @@ UUID UUIDGenerator::create() if (!_haveNode) { - getNode(); + Environment::nodeId(_node); _haveNode = true; } Timestamp::UtcTimeVal tv = timeStamp(); @@ -153,210 +153,3 @@ UUIDGenerator& UUIDGenerator::defaultGenerator() } // namespace Poco - - -// -// platform-specific code below -// - - -#if defined(POCO_OS_FAMILY_WINDOWS) -// -// Windows -// -#include "Poco/UnWindows.h" -#include - - -namespace Poco { - - -void UUIDGenerator::getNode() -{ - PIP_ADAPTER_INFO pAdapterInfo; - PIP_ADAPTER_INFO pAdapter = 0; - ULONG len = sizeof(IP_ADAPTER_INFO); - pAdapterInfo = reinterpret_cast(new char[len]); - // Make an initial call to GetAdaptersInfo to get - // the necessary size into len - DWORD rc = GetAdaptersInfo(pAdapterInfo, &len); - if (rc == ERROR_BUFFER_OVERFLOW) - { - delete [] reinterpret_cast(pAdapterInfo); - pAdapterInfo = reinterpret_cast(new char[len]); - } - else if (rc != ERROR_SUCCESS) - { - throw SystemException("cannot get network adapter list"); - } - try - { - bool found = false; - if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR) - { - pAdapter = pAdapterInfo; - while (pAdapter && !found) - { - if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(_node)) - { - std::memcpy(_node, pAdapter->Address, pAdapter->AddressLength); - found = true; - } - pAdapter = pAdapter->Next; - } - } - else throw SystemException("cannot get network adapter list"); - if (!found) throw SystemException("no Ethernet adapter found"); - } - catch (Exception&) - { - delete [] reinterpret_cast(pAdapterInfo); - throw; - } - delete [] reinterpret_cast(pAdapterInfo); -} - - -} // namespace Poco - - -#elif defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX -// -// BSD variants -// -#include -#include -#include -#include - - -namespace Poco { - - -void UUIDGenerator::getNode() -{ - struct ifaddrs* ifaphead; - int rc = getifaddrs(&ifaphead); - if (rc) throw SystemException("cannot get network adapter list"); - - bool foundAdapter = false; - 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 = reinterpret_cast(ifap->ifa_addr); - caddr_t ap = (caddr_t) (sdl->sdl_data + sdl->sdl_nlen); - int alen = sdl->sdl_alen; - if (ap && alen > 0) - { - std::memcpy(_node, ap, sizeof(_node)); - foundAdapter = true; - break; - } - } - } - freeifaddrs(ifaphead); - if (!foundAdapter) throw SystemException("cannot determine MAC address (no suitable network adapter found)"); -} - - -} // namespace Poco - - -#elif defined(__CYGWIN__) || POCO_OS == POCO_OS_LINUX -// -// Linux -// -#include -#include -#include -#include -#include -#include - - -namespace Poco { - - -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 = reinterpret_cast(&ifr.ifr_addr); - std::memcpy(_node, sa->sa_data, sizeof(_node)); -} - - -} // namespace Poco - - -#elif defined(POCO_OS_FAMILY_UNIX) || defined(POCO_OS_FAMILY_VMS) -// -// Unix/VMS -// -#if defined(__VMS) -#include -#else -#include -#endif -#if defined(sun) || defined(__sun) -#include -#endif -#include -#include -#include -#include -#if defined(__VMS) -#include -#else -#include -#endif -#include -#include -#include -#include - - -#if defined(__VMS) -#define MAXHOSTNAMELEN 64 -#endif - - -namespace Poco { - - -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; - std::memset(&ar, 0, sizeof(ar)); - struct sockaddr_in* pAddr = reinterpret_cast(&ar.arp_pa); - pAddr->sin_family = AF_INET; - std::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"); - std::memcpy(_node, ar.arp_ha.sa_data, sizeof(_node)); -} - - -} // namespace Poco - - -#endif diff --git a/Foundation/testsuite/src/CoreTest.cpp b/Foundation/testsuite/src/CoreTest.cpp index 4ed015024..9e281e927 100644 --- a/Foundation/testsuite/src/CoreTest.cpp +++ b/Foundation/testsuite/src/CoreTest.cpp @@ -1,7 +1,7 @@ // // CoreTest.cpp // -// $Id: //poco/svn/Foundation/testsuite/src/CoreTest.cpp#2 $ +// $Id: //poco/1.3/Foundation/testsuite/src/CoreTest.cpp#4 $ // // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // and Contributors. @@ -143,6 +143,7 @@ void CoreTest::testEnvironment() std::cout << "OS Version: " << Environment::osVersion() << std::endl; std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl; std::cout << "Node Name: " << Environment::nodeName() << std::endl; + std::cout << "Node ID: " << Environment::nodeId() << std::endl; } diff --git a/Foundation/testsuite/src/PathTest.cpp b/Foundation/testsuite/src/PathTest.cpp index afd62d429..17e24518c 100644 --- a/Foundation/testsuite/src/PathTest.cpp +++ b/Foundation/testsuite/src/PathTest.cpp @@ -1,7 +1,7 @@ // // PathTest.cpp // -// $Id: //poco/svn/Foundation/testsuite/src/PathTest.cpp#2 $ +// $Id: //poco/1.3/Foundation/testsuite/src/PathTest.cpp#2 $ // // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // and Contributors. @@ -822,7 +822,7 @@ void PathTest::testParseWindows4() try { - p.parse("ü:\\", Path::PATH_WINDOWS); + p.parse("~:\\", Path::PATH_WINDOWS); fail("bad path - must throw exception"); } catch (PathSyntaxException&)