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