Move hotspot credential construction into each platform.

PiperOrigin-RevId: 826272274
This commit is contained in:
Francis Tsui
2025-10-30 18:58:19 -07:00
committed by Copybara-Service
parent 03474168b5
commit ca1ab209b7
8 changed files with 66 additions and 56 deletions
@@ -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="
@@ -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);
@@ -20,13 +20,13 @@
#include <string>
#include <utility>
#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<std::string> GetIpAddresses() const;
@@ -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.
@@ -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(
@@ -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);
+6 -13
View File
@@ -18,7 +18,6 @@
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/base/thread_annotations.h"
@@ -109,16 +108,10 @@ class WifiHotspotServerSocket final {
std::unique_ptr<api::WifiHotspotServerSocket> 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.
+26 -22
View File
@@ -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()) {