From 1bd98b7804664494d5636d7b9bc54ec55a34a17f Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 8 Jul 2025 15:57:15 -0700 Subject: [PATCH] [netdata] enhance `NetworkData::Service::Iterator` (#11680) This commit enhances the `NetworkData::Service::Iterator` class. The `Iterator` is now a separate class from `Service::Manager`. It provides `GetNextDnsSrpAnycastInfo()` & `GetNextDnsSrpUnicastInfo()` methods, simplifying the code for iterating over these service entries. The `Iterator` is also generalized to track a given `NetworkData` instance, allowing it to iterate over service entries on any `NetworkData` object, not just the Leader's. --- src/core/net/srp_client.cpp | 14 +-- src/core/net/srp_server.cpp | 8 +- src/core/thread/address_resolver.cpp | 4 +- src/core/thread/network_data.hpp | 4 +- src/core/thread/network_data_publisher.cpp | 16 +-- src/core/thread/network_data_service.cpp | 104 ++++++++++------- src/core/thread/network_data_service.hpp | 128 +++++++++++---------- tests/unit/test_network_data.cpp | 24 ++-- 8 files changed, 163 insertions(+), 139 deletions(-) diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index d43bd52b6..e9dcca06e 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -2413,9 +2413,9 @@ exit: Error Client::SelectUnicastEntry(DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo) const { - Error error = kErrorNotFound; - DnsSrpUnicastInfo unicastInfo; - NetworkData::Service::Manager::Iterator iterator; + Error error = kErrorNotFound; + DnsSrpUnicastInfo unicastInfo; + NetworkData::Service::Iterator iterator(GetInstance()); #if OPENTHREAD_CONFIG_SRP_CLIENT_SAVE_SELECTED_SERVER_ENABLE Settings::SrpClientInfo savedInfo; bool hasSavedServerInfo = false; @@ -2426,7 +2426,7 @@ Error Client::SelectUnicastEntry(DnsSrpUnicastType aType, DnsSrpUnicastInfo &aIn } #endif - while (Get().GetNextDnsSrpUnicastInfo(iterator, aType, unicastInfo) == kErrorNone) + while (iterator.GetNextDnsSrpUnicastInfo(aType, unicastInfo) == kErrorNone) { bool preferNewEntry; @@ -2519,10 +2519,10 @@ void Client::SelectNextServer(bool aDisallowSwitchOnRegisteredHost) do { - DnsSrpUnicastInfo unicastInfo; - NetworkData::Service::Manager::Iterator iterator; + DnsSrpUnicastInfo unicastInfo; + NetworkData::Service::Iterator iterator(GetInstance()); - while (Get().GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNone) + while (iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo) == kErrorNone) { if (selectNext) { diff --git a/src/core/net/srp_server.cpp b/src/core/net/srp_server.cpp index a67bd36e2..87f8e9688 100644 --- a/src/core/net/srp_server.cpp +++ b/src/core/net/srp_server.cpp @@ -303,7 +303,7 @@ bool Server::NetDataContainsOtherSrpServers(void) const bool contains = false; NetworkData::Service::DnsSrpAnycastInfo anycastInfo; NetworkData::Service::DnsSrpUnicastInfo unicastInfo; - NetworkData::Service::Manager::Iterator iterator; + NetworkData::Service::Iterator iterator(GetInstance()); if (Get().FindPreferredDnsSrpAnycastInfo(anycastInfo) == kErrorNone) { @@ -313,8 +313,7 @@ bool Server::NetDataContainsOtherSrpServers(void) const iterator.Reset(); - if (Get().GetNextDnsSrpUnicastInfo( - iterator, NetworkData::Service::kAddrInServiceData, unicastInfo) == kErrorNone) + if (iterator.GetNextDnsSrpUnicastInfo(NetworkData::Service::kAddrInServiceData, unicastInfo) == kErrorNone) { contains = true; ExitNow(); @@ -322,8 +321,7 @@ bool Server::NetDataContainsOtherSrpServers(void) const iterator.Reset(); - while (Get().GetNextDnsSrpUnicastInfo( - iterator, NetworkData::Service::kAddrInServerData, unicastInfo) == kErrorNone) + while (iterator.GetNextDnsSrpUnicastInfo(NetworkData::Service::kAddrInServerData, unicastInfo) == kErrorNone) { if (!Get().HasRloc16(unicastInfo.mRloc16) && Get().GetMeshLocalEid() != unicastInfo.mSockAddr.GetAddress()) diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index eb042c5da..546195097 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -587,13 +587,13 @@ Error AddressResolver::ResolveUsingNetDataServices(const Ip6::Address &aEid, uin // if successful, otherwise returns `kErrorNotFound`. Error error = kErrorNotFound; - NetworkData::Service::Manager::Iterator iterator; + NetworkData::Service::Iterator iterator(GetInstance()); NetworkData::Service::DnsSrpUnicastInfo unicastInfo; NetworkData::Service::DnsSrpUnicastType type = NetworkData::Service::kAddrInServerData; VerifyOrExit(Get().GetDeviceMode().GetNetworkDataType() == NetworkData::kFullSet); - while (Get().GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNone) + while (iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo) == kErrorNone) { if (aEid == unicastInfo.mSockAddr.GetAddress()) { diff --git a/src/core/thread/network_data.hpp b/src/core/thread/network_data.hpp index fd1c2988d..7eab9b7d7 100644 --- a/src/core/thread/network_data.hpp +++ b/src/core/thread/network_data.hpp @@ -78,8 +78,9 @@ namespace ot { namespace NetworkData { namespace Service { +class Iterator; class Manager; -} +} // namespace Service /** * @addtogroup core-netdata-core @@ -109,6 +110,7 @@ class NetworkData : public InstanceLocator friend class Leader; friend class Publisher; friend class MutableNetworkData; + friend class Service::Iterator; friend class Service::Manager; public: diff --git a/src/core/thread/network_data_publisher.cpp b/src/core/thread/network_data_publisher.cpp index de7d67237..69af30e3a 100644 --- a/src/core/thread/network_data_publisher.cpp +++ b/src/core/thread/network_data_publisher.cpp @@ -690,10 +690,10 @@ void Publisher::DnsSrpServiceEntry::CountAnycastEntries(uint8_t &aNumEntries, ui // (routers are preferred over end-devices. If same type, then // the smaller RLOC16 value is preferred). - Service::Manager::Iterator iterator; + Service::Iterator iterator(GetInstance()); Service::DnsSrpAnycastInfo anycastInfo; - while (Get().GetNextDnsSrpAnycastInfo(iterator, anycastInfo) == kErrorNone) + while (iterator.GetNextDnsSrpAnycastInfo(anycastInfo) == kErrorNone) { if (anycastInfo.mSequenceNumber == mInfo.GetSequenceNumber() && (anycastInfo.mVersion >= mInfo.GetVersion())) { @@ -709,10 +709,10 @@ void Publisher::DnsSrpServiceEntry::CountAnycastEntries(uint8_t &aNumEntries, ui bool Publisher::DnsSrpServiceEntry::HasAnyAnycastEntry(void) const { - Service::Manager::Iterator iterator; + Service::Iterator iterator(GetInstance()); Service::DnsSrpAnycastInfo anycastInfo; - return (Get().GetNextDnsSrpAnycastInfo(iterator, anycastInfo) == kErrorNone); + return (iterator.GetNextDnsSrpAnycastInfo(anycastInfo) == kErrorNone); } void Publisher::DnsSrpServiceEntry::CountUnicastEntries(Service::DnsSrpUnicastType aType, @@ -726,10 +726,10 @@ void Publisher::DnsSrpServiceEntry::CountUnicastEntries(Service::DnsSrpUnicastTy // over end-devices. If same type, then the smaller RLOC16 value is // preferred). - Service::Manager::Iterator iterator; + Service::Iterator iterator(GetInstance()); Service::DnsSrpUnicastInfo unicastInfo; - while (Get().GetNextDnsSrpUnicastInfo(iterator, aType, unicastInfo) == kErrorNone) + while (iterator.GetNextDnsSrpUnicastInfo(aType, unicastInfo) == kErrorNone) { if (unicastInfo.mVersion >= mInfo.GetVersion()) { @@ -745,11 +745,11 @@ void Publisher::DnsSrpServiceEntry::CountUnicastEntries(Service::DnsSrpUnicastTy bool Publisher::DnsSrpServiceEntry::HasAnyServiceDataUnicastEntry(void) const { - Service::Manager::Iterator iterator; + Service::Iterator iterator(GetInstance()); Service::DnsSrpUnicastInfo unicastInfo; Service::DnsSrpUnicastType type = Service::kAddrInServiceData; - return (Get().GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNone); + return (iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo) == kErrorNone); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/core/thread/network_data_service.cpp b/src/core/thread/network_data_service.cpp index ead0b05b7..6d72bc62b 100644 --- a/src/core/thread/network_data_service.cpp +++ b/src/core/thread/network_data_service.cpp @@ -39,6 +39,25 @@ namespace ot { namespace NetworkData { namespace Service { +Iterator::Iterator(Instance &aInstance) + : Iterator(aInstance, aInstance.Get()) +{ +} + +Iterator::Iterator(Instance &aInstance, const NetworkData &aNetworkData) + : InstanceLocator(aInstance) + , mNetworkData(aNetworkData) + , mServiceTlv(nullptr) + , mServerSubTlv(nullptr) +{ +} + +void Iterator::Reset(void) +{ + mServiceTlv = nullptr; + mServerSubTlv = nullptr; +} + #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE Error Manager::AddDnsSrpAnycastService(uint8_t aSequenceNumber, uint8_t aVersion) @@ -99,11 +118,11 @@ void Manager::GetBackboneRouterPrimary(ot::BackboneRouter::Config &aConfig) cons while ((serviceTlv = Get().FindNextThreadService(serviceTlv, serviceData, NetworkData::kServicePrefixMatch)) != nullptr) { - Iterator iterator; + Iterator iterator(GetInstance()); iterator.mServiceTlv = serviceTlv; - while (IterateToNextServer(iterator) == kErrorNone) + while (iterator.AdvanceToNextServer() == kErrorNone) { ServerData data; const BbrServerData *serverData; @@ -157,10 +176,10 @@ exit: #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) -Error Manager::GetNextDnsSrpAnycastInfo(Iterator &aIterator, DnsSrpAnycastInfo &aInfo) const +Error Iterator::GetNextDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) { Error error = kErrorNone; - uint8_t serviceNumber = kDnsSrpAnycastServiceNumber; + uint8_t serviceNumber = Manager::kDnsSrpAnycastServiceNumber; do { @@ -168,21 +187,20 @@ Error Manager::GetNextDnsSrpAnycastInfo(Iterator &aIterator, DnsSrpAnycastInfo & // Process the next Server sub-TLV in the current Service TLV. - if (IterateToNextServer(aIterator) == kErrorNone) + if (AdvanceToNextServer() == kErrorNone) { - uint8_t dataLength = aIterator.mServiceTlv->GetServiceDataLength(); + uint8_t dataLength = mServiceTlv->GetServiceDataLength(); - if (dataLength >= sizeof(DnsSrpAnycastServiceData)) + if (dataLength >= sizeof(Manager::DnsSrpAnycastServiceData)) { - const DnsSrpAnycastServiceData *anycastData = - reinterpret_cast(aIterator.mServiceTlv->GetServiceData()); + const Manager::DnsSrpAnycastServiceData *anycastData = + reinterpret_cast(mServiceTlv->GetServiceData()); - Get().GetServiceAloc(aIterator.mServiceTlv->GetServiceId(), aInfo.mAnycastAddress); + Get().GetServiceAloc(mServiceTlv->GetServiceId(), aInfo.mAnycastAddress); aInfo.mSequenceNumber = anycastData->GetSequenceNumber(); - aInfo.mRloc16 = aIterator.mServerSubTlv->GetServer16(); - aInfo.mVersion = (aIterator.mServerSubTlv->GetServerDataLength() >= sizeof(uint8_t)) - ? *aIterator.mServerSubTlv->GetServerData() - : 0; + aInfo.mRloc16 = mServerSubTlv->GetServer16(); + aInfo.mVersion = + (mServerSubTlv->GetServerDataLength() >= sizeof(uint8_t)) ? *mServerSubTlv->GetServerData() : 0; ExitNow(); } } @@ -190,14 +208,13 @@ Error Manager::GetNextDnsSrpAnycastInfo(Iterator &aIterator, DnsSrpAnycastInfo & // Find the next matching Service TLV. serviceData.InitFrom(serviceNumber); - aIterator.mServiceTlv = - Get().FindNextThreadService(aIterator.mServiceTlv, serviceData, NetworkData::kServicePrefixMatch); - aIterator.mServerSubTlv = nullptr; + mServiceTlv = mNetworkData.FindNextThreadService(mServiceTlv, serviceData, NetworkData::kServicePrefixMatch); + mServerSubTlv = nullptr; // If we have a valid Service TLV, restart the loop // to process its Server sub-TLVs. - } while (aIterator.mServiceTlv != nullptr); + } while (mServiceTlv != nullptr); error = kErrorNotFound; @@ -208,7 +225,7 @@ exit: Error Manager::FindPreferredDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) const { Error error = kErrorNotFound; - Iterator iterator; + Iterator iterator(GetInstance()); DnsSrpAnycastInfo info; DnsSrpAnycastInfo maxNumericalSeqNumInfo; @@ -217,7 +234,7 @@ Error Manager::FindPreferredDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) const // comparison, while `maxNumericalSeqNumInfo` tracks the largest // using normal numerical comparison. - while (GetNextDnsSrpAnycastInfo(iterator, info) == kErrorNone) + while (iterator.GetNextDnsSrpAnycastInfo(info) == kErrorNone) { if (error == kErrorNotFound) { @@ -246,7 +263,7 @@ Error Manager::FindPreferredDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) const iterator.Reset(); - while (GetNextDnsSrpAnycastInfo(iterator, info) == kErrorNone) + while (iterator.GetNextDnsSrpAnycastInfo(info) == kErrorNone) { constexpr uint8_t kMidValue = (NumericLimits::kMax / 2) + 1; uint8_t seqNumber = info.mSequenceNumber; @@ -272,7 +289,7 @@ Error Manager::FindPreferredDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) const iterator.Reset(); - while (GetNextDnsSrpAnycastInfo(iterator, info) == kErrorNone) + while (iterator.GetNextDnsSrpAnycastInfo(info) == kErrorNone) { if (info.mSequenceNumber == aInfo.mSequenceNumber) { @@ -299,10 +316,10 @@ exit: return error; } -Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo) const +Error Iterator::GetNextDnsSrpUnicastInfo(DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo) { Error error = kErrorNone; - uint8_t serviceNumber = kDnsSrpUnicastServiceNumber; + uint8_t serviceNumber = Manager::kDnsSrpUnicastServiceNumber; do { @@ -310,13 +327,13 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicastType a // Process Server sub-TLVs in the current Service TLV. - while (IterateToNextServer(aIterator) == kErrorNone) + while (AdvanceToNextServer() == kErrorNone) { - aInfo.mRloc16 = aIterator.mServerSubTlv->GetServer16(); + aInfo.mRloc16 = mServerSubTlv->GetServer16(); if (aType == kAddrInServiceData) { - if (DnsSrpUnicast::ServiceData::ParseFrom(*aIterator.mServiceTlv, aInfo) == kErrorNone) + if (Manager::DnsSrpUnicast::ServiceData::ParseFrom(*mServiceTlv, aInfo) == kErrorNone) { ExitNow(); } @@ -335,20 +352,20 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicastType a // (then we parse and return the info), or it can be // empty (then we skip over it). - if (DnsSrpUnicast::ServerData::ParseFrom(*aIterator.mServerSubTlv, aInfo) == kErrorNone) + if (Manager::DnsSrpUnicast::ServerData::ParseFrom(*mServerSubTlv, aInfo) == kErrorNone) { ExitNow(); } - if (aIterator.mServerSubTlv->GetServerDataLength() == sizeof(uint16_t)) + if (mServerSubTlv->GetServerDataLength() == sizeof(uint16_t)) { // Handle the case where the server TLV data only // contains a port number and use the RLOC as the // IPv6 address. aInfo.mSockAddr.GetAddress().SetToRoutingLocator(Get().GetMeshLocalPrefix(), - aIterator.mServerSubTlv->GetServer16()); - aInfo.mSockAddr.SetPort(BigEndian::ReadUint16(aIterator.mServerSubTlv->GetServerData())); + mServerSubTlv->GetServer16()); + aInfo.mSockAddr.SetPort(BigEndian::ReadUint16(mServerSubTlv->GetServerData())); aInfo.mVersion = 0; ExitNow(); } @@ -357,14 +374,13 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicastType a // Find the next matching Service TLV. serviceData.InitFrom(serviceNumber); - aIterator.mServiceTlv = - Get().FindNextThreadService(aIterator.mServiceTlv, serviceData, NetworkData::kServicePrefixMatch); - aIterator.mServerSubTlv = nullptr; + mServiceTlv = mNetworkData.FindNextThreadService(mServiceTlv, serviceData, NetworkData::kServicePrefixMatch); + mServerSubTlv = nullptr; // If we have a valid Service TLV, restart the loop // to process its Server sub-TLVs. - } while (aIterator.mServiceTlv != nullptr); + } while (mServiceTlv != nullptr); error = kErrorNotFound; @@ -372,18 +388,20 @@ exit: return error; } -Error Manager::IterateToNextServer(Iterator &aIterator) const +Error Iterator::AdvanceToNextServer(void) { - Error error = kErrorNotFound; + Error error = kErrorNotFound; + const NetworkDataTlv *start; + const NetworkDataTlv *end; - VerifyOrExit(aIterator.mServiceTlv != nullptr); + VerifyOrExit(mServiceTlv != nullptr); - aIterator.mServerSubTlv = NetworkDataTlv::Find( - /* aStart */ (aIterator.mServerSubTlv != nullptr) ? aIterator.mServerSubTlv->GetNext() - : aIterator.mServiceTlv->GetSubTlvs(), - /* aEnd */ aIterator.mServiceTlv->GetNext()); + start = (mServerSubTlv != nullptr) ? mServerSubTlv->GetNext() : mServiceTlv->GetSubTlvs(); + end = mServiceTlv->GetNext(); - if (aIterator.mServerSubTlv != nullptr) + mServerSubTlv = NetworkDataTlv::Find(start, end); + + if (mServerSubTlv != nullptr) { error = kErrorNone; } diff --git a/src/core/thread/network_data_service.hpp b/src/core/thread/network_data_service.hpp index 7bfc324d2..901911090 100644 --- a/src/core/thread/network_data_service.hpp +++ b/src/core/thread/network_data_service.hpp @@ -82,43 +82,79 @@ struct DnsSrpUnicastInfo uint16_t mRloc16; ///< The BR RLOC16 adding the entry. }; +class Manager; + +/** + * Represents an iterator to iterate over service entries in a Network Data. + */ +class Iterator : public InstanceLocator, private NonCopyable +{ + friend class Manager; + +public: + /** + * Initializes the `Iterator` for iterating over service entries in the Leader Network Data + * + * @param[in] aInstance The OpenThread instance. + */ + explicit Iterator(Instance &aInstance); + + /** + * Initializes the `Iterator` for iterating over service entries in a given Network Data. + * + * @param[in] aInstance The OpenThread instance. + * @param[in] aNetworkData The `NetworkData` to use with this iterator. + */ + Iterator(Instance &aInstance, const NetworkData &aNetworkData); + + /** + * Resets the `Iterator` to start over. + */ + void Reset(void); + + /** + * Gets the next DNS/SRP info from the Thread Network Data "DNS/SRP Service Anycast Address" entries. + * + * To start from the first entry, ensure the iterator is reset (e.g., by creating a new `Iterator` instance, or by + * calling `Reset()`). + * + * @param[out] aInfo A reference to `DnsSrpAnycastInfo` to return the info. + * + * @retval kErrorNone Successfully got the next info. @p aInfo is updated. + * @retval kErrorNotFound No more matching entries in the Network Data. + */ + Error GetNextDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo); + + /** + * Gets the next DNS/SRP info from the Thread Network Data "DNS/SRP Service Unicast Address" entries. + * + * To start from the first entry, ensure the iterator is reset (e.g., by creating a new `Iterator` instance, or by + * calling `Reset()`). + * + * @param[in] aType The entry type, `kAddrInServiceData` or `kAddrInServerData` + * @param[out] aInfo A reference to `DnsSrpUnicastInfo` to return the info. + * + * @retval kErrorNone Successfully got the next info. @p aInfo is updated. + * @retval kErrorNotFound No more matching entries in the Network Data. + */ + Error GetNextDnsSrpUnicastInfo(DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo); + +private: + Error AdvanceToNextServer(void); + + const NetworkData &mNetworkData; + const ServiceTlv *mServiceTlv; + const ServerTlv *mServerSubTlv; +}; + /** * Manages the Thread Service entries in Thread Network Data. */ class Manager : public InstanceLocator, private NonCopyable { + friend class Iterator; + public: - /** - * Represents an iterator used to iterate through Network Data Service entries. - */ - class Iterator : public Clearable - { - friend class Manager; - - public: - /** - * Initializes the iterator (as empty/clear). - */ - Iterator(void) - : mServiceTlv(nullptr) - , mServerSubTlv(nullptr) - { - } - - /** - * Resets the iterator to start from beginning. - */ - void Reset(void) - { - mServiceTlv = nullptr; - mServerSubTlv = nullptr; - } - - private: - const ServiceTlv *mServiceTlv; - const ServerTlv *mServerSubTlv; - }; - /** * Initializes the `Manager` object. * @@ -259,20 +295,6 @@ public: } #endif - /** - * Gets the next DNS/SRP info from the Thread Network Data "DNS/SRP Service Anycast Address" entries. - * - * To get the first entry, @p aIterator should be cleared (e.g., a new instance of `Iterator` or calling `Clear()` - * method). - * - * @param[in,out] aIterator A reference to an iterator. - * @param[out] aInfo A reference to `DnsSrpAnycastInfo` to return the info. - * - * @retval kErrorNone Successfully got the next info. @p aInfo and @p aIterator are updated. - * @retval kErrorNotFound No more matching entries in the Network Data. - */ - Error GetNextDnsSrpAnycastInfo(Iterator &aIterator, DnsSrpAnycastInfo &aInfo) const; - /** * Finds the preferred DNS/SRP info among all the Thread Network Data "DNS/SRP Service Anycast Address" * entries. @@ -290,21 +312,6 @@ public: */ Error FindPreferredDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) const; - /** - * Gets the next DNS/SRP info from the Thread Network Data "DNS/SRP Service Unicast Address" entries. - * - * To get the first entry @p aIterator should be cleared (e.g., a new instance of `Iterator` or calling `Clear()` - * method). - * - * @param[in,out] aIterator A reference to an iterator. - * @param[in] aType The entry type, `kAddrInServiceData` or `kAddrInServerData` - * @param[out] aInfo A reference to `DnsSrpUnicastInfo` to return the info. - * - * @retval kErrorNone Successfully got the next info. @p aInfo and @p aIterator are updated. - * @retval kErrorNotFound No more matching entries in the Network Data. - */ - Error GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo) const; - private: static constexpr uint8_t kBackboneRouterServiceNumber = 0x01; static constexpr uint8_t kDnsSrpAnycastServiceNumber = 0x5c; @@ -476,7 +483,6 @@ private: #endif Error GetServiceId(uint8_t aServiceNumber, uint8_t &aServiceId) const; - Error IterateToNextServer(Iterator &aIterator) const; #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) bool IsBackboneRouterPreferredTo(const ServerTlv &aServerTlv, diff --git a/tests/unit/test_network_data.cpp b/tests/unit/test_network_data.cpp index 87f4e72e2..e67a147ce 100644 --- a/tests/unit/test_network_data.cpp +++ b/tests/unit/test_network_data.cpp @@ -735,7 +735,7 @@ void TestNetworkDataDsnSrpServices(void) const uint8_t kPreferredAnycastEntryIndex = 2; Service::Manager &manager = instance->Get(); - Service::Manager::Iterator iterator; + Service::Iterator iterator(*instance); Service::DnsSrpAnycastInfo anycastInfo; Service::DnsSrpUnicastInfo unicastInfo; Service::DnsSrpUnicastType type; @@ -766,7 +766,7 @@ void TestNetworkDataDsnSrpServices(void) for (const AnycastEntry &entry : kAnycastEntries) { - SuccessOrQuit(manager.GetNextDnsSrpAnycastInfo(iterator, anycastInfo)); + SuccessOrQuit(iterator.GetNextDnsSrpAnycastInfo(anycastInfo)); printf("\nanycastInfo { %s, seq:%d, rlco16:%04x, version:%u }", anycastInfo.mAnycastAddress.ToString().AsCString(), anycastInfo.mSequenceNumber, anycastInfo.mRloc16, @@ -775,7 +775,7 @@ void TestNetworkDataDsnSrpServices(void) VerifyOrQuit(entry.Matches(anycastInfo), "GetNextDnsSrpAnycastInfo() returned incorrect info"); } - VerifyOrQuit(manager.GetNextDnsSrpAnycastInfo(iterator, anycastInfo) == kErrorNotFound, + VerifyOrQuit(iterator.GetNextDnsSrpAnycastInfo(anycastInfo) == kErrorNotFound, "GetNextDnsSrpAnycastInfo() returned unexpected extra entry"); // Find the preferred "DNS/SRP Anycast Service" entries in Network Data @@ -791,37 +791,37 @@ void TestNetworkDataDsnSrpServices(void) printf("\n\n- - - - - - - - - - - - - - - - - - - -"); printf("\nDNS/SRP Unicast Service entries (server data)\n"); - iterator.Clear(); + iterator.Reset(); type = Service::kAddrInServerData; for (const UnicastEntry &entry : kUnicastEntriesFromServerData) { - SuccessOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo)); + SuccessOrQuit(iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo)); printf("\nunicastInfo { %s, rloc16:%04x }", unicastInfo.mSockAddr.ToString().AsCString(), unicastInfo.mRloc16); VerifyOrQuit(entry.Matches(unicastInfo), "GetNextDnsSrpUnicastInfo() returned incorrect info"); } - VerifyOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNotFound, + VerifyOrQuit(iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo) == kErrorNotFound, "GetNextDnsSrpUnicastInfo() returned unexpected extra entry"); printf("\n\n- - - - - - - - - - - - - - - - - - - -"); printf("\nDNS/SRP Unicast Service entries (service data)\n"); - iterator.Clear(); + iterator.Reset(); type = Service::kAddrInServiceData; for (const UnicastEntry &entry : kUnicastEntriesFromServiceData) { - SuccessOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo)); + SuccessOrQuit(iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo)); printf("\nunicastInfo { %s, rloc16:%04x }", unicastInfo.mSockAddr.ToString().AsCString(), unicastInfo.mRloc16); VerifyOrQuit(entry.Matches(unicastInfo), "GetNextDnsSrpUnicastInfo() returned incorrect info"); } - VerifyOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNotFound, + VerifyOrQuit(iterator.GetNextDnsSrpUnicastInfo(type, unicastInfo) == kErrorNotFound, "GetNextDnsSrpUnicastInfo() returned unexpected extra entry"); printf("\n"); @@ -1036,7 +1036,7 @@ void TestNetworkDataDsnSrpAnycastSeqNumSelection(void) for (const TestInfo &test : kTests) { - Service::Manager::Iterator iterator; + Service::Iterator iterator(*instance); Service::DnsSrpAnycastInfo anycastInfo; reinterpret_cast(instance->Get()).Populate(test.mNetworkData, test.mNetworkDataLength); @@ -1046,7 +1046,7 @@ void TestNetworkDataDsnSrpAnycastSeqNumSelection(void) for (uint8_t index = 0; index < test.mSeqNumbersLength; index++) { - SuccessOrQuit(manager.GetNextDnsSrpAnycastInfo(iterator, anycastInfo)); + SuccessOrQuit(iterator.GetNextDnsSrpAnycastInfo(anycastInfo)); printf("\n { %s, seq:%u, version:%u, rlco16:%04x }", anycastInfo.mAnycastAddress.ToString().AsCString(), @@ -1056,7 +1056,7 @@ void TestNetworkDataDsnSrpAnycastSeqNumSelection(void) VerifyOrQuit(anycastInfo.mRloc16 == 0x5000 + index); } - VerifyOrQuit(manager.GetNextDnsSrpAnycastInfo(iterator, anycastInfo) == kErrorNotFound); + VerifyOrQuit(iterator.GetNextDnsSrpAnycastInfo(anycastInfo) == kErrorNotFound); SuccessOrQuit(manager.FindPreferredDnsSrpAnycastInfo(anycastInfo)); printf("\n preferred -> seq:%u, version:%u ", anycastInfo.mSequenceNumber, anycastInfo.mVersion);