[netdata] update GetNextDnsSrpUnicastInfo() to use Type (#10616)

This commit updates `Service::Manager::GetNextDnsSrpUnicastInfo()` to
accept a `DnsSrpUnicast::Type`, indicating the desired entry type
(either `kFromServiceData` or `kFromServerData`). This simplifies the
code, which previously iterated over all types and performed type
checks. Additionally, this change simplifies the `Publisher` methods
used for counting existing DNS/SRP unicast entries of different
types.
This commit is contained in:
Abtin Keshavarzian
2024-08-16 10:27:14 -07:00
committed by GitHub
parent 6209b0b855
commit d0895415da
8 changed files with 117 additions and 151 deletions
+8 -18
View File
@@ -2378,7 +2378,7 @@ exit:
return;
}
Error Client::SelectUnicastEntry(DnsSrpUnicast::Origin aOrigin, DnsSrpUnicast::Info &aInfo) const
Error Client::SelectUnicastEntry(DnsSrpUnicast::Type aType, DnsSrpUnicast::Info &aInfo) const
{
Error error = kErrorNotFound;
DnsSrpUnicast::Info unicastInfo;
@@ -2393,13 +2393,8 @@ Error Client::SelectUnicastEntry(DnsSrpUnicast::Origin aOrigin, DnsSrpUnicast::I
}
#endif
while (Get<NetworkData::Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, unicastInfo) == kErrorNone)
while (Get<NetworkData::Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, aType, unicastInfo) == kErrorNone)
{
if (unicastInfo.mOrigin != aOrigin)
{
continue;
}
if (mAutoStart.HasSelectedServer() && (GetServerAddress() == unicastInfo.mSockAddr))
{
aInfo = unicastInfo;
@@ -2441,9 +2436,9 @@ void Client::SelectNextServer(bool aDisallowSwitchOnRegisteredHost)
// restarts the client with the new server (keeping the retry wait
// interval as before).
Ip6::SockAddr serverSockAddr;
bool selectNext = false;
DnsSrpUnicast::Origin origin = DnsSrpUnicast::kFromServiceData;
Ip6::SockAddr serverSockAddr;
bool selectNext = false;
DnsSrpUnicast::Type type = DnsSrpUnicast::kFromServiceData;
serverSockAddr.Clear();
@@ -2455,11 +2450,11 @@ void Client::SelectNextServer(bool aDisallowSwitchOnRegisteredHost)
switch (mAutoStart.GetState())
{
case AutoStart::kSelectedUnicastPreferred:
origin = DnsSrpUnicast::kFromServiceData;
type = DnsSrpUnicast::kFromServiceData;
break;
case AutoStart::kSelectedUnicast:
origin = DnsSrpUnicast::kFromServerData;
type = DnsSrpUnicast::kFromServerData;
break;
case AutoStart::kSelectedAnycast:
@@ -2485,13 +2480,8 @@ void Client::SelectNextServer(bool aDisallowSwitchOnRegisteredHost)
DnsSrpUnicast::Info unicastInfo;
NetworkData::Service::Manager::Iterator iterator;
while (Get<NetworkData::Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, unicastInfo) == kErrorNone)
while (Get<NetworkData::Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNone)
{
if (unicastInfo.mOrigin != origin)
{
continue;
}
if (selectNext)
{
serverSockAddr = unicastInfo.mSockAddr;
+1 -1
View File
@@ -1110,7 +1110,7 @@ private:
#if OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE
void ApplyAutoStartGuardOnAttach(void);
void ProcessAutoStart(void);
Error SelectUnicastEntry(DnsSrpUnicast::Origin aOrigin, DnsSrpUnicast::Info &aInfo) const;
Error SelectUnicastEntry(DnsSrpUnicast::Type aType, DnsSrpUnicast::Info &aInfo) const;
void HandleGuardTimer(void) {}
#if OPENTHREAD_CONFIG_SRP_CLIENT_SWITCH_SERVER_ON_FAILURE
void SelectNextServer(bool aDisallowSwitchOnRegisteredHost);
+2 -6
View File
@@ -602,16 +602,12 @@ Error AddressResolver::ResolveUsingNetDataServices(const Ip6::Address &aEid, uin
Error error = kErrorNotFound;
NetworkData::Service::Manager::Iterator iterator;
NetworkData::Service::DnsSrpUnicast::Info unicastInfo;
NetworkData::Service::DnsSrpUnicast::Type type = NetworkData::Service::DnsSrpUnicast::kFromServerData;
VerifyOrExit(Get<Mle::Mle>().GetDeviceMode().GetNetworkDataType() == NetworkData::kFullSet);
while (Get<NetworkData::Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, unicastInfo) == kErrorNone)
while (Get<NetworkData::Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNone)
{
if (unicastInfo.mOrigin != NetworkData::Service::DnsSrpUnicast::kFromServerData)
{
continue;
}
if (aEid == unicastInfo.mSockAddr.GetAddress())
{
aRloc16 = unicastInfo.mRloc16;
+19 -68
View File
@@ -44,7 +44,6 @@
#include "common/random.hpp"
#include "instance/instance.hpp"
#include "thread/network_data_local.hpp"
#include "thread/network_data_service.hpp"
namespace ot {
namespace NetworkData {
@@ -661,12 +660,12 @@ void Publisher::DnsSrpServiceEntry::Process(void)
case kTypeUnicastMeshLocalEid:
{
Service::DnsSrpAnycast::Info anycastInfo;
bool hasServiceDataEntry;
CountServerDataUnicastEntries(numEntries, numPreferredEntries, hasServiceDataEntry);
CountUnicastEntries(Service::DnsSrpUnicast::kFromServerData, numEntries, numPreferredEntries);
desiredNumEntries = kDesiredNumUnicast;
if (hasServiceDataEntry || (Get<Service::Manager>().FindPreferredDnsSrpAnycastInfo(anycastInfo) == kErrorNone))
if (HasAnyServiceDataUnicastEntry() ||
(Get<Service::Manager>().FindPreferredDnsSrpAnycastInfo(anycastInfo) == kErrorNone))
{
// If there is any service data unicast entry or anycast
// entry, we set the desired number of server data
@@ -681,7 +680,7 @@ void Publisher::DnsSrpServiceEntry::Process(void)
case kTypeUnicast:
desiredNumEntries = kDesiredNumUnicast;
CountServiceDataUnicastEntries(numEntries, numPreferredEntries);
CountUnicastEntries(Service::DnsSrpUnicast::kFromServiceData, numEntries, numPreferredEntries);
break;
}
@@ -722,81 +721,33 @@ void Publisher::DnsSrpServiceEntry::CountAnycastEntries(uint8_t &aNumEntries, ui
}
}
void Publisher::DnsSrpServiceEntry::CountServerDataUnicastEntries(uint8_t &aNumEntries,
uint8_t &aNumPreferredEntries,
bool &aHasServiceDataEntry) const
void Publisher::DnsSrpServiceEntry::CountUnicastEntries(Service::DnsSrpUnicast::Type aType,
uint8_t &aNumEntries,
uint8_t &aNumPreferredEntries) const
{
// Count the number of server data DNS/SRP unicast entries in the
// Network Data. Also determine whether there is any service data
// DNS/SRP unicast entry (update `aHasServiceDataEntry`).
// Count the number of DNS/SRP unicast entries in the Network Data.
const ServiceTlv *serviceTlv = nullptr;
ServiceData data;
Service::Manager::Iterator iterator;
Service::DnsSrpUnicast::Info unicastInfo;
aHasServiceDataEntry = false;
data.InitFrom(Service::DnsSrpUnicast::kServiceData);
while ((serviceTlv = Get<Leader>().FindNextThreadService(serviceTlv, data, NetworkData::kServicePrefixMatch)) !=
nullptr)
while (Get<Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, aType, unicastInfo) == kErrorNone)
{
TlvIterator subTlvIterator(*serviceTlv);
const ServerTlv *serverSubTlv;
aNumEntries++;
if (serviceTlv->GetServiceDataLength() >= sizeof(Service::DnsSrpUnicast::ServiceData))
if (IsPreferred(unicastInfo.mRloc16))
{
aHasServiceDataEntry = true;
}
while (((serverSubTlv = subTlvIterator.Iterate<ServerTlv>())) != nullptr)
{
if (serverSubTlv->GetServerDataLength() < sizeof(Service::DnsSrpUnicast::ServerData))
{
continue;
}
aNumEntries++;
if (IsPreferred(serverSubTlv->GetServer16()))
{
aNumPreferredEntries++;
}
aNumPreferredEntries++;
}
}
}
void Publisher::DnsSrpServiceEntry::CountServiceDataUnicastEntries(uint8_t &aNumEntries,
uint8_t &aNumPreferredEntries) const
bool Publisher::DnsSrpServiceEntry::HasAnyServiceDataUnicastEntry(void) const
{
// Count the number of service data DNS/SRP unicast entries in
// the Network Data.
Service::Manager::Iterator iterator;
Service::DnsSrpUnicast::Info unicastInfo;
Service::DnsSrpUnicast::Type type = Service::DnsSrpUnicast::kFromServiceData;
const ServiceTlv *serviceTlv = nullptr;
ServiceData data;
data.InitFrom(Service::DnsSrpUnicast::kServiceData);
while ((serviceTlv = Get<Leader>().FindNextThreadService(serviceTlv, data, NetworkData::kServicePrefixMatch)) !=
nullptr)
{
TlvIterator subTlvIterator(*serviceTlv);
const ServerTlv *serverSubTlv;
if (serviceTlv->GetServiceDataLength() < sizeof(Service::DnsSrpUnicast::ServiceData))
{
continue;
}
while (((serverSubTlv = subTlvIterator.Iterate<ServerTlv>())) != nullptr)
{
aNumEntries++;
if (IsPreferred(serverSubTlv->GetServer16()))
{
aNumPreferredEntries++;
}
}
}
return (Get<Service::Manager>().GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNone);
}
//---------------------------------------------------------------------------------------------------------------------
+5 -4
View File
@@ -56,6 +56,7 @@
#include "common/string.hpp"
#include "common/timer.hpp"
#include "net/ip6_address.hpp"
#include "thread/network_data_service.hpp"
#include "thread/network_data_types.hpp"
namespace ot {
@@ -437,10 +438,10 @@ private:
void Notify(Event aEvent) const;
void Process(void);
void CountAnycastEntries(uint8_t &aNumEntries, uint8_t &aNumPreferredEntries) const;
void CountServiceDataUnicastEntries(uint8_t &aNumEntries, uint8_t &aNumPreferredEntries) const;
void CountServerDataUnicastEntries(uint8_t &aNumEntries,
uint8_t &aNumPreferredEntries,
bool &aHasServiceDataEntry) const;
void CountUnicastEntries(Service::DnsSrpUnicast::Type aType,
uint8_t &aNumEntries,
uint8_t &aNumPreferredEntries) const;
bool HasAnyServiceDataUnicastEntry(void) const;
Info mInfo;
Callback<DnsSrpServiceCallback> mCallback;
+35 -26
View File
@@ -260,21 +260,44 @@ exit:
return error;
}
Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info &aInfo) const
Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator,
DnsSrpUnicast::Type aType,
DnsSrpUnicast::Info &aInfo) const
{
Error error = kErrorNone;
ServiceData serviceData;
Error error = kErrorNone;
serviceData.InitFrom(DnsSrpUnicast::kServiceData);
while (true)
do
{
ServiceData serviceData;
// Process Server sub-TLVs in the current Service TLV.
while (IterateToNextServer(aIterator) == kErrorNone)
{
ServerData data;
if (aType == DnsSrpUnicast::kFromServiceData)
{
const DnsSrpUnicast::ServiceData *dnsServiceData;
if (aIterator.mServiceTlv->GetServiceDataLength() < sizeof(DnsSrpUnicast::ServiceData))
{
// Break from `while(IterateToNextServer())` loop
// to skip over the Service TLV and all its
// sub-TLVs and go to the next one.
break;
}
aIterator.mServiceTlv->GetServiceData(serviceData);
dnsServiceData = reinterpret_cast<const DnsSrpUnicast::ServiceData *>(serviceData.GetBytes());
aInfo.mSockAddr.SetAddress(dnsServiceData->GetAddress());
aInfo.mSockAddr.SetPort(dnsServiceData->GetPort());
aInfo.mRloc16 = aIterator.mServerSubTlv->GetServer16();
ExitNow();
}
// `aType` is `kFromServerData`.
// 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).
@@ -288,7 +311,6 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info
aInfo.mSockAddr.SetAddress(serverData->GetAddress());
aInfo.mSockAddr.SetPort(serverData->GetPort());
aInfo.mOrigin = DnsSrpUnicast::kFromServerData;
aInfo.mRloc16 = aIterator.mServerSubTlv->GetServer16();
ExitNow();
}
@@ -301,7 +323,6 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info
aInfo.mSockAddr.GetAddress().SetToRoutingLocator(Get<Mle::Mle>().GetMeshLocalPrefix(),
aIterator.mServerSubTlv->GetServer16());
aInfo.mSockAddr.SetPort(BigEndian::ReadUint16(data.GetBytes()));
aInfo.mOrigin = DnsSrpUnicast::kFromServerData;
aInfo.mRloc16 = aIterator.mServerSubTlv->GetServer16();
ExitNow();
}
@@ -309,29 +330,17 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info
// Find the next matching Service TLV.
serviceData.InitFrom(DnsSrpUnicast::kServiceData);
aIterator.mServiceTlv =
Get<Leader>().FindNextThreadService(aIterator.mServiceTlv, serviceData, NetworkData::kServicePrefixMatch);
aIterator.mServerSubTlv = nullptr;
VerifyOrExit(aIterator.mServiceTlv != nullptr, error = kErrorNotFound);
// If we have a valid Service TLV, restart the loop
// to process its Server sub-TLVs.
if (aIterator.mServiceTlv->GetServiceDataLength() >= sizeof(DnsSrpUnicast::ServiceData))
{
// The Service TLV data contains the address and port info.
} while (aIterator.mServiceTlv != nullptr);
const DnsSrpUnicast::ServiceData *dnsServiceData;
aIterator.mServiceTlv->GetServiceData(serviceData);
dnsServiceData = reinterpret_cast<const DnsSrpUnicast::ServiceData *>(serviceData.GetBytes());
aInfo.mSockAddr.SetAddress(dnsServiceData->GetAddress());
aInfo.mSockAddr.SetPort(dnsServiceData->GetPort());
aInfo.mOrigin = DnsSrpUnicast::kFromServiceData;
aInfo.mRloc16 = Mle::kInvalidRloc16;
ExitNow();
}
// Go back to the start of `while (true)` loop to
// process the Server sub-TLVs in the new Service TLV.
}
error = kErrorNotFound;
exit:
return error;
+5 -5
View File
@@ -238,10 +238,10 @@ public:
static const uint8_t kServiceData = kServiceNumber;
/**
* Represents the origin a `DnsSrpUnicast` entry.
* Represents the `DnsSrpUnicast` entry type.
*
*/
enum Origin : uint8_t
enum Type : uint8_t
{
kFromServiceData, ///< Socket address is from service data.
kFromServerData, ///< Socket address is from server data.
@@ -254,8 +254,7 @@ public:
struct Info
{
Ip6::SockAddr mSockAddr; ///< The socket address (IPv6 address and port) of the DNS/SRP server.
Origin mOrigin; ///< The origin of the socket address (whether from service or server data).
uint16_t mRloc16; ///< The BR RLOC16 adding the entry (only used when `mOrigin == kFromServerData`).
uint16_t mRloc16; ///< The BR RLOC16 adding the entry.
};
/**
@@ -581,13 +580,14 @@ public:
* method).
*
* @param[in,out] aIterator A reference to an iterator.
* @param[in] aType The entry type, `kFromServiceData` (preferred) or `kFromServerData` (non-preferred).
* @param[out] aInfo A reference to `DnsSrpUnicast::Info` 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, DnsSrpUnicast::Info &aInfo) const;
Error GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Type aType, DnsSrpUnicast::Info &aInfo) const;
private:
#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
+42 -23
View File
@@ -612,11 +612,6 @@ void TestNetworkDataFindNextService(void)
void TestNetworkDataDsnSrpServices(void)
{
static const char *kOriginStrings[] = {
"service-data", // (0) Service::DnsSrpUnicast::kFromServiceData
"server-data", // (1) Service::DnsSrpUnicast::kFromServerData
};
class TestLeader : public Leader
{
public:
@@ -652,19 +647,18 @@ void TestNetworkDataDsnSrpServices(void)
struct UnicastEntry
{
const char *mAddress;
uint16_t mPort;
Service::DnsSrpUnicast::Origin mOrigin;
uint16_t mRloc16;
const char *mAddress;
uint16_t mPort;
uint16_t mRloc16;
bool Matches(Service::DnsSrpUnicast::Info aInfo) const
bool Matches(const Service::DnsSrpUnicast::Info &aInfo) const
{
Ip6::SockAddr sockAddr;
SuccessOrQuit(sockAddr.GetAddress().FromString(mAddress));
sockAddr.SetPort(mPort);
return (aInfo.mSockAddr == sockAddr) && (aInfo.mOrigin == mOrigin) && (aInfo.mRloc16 == mRloc16);
return (aInfo.mSockAddr == sockAddr) && (aInfo.mRloc16 == mRloc16);
}
};
@@ -684,12 +678,17 @@ void TestNetworkDataDsnSrpServices(void)
{0xfc12, 0x03},
};
const UnicastEntry kUnicastEntries[] = {
{"fdde:ad00:beef:0:2d0e:c627:5556:18d9", 0x1234, Service::DnsSrpUnicast::kFromServiceData, 0xfffe},
{"fd00:aabb:ccdd:eeff:11:2233:4455:6677", 0xabcd, Service::DnsSrpUnicast::kFromServerData, 0x6c00},
{"fdde:ad00:beef:0:0:ff:fe00:2800", 0x5678, Service::DnsSrpUnicast::kFromServerData, 0x2800},
{"fd00:1234:5678:9abc:def0:123:4567:89ab", 0x0e, Service::DnsSrpUnicast::kFromServerData, 0x4c00},
{"fdde:ad00:beef:0:0:ff:fe00:6c00", 0xcd12, Service::DnsSrpUnicast::kFromServerData, 0x6c00},
const UnicastEntry kUnicastEntriesFromServerData[] = {
{"fd00:aabb:ccdd:eeff:11:2233:4455:6677", 0xabcd, 0x6c00},
{"fdde:ad00:beef:0:0:ff:fe00:2800", 0x5678, 0x2800},
{"fd00:1234:5678:9abc:def0:123:4567:89ab", 0x0e, 0x4c00},
{"fdde:ad00:beef:0:0:ff:fe00:6c00", 0xcd12, 0x6c00},
};
const UnicastEntry kUnicastEntriesFromServiceData[] = {
{"fdde:ad00:beef:0:2d0e:c627:5556:18d9", 0x1234, 0x0000},
{"fdde:ad00:beef:0:2d0e:c627:5556:18d9", 0x1234, 0x6c00},
{"fdde:ad00:beef:0:2d0e:c627:5556:18d9", 0x1234, 0x2800},
};
const uint16_t kExpectedRlocs[] = {0x6c00, 0x2800, 0x4c00, 0x0000};
@@ -700,6 +699,7 @@ void TestNetworkDataDsnSrpServices(void)
Service::Manager::Iterator iterator;
Service::DnsSrpAnycast::Info anycastInfo;
Service::DnsSrpUnicast::Info unicastInfo;
Service::DnsSrpUnicast::Type type;
Rlocs rlocs;
reinterpret_cast<TestLeader &>(instance->Get<Leader>()).Populate(kNetworkData, sizeof(kNetworkData));
@@ -749,20 +749,39 @@ void TestNetworkDataDsnSrpServices(void)
"FindPreferredDnsSrpAnycastInfo() returned invalid info");
printf("\n\n- - - - - - - - - - - - - - - - - - - -");
printf("\nDNS/SRP Unicast Service entries\n");
printf("\nDNS/SRP Unicast Service entries (server data)\n");
iterator.Clear();
type = Service::DnsSrpUnicast::kFromServerData;
for (const UnicastEntry &entry : kUnicastEntries)
for (const UnicastEntry &entry : kUnicastEntriesFromServerData)
{
SuccessOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, unicastInfo));
printf("\nunicastInfo { %s, origin:%s, rloc16:%04x }", unicastInfo.mSockAddr.ToString().AsCString(),
kOriginStrings[unicastInfo.mOrigin], unicastInfo.mRloc16);
SuccessOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, 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, unicastInfo) == kErrorNotFound,
VerifyOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, type, unicastInfo) == kErrorNotFound,
"GetNextDnsSrpUnicastInfo() returned unexpected extra entry");
printf("\n\n- - - - - - - - - - - - - - - - - - - -");
printf("\nDNS/SRP Unicast Service entries (service data)\n");
iterator.Clear();
type = Service::DnsSrpUnicast::kFromServiceData;
for (const UnicastEntry &entry : kUnicastEntriesFromServiceData)
{
SuccessOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, 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,
"GetNextDnsSrpUnicastInfo() returned unexpected extra entry");
printf("\n");