From ec4cc9338cd712ed1cd71ada05317fdaa0b13034 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Thu, 4 Sep 2025 11:11:35 -0700 Subject: [PATCH] Fix mDNS registration using wrong hostname. PiperOrigin-RevId: 803100146 --- .../implementation/windows/device_info.cc | 24 +++----------- .../platform/implementation/windows/utils.cc | 33 +++++++++++++++++-- .../platform/implementation/windows/utils.h | 4 +++ .../implementation/windows/utils_test.cc | 9 +++++ .../implementation/windows/wifi_lan_mdns.cc | 31 +++++------------ .../implementation/windows/wifi_lan_mdns.h | 7 ++-- 6 files changed, 59 insertions(+), 49 deletions(-) diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index e524b656..43ea0b7c 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -29,7 +29,7 @@ #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/windows/device_paths.h" #include "internal/platform/implementation/windows/string_utils.h" -#include "internal/platform/logging.h" +#include "internal/platform/implementation/windows/utils.h" #include "winrt/Windows.Foundation.Collections.h" #include "winrt/Windows.Foundation.h" #include "winrt/Windows.System.h" @@ -52,26 +52,10 @@ template using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation; std::optional DeviceInfo::GetOsDeviceName() const { - DWORD size = 0; - - // Get length of the computer name. - if (GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size) == 0) { - if (GetLastError() != ERROR_MORE_DATA) { - LOG(ERROR) << ": Failed to get device name size, error:" - << GetLastError(); - return std::nullopt; - } + std::optional device_name = GetDnsHostName(); + if (device_name.has_value()) { + return WideStringToString(*device_name); } - std::wstring device_name(size, L' '); - if (GetComputerNameExW(ComputerNameDnsHostname, device_name.data(), &size) != - 0) { - // On input size includes null termination. - // On output size excludes null termination. - device_name.resize(size); - return WideStringToString(device_name); - } - - LOG(ERROR) << ": Failed to get device name, error:" << GetLastError(); return std::nullopt; } diff --git a/internal/platform/implementation/windows/utils.cc b/internal/platform/implementation/windows/utils.cc index 3641bcd5..036f75ef 100644 --- a/internal/platform/implementation/windows/utils.cc +++ b/internal/platform/implementation/windows/utils.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -142,11 +143,15 @@ void GetIpAddressesNative(int family, std::vector& wifi_addresses, while (next_address != nullptr) { if (next_address->OperStatus == IfOperStatusUp) { if (next_address->IfType == IF_TYPE_ETHERNET_CSMACD) { - VLOG(1) << "Found ethernet adater: " << next_address->AdapterName; + VLOG(1) << "Found ethernet adater: " << next_address->AdapterName + << " index: " << next_address->IfIndex + << " v6 index: " << next_address->Ipv6IfIndex; AddIpUnicastAddresses(next_address->FirstUnicastAddress, ethernet_addresses); } else if (next_address->IfType == IF_TYPE_IEEE80211) { - VLOG(1) << "Found wifi adapter: " << next_address->AdapterName; + VLOG(1) << "Found wifi adapter: " << next_address->AdapterName + << " index: " << next_address->IfIndex + << " v6 index: " << next_address->Ipv6IfIndex; AddIpUnicastAddresses(next_address->FirstUnicastAddress, wifi_addresses); } else if (next_address->IfType != IF_TYPE_SOFTWARE_LOOPBACK) { @@ -402,5 +407,29 @@ std::vector InspectableReader::ReadStringArray( return result; } +std::optional GetDnsHostName() { + DWORD size = 0; + + // Get length of the computer name. + if (GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size) == 0) { + if (GetLastError() != ERROR_MORE_DATA) { + LOG(ERROR) << ": Failed to get device dns name size, error:" + << GetLastError(); + return std::nullopt; + } + } + std::wstring device_name(size, L' '); + if (GetComputerNameExW(ComputerNameDnsHostname, device_name.data(), &size) != + 0) { + // On input size includes null termination. + // On output size excludes null termination. + device_name.resize(size); + return device_name; + } + + LOG(ERROR) << ": Failed to get device dns name, error:" << GetLastError(); + return std::nullopt; +} + } // namespace windows } // namespace nearby diff --git a/internal/platform/implementation/windows/utils.h b/internal/platform/implementation/windows/utils.h index c3cee955..d3d79bfe 100644 --- a/internal/platform/implementation/windows/utils.h +++ b/internal/platform/implementation/windows/utils.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -59,6 +60,9 @@ winrt::guid nearby_uuid_to_winrt_guid(Uuid uuid); bool is_nearby_uuid_equal_to_winrt_guid(const Uuid& uuid, const ::winrt::guid& guid); +// Returns the DNS host name of the computer or std::nullopt if it fails. +std::optional GetDnsHostName(); + namespace Constants { // The Id of the Service Name SDP attribute const uint16_t SdpServiceNameAttributeId = 0x100; diff --git a/internal/platform/implementation/windows/utils_test.cc b/internal/platform/implementation/windows/utils_test.cc index fd58218a..2dd15e35 100644 --- a/internal/platform/implementation/windows/utils_test.cc +++ b/internal/platform/implementation/windows/utils_test.cc @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -25,6 +26,7 @@ #include "gtest/gtest.h" #include "absl/strings/string_view.h" #include "internal/platform/byte_array.h" +#include "internal/platform/implementation/windows/string_utils.h" #include "internal/platform/logging.h" #include "internal/platform/uuid.h" #include "winrt/Windows.Foundation.h" @@ -165,5 +167,12 @@ TEST(UtilsTests, GetIpv4Addresses) { LOG(ERROR) << "GetIpv4Addresses done"; } +TEST(UtilsTests, GetDnsHostName) { + std::optional host_name = GetDnsHostName(); + ASSERT_TRUE(host_name.has_value()); + LOG(ERROR) << "host_name: " + << nearby::windows::string_utils::WideStringToString(*host_name); +} + } // namespace windows } // namespace nearby diff --git a/internal/platform/implementation/windows/wifi_lan_mdns.cc b/internal/platform/implementation/windows/wifi_lan_mdns.cc index 42257961..69184b57 100644 --- a/internal/platform/implementation/windows/wifi_lan_mdns.cc +++ b/internal/platform/implementation/windows/wifi_lan_mdns.cc @@ -30,6 +30,7 @@ #include "absl/synchronization/notification.h" #include "absl/time/time.h" #include "internal/platform/implementation/windows/string_utils.h" +#include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" namespace nearby::windows { @@ -66,22 +67,20 @@ bool WifiLanMdns::StartMdnsService( // Composite the service request. std::string instance_name = absl::StrFormat(kMdnsInstanceNameFormat, service_name, service_type); - dns_service_instance_name_ = std::make_unique( - string_utils::StringToWideString(instance_name)); + dns_service_instance_name_ = string_utils::StringToWideString(instance_name); - std::optional computer_name = GetComputerName(); + std::optional computer_name = GetDnsHostName(); if (!computer_name.has_value()) { LOG(ERROR) << "Failed to get computer name."; return false; } + computer_name->append(L".local"); + host_name_ = computer_name.value(); - std::string host_name = absl::StrFormat(kMdnsHostName, *computer_name); - host_name_ = std::make_unique( - string_utils::StringToWideString(host_name)); - - dns_service_instance_.pszInstanceName = - (LPWSTR)dns_service_instance_name_->c_str(); - dns_service_instance_.pszHostName = (LPWSTR)host_name_->c_str(); + dns_service_instance_.pszInstanceName = dns_service_instance_name_.data(); + // Hostname must match the host's DNS name, otherwise A/AAAA records cannot be + // resolved. + dns_service_instance_.pszHostName = host_name_.data(); dns_service_instance_.wPort = port; // Allocate memory for filling text records, it should be freed in @@ -181,18 +180,6 @@ void WifiLanMdns::NotifyStatusUpdated(DWORD status) { } } -std::optional WifiLanMdns::GetComputerName() { - char computer_name[MAX_COMPUTERNAME_LENGTH + 1]; - DWORD size = sizeof(computer_name); - - // Get the computer name. - if (::GetComputerNameA(computer_name, &size)) { - return std::string(computer_name, size); - } else { - return std::nullopt; - } -} - void WifiLanMdns::DnsServiceRegisterComplete(DWORD Status, PVOID pQueryContext, PDNS_SERVICE_INSTANCE pInstance) { VLOG(1) << "DnsServiceRegisterComplete: " << Status; diff --git a/internal/platform/implementation/windows/wifi_lan_mdns.h b/internal/platform/implementation/windows/wifi_lan_mdns.h index 9cc10bff..e3e1c125 100644 --- a/internal/platform/implementation/windows/wifi_lan_mdns.h +++ b/internal/platform/implementation/windows/wifi_lan_mdns.h @@ -21,7 +21,6 @@ // clang-format on #include -#include #include #include "absl/base/thread_annotations.h" @@ -48,15 +47,13 @@ class WifiLanMdns { private: static void DnsServiceRegisterComplete(DWORD Status, PVOID pQueryContext, PDNS_SERVICE_INSTANCE pInstance); - std::optional GetComputerName(); void CleanUp() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); absl::Mutex mutex_; std::unique_ptr dns_service_notification_ = nullptr; bool is_service_started_ ABSL_GUARDED_BY(mutex_) = false; - std::unique_ptr dns_service_instance_name_ - ABSL_GUARDED_BY(mutex_); - std::unique_ptr host_name_ ABSL_GUARDED_BY(mutex_); + std::wstring dns_service_instance_name_ ABSL_GUARDED_BY(mutex_); + std::wstring host_name_ ABSL_GUARDED_BY(mutex_); DNS_SERVICE_INSTANCE dns_service_instance_ ABSL_GUARDED_BY(mutex_); DNS_SERVICE_REGISTER_REQUEST dns_service_register_request_