diff --git a/internal/platform/implementation/windows/utils.cc b/internal/platform/implementation/windows/utils.cc index 563c8613..28344e56 100644 --- a/internal/platform/implementation/windows/utils.cc +++ b/internal/platform/implementation/windows/utils.cc @@ -14,11 +14,7 @@ #include "internal/platform/implementation/windows/utils.h" -// Windows headers -#include -#include -#include -#include +#include // Standard C/C++ headers #include @@ -37,8 +33,9 @@ #include "internal/platform/bluetooth_utils.h" #include "internal/platform/byte_array.h" #include "internal/platform/implementation/crypto.h" -#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.Collections.h" -#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Connectivity.h" +#include "internal/platform/logging.h" +#include "winrt/Windows.Foundation.Collections.h" +#include "winrt/Windows.Networking.Connectivity.h" namespace nearby { namespace windows { @@ -110,13 +107,65 @@ std::string wstring_to_string(std::wstring wstr) { std::vector GetIpv4Addresses() { std::vector result; - auto host_names = NetworkInformation::GetHostNames(); - for (const auto& host_name : host_names) { - if (host_name.IPInformation() != nullptr && - host_name.IPInformation().NetworkAdapter() != nullptr && - host_name.Type() == HostNameType::Ipv4) { - result.push_back(winrt::to_string(host_name.ToString())); + try { + auto host_names = NetworkInformation::GetHostNames(); + for (const auto& host_name : host_names) { + if (host_name.IPInformation() != nullptr && + host_name.IPInformation().NetworkAdapter() != nullptr && + host_name.Type() == HostNameType::Ipv4) { + result.push_back(winrt::to_string(host_name.ToString())); + } } + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ + << ": Cannot get IPv4 addresses. Exception : " + << exception.what(); + } catch (const winrt::hresult_error& error) { + NEARBY_LOGS(ERROR) << __func__ + << ": Cannot get IPv4 addresses. WinRT exception: " + << error.code() << ": " + << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption."; + } + + return result; +} + +std::vector Get4BytesIpv4Addresses() { + std::vector result; + try { + auto host_names = NetworkInformation::GetHostNames(); + for (auto host_name : host_names) { + if (host_name.IPInformation() != nullptr && + host_name.IPInformation().NetworkAdapter() != nullptr && + host_name.Type() == HostNameType::Ipv4) { + std::string ipv4_s = winrt::to_string(host_name.ToString()); + // Converts IP address from x.x.x.x to 4 bytes format. + in_addr address; + address.S_un.S_addr = inet_addr(ipv4_s.c_str()); + char ipv4_b[5]; + ipv4_b[0] = address.S_un.S_un_b.s_b1; + ipv4_b[1] = address.S_un.S_un_b.s_b2; + ipv4_b[2] = address.S_un.S_un_b.s_b3; + ipv4_b[3] = address.S_un.S_un_b.s_b4; + ipv4_b[4] = 0; + std::string ipv4_b_s = std::string(ipv4_b, 4); + + result.push_back(ipv4_b_s); + } + } + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ + << ": Cannot get IPv4 addresses. Exception : " + << exception.what(); + } catch (const winrt::hresult_error& error) { + NEARBY_LOGS(ERROR) << __func__ + << ": Cannot get IPv4 addresses. WinRT exception: " + << error.code() << ": " + << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption."; } return result; diff --git a/internal/platform/implementation/windows/utils.h b/internal/platform/implementation/windows/utils.h index b3e82e5c..a4b83cf6 100644 --- a/internal/platform/implementation/windows/utils.h +++ b/internal/platform/implementation/windows/utils.h @@ -15,8 +15,7 @@ #ifndef PLATFORM_IMPL_WINDOWS_UTILS_H_ #define PLATFORM_IMPL_WINDOWS_UTILS_H_ -#include -#include +#include #include #include @@ -24,8 +23,8 @@ #include "absl/strings/string_view.h" #include "internal/platform/byte_array.h" -#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" -#include "internal/platform/implementation/windows/generated/winrt/base.h" +#include "winrt/Windows.Foundation.h" +#include "winrt/base.h" namespace nearby { namespace windows { @@ -45,6 +44,7 @@ ByteArray Sha256(absl::string_view input, size_t size); // Reads the IPv4 addresses std::vector GetIpv4Addresses(); +std::vector Get4BytesIpv4Addresses(); namespace Constants { // The Id of the Service Name SDP attribute diff --git a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc index 56e392fa..6fbc373e 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc @@ -227,20 +227,30 @@ fire_and_forget WifiHotspotServerSocket::Listener_ConnectionReceived( } std::vector WifiHotspotServerSocket::GetIpAddresses() const { - std::vector result{}; - auto host_names = NetworkInformation::GetHostNames(); - for (auto host_name : host_names) { - if (host_name.IPInformation() != nullptr && - host_name.IPInformation().NetworkAdapter() != nullptr && - host_name.Type() == HostNameType::Ipv4) { - std::string ipv4_s = winrt::to_string(host_name.ToString()); + std::vector result; + try { + auto host_names = NetworkInformation::GetHostNames(); + for (auto host_name : host_names) { + if (host_name.IPInformation() != nullptr && + host_name.IPInformation().NetworkAdapter() != nullptr && + host_name.Type() == HostNameType::Ipv4) { + std::string ipv4_s = winrt::to_string(host_name.ToString()); - if (absl::EndsWith(ipv4_s, ".1")) { - NEARBY_LOGS(INFO) << "Found Hotspot IP: " << ipv4_s; - result.push_back(ipv4_s); + if (absl::EndsWith(ipv4_s, ".1")) { + NEARBY_LOGS(INFO) << "Found Hotspot IP: " << ipv4_s; + result.push_back(ipv4_s); + } } } + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); + } catch (const winrt::hresult_error &error) { + NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() + << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption."; } + return result; } diff --git a/internal/platform/implementation/windows/wifi_lan.h b/internal/platform/implementation/windows/wifi_lan.h index 0783d9cf..c0e390fb 100644 --- a/internal/platform/implementation/windows/wifi_lan.h +++ b/internal/platform/implementation/windows/wifi_lan.h @@ -201,9 +201,6 @@ class WifiLanServerSocket : public api::WifiLanServerSocket { StreamSocketListener listener, StreamSocketListenerConnectionReceivedEventArgs const& args); - // Retrieves IP addresses from local machine - std::vector GetIpAddresses() const; - mutable absl::Mutex mutex_; absl::CondVar cond_; std::deque pending_sockets_ ABSL_GUARDED_BY(mutex_); diff --git a/internal/platform/implementation/windows/wifi_lan_server_socket.cc b/internal/platform/implementation/windows/wifi_lan_server_socket.cc index 2b75c5dc..d7f19e55 100644 --- a/internal/platform/implementation/windows/wifi_lan_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_lan_server_socket.cc @@ -40,16 +40,16 @@ WifiLanServerSocket::~WifiLanServerSocket() { Close(); } // Returns the first IP address. std::string WifiLanServerSocket::GetIPAddress() const { if (stream_socket_listener_ == nullptr) { - return {}; + NEARBY_LOGS(ERROR) << "Failed to get IP address due to no server socket."; + return ""; } if (ip_addresses_.empty()) { - auto ip_addr = GetIpAddresses(); - if (ip_addr.empty()) { - return {}; - } - return ip_addr.front(); + NEARBY_LOGS(ERROR) + << "Failed to get IP address due to no avaible IP addresses."; + return ""; } + return ip_addresses_.front(); } @@ -139,7 +139,7 @@ Exception WifiLanServerSocket::Close() { bool WifiLanServerSocket::listen() { // Get current IP addresses of the device. - ip_addresses_ = GetIpAddresses(); + ip_addresses_ = Get4BytesIpv4Addresses(); if (ip_addresses_.empty()) { NEARBY_LOGS(WARNING) << "failed to start accepting connection without IP " @@ -219,31 +219,5 @@ fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived( return fire_and_forget{}; } -// Retrieves IP addresses from local machine. -std::vector WifiLanServerSocket::GetIpAddresses() const { - std::vector result{}; - auto host_names = NetworkInformation::GetHostNames(); - for (auto host_name : host_names) { - if (host_name.IPInformation() != nullptr && - host_name.IPInformation().NetworkAdapter() != nullptr && - host_name.Type() == HostNameType::Ipv4) { - std::string ipv4_s = winrt::to_string(host_name.ToString()); - // Converts ip address from x.x.x.x to 4 bytes format. - in_addr address; - address.S_un.S_addr = inet_addr(ipv4_s.c_str()); - char ipv4_b[5]; - ipv4_b[0] = address.S_un.S_un_b.s_b1; - ipv4_b[1] = address.S_un.S_un_b.s_b2; - ipv4_b[2] = address.S_un.S_un_b.s_b3; - ipv4_b[3] = address.S_un.S_un_b.s_b4; - ipv4_b[4] = 0; - std::string ipv4_b_s = std::string(ipv4_b, 4); - - result.push_back(ipv4_b_s); - } - } - return result; -} - } // namespace windows } // namespace nearby