Provide IP address candidate list for LAN upgrade.

PiperOrigin-RevId: 825584250
This commit is contained in:
Francis Tsui
2025-10-29 09:37:19 -07:00
committed by Copybara-Service
parent 389a1bc151
commit 8622fe7bf5
20 changed files with 163 additions and 50 deletions
@@ -124,6 +124,7 @@ class WifiLanMedium : public api::WifiLanMedium {
std::unique_ptr<api::WifiLanSocket> ConnectToService(
const std::string& ip_address, int port, CancellationFlag* cancellation_flag) override;
std::unique_ptr<api::WifiLanServerSocket> ListenForService(int port) override;
std::vector<std::string> GetUpgradeAddressCandidates(const api::WifiLanServerSocket& server_socket) override;
private:
GNCNWFramework* medium_;
@@ -183,5 +183,10 @@ std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(int po
return nil;
}
std::vector<std::string> WifiLanMedium::GetUpgradeAddressCandidates(
const api::WifiLanServerSocket& server_socket) {
return { server_socket.GetIPAddress() };
}
} // namespace apple
} // namespace nearby
@@ -17,6 +17,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/log/check.h"
#include "absl/synchronization/mutex.h"
@@ -281,5 +282,10 @@ std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(
return server_socket;
}
std::vector<std::string> WifiLanMedium::GetUpgradeAddressCandidates(
const api::WifiLanServerSocket& server_socket) {
return { server_socket.GetIPAddress() };
}
} // namespace g3
} // namespace nearby
@@ -18,6 +18,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
@@ -206,6 +207,9 @@ class WifiLanMedium : public api::WifiLanMedium {
return std::nullopt;
}
std::vector<std::string> GetUpgradeAddressCandidates(
const api::WifiLanServerSocket& server_socket) override;
private:
struct AdvertisingInfo {
bool Empty() const { return service_types.empty(); }
@@ -16,6 +16,7 @@
#define PLATFORM_API_WIFI_LAN_H_
#include <string>
#include <vector>
#include "absl/functional/any_invocable.h"
#include "internal/platform/cancellation_flag.h"
@@ -151,6 +152,15 @@ class WifiLanMedium {
// Returns the port range as a pair of min and max port.
virtual absl::optional<std::pair<std::int32_t, std::int32_t>>
GetDynamicPortRange() = 0;
// Returns the list of ip address candidates that can be used to connect to
// this device for bandwidth upgrade.
// `server_socket` is the socket that is currently listening for service
// requests.
// Returned adddress list is sorted so IPv6 addresses are first. Both IPv4
// and IPv6 addresses are represented as network order byte sequence.
virtual std::vector<std::string> GetUpgradeAddressCandidates(
const WifiLanServerSocket& server_socket) = 0;
};
} // namespace api
@@ -250,6 +250,9 @@ class WifiLanMedium : public api::WifiLanMedium {
return absl::nullopt;
}
std::vector<std::string> GetUpgradeAddressCandidates(
const api::WifiLanServerSocket& server_socket) override;
private:
// Nsd status
static const int kMediumStatusIdle = 0;
@@ -45,11 +45,13 @@
#include "internal/platform/exception.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.Enumeration.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/implementation/windows/string_utils.h"
#include "internal/platform/implementation/windows/network_info.h"
#include "internal/platform/implementation/windows/socket_address.h"
#include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
#include "internal/platform/nsd_service_info.h"
@@ -780,4 +782,40 @@ bool WifiLanMedium::IsConnectableIpAddress(NsdServiceInfo& nsd_service_info,
return false;
}
std::vector<std::string> WifiLanMedium::GetUpgradeAddressCandidates(
const api::WifiLanServerSocket& server_socket) {
const NetworkInfo& network_info = NetworkInfo::GetNetworkInfo();
std::vector<std::string> ip_addresses;
std::vector<std::string> ipv4_addresses;
for (const auto& net_interface : network_info.GetInterfaces()) {
// Only use wifi and ethernet interfaces for upgrade.
if (net_interface.type != InterfaceType::kWifi &&
net_interface.type != InterfaceType::kEthernet) {
continue;
}
for (const auto& ipv6_address : net_interface.ipv6_addresses) {
SocketAddress address(ipv6_address);
// Link local addresses cannot be used for upgrade since we can't tell
// which interface on the remote device the address is valid.
if (address.IsV6LinkLocal()) {
continue;
}
ip_addresses.push_back(
std::string(reinterpret_cast<const char*>(
&address.ipv6_address()->sin6_addr.u.Byte[0]),
16));
}
for (const auto& ipv4address : net_interface.ipv4_addresses) {
auto address = reinterpret_cast<const sockaddr_in*>(&ipv4address);
ipv4_addresses.push_back(std::string(
reinterpret_cast<const char*>(&address->sin_addr.S_un.S_un_b.s_b1),
4));
}
}
// Append v4 addresses to the end of the list.
ip_addresses.insert(ip_addresses.end(), ipv4_addresses.begin(),
ipv4_addresses.end());
return ip_addresses;
}
} // namespace nearby::windows
@@ -45,10 +45,7 @@ WifiLanServerSocket::~WifiLanServerSocket() { Close(); }
// Returns the first IP address.
std::string WifiLanServerSocket::GetIPAddress() const {
// The result of this function is used in BWU to let the remote side know
// which IP to connect to.
// server_socket_ is not bound to any addresses. So we need to pick an
// IP address from the list of available addresses.
// Just pick an IP address from the list of available addresses.
std::vector<std::string> ip_addresses = GetIpv4Addresses();
if (ip_addresses.empty()) {
LOG(ERROR) << "No IP addresses found.";
+6
View File
@@ -17,6 +17,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "internal/platform/cancellation_flag.h"
@@ -201,4 +202,9 @@ WifiLanSocket WifiLanMedium::ConnectToService(
impl_->ConnectToService(ip_address, port, cancellation_flag));
}
std::vector<std::string> WifiLanMedium::GetUpgradeAddressCandidates(
const WifiLanServerSocket& server_socket) {
return impl_->GetUpgradeAddressCandidates(server_socket.GetImpl());
}
} // namespace nearby
+11
View File
@@ -19,6 +19,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
@@ -178,6 +179,7 @@ class WifiLanServerSocket final {
bool IsValid() const { return impl_ != nullptr; }
api::WifiLanServerSocket& GetImpl() { return *impl_; }
const api::WifiLanServerSocket& GetImpl() const { return *impl_; }
private:
std::shared_ptr<api::WifiLanServerSocket> impl_;
@@ -267,6 +269,15 @@ class WifiLanMedium {
api::WifiLanMedium& GetImpl() { return *impl_; }
// Returns the list of ip address candidates that can be used to connect to
// this device for bandwidth upgrade.
// `server_socket` is the socket that is currently listening for service
// requests.
// Returned adddress list is sorted so IPv6 addresses are first. Both IPv4
// and IPv6 addresses are represented as network order byte sequence.
std::vector<std::string> GetUpgradeAddressCandidates(
const WifiLanServerSocket& server_socket);
private:
Mutex mutex_;
std::unique_ptr<api::WifiLanMedium> impl_;