diff --git a/src/core/thread/network_data_service.cpp b/src/core/thread/network_data_service.cpp index 53ceba22b..cd8c78981 100644 --- a/src/core/thread/network_data_service.cpp +++ b/src/core/thread/network_data_service.cpp @@ -39,6 +39,9 @@ namespace ot { namespace NetworkData { namespace Service { +//---------------------------------------------------------------------------------------------------------------------- +// Iterator + Iterator::Iterator(Instance &aInstance) : Iterator(aInstance, aInstance.Get()) { @@ -58,6 +61,153 @@ void Iterator::Reset(void) mServerSubTlv = nullptr; } +Error Iterator::GetNextDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) +{ + Error error = kErrorNone; + uint8_t serviceNumber = Manager::kDnsSrpAnycastServiceNumber; + + aInfo.Clear(); + + do + { + ServiceData serviceData; + + // Process the next Server sub-TLV in the current Service TLV. + + if (AdvanceToNextServer() == kErrorNone) + { + uint8_t dataLength = mServiceTlv->GetServiceDataLength(); + + if (dataLength >= sizeof(Manager::DnsSrpAnycastServiceData)) + { + const Manager::DnsSrpAnycastServiceData *anycastData = + reinterpret_cast(mServiceTlv->GetServiceData()); + + Get().GetServiceAloc(mServiceTlv->GetServiceId(), aInfo.mAnycastAddress); + aInfo.mSequenceNumber = anycastData->GetSequenceNumber(); + aInfo.mRloc16 = mServerSubTlv->GetServer16(); + aInfo.mVersion = + (mServerSubTlv->GetServerDataLength() >= sizeof(uint8_t)) ? *mServerSubTlv->GetServerData() : 0; + ExitNow(); + } + } + + // Find the next matching Service TLV. + + serviceData.InitFrom(serviceNumber); + 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 (mServiceTlv != nullptr); + + error = kErrorNotFound; + +exit: + return error; +} + +Error Iterator::GetNextDnsSrpUnicastInfo(DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo) +{ + Error error = kErrorNone; + uint8_t serviceNumber = Manager::kDnsSrpUnicastServiceNumber; + + aInfo.Clear(); + + do + { + ServiceData serviceData; + + // Process Server sub-TLVs in the current Service TLV. + + while (AdvanceToNextServer() == kErrorNone) + { + aInfo.mRloc16 = mServerSubTlv->GetServer16(); + + if (aType == kAddrInServiceData) + { + if (Manager::DnsSrpUnicast::ServiceData::ParseFrom(*mServiceTlv, aInfo) == kErrorNone) + { + ExitNow(); + } + + // If Service Data does not contain address info, we + // break from `while (IterateToNextServer())` loop + // to skip over the entire Service TLV and all its + // sub-TLVs and go to the next one. + + break; + } + + // `aType` is `kAddrInServerData`. + + // Server sub-TLV can contain address and port info + // (then we parse and return the info), or it can be + // empty (then we skip over it). + + if (Manager::DnsSrpUnicast::ServerData::ParseFrom(*mServerSubTlv, aInfo) == kErrorNone) + { + ExitNow(); + } + + 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(), + mServerSubTlv->GetServer16()); + aInfo.mSockAddr.SetPort(BigEndian::ReadUint16(mServerSubTlv->GetServerData())); + aInfo.mVersion = 0; + ExitNow(); + } + } + + // Find the next matching Service TLV. + + serviceData.InitFrom(serviceNumber); + 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 (mServiceTlv != nullptr); + + error = kErrorNotFound; + +exit: + return error; +} + +Error Iterator::AdvanceToNextServer(void) +{ + Error error = kErrorNotFound; + const NetworkDataTlv *start; + const NetworkDataTlv *end; + + VerifyOrExit(mServiceTlv != nullptr); + + start = (mServerSubTlv != nullptr) ? mServerSubTlv->GetNext() : mServiceTlv->GetSubTlvs(); + end = mServiceTlv->GetNext(); + + mServerSubTlv = NetworkDataTlv::Find(start, end); + + if (mServerSubTlv != nullptr) + { + error = kErrorNone; + } + +exit: + return error; +} + +//---------------------------------------------------------------------------------------------------------------------- +// Manager + #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE Error Manager::AddDnsSrpAnycastService(uint8_t aSequenceNumber, uint8_t aVersion) @@ -176,54 +326,6 @@ exit: #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) -Error Iterator::GetNextDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) -{ - Error error = kErrorNone; - uint8_t serviceNumber = Manager::kDnsSrpAnycastServiceNumber; - - aInfo.Clear(); - - do - { - ServiceData serviceData; - - // Process the next Server sub-TLV in the current Service TLV. - - if (AdvanceToNextServer() == kErrorNone) - { - uint8_t dataLength = mServiceTlv->GetServiceDataLength(); - - if (dataLength >= sizeof(Manager::DnsSrpAnycastServiceData)) - { - const Manager::DnsSrpAnycastServiceData *anycastData = - reinterpret_cast(mServiceTlv->GetServiceData()); - - Get().GetServiceAloc(mServiceTlv->GetServiceId(), aInfo.mAnycastAddress); - aInfo.mSequenceNumber = anycastData->GetSequenceNumber(); - aInfo.mRloc16 = mServerSubTlv->GetServer16(); - aInfo.mVersion = - (mServerSubTlv->GetServerDataLength() >= sizeof(uint8_t)) ? *mServerSubTlv->GetServerData() : 0; - ExitNow(); - } - } - - // Find the next matching Service TLV. - - serviceData.InitFrom(serviceNumber); - 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 (mServiceTlv != nullptr); - - error = kErrorNotFound; - -exit: - return error; -} - Error Manager::FindPreferredDnsSrpAnycastInfo(DnsSrpAnycastInfo &aInfo) const { Error error = kErrorNotFound; @@ -305,6 +407,9 @@ exit: return error; } +//---------------------------------------------------------------------------------------------------------------------- +// Manager::DnsSrpUnicast::AddrData + Error Manager::DnsSrpUnicast::AddrData::ParseFrom(const uint8_t *aData, uint8_t aLength, DnsSrpUnicastInfo &aInfo) { Error error = kErrorNone; @@ -320,102 +425,6 @@ exit: return error; } -Error Iterator::GetNextDnsSrpUnicastInfo(DnsSrpUnicastType aType, DnsSrpUnicastInfo &aInfo) -{ - Error error = kErrorNone; - uint8_t serviceNumber = Manager::kDnsSrpUnicastServiceNumber; - - aInfo.Clear(); - - do - { - ServiceData serviceData; - - // Process Server sub-TLVs in the current Service TLV. - - while (AdvanceToNextServer() == kErrorNone) - { - aInfo.mRloc16 = mServerSubTlv->GetServer16(); - - if (aType == kAddrInServiceData) - { - if (Manager::DnsSrpUnicast::ServiceData::ParseFrom(*mServiceTlv, aInfo) == kErrorNone) - { - ExitNow(); - } - - // If Service Data does not contain address info, we - // break from `while (IterateToNextServer())` loop - // to skip over the entire Service TLV and all its - // sub-TLVs and go to the next one. - - break; - } - - // `aType` is `kAddrInServerData`. - - // Server sub-TLV can contain address and port info - // (then we parse and return the info), or it can be - // empty (then we skip over it). - - if (Manager::DnsSrpUnicast::ServerData::ParseFrom(*mServerSubTlv, aInfo) == kErrorNone) - { - ExitNow(); - } - - 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(), - mServerSubTlv->GetServer16()); - aInfo.mSockAddr.SetPort(BigEndian::ReadUint16(mServerSubTlv->GetServerData())); - aInfo.mVersion = 0; - ExitNow(); - } - } - - // Find the next matching Service TLV. - - serviceData.InitFrom(serviceNumber); - 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 (mServiceTlv != nullptr); - - error = kErrorNotFound; - -exit: - return error; -} - -Error Iterator::AdvanceToNextServer(void) -{ - Error error = kErrorNotFound; - const NetworkDataTlv *start; - const NetworkDataTlv *end; - - VerifyOrExit(mServiceTlv != nullptr); - - start = (mServerSubTlv != nullptr) ? mServerSubTlv->GetNext() : mServiceTlv->GetSubTlvs(); - end = mServiceTlv->GetNext(); - - mServerSubTlv = NetworkDataTlv::Find(start, end); - - if (mServerSubTlv != nullptr) - { - error = kErrorNone; - } - -exit: - return error; -} - } // namespace Service } // namespace NetworkData } // namespace ot diff --git a/src/core/thread/network_data_service.hpp b/src/core/thread/network_data_service.hpp index beca23e0c..9580941cb 100644 --- a/src/core/thread/network_data_service.hpp +++ b/src/core/thread/network_data_service.hpp @@ -319,6 +319,8 @@ private: static constexpr uint8_t kDnsSrpAnycastServiceNumber = 0x5c; static constexpr uint8_t kDnsSrpUnicastServiceNumber = 0x5d; + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + OT_TOOL_PACKED_BEGIN class DnsSrpAnycastServiceData { @@ -338,6 +340,8 @@ private: uint8_t mSequenceNumber; } OT_TOOL_PACKED_END; + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + class DnsSrpUnicast { public: @@ -428,6 +432,8 @@ private: DnsSrpUnicast(void) = delete; }; + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) OT_TOOL_PACKED_BEGIN class BbrServerData @@ -452,7 +458,10 @@ private: } OT_TOOL_PACKED_END; #endif + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE + template Error AddService(const ServiceDataType &aServiceData) { return AddService(&aServiceData, aServiceData.GetLength(), nullptr, 0); @@ -482,7 +491,8 @@ private: Error RemoveService(uint8_t aServiceNumber) { return RemoveService(&aServiceNumber, sizeof(uint8_t)); } Error RemoveService(const void *aServiceData, uint8_t aServiceDataLength); -#endif + +#endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE Error GetServiceId(uint8_t aServiceNumber, uint8_t &aServiceId) const;