Fix BWU crash problem because WIFI_LAN create socket based on IPV6.

PiperOrigin-RevId: 432222945
This commit is contained in:
hai007
2022-03-03 10:16:22 -08:00
committed by Copybara-Service
parent 91dfdefb9f
commit eadc972af3
4 changed files with 32 additions and 14 deletions
@@ -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);
}
@@ -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<std::string> GetIpAddresses();
std::vector<std::string> GetIpAddresses() const;
mutable absl::Mutex mutex_;
absl::CondVar cond_;
@@ -292,7 +292,7 @@ std::unique_ptr<api::WifiLanSocket> 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<api::WifiLanSocket> WifiLanMedium::ConnectToService(
std::unique_ptr<WifiLanSocket> wifi_lan_socket =
std::make_unique<WifiLanSocket>(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;
}
@@ -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<std::string> WifiLanServerSocket::GetIpAddresses() {
std::vector<std::string> WifiLanServerSocket::GetIpAddresses() const {
std::vector<std::string> 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;