mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Fix BWU crash problem because WIFI_LAN create socket based on IPV6.
PiperOrigin-RevId: 432222945
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user