From eadc972af36774bd9fc0ea5d988d1759c7854b79 Mon Sep 17 00:00:00 2001 From: hai007 Date: Thu, 3 Mar 2022 10:15:19 -0800 Subject: [PATCH] Fix BWU crash problem because WIFI_LAN create socket based on IPV6. PiperOrigin-RevId: 432222945 --- .../implementation/wifi_lan_bwu_handler.cc | 5 +++ .../implementation/windows/wifi_lan.h | 3 +- .../implementation/windows/wifi_lan_medium.cc | 6 ++-- .../windows/wifi_lan_server_socket.cc | 32 +++++++++++++------ 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/connections/implementation/wifi_lan_bwu_handler.cc b/connections/implementation/wifi_lan_bwu_handler.cc index fb2f9f27..0135acef 100644 --- a/connections/implementation/wifi_lan_bwu_handler.cc +++ b/connections/implementation/wifi_lan_bwu_handler.cc @@ -74,6 +74,7 @@ ByteArray WifiLanBwuHandler::InitializeUpgradedMediumForEndpoint( auto credential = wifi_lan_medium_.GetCredentials(upgrade_service_id); auto ip_address = credential.first; auto port = credential.second; + if (ip_address.empty()) { NEARBY_LOGS(INFO) << "WifiLanBwuHandler couldn't initiate the wifi_lan upgrade for " @@ -82,6 +83,10 @@ ByteArray WifiLanBwuHandler::InitializeUpgradedMediumForEndpoint( << " because the wifi_lan ip address were unable to be obtained."; return {}; } + NEARBY_LOGS(INFO) + << "WifiLanBwuHandler retrieved WIFI_LAN credential. IP addr: " + << ip_address[0] << "." << ip_address[1] << "." << ip_address[2] << "." + << ip_address[3] << ", Port: " << port; return parser::ForBwuWifiLanPathAvailable(ip_address, port); } diff --git a/internal/platform/implementation/windows/wifi_lan.h b/internal/platform/implementation/windows/wifi_lan.h index 6daae344..df546027 100644 --- a/internal/platform/implementation/windows/wifi_lan.h +++ b/internal/platform/implementation/windows/wifi_lan.h @@ -60,6 +60,7 @@ using winrt::Windows::Devices::Enumeration::DeviceWatcher; using winrt::Windows::Foundation::IInspectable; using winrt::Windows::Foundation::Collections::IMapView; using winrt::Windows::Networking::HostName; +using winrt::Windows::Networking::HostNameType; using winrt::Windows::Networking::Connectivity::NetworkInformation; using winrt::Windows::Networking::ServiceDiscovery::Dnssd:: DnssdRegistrationResult; @@ -193,7 +194,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket { StreamSocketListenerConnectionReceivedEventArgs const& args); // Retrieves IP addresses from local machine - std::vector GetIpAddresses(); + std::vector GetIpAddresses() const; mutable absl::Mutex mutex_; absl::CondVar cond_; diff --git a/internal/platform/implementation/windows/wifi_lan_medium.cc b/internal/platform/implementation/windows/wifi_lan_medium.cc index 163b96d1..371a9b06 100644 --- a/internal/platform/implementation/windows/wifi_lan_medium.cc +++ b/internal/platform/implementation/windows/wifi_lan_medium.cc @@ -292,7 +292,7 @@ std::unique_ptr WifiLanMedium::ConnectToService( if (cancellation_flag != nullptr) { if (cancellation_flag->Cancelled()) { NEARBY_LOGS(INFO) << "connect has been cancelled to service " - << ip_address << ":" << port; + << ipv4_address << ":" << port; return nullptr; } @@ -308,11 +308,11 @@ std::unique_ptr WifiLanMedium::ConnectToService( std::unique_ptr wifi_lan_socket = std::make_unique(socket); - NEARBY_LOGS(INFO) << "connected to remote service " << ip_address << ":" + NEARBY_LOGS(INFO) << "connected to remote service " << ipv4_address << ":" << port; return wifi_lan_socket; } catch (...) { - NEARBY_LOGS(ERROR) << "failed to connect remote service " << ip_address + NEARBY_LOGS(ERROR) << "failed to connect remote service " << ipv4_address << ":" << port; } diff --git a/internal/platform/implementation/windows/wifi_lan_server_socket.cc b/internal/platform/implementation/windows/wifi_lan_server_socket.cc index 85112990..82ca87ab 100644 --- a/internal/platform/implementation/windows/wifi_lan_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_lan_server_socket.cc @@ -30,15 +30,14 @@ std::string WifiLanServerSocket::GetIPAddress() const { return {}; } - auto host_names = NetworkInformation::GetHostNames(); - for (auto host_name : host_names) { - if (host_name.IPInformation() != nullptr && - host_name.IPInformation().NetworkAdapter() != nullptr) { - return wstring_to_string(host_name.ToString().c_str()); + if (ip_addresses_.empty()) { + auto ip_addr = GetIpAddresses(); + if (ip_addr.empty()) { + return {}; } + return ip_addr.front(); } - - return {}; + return ip_addresses_.front(); } // Returns port. @@ -163,13 +162,26 @@ fire_and_forget WifiLanServerSocket::Listener_ConnectionReceived( } // Retrieves IP addresses from local machine -std::vector WifiLanServerSocket::GetIpAddresses() { +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) { - result.push_back(wstring_to_string(host_name.ToString().c_str())); + 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;