From 107919603e5fdb4a0910864fb6f5be24a222fa08 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 20 Feb 2023 09:33:44 -0500 Subject: [PATCH] Don't claim minimal mdns is initialized until it's advertising on some IPv6 interface. (#25173) This fixes two things: 1. We now don't consider advertising properly initialized until we are advertising on at least one ipv6 interface. 2. Actually check which sorts of addresses interfaces have, instead of just assuming that all interfaces have both IPv4 and IPv6 addresses. Fixes https://github.com/project-chip/connectedhomeip/issues/25013 --- .../AddressPolicy_DefaultImpl.cpp | 42 ++++++++++++++++--- src/lib/dnssd/minimal_mdns/Server.cpp | 4 +- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.cpp b/src/lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.cpp index 5e668966bfbf51..0710c2249496c2 100644 --- a/src/lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.cpp +++ b/src/lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.cpp @@ -69,23 +69,38 @@ class AllInterfaces : public mdns::Minimal::ListenIterator #if INET_CONFIG_ENABLE_IPV4 if (mState == State::kIpV4) { - *id = mIterator.GetInterfaceId(); - *type = chip::Inet::IPAddressType::kIPv4; mState = State::kIpV6; - return true; + + if (CurrentInterfaceHasAddressOfType(chip::Inet::IPAddressType::kIPv4)) + { + *id = mIterator.GetInterfaceId(); + *type = chip::Inet::IPAddressType::kIPv4; + return true; + } } #endif - *id = mIterator.GetInterfaceId(); - *type = chip::Inet::IPAddressType::kIPv6; #if INET_CONFIG_ENABLE_IPV4 mState = State::kIpV4; #endif + bool haveResult = CurrentInterfaceHasAddressOfType(chip::Inet::IPAddressType::kIPv6); + if (haveResult) + { + *id = mIterator.GetInterfaceId(); + *type = chip::Inet::IPAddressType::kIPv6; + } + for (mIterator.Next(); SkipCurrentInterface(); mIterator.Next()) { } - return true; + + if (haveResult) + { + return true; + } + + return Next(id, type); } private: @@ -119,6 +134,8 @@ class AllInterfaces : public mdns::Minimal::ListenIterator return !IsCurrentInterfaceUsable(mIterator); } + + bool CurrentInterfaceHasAddressOfType(chip::Inet::IPAddressType type); }; class AllAddressesIterator : public mdns::Minimal::IpAddressIterator @@ -170,6 +187,19 @@ class AllAddressesIterator : public mdns::Minimal::IpAddressIterator chip::Inet::InterfaceAddressIterator mIterator; }; +bool AllInterfaces::CurrentInterfaceHasAddressOfType(chip::Inet::IPAddressType type) +{ + // mIterator.HasCurrent() must be true here. + AllAddressesIterator addressIter(mIterator.GetInterfaceId(), type); + chip::Inet::IPAddress addr; + if (addressIter.Next(addr)) + { + return true; + } + + return false; +} + class DefaultAddressPolicy : public AddressPolicy { public: diff --git a/src/lib/dnssd/minimal_mdns/Server.cpp b/src/lib/dnssd/minimal_mdns/Server.cpp index ea6dc693739d86..7fd02f4968aae9 100644 --- a/src/lib/dnssd/minimal_mdns/Server.cpp +++ b/src/lib/dnssd/minimal_mdns/Server.cpp @@ -279,8 +279,8 @@ CHIP_ERROR ServerBase::Listen(chip::Inet::EndPointManager