mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-13 10:32:57 +01:00
added Environment::nodeId()
This commit is contained in:
parent
b5b35cb173
commit
5ae8225ece
@ -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.
|
||||
};
|
||||
|
||||
|
||||
|
@ -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<std::string, std::string> StringMap;
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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&);
|
||||
|
@ -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 <cstdlib>
|
||||
|
||||
|
||||
#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
|
||||
|
@ -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 <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if_dl.h>
|
||||
|
||||
|
||||
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<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)
|
||||
{
|
||||
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 <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
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<struct sockaddr*>(&ifr.ifr_addr);
|
||||
std::memcpy(&id, sa->sa_data, sizeof(id));
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#elif defined(POCO_OS_FAMILY_UNIX)
|
||||
//
|
||||
// General Unix
|
||||
//
|
||||
#include <sys/ioctl.h>
|
||||
#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>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
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<struct sockaddr_in*>(&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
|
||||
|
@ -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 <syidef.h>
|
||||
#include <iledef.h>
|
||||
#include <lnmdef.h>
|
||||
#include <ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <inet.h>
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#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<struct sockaddr_in*>(&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
|
||||
|
@ -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 <sstream>
|
||||
#include "Poco/UnWindows.h"
|
||||
#include <iphlpapi.h>
|
||||
|
||||
|
||||
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<IP_ADAPTER_INFO*>(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<char*>(pAdapterInfo);
|
||||
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(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<char*>(pAdapterInfo);
|
||||
throw;
|
||||
}
|
||||
delete [] reinterpret_cast<char*>(pAdapterInfo);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
@ -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 <sstream>
|
||||
#include <cstring>
|
||||
#include "Poco/UnWindows.h"
|
||||
#include <iphlpapi.h>
|
||||
|
||||
|
||||
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<IP_ADAPTER_INFO*>(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<char*>(pAdapterInfo);
|
||||
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(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<char*>(pAdapterInfo);
|
||||
throw;
|
||||
}
|
||||
delete [] reinterpret_cast<char*>(pAdapterInfo);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
@ -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 <algorithm>
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
@ -46,6 +45,7 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <utime.h>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
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;
|
||||
|
@ -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;
|
||||
|
@ -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 <iphlpapi.h>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
void UUIDGenerator::getNode()
|
||||
{
|
||||
PIP_ADAPTER_INFO pAdapterInfo;
|
||||
PIP_ADAPTER_INFO pAdapter = 0;
|
||||
ULONG len = sizeof(IP_ADAPTER_INFO);
|
||||
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(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<char*>(pAdapterInfo);
|
||||
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(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<char*>(pAdapterInfo);
|
||||
throw;
|
||||
}
|
||||
delete [] reinterpret_cast<char*>(pAdapterInfo);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#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>
|
||||
|
||||
|
||||
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<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)
|
||||
{
|
||||
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 <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
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<struct sockaddr*>(&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 <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
|
||||
|
||||
|
||||
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<struct sockaddr_in*>(&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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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&)
|
||||
|
Loading…
Reference in New Issue
Block a user