diff --git a/src/mdns/mdns_mdnssd.cpp b/src/mdns/mdns_mdnssd.cpp index 39709405600..5a07559f805 100644 --- a/src/mdns/mdns_mdnssd.cpp +++ b/src/mdns/mdns_mdnssd.cpp @@ -270,6 +270,19 @@ void PublisherMDnsSd::Stop(StopMode aStopMode) return; } +DNSServiceErrorType PublisherMDnsSd::CreateSharedHostsRef(void) +{ + DNSServiceErrorType dnsError = kDNSServiceErr_NoError; + + VerifyOrExit(mHostsRef == nullptr); + + dnsError = DNSServiceCreateConnection(&mHostsRef); + otbrLogDebug("Created new shared DNSServiceRef: %p", mHostsRef); + +exit: + return dnsError; +} + void PublisherMDnsSd::DeallocateHostsRef(void) { VerifyOrExit(mHostsRef != nullptr); @@ -289,15 +302,7 @@ void PublisherMDnsSd::Update(MainloopContext &aMainloop) { auto &serviceReg = static_cast(*kv.second); - assert(serviceReg.GetServiceRef() != nullptr); - - int fd = DNSServiceRefSockFD(serviceReg.GetServiceRef()); - - if (fd != -1) - { - FD_SET(fd, &aMainloop.mReadFdSet); - aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, fd); - } + serviceReg.Update(aMainloop); } if (mHostsRef != nullptr) @@ -329,12 +334,8 @@ void PublisherMDnsSd::Process(const MainloopContext &aMainloop) for (auto &kv : mServiceRegistrations) { auto &serviceReg = static_cast(*kv.second); - int fd = DNSServiceRefSockFD(serviceReg.GetServiceRef()); - if (FD_ISSET(fd, &aMainloop.mReadFdSet)) - { - mServiceRefsToProcess.push_back(serviceReg.GetServiceRef()); - } + serviceReg.Process(aMainloop, mServiceRefsToProcess); } if (mHostsRef != nullptr) @@ -408,131 +409,224 @@ void PublisherMDnsSd::HandleServiceRefDeallocating(const DNSServiceRef &aService } } -PublisherMDnsSd::DnssdServiceRegistration::~DnssdServiceRegistration(void) +void PublisherMDnsSd::DnssdServiceRegistration::Update(MainloopContext &aMainloop) const +{ + int fd; + + VerifyOrExit(mServiceRef != nullptr); + + fd = DNSServiceRefSockFD(mServiceRef); + VerifyOrExit(fd != -1); + + FD_SET(fd, &aMainloop.mReadFdSet); + aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, fd); + +exit: + return; +} + +void PublisherMDnsSd::DnssdServiceRegistration::Process(const MainloopContext &aMainloop, + std::vector &aReadyServices) const +{ + int fd; + + VerifyOrExit(mServiceRef != nullptr); + + fd = DNSServiceRefSockFD(mServiceRef); + VerifyOrExit(fd != -1); + + VerifyOrExit(FD_ISSET(fd, &aMainloop.mReadFdSet)); + aReadyServices.push_back(mServiceRef); + +exit: + return; +} + +otbrError PublisherMDnsSd::DnssdServiceRegistration::Register(void) +{ + std::string fullHostName; + std::string regType = MakeRegType(mType, mSubTypeList); + const char *hostNameCString = nullptr; + const char *serviceNameCString = nullptr; + DNSServiceErrorType dnsError; + + if (!mHostName.empty()) + { + fullHostName = MakeFullHostName(mHostName); + hostNameCString = fullHostName.c_str(); + } + + if (!mName.empty()) + { + serviceNameCString = mName.c_str(); + } + + otbrLogInfo("Registering service %s.%s", mName.c_str(), regType.c_str()); + + dnsError = DNSServiceRegister(&mServiceRef, kDNSServiceFlagsNoAutoRename, kDNSServiceInterfaceIndexAny, + serviceNameCString, regType.c_str(), + /* domain */ nullptr, hostNameCString, htons(mPort), mTxtData.size(), mTxtData.data(), + HandleRegisterResult, this); + + if (dnsError != kDNSServiceErr_NoError) + { + HandleRegisterResult(/* aFlags */ 0, dnsError); + } + + return GetPublisher().DnsErrorToOtbrError(dnsError); +} + +void PublisherMDnsSd::DnssdServiceRegistration::Unregister(void) { if (mServiceRef != nullptr) { GetPublisher().HandleServiceRefDeallocating(mServiceRef); DNSServiceRefDeallocate(mServiceRef); + mServiceRef = nullptr; } } -PublisherMDnsSd::DnssdHostRegistration::~DnssdHostRegistration(void) +void PublisherMDnsSd::DnssdServiceRegistration::HandleRegisterResult(DNSServiceRef aServiceRef, + DNSServiceFlags aFlags, + DNSServiceErrorType aError, + const char *aName, + const char *aType, + const char *aDomain, + void *aContext) { - int dnsError; + OTBR_UNUSED_VARIABLE(aServiceRef); + OTBR_UNUSED_VARIABLE(aName); + OTBR_UNUSED_VARIABLE(aType); + OTBR_UNUSED_VARIABLE(aDomain); - VerifyOrExit(GetPublisher().mHostsRef != nullptr); - VerifyOrExit(mServiceRef != nullptr); + static_cast(aContext)->HandleRegisterResult(aFlags, aError); +} - for (const auto &recordRefAndAddress : GetRecordRefMap()) +void PublisherMDnsSd::DnssdServiceRegistration::HandleRegisterResult(DNSServiceFlags aFlags, DNSServiceErrorType aError) +{ + if ((aError == kDNSServiceErr_NoError) && (aFlags & kDNSServiceFlagsAdd)) { - const DNSRecordRef &recordRef = recordRefAndAddress.first; - const Ip6Address &address = recordRefAndAddress.second; - if (IsCompleted()) - { - // The Bonjour mDNSResponder somehow doesn't send goodbye message for the AAAA record when it is - // removed by `DNSServiceRemoveRecord`. Per RFC 6762, a goodbye message of a record sets its TTL - // to zero but the receiver should record the TTL of 1 and flushes the cache 1 second later. Here - // we remove the AAAA record after updating its TTL to 1 second. This has the same effect as - // sending a goodbye message. - // TODO: resolve the goodbye issue with Bonjour mDNSResponder. - dnsError = DNSServiceUpdateRecord(mServiceRef, recordRef, kDNSServiceFlagsUnique, sizeof(address.m8), - address.m8, /* ttl */ 1); - otbrLogResult(DNSErrorToOtbrError(dnsError), "Send goodbye message for host %s address %s: %s", - MakeFullHostName(mName).c_str(), address.ToString().c_str(), DNSErrorToString(dnsError)); - } - dnsError = DNSServiceRemoveRecord(mServiceRef, recordRef, /* flags */ 0); - otbrLogResult(DNSErrorToOtbrError(dnsError), "Remove record for host %s address %s: %s", - MakeFullHostName(mName).c_str(), address.ToString().c_str(), DNSErrorToString(dnsError)); - // TODO: ? - // DNSRecordRefDeallocate(recordRef); + otbrLogInfo("Successfully registered service %s.%s", mName.c_str(), mType.c_str()); + Complete(OTBR_ERROR_NONE); + } + else + { + otbrLogErr("Failed to register service %s.%s: %s", mName.c_str(), mType.c_str(), DNSErrorToString(aError)); + GetPublisher().RemoveServiceRegistration(mName, mType, DNSErrorToOtbrError(aError)); } - -exit: - return; } -Publisher::ServiceRegistration *PublisherMDnsSd::FindServiceRegistration(const DNSServiceRef &aServiceRef) +otbrError PublisherMDnsSd::DnssdHostRegistration::Register(void) { - ServiceRegistration *result = nullptr; + DNSServiceErrorType dnsError = kDNSServiceErr_NoError; - for (auto &kv : mServiceRegistrations) + otbrLogInfo("Registering new host %s", mName.c_str()); + + for (const Ip6Address &address : mAddresses) { - // We are sure that the service registrations must be instances of `DnssdServiceRegistration`. - auto &serviceReg = static_cast(*kv.second); + DNSRecordRef recordRef = nullptr; - if (serviceReg.GetServiceRef() == aServiceRef) - { - result = kv.second.get(); - break; - } + dnsError = GetPublisher().CreateSharedHostsRef(); + VerifyOrExit(dnsError == kDNSServiceErr_NoError); + + dnsError = DNSServiceRegisterRecord(GetPublisher().mHostsRef, &recordRef, kDNSServiceFlagsShared, + kDNSServiceInterfaceIndexAny, MakeFullHostName(mName).c_str(), + kDNSServiceType_AAAA, kDNSServiceClass_IN, sizeof(address.m8), address.m8, + /* ttl */ 0, HandleRegisterResult, this); + VerifyOrExit(dnsError == kDNSServiceErr_NoError); + + mAddrRecordRefs.push_back(recordRef); + mAddrRegistered.push_back(false); } - return result; +exit: + if ((dnsError != kDNSServiceErr_NoError) || mAddresses.empty()) + { + HandleRegisterResult(/* aRecordRef */ nullptr, dnsError); + } + + return GetPublisher().DnsErrorToOtbrError(dnsError); } -Publisher::HostRegistration *PublisherMDnsSd::FindHostRegistration(const DNSServiceRef &aServiceRef, - const DNSRecordRef &aRecordRef) +void PublisherMDnsSd::DnssdHostRegistration::Unregister(void) { - HostRegistration *result = nullptr; + DNSServiceErrorType dnsError; + + VerifyOrExit(GetPublisher().mHostsRef != nullptr); - for (auto &kv : mHostRegistrations) + for (size_t index = 0; index < mAddrRecordRefs.size(); index++) { - // We are sure that the host registrations must be instances of `DnssdHostRegistration`. - auto &hostReg = static_cast(*kv.second); + const Ip6Address &address = mAddresses[index]; - if (hostReg.GetServiceRef() == aServiceRef && hostReg.GetRecordRefMap().count(aRecordRef)) + if (mAddrRegistered[index]) { - result = kv.second.get(); - break; + // The Bonjour mDNSResponder somehow doesn't send goodbye message for the AAAA record when it is + // removed by `DNSServiceRemoveRecord`. Per RFC 6762, a goodbye message of a record sets its TTL + // to zero but the receiver should record the TTL of 1 and flushes the cache 1 second later. Here + // we remove the AAAA record after updating its TTL to 1 second. This has the same effect as + // sending a goodbye message. + // TODO: resolve the goodbye issue with Bonjour mDNSResponder. + dnsError = DNSServiceUpdateRecord(GetPublisher().mHostsRef, mAddrRecordRefs[index], kDNSServiceFlagsUnique, + sizeof(address.m8), address.m8, /* ttl */ 1); + otbrLogResult(DNSErrorToOtbrError(dnsError), "Send goodbye message for host %s address %s: %s", + MakeFullHostName(mName).c_str(), address.ToString().c_str(), DNSErrorToString(dnsError)); } + + dnsError = DNSServiceRemoveRecord(GetPublisher().mHostsRef, mAddrRecordRefs[index], /* flags */ 0); + + otbrLogResult(DNSErrorToOtbrError(dnsError), "Remove record for host %s address %s: %s", + MakeFullHostName(mName).c_str(), address.ToString().c_str(), DNSErrorToString(dnsError)); } - return result; +exit: + mAddrRegistered.clear(); + mAddrRecordRefs.clear(); } -void PublisherMDnsSd::HandleServiceRegisterResult(DNSServiceRef aService, - const DNSServiceFlags aFlags, - DNSServiceErrorType aError, - const char *aName, - const char *aType, - const char *aDomain, - void *aContext) +void PublisherMDnsSd::DnssdHostRegistration::HandleRegisterResult(DNSServiceRef aServiceRef, + DNSRecordRef aRecordRef, + DNSServiceFlags aFlags, + DNSServiceErrorType aError, + void *aContext) { - static_cast(aContext)->HandleServiceRegisterResult(aService, aFlags, aError, aName, aType, - aDomain); + OT_UNUSED_VARIABLE(aServiceRef); + OT_UNUSED_VARIABLE(aFlags); + + static_cast(aContext)->HandleRegisterResult(aRecordRef, aError); } -void PublisherMDnsSd::HandleServiceRegisterResult(DNSServiceRef aServiceRef, - const DNSServiceFlags aFlags, - DNSServiceErrorType aError, - const char *aName, - const char *aType, - const char *aDomain) +void PublisherMDnsSd::DnssdHostRegistration::HandleRegisterResult(DNSRecordRef aRecordRef, DNSServiceErrorType aError) { - OTBR_UNUSED_VARIABLE(aDomain); - - otbrError error = DNSErrorToOtbrError(aError); - ServiceRegistration *serviceReg = FindServiceRegistration(aServiceRef); - serviceReg->mName = aName; - - otbrLogInfo("Received reply for service %s.%s, serviceRef = %p", aName, aType, aServiceRef); - - VerifyOrExit(serviceReg != nullptr); - - if (aError == kDNSServiceErr_NoError && (aFlags & kDNSServiceFlagsAdd)) + if (aError != kDNSServiceErr_NoError) { - otbrLogInfo("Successfully registered service %s.%s", aName, aType); - serviceReg->Complete(OTBR_ERROR_NONE); + otbrLogErr("Failed to register host %s: %s", mName.c_str(), DNSErrorToString(aError)); + GetPublisher().RemoveHostRegistration(mName, DNSErrorToOtbrError(aError)); } else { - otbrLogErr("Failed to register service %s.%s: %s", aName, aType, DNSErrorToString(aError)); - RemoveServiceRegistration(serviceReg->mName, serviceReg->mType, error); - } + bool shouldComplete = !IsCompleted(); -exit: - return; + for (size_t index = 0; index < mAddrRecordRefs.size(); index++) + { + if ((mAddrRecordRefs[index] == aRecordRef) && !mAddrRegistered[index]) + { + mAddrRegistered[index] = true; + otbrLogInfo("Successfully registered host %s address %s", mName.c_str(), + mAddresses[index].ToString().c_str()); + } + + if (!mAddrRegistered[index]) + { + shouldComplete = false; + } + } + + if (shouldComplete) + { + otbrLogInfo("Successfully registered all host %s addresses", mName.c_str()); + Complete(OTBR_ERROR_NONE); + } + } } otbrError PublisherMDnsSd::PublishServiceImpl(const std::string &aHostName, @@ -543,57 +637,30 @@ otbrError PublisherMDnsSd::PublishServiceImpl(const std::string &aHostName, const TxtData &aTxtData, ResultCallback &&aCallback) { - 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); - - if (!aHostName.empty()) - { - fullHostName = MakeFullHostName(aHostName); - hostNameCString = fullHostName.c_str(); - } - if (!aName.empty()) + otbrError error = OTBR_ERROR_NONE; + SubTypeList sortedSubTypeList = SortSubTypeList(aSubTypeList); + std::string regType = MakeRegType(aType, sortedSubTypeList); + DnssdServiceRegistration *serviceReg; + + if (mState != State::kReady) { - serviceNameCString = aName.c_str(); + error = OTBR_ERROR_INVALID_STATE; + std::move(aCallback)(error); + ExitNow(); } aCallback = HandleDuplicateServiceRegistration(aHostName, aName, aType, sortedSubTypeList, aPort, aTxtData, std::move(aCallback)); VerifyOrExit(!aCallback.IsNull()); - 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), aTxtData.size(), - aTxtData.data(), HandleServiceRegisterResult, this)); - AddServiceRegistration(std::unique_ptr(new DnssdServiceRegistration( - aHostName, aName, aType, sortedSubTypeList, aPort, aTxtData, std::move(aCallback), serviceRef, this))); + serviceReg = new DnssdServiceRegistration(aHostName, aName, aType, sortedSubTypeList, aPort, aTxtData, + std::move(aCallback), this); + AddServiceRegistration(std::unique_ptr(serviceReg)); -exit: - if (error != kDNSServiceErr_NoError || ret != OTBR_ERROR_NONE) - { - if (error != kDNSServiceErr_NoError) - { - ret = DNSErrorToOtbrError(error); - otbrLogErr("Failed to publish service %s.%s for mdnssd error: %s!", aName.c_str(), aType.c_str(), - DNSErrorToString(error)); - } + error = serviceReg->Register(); - if (serviceRef != nullptr) - { - HandleServiceRefDeallocating(serviceRef); - DNSServiceRefDeallocate(serviceRef); - } - std::move(aCallback)(ret); - } - return ret; +exit: + return error; } void PublisherMDnsSd::UnpublishService(const std::string &aName, const std::string &aType, ResultCallback &&aCallback) @@ -611,54 +678,26 @@ otbrError PublisherMDnsSd::PublishHostImpl(const std::string &aName, const AddressList &aAddresses, ResultCallback &&aCallback) { - otbrError ret = OTBR_ERROR_NONE; - int error = 0; - std::string fullName; - std::unique_ptr registration; - - VerifyOrExit(mState == Publisher::State::kReady, ret = OTBR_ERROR_INVALID_STATE); + otbrError error = OTBR_ERROR_NONE; + DnssdHostRegistration *hostReg; - fullName = MakeFullHostName(aName); - - aCallback = HandleDuplicateHostRegistration(aName, aAddresses, std::move(aCallback)); - VerifyOrExit(!aCallback.IsNull()); - VerifyOrExit(!aAddresses.empty(), std::move(aCallback)(OTBR_ERROR_NONE)); - - if (mHostsRef == nullptr) + if (mState != State::kReady) { - SuccessOrExit(error = DNSServiceCreateConnection(&mHostsRef)); - otbrLogDebug("Created new DNSServiceRef for hosts: %p", mHostsRef); + error = OTBR_ERROR_INVALID_STATE; + std::move(aCallback)(error); + ExitNow(); } - registration.reset(new DnssdHostRegistration(aName, aAddresses, std::move(aCallback), mHostsRef, this)); + aCallback = HandleDuplicateHostRegistration(aName, aAddresses, std::move(aCallback)); + VerifyOrExit(!aCallback.IsNull()); - otbrLogInfo("Registering new host %s", aName.c_str()); - for (const auto &address : aAddresses) - { - DNSRecordRef recordRef = nullptr; - // Supports only IPv6 for now, may support IPv4 in the future. - SuccessOrExit(error = DNSServiceRegisterRecord(mHostsRef, &recordRef, kDNSServiceFlagsShared, - kDNSServiceInterfaceIndexAny, fullName.c_str(), - kDNSServiceType_AAAA, kDNSServiceClass_IN, sizeof(address.m8), - address.m8, /* ttl */ 0, HandleRegisterHostResult, this)); - registration->GetRecordRefMap()[recordRef] = address; - } + hostReg = new DnssdHostRegistration(aName, aAddresses, std::move(aCallback), this); + AddHostRegistration(std::unique_ptr(hostReg)); - AddHostRegistration(std::move(registration)); + error = hostReg->Register(); exit: - if (error != kDNSServiceErr_NoError || ret != OTBR_ERROR_NONE) - { - if (error != kDNSServiceErr_NoError) - { - ret = DNSErrorToOtbrError(error); - otbrLogErr("Failed to publish/update host %s for mdnssd error: %s!", aName.c_str(), - DNSErrorToString(error)); - } - - std::move(aCallback)(ret); - } - return ret; + return error; } void PublisherMDnsSd::UnpublishHost(const std::string &aName, ResultCallback &&aCallback) @@ -675,52 +714,6 @@ void PublisherMDnsSd::UnpublishHost(const std::string &aName, ResultCallback &&a std::move(aCallback)(error); } -void PublisherMDnsSd::HandleRegisterHostResult(DNSServiceRef aServiceRef, - DNSRecordRef aRecordRef, - DNSServiceFlags aFlags, - DNSServiceErrorType aError, - void *aContext) -{ - static_cast(aContext)->HandleRegisterHostResult(aServiceRef, aRecordRef, aFlags, aError); -} - -void PublisherMDnsSd::HandleRegisterHostResult(DNSServiceRef aServiceRef, - DNSRecordRef aRecordRef, - DNSServiceFlags aFlags, - DNSServiceErrorType aError) -{ - OTBR_UNUSED_VARIABLE(aFlags); - - otbrError error = DNSErrorToOtbrError(aError); - auto *hostReg = static_cast(FindHostRegistration(aServiceRef, aRecordRef)); - - std::string hostName; - - VerifyOrExit(hostReg != nullptr); - - hostName = MakeFullHostName(hostReg->mName); - - otbrLogInfo("Received reply for host %s: %s", hostName.c_str(), DNSErrorToString(aError)); - - if (error == OTBR_ERROR_NONE) - { - --hostReg->mCallbackCount; - if (!hostReg->mCallbackCount) - { - otbrLogInfo("Successfully registered host %s", hostName.c_str()); - hostReg->Complete(OTBR_ERROR_NONE); - } - } - else - { - otbrLogWarning("Failed to register host %s for mdnssd error: %s", hostName.c_str(), DNSErrorToString(aError)); - RemoveHostRegistration(hostReg->mName, error); - } - -exit: - return; -} - // See `regtype` parameter of the DNSServiceRegister() function for more information. std::string PublisherMDnsSd::MakeRegType(const std::string &aType, SubTypeList aSubTypeList) { diff --git a/src/mdns/mdns_mdnssd.hpp b/src/mdns/mdns_mdnssd.hpp index 788d581460d..fa4bf4d1a73 100644 --- a/src/mdns/mdns_mdnssd.hpp +++ b/src/mdns/mdns_mdnssd.hpp @@ -112,63 +112,50 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher class DnssdServiceRegistration : public ServiceRegistration { public: - DnssdServiceRegistration(const std::string &aHostName, - const std::string &aName, - const std::string &aType, - const SubTypeList &aSubTypeList, - uint16_t aPort, - const TxtData &aTxtData, - ResultCallback &&aCallback, - DNSServiceRef aServiceRef, - PublisherMDnsSd *aPublisher) - : ServiceRegistration(aHostName, - aName, - aType, - aSubTypeList, - aPort, - aTxtData, - std::move(aCallback), - aPublisher) - , mServiceRef(aServiceRef) - { - } + using ServiceRegistration::ServiceRegistration; // Inherit base constructor - ~DnssdServiceRegistration(void) override; - const DNSServiceRef &GetServiceRef() const { return mServiceRef; } - PublisherMDnsSd &GetPublisher(void) { return *static_cast(mPublisher); } + ~DnssdServiceRegistration(void) override { Unregister(); } + + void Update(MainloopContext &aMainloop) const; + void Process(const MainloopContext &aMainloop, std::vector &aReadyServices) const; + otbrError Register(void); private: - DNSServiceRef mServiceRef; + void Unregister(void); + PublisherMDnsSd &GetPublisher(void) { return *static_cast(mPublisher); } + void HandleRegisterResult(DNSServiceFlags aFlags, DNSServiceErrorType aError); + static void HandleRegisterResult(DNSServiceRef aServiceRef, + DNSServiceFlags aFlags, + DNSServiceErrorType aError, + const char *aName, + const char *aType, + const char *aDomain, + void *aContext); + + DNSServiceRef mServiceRef = nullptr; }; class DnssdHostRegistration : public HostRegistration { public: - DnssdHostRegistration(const std::string &aName, - const AddressList &aAddresses, - ResultCallback &&aCallback, - DNSServiceRef aServiceRef, - Publisher *aPublisher) - : HostRegistration(aName, aAddresses, std::move(aCallback), aPublisher) - , mServiceRef(aServiceRef) - , mRecordRefMap() - , mCallbackCount(aAddresses.size()) - { - } + using HostRegistration::HostRegistration; // Inherit base class constructor - ~DnssdHostRegistration(void) override; - const DNSServiceRef &GetServiceRef() const { return mServiceRef; } - const std::map &GetRecordRefMap() const { return mRecordRefMap; } - std::map &GetRecordRefMap() { return mRecordRefMap; } + ~DnssdHostRegistration(void) override { Unregister(); } + + otbrError Register(void); private: + void Unregister(void); PublisherMDnsSd &GetPublisher(void) { return *static_cast(mPublisher); } - - DNSServiceRef mServiceRef; - - public: - std::map mRecordRefMap; - uint32_t mCallbackCount; + void HandleRegisterResult(DNSRecordRef aRecordRef, DNSServiceErrorType aError); + static void HandleRegisterResult(DNSServiceRef aServiceRef, + DNSRecordRef aRecordRef, + DNSServiceFlags aFlags, + DNSServiceErrorType aErrorCode, + void *aContext); + + std::vector mAddrRecordRefs; + std::vector mAddrRegistered; }; struct ServiceRef : private ::NonCopyable @@ -327,37 +314,12 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher using ServiceSubscriptionList = std::vector>; using HostSubscriptionList = std::vector>; - static void HandleServiceRegisterResult(DNSServiceRef aService, - const DNSServiceFlags aFlags, - DNSServiceErrorType aError, - const char *aName, - const char *aType, - const char *aDomain, - void *aContext); - void HandleServiceRegisterResult(DNSServiceRef aService, - const DNSServiceFlags aFlags, - DNSServiceErrorType aError, - const char *aName, - const char *aType, - const char *aDomain); - static void HandleRegisterHostResult(DNSServiceRef aHostsConnection, - DNSRecordRef aHostRecord, - DNSServiceFlags aFlags, - DNSServiceErrorType aErrorCode, - void *aContext); - void HandleRegisterHostResult(DNSServiceRef aHostsConnection, - DNSRecordRef aHostRecord, - DNSServiceFlags aFlags, - DNSServiceErrorType aErrorCode); - static std::string MakeRegType(const std::string &aType, SubTypeList aSubTypeList); - void Stop(StopMode aStopMode); - void DeallocateHostsRef(void); - void HandleServiceRefDeallocating(const DNSServiceRef &aServiceRef); - - ServiceRegistration *FindServiceRegistration(const DNSServiceRef &aServiceRef); - HostRegistration *FindHostRegistration(const DNSServiceRef &aServiceRef, const DNSRecordRef &aRecordRef); + void Stop(StopMode aStopMode); + DNSServiceErrorType CreateSharedHostsRef(void); + void DeallocateHostsRef(void); + void HandleServiceRefDeallocating(const DNSServiceRef &aServiceRef); DNSServiceRef mHostsRef; State mState;