Skip to content

Commit

Permalink
[trel-dnssd] prioritize wider-scope IPv6 address of TREL peers (#2118)
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
abtink committed Dec 8, 2023
1 parent c21797d commit 9f34b9d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/trel_dnssd/trel_dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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();
Expand Down

0 comments on commit 9f34b9d

Please sign in to comment.