diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 7e3005228..4f5d46fa3 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -1298,20 +1298,16 @@ exit: const NetifUnicastAddress *Ip6::SelectSourceAddress(MessageInfo &aMessageInfo) { - Address * destination = &aMessageInfo.GetPeerAddr(); - const NetifUnicastAddress *rvalAddr = NULL; - const Address * candidateAddr; - uint8_t rvalPrefixMatched = 0; + Address * destination = &aMessageInfo.GetPeerAddr(); uint8_t destinationScope = destination->GetScope(); + const NetifUnicastAddress *rvalAddr = NULL; + uint8_t rvalPrefixMatched = 0; for (const NetifUnicastAddress *addr = Get().GetUnicastAddresses(); addr; addr = addr->GetNext()) { - uint8_t overrideScope; - uint8_t candidatePrefixMatched; - - candidateAddr = &addr->GetAddress(); - candidatePrefixMatched = destination->PrefixMatch(*candidateAddr); - overrideScope = (candidatePrefixMatched >= addr->mPrefixLength) ? addr->GetScope() : destinationScope; + const Address *candidateAddr = &addr->GetAddress(); + uint8_t candidatePrefixMatched; + uint8_t overrideScope; if (candidateAddr->IsAnycastRoutingLocator()) { @@ -1319,6 +1315,9 @@ const NetifUnicastAddress *Ip6::SelectSourceAddress(MessageInfo &aMessageInfo) continue; } + candidatePrefixMatched = destination->PrefixMatch(*candidateAddr); + overrideScope = (candidatePrefixMatched >= addr->mPrefixLength) ? addr->GetScope() : destinationScope; + if (rvalAddr == NULL) { // Rule 0: Prefer any address