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
@@ -18,6 +18,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
@@ -603,15 +604,15 @@ ExceptionOr<WifiLanSocket> WifiLan::CreateOutgoingMultiplexSocketLocked(
return ExceptionOr<WifiLanSocket>(Exception::kFailed);
}
std::pair<std::string, int> WifiLan::GetCredentials(
std::pair<std::vector<std::string>, int> WifiLan::GetUpgradeAddressCandidates(
const std::string& service_id) {
MutexLock lock(&mutex_);
const auto& it = server_sockets_.find(service_id);
if (it == server_sockets_.end()) {
return std::pair<std::string, int>();
return std::pair<std::vector<std::string>, int>();
}
return std::pair<std::string, int>(it->second.GetIPAddress(),
it->second.GetPort());
return {medium_.GetUpgradeAddressCandidates(it->second),
it->second.GetPort()};
}
std::string WifiLan::GenerateServiceType(const std::string& service_id) {
@@ -18,6 +18,7 @@
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
@@ -109,12 +110,14 @@ class WifiLan {
CancellationFlag* cancellation_flag)
ABSL_LOCKS_EXCLUDED(mutex_);
// Gets ip address + port for remote services on the network to identify and
// connect to this service.
//
// Credential is for the currently-hosted Wifi ServerSocket (if any).
std::pair<std::string, int> GetCredentials(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns the list of ip address candidates that can be used to connect to
// this device for bandwidth upgrade + port number the service is listening
// on.
// The candidates list is ordered to have IPv6 addresses first, then IPv4.
// Both IPv4 and IPv6 adddresses are represented as network order byte
// sequence.
std::pair<std::vector<std::string>, int> GetUpgradeAddressCandidates(
const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
private:
struct AdvertisingInfo {
@@ -73,12 +73,14 @@ TEST_P(WifiLanTest, AdvertiseSameServiceNameReusesPort) {
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceName(std::string(kServiceInfoName));
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [address, port] = wifi_lan_server.GetCredentials(service_id);
auto [addresses, port] =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
wifi_lan_server.StopAdvertising(service_id);
wifi_lan_server.StopAcceptingConnections(service_id);
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [address2, port2] = wifi_lan_server.GetCredentials(service_id);
auto [addresses2, port2] =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
EXPECT_EQ(port, port2);
env_.Stop();
}
@@ -95,13 +97,15 @@ TEST_P(WifiLanTest, AdvertiseDifferentServiceNameUsesDifferentPort) {
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceName(std::string(kServiceInfoName));
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [address, port] = wifi_lan_server.GetCredentials(service_id);
auto [addresses, port] =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
wifi_lan_server.StopAdvertising(service_id);
wifi_lan_server.StopAcceptingConnections(service_id);
nsd_service_info.SetServiceName("ServiceInfoName2");
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [address2, port2] = wifi_lan_server.GetCredentials(service_id);
auto [addresses2, port2] =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
EXPECT_NE(port, port2);
env_.Stop();
}
@@ -313,13 +317,15 @@ TEST_P(WifiLanTest, CanConnectWithIpAddressAndPort) {
accept_latch.CountDown();
}));
auto server_credentials = wifi_lan_server.GetCredentials(service_id);
ASSERT_FALSE(server_credentials.first.empty());
ASSERT_NE(server_credentials.second, 0);
auto server_candidates =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
ASSERT_FALSE(server_candidates.first.empty());
ASSERT_NE(server_candidates.second, 0);
CancellationFlag flag;
ErrorOr<WifiLanSocket> socket_for_client_result = wifi_lan_client.Connect(
service_id, server_credentials.first, server_credentials.second, &flag);
ErrorOr<WifiLanSocket> socket_for_client_result =
wifi_lan_client.Connect(service_id, server_candidates.first.front(),
server_candidates.second, &flag);
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));