[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()) for (const Netif::UnicastAddress &addr : Get<ThreadNetif>().GetUnicastAddresses())
{ {
bool newAddrIsPreferred = false;
uint8_t matchLen; uint8_t matchLen;
uint8_t overrideScope; uint8_t overrideScope;
@@ -1339,71 +1340,46 @@ const Address *Ip6::SelectSourceAddress(const Address &aDestination) const
if (bestAddr == nullptr) if (bestAddr == nullptr)
{ {
bestAddr = &addr; newAddrIsPreferred = true;
bestMatchLen = matchLen;
} }
else if (addr.GetScope() < bestAddr->GetScope()) else if (addr.GetScope() < bestAddr->GetScope())
{ {
// Rule 2: Prefer appropriate scope // Rule 2: Prefer appropriate scope
if (addr.GetScope() >= overrideScope) newAddrIsPreferred = (addr.GetScope() >= overrideScope);
{
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
}
} }
else if (addr.GetScope() > bestAddr->GetScope()) else if (addr.GetScope() > bestAddr->GetScope())
{ {
if (bestAddr->GetScope() < overrideScope) newAddrIsPreferred = (bestAddr->GetScope() < overrideScope);
{
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
}
} }
else if (addr.mPreferred != bestAddr->mPreferred) else if (addr.mPreferred != bestAddr->mPreferred)
{ {
// Rule 3: Avoid deprecated addresses // Rule 3: Avoid deprecated addresses
newAddrIsPreferred = addr.mPreferred;
if (addr.mPreferred)
{
bestAddr = &addr;
bestMatchLen = matchLen;
}
else
{
continue;
}
} }
else if (matchLen > bestMatchLen) else if (matchLen > bestMatchLen)
{ {
// Rule 6: Prefer matching label // Rule 6: Prefer matching label
// Rule 7: Prefer public address // Rule 7: Prefer public address
// Rule 8: Use longest prefix matching // Rule 8: Use longest prefix matching
bestAddr = &addr;
bestMatchLen = matchLen; newAddrIsPreferred = true;
} }
else if ((matchLen == bestMatchLen) && (destIsRloc == Get<Mle::Mle>().IsRoutingLocator(addr.GetAddress()))) else if ((matchLen == bestMatchLen) && (destIsRloc == Get<Mle::Mle>().IsRoutingLocator(addr.GetAddress())))
{ {
// Additional rule: Prefer RLOC source for RLOC destination, EID source for anything else // Additional rule: Prefer RLOC source for RLOC destination, EID source for anything else
bestAddr = &addr; newAddrIsPreferred = true;
bestMatchLen = matchLen;
}
else
{
continue;
} }
// infer destination scope based on prefix match if (newAddrIsPreferred)
if (bestMatchLen >= bestAddr->mPrefixLength)
{ {
destScope = bestAddr->GetScope(); bestAddr = &addr;
bestMatchLen = matchLen;
// Infer destination scope based on prefix match
if (bestMatchLen >= bestAddr->mPrefixLength)
{
destScope = bestAddr->GetScope();
}
} }
} }