diff --git a/src/core/net/netif.hpp b/src/core/net/netif.hpp index 21a21d536..c5cf008a1 100644 --- a/src/core/net/netif.hpp +++ b/src/core/net/netif.hpp @@ -219,12 +219,21 @@ public: * @param[in] aNetif A reference to the Netif instance. * */ - explicit ExternalMulticastAddressIterator(Netif &aNetif) + explicit ExternalMulticastAddressIterator(const Netif &aNetif) : mNetif(aNetif) { AdvanceFrom(mNetif.GetMulticastAddresses()); } + /** + * This method indicates whether the iterator has reached end of the list. + * + * @retval TRUE There are no more entries in the list (reached end of the list). + * @retval FALSE The current address entry is valid. + * + */ + bool IsDone(void) const { return mCurrent != nullptr; } + /** * This method overloads `++` operator (pre-increment) to advance the iterator. * @@ -298,7 +307,7 @@ public: kEndIterator, }; - ExternalMulticastAddressIterator(Netif &aNetif, IteratorType) + ExternalMulticastAddressIterator(const Netif &aNetif, IteratorType) : mNetif(aNetif) , mCurrent(nullptr) { @@ -315,7 +324,7 @@ public: const_cast(static_cast(aAddr)); } - Netif & mNetif; + const Netif & mNetif; ExternalNetifMulticastAddress *mCurrent; }; @@ -569,6 +578,15 @@ public: return ExternalMulticastAddressIteratorBuilder(*this); } + /** + * This method indicates whether or not the network interfaces is subscribed to any external multicast address. + * + * @retval TRUE The network interface is subscribed to at least one external multicast address. + * @retval FALSE The network interface is not subscribed to any external multicast address. + * + */ + bool HasAnyExternalMulticastAddress(void) const { return !ExternalMulticastAddressIterator(*this).IsDone(); } + protected: /** * This method subscribes the network interface to the realm-local all MPL forwarders, link-local, and realm-local @@ -595,7 +613,7 @@ private: class ExternalMulticastAddressIteratorBuilder { public: - ExternalMulticastAddressIteratorBuilder(Netif &aNetif) + ExternalMulticastAddressIteratorBuilder(const Netif &aNetif) : mNetif(aNetif) { } @@ -607,7 +625,7 @@ private: } private: - Netif &mNetif; + const Netif &mNetif; }; LinkedList mUnicastAddresses; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 98de82fd2..68c78d02a 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1235,14 +1235,7 @@ bool Mle::HasUnregisteredAddress(void) // For sleepy end-device, we register any external multicast // addresses. - for (const Ip6::NetifMulticastAddress *address = Get().GetMulticastAddresses(); address != nullptr; - address = address->GetNext()) - { - if (Get().IsMulticastAddressExternal(*address)) - { - ExitNow(retval = true); - } - } + retval = Get().HasAnyExternalMulticastAddress(); } exit: @@ -1341,25 +1334,19 @@ otError Mle::AppendAddressRegistration(Message &aMessage, AddressRegistrationMod #endif ) { - for (const Ip6::NetifMulticastAddress *addr = Get().GetMulticastAddresses(); addr != nullptr; - addr = addr->GetNext()) + for (const Ip6::NetifMulticastAddress &addr : Get().IterateExternalMulticastAddresses()) { - if (!Get().IsMulticastAddressExternal(*addr)) - { - continue; - } - #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) // For Thread 1.2 MED, skip multicast address with scope not // larger than realm local when registering. - if (IsRxOnWhenIdle() && !addr->GetAddress().IsMulticastLargerThanRealmLocal()) + if (IsRxOnWhenIdle() && !addr.GetAddress().IsMulticastLargerThanRealmLocal()) { continue; } #endif entry.SetUncompressed(); - entry.SetIp6Address(addr->GetAddress()); + entry.SetIp6Address(addr.GetAddress()); SuccessOrExit(error = aMessage.Append(&entry, entry.GetLength())); length += entry.GetLength();