mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Fix mDNS registration using wrong hostname.
PiperOrigin-RevId: 803100146
This commit is contained in:
committed by
Copybara-Service
parent
a43b20008a
commit
ec4cc9338c
@@ -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 <typename T>
|
||||
using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation<T>;
|
||||
|
||||
std::optional<std::string> 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<std::wstring> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -142,11 +143,15 @@ void GetIpAddressesNative(int family, std::vector<std::string>& 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<std::string> InspectableReader::ReadStringArray(
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<std::wstring> 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
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -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<std::wstring> GetDnsHostName();
|
||||
|
||||
namespace Constants {
|
||||
// The Id of the Service Name SDP attribute
|
||||
const uint16_t SdpServiceNameAttributeId = 0x100;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -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<std::wstring> host_name = GetDnsHostName();
|
||||
ASSERT_TRUE(host_name.has_value());
|
||||
LOG(ERROR) << "host_name: "
|
||||
<< nearby::windows::string_utils::WideStringToString(*host_name);
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
@@ -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<std::wstring>(
|
||||
string_utils::StringToWideString(instance_name));
|
||||
dns_service_instance_name_ = string_utils::StringToWideString(instance_name);
|
||||
|
||||
std::optional<std::string> computer_name = GetComputerName();
|
||||
std::optional<std::wstring> 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<std::wstring>(
|
||||
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<std::string> 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;
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
// clang-format on
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#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<std::string> GetComputerName();
|
||||
void CleanUp() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
absl::Mutex mutex_;
|
||||
std::unique_ptr<absl::Notification> dns_service_notification_ = nullptr;
|
||||
bool is_service_started_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
std::unique_ptr<std::wstring> dns_service_instance_name_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
std::unique_ptr<std::wstring> 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_
|
||||
|
||||
Reference in New Issue
Block a user