From 119df62cbddce9d3e10b0a56d7fbf9d53c7fa218 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 23 Mar 2022 13:17:53 -0700 Subject: [PATCH] [netdata] update `Service::DnsSrpUnicast` to include `Origin` (#7498) This commit updates `NetworkData::Service::DnsSrpUnicast` to indicate `Origin` of the entry, whether the server IPv6 address and port number is included as part of service data or server data. This will be used by clients to prefer entries with the info in service data (which coveys the infra-structure provided (authoritative) server) over the ones in server data (which are used by Thread BRs). This commit also updates the unit test `test_network_data` to validate the new behavior. --- src/core/thread/network_data_service.cpp | 3 +++ src/core/thread/network_data_service.hpp | 11 +++++++++++ tests/unit/test_network_data.cpp | 23 ++++++++++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/core/thread/network_data_service.cpp b/src/core/thread/network_data_service.cpp index 878c027c2..f4d7f5813 100644 --- a/src/core/thread/network_data_service.cpp +++ b/src/core/thread/network_data_service.cpp @@ -239,6 +239,7 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info aInfo.mSockAddr.SetAddress(serverData->GetAddress()); aInfo.mSockAddr.SetPort(serverData->GetPort()); + aInfo.mOrigin = DnsSrpUnicast::kFromServerData; ExitNow(); } @@ -250,6 +251,7 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info aInfo.mSockAddr.GetAddress().SetToRoutingLocator(Get().GetMeshLocalPrefix(), aIterator.mServerSubTlv->GetServer16()); aInfo.mSockAddr.SetPort(Encoding::BigEndian::ReadUint16(data.GetBytes())); + aInfo.mOrigin = DnsSrpUnicast::kFromServerData; ExitNow(); } } @@ -271,6 +273,7 @@ Error Manager::GetNextDnsSrpUnicastInfo(Iterator &aIterator, DnsSrpUnicast::Info dnsServiceData = reinterpret_cast(serviceData.GetBytes()); aInfo.mSockAddr.SetAddress(dnsServiceData->GetAddress()); aInfo.mSockAddr.SetPort(dnsServiceData->GetPort()); + aInfo.mOrigin = DnsSrpUnicast::kFromServiceData; ExitNow(); } diff --git a/src/core/thread/network_data_service.hpp b/src/core/thread/network_data_service.hpp index b7f16f97c..25cf77094 100644 --- a/src/core/thread/network_data_service.hpp +++ b/src/core/thread/network_data_service.hpp @@ -259,6 +259,16 @@ public: */ static const uint8_t kServiceData = kServiceNumber; + /** + * This enumeration represents the origin a `DnsSrpUnicast` entry. + * + */ + enum Origin : uint8_t + { + kFromServiceData, ///< Socket address is from service data. + kFromServerData, ///< Socket address is from server data. + }; + /** * This structure represents information about an DNS/SRP server parsed from related Network Data service entries. * @@ -266,6 +276,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). }; /** diff --git a/tests/unit/test_network_data.cpp b/tests/unit/test_network_data.cpp index 31438a1d1..93d607e8a 100644 --- a/tests/unit/test_network_data.cpp +++ b/tests/unit/test_network_data.cpp @@ -318,6 +318,11 @@ 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: @@ -353,8 +358,9 @@ void TestNetworkDataDsnSrpServices(void) struct UnicastEntry { - const char *mAddress; - uint16_t mPort; + const char * mAddress; + uint16_t mPort; + Service::DnsSrpUnicast::Origin mOrigin; bool Matches(Service::DnsSrpUnicast::Info aInfo) const { @@ -363,7 +369,7 @@ void TestNetworkDataDsnSrpServices(void) SuccessOrQuit(sockAddr.GetAddress().FromString(mAddress)); sockAddr.SetPort(mPort); - return (aInfo.mSockAddr == sockAddr); + return (aInfo.mSockAddr == sockAddr) && (aInfo.mOrigin == mOrigin); } }; @@ -384,9 +390,11 @@ void TestNetworkDataDsnSrpServices(void) }; const UnicastEntry kUnicastEntries[] = { - {"fdde:ad00:beef:0:2d0e:c627:5556:18d9", 0x1234}, {"fd00:aabb:ccdd:eeff:11:2233:4455:6677", 0xabcd}, - {"fdde:ad00:beef:0:0:ff:fe00:2800", 0x5678}, {"fd00:1234:5678:9abc:def0:123:4567:89ab", 0x0e}, - {"fdde:ad00:beef:0:0:ff:fe00:6c00", 0xcd12}, + {"fdde:ad00:beef:0:2d0e:c627:5556:18d9", 0x1234, Service::DnsSrpUnicast::kFromServiceData}, + {"fd00:aabb:ccdd:eeff:11:2233:4455:6677", 0xabcd, Service::DnsSrpUnicast::kFromServerData}, + {"fdde:ad00:beef:0:0:ff:fe00:2800", 0x5678, Service::DnsSrpUnicast::kFromServerData}, + {"fd00:1234:5678:9abc:def0:123:4567:89ab", 0x0e, Service::DnsSrpUnicast::kFromServerData}, + {"fdde:ad00:beef:0:0:ff:fe00:6c00", 0xcd12, Service::DnsSrpUnicast::kFromServerData}, }; const uint8_t kPreferredAnycastEntryIndex = 2; @@ -436,7 +444,8 @@ void TestNetworkDataDsnSrpServices(void) for (const UnicastEntry &entry : kUnicastEntries) { SuccessOrQuit(manager.GetNextDnsSrpUnicastInfo(iterator, unicastInfo)); - printf("\nunicastInfo %s", unicastInfo.mSockAddr.ToString().AsCString()); + printf("\nunicastInfo { %s, origin:%s }", unicastInfo.mSockAddr.ToString().AsCString(), + kOriginStrings[unicastInfo.mOrigin]); VerifyOrQuit(entry.Matches(unicastInfo), "GetNextDnsSrpUnicastInfo() returned incorrect info"); }