From 3c78cb30b7d3804d3b50f5a939bc585bad4e9525 Mon Sep 17 00:00:00 2001 From: hai007 Date: Thu, 17 Nov 2022 11:21:48 -0800 Subject: [PATCH] When calling GetIpAddress() before GetInformation(), it will fail to get right IP Address. PiperOrigin-RevId: 489261279 --- internal/platform/implementation/windows/wifi.h | 5 +++-- internal/platform/implementation/windows/wifi_medium.cc | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/platform/implementation/windows/wifi.h b/internal/platform/implementation/windows/wifi.h index e4b8cd22..597b9419 100644 --- a/internal/platform/implementation/windows/wifi.h +++ b/internal/platform/implementation/windows/wifi.h @@ -15,9 +15,9 @@ #ifndef PLATFORM_IMPL_WINDOWS_WIFI_H_ #define PLATFORM_IMPL_WINDOWS_WIFI_H_ +#include #include #include -#include #include // Nearby connections headers @@ -25,8 +25,8 @@ #include "internal/platform/wifi_utils.h" // WinRT headers -#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.Collections.h" +#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Connectivity.h" namespace location { @@ -117,6 +117,7 @@ class WifiMedium : public api::WifiMedium { // Since the WiFi interface capability won't change in the connection session, // we only need to query it once at the beginning void InitCapability(); + std::string InternalGetIpAddress(); bool wifi_interface_valid_; api::WifiCapability wifi_capability_; diff --git a/internal/platform/implementation/windows/wifi_medium.cc b/internal/platform/implementation/windows/wifi_medium.cc index 8ec4ffa5..19e00463 100644 --- a/internal/platform/implementation/windows/wifi_medium.cc +++ b/internal/platform/implementation/windows/wifi_medium.cc @@ -194,7 +194,7 @@ api::WifiInformation& WifiMedium::GetInformation() { p_intf_list = NULL; WlanCloseHandle(client_handle, NULL); - wifi_information_.ip_address_dot_decimal = GetIpAddress(); + wifi_information_.ip_address_dot_decimal = InternalGetIpAddress(); if (!wifi_information_.ip_address_dot_decimal.empty()) { wifi_information_.ip_address_4_bytes = ipaddr_dotdecimal_to_4bytes_string( wifi_information_.ip_address_dot_decimal); @@ -204,6 +204,11 @@ api::WifiInformation& WifiMedium::GetInformation() { } std::string WifiMedium::GetIpAddress() { + GetInformation(); + return wifi_information_.ip_address_dot_decimal; +} + +std::string WifiMedium::InternalGetIpAddress() { try { auto host_names = NetworkInformation::GetHostNames(); for (auto host_name : host_names) {