From 0c0cedc48136927a49e0ace11faf0b14bdd3ebff Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 5 Feb 2024 10:23:57 -0800 Subject: [PATCH] [ip6] `SelectSourceAddress()` rule 1 on preferring same address (#9830) This commit moves the check for rule 1 on preferring same address as the destination before the `(bestAddr == nullptr)` check. This ensures that rule 1 will be correctly applied even when the address happens to be first one in the `GetUnicastAddresses()` list. --- src/core/net/ip6.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 2334aa712..3637b9008 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -1330,18 +1330,18 @@ const Address *Ip6::SelectSourceAddress(const Address &aDestination) const overrideScope = destScope; } - if (bestAddr == nullptr) - { - // Rule 0: Prefer any address - bestAddr = &addr; - bestMatchLen = matchLen; - } - else if (addr.GetAddress() == aDestination) + if (addr.GetAddress() == aDestination) { // Rule 1: Prefer same address bestAddr = &addr; ExitNow(); } + + if (bestAddr == nullptr) + { + bestAddr = &addr; + bestMatchLen = matchLen; + } else if (addr.GetScope() < bestAddr->GetScope()) { // Rule 2: Prefer appropriate scope