Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[trel-dnssd] prioritize wider-scope IPv6 address of TREL peers #2118

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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