From a0b84f865ccd3ad9bf3d9e0f3dd2ca13fb4ee736 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Tue, 17 Dec 2024 10:07:18 -0500 Subject: [PATCH] Update non-LL interface for the dedup logic as well --- .../AddressResolve_DefaultImpl.cpp | 28 +++++++++++-------- .../tests/TestAddressResolve_DefaultImpl.cpp | 8 ++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/lib/address_resolve/AddressResolve_DefaultImpl.cpp b/src/lib/address_resolve/AddressResolve_DefaultImpl.cpp index 1df91dc7985989..884016f26558bd 100644 --- a/src/lib/address_resolve/AddressResolve_DefaultImpl.cpp +++ b/src/lib/address_resolve/AddressResolve_DefaultImpl.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include "transport/raw/PeerAddress.h" #include #include @@ -128,6 +129,17 @@ NodeLookupAction NodeLookupHandle::NextAction(System::Clock::Timestamp now) bool NodeLookupResults::UpdateResults(const ResolveResult & result, const Dnssd::IPAddressSorter::IpScore newScore) { + Transport::PeerAddress addressWithAdjustedInterface = result.address; + if (!addressWithAdjustedInterface.GetIPAddress().IsIPv6LinkLocal()) + { + // Only use the DNS-SD resolution's InterfaceID for addresses that are IPv6 LLA. + // For all other addresses, we should rely on the device's routing table to route messages sent. + // Forcing messages down an InterfaceId might fail. For example, in bridged networks like Thread, + // mDNS advertisements are not usually received on the same interface the peer is reachable on. + addressWithAdjustedInterface.SetInterface(Inet::InterfaceId::Null()); + ChipLogDetail(Discovery, "Lookup clearing interface for non LL address"); + } + uint8_t insertAtIndex = 0; for (; insertAtIndex < kNodeLookupResultsLen; insertAtIndex++) { @@ -139,7 +151,7 @@ bool NodeLookupResults::UpdateResults(const ResolveResult & result, const Dnssd: auto & oldAddress = results[insertAtIndex].address; - if (oldAddress == result.address) + if (oldAddress == addressWithAdjustedInterface) { // this address is already in our list. return false; @@ -179,17 +191,9 @@ bool NodeLookupResults::UpdateResults(const ResolveResult & result, const Dnssd: count++; } - auto & updatedResult = results[insertAtIndex]; - updatedResult = result; - if (!updatedResult.address.GetIPAddress().IsIPv6LinkLocal()) - { - // Only use the DNS-SD resolution's InterfaceID for addresses that are IPv6 LLA. - // For all other addresses, we should rely on the device's routing table to route messages sent. - // Forcing messages down an InterfaceId might fail. For example, in bridged networks like Thread, - // mDNS advertisements are not usually received on the same interface the peer is reachable on. - updatedResult.address.SetInterface(Inet::InterfaceId::Null()); - ChipLogDetail(Discovery, "Lookup clearing interface for non LL address"); - } + auto & updatedResult = results[insertAtIndex]; + updatedResult = result; + updatedResult.address = addressWithAdjustedInterface; return true; } diff --git a/src/lib/address_resolve/tests/TestAddressResolve_DefaultImpl.cpp b/src/lib/address_resolve/tests/TestAddressResolve_DefaultImpl.cpp index 5d6f9e21ff88f4..9bdc52e9850dbb 100644 --- a/src/lib/address_resolve/tests/TestAddressResolve_DefaultImpl.cpp +++ b/src/lib/address_resolve/tests/TestAddressResolve_DefaultImpl.cpp @@ -44,6 +44,14 @@ using chip::Dnssd::IPAddressSorter::ScoreIpAddress; constexpr uint8_t kNumberOfAvailableSlots = CHIP_CONFIG_MDNS_RESOLVE_LOOKUP_RESULTS; +/// Get an address that should have `kUniqueLocal` (one of the lowest) priority. +/// +/// Since for various tests we check filling the cache with values, we allow +/// unique address generation by varying the `idx` parameter +/// +/// @param idx - a value to generate a unique IP address (in case we do not wand dedups) +/// @param port - port in case some tests would like to vary it. Required for PeerAddress +/// @param interfaceId - interface required for PeerAddress Transport::PeerAddress GetAddressWithLowScore(uint16_t idx = 4, uint16_t port = CHIP_PORT, Inet::InterfaceId interfaceId = Inet::InterfaceId::Null()) {