From ca1ab209b76894a2c684d8e6e6ec5c204c87074c Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Thu, 30 Oct 2025 18:56:59 -0700 Subject: [PATCH] Move hotspot credential construction into each platform. PiperOrigin-RevId: 826272274 --- .../implementation/mediums/wifi_hotspot.cc | 6 +-- .../implementation/g3/wifi_hotspot.cc | 14 ++++-- .../platform/implementation/g3/wifi_hotspot.h | 12 ++--- .../platform/implementation/wifi_hotspot.h | 7 ++- .../windows/wifi_hotspot_server_socket.cc | 11 +++-- .../windows/wifi_hotspot_server_socket.h | 5 +- internal/platform/wifi_hotspot.h | 19 +++----- internal/platform/wifi_hotspot_test.cc | 48 ++++++++++--------- 8 files changed, 66 insertions(+), 56 deletions(-) diff --git a/connections/implementation/mediums/wifi_hotspot.cc b/connections/implementation/mediums/wifi_hotspot.cc index 7eb64c86..b6530e80 100644 --- a/connections/implementation/mediums/wifi_hotspot.cc +++ b/connections/implementation/mediums/wifi_hotspot.cc @@ -134,9 +134,7 @@ HotspotCredentials* WifiHotspot::GetCredentials(absl::string_view service_id) { << ". Use default credentials"; return crendential; } - crendential->SetGateway(it->second.GetIPAddress()); - crendential->SetPort(it->second.GetPort()); - + it->second.PopulateHotspotCredentials(*crendential); return crendential; } @@ -165,7 +163,7 @@ bool WifiHotspot::StartAcceptingConnections( } // "port=0" to let the platform to select an available port for the socket - WifiHotspotServerSocket server_socket = medium_.ListenForService(/*port=*/0); + WifiHotspotServerSocket server_socket = medium_.ListenForService(); if (!server_socket.IsValid()) { LOG(INFO) << "Failed to start accepting WifiHotspot connections for service_id=" diff --git a/internal/platform/implementation/g3/wifi_hotspot.cc b/internal/platform/implementation/g3/wifi_hotspot.cc index fb03770d..de71771c 100644 --- a/internal/platform/implementation/g3/wifi_hotspot.cc +++ b/internal/platform/implementation/g3/wifi_hotspot.cc @@ -110,6 +110,13 @@ Exception WifiHotspotServerSocket::DoClose() { return {Exception::kSuccess}; } +void WifiHotspotServerSocket::PopulateHotspotCredentials( + HotspotCredentials& hotspot_credentials) { + absl::MutexLock lock(mutex_); + hotspot_credentials.SetGateway(ip_address_); + hotspot_credentials.SetPort(port_); +} + // Code for WifiHotspotMedium WifiHotspotMedium::WifiHotspotMedium() { auto& env = MediumEnvironment::Instance(); @@ -263,9 +270,10 @@ WifiHotspotMedium::ListenForService(int port) { dot_decimal_ip.pop_back(); server_socket->SetIPAddress(dot_decimal_ip); - server_socket->SetPort(port == 0 ? env.GetFakePort() : port); - std::string socket_name = WifiHotspotServerSocket::GetName( - server_socket->GetIPAddress(), server_socket->GetPort()); + int port_to_use = port == 0 ? env.GetFakePort() : port; + server_socket->SetPort(port_to_use); + std::string socket_name = + WifiHotspotServerSocket::GetName(dot_decimal_ip, port_to_use); server_socket->SetCloseNotifier([this, socket_name]() { absl::MutexLock lock(mutex_); server_sockets_.erase(socket_name); diff --git a/internal/platform/implementation/g3/wifi_hotspot.h b/internal/platform/implementation/g3/wifi_hotspot.h index 1b69d1a6..6fde38ae 100644 --- a/internal/platform/implementation/g3/wifi_hotspot.h +++ b/internal/platform/implementation/g3/wifi_hotspot.h @@ -20,13 +20,13 @@ #include #include +#include "absl/base/thread_annotations.h" #include "absl/synchronization/mutex.h" -#include "internal/platform/byte_array.h" -#include "internal/platform/implementation/g3/multi_thread_executor.h" #include "internal/platform/implementation/g3/socket_base.h" #include "internal/platform/implementation/wifi_hotspot.h" #include "internal/platform/input_stream.h" #include "internal/platform/output_stream.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace g3 { @@ -65,11 +65,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket { static std::string GetName(absl::string_view ip_address, int port); - std::string GetIPAddress() const override ABSL_LOCKS_EXCLUDED(mutex_) { - absl::MutexLock lock(&mutex_); - return ip_address_; - } - void SetIPAddress(const std::string& ip_address) ABSL_LOCKS_EXCLUDED(mutex_) { absl::MutexLock lock(&mutex_); ip_address_ = ip_address; @@ -113,6 +108,9 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket { // Calls close_notifier if it was previously set, and marks socket as closed. Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_); + void PopulateHotspotCredentials(HotspotCredentials& hotspot_credentials) + override ABSL_LOCKS_EXCLUDED(mutex_); + private: // Retrieves IP addresses from local machine std::vector GetIpAddresses() const; diff --git a/internal/platform/implementation/wifi_hotspot.h b/internal/platform/implementation/wifi_hotspot.h index 83ff5255..ea658f40 100644 --- a/internal/platform/implementation/wifi_hotspot.h +++ b/internal/platform/implementation/wifi_hotspot.h @@ -51,8 +51,6 @@ class WifiHotspotServerSocket { public: virtual ~WifiHotspotServerSocket() = default; - virtual std::string GetIPAddress() const = 0; - virtual int GetPort() const = 0; // Blocks until either: @@ -65,6 +63,11 @@ class WifiHotspotServerSocket { // Returns Exception::kIo on error, Exception::kSuccess otherwise. virtual Exception Close() = 0; + + // Populates the hotspot credentials with the server socket's service + // addresses and ports. + virtual void PopulateHotspotCredentials( + HotspotCredentials& hotspot_credentials) = 0; }; // Container of operations that can be performed over the WifiHotspot medium. diff --git a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc index dc2e351c..2c5f7a63 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc @@ -39,6 +39,7 @@ #include "internal/platform/implementation/windows/wifi_hotspot_server_socket.h" #include "internal/platform/implementation/windows/wifi_hotspot_socket.h" #include "internal/platform/logging.h" +#include "internal/platform/wifi_credential.h" namespace nearby::windows { namespace { @@ -51,10 +52,6 @@ WifiHotspotServerSocket::WifiHotspotServerSocket(int port) : port_(port) {} WifiHotspotServerSocket::~WifiHotspotServerSocket() { Close(); } -std::string WifiHotspotServerSocket::GetIPAddress() const { - return hotspot_ipaddr_; -} - int WifiHotspotServerSocket::GetPort() const { return server_socket_.GetPort(); } @@ -91,6 +88,12 @@ Exception WifiHotspotServerSocket::Close() { return {Exception::kSuccess}; } +void WifiHotspotServerSocket::PopulateHotspotCredentials( + HotspotCredentials& hotspot_credentials) { + hotspot_credentials.SetGateway(hotspot_ipaddr_); + hotspot_credentials.SetPort(port_); +} + bool WifiHotspotServerSocket::Listen(bool dual_stack) { // Get current IP addresses of the device. int64_t ip_address_max_retries = NearbyFlags::GetInstance().GetInt64Flag( diff --git a/internal/platform/implementation/windows/wifi_hotspot_server_socket.h b/internal/platform/implementation/windows/wifi_hotspot_server_socket.h index 3483133e..4763c288 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_server_socket.h +++ b/internal/platform/implementation/windows/wifi_hotspot_server_socket.h @@ -36,6 +36,7 @@ #include "internal/platform/exception.h" #include "internal/platform/implementation/wifi_hotspot.h" #include "internal/platform/implementation/windows/nearby_server_socket.h" +#include "internal/platform/wifi_credential.h" namespace nearby::windows { @@ -50,7 +51,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket { WifiHotspotServerSocket& operator=(const WifiHotspotServerSocket&) = default; WifiHotspotServerSocket& operator=(WifiHotspotServerSocket&&) = default; - std::string GetIPAddress() const override; int GetPort() const override; // Blocks until either: @@ -69,6 +69,9 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket { // Returns Exception::kIo on error, Exception::kSuccess otherwise. Exception Close() override; + void PopulateHotspotCredentials( + HotspotCredentials& hotspot_credentials) override; + // Binds to local port bool Listen(bool dual_stack); diff --git a/internal/platform/wifi_hotspot.h b/internal/platform/wifi_hotspot.h index c0b0ed44..981b7598 100644 --- a/internal/platform/wifi_hotspot.h +++ b/internal/platform/wifi_hotspot.h @@ -18,7 +18,6 @@ #include #include #include -#include #include #include "absl/base/thread_annotations.h" @@ -109,16 +108,10 @@ class WifiHotspotServerSocket final { std::unique_ptr socket) : impl_(std::move(socket)) {} - // Returns ip address. - std::string GetIPAddress() const { - CHECK(impl_); - return impl_->GetIPAddress(); - } - - // Returns port. - int GetPort() const { - CHECK(impl_); - return impl_->GetPort(); + // Populates the hotspot credentials with the server socket's service + // addresses and ports. + void PopulateHotspotCredentials(HotspotCredentials& hotspot_credentials) { + impl_->PopulateHotspotCredentials(hotspot_credentials); } // Blocks until either: @@ -168,8 +161,8 @@ class WifiHotspotMedium { // Returns a new WifiHotspotServerSocket. // On Success, WifiHotspotServerSocket::IsValid() returns true. - WifiHotspotServerSocket ListenForService(int port = 0) { - return WifiHotspotServerSocket(impl_->ListenForService(port)); + WifiHotspotServerSocket ListenForService() { + return WifiHotspotServerSocket(impl_->ListenForService(/*port=*/0)); } // Returns the port range as a pair of min and max port. diff --git a/internal/platform/wifi_hotspot_test.cc b/internal/platform/wifi_hotspot_test.cc index 3e9438c8..e7375e78 100644 --- a/internal/platform/wifi_hotspot_test.cc +++ b/internal/platform/wifi_hotspot_test.cc @@ -140,7 +140,9 @@ TEST_P(WifiHotspotMediumTest, CanStartHotspotThatOtherConnect) { WifiHotspotServerSocket server_socket = wifi_hotspot_a->ListenForService(); EXPECT_TRUE(server_socket.IsValid()); - wifi_hotspot_a->GetCredential()->SetGateway(server_socket.GetIPAddress()); + server_socket.PopulateHotspotCredentials(*wifi_hotspot_a->GetCredential()); + std::string hotspot_a_ip = wifi_hotspot_a->GetCredential()->GetGateway(); + int hotspot_a_port = wifi_hotspot_a->GetCredential()->GetPort(); WifiHotspotSocket socket_a; WifiHotspotSocket socket_b; @@ -151,16 +153,16 @@ TEST_P(WifiHotspotMediumTest, CanStartHotspotThatOtherConnect) { CancellationFlag flag; SingleThreadExecutor server_executor; SingleThreadExecutor client_executor; - client_executor.Execute( - [&wifi_hotspot_b, &socket_b, &server_socket, &flag]() { - socket_b = wifi_hotspot_b->ConnectToService(kIp, kPort, &flag); - EXPECT_FALSE(socket_b.IsValid()); - socket_b = wifi_hotspot_b->ConnectToService( - server_socket.GetIPAddress(), server_socket.GetPort(), &flag); - if (!socket_b.IsValid()) { - server_socket.Close(); - } - }); + client_executor.Execute([&wifi_hotspot_b, &socket_b, hotspot_a_ip, + hotspot_a_port, &server_socket, &flag]() { + socket_b = wifi_hotspot_b->ConnectToService(kIp, kPort, &flag); + EXPECT_FALSE(socket_b.IsValid()); + socket_b = + wifi_hotspot_b->ConnectToService(hotspot_a_ip, hotspot_a_port, &flag); + if (!socket_b.IsValid()) { + server_socket.Close(); + } + }); server_executor.Execute([&socket_a, &server_socket]() { socket_a = server_socket.Accept(); if (!socket_a.IsValid()) { @@ -205,7 +207,9 @@ TEST_P(WifiHotspotMediumTest, CanStartHotspotThatOtherCanCancelConnect) { WifiHotspotServerSocket server_socket = wifi_hotspot_a->ListenForService(); EXPECT_TRUE(server_socket.IsValid()); - wifi_hotspot_a->GetCredential()->SetGateway(server_socket.GetIPAddress()); + server_socket.PopulateHotspotCredentials(*wifi_hotspot_a->GetCredential()); + std::string hotspot_a_ip = wifi_hotspot_a->GetCredential()->GetGateway(); + int hotspot_a_port = wifi_hotspot_a->GetCredential()->GetPort(); WifiHotspotSocket socket_a; WifiHotspotSocket socket_b; @@ -216,16 +220,16 @@ TEST_P(WifiHotspotMediumTest, CanStartHotspotThatOtherCanCancelConnect) { CancellationFlag flag(true); SingleThreadExecutor server_executor; SingleThreadExecutor client_executor; - client_executor.Execute( - [&wifi_hotspot_b, &socket_b, &server_socket, &flag]() { - socket_b = wifi_hotspot_b->ConnectToService(kIp, kPort, &flag); - EXPECT_FALSE(socket_b.IsValid()); - socket_b = wifi_hotspot_b->ConnectToService( - server_socket.GetIPAddress(), server_socket.GetPort(), &flag); - if (!socket_b.IsValid()) { - server_socket.Close(); - } - }); + client_executor.Execute([&wifi_hotspot_b, &socket_b, hotspot_a_ip, + hotspot_a_port, &server_socket, &flag]() { + socket_b = wifi_hotspot_b->ConnectToService(kIp, kPort, &flag); + EXPECT_FALSE(socket_b.IsValid()); + socket_b = + wifi_hotspot_b->ConnectToService(hotspot_a_ip, hotspot_a_port, &flag); + if (!socket_b.IsValid()) { + server_socket.Close(); + } + }); server_executor.Execute([&socket_a, &server_socket]() { socket_a = server_socket.Accept(); if (!socket_a.IsValid()) {