diff --git a/src/border_agent/border_agent.cpp b/src/border_agent/border_agent.cpp index a1a30c0ef78..78053b1015d 100644 --- a/src/border_agent/border_agent.cpp +++ b/src/border_agent/border_agent.cpp @@ -368,7 +368,11 @@ void BorderAgent::PublishMeshCopService(void) const otExtAddress *extAddr = otLinkGetExtendedAddress(instance); const char *networkName = otThreadGetNetworkName(instance); Mdns::Publisher::TxtList txtList{{"rv", "1"}}; + Mdns::Publisher::TxtData txtData; int port; + otbrError error; + + OTBR_UNUSED_VARIABLE(error); otbrLogInfo("Publish meshcop service %s.%s.local.", mServiceInstanceName.c_str(), kBorderAgentServiceType); @@ -434,8 +438,11 @@ void BorderAgent::PublishMeshCopService(void) port = kBorderAgentServiceDummyPort; } + error = Mdns::Publisher::EncodeTxtData(txtList, txtData); + assert(error == OTBR_ERROR_NONE); + mPublisher->PublishService(/* aHostName */ "", mServiceInstanceName, kBorderAgentServiceType, - Mdns::Publisher::SubTypeList{}, port, txtList, [this](otbrError aError) { + Mdns::Publisher::SubTypeList{}, port, txtData, [this](otbrError aError) { if (aError == OTBR_ERROR_ABORTED) { // OTBR_ERROR_ABORTED is thrown when an ongoing service registration is diff --git a/src/mdns/mdns.cpp b/src/mdns/mdns.cpp index 2ceaf797714..fc2cf8a31fc 100644 --- a/src/mdns/mdns.cpp +++ b/src/mdns/mdns.cpp @@ -52,14 +52,14 @@ void Publisher::PublishService(const std::string &aHostName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) { otbrError error; mServiceRegistrationBeginTime[std::make_pair(aName, aType)] = Clock::now(); - error = PublishServiceImpl(aHostName, aName, aType, aSubTypeList, aPort, aTxtList, std::move(aCallback)); + error = PublishServiceImpl(aHostName, aName, aType, aSubTypeList, aPort, aTxtData, std::move(aCallback)); if (error != OTBR_ERROR_NONE) { UpdateMdnsResponseCounters(mTelemetryInfo.mServiceRegistrations, error); @@ -280,13 +280,6 @@ Publisher::SubTypeList Publisher::SortSubTypeList(SubTypeList aSubTypeList) return aSubTypeList; } -Publisher::TxtList Publisher::SortTxtList(TxtList aTxtList) -{ - std::sort(aTxtList.begin(), aTxtList.end(), - [](const TxtEntry &aLhs, const TxtEntry &aRhs) { return aLhs.mKey < aRhs.mKey; }); - return aTxtList; -} - Publisher::AddressList Publisher::SortAddressList(AddressList aAddressList) { std::sort(aAddressList.begin(), aAddressList.end()); @@ -339,14 +332,14 @@ Publisher::ResultCallback Publisher::HandleDuplicateServiceRegistration(const st const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) { ServiceRegistration *serviceReg = FindServiceRegistration(aName, aType); VerifyOrExit(serviceReg != nullptr); - if (serviceReg->IsOutdated(aHostName, aName, aType, aSubTypeList, aPort, aTxtList)) + if (serviceReg->IsOutdated(aHostName, aName, aType, aSubTypeList, aPort, aTxtData)) { otbrLogInfo("Removing existing service %s.%s: outdated", aName.c_str(), aType.c_str()); RemoveServiceRegistration(aName, aType, OTBR_ERROR_ABORTED); @@ -454,10 +447,10 @@ bool Publisher::ServiceRegistration::IsOutdated(const std::string &aHostName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList) const + const TxtData &aTxtData) const { return !(mHostName == aHostName && mName == aName && mType == aType && mSubTypeList == aSubTypeList && - mPort == aPort && mTxtList == aTxtList); + mPort == aPort && mTxtData == aTxtData); } void Publisher::ServiceRegistration::Complete(otbrError aError) diff --git a/src/mdns/mdns.hpp b/src/mdns/mdns.hpp index 78e8c5187e1..a4de9181dde 100644 --- a/src/mdns/mdns.hpp +++ b/src/mdns/mdns.hpp @@ -114,6 +114,7 @@ class Publisher : private NonCopyable } }; + typedef std::vector TxtData; typedef std::vector TxtList; typedef std::vector SubTypeList; typedef std::vector AddressList; @@ -124,16 +125,16 @@ class Publisher : private NonCopyable */ struct DiscoveredInstanceInfo { - bool mRemoved = false; ///< The Service Instance is removed. - uint32_t mNetifIndex = 0; ///< Network interface. - std::string mName; ///< Instance name. - std::string mHostName; ///< Full host name. - std::vector mAddresses; ///< IPv6 addresses. - uint16_t mPort = 0; ///< Port. - uint16_t mPriority = 0; ///< Service priority. - uint16_t mWeight = 0; ///< Service weight. - std::vector mTxtData; ///< TXT RDATA bytes. - uint32_t mTtl = 0; ///< Service TTL. + bool mRemoved = false; ///< The Service Instance is removed. + uint32_t mNetifIndex = 0; ///< Network interface. + std::string mName; ///< Instance name. + std::string mHostName; ///< Full host name. + AddressList mAddresses; ///< IPv6 addresses. + uint16_t mPort = 0; ///< Port. + uint16_t mPriority = 0; ///< Service priority. + uint16_t mWeight = 0; ///< Service weight. + TxtData mTxtData; ///< TXT RDATA bytes. + uint32_t mTtl = 0; ///< Service TTL. }; /** @@ -213,7 +214,7 @@ class Publisher : private NonCopyable * @param[in] aType The type of this service. * @param[in] aSubTypeList A list of service subtypes. * @param[in] aPort The port number of this service. - * @param[in] aTxtList A list of TXT name/value pairs. + * @param[in] aTxtData The encoded TXT data for this service. * @param[in] aCallback The callback for receiving the publishing result. `OTBR_ERROR_NONE` will be * returned if the operation is successful and all other values indicate a * failure. Specifically, `OTBR_ERROR_DUPLICATED` indicates that the name has @@ -226,7 +227,7 @@ class Publisher : private NonCopyable const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback); /** @@ -381,7 +382,7 @@ class Publisher : private NonCopyable * @sa DecodeTxtData * */ - static otbrError EncodeTxtData(const TxtList &aTxtList, std::vector &aTxtData); + static otbrError EncodeTxtData(const TxtList &aTxtList, TxtData &aTxtData); /** * This function decodes a TXT entry list from a TXT data buffer. @@ -441,14 +442,14 @@ class Publisher : private NonCopyable std::string mType; SubTypeList mSubTypeList; uint16_t mPort; - TxtList mTxtList; + TxtData mTxtData; ServiceRegistration(std::string aHostName, std::string aName, std::string aType, SubTypeList aSubTypeList, uint16_t aPort, - TxtList aTxtList, + TxtData aTxtData, ResultCallback &&aCallback, Publisher *aPublisher) : Registration(std::move(aCallback), aPublisher) @@ -457,7 +458,7 @@ class Publisher : private NonCopyable , mType(std::move(aType)) , mSubTypeList(SortSubTypeList(std::move(aSubTypeList))) , mPort(aPort) - , mTxtList(SortTxtList(std::move(aTxtList))) + , mTxtData(std::move(aTxtData)) { } ~ServiceRegistration(void) override { OnComplete(OTBR_ERROR_ABORTED); } @@ -472,7 +473,7 @@ class Publisher : private NonCopyable const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList) const; + const TxtData &aTxtData) const; }; class HostRegistration : public Registration @@ -504,7 +505,6 @@ class Publisher : private NonCopyable using HostRegistrationMap = std::map; static SubTypeList SortSubTypeList(SubTypeList aSubTypeList); - static TxtList SortTxtList(TxtList aTxtList); static AddressList SortAddressList(AddressList aAddressList); static std::string MakeFullServiceName(const std::string &aName, const std::string &aType); static std::string MakeFullHostName(const std::string &aName); @@ -514,7 +514,7 @@ class Publisher : private NonCopyable const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) = 0; virtual otbrError PublishHostImpl(const std::string &aName, const std::vector &aAddresses, @@ -544,7 +544,7 @@ class Publisher : private NonCopyable const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback); ResultCallback HandleDuplicateHostRegistration(const std::string &aName, diff --git a/src/mdns/mdns_avahi.cpp b/src/mdns/mdns_avahi.cpp index 735a2c3a1ef..a51cbcc5f14 100644 --- a/src/mdns/mdns_avahi.cpp +++ b/src/mdns/mdns_avahi.cpp @@ -639,13 +639,12 @@ otbrError PublisherAvahi::PublishServiceImpl(const std::string &aHostName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) { otbrError error = OTBR_ERROR_NONE; int avahiError = AVAHI_OK; SubTypeList sortedSubTypeList = SortSubTypeList(aSubTypeList); - TxtList sortedTxtList = SortTxtList(aTxtList); const std::string logHostName = !aHostName.empty() ? aHostName : "localhost"; std::string fullHostName; std::string serviceName = aName; @@ -667,11 +666,11 @@ otbrError PublisherAvahi::PublishServiceImpl(const std::string &aHostName, serviceName = avahi_client_get_host_name(mClient); } - aCallback = HandleDuplicateServiceRegistration(aHostName, serviceName, aType, sortedSubTypeList, aPort, - sortedTxtList, std::move(aCallback)); + aCallback = HandleDuplicateServiceRegistration(aHostName, serviceName, aType, sortedSubTypeList, aPort, aTxtData, + std::move(aCallback)); VerifyOrExit(!aCallback.IsNull()); - SuccessOrExit(error = TxtListToAvahiStringList(aTxtList, txtBuffer, sizeof(txtBuffer), txtHead)); + SuccessOrExit(error = TxtDataToAvahiStringList(aTxtData, txtBuffer, sizeof(txtBuffer), txtHead)); VerifyOrExit((group = CreateGroup(mClient)) != nullptr, error = OTBR_ERROR_MDNS); avahiError = avahi_entry_group_add_service_strlst(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags{}, serviceName.c_str(), aType.c_str(), @@ -693,7 +692,7 @@ otbrError PublisherAvahi::PublishServiceImpl(const std::string &aHostName, VerifyOrExit(avahiError == AVAHI_OK); AddServiceRegistration(std::unique_ptr(new AvahiServiceRegistration( - aHostName, serviceName, aType, sortedSubTypeList, aPort, sortedTxtList, std::move(aCallback), group, this))); + aHostName, serviceName, aType, sortedSubTypeList, aPort, aTxtData, std::move(aCallback), group, this))); exit: if (avahiError != AVAHI_OK || error != OTBR_ERROR_NONE) @@ -790,7 +789,7 @@ void PublisherAvahi::UnpublishHost(const std::string &aName, ResultCallback &&aC std::move(aCallback)(error); } -otbrError PublisherAvahi::TxtListToAvahiStringList(const TxtList &aTxtList, +otbrError PublisherAvahi::TxtDataToAvahiStringList(const TxtData &aTxtData, AvahiStringList *aBuffer, size_t aBufferSize, AvahiStringList *&aHead) @@ -799,9 +798,12 @@ otbrError PublisherAvahi::TxtListToAvahiStringList(const TxtList &aTxtList, size_t used = 0; AvahiStringList *last = nullptr; AvahiStringList *curr = aBuffer; + TxtList txtList; + + SuccessOrExit(error = DecodeTxtData(txtList, aTxtData.data(), aTxtData.size())); aHead = nullptr; - for (const auto &txtEntry : aTxtList) + for (const auto &txtEntry : txtList) { const char *key = txtEntry.mKey.c_str(); size_t keyLength = txtEntry.mKey.length(); diff --git a/src/mdns/mdns_avahi.hpp b/src/mdns/mdns_avahi.hpp index f1f48b0fcf8..29b5ba6730b 100644 --- a/src/mdns/mdns_avahi.hpp +++ b/src/mdns/mdns_avahi.hpp @@ -92,7 +92,7 @@ class PublisherAvahi : public Publisher const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) override; otbrError PublishHostImpl(const std::string &aName, const std::vector &aAddresses, @@ -115,7 +115,7 @@ class PublisherAvahi : public Publisher const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback, AvahiEntryGroup *aEntryGroup, PublisherAvahi *aPublisher) @@ -124,7 +124,7 @@ class PublisherAvahi : public Publisher aType, aSubTypeList, aPort, - aTxtList, + aTxtData, std::move(aCallback), aPublisher) , mEntryGroup(aEntryGroup) @@ -340,7 +340,7 @@ class PublisherAvahi : public Publisher void HandleGroupState(AvahiEntryGroup *aGroup, AvahiEntryGroupState aState); void CallHostOrServiceCallback(AvahiEntryGroup *aGroup, otbrError aError); - static otbrError TxtListToAvahiStringList(const TxtList &aTxtList, + static otbrError TxtDataToAvahiStringList(const TxtData &aTxtData, AvahiStringList *aBuffer, size_t aBufferSize, AvahiStringList *&aHead); diff --git a/src/mdns/mdns_mdnssd.cpp b/src/mdns/mdns_mdnssd.cpp index f95d2f50e0f..af3d5468a42 100644 --- a/src/mdns/mdns_mdnssd.cpp +++ b/src/mdns/mdns_mdnssd.cpp @@ -490,19 +490,17 @@ otbrError PublisherMDnsSd::PublishServiceImpl(const std::string &aHostName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) { - otbrError ret = OTBR_ERROR_NONE; - int error = 0; - std::vector txt; - SubTypeList sortedSubTypeList = SortSubTypeList(aSubTypeList); - TxtList sortedTxtList = SortTxtList(aTxtList); - std::string regType = MakeRegType(aType, sortedSubTypeList); - DNSServiceRef serviceRef = nullptr; - std::string fullHostName; - const char *hostNameCString = nullptr; - const char *serviceNameCString = nullptr; + otbrError ret = OTBR_ERROR_NONE; + int error = 0; + SubTypeList sortedSubTypeList = SortSubTypeList(aSubTypeList); + std::string regType = MakeRegType(aType, sortedSubTypeList); + DNSServiceRef serviceRef = nullptr; + std::string fullHostName; + const char *hostNameCString = nullptr; + const char *serviceNameCString = nullptr; VerifyOrExit(mState == State::kReady, ret = OTBR_ERROR_INVALID_STATE); @@ -516,18 +514,17 @@ otbrError PublisherMDnsSd::PublishServiceImpl(const std::string &aHostName, serviceNameCString = aName.c_str(); } - aCallback = HandleDuplicateServiceRegistration(aHostName, aName, aType, sortedSubTypeList, aPort, sortedTxtList, + aCallback = HandleDuplicateServiceRegistration(aHostName, aName, aType, sortedSubTypeList, aPort, aTxtData, std::move(aCallback)); VerifyOrExit(!aCallback.IsNull()); - SuccessOrExit(ret = EncodeTxtData(aTxtList, txt)); otbrLogInfo("Registering new service %s.%s.local, serviceRef = %p", aName.c_str(), regType.c_str(), serviceRef); SuccessOrExit(error = DNSServiceRegister(&serviceRef, kDNSServiceFlagsNoAutoRename, kDNSServiceInterfaceIndexAny, serviceNameCString, regType.c_str(), - /* domain */ nullptr, hostNameCString, htons(aPort), txt.size(), - txt.data(), HandleServiceRegisterResult, this)); + /* domain */ nullptr, hostNameCString, htons(aPort), aTxtData.size(), + aTxtData.data(), HandleServiceRegisterResult, this)); AddServiceRegistration(std::unique_ptr(new DnssdServiceRegistration( - aHostName, aName, aType, sortedSubTypeList, aPort, sortedTxtList, std::move(aCallback), serviceRef, this))); + aHostName, aName, aType, sortedSubTypeList, aPort, aTxtData, std::move(aCallback), serviceRef, this))); exit: if (error != kDNSServiceErr_NoError || ret != OTBR_ERROR_NONE) diff --git a/src/mdns/mdns_mdnssd.hpp b/src/mdns/mdns_mdnssd.hpp index 76347df6ba4..76d13f66004 100644 --- a/src/mdns/mdns_mdnssd.hpp +++ b/src/mdns/mdns_mdnssd.hpp @@ -89,7 +89,7 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback) override; otbrError PublishHostImpl(const std::string &aName, const std::vector &aAddress, @@ -111,7 +111,7 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, - const TxtList &aTxtList, + const TxtData &aTxtData, ResultCallback &&aCallback, DNSServiceRef aServiceRef, PublisherMDnsSd *aPublisher) @@ -120,7 +120,7 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher aType, aSubTypeList, aPort, - aTxtList, + aTxtData, std::move(aCallback), aPublisher) , mServiceRef(aServiceRef) diff --git a/src/sdp_proxy/advertising_proxy.cpp b/src/sdp_proxy/advertising_proxy.cpp index 9f6ce494ad5..7eac4d0c329 100644 --- a/src/sdp_proxy/advertising_proxy.cpp +++ b/src/sdp_proxy/advertising_proxy.cpp @@ -265,12 +265,12 @@ otbrError AdvertisingProxy::PublishHostAndItsServices(const otSrpServerHost *aHo if (!hostDeleted && !otSrpServerServiceIsDeleted(service)) { - Mdns::Publisher::TxtList txtList = MakeTxtList(service); + Mdns::Publisher::TxtData txtData = MakeTxtData(service); Mdns::Publisher::SubTypeList subTypeList = MakeSubTypeList(service); otbrLogDebug("Publish SRP service '%s'", fullServiceName.c_str()); mPublisher.PublishService( - hostName, serviceName, serviceType, subTypeList, otSrpServerServiceGetPort(service), txtList, + hostName, serviceName, serviceType, subTypeList, otSrpServerServiceGetPort(service), txtData, [this, hasUpdate, updateId, fullServiceName](otbrError aError) { otbrLogResult(aError, "Handle publish SRP service '%s'", fullServiceName.c_str()); if (hasUpdate) @@ -338,16 +338,14 @@ otbrError AdvertisingProxy::PublishHostAndItsServices(const otSrpServerHost *aHo return error; } -Mdns::Publisher::TxtList AdvertisingProxy::MakeTxtList(const otSrpServerService *aSrpService) +Mdns::Publisher::TxtData AdvertisingProxy::MakeTxtData(const otSrpServerService *aSrpService) { - const uint8_t *txtData; - uint16_t txtDataLength = 0; - Mdns::Publisher::TxtList txtList; + const uint8_t *data; + uint16_t length = 0; - txtData = otSrpServerServiceGetTxtData(aSrpService, &txtDataLength); - Mdns::Publisher::DecodeTxtData(txtList, txtData, txtDataLength); + data = otSrpServerServiceGetTxtData(aSrpService, &length); - return txtList; + return Mdns::Publisher::TxtData(data, data + length); } Mdns::Publisher::SubTypeList AdvertisingProxy::MakeSubTypeList(const otSrpServerService *aSrpService) diff --git a/src/sdp_proxy/advertising_proxy.hpp b/src/sdp_proxy/advertising_proxy.hpp index c9a3e2a615f..385dadd6fd7 100644 --- a/src/sdp_proxy/advertising_proxy.hpp +++ b/src/sdp_proxy/advertising_proxy.hpp @@ -100,7 +100,7 @@ class AdvertisingProxy : private NonCopyable void *aContext); void AdvertisingHandler(otSrpServerServiceUpdateId aId, const otSrpServerHost *aHost, uint32_t aTimeout); - static Mdns::Publisher::TxtList MakeTxtList(const otSrpServerService *aSrpService); + static Mdns::Publisher::TxtData MakeTxtData(const otSrpServerService *aSrpService); static Mdns::Publisher::SubTypeList MakeSubTypeList(const otSrpServerService *aSrpService); void OnMdnsPublishResult(otSrpServerServiceUpdateId aUpdateId, otbrError aError); diff --git a/src/trel_dnssd/trel_dnssd.cpp b/src/trel_dnssd/trel_dnssd.cpp index 37ec8c5809b..c285a83410a 100644 --- a/src/trel_dnssd/trel_dnssd.cpp +++ b/src/trel_dnssd/trel_dnssd.cpp @@ -249,7 +249,7 @@ void TrelDnssd::PublishTrelService(void) mRegisterInfo.mInstanceName = GetTrelInstanceName(); mPublisher.PublishService(/* aHostName */ "", mRegisterInfo.mInstanceName, kTrelServiceName, - Mdns::Publisher::SubTypeList{}, mRegisterInfo.mPort, mRegisterInfo.mTxtEntries, + Mdns::Publisher::SubTypeList{}, mRegisterInfo.mPort, mRegisterInfo.mTxtData, [](otbrError aError) { HandlePublishTrelServiceError(aError); }); } @@ -461,18 +461,11 @@ uint16_t TrelDnssd::CountDuplicatePeers(const TrelDnssd::Peer &aPeer) void TrelDnssd::RegisterInfo::Assign(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength) { - otbrError error; - - OTBR_UNUSED_VARIABLE(error); - assert(!IsPublished()); assert(aPort > 0); mPort = aPort; - mTxtEntries.clear(); - - error = Mdns::Publisher::DecodeTxtData(mTxtEntries, aTxtData, aTxtLength); - assert(error == OTBR_ERROR_NONE); + mTxtData.assign(aTxtData, aTxtData + aTxtLength); } void TrelDnssd::RegisterInfo::Clear(void) @@ -480,7 +473,7 @@ void TrelDnssd::RegisterInfo::Clear(void) assert(!IsPublished()); mPort = 0; - mTxtEntries.clear(); + mTxtData.clear(); } const char TrelDnssd::Peer::kTxtRecordExtAddressKey[] = "xa"; diff --git a/src/trel_dnssd/trel_dnssd.hpp b/src/trel_dnssd/trel_dnssd.hpp index cd7daaa8893..8f44104e13e 100644 --- a/src/trel_dnssd/trel_dnssd.hpp +++ b/src/trel_dnssd/trel_dnssd.hpp @@ -120,9 +120,9 @@ class TrelDnssd struct RegisterInfo { - uint16_t mPort = 0; - std::vector mTxtEntries; - std::string mInstanceName; + uint16_t mPort = 0; + Mdns::Publisher::TxtData mTxtData; + std::string mInstanceName; bool IsValid(void) const { return mPort > 0; } bool IsPublished(void) const { return !mInstanceName.empty(); } diff --git a/tests/mdns/main.cpp b/tests/mdns/main.cpp index 607f14435c4..583175161e0 100644 --- a/tests/mdns/main.cpp +++ b/tests/mdns/main.cpp @@ -96,14 +96,17 @@ void PublishSingleServiceWithCustomHost(void *aContext, Mdns::Publisher::State a VerifyOrDie(aContext == &sContext, "unexpected context"); if (aState == Mdns::Publisher::State::kReady) { + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool"}, {"xp", xpanid, sizeof(xpanid)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + sContext.mPublisher->PublishHost(hostName, {Ip6Address(hostAddr)}, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the host"); }); sContext.mPublisher->PublishService( - hostName, "SingleService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + hostName, "SingleService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the service"); }); } } @@ -123,18 +126,21 @@ void PublishMultipleServicesWithCustomHost(void *aContext, Mdns::Publisher::Stat VerifyOrDie(aContext == &sContext, "unexpected context"); if (aState == Mdns::Publisher::State::kReady) { + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool"}, {"xp", xpanid, sizeof(xpanid)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + sContext.mPublisher->PublishHost(hostName1, {Ip6Address(hostAddr)}, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the host"); }); sContext.mPublisher->PublishService( - hostName1, "MultipleService11", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + hostName1, "MultipleService11", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the first service"); }); sContext.mPublisher->PublishService( - hostName1, "MultipleService12", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + hostName1, "MultipleService12", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the second service"); }); sContext.mPublisher->PublishHost(hostName2, {Ip6Address(hostAddr)}, [](otbrError aError) { @@ -142,11 +148,11 @@ void PublishMultipleServicesWithCustomHost(void *aContext, Mdns::Publisher::Stat }); sContext.mPublisher->PublishService( - hostName2, "MultipleService21", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + hostName2, "MultipleService21", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the first service"); }); sContext.mPublisher->PublishService( - hostName2, "MultipleService22", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + hostName2, "MultipleService22", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "cannot publish the second service"); }); } } @@ -157,14 +163,17 @@ void PublishSingleService(void *aContext, Mdns::Publisher::State aState) uint8_t xpanid[kSizeExtPanId] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; uint8_t extAddr[kSizeExtAddr] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool"}, {"xp", xpanid, sizeof(xpanid)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + assert(aContext == &sContext); if (aState == Mdns::Publisher::State::kReady) { sContext.mPublisher->PublishService( - "", "SingleService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + "", "SingleService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "SingleService._meshcop._udp."); }); } } @@ -175,13 +184,16 @@ void PublishSingleServiceWithEmptyName(void *aContext, Mdns::Publisher::State aS uint8_t xpanid[kSizeExtPanId] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; uint8_t extAddr[kSizeExtAddr] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool"}, {"xp", xpanid, sizeof(xpanid)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + assert(aContext == &sContext); if (aState == Mdns::Publisher::State::kReady) { - sContext.mPublisher->PublishService("", "", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + sContext.mPublisher->PublishService("", "", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "(empty)._meshcop._udp."); }); } } @@ -196,21 +208,27 @@ void PublishMultipleServices(void *aContext, Mdns::Publisher::State aState) assert(aContext == &sContext); if (aState == Mdns::Publisher::State::kReady) { + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool1"}, {"xp", xpanid, sizeof(xpanid)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + sContext.mPublisher->PublishService( - "", "MultipleService1", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + "", "MultipleService1", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "MultipleService1._meshcop._udp."); }); } if (aState == Mdns::Publisher::State::kReady) { + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool2"}, {"xp", xpanid, sizeof(xpanid)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + sContext.mPublisher->PublishService( - "", "MultipleService2", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + "", "MultipleService2", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "MultipleService2._meshcop._udp."); }); } } @@ -226,28 +244,38 @@ void PublishUpdateServices(void *aContext) assert(aContext == &sContext); if (!sContext.mUpdate) { + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{ {"nn", "cool"}, {"xp", xpanidOld, sizeof(xpanidOld)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + sContext.mPublisher->PublishService( - "", "UpdateService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + "", "UpdateService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { otbrLogResult(aError, "UpdateService._meshcop._udp"); }); } else { + Mdns::Publisher::TxtData txtData; Mdns::Publisher::TxtList txtList{{"nn", "coolcool"}, {"xp", xpanidNew, sizeof(xpanidNew)}, {"tv", "1.1.1"}, {"xa", extAddr, sizeof(extAddr)}}; + Mdns::Publisher::EncodeTxtData(txtList, txtData); + sContext.mPublisher->PublishService( - "", "UpdateService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtList, + "", "UpdateService", "_meshcop._udp.", Mdns::Publisher::SubTypeList{}, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "UpdateService._meshcop._udp"); }); } } void PublishServiceSubTypes(void *aContext) { + Mdns::Publisher::TxtData txtData; + + txtData.push_back(0); + OT_UNUSED_VARIABLE(aContext); assert(aContext == &sContext); @@ -257,7 +285,7 @@ void PublishServiceSubTypes(void *aContext) subTypeList.back() = "_SUBTYPE3"; sContext.mPublisher->PublishService( - "", "ServiceWithSubTypes", "_meshcop._udp.", subTypeList, 12345, Mdns::Publisher::TxtList{}, + "", "ServiceWithSubTypes", "_meshcop._udp.", subTypeList, 12345, txtData, [](otbrError aError) { SuccessOrDie(aError, "ServiceWithSubTypes._meshcop._udp."); }); }