diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index 67b54c8f..73c2a10f 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "absl/strings/str_format.h" @@ -99,6 +100,17 @@ std::unique_ptr 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 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(); diff --git a/internal/platform/implementation/windows/wifi_hotspot_native.cc b/internal/platform/implementation/windows/wifi_hotspot_native.cc index 6dbf4471..89ffc081 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_native.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_native.cc @@ -22,9 +22,11 @@ #include // clang-format on +#include #include #include #include +#include #include #include #include @@ -434,4 +436,21 @@ bool WifiHotspotNative::RenewIpv4Address() const { return false; } +std::optional 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 diff --git a/internal/platform/implementation/windows/wifi_hotspot_native.h b/internal/platform/implementation/windows/wifi_hotspot_native.h index 880d8ec4..6cc64aa4 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_native.h +++ b/internal/platform/implementation/windows/wifi_hotspot_native.h @@ -22,7 +22,9 @@ #include // clang-format on +#include #include +#include #include #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 GetWifiInterfaceIndex() const; private: // Context for WLAN notification callback.