From 6c84ccad25890ac507320728ec42c786abc40ace Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 24 Nov 2023 09:11:19 -0800 Subject: [PATCH] [ip6] update Ip6::SelectSourceAddress() to prefer non-deprecated addresses (#9634) This commit updates `SelectSourceAddress()` to ensure that non-deprecated addresses are always preferred over deprecated ones. When iterating over addresses, the best option is tracked in `bestAddr`. If the new address is not preferred and the currently selected `bestAddr` is, the new address is skipped to ensure that it is not picked. --- src/core/net/ip6.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index b7759ef65..d5ddeecad 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -1366,11 +1366,19 @@ const Address *Ip6::SelectSourceAddress(const Address &aDestination) const continue; } } - else if (addr.mPreferred && !bestAddr->mPreferred) + else if (addr.mPreferred != bestAddr->mPreferred) { // Rule 3: Avoid deprecated addresses - bestAddr = &addr; - bestMatchLen = matchLen; + + if (addr.mPreferred) + { + bestAddr = &addr; + bestMatchLen = matchLen; + } + else + { + continue; + } } else if (matchLen > bestMatchLen) {