From cfc9ce380ee9970f2d90cf9061541e239d06d261 Mon Sep 17 00:00:00 2001 From: Andrew Auclair Date: Thu, 14 Dec 2023 10:41:28 -0500 Subject: [PATCH] Throw exceptions when node ID retrieval is unsuccessful and support Wireless Adapters on Windows (#4336) --- Foundation/src/Environment_UNIX.cpp | 9 ++--- Foundation/src/Environment_WIN32U.cpp | 48 +++++++++++++++++++-------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Foundation/src/Environment_UNIX.cpp b/Foundation/src/Environment_UNIX.cpp index eb9042b9c..bb67887c0 100644 --- a/Foundation/src/Environment_UNIX.cpp +++ b/Foundation/src/Environment_UNIX.cpp @@ -352,15 +352,16 @@ namespace Poco { void EnvironmentImpl::nodeIdImpl(NodeId& id) { std::memset(&id, 0, sizeof(id)); + char name[MAXHOSTNAMELEN]; if (gethostname(name, sizeof(name))) - return; + throw SystemException("unable to get hostname"); 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); - if (s == -1) return; + if (s == -1) throw SystemException("unable to open socket"); struct arpreq 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)); int rc = ioctl(s, SIOCGARP, &ar); 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)); } diff --git a/Foundation/src/Environment_WIN32U.cpp b/Foundation/src/Environment_WIN32U.cpp index 561e9de61..e561f0668 100644 --- a/Foundation/src/Environment_WIN32U.cpp +++ b/Foundation/src/Environment_WIN32U.cpp @@ -202,38 +202,60 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id) { std::memset(&id, 0, sizeof(id)); - PIP_ADAPTER_INFO pAdapterInfo; - PIP_ADAPTER_INFO pAdapter = 0; + auto pAdapterInfo = std::make_unique(1); 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); + const DWORD rc = GetAdaptersInfo(pAdapterInfo.get(), &len); + if (rc == ERROR_BUFFER_OVERFLOW) { - delete [] reinterpret_cast(pAdapterInfo); - pAdapterInfo = reinterpret_cast(new char[len]); + pAdapterInfo = std::make_unique(len / sizeof(IP_ADAPTER_INFO)); } else if (rc != ERROR_SUCCESS) { - delete[] reinterpret_cast(pAdapterInfo); throw SystemException("cannot get network adapter list"); } - if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR) + + if (GetAdaptersInfo(pAdapterInfo.get(), &len) == NO_ERROR) { - pAdapter = pAdapterInfo; - bool found = false; - while (pAdapter && !found) + IP_ADAPTER_INFO* pAdapter = pAdapterInfo.get(); + + while (pAdapter) { if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id)) { - found = true; 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; } } - delete [] reinterpret_cast(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"); }