xref: /aosp_15_r20/external/ot-br-posix/src/mdns/mdns_mdnssd.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker /*
2*4a64e381SAndroid Build Coastguard Worker  *    Copyright (c) 2018, The OpenThread Authors.
3*4a64e381SAndroid Build Coastguard Worker  *    All rights reserved.
4*4a64e381SAndroid Build Coastguard Worker  *
5*4a64e381SAndroid Build Coastguard Worker  *    Redistribution and use in source and binary forms, with or without
6*4a64e381SAndroid Build Coastguard Worker  *    modification, are permitted provided that the following conditions are met:
7*4a64e381SAndroid Build Coastguard Worker  *    1. Redistributions of source code must retain the above copyright
8*4a64e381SAndroid Build Coastguard Worker  *       notice, this list of conditions and the following disclaimer.
9*4a64e381SAndroid Build Coastguard Worker  *    2. Redistributions in binary form must reproduce the above copyright
10*4a64e381SAndroid Build Coastguard Worker  *       notice, this list of conditions and the following disclaimer in the
11*4a64e381SAndroid Build Coastguard Worker  *       documentation and/or other materials provided with the distribution.
12*4a64e381SAndroid Build Coastguard Worker  *    3. Neither the name of the copyright holder nor the
13*4a64e381SAndroid Build Coastguard Worker  *       names of its contributors may be used to endorse or promote products
14*4a64e381SAndroid Build Coastguard Worker  *       derived from this software without specific prior written permission.
15*4a64e381SAndroid Build Coastguard Worker  *
16*4a64e381SAndroid Build Coastguard Worker  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*4a64e381SAndroid Build Coastguard Worker  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*4a64e381SAndroid Build Coastguard Worker  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*4a64e381SAndroid Build Coastguard Worker  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*4a64e381SAndroid Build Coastguard Worker  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*4a64e381SAndroid Build Coastguard Worker  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*4a64e381SAndroid Build Coastguard Worker  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*4a64e381SAndroid Build Coastguard Worker  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*4a64e381SAndroid Build Coastguard Worker  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*4a64e381SAndroid Build Coastguard Worker  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*4a64e381SAndroid Build Coastguard Worker  *    POSSIBILITY OF SUCH DAMAGE.
27*4a64e381SAndroid Build Coastguard Worker  */
28*4a64e381SAndroid Build Coastguard Worker 
29*4a64e381SAndroid Build Coastguard Worker /**
30*4a64e381SAndroid Build Coastguard Worker  * @file
31*4a64e381SAndroid Build Coastguard Worker  *   This file includes definition for mDNS publisher.
32*4a64e381SAndroid Build Coastguard Worker  */
33*4a64e381SAndroid Build Coastguard Worker 
34*4a64e381SAndroid Build Coastguard Worker #ifndef OTBR_AGENT_MDNS_MDNSSD_HPP_
35*4a64e381SAndroid Build Coastguard Worker #define OTBR_AGENT_MDNS_MDNSSD_HPP_
36*4a64e381SAndroid Build Coastguard Worker 
37*4a64e381SAndroid Build Coastguard Worker #include "openthread-br/config.h"
38*4a64e381SAndroid Build Coastguard Worker 
39*4a64e381SAndroid Build Coastguard Worker #include <array>
40*4a64e381SAndroid Build Coastguard Worker #include <map>
41*4a64e381SAndroid Build Coastguard Worker #include <memory>
42*4a64e381SAndroid Build Coastguard Worker #include <utility>
43*4a64e381SAndroid Build Coastguard Worker #include <vector>
44*4a64e381SAndroid Build Coastguard Worker 
45*4a64e381SAndroid Build Coastguard Worker #include <assert.h>
46*4a64e381SAndroid Build Coastguard Worker #include <dns_sd.h>
47*4a64e381SAndroid Build Coastguard Worker 
48*4a64e381SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
49*4a64e381SAndroid Build Coastguard Worker #include "common/mainloop.hpp"
50*4a64e381SAndroid Build Coastguard Worker #include "common/types.hpp"
51*4a64e381SAndroid Build Coastguard Worker #include "mdns/mdns.hpp"
52*4a64e381SAndroid Build Coastguard Worker 
53*4a64e381SAndroid Build Coastguard Worker namespace otbr {
54*4a64e381SAndroid Build Coastguard Worker 
55*4a64e381SAndroid Build Coastguard Worker namespace Mdns {
56*4a64e381SAndroid Build Coastguard Worker 
57*4a64e381SAndroid Build Coastguard Worker /**
58*4a64e381SAndroid Build Coastguard Worker  * This class implements mDNS publisher with mDNSResponder.
59*4a64e381SAndroid Build Coastguard Worker  */
60*4a64e381SAndroid Build Coastguard Worker class PublisherMDnsSd : public MainloopProcessor, public Publisher
61*4a64e381SAndroid Build Coastguard Worker {
62*4a64e381SAndroid Build Coastguard Worker public:
63*4a64e381SAndroid Build Coastguard Worker     explicit PublisherMDnsSd(StateCallback aCallback);
64*4a64e381SAndroid Build Coastguard Worker 
65*4a64e381SAndroid Build Coastguard Worker     ~PublisherMDnsSd(void) override;
66*4a64e381SAndroid Build Coastguard Worker 
67*4a64e381SAndroid Build Coastguard Worker     // Implementation of Mdns::Publisher.
68*4a64e381SAndroid Build Coastguard Worker 
69*4a64e381SAndroid Build Coastguard Worker     void UnpublishService(const std::string &aName, const std::string &aType, ResultCallback &&aCallback) override;
70*4a64e381SAndroid Build Coastguard Worker 
71*4a64e381SAndroid Build Coastguard Worker     void      UnpublishHost(const std::string &aName, ResultCallback &&aCallback) override;
72*4a64e381SAndroid Build Coastguard Worker     void      UnpublishKey(const std::string &aName, ResultCallback &&aCallback) override;
73*4a64e381SAndroid Build Coastguard Worker     void      SubscribeService(const std::string &aType, const std::string &aInstanceName) override;
74*4a64e381SAndroid Build Coastguard Worker     void      UnsubscribeService(const std::string &aType, const std::string &aInstanceName) override;
75*4a64e381SAndroid Build Coastguard Worker     void      SubscribeHost(const std::string &aHostName) override;
76*4a64e381SAndroid Build Coastguard Worker     void      UnsubscribeHost(const std::string &aHostName) override;
77*4a64e381SAndroid Build Coastguard Worker     otbrError Start(void) override;
78*4a64e381SAndroid Build Coastguard Worker     bool      IsStarted(void) const override;
Stop(void)79*4a64e381SAndroid Build Coastguard Worker     void      Stop(void) override { Stop(kNormalStop); }
80*4a64e381SAndroid Build Coastguard Worker 
81*4a64e381SAndroid Build Coastguard Worker     // Implementation of MainloopProcessor.
82*4a64e381SAndroid Build Coastguard Worker 
83*4a64e381SAndroid Build Coastguard Worker     void Update(MainloopContext &aMainloop) override;
84*4a64e381SAndroid Build Coastguard Worker     void Process(const MainloopContext &aMainloop) override;
85*4a64e381SAndroid Build Coastguard Worker 
86*4a64e381SAndroid Build Coastguard Worker protected:
87*4a64e381SAndroid Build Coastguard Worker     otbrError PublishServiceImpl(const std::string &aHostName,
88*4a64e381SAndroid Build Coastguard Worker                                  const std::string &aName,
89*4a64e381SAndroid Build Coastguard Worker                                  const std::string &aType,
90*4a64e381SAndroid Build Coastguard Worker                                  const SubTypeList &aSubTypeList,
91*4a64e381SAndroid Build Coastguard Worker                                  uint16_t           aPort,
92*4a64e381SAndroid Build Coastguard Worker                                  const TxtData     &aTxtData,
93*4a64e381SAndroid Build Coastguard Worker                                  ResultCallback   &&aCallback) override;
94*4a64e381SAndroid Build Coastguard Worker     otbrError PublishHostImpl(const std::string &aName,
95*4a64e381SAndroid Build Coastguard Worker                               const AddressList &aAddress,
96*4a64e381SAndroid Build Coastguard Worker                               ResultCallback   &&aCallback) override;
97*4a64e381SAndroid Build Coastguard Worker     otbrError PublishKeyImpl(const std::string &aName, const KeyData &aKeyData, ResultCallback &&aCallback) override;
98*4a64e381SAndroid Build Coastguard Worker     void      OnServiceResolveFailedImpl(const std::string &aType,
99*4a64e381SAndroid Build Coastguard Worker                                          const std::string &aInstanceName,
100*4a64e381SAndroid Build Coastguard Worker                                          int32_t            aErrorCode) override;
101*4a64e381SAndroid Build Coastguard Worker     void      OnHostResolveFailedImpl(const std::string &aHostName, int32_t aErrorCode) override;
102*4a64e381SAndroid Build Coastguard Worker     otbrError DnsErrorToOtbrError(int32_t aErrorCode) override;
103*4a64e381SAndroid Build Coastguard Worker 
104*4a64e381SAndroid Build Coastguard Worker private:
105*4a64e381SAndroid Build Coastguard Worker     static constexpr uint32_t kDefaultTtl = 10;
106*4a64e381SAndroid Build Coastguard Worker 
107*4a64e381SAndroid Build Coastguard Worker     enum StopMode : uint8_t
108*4a64e381SAndroid Build Coastguard Worker     {
109*4a64e381SAndroid Build Coastguard Worker         kNormalStop,
110*4a64e381SAndroid Build Coastguard Worker         kStopOnServiceNotRunningError,
111*4a64e381SAndroid Build Coastguard Worker     };
112*4a64e381SAndroid Build Coastguard Worker 
113*4a64e381SAndroid Build Coastguard Worker     class DnssdKeyRegistration;
114*4a64e381SAndroid Build Coastguard Worker 
115*4a64e381SAndroid Build Coastguard Worker     class DnssdServiceRegistration : public ServiceRegistration
116*4a64e381SAndroid Build Coastguard Worker     {
117*4a64e381SAndroid Build Coastguard Worker         friend class DnssdKeyRegistration;
118*4a64e381SAndroid Build Coastguard Worker 
119*4a64e381SAndroid Build Coastguard Worker     public:
120*4a64e381SAndroid Build Coastguard Worker         using ServiceRegistration::ServiceRegistration; // Inherit base constructor
121*4a64e381SAndroid Build Coastguard Worker 
~DnssdServiceRegistration(void)122*4a64e381SAndroid Build Coastguard Worker         ~DnssdServiceRegistration(void) override { Unregister(); }
123*4a64e381SAndroid Build Coastguard Worker 
124*4a64e381SAndroid Build Coastguard Worker         void      Update(MainloopContext &aMainloop) const;
125*4a64e381SAndroid Build Coastguard Worker         void      Process(const MainloopContext &aMainloop, std::vector<DNSServiceRef> &aReadyServices) const;
126*4a64e381SAndroid Build Coastguard Worker         otbrError Register(void);
127*4a64e381SAndroid Build Coastguard Worker 
128*4a64e381SAndroid Build Coastguard Worker     private:
129*4a64e381SAndroid Build Coastguard Worker         void             Unregister(void);
GetPublisher(void)130*4a64e381SAndroid Build Coastguard Worker         PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }
131*4a64e381SAndroid Build Coastguard Worker         void             HandleRegisterResult(DNSServiceFlags aFlags, DNSServiceErrorType aError);
132*4a64e381SAndroid Build Coastguard Worker         static void      HandleRegisterResult(DNSServiceRef       aServiceRef,
133*4a64e381SAndroid Build Coastguard Worker                                               DNSServiceFlags     aFlags,
134*4a64e381SAndroid Build Coastguard Worker                                               DNSServiceErrorType aError,
135*4a64e381SAndroid Build Coastguard Worker                                               const char         *aName,
136*4a64e381SAndroid Build Coastguard Worker                                               const char         *aType,
137*4a64e381SAndroid Build Coastguard Worker                                               const char         *aDomain,
138*4a64e381SAndroid Build Coastguard Worker                                               void               *aContext);
139*4a64e381SAndroid Build Coastguard Worker 
140*4a64e381SAndroid Build Coastguard Worker         DNSServiceRef         mServiceRef    = nullptr;
141*4a64e381SAndroid Build Coastguard Worker         DnssdKeyRegistration *mRelatedKeyReg = nullptr;
142*4a64e381SAndroid Build Coastguard Worker     };
143*4a64e381SAndroid Build Coastguard Worker 
144*4a64e381SAndroid Build Coastguard Worker     class DnssdHostRegistration : public HostRegistration
145*4a64e381SAndroid Build Coastguard Worker     {
146*4a64e381SAndroid Build Coastguard Worker     public:
147*4a64e381SAndroid Build Coastguard Worker         using HostRegistration::HostRegistration; // Inherit base class constructor
148*4a64e381SAndroid Build Coastguard Worker 
~DnssdHostRegistration(void)149*4a64e381SAndroid Build Coastguard Worker         ~DnssdHostRegistration(void) override { Unregister(); }
150*4a64e381SAndroid Build Coastguard Worker 
151*4a64e381SAndroid Build Coastguard Worker         otbrError Register(void);
152*4a64e381SAndroid Build Coastguard Worker 
153*4a64e381SAndroid Build Coastguard Worker     private:
154*4a64e381SAndroid Build Coastguard Worker         void             Unregister(void);
GetPublisher(void)155*4a64e381SAndroid Build Coastguard Worker         PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }
156*4a64e381SAndroid Build Coastguard Worker         void             HandleRegisterResult(DNSRecordRef aRecordRef, DNSServiceErrorType aError);
157*4a64e381SAndroid Build Coastguard Worker         static void      HandleRegisterResult(DNSServiceRef       aServiceRef,
158*4a64e381SAndroid Build Coastguard Worker                                               DNSRecordRef        aRecordRef,
159*4a64e381SAndroid Build Coastguard Worker                                               DNSServiceFlags     aFlags,
160*4a64e381SAndroid Build Coastguard Worker                                               DNSServiceErrorType aErrorCode,
161*4a64e381SAndroid Build Coastguard Worker                                               void               *aContext);
162*4a64e381SAndroid Build Coastguard Worker 
163*4a64e381SAndroid Build Coastguard Worker         std::vector<DNSRecordRef> mAddrRecordRefs;
164*4a64e381SAndroid Build Coastguard Worker         std::vector<bool>         mAddrRegistered;
165*4a64e381SAndroid Build Coastguard Worker     };
166*4a64e381SAndroid Build Coastguard Worker 
167*4a64e381SAndroid Build Coastguard Worker     class DnssdKeyRegistration : public KeyRegistration
168*4a64e381SAndroid Build Coastguard Worker     {
169*4a64e381SAndroid Build Coastguard Worker         friend class DnssdServiceRegistration;
170*4a64e381SAndroid Build Coastguard Worker 
171*4a64e381SAndroid Build Coastguard Worker     public:
172*4a64e381SAndroid Build Coastguard Worker         using KeyRegistration::KeyRegistration; // Inherit base class constructor
173*4a64e381SAndroid Build Coastguard Worker 
~DnssdKeyRegistration(void)174*4a64e381SAndroid Build Coastguard Worker         ~DnssdKeyRegistration(void) override { Unregister(); }
175*4a64e381SAndroid Build Coastguard Worker 
176*4a64e381SAndroid Build Coastguard Worker         otbrError Register(void);
177*4a64e381SAndroid Build Coastguard Worker 
178*4a64e381SAndroid Build Coastguard Worker     private:
179*4a64e381SAndroid Build Coastguard Worker         void             Unregister(void);
GetPublisher(void)180*4a64e381SAndroid Build Coastguard Worker         PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }
181*4a64e381SAndroid Build Coastguard Worker         void             HandleRegisterResult(DNSServiceErrorType aError);
182*4a64e381SAndroid Build Coastguard Worker         static void      HandleRegisterResult(DNSServiceRef       aServiceRef,
183*4a64e381SAndroid Build Coastguard Worker                                               DNSRecordRef        aRecordRef,
184*4a64e381SAndroid Build Coastguard Worker                                               DNSServiceFlags     aFlags,
185*4a64e381SAndroid Build Coastguard Worker                                               DNSServiceErrorType aErrorCode,
186*4a64e381SAndroid Build Coastguard Worker                                               void               *aContext);
187*4a64e381SAndroid Build Coastguard Worker 
188*4a64e381SAndroid Build Coastguard Worker         DNSRecordRef              mRecordRef         = nullptr;
189*4a64e381SAndroid Build Coastguard Worker         DnssdServiceRegistration *mRelatedServiceReg = nullptr;
190*4a64e381SAndroid Build Coastguard Worker     };
191*4a64e381SAndroid Build Coastguard Worker 
192*4a64e381SAndroid Build Coastguard Worker     struct ServiceRef : private ::NonCopyable
193*4a64e381SAndroid Build Coastguard Worker     {
194*4a64e381SAndroid Build Coastguard Worker         DNSServiceRef    mServiceRef;
195*4a64e381SAndroid Build Coastguard Worker         PublisherMDnsSd &mPublisher;
196*4a64e381SAndroid Build Coastguard Worker 
ServiceRefotbr::Mdns::PublisherMDnsSd::ServiceRef197*4a64e381SAndroid Build Coastguard Worker         explicit ServiceRef(PublisherMDnsSd &aPublisher)
198*4a64e381SAndroid Build Coastguard Worker             : mServiceRef(nullptr)
199*4a64e381SAndroid Build Coastguard Worker             , mPublisher(aPublisher)
200*4a64e381SAndroid Build Coastguard Worker         {
201*4a64e381SAndroid Build Coastguard Worker         }
202*4a64e381SAndroid Build Coastguard Worker 
~ServiceRefotbr::Mdns::PublisherMDnsSd::ServiceRef203*4a64e381SAndroid Build Coastguard Worker         ~ServiceRef() { Release(); }
204*4a64e381SAndroid Build Coastguard Worker 
205*4a64e381SAndroid Build Coastguard Worker         void Update(MainloopContext &aMainloop) const;
206*4a64e381SAndroid Build Coastguard Worker         void Process(const MainloopContext &aMainloop, std::vector<DNSServiceRef> &aReadyServices) const;
207*4a64e381SAndroid Build Coastguard Worker         void Release(void);
208*4a64e381SAndroid Build Coastguard Worker         void DeallocateServiceRef(void);
209*4a64e381SAndroid Build Coastguard Worker     };
210*4a64e381SAndroid Build Coastguard Worker 
211*4a64e381SAndroid Build Coastguard Worker     struct ServiceSubscription;
212*4a64e381SAndroid Build Coastguard Worker 
213*4a64e381SAndroid Build Coastguard Worker     struct ServiceInstanceResolution : public ServiceRef
214*4a64e381SAndroid Build Coastguard Worker     {
ServiceInstanceResolutionotbr::Mdns::PublisherMDnsSd::ServiceInstanceResolution215*4a64e381SAndroid Build Coastguard Worker         explicit ServiceInstanceResolution(ServiceSubscription &aSubscription,
216*4a64e381SAndroid Build Coastguard Worker                                            std::string          aInstanceName,
217*4a64e381SAndroid Build Coastguard Worker                                            std::string          aType,
218*4a64e381SAndroid Build Coastguard Worker                                            std::string          aDomain,
219*4a64e381SAndroid Build Coastguard Worker                                            uint32_t             aNetifIndex)
220*4a64e381SAndroid Build Coastguard Worker             : ServiceRef(aSubscription.mPublisher)
221*4a64e381SAndroid Build Coastguard Worker             , mSubscription(&aSubscription)
222*4a64e381SAndroid Build Coastguard Worker             , mInstanceName(std::move(aInstanceName))
223*4a64e381SAndroid Build Coastguard Worker             , mType(std::move(aType))
224*4a64e381SAndroid Build Coastguard Worker             , mDomain(std::move(aDomain))
225*4a64e381SAndroid Build Coastguard Worker             , mNetifIndex(aNetifIndex)
226*4a64e381SAndroid Build Coastguard Worker         {
227*4a64e381SAndroid Build Coastguard Worker         }
228*4a64e381SAndroid Build Coastguard Worker 
229*4a64e381SAndroid Build Coastguard Worker         void      Resolve(void);
230*4a64e381SAndroid Build Coastguard Worker         otbrError GetAddrInfo(uint32_t aInterfaceIndex);
231*4a64e381SAndroid Build Coastguard Worker         void      FinishResolution(void);
232*4a64e381SAndroid Build Coastguard Worker 
233*4a64e381SAndroid Build Coastguard Worker         static void HandleResolveResult(DNSServiceRef        aServiceRef,
234*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceFlags      aFlags,
235*4a64e381SAndroid Build Coastguard Worker                                         uint32_t             aInterfaceIndex,
236*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceErrorType  aErrorCode,
237*4a64e381SAndroid Build Coastguard Worker                                         const char          *aFullName,
238*4a64e381SAndroid Build Coastguard Worker                                         const char          *aHostTarget,
239*4a64e381SAndroid Build Coastguard Worker                                         uint16_t             aPort, // In network byte order.
240*4a64e381SAndroid Build Coastguard Worker                                         uint16_t             aTxtLen,
241*4a64e381SAndroid Build Coastguard Worker                                         const unsigned char *aTxtRecord,
242*4a64e381SAndroid Build Coastguard Worker                                         void                *aContext);
243*4a64e381SAndroid Build Coastguard Worker         void        HandleResolveResult(DNSServiceRef        aServiceRef,
244*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceFlags      aFlags,
245*4a64e381SAndroid Build Coastguard Worker                                         uint32_t             aInterfaceIndex,
246*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceErrorType  aErrorCode,
247*4a64e381SAndroid Build Coastguard Worker                                         const char          *aFullName,
248*4a64e381SAndroid Build Coastguard Worker                                         const char          *aHostTarget,
249*4a64e381SAndroid Build Coastguard Worker                                         uint16_t             aPort, // In network byte order.
250*4a64e381SAndroid Build Coastguard Worker                                         uint16_t             aTxtLen,
251*4a64e381SAndroid Build Coastguard Worker                                         const unsigned char *aTxtRecord);
252*4a64e381SAndroid Build Coastguard Worker         static void HandleGetAddrInfoResult(DNSServiceRef          aServiceRef,
253*4a64e381SAndroid Build Coastguard Worker                                             DNSServiceFlags        aFlags,
254*4a64e381SAndroid Build Coastguard Worker                                             uint32_t               aInterfaceIndex,
255*4a64e381SAndroid Build Coastguard Worker                                             DNSServiceErrorType    aErrorCode,
256*4a64e381SAndroid Build Coastguard Worker                                             const char            *aHostName,
257*4a64e381SAndroid Build Coastguard Worker                                             const struct sockaddr *aAddress,
258*4a64e381SAndroid Build Coastguard Worker                                             uint32_t               aTtl,
259*4a64e381SAndroid Build Coastguard Worker                                             void                  *aContext);
260*4a64e381SAndroid Build Coastguard Worker         void        HandleGetAddrInfoResult(DNSServiceRef          aServiceRef,
261*4a64e381SAndroid Build Coastguard Worker                                             DNSServiceFlags        aFlags,
262*4a64e381SAndroid Build Coastguard Worker                                             uint32_t               aInterfaceIndex,
263*4a64e381SAndroid Build Coastguard Worker                                             DNSServiceErrorType    aErrorCode,
264*4a64e381SAndroid Build Coastguard Worker                                             const char            *aHostName,
265*4a64e381SAndroid Build Coastguard Worker                                             const struct sockaddr *aAddress,
266*4a64e381SAndroid Build Coastguard Worker                                             uint32_t               aTtl);
267*4a64e381SAndroid Build Coastguard Worker 
268*4a64e381SAndroid Build Coastguard Worker         ServiceSubscription   *mSubscription;
269*4a64e381SAndroid Build Coastguard Worker         std::string            mInstanceName;
270*4a64e381SAndroid Build Coastguard Worker         std::string            mType;
271*4a64e381SAndroid Build Coastguard Worker         std::string            mDomain;
272*4a64e381SAndroid Build Coastguard Worker         uint32_t               mNetifIndex;
273*4a64e381SAndroid Build Coastguard Worker         DiscoveredInstanceInfo mInstanceInfo;
274*4a64e381SAndroid Build Coastguard Worker     };
275*4a64e381SAndroid Build Coastguard Worker 
276*4a64e381SAndroid Build Coastguard Worker     struct ServiceSubscription : public ServiceRef
277*4a64e381SAndroid Build Coastguard Worker     {
ServiceSubscriptionotbr::Mdns::PublisherMDnsSd::ServiceSubscription278*4a64e381SAndroid Build Coastguard Worker         explicit ServiceSubscription(PublisherMDnsSd &aPublisher, std::string aType, std::string aInstanceName)
279*4a64e381SAndroid Build Coastguard Worker             : ServiceRef(aPublisher)
280*4a64e381SAndroid Build Coastguard Worker             , mType(std::move(aType))
281*4a64e381SAndroid Build Coastguard Worker             , mInstanceName(std::move(aInstanceName))
282*4a64e381SAndroid Build Coastguard Worker         {
283*4a64e381SAndroid Build Coastguard Worker         }
284*4a64e381SAndroid Build Coastguard Worker 
285*4a64e381SAndroid Build Coastguard Worker         void Browse(void);
286*4a64e381SAndroid Build Coastguard Worker         void Resolve(uint32_t           aNetifIndex,
287*4a64e381SAndroid Build Coastguard Worker                      const std::string &aInstanceName,
288*4a64e381SAndroid Build Coastguard Worker                      const std::string &aType,
289*4a64e381SAndroid Build Coastguard Worker                      const std::string &aDomain);
290*4a64e381SAndroid Build Coastguard Worker         void UpdateAll(MainloopContext &aMainloop) const;
291*4a64e381SAndroid Build Coastguard Worker         void ProcessAll(const MainloopContext &aMainloop, std::vector<DNSServiceRef> &aReadyServices) const;
292*4a64e381SAndroid Build Coastguard Worker 
293*4a64e381SAndroid Build Coastguard Worker         static void HandleBrowseResult(DNSServiceRef       aServiceRef,
294*4a64e381SAndroid Build Coastguard Worker                                        DNSServiceFlags     aFlags,
295*4a64e381SAndroid Build Coastguard Worker                                        uint32_t            aInterfaceIndex,
296*4a64e381SAndroid Build Coastguard Worker                                        DNSServiceErrorType aErrorCode,
297*4a64e381SAndroid Build Coastguard Worker                                        const char         *aInstanceName,
298*4a64e381SAndroid Build Coastguard Worker                                        const char         *aType,
299*4a64e381SAndroid Build Coastguard Worker                                        const char         *aDomain,
300*4a64e381SAndroid Build Coastguard Worker                                        void               *aContext);
301*4a64e381SAndroid Build Coastguard Worker         void        HandleBrowseResult(DNSServiceRef       aServiceRef,
302*4a64e381SAndroid Build Coastguard Worker                                        DNSServiceFlags     aFlags,
303*4a64e381SAndroid Build Coastguard Worker                                        uint32_t            aInterfaceIndex,
304*4a64e381SAndroid Build Coastguard Worker                                        DNSServiceErrorType aErrorCode,
305*4a64e381SAndroid Build Coastguard Worker                                        const char         *aInstanceName,
306*4a64e381SAndroid Build Coastguard Worker                                        const char         *aType,
307*4a64e381SAndroid Build Coastguard Worker                                        const char         *aDomain);
308*4a64e381SAndroid Build Coastguard Worker 
309*4a64e381SAndroid Build Coastguard Worker         std::string mType;
310*4a64e381SAndroid Build Coastguard Worker         std::string mInstanceName;
311*4a64e381SAndroid Build Coastguard Worker 
312*4a64e381SAndroid Build Coastguard Worker         std::vector<std::unique_ptr<ServiceInstanceResolution>> mResolvingInstances;
313*4a64e381SAndroid Build Coastguard Worker     };
314*4a64e381SAndroid Build Coastguard Worker 
315*4a64e381SAndroid Build Coastguard Worker     struct HostSubscription : public ServiceRef
316*4a64e381SAndroid Build Coastguard Worker     {
HostSubscriptionotbr::Mdns::PublisherMDnsSd::HostSubscription317*4a64e381SAndroid Build Coastguard Worker         explicit HostSubscription(PublisherMDnsSd &aPublisher, std::string aHostName)
318*4a64e381SAndroid Build Coastguard Worker             : ServiceRef(aPublisher)
319*4a64e381SAndroid Build Coastguard Worker             , mHostName(std::move(aHostName))
320*4a64e381SAndroid Build Coastguard Worker         {
321*4a64e381SAndroid Build Coastguard Worker         }
322*4a64e381SAndroid Build Coastguard Worker 
323*4a64e381SAndroid Build Coastguard Worker         void        Resolve(void);
324*4a64e381SAndroid Build Coastguard Worker         static void HandleResolveResult(DNSServiceRef          aServiceRef,
325*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceFlags        aFlags,
326*4a64e381SAndroid Build Coastguard Worker                                         uint32_t               aInterfaceIndex,
327*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceErrorType    aErrorCode,
328*4a64e381SAndroid Build Coastguard Worker                                         const char            *aHostName,
329*4a64e381SAndroid Build Coastguard Worker                                         const struct sockaddr *aAddress,
330*4a64e381SAndroid Build Coastguard Worker                                         uint32_t               aTtl,
331*4a64e381SAndroid Build Coastguard Worker                                         void                  *aContext);
332*4a64e381SAndroid Build Coastguard Worker         void        HandleResolveResult(DNSServiceRef          aServiceRef,
333*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceFlags        aFlags,
334*4a64e381SAndroid Build Coastguard Worker                                         uint32_t               aInterfaceIndex,
335*4a64e381SAndroid Build Coastguard Worker                                         DNSServiceErrorType    aErrorCode,
336*4a64e381SAndroid Build Coastguard Worker                                         const char            *aHostName,
337*4a64e381SAndroid Build Coastguard Worker                                         const struct sockaddr *aAddress,
338*4a64e381SAndroid Build Coastguard Worker                                         uint32_t               aTtl);
339*4a64e381SAndroid Build Coastguard Worker 
340*4a64e381SAndroid Build Coastguard Worker         std::string        mHostName;
341*4a64e381SAndroid Build Coastguard Worker         DiscoveredHostInfo mHostInfo;
342*4a64e381SAndroid Build Coastguard Worker     };
343*4a64e381SAndroid Build Coastguard Worker 
344*4a64e381SAndroid Build Coastguard Worker     using ServiceSubscriptionList = std::vector<std::unique_ptr<ServiceSubscription>>;
345*4a64e381SAndroid Build Coastguard Worker     using HostSubscriptionList    = std::vector<std::unique_ptr<HostSubscription>>;
346*4a64e381SAndroid Build Coastguard Worker 
347*4a64e381SAndroid Build Coastguard Worker     static std::string MakeRegType(const std::string &aType, SubTypeList aSubTypeList);
348*4a64e381SAndroid Build Coastguard Worker 
349*4a64e381SAndroid Build Coastguard Worker     void                Stop(StopMode aStopMode);
350*4a64e381SAndroid Build Coastguard Worker     DNSServiceErrorType CreateSharedHostsRef(void);
351*4a64e381SAndroid Build Coastguard Worker     void                DeallocateHostsRef(void);
352*4a64e381SAndroid Build Coastguard Worker     void                HandleServiceRefDeallocating(const DNSServiceRef &aServiceRef);
353*4a64e381SAndroid Build Coastguard Worker 
354*4a64e381SAndroid Build Coastguard Worker     DNSServiceRef mHostsRef;
355*4a64e381SAndroid Build Coastguard Worker     State         mState;
356*4a64e381SAndroid Build Coastguard Worker     StateCallback mStateCallback;
357*4a64e381SAndroid Build Coastguard Worker 
358*4a64e381SAndroid Build Coastguard Worker     ServiceSubscriptionList mSubscribedServices;
359*4a64e381SAndroid Build Coastguard Worker     HostSubscriptionList    mSubscribedHosts;
360*4a64e381SAndroid Build Coastguard Worker 
361*4a64e381SAndroid Build Coastguard Worker     std::vector<DNSServiceRef> mServiceRefsToProcess;
362*4a64e381SAndroid Build Coastguard Worker };
363*4a64e381SAndroid Build Coastguard Worker 
364*4a64e381SAndroid Build Coastguard Worker /**
365*4a64e381SAndroid Build Coastguard Worker  * @}
366*4a64e381SAndroid Build Coastguard Worker  */
367*4a64e381SAndroid Build Coastguard Worker 
368*4a64e381SAndroid Build Coastguard Worker } // namespace Mdns
369*4a64e381SAndroid Build Coastguard Worker 
370*4a64e381SAndroid Build Coastguard Worker } // namespace otbr
371*4a64e381SAndroid Build Coastguard Worker 
372*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_AGENT_MDNS_MDNSSD_HPP_
373