[ip6] simplify Ip6::SelectSourceAddress() (#9832)

This commit simplifies `Ip6::SelectSourceAddress()` by adding a new
boolean `newAddrIsPreferred` tracking whether the new address is
preferred over the previously chosen one as we iterate over all
addresses.
This commit is contained in:
Abtin Keshavarzian
2024-02-07 10:38:57 -08:00
committed by GitHub
parent ffe2f52579
commit aa8dfe10bd
+17 -41
View File
@@ -1309,6 +1309,7 @@ const Address *Ip6::SelectSourceAddress(const Address &aDestination) const
for (const Netif::UnicastAddress &addr : Get<ThreadNetif>().GetUnicastAddresses())
{
bool newAddrIsPreferred = false;
uint8_t matchLen;
uint8_t overrideScope;
@@ -1339,71 +1340,46 @@ const Address *Ip6::SelectSourceAddress(const Address &aDestination) const
if (bestAddr == nullptr)
{
bestAddr = &addr;
bestMatchLen = matchLen;
newAddrIsPreferred = true;
}
else if (addr.GetScope() < bestAddr->GetScope())
{
// Rule 2: Prefer appropriate scope
if (addr.GetScope() >= overrideScope)
{
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
}
newAddrIsPreferred = (addr.GetScope() >= overrideScope);
}
else if (addr.GetScope() > bestAddr->GetScope())
{
if (bestAddr->GetScope() < overrideScope)
{
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
}
newAddrIsPreferred = (bestAddr->GetScope() < overrideScope);
}
else if (addr.mPreferred != bestAddr->mPreferred)
{
// Rule 3: Avoid deprecated addresses
if (addr.mPreferred)
{
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
}
newAddrIsPreferred = addr.mPreferred;
}
else if (matchLen > bestMatchLen)
{
// Rule 6: Prefer matching label
// Rule 7: Prefer public address
// Rule 8: Use longest prefix matching
bestAddr = &addr;
bestMatchLen = matchLen;
newAddrIsPreferred = true;
}
else if ((matchLen == bestMatchLen) && (destIsRloc == Get<Mle::Mle>().IsRoutingLocator(addr.GetAddress())))
{
// Additional rule: Prefer RLOC source for RLOC destination, EID source for anything else
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
newAddrIsPreferred = true;
}
// infer destination scope based on prefix match
if (bestMatchLen >= bestAddr->mPrefixLength)
if (newAddrIsPreferred)
{
destScope = bestAddr->GetScope();
bestAddr = &addr;
bestMatchLen = matchLen;
// Infer destination scope based on prefix match
if (bestMatchLen >= bestAddr->mPrefixLength)
{
destScope = bestAddr->GetScope();
}
}
}