From 4652aef48cbd0ca1e2e4f032a1606e1a149ac5ed Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Tue, 9 Dec 2025 08:46:50 -0800 Subject: [PATCH] WifiLan::Change GetUpgradeAddressCandidates to use ServiceAddress. PiperOrigin-RevId: 842257461 --- .../implementation/base_pcp_handler.cc | 18 ++++++------ .../implementation/bwu_manager_test.cc | 7 +++-- connections/implementation/fake_bwu_handler.h | 5 ++-- .../implementation/mediums/wifi_lan.cc | 8 +++--- connections/implementation/mediums/wifi_lan.h | 3 +- .../implementation/mediums/wifi_lan_test.cc | 27 ++++++++++-------- connections/implementation/offline_frames.cc | 22 +++++++++------ connections/implementation/offline_frames.h | 3 +- .../implementation/offline_frames_test.cc | 13 +++++---- .../offline_frames_validator_test.cc | 13 +++++---- .../implementation/wifi_lan_bwu_handler.cc | 8 ++---- .../wifi_lan_bwu_handler_test.cc | 11 +++++--- .../platform/implementation/apple/wifi_lan.h | 3 +- .../platform/implementation/apple/wifi_lan.mm | 6 ++-- .../platform/implementation/g3/wifi_lan.cc | 9 ++++-- .../platform/implementation/g3/wifi_lan.h | 3 +- internal/platform/implementation/wifi_lan.h | 3 +- .../implementation/windows/wifi_lan.h | 4 ++- .../implementation/windows/wifi_lan_medium.cc | 28 ++++++++++++------- internal/platform/mock_wifi_lan_medium.h | 3 +- internal/platform/wifi_lan.cc | 3 +- internal/platform/wifi_lan.h | 3 +- 22 files changed, 121 insertions(+), 82 deletions(-) diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index 00a1e2b9..52ce9a1d 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -81,6 +81,7 @@ #include "internal/platform/prng.h" #include "internal/platform/runnable.h" #include "internal/platform/wifi.h" +#include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_lan_connection_info.h" #include "proto/connections_enums.pb.h" @@ -196,22 +197,21 @@ std::vector BasePcpHandler::GetConnectionInfoFromResult( BleConnectionInfo info("", "", "", {}); connection_infos.push_back(info); } else if (medium == location::nearby::proto::connections::WIFI_LAN) { - std::pair, int> upgrade_candidates = + std::vector upgrade_candidates = mediums_->GetWifiLan().GetUpgradeAddressCandidates( std::string(service_id)); - const std::vector& ip_addresses = upgrade_candidates.first; - std::string ip_address; // Only use IPv4 address. IPv4 addresses are always at the end of the // list. - if (!ip_addresses.empty()) { - ip_address = ip_addresses.back(); - if (ip_address.size() != 4) { - ip_address.clear(); + std::vector ip_address; + int port = 0; + if (!upgrade_candidates.empty()) { + ip_address = upgrade_candidates.back().address; + if (ip_address.size() == 4) { + port = upgrade_candidates.back().port; } } - int port = upgrade_candidates.second; WifiLanConnectionInfo info( - ip_address, + std::string(ip_address.begin(), ip_address.end()), absl::StrCat(absl::Hex(port, absl::kZeroPad16)), "", {}); connection_infos.push_back(info); diff --git a/connections/implementation/bwu_manager_test.cc b/connections/implementation/bwu_manager_test.cc index 0906d3ec..72dda764 100644 --- a/connections/implementation/bwu_manager_test.cc +++ b/connections/implementation/bwu_manager_test.cc @@ -40,6 +40,7 @@ #include "internal/platform/count_down_latch.h" #include "internal/platform/exception.h" #include "internal/platform/feature_flags.h" +#include "internal/platform/wifi_credential.h" #include "internal/proto/analytics/connections_log.pb.h" #include "proto/connections_enums.pb.h" @@ -984,9 +985,9 @@ TEST_F(BwuManagerTest, InitiateBwu_Revert_OnDisconnect_Wlan) { CreateInitialEndpoint(&client_, kServiceIdA, kEndpointId1, Medium::BLUETOOTH); - ExceptionOr wlan_path_available_frame = parser::FromBytes( - parser::ForBwuWifiLanPathAvailable(/*ip_addresses=*/{"ABCD"}, - /*port=*/1234)); + ExceptionOr wlan_path_available_frame = + parser::FromBytes(parser::ForBwuWifiLanPathAvailable( + {ServiceAddress{.address = {'A', 'B', 'C', 'D'}, .port = 1234}})); OfflineFrame frame = wlan_path_available_frame.result(); frame.set_version(OfflineFrame::V1); auto* v1_frame = frame.mutable_v1(); diff --git a/connections/implementation/fake_bwu_handler.h b/connections/implementation/fake_bwu_handler.h index 5c7b08d8..1ba9dbab 100644 --- a/connections/implementation/fake_bwu_handler.h +++ b/connections/implementation/fake_bwu_handler.h @@ -34,6 +34,7 @@ #include "internal/platform/expected.h" #include "internal/platform/logging.h" #include "internal/platform/mac_address.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace connections { @@ -147,8 +148,8 @@ class FakeBwuHandler : public BaseBwuHandler { /*mac_address=*/mac_address); } case location::nearby::proto::connections::WIFI_LAN: - return parser::ForBwuWifiLanPathAvailable(/*ip_addresses=*/{"ABCD"}, - /*port=*/1234); + return parser::ForBwuWifiLanPathAvailable( + {ServiceAddress{.address = {'A', 'B', 'C', 'D'}, .port = 1234}}); case location::nearby::proto::connections::WEB_RTC: case location::nearby::proto::connections::WEB_RTC_NON_CELLULAR: return parser::ForBwuWebrtcPathAvailable( diff --git a/connections/implementation/mediums/wifi_lan.cc b/connections/implementation/mediums/wifi_lan.cc index 0353f44f..a9665905 100644 --- a/connections/implementation/mediums/wifi_lan.cc +++ b/connections/implementation/mediums/wifi_lan.cc @@ -36,6 +36,7 @@ #include "internal/platform/nsd_service_info.h" #include "internal/platform/socket.h" #include "internal/platform/types.h" +#include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_lan.h" namespace nearby { @@ -607,15 +608,14 @@ ExceptionOr WifiLan::CreateOutgoingMultiplexSocketLocked( return ExceptionOr(Exception::kFailed); } -std::pair, int> WifiLan::GetUpgradeAddressCandidates( +std::vector 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, int>(); + return {}; } - return {medium_.GetUpgradeAddressCandidates(it->second), - it->second.GetPort()}; + return medium_.GetUpgradeAddressCandidates(it->second); } std::string WifiLan::GenerateServiceType(const std::string& service_id) { diff --git a/connections/implementation/mediums/wifi_lan.h b/connections/implementation/mediums/wifi_lan.h index ecae5243..3205c577 100644 --- a/connections/implementation/mediums/wifi_lan.h +++ b/connections/implementation/mediums/wifi_lan.h @@ -33,6 +33,7 @@ #include "internal/platform/multi_thread_executor.h" #include "internal/platform/mutex.h" #include "internal/platform/nsd_service_info.h" +#include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_lan.h" namespace nearby { @@ -116,7 +117,7 @@ class WifiLan { // 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, int> GetUpgradeAddressCandidates( + std::vector GetUpgradeAddressCandidates( const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_); private: diff --git a/connections/implementation/mediums/wifi_lan_test.cc b/connections/implementation/mediums/wifi_lan_test.cc index 09736bc9..bdc9a45b 100644 --- a/connections/implementation/mediums/wifi_lan_test.cc +++ b/connections/implementation/mediums/wifi_lan_test.cc @@ -16,6 +16,7 @@ #include #include +#include #include "gtest/gtest.h" #include "absl/strings/string_view.h" @@ -31,6 +32,7 @@ #include "internal/platform/medium_environment.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/single_thread_executor.h" +#include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_lan.h" namespace nearby { @@ -73,15 +75,15 @@ 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 [addresses, port] = + std::vector addresses = 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 [addresses2, port2] = + std::vector addresses2 = wifi_lan_server.GetUpgradeAddressCandidates(service_id); - EXPECT_EQ(port, port2); + EXPECT_EQ(addresses.back().port, addresses2.back().port); env_.Stop(); } @@ -97,16 +99,16 @@ 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 [addresses, port] = + std::vector addresses = 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 [addresses2, port2] = + std::vector addresses2 = wifi_lan_server.GetUpgradeAddressCandidates(service_id); - EXPECT_NE(port, port2); + EXPECT_NE(addresses.back().port, addresses2.back().port); env_.Stop(); } @@ -317,15 +319,16 @@ TEST_P(WifiLanTest, CanConnectWithIpAddressAndPort) { accept_latch.CountDown(); })); - auto server_candidates = + std::vector server_candidates = wifi_lan_server.GetUpgradeAddressCandidates(service_id); - ASSERT_FALSE(server_candidates.first.empty()); - ASSERT_NE(server_candidates.second, 0); + ASSERT_FALSE(server_candidates.empty()); + ASSERT_NE(server_candidates.back().port, 0); CancellationFlag flag; - ErrorOr socket_for_client_result = - wifi_lan_client.Connect(service_id, server_candidates.first.front(), - server_candidates.second, &flag); + std::string ip_address{server_candidates.front().address.begin(), + server_candidates.front().address.end()}; + ErrorOr socket_for_client_result = wifi_lan_client.Connect( + service_id, ip_address, server_candidates.front().port, &flag); EXPECT_TRUE(accept_latch.Await(kWaitDuration).result()); EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id)); EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id)); diff --git a/connections/implementation/offline_frames.cc b/connections/implementation/offline_frames.cc index 31611d69..ab72458a 100644 --- a/connections/implementation/offline_frames.cc +++ b/connections/implementation/offline_frames.cc @@ -32,6 +32,7 @@ #include "internal/platform/implementation/wifi_utils.h" #include "internal/platform/logging.h" #include "internal/platform/mac_address.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace connections { @@ -289,7 +290,7 @@ ByteArray ForBwuWifiHotspotPathAvailable( } ByteArray ForBwuWifiLanPathAvailable( - const std::vector& ip_addresses, std::int32_t port) { + const std::vector& addresses) { OfflineFrame frame; frame.set_version(OfflineFrame::V1); @@ -304,18 +305,21 @@ ByteArray ForBwuWifiLanPathAvailable( auto* wifi_lan_socket = upgrade_path_info->mutable_wifi_lan_socket(); // For compatibility with Android versions, only use IPv4 address. // IPv4 addresses are always at the end of the list. - std::string ip_address = ip_addresses.back(); - if (ip_address.size() == 4) { - wifi_lan_socket->set_ip_address(ip_address); - wifi_lan_socket->set_wifi_port(port); + const auto& last_address = addresses.back(); + if (last_address.address.size() == 4) { + wifi_lan_socket->set_ip_address( + std::string(last_address.address.begin(), last_address.address.end())); + wifi_lan_socket->set_wifi_port(last_address.port); } - for (const auto& address : ip_addresses) { + for (const auto& address : addresses) { auto* address_candidate = wifi_lan_socket->add_address_candidates(); - std::string ip_address = std::string(address.begin(), address.end()); + std::string ip_address = + std::string(address.address.begin(), address.address.end()); address_candidate->set_ip_address(ip_address); - address_candidate->set_port(port); + address_candidate->set_port(address.port); VLOG(1) << "ForBwuWifiLanPathAvailable: " - << WifiUtils::GetHumanReadableIpAddress(ip_address) << ":" << port; + << WifiUtils::GetHumanReadableIpAddress(ip_address) << ":" + << address.port; } return ToBytes(std::move(frame)); } diff --git a/connections/implementation/offline_frames.h b/connections/implementation/offline_frames.h index 0114d83f..16accf38 100644 --- a/connections/implementation/offline_frames.h +++ b/connections/implementation/offline_frames.h @@ -25,6 +25,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/exception.h" #include "internal/platform/mac_address.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace connections { @@ -83,7 +84,7 @@ ByteArray ForBwuWifiHotspotPathAvailable( UpgradePathInfo::WifiHotspotCredentials credentials, bool supports_disabling_encryption); ByteArray ForBwuWifiLanPathAvailable( - const std::vector& ip_addresses, std::int32_t port); + const std::vector& addresses); ByteArray ForBwuAwdlPathAvailable(const std::string& service_name, const std::string& service_type, const std::string& password, diff --git a/connections/implementation/offline_frames_test.cc b/connections/implementation/offline_frames_test.cc index 4f039d41..63559d00 100644 --- a/connections/implementation/offline_frames_test.cc +++ b/connections/implementation/offline_frames_test.cc @@ -30,6 +30,7 @@ #include "internal/flags/nearby_flags.h" #include "internal/platform/byte_array.h" #include "internal/platform/mac_address.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace connections { @@ -532,11 +533,13 @@ TEST(OfflineFramesTest, CanGenerateBwuWifiLanPathAvailable) { > >)pb"; ByteArray bytes = ForBwuWifiLanPathAvailable( - {std::string( - "\x2a\x00\x79\xe0\x2e\x87\x00\x06\xb7\x28\x67\x45\x7a\xdd\x01\x53", - 16), - "\x01\x02\x03\x04"}, - 1234); + {ServiceAddress{ + .address = {'\x2a', '\x00', '\x79', '\xe0', '\x2e', '\x87', '\x00', + '\x06', '\xb7', '\x28', '\x67', '\x45', '\x7a', '\xdd', + '\x01', '\x53'}, + .port = 1234}, + ServiceAddress{.address = {'\x01', '\x02', '\x03', '\x04'}, + .port = 1234}}); auto response = FromBytes(bytes); ASSERT_TRUE(response.ok()); OfflineFrame message = response.result(); diff --git a/connections/implementation/offline_frames_validator_test.cc b/connections/implementation/offline_frames_validator_test.cc index f0a63ff6..e9c6ee9f 100644 --- a/connections/implementation/offline_frames_validator_test.cc +++ b/connections/implementation/offline_frames_validator_test.cc @@ -27,6 +27,7 @@ #include "connections/medium_selector.h" #include "internal/platform/byte_array.h" #include "internal/platform/exception.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace connections { @@ -686,13 +687,13 @@ TEST(OfflineFramesValidatorTest, TEST(OfflineFramesValidatorTest, ValidateWifiLanUpgradeFrameWithAddressCandidatesSucceeds) { OfflineFrame offline_frame; - std::vector address_candidates = { - std::string( - "\x2a\x00\x79\xe0\x2e\x87\x00\x06\xb7\x28\x67\x45\x7a\xdd\x01\x53", - 16), - std::string("\xc0\xa8\x00\x01", 4), + std::vector address_candidates = { + {{'\x2a', '\x00', '\x79', '\xe0', '\x2e', '\x87', '\x00', '\x06', '\xb7', + '\x28', '\x67', '\x45', '\x7a', '\xdd', '\x01', '\x53'}, + kPort}, + {{'\xc0', '\xa8', '\x00', '\x01'}, kPort}, }; - ByteArray bytes = ForBwuWifiLanPathAvailable(address_candidates, kPort); + ByteArray bytes = ForBwuWifiLanPathAvailable(address_candidates); offline_frame.ParseFromString(std::string(bytes)); auto ret_value = EnsureValidOfflineFrame(offline_frame); diff --git a/connections/implementation/wifi_lan_bwu_handler.cc b/connections/implementation/wifi_lan_bwu_handler.cc index 0d99ba8c..855bd5dc 100644 --- a/connections/implementation/wifi_lan_bwu_handler.cc +++ b/connections/implementation/wifi_lan_bwu_handler.cc @@ -145,17 +145,15 @@ ByteArray WifiLanBwuHandler::HandleInitializeUpgradedMediumForEndpoint( // Address candidates are not populated until StartAcceptingConnections() is // called and the server socket is created. Be careful moving this codeblock // around. - std::pair, int> upgrade_candidates = + std::vector upgrade_candidates = wifi_lan_medium_.GetUpgradeAddressCandidates(upgrade_service_id); - const std::vector& ip_addresses = upgrade_candidates.first; - int port = upgrade_candidates.second; - if (ip_addresses.empty()) { + if (upgrade_candidates.empty()) { LOG(INFO) << "WifiLanBwuHandler couldn't initiate the wifi_lan upgrade for " << "service " << upgrade_service_id << " and endpoint " << endpoint_id << " because there are no available ip addresses."; return {}; } - return parser::ForBwuWifiLanPathAvailable(ip_addresses, port); + return parser::ForBwuWifiLanPathAvailable(upgrade_candidates); } void WifiLanBwuHandler::HandleRevertInitiatorStateForService( diff --git a/connections/implementation/wifi_lan_bwu_handler_test.cc b/connections/implementation/wifi_lan_bwu_handler_test.cc index 07dc665e..aa3fe067 100644 --- a/connections/implementation/wifi_lan_bwu_handler_test.cc +++ b/connections/implementation/wifi_lan_bwu_handler_test.cc @@ -34,6 +34,7 @@ #include "internal/platform/mock_wifi_lan_medium.h" #include "internal/platform/mock_wifi_lan_server_socket.h" #include "internal/platform/mock_wifi_lan_socket.h" +#include "internal/platform/wifi_credential.h" namespace nearby { @@ -195,8 +196,10 @@ TEST_F(WifiLanBwuHandlerTest, InitializeUpgradedMediumForEndpoint_Success) { EXPECT_CALL(*wifi_lan_medium, ListenForService(_)) .WillOnce(Return(ByMove(std::move(wifi_lan_server_socket)))); EXPECT_CALL(*wifi_lan_medium, GetUpgradeAddressCandidates(_)) - .WillOnce(Return(std::vector{std::string(kIpv6Address), - std::string(kIpv4Address)})); + .WillOnce(Return(std::vector{ + {std::vector{kIpv6Address.begin(), kIpv6Address.end()}, 8080}, + {std::vector{kIpv4Address.begin(), kIpv4Address.end()}, + 8888}})); OfflineFrame expected_frame; expected_frame.set_version(OfflineFrame::V1); expected_frame.mutable_v1()->set_type(V1Frame::BANDWIDTH_UPGRADE_NEGOTIATION); @@ -212,13 +215,13 @@ TEST_F(WifiLanBwuHandlerTest, InitializeUpgradedMediumForEndpoint_Success) { ->mutable_upgrade_path_info() ->mutable_wifi_lan_socket(); wifi_lan_socket->set_ip_address(kIpv4Address); - wifi_lan_socket->set_wifi_port(8080); + wifi_lan_socket->set_wifi_port(8888); auto* address_candidate = wifi_lan_socket->add_address_candidates(); address_candidate->set_ip_address(kIpv6Address); address_candidate->set_port(8080); address_candidate = wifi_lan_socket->add_address_candidates(); address_candidate->set_ip_address(kIpv4Address); - address_candidate->set_port(8080); + address_candidate->set_port(8888); upgrade_path_info->set_supports_client_introduction_ack(true); ByteArray result = handler_.InitializeUpgradedMediumForEndpoint( diff --git a/internal/platform/implementation/apple/wifi_lan.h b/internal/platform/implementation/apple/wifi_lan.h index cf0628b2..04a74b88 100644 --- a/internal/platform/implementation/apple/wifi_lan.h +++ b/internal/platform/implementation/apple/wifi_lan.h @@ -23,6 +23,7 @@ #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/nsd_service_info.h" +#include "internal/platform/wifi_credential.h" @class GNCNWFramework; @class GNCNWFrameworkServerSocket; @@ -124,7 +125,7 @@ class WifiLanMedium : public api::WifiLanMedium { std::unique_ptr ConnectToService( const std::string& ip_address, int port, CancellationFlag* cancellation_flag) override; std::unique_ptr ListenForService(int port) override; - std::vector GetUpgradeAddressCandidates(const api::WifiLanServerSocket& server_socket) override; + std::vector GetUpgradeAddressCandidates(const api::WifiLanServerSocket& server_socket) override; private: GNCNWFramework* medium_; diff --git a/internal/platform/implementation/apple/wifi_lan.mm b/internal/platform/implementation/apple/wifi_lan.mm index 96a8b94b..80c24cbb 100644 --- a/internal/platform/implementation/apple/wifi_lan.mm +++ b/internal/platform/implementation/apple/wifi_lan.mm @@ -183,9 +183,11 @@ std::unique_ptr WifiLanMedium::ListenForService(int po return nil; } -std::vector WifiLanMedium::GetUpgradeAddressCandidates( +std::vector WifiLanMedium::GetUpgradeAddressCandidates( const api::WifiLanServerSocket& server_socket) { - return { server_socket.GetIPAddress() }; + std::string ip_address = server_socket.GetIPAddress(); + return {ServiceAddress{.address = std::vector(ip_address.begin(), ip_address.end()), + .port = static_cast(server_socket.GetPort())}}; } } // namespace apple diff --git a/internal/platform/implementation/g3/wifi_lan.cc b/internal/platform/implementation/g3/wifi_lan.cc index 9e64829a..249b4502 100644 --- a/internal/platform/implementation/g3/wifi_lan.cc +++ b/internal/platform/implementation/g3/wifi_lan.cc @@ -14,6 +14,7 @@ #include "internal/platform/implementation/g3/wifi_lan.h" +#include #include #include #include @@ -28,6 +29,7 @@ #include "internal/platform/logging.h" #include "internal/platform/medium_environment.h" #include "internal/platform/nsd_service_info.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace g3 { @@ -282,9 +284,12 @@ std::unique_ptr WifiLanMedium::ListenForService( return server_socket; } -std::vector WifiLanMedium::GetUpgradeAddressCandidates( +std::vector WifiLanMedium::GetUpgradeAddressCandidates( const api::WifiLanServerSocket& server_socket) { - return { server_socket.GetIPAddress() }; + std::string ip_address = server_socket.GetIPAddress(); + return {ServiceAddress{ + .address = std::vector(ip_address.begin(), ip_address.end()), + .port = static_cast(server_socket.GetPort())}}; } } // namespace g3 diff --git a/internal/platform/implementation/g3/wifi_lan.h b/internal/platform/implementation/g3/wifi_lan.h index 72dd4efd..1b695625 100644 --- a/internal/platform/implementation/g3/wifi_lan.h +++ b/internal/platform/implementation/g3/wifi_lan.h @@ -29,6 +29,7 @@ #include "internal/platform/medium_environment.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/output_stream.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace g3 { @@ -207,7 +208,7 @@ class WifiLanMedium : public api::WifiLanMedium { return std::nullopt; } - std::vector GetUpgradeAddressCandidates( + std::vector GetUpgradeAddressCandidates( const api::WifiLanServerSocket& server_socket) override; private: diff --git a/internal/platform/implementation/wifi_lan.h b/internal/platform/implementation/wifi_lan.h index 82691625..632c1eee 100644 --- a/internal/platform/implementation/wifi_lan.h +++ b/internal/platform/implementation/wifi_lan.h @@ -25,6 +25,7 @@ #include "internal/platform/listeners.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/output_stream.h" +#include "internal/platform/wifi_credential.h" namespace nearby { namespace api { @@ -160,7 +161,7 @@ class WifiLanMedium { // 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 GetUpgradeAddressCandidates( + virtual std::vector GetUpgradeAddressCandidates( const WifiLanServerSocket& server_socket) = 0; }; diff --git a/internal/platform/implementation/windows/wifi_lan.h b/internal/platform/implementation/windows/wifi_lan.h index 8b866e91..6052352a 100644 --- a/internal/platform/implementation/windows/wifi_lan.h +++ b/internal/platform/implementation/windows/wifi_lan.h @@ -27,6 +27,7 @@ #include #include #include +#include // Nearby connections headers #include "absl/base/nullability.h" @@ -50,6 +51,7 @@ #include "internal/platform/input_stream.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/output_stream.h" +#include "internal/platform/wifi_credential.h" // WinRT headers #include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.Enumeration.h" @@ -182,7 +184,7 @@ class WifiLanMedium : public api::WifiLanMedium { return absl::nullopt; } - std::vector GetUpgradeAddressCandidates( + std::vector GetUpgradeAddressCandidates( const api::WifiLanServerSocket& server_socket) override; private: diff --git a/internal/platform/implementation/windows/wifi_lan_medium.cc b/internal/platform/implementation/windows/wifi_lan_medium.cc index 717a1977..fbff2727 100644 --- a/internal/platform/implementation/windows/wifi_lan_medium.cc +++ b/internal/platform/implementation/windows/wifi_lan_medium.cc @@ -40,6 +40,7 @@ #include "internal/platform/exception.h" #include "internal/platform/feature_flags.h" #include "internal/platform/flags/nearby_platform_feature_flags.h" +#include "internal/platform/wifi_credential.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" @@ -735,11 +736,12 @@ bool WifiLanMedium::IsConnectableIpAddress(NsdServiceInfo& nsd_service_info, return false; } -std::vector WifiLanMedium::GetUpgradeAddressCandidates( +std::vector WifiLanMedium::GetUpgradeAddressCandidates( const api::WifiLanServerSocket& server_socket) { const NetworkInfo& network_info = NetworkInfo::GetNetworkInfo(); - std::vector ip_addresses; - std::vector ipv4_addresses; + uint16_t port = server_socket.GetPort(); + std::vector ip_addresses; + std::vector ipv4_addresses; for (const auto& net_interface : network_info.GetInterfaces()) { // Only use wifi and ethernet interfaces for upgrade. if (net_interface.type != InterfaceType::kWifi && @@ -753,16 +755,22 @@ std::vector WifiLanMedium::GetUpgradeAddressCandidates( if (address.IsV6LinkLocal()) { continue; } - ip_addresses.push_back( - std::string(reinterpret_cast( - &address.ipv6_address()->sin6_addr.u.Byte[0]), - 16)); + ip_addresses.push_back(ServiceAddress{ + .address = + std::vector(address.ipv6_address()->sin6_addr.u.Byte, + address.ipv6_address()->sin6_addr.u.Byte + 16), + .port = port, + }); } for (const auto& ipv4address : net_interface.ipv4_addresses) { auto address = reinterpret_cast(&ipv4address); - ipv4_addresses.push_back(std::string( - reinterpret_cast(&address->sin_addr.S_un.S_un_b.s_b1), - 4)); + ipv4_addresses.push_back(ServiceAddress{ + .address = {address->sin_addr.S_un.S_un_b.s_b1, + address->sin_addr.S_un.S_un_b.s_b2, + address->sin_addr.S_un.S_un_b.s_b3, + address->sin_addr.S_un.S_un_b.s_b4}, + .port = port, + }); } } if (NearbyFlags::GetInstance().GetBoolFlag( diff --git a/internal/platform/mock_wifi_lan_medium.h b/internal/platform/mock_wifi_lan_medium.h index 8b759969..ceb0700e 100644 --- a/internal/platform/mock_wifi_lan_medium.h +++ b/internal/platform/mock_wifi_lan_medium.h @@ -26,6 +26,7 @@ #include "internal/platform/cancellation_flag.h" #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/nsd_service_info.h" +#include "internal/platform/wifi_credential.h" namespace nearby { @@ -54,7 +55,7 @@ class MockWifiLanMedium : public api::WifiLanMedium { (int port), (override)); MOCK_METHOD((absl::optional>), GetDynamicPortRange, (), (override)); - MOCK_METHOD(std::vector, GetUpgradeAddressCandidates, + MOCK_METHOD(std::vector, GetUpgradeAddressCandidates, (const api::WifiLanServerSocket& server_socket), (override)); }; diff --git a/internal/platform/wifi_lan.cc b/internal/platform/wifi_lan.cc index 2dfea8f0..64355268 100644 --- a/internal/platform/wifi_lan.cc +++ b/internal/platform/wifi_lan.cc @@ -27,6 +27,7 @@ #include "internal/platform/nsd_service_info.h" #include "internal/platform/output_stream.h" #include "internal/platform/socket.h" +#include "internal/platform/wifi_credential.h" namespace nearby { using location::nearby::proto::connections::Medium; @@ -202,7 +203,7 @@ WifiLanSocket WifiLanMedium::ConnectToService( impl_->ConnectToService(ip_address, port, cancellation_flag)); } -std::vector WifiLanMedium::GetUpgradeAddressCandidates( +std::vector WifiLanMedium::GetUpgradeAddressCandidates( const WifiLanServerSocket& server_socket) { return impl_->GetUpgradeAddressCandidates(server_socket.GetImpl()); } diff --git a/internal/platform/wifi_lan.h b/internal/platform/wifi_lan.h index 0ad0845a..d52f37c2 100644 --- a/internal/platform/wifi_lan.h +++ b/internal/platform/wifi_lan.h @@ -37,6 +37,7 @@ #include "internal/platform/nsd_service_info.h" #include "internal/platform/output_stream.h" #include "internal/platform/socket.h" +#include "internal/platform/wifi_credential.h" namespace nearby { @@ -275,7 +276,7 @@ class WifiLanMedium { // 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 GetUpgradeAddressCandidates( + std::vector GetUpgradeAddressCandidates( const WifiLanServerSocket& server_socket); private: