Support connecting using link local address in hotspot.

PiperOrigin-RevId: 829634620
This commit is contained in:
Francis Tsui
2025-11-07 17:29:44 -08:00
committed by Copybara-Service
parent 4f2f9146cb
commit f58e9820d5
3 changed files with 37 additions and 0 deletions
@@ -14,6 +14,7 @@
#include <cstdint>
#include <exception>
#include <memory>
#include <optional>
#include <string>
#include "absl/strings/str_format.h"
@@ -99,6 +100,17 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
LOG(ERROR) << "no valid service address and port to connect.";
return nullptr;
}
if (server_address.IsV6LinkLocal()) {
// Link local address need to be scoped to the wifi interface.
std::optional<uint32_t> wifi_interface_index =
wifi_hotspot_native_.GetWifiInterfaceIndex();
if (!wifi_interface_index.has_value()) {
LOG(ERROR)
<< "Wifi interface index is not available, skip link local address.";
return nullptr;
}
server_address.SetScopeId(wifi_interface_index.value());
}
VLOG(1) << "ConnectToService address: " << server_address.ToString();
LOG(INFO) << "Connecting to service.";
auto wifi_hotspot_socket = std::make_unique<WifiHotspotSocket>();
@@ -22,9 +22,11 @@
#include <cguid.h>
// clang-format on
#include <cstdint>
#include <cstring>
#include <cwchar>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
@@ -434,4 +436,21 @@ bool WifiHotspotNative::RenewIpv4Address() const {
return false;
}
std::optional<uint32_t> WifiHotspotNative::GetWifiInterfaceIndex() const {
GUID interface_guid = GetInterfaceGuid();
if (interface_guid == GUID_NULL) {
LOG(ERROR) << __func__ << ": No available WLAN Interface to use.";
return std::nullopt;
}
NET_LUID luid;
if (ConvertInterfaceGuidToLuid(&interface_guid, &luid) == NO_ERROR) {
NET_IFINDEX interface_index = 0;
if (ConvertInterfaceLuidToIndex(&luid, &interface_index) == NO_ERROR) {
return interface_index;
}
}
LOG(ERROR) << "Failed to convert interface guid to interface index.";
return std::nullopt;
}
} // namespace nearby::windows
@@ -22,7 +22,9 @@
#include <wlanapi.h>
// clang-format on
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include "absl/base/nullability.h"
@@ -48,6 +50,10 @@ class WifiHotspotNative {
// Returns true if the interface has a non local scoped IPv4 address.
bool HasAssignedAddress();
bool RenewIpv4Address() const;
// Return the interface index of the wifi interface used to connect to the
// hotspot.
// On error, returns std::nullopt.
std::optional<uint32_t> GetWifiInterfaceIndex() const;
private:
// Context for WLAN notification callback.