From 9f34b9d31efd2807d0988901441230e5ea28bce3 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 8 Dec 2023 00:05:44 -0800 Subject: [PATCH] [trel-dnssd] prioritize wider-scope IPv6 address of TREL peers (#2118) This commit updates `TrelDnssd` to select the numerically smallest IPv6 address when a peer advertises multiple IPv6 addresses. This prioritizes globally-unique addresses (GUA) over unique-local addresses (ULA) with `fc00::/7` prefix, followed by link-local addresses (`fe80::/10`). --- src/trel_dnssd/trel_dnssd.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/trel_dnssd/trel_dnssd.cpp b/src/trel_dnssd/trel_dnssd.cpp index c285a83410a..4be678a5083 100644 --- a/src/trel_dnssd/trel_dnssd.cpp +++ b/src/trel_dnssd/trel_dnssd.cpp @@ -282,6 +282,7 @@ void TrelDnssd::HandleUnpublishTrelServiceError(otbrError aError) void TrelDnssd::OnTrelServiceInstanceAdded(const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo) { std::string instanceName = StringUtils::ToLowercase(aInstanceInfo.mName); + Ip6Address selectedAddress; otPlatTrelPeerInfo peerInfo; // Remove any existing TREL service instance before adding @@ -295,6 +296,15 @@ void TrelDnssd::OnTrelServiceInstanceAdded(const Mdns::Publisher::DiscoveredInst for (const auto &addr : aInstanceInfo.mAddresses) { otbrLogDebug("Peer address: %s", addr.ToString().c_str()); + + // If there are multiple addresses, we prefer the address + // which is numerically smallest. This prefers GUA over ULA + // (`fc00::/7`) and then link-local (`fe80::/10`). + + if (selectedAddress.IsUnspecified() || (addr < selectedAddress)) + { + selectedAddress = addr; + } } if (aInstanceInfo.mAddresses.empty()) @@ -304,7 +314,7 @@ void TrelDnssd::OnTrelServiceInstanceAdded(const Mdns::Publisher::DiscoveredInst } peerInfo.mRemoved = false; - memcpy(&peerInfo.mSockAddr.mAddress, &aInstanceInfo.mAddresses[0], sizeof(peerInfo.mSockAddr.mAddress)); + memcpy(&peerInfo.mSockAddr.mAddress, &selectedAddress, sizeof(peerInfo.mSockAddr.mAddress)); peerInfo.mSockAddr.mPort = aInstanceInfo.mPort; peerInfo.mTxtData = aInstanceInfo.mTxtData.data(); peerInfo.mTxtLength = aInstanceInfo.mTxtData.size();