• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef DNS_SERVER_HPP_
30 #define DNS_SERVER_HPP_
31 
32 #include "openthread-core-config.h"
33 
34 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
35 
36 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
37 
38 #if !OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE && !OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE
39 #error "OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE requires either PLATFORM_DNSSD_ENABLE or MULTICAST_DNS_ENABLE"
40 #endif
41 #if !OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
42 #error "OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE requires OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE"
43 #endif
44 
45 #endif // OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
46 
47 #include <openthread/dnssd_server.h>
48 #include <openthread/platform/dns.h>
49 
50 #include "border_router/infra_if.hpp"
51 #include "common/as_core_type.hpp"
52 #include "common/callback.hpp"
53 #include "common/equatable.hpp"
54 #include "common/message.hpp"
55 #include "common/non_copyable.hpp"
56 #include "common/owned_ptr.hpp"
57 #include "common/timer.hpp"
58 #include "net/dns_types.hpp"
59 #include "net/dnssd.hpp"
60 #include "net/ip6.hpp"
61 #include "net/netif.hpp"
62 #include "net/srp_server.hpp"
63 
64 /**
65  * @file
66  *   This file includes definitions for the DNS-SD server.
67  */
68 
69 struct otPlatDnsUpstreamQuery
70 {
71 };
72 
73 namespace ot {
74 
75 namespace Srp {
76 class Server;
77 }
78 
79 namespace Dns {
80 namespace ServiceDiscovery {
81 
82 /**
83  * Implements DNS-SD server.
84  */
85 class Server : public InstanceLocator, private NonCopyable
86 {
87     friend class Srp::Server;
88 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
89     friend class ot::Dnssd;
90     friend class ot::BorderRouter::InfraIf;
91 #endif
92 
93 public:
94     /**
95      * Contains the counters of the DNS-SD server.
96      */
97     class Counters : public otDnssdCounters, public Clearable<Counters>
98     {
99     public:
100         /**
101          * Returns the total number of processed queries (successful or failed responses).
102          *
103          * @return The total number of queries.
104          */
GetTotalQueries(void) const105         uint32_t GetTotalQueries(void) const { return mSuccessResponse + GetTotalFailedQueries(); }
106 
107         /**
108          * Returns the total number of failed queries (any error response code).
109          *
110          * @return The total number of failed queries.
111          */
GetTotalFailedQueries(void) const112         uint32_t GetTotalFailedQueries(void) const
113         {
114             return mServerFailureResponse + mFormatErrorResponse + mNameErrorResponse + mNotImplementedResponse +
115                    mOtherResponse;
116         }
117     };
118 
119 #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
120     /**
121      * Represents an upstream query transaction. The methods should only be used by
122      * `Dns::ServiceDiscovery::Server`.
123      */
124     class UpstreamQueryTransaction : public otPlatDnsUpstreamQuery
125     {
126     public:
127         /**
128          * Returns whether the transaction is valid.
129          *
130          * @retval  TRUE  The transaction is valid.
131          * @retval  FALSE The transaction is not valid.
132          */
IsValid(void) const133         bool IsValid(void) const { return mValid; }
134 
135         /**
136          * Returns the time when the transaction expires.
137          *
138          * @returns The expire time of the transaction.
139          */
GetExpireTime(void) const140         TimeMilli GetExpireTime(void) const { return mExpireTime; }
141 
142         /**
143          * Resets the transaction with a reason. The transaction will be invalid and can be reused for
144          * another upstream query after this call.
145          */
Reset(void)146         void Reset(void) { mValid = false; }
147 
148         /**
149          * Initializes the transaction.
150          *
151          * @param[in] aMessageInfo  The IP message info of the query.
152          */
153         void Init(const Ip6::MessageInfo &aMessageInfo);
154 
155         /**
156          * Returns the message info of the query.
157          *
158          * @returns  The message info of the query.
159          */
GetMessageInfo(void) const160         const Ip6::MessageInfo &GetMessageInfo(void) const { return mMessageInfo; }
161 
162     private:
163         Ip6::MessageInfo mMessageInfo;
164         TimeMilli        mExpireTime;
165         bool             mValid;
166     };
167 #endif
168 
169     /**
170      * Specifies a DNS-SD query type.
171      */
172     enum DnsQueryType : uint8_t
173     {
174         kDnsQueryNone        = OT_DNSSD_QUERY_TYPE_NONE,         ///< Service type unspecified.
175         kDnsQueryBrowse      = OT_DNSSD_QUERY_TYPE_BROWSE,       ///< Service type browse service.
176         kDnsQueryResolve     = OT_DNSSD_QUERY_TYPE_RESOLVE,      ///< Service type resolve service instance.
177         kDnsQueryResolveHost = OT_DNSSD_QUERY_TYPE_RESOLVE_HOST, ///< Service type resolve hostname.
178     };
179 
180     typedef otDnssdServiceInstanceInfo ServiceInstanceInfo; ///< A discovered service instance for a DNS-SD query.
181     typedef otDnssdHostInfo            HostInfo;            ///< A discover host for a DNS-SD query.
182 
183     typedef otDnssdQuerySubscribeCallback   SubscribeCallback;
184     typedef otDnssdQueryUnsubscribeCallback UnsubscribeCallback;
185 
186     static constexpr uint16_t kPort = OPENTHREAD_CONFIG_DNSSD_SERVER_PORT; ///< The DNS-SD server port.
187 
188     /**
189      * Initializes the object.
190      *
191      * @param[in]  aInstance     A reference to the OpenThread instance.
192      */
193     explicit Server(Instance &aInstance);
194 
195     /**
196      * Starts the DNS-SD server.
197      *
198      * @retval kErrorNone     Successfully started the DNS-SD server.
199      * @retval kErrorFailed   If failed to open or bind the UDP socket.
200      */
201     Error Start(void);
202 
203     /**
204      * Stops the DNS-SD server.
205      */
206     void Stop(void);
207 
208     /**
209      * Sets DNS-SD query callbacks.
210      *
211      * @param[in] aSubscribe    A pointer to the callback function to subscribe a service or service instance.
212      * @param[in] aUnsubscribe  A pointer to the callback function to unsubscribe a service or service instance.
213      * @param[in] aContext      A pointer to the application-specific context.
214      */
215     void SetQueryCallbacks(SubscribeCallback aSubscribe, UnsubscribeCallback aUnsubscribe, void *aContext);
216 
217     /**
218      * Notifies a discovered service instance.
219      *
220      * @param[in] aServiceFullName  The null-terminated full service name.
221      * @param[in] aInstanceInfo     A reference to the discovered service instance information.
222      */
223     void HandleDiscoveredServiceInstance(const char *aServiceFullName, const ServiceInstanceInfo &aInstanceInfo);
224 
225 #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
226     /**
227      * Notifies an answer of an upstream DNS query.
228      *
229      * The Transaction will be released.
230      *
231      * @param[in] aQueryTransaction    A reference to upstream DNS query transaction.
232      * @param[in] aResponseMessage     A pointer to response UDP message, should be allocated from Udp::NewMessage.
233      *                                 Passing a nullptr means close the transaction without a response.
234      */
235     void OnUpstreamQueryDone(UpstreamQueryTransaction &aQueryTransaction, Message *aResponseMessage);
236 
237     /**
238      * Indicates whether the server will forward DNS queries to platform DNS upstream API.
239      *
240      * @retval TRUE  If the server will forward DNS queries.
241      * @retval FALSE If the server will not forward DNS queries.
242      */
IsUpstreamQueryEnabled(void) const243     bool IsUpstreamQueryEnabled(void) const { return mEnableUpstreamQuery; }
244 
245     /**
246      * Enables or disables forwarding DNS queries to platform DNS upstream API.
247      *
248      * @param[in]  aEnabled   A boolean to enable/disable forwarding DNS queries to upstream.
249      */
SetUpstreamQueryEnabled(bool aEnabled)250     void SetUpstreamQueryEnabled(bool aEnabled) { mEnableUpstreamQuery = aEnabled; }
251 #endif // OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
252 
253     /**
254      * Notifies a discovered host.
255      *
256      * @param[in] aHostFullName     The null-terminated full host name.
257      * @param[in] aHostInfo         A reference to the discovered host information.
258      */
259     void HandleDiscoveredHost(const char *aHostFullName, const HostInfo &aHostInfo);
260 
261     /**
262      * Acquires the next query in the server.
263      *
264      * @param[in] aQuery            The query pointer. Pass `nullptr` to get the first query.
265      *
266      * @returns  A pointer to the query or `nullptr` if no more queries.
267      */
268     const otDnssdQuery *GetNextQuery(const otDnssdQuery *aQuery) const;
269 
270     /**
271      * Acquires the DNS-SD query type and name for a specific query.
272      *
273      * @param[in]   aQuery      The query pointer.
274      * @param[out]  aName       The name output buffer.
275      *
276      * @returns The DNS-SD query type.
277      */
278     static DnsQueryType GetQueryTypeAndName(const otDnssdQuery *aQuery, Dns::Name::Buffer &aName);
279 
280     /**
281      * Returns the counters of the DNS-SD server.
282      *
283      * @returns  A reference to the `Counters` instance.
284      */
GetCounters(void) const285     const Counters &GetCounters(void) const { return mCounters; };
286 
287     /**
288      * Represents different test mode flags for use in `SetTestMode()`.
289      */
290     enum TestModeFlags : uint8_t
291     {
292         kTestModeRejectMultiQuestionQuery = 1 << 0, ///< Send `FormatError` for a query with multiple questions.
293         kTestModeIgnoreMultiQuestionQuery = 1 << 1, ///< Ignore a query with multiple questions (send no response).
294         kTestModeEmptyAdditionalSection   = 1 << 2, ///< Do not include any RR in additional section.
295     };
296 
297     static constexpr uint8_t kTestModeDisabled = 0; ///< Test mode is disabled (no flags).
298 
299     /**
300      * Sets the test mode for `Server`.
301      *
302      * The test mode flags are intended for testing the client by having server behave in certain ways, e.g., reject
303      * messages with certain format (e.g., more than one question in query).
304      *
305      * @param[in] aTestMode   The new test mode (combination of `TestModeFlags`).
306      */
SetTestMode(uint8_t aTestMode)307     void SetTestMode(uint8_t aTestMode) { mTestMode = aTestMode; }
308 
309 private:
310     static constexpr bool     kBindUnspecifiedNetif         = OPENTHREAD_CONFIG_DNSSD_SERVER_BIND_UNSPECIFIED_NETIF;
311     static constexpr uint32_t kQueryTimeout                 = OPENTHREAD_CONFIG_DNSSD_QUERY_TIMEOUT;
312     static constexpr uint16_t kMaxConcurrentUpstreamQueries = 32;
313 
314     typedef Header::Response ResponseCode;
315 
316     typedef Message      ProxyQuery;
317     typedef MessageQueue ProxyQueryList;
318 
319     enum QueryType : uint8_t
320     {
321         kPtrQuery,
322         kSrvQuery,
323         kTxtQuery,
324         kSrvTxtQuery,
325         kAaaaQuery,
326         kAQuery,
327     };
328 
329     enum Section : uint8_t
330     {
331         kAnswerSection,
332         kAdditionalDataSection,
333     };
334 
335     enum AddrType : uint8_t
336     {
337         kIp6AddrType,
338         kIp4AddrType,
339     };
340 
341 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
342     enum ProxyAction : uint8_t
343     {
344         kNoAction,
345         kBrowsing,
346         kResolvingSrv,
347         kResolvingTxt,
348         kResolvingIp6Address,
349         kResolvingIp4Address
350     };
351 #endif
352 
353     struct Request
354     {
355         ResponseCode ParseQuestions(uint8_t aTestMode, bool &aShouldRespond);
356 
357         const Message          *mMessage;
358         const Ip6::MessageInfo *mMessageInfo;
359         Header                  mHeader;
360         QueryType               mType;
361     };
362 
363     struct ProxyQueryInfo;
364 
365     struct NameOffsets : public Clearable<NameOffsets>
366     {
367         uint16_t mDomainName;
368         uint16_t mServiceName;
369         uint16_t mInstanceName;
370         uint16_t mHostName;
371     };
372 
373 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
374     union ProxyResult
375     {
ProxyResult(const Dnssd::BrowseResult & aBrowseResult)376         explicit ProxyResult(const Dnssd::BrowseResult &aBrowseResult) { mBrowseResult = &aBrowseResult; }
ProxyResult(const Dnssd::SrvResult & aSrvResult)377         explicit ProxyResult(const Dnssd::SrvResult &aSrvResult) { mSrvResult = &aSrvResult; }
ProxyResult(const Dnssd::TxtResult & aTxtResult)378         explicit ProxyResult(const Dnssd::TxtResult &aTxtResult) { mTxtResult = &aTxtResult; }
ProxyResult(const Dnssd::AddressResult & aAddressResult)379         explicit ProxyResult(const Dnssd::AddressResult &aAddressResult) { mAddressResult = &aAddressResult; }
380 
381         const Dnssd::BrowseResult  *mBrowseResult;
382         const Dnssd::SrvResult     *mSrvResult;
383         const Dnssd::TxtResult     *mTxtResult;
384         const Dnssd::AddressResult *mAddressResult;
385     };
386 #endif
387 
388     class Response : public InstanceLocator, private NonCopyable
389     {
390     public:
391         explicit Response(Instance &aInstance);
392         ResponseCode AddQuestionsFrom(const Request &aRequest);
393 
394         Error AllocateAndInitFrom(const Request &aRequest);
395         void  InitFrom(ProxyQuery &aQuery, const ProxyQueryInfo &aInfo);
SetResponseCode(ResponseCode aResponseCode)396         void  SetResponseCode(ResponseCode aResponseCode) { mHeader.SetResponseCode(aResponseCode); }
397         Error ParseQueryName(void);
398         void  ReadQueryName(Name::Buffer &aName) const;
399         bool  QueryNameMatches(const char *aName) const;
400         Error AppendQueryName(void);
401         Error AppendPtrRecord(const char *aInstanceLabel, uint32_t aTtl);
402         Error AppendSrvRecord(const ServiceInstanceInfo &aInstanceInfo);
403         Error AppendSrvRecord(const char *aHostName,
404                               uint32_t    aTtl,
405                               uint16_t    aPriority,
406                               uint16_t    aWeight,
407                               uint16_t    aPort);
408         Error AppendTxtRecord(const ServiceInstanceInfo &aInstanceInfo);
409         Error AppendTxtRecord(const void *aTxtData, uint16_t aTxtLength, uint32_t aTtl);
410         Error AppendHostAddresses(AddrType aAddrType, const HostInfo &aHostInfo);
411         Error AppendHostAddresses(const ServiceInstanceInfo &aInstanceInfo);
412         Error AppendHostAddresses(AddrType aAddrType, const Ip6::Address *aAddrs, uint16_t aAddrsLength, uint32_t aTtl);
413         Error AppendAaaaRecord(const Ip6::Address &aAddress, uint32_t aTtl);
414         Error AppendARecord(const Ip6::Address &aAddress, uint32_t aTtl);
415         void  UpdateRecordLength(ResourceRecord &aRecord, uint16_t aOffset);
416         void  IncResourceRecordCount(void);
417         void  Send(const Ip6::MessageInfo &aMessageInfo);
418         void  Answer(const HostInfo &aHostInfo, const Ip6::MessageInfo &aMessageInfo);
419         void  Answer(const ServiceInstanceInfo &aInstanceInfo, const Ip6::MessageInfo &aMessageInfo);
420         Error ExtractServiceInstanceLabel(const char *aInstanceName, Name::LabelBuffer &aLabel);
421 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
422         Error ResolveBySrp(void);
423         bool  QueryNameMatchesService(const Srp::Server::Service &aService) const;
424         Error AppendSrvRecord(const Srp::Server::Service &aService);
425         Error AppendTxtRecord(const Srp::Server::Service &aService);
426         Error AppendHostAddresses(const Srp::Server::Host &aHost);
427 #endif
428 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
429         Error AppendPtrRecord(const ProxyResult &aResult);
430         Error AppendSrvRecord(const ProxyResult &aResult);
431         Error AppendTxtRecord(const ProxyResult &aResult);
432         Error AppendHostIp6Addresses(const ProxyResult &aResult);
433         Error AppendHostIp4Addresses(const ProxyResult &aResult);
434 #endif
435 
436 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
437         void               Log(void) const;
438         static const char *QueryTypeToString(QueryType aType);
439 #endif
440 
441         OwnedPtr<Message> mMessage;
442         Header            mHeader;
443         QueryType         mType;
444         Section           mSection;
445         NameOffsets       mOffsets;
446     };
447 
448     struct ProxyQueryInfo : Message::FooterData<ProxyQueryInfo>
449     {
450         QueryType        mType;
451         Ip6::MessageInfo mMessageInfo;
452         TimeMilli        mExpireTime;
453         NameOffsets      mOffsets;
454 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
455         ProxyAction mAction;
456 #endif
457     };
458 
459 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
460     class DiscoveryProxy : public InstanceLocator, private NonCopyable
461     {
462         friend ot::Dnssd;
463 
464     public:
465         explicit DiscoveryProxy(Instance &aInstance);
466 
IsRunning(void) const467         bool IsRunning(void) const { return mIsRunning; }
468         void UpdateState(void);
469         void Start(void);
470         void Stop(void);
471         void Resolve(ProxyQuery &aQuery, ProxyQueryInfo &aInfo);
472         void CancelAction(ProxyQuery &aQuery, ProxyQueryInfo &aInfo);
473 
474     private:
475         enum Command : uint8_t
476         {
477             kStart,
478             kStop,
479         };
480 
481         typedef Error (Response::*ResponseAppender)(const ProxyResult &aResult);
482 
483         void Perform(ProxyAction aAction, ProxyQuery &aQuery, ProxyQueryInfo &aInfo);
484         void ReadNameFor(ProxyAction aAction, ProxyQuery &aQuery, ProxyQueryInfo &aInfo, Name::Buffer &aName) const;
485         bool HasActive(ProxyAction aAction, const Name::Buffer &aName) const;
486         bool QueryMatches(const ProxyQuery     &aQuery,
487                           const ProxyQueryInfo &aInfo,
488                           ProxyAction           aAction,
489                           const Name::Buffer   &aName) const;
490         void UpdateProxy(Command               aCommand,
491                          ProxyAction           aAction,
492                          const ProxyQuery     &aQuery,
493                          const ProxyQueryInfo &aInfo,
494                          Name::Buffer         &aName);
495         void StartOrStopBrowser(Command aCommand, Name::Buffer &aServiceName);
496         void StartOrStopSrvResolver(Command aCommand, const ProxyQuery &aQuery, const ProxyQueryInfo &aInfo);
497         void StartOrStopTxtResolver(Command aCommand, const ProxyQuery &aQuery, const ProxyQueryInfo &aInfo);
498         void StartOrStopIp6Resolver(Command aCommand, Name::Buffer &aHostName);
499         void StartOrStopIp4Resolver(Command aCommand, Name::Buffer &aHostName);
500 
501         static void HandleBrowseResult(otInstance *aInstance, const otPlatDnssdBrowseResult *aResult);
502         static void HandleSrvResult(otInstance *aInstance, const otPlatDnssdSrvResult *aResult);
503         static void HandleTxtResult(otInstance *aInstance, const otPlatDnssdTxtResult *aResult);
504         static void HandleIp6AddressResult(otInstance *aInstance, const otPlatDnssdAddressResult *aResult);
505         static void HandleIp4AddressResult(otInstance *aInstance, const otPlatDnssdAddressResult *aResult);
506 
507         void HandleBrowseResult(const Dnssd::BrowseResult &aResult);
508         void HandleSrvResult(const Dnssd::SrvResult &aResult);
509         void HandleTxtResult(const Dnssd::TxtResult &aResult);
510         void HandleIp6AddressResult(const Dnssd::AddressResult &aResult);
511         void HandleIp4AddressResult(const Dnssd::AddressResult &aResult);
512         void HandleResult(ProxyAction         aAction,
513                           const Name::Buffer &aName,
514                           ResponseAppender    aAppender,
515                           const ProxyResult  &aResult);
516 
517         static bool IsActionForAdditionalSection(ProxyAction aAction, QueryType aQueryType);
518 
519         bool mIsRunning;
520     };
521 #endif
522 
IsRunning(void) const523     bool IsRunning(void) const { return mSocket.IsBound(); }
524     void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
525     void ProcessQuery(Request &aRequest);
526     void ResolveByProxy(Response &aResponse, const Ip6::MessageInfo &aMessageInfo);
527     void RemoveQueryAndPrepareResponse(ProxyQuery &aQuery, ProxyQueryInfo &aInfo, Response &aResponse);
528     void Finalize(ProxyQuery &aQuery, ResponseCode aResponseCode);
529 
530     static void  ReadQueryName(const Message &aQuery, Name::Buffer &aName);
531     static bool  QueryNameMatches(const Message &aQuery, const char *aName);
532     static void  ReadQueryInstanceName(const ProxyQuery &aQuery, const ProxyQueryInfo &aInfo, Name::Buffer &aName);
533     static void  ReadQueryInstanceName(const ProxyQuery     &aQuery,
534                                        const ProxyQueryInfo &aInfo,
535                                        Name::LabelBuffer    &aInstanceLabel,
536                                        Name::Buffer         &aServiceType);
537     static bool  QueryInstanceNameMatches(const ProxyQuery &aQuery, const ProxyQueryInfo &aInfo, const char *aName);
538     static void  ReadQueryHostName(const ProxyQuery &aQuery, const ProxyQueryInfo &aInfo, Name::Buffer &aName);
539     static bool  QueryHostNameMatches(const ProxyQuery &aQuery, const ProxyQueryInfo &aInfo, const char *aName);
540     static Error StripDomainName(const char *aFullName, Name::Buffer &aLabels);
541     static Error StripDomainName(Name::Buffer &aName);
542     static void  ConstructFullName(const char *aLabels, Name::Buffer &aFullName);
543     static void  ConstructFullInstanceName(const char   *aInstanceLabel,
544                                            const char   *aServiceType,
545                                            Name::Buffer &aFullName);
546     static void  ConstructFullServiceSubTypeName(const char   *aServiceType,
547                                                  const char   *aSubTypeLabel,
548                                                  Name::Buffer &aFullName);
549 
550 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
HandleInfraIfStateChanged(void)551     void        HandleInfraIfStateChanged(void) { mDiscoveryProxy.UpdateState(); }
HandleDnssdPlatformStateChange(void)552     void        HandleDnssdPlatformStateChange(void) { mDiscoveryProxy.UpdateState(); }
553     static bool IsProxyAddressValid(const Ip6::Address &aAddress);
554 #endif
555 
556 #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
557     static bool               ShouldForwardToUpstream(const Request &aRequest);
558     UpstreamQueryTransaction *AllocateUpstreamQueryTransaction(const Ip6::MessageInfo &aMessageInfo);
559     void                      ResetUpstreamQueryTransaction(UpstreamQueryTransaction &aTxn, Error aError);
560     Error                     ResolveByUpstream(const Request &aRequest);
561 #endif
562 
563     void HandleTimer(void);
564     void ResetTimer(void);
565 
566     void UpdateResponseCounters(ResponseCode aResponseCode);
567 
568     using ServerTimer  = TimerMilliIn<Server, &Server::HandleTimer>;
569     using ServerSocket = Ip6::Udp::SocketIn<Server, &Server::HandleUdpReceive>;
570 
571     static const char kDefaultDomainName[];
572     static const char kSubLabel[];
573 #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
574     static const char *kBlockedDomains[];
575 #endif
576 
577     ServerSocket mSocket;
578 
579     ProxyQueryList                mProxyQueries;
580     Callback<SubscribeCallback>   mQuerySubscribe;
581     Callback<UnsubscribeCallback> mQueryUnsubscribe;
582 
583 #if OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
584     DiscoveryProxy mDiscoveryProxy;
585 #endif
586 
587 #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
588     bool                     mEnableUpstreamQuery;
589     UpstreamQueryTransaction mUpstreamQueryTransactions[kMaxConcurrentUpstreamQueries];
590 #endif
591 
592     ServerTimer mTimer;
593     Counters    mCounters;
594     uint8_t     mTestMode;
595 };
596 
597 } // namespace ServiceDiscovery
598 } // namespace Dns
599 
600 DefineMapEnum(otDnssdQueryType, Dns::ServiceDiscovery::Server::DnsQueryType);
601 #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
602 DefineCoreType(otPlatDnsUpstreamQuery, Dns::ServiceDiscovery::Server::UpstreamQueryTransaction);
603 #endif
604 
605 } // namespace ot
606 
607 #endif // OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
608 
609 #endif // DNS_SERVER_HPP_
610