mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-06 05:00:04 +01:00
Throw exceptions when node ID retrieval is unsuccessful and support Wireless Adapters on Windows (#4336)
This commit is contained in:
@@ -352,15 +352,16 @@ namespace Poco {
|
|||||||
void EnvironmentImpl::nodeIdImpl(NodeId& id)
|
void EnvironmentImpl::nodeIdImpl(NodeId& id)
|
||||||
{
|
{
|
||||||
std::memset(&id, 0, sizeof(id));
|
std::memset(&id, 0, sizeof(id));
|
||||||
|
|
||||||
char name[MAXHOSTNAMELEN];
|
char name[MAXHOSTNAMELEN];
|
||||||
if (gethostname(name, sizeof(name)))
|
if (gethostname(name, sizeof(name)))
|
||||||
return;
|
throw SystemException("unable to get hostname");
|
||||||
|
|
||||||
struct hostent* pHost = gethostbyname(name);
|
struct hostent* pHost = gethostbyname(name);
|
||||||
if (!pHost) return;
|
if (!pHost) throw SystemException("unable to get host");
|
||||||
|
|
||||||
int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (s == -1) return;
|
if (s == -1) throw SystemException("unable to open socket");
|
||||||
|
|
||||||
struct arpreq ar;
|
struct arpreq ar;
|
||||||
std::memset(&ar, 0, sizeof(ar));
|
std::memset(&ar, 0, sizeof(ar));
|
||||||
@@ -369,7 +370,7 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
|
|||||||
std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
|
std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
|
||||||
int rc = ioctl(s, SIOCGARP, &ar);
|
int rc = ioctl(s, SIOCGARP, &ar);
|
||||||
close(s);
|
close(s);
|
||||||
if (rc < 0) return;
|
if (rc < 0) throw SystemException("unable to get socket data");
|
||||||
std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
|
std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -202,38 +202,60 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
|
|||||||
{
|
{
|
||||||
std::memset(&id, 0, sizeof(id));
|
std::memset(&id, 0, sizeof(id));
|
||||||
|
|
||||||
PIP_ADAPTER_INFO pAdapterInfo;
|
auto pAdapterInfo = std::make_unique<IP_ADAPTER_INFO[]>(1);
|
||||||
PIP_ADAPTER_INFO pAdapter = 0;
|
|
||||||
ULONG len = sizeof(IP_ADAPTER_INFO);
|
ULONG len = sizeof(IP_ADAPTER_INFO);
|
||||||
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
|
|
||||||
// Make an initial call to GetAdaptersInfo to get
|
// Make an initial call to GetAdaptersInfo to get
|
||||||
// the necessary size into len
|
// the necessary size into len
|
||||||
DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
|
const DWORD rc = GetAdaptersInfo(pAdapterInfo.get(), &len);
|
||||||
|
|
||||||
if (rc == ERROR_BUFFER_OVERFLOW)
|
if (rc == ERROR_BUFFER_OVERFLOW)
|
||||||
{
|
{
|
||||||
delete [] reinterpret_cast<char*>(pAdapterInfo);
|
pAdapterInfo = std::make_unique<IP_ADAPTER_INFO[]>(len / sizeof(IP_ADAPTER_INFO));
|
||||||
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
|
|
||||||
}
|
}
|
||||||
else if (rc != ERROR_SUCCESS)
|
else if (rc != ERROR_SUCCESS)
|
||||||
{
|
{
|
||||||
delete[] reinterpret_cast<char*>(pAdapterInfo);
|
|
||||||
throw SystemException("cannot get network adapter list");
|
throw SystemException("cannot get network adapter list");
|
||||||
}
|
}
|
||||||
if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
|
|
||||||
|
if (GetAdaptersInfo(pAdapterInfo.get(), &len) == NO_ERROR)
|
||||||
{
|
{
|
||||||
pAdapter = pAdapterInfo;
|
IP_ADAPTER_INFO* pAdapter = pAdapterInfo.get();
|
||||||
bool found = false;
|
|
||||||
while (pAdapter && !found)
|
while (pAdapter)
|
||||||
{
|
{
|
||||||
if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
|
if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
|
||||||
{
|
{
|
||||||
found = true;
|
|
||||||
std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
|
std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
|
||||||
|
|
||||||
|
// found an ethernet adapter, we can return now
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pAdapter = pAdapter->Next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if an ethernet adapter was not found, search for a wifi adapter
|
||||||
|
pAdapter = pAdapterInfo.get();
|
||||||
|
|
||||||
|
while (pAdapter)
|
||||||
|
{
|
||||||
|
if (pAdapter->Type == IF_TYPE_IEEE80211 && pAdapter->AddressLength == sizeof(id))
|
||||||
|
{
|
||||||
|
std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
|
||||||
|
|
||||||
|
// found a wifi adapter, we can return now
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
pAdapter = pAdapter->Next;
|
pAdapter = pAdapter->Next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete [] reinterpret_cast<char*>(pAdapterInfo);
|
else
|
||||||
|
{
|
||||||
|
throw SystemException("cannot get network adapter list");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ethernet and wifi adapters not found, fail the search
|
||||||
|
throw SystemException("no ethernet or wifi adapter found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user