1 /*
2  *  Copyright (c) 2020, 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 /**
30  * @file
31  *   This file includes definitions for SRP server.
32  */
33 
34 #ifndef NET_SRP_SERVER_HPP_
35 #define NET_SRP_SERVER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
40 
41 #if !OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
42 #error "OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE is required for OPENTHREAD_CONFIG_SRP_SERVER_ENABLE"
43 #endif
44 
45 #if !OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE
46 #error "OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE is required for OPENTHREAD_CONFIG_SRP_SERVER_ENABLE"
47 #endif
48 
49 #if !OPENTHREAD_CONFIG_ECDSA_ENABLE
50 #error "OPENTHREAD_CONFIG_ECDSA_ENABLE is required for OPENTHREAD_CONFIG_SRP_SERVER_ENABLE"
51 #endif
52 
53 #include <openthread/ip6.h>
54 #include <openthread/srp_server.h>
55 
56 #include "common/array.hpp"
57 #include "common/as_core_type.hpp"
58 #include "common/callback.hpp"
59 #include "common/clearable.hpp"
60 #include "common/heap.hpp"
61 #include "common/heap_allocatable.hpp"
62 #include "common/heap_array.hpp"
63 #include "common/heap_data.hpp"
64 #include "common/heap_string.hpp"
65 #include "common/linked_list.hpp"
66 #include "common/locator.hpp"
67 #include "common/non_copyable.hpp"
68 #include "common/notifier.hpp"
69 #include "common/num_utils.hpp"
70 #include "common/numeric_limits.hpp"
71 #include "common/retain_ptr.hpp"
72 #include "common/timer.hpp"
73 #include "crypto/ecdsa.hpp"
74 #include "net/dns_types.hpp"
75 #include "net/dnssd.hpp"
76 #include "net/ip6.hpp"
77 #include "net/ip6_address.hpp"
78 #include "net/udp6.hpp"
79 #include "thread/network_data_publisher.hpp"
80 
81 struct otSrpServerHost
82 {
83 };
84 
85 struct otSrpServerService
86 {
87 };
88 
89 namespace ot {
90 
91 namespace Dns {
92 namespace ServiceDiscovery {
93 class Server;
94 }
95 } // namespace Dns
96 
97 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
98 namespace BorderRouter {
99 class RoutingManager;
100 }
101 #endif
102 
103 namespace Srp {
104 
105 class AdvertisingProxy;
106 
107 /**
108  * Implements the SRP server.
109  *
110  */
111 class Server : public InstanceLocator, private NonCopyable
112 {
113     friend class NetworkData::Publisher;
114     friend class UpdateMetadata;
115     friend class Service;
116     friend class Host;
117     friend class Dns::ServiceDiscovery::Server;
118     friend class AdvertisingProxy;
119 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
120     friend class BorderRouter::RoutingManager;
121 #endif
122 
123     enum RetainName : bool
124     {
125         kDeleteName = false,
126         kRetainName = true,
127     };
128 
129     enum NotifyMode : bool
130     {
131         kDoNotNotifyServiceHandler = false,
132         kNotifyServiceHandler      = true,
133     };
134 
135 #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE
136     static constexpr Dnssd::RequestId kInvalidRequestId = 0;
137 #endif
138 
139 public:
140     static constexpr uint16_t kUdpPortMin = OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT_MIN; ///< The reserved min port.
141     static constexpr uint16_t kUdpPortMax = OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT_MAX; ///< The reserved max port.
142 
143     static_assert(kUdpPortMin <= kUdpPortMax, "invalid port range");
144 
145     /**
146      * The ID of SRP service update transaction.
147      *
148      */
149     typedef otSrpServerServiceUpdateId ServiceUpdateId;
150 
151     /**
152      * The SRP server lease information of a host/service.
153      *
154      */
155     typedef otSrpServerLeaseInfo LeaseInfo;
156 
157     /**
158      * Represents the address mode used by the SRP server.
159      *
160      * Address mode specifies how the address and port number are determined by the SRP server and how this info ins
161      * published in the Thread Network Data.
162      *
163      */
164     enum AddressMode : uint8_t
165     {
166         kAddressModeUnicast = OT_SRP_SERVER_ADDRESS_MODE_UNICAST, ///< Unicast address mode.
167         kAddressModeAnycast = OT_SRP_SERVER_ADDRESS_MODE_ANYCAST, ///< Anycast address mode.
168     };
169 
170     class Host;
171 
172     /**
173      * Represents the state of SRP server.
174      *
175      */
176     enum State : uint8_t
177     {
178         kStateDisabled = OT_SRP_SERVER_STATE_DISABLED, ///< Server is disabled.
179         kStateRunning  = OT_SRP_SERVER_STATE_RUNNING,  ///< Server is enabled and running.
180         kStateStopped  = OT_SRP_SERVER_STATE_STOPPED,  ///< Server is enabled but stopped.
181     };
182 
183     /**
184      * Implements a server-side SRP service.
185      *
186      */
187     class Service : public otSrpServerService,
188                     public LinkedListEntry<Service>,
189                     private Heap::Allocatable<Service>,
190                     private NonCopyable
191     {
192         friend class Server;
193         friend class LinkedList<Service>;
194         friend class LinkedListEntry<Service>;
195         friend class Heap::Allocatable<Service>;
196         friend class AdvertisingProxy;
197 
198     public:
199         /**
200          * Tells if the SRP service has been deleted.
201          *
202          * A SRP service can be deleted but retains its name for future uses.
203          * In this case, the service instance is not removed from the SRP server/registry.
204          * It is guaranteed that all services are deleted if the host is deleted.
205          *
206          * @returns  TRUE if the service has been deleted, FALSE if not.
207          *
208          */
IsDeleted(void) const209         bool IsDeleted(void) const { return mIsDeleted; }
210 
211         /**
212          * Gets the full service instance name of the service.
213          *
214          * @returns  A pointer service instance name (as a null-terminated C string).
215          *
216          */
GetInstanceName(void) const217         const char *GetInstanceName(void) const { return mInstanceName.AsCString(); }
218 
219         /**
220          * Gets the service instance label of the service.
221          *
222          * @returns  A pointer service instance label (as a null-terminated C string).
223          *
224          */
GetInstanceLabel(void) const225         const char *GetInstanceLabel(void) const { return mInstanceLabel.AsCString(); }
226 
227         /**
228          * Gets the full service name of the service.
229          *
230          * @returns  A pointer service name (as a null-terminated C string).
231          *
232          */
GetServiceName(void) const233         const char *GetServiceName(void) const { return mServiceName.AsCString(); }
234 
235         /**
236          * Gets number of sub-types of this service.
237          *
238          * @returns The number of sub-types.
239          *
240          */
GetNumberOfSubTypes(void) const241         uint16_t GetNumberOfSubTypes(void) const { return mSubTypes.GetLength(); }
242 
243         /**
244          * Gets the sub-type service name (full name) at a given index.
245          *
246          * The full service name for a sub-type service follows "<sub-label>._sub.<service-labels>.<domain>.".
247          *
248          * @param[in] aIndex   The index to get.
249          *
250          * @returns A pointer to sub-type service name at @p aIndex, or `nullptr` if none at this index.
251          *
252          */
253         const char *GetSubTypeServiceNameAt(uint16_t aIndex) const;
254 
255         /**
256          * Indicates whether or not service has a given sub-type.
257          *
258          * @param[in] aSubTypeServiceName  The sub-type service name (full name).
259          *
260          * @retval TRUE   Service contains the sub-type @p aSubTypeServiceName.
261          * @retval FALSE  Service does not contain the sub-type @p aSubTypeServiceName.
262          *
263          */
264         bool HasSubTypeServiceName(const char *aSubTypeServiceName) const;
265 
266         /**
267          * Parses a sub-type service name (full name) and extracts the sub-type label.
268          *
269          * The full service name for a sub-type service follows "<sub-label>._sub.<service-labels>.<domain>.".
270          *
271          * @param[in]  aSubTypeServiceName  A sub-type service name (full name).
272          * @param[out] aLabel               A pointer to a buffer to copy the extracted sub-type label.
273          * @param[in]  aLabelSize           Maximum size of @p aLabel buffer.
274          *
275          * @retval kErrorNone         Name was successfully parsed and @p aLabel was updated.
276          * @retval kErrorNoBufs       The sub-type label could not fit in @p aLabel buffer (number of chars from label
277          *                            that could fit are copied in @p aLabel ensuring it is null-terminated).
278          * @retval kErrorInvalidArgs  @p aSubTypeServiceName is not a valid sub-type format.
279          *
280          */
281         static Error ParseSubTypeServiceName(const char *aSubTypeServiceName, char *aLabel, uint8_t aLabelSize);
282 
283         /**
284          * Returns the TTL of the service instance.
285          *
286          * @returns The TTL of the service instance.
287          *
288          */
GetTtl(void) const289         uint32_t GetTtl(void) const { return mTtl; }
290 
291         /**
292          * Returns the port of the service instance.
293          *
294          * @returns  The port of the service.
295          *
296          */
GetPort(void) const297         uint16_t GetPort(void) const { return mPort; }
298 
299         /**
300          * Returns the weight of the service instance.
301          *
302          * @returns  The weight of the service.
303          *
304          */
GetWeight(void) const305         uint16_t GetWeight(void) const { return mWeight; }
306 
307         /**
308          * Returns the priority of the service instance.
309          *
310          * @returns  The priority of the service.
311          *
312          */
GetPriority(void) const313         uint16_t GetPriority(void) const { return mPriority; }
314 
315         /**
316          * Returns the TXT record data of the service instance.
317          *
318          * @returns A pointer to the buffer containing the TXT record data.
319          *
320          */
GetTxtData(void) const321         const uint8_t *GetTxtData(void) const { return mTxtData.GetBytes(); }
322 
323         /**
324          * Returns the TXT record data length of the service instance.
325          *
326          * @return The TXT record data length (number of bytes in buffer returned from `GetTxtData()`).
327          *
328          */
GetTxtDataLength(void) const329         uint16_t GetTxtDataLength(void) const { return mTxtData.GetLength(); }
330 
331         /**
332          * Returns the host which the service instance reside on.
333          *
334          * @returns  A reference to the host instance.
335          *
336          */
GetHost(void) const337         const Host &GetHost(void) const { return *mHost; }
338 
339         /**
340          * Returns the LEASE time of the service.
341          *
342          * @returns  The LEASE time in seconds.
343          *
344          */
GetLease(void) const345         uint32_t GetLease(void) const { return mLease; }
346 
347         /**
348          * Returns the KEY-LEASE time of the key of the service.
349          *
350          * @returns  The KEY-LEASE time in seconds.
351          *
352          */
GetKeyLease(void) const353         uint32_t GetKeyLease(void) const { return mKeyLease; }
354 
355         /**
356          * Returns the expire time (in milliseconds) of the service.
357          *
358          * @returns  The service expire time in milliseconds.
359          *
360          */
361         TimeMilli GetExpireTime(void) const;
362 
363         /**
364          * Returns the key expire time (in milliseconds) of the service.
365          *
366          * @returns  The service key expire time in milliseconds.
367          *
368          */
369         TimeMilli GetKeyExpireTime(void) const;
370 
371         /**
372          * Gets the LEASE and KEY-LEASE information of a given service.
373          *
374          * @param[out]  aLeaseInfo  A reference to a LeaseInfo instance. It contains the LEASE time, KEY-LEASE time,
375          *                          remaining LEASE time and the remaining KEY-LEASE time.
376          *
377          */
378         void GetLeaseInfo(LeaseInfo &aLeaseInfo) const;
379 
380         /**
381          * Indicates whether this service matches a given service instance name.
382          *
383          * @param[in]  aInstanceName  The service instance name.
384          *
385          * @retval  TRUE   If the service matches the service instance name.
386          * @retval  FALSE  If the service does not match the service instance name.
387          *
388          */
389         bool MatchesInstanceName(const char *aInstanceName) const;
390 
391         /**
392          * Tells whether this service matches a given service name.
393          *
394          * @param[in] aServiceName  The full service name to match.
395          *
396          * @retval  TRUE   If the service matches the full service name.
397          * @retval  FALSE  If the service does not match the full service name.
398          *
399          */
400         bool MatchesServiceName(const char *aServiceName) const;
401 
402     private:
403         enum Action : uint8_t
404         {
405             kAddNew,
406             kUpdateExisting,
407             kKeepUnchanged,
408             kRemoveButRetainName,
409             kFullyRemove,
410             kLeaseExpired,
411             kKeyLeaseExpired,
412         };
413 
414         Error Init(const char *aInstanceName, const char *aInstanceLabel, Host &aHost, TimeMilli aUpdateTime);
415         Error SetTxtDataFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
416         bool  Matches(const char *aInstanceName) const;
417         void  Log(Action aAction) const;
418 
419         template <uint16_t kLabelSize>
ParseSubTypeServiceName(const char * aSubTypeServiceName,char (& aLabel)[kLabelSize])420         static Error ParseSubTypeServiceName(const char *aSubTypeServiceName, char (&aLabel)[kLabelSize])
421         {
422             return ParseSubTypeServiceName(aSubTypeServiceName, aLabel, kLabelSize);
423         }
424 
425         Service                  *mNext;
426         Heap::String              mInstanceName;
427         Heap::String              mInstanceLabel;
428         Heap::String              mServiceName;
429         Heap::Array<Heap::String> mSubTypes;
430         Host                     *mHost;
431         Heap::Data                mTxtData;
432         uint16_t                  mPriority;
433         uint16_t                  mWeight;
434         uint16_t                  mPort;
435         uint32_t                  mTtl;      // In seconds
436         uint32_t                  mLease;    // In seconds
437         uint32_t                  mKeyLease; // In seconds
438         TimeMilli                 mUpdateTime;
439         bool                      mIsDeleted : 1;
440         bool                      mIsCommitted : 1;
441         bool                      mParsedDeleteAllRrset : 1;
442         bool                      mParsedSrv : 1;
443         bool                      mParsedTxt : 1;
444 #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE
445         bool             mIsRegistered : 1;
446         bool             mIsKeyRegistered : 1;
447         bool             mIsReplaced : 1;
448         bool             mShouldAdvertise : 1;
449         bool             mShouldRegisterKey : 1;
450         Dnssd::RequestId mAdvId;
451         Dnssd::RequestId mKeyAdvId;
452 #endif
453     };
454 
455     /**
456      * Implements the Host which registers services on the SRP server.
457      *
458      */
459     class Host : public otSrpServerHost,
460                  public InstanceLocator,
461                  public LinkedListEntry<Host>,
462                  private Heap::Allocatable<Host>,
463                  private NonCopyable
464     {
465         friend class Server;
466         friend class LinkedListEntry<Host>;
467         friend class Heap::Allocatable<Host>;
468         friend class AdvertisingProxy;
469 
470     public:
471         typedef Crypto::Ecdsa::P256::PublicKey Key; ///< Host key (public ECDSA P256 key).
472 
473         /**
474          * Tells whether the Host object has been deleted.
475          *
476          * The Host object retains event if the host has been deleted by the SRP client,
477          * because the host name may retain.
478          *
479          * @returns  TRUE if the host is deleted, FALSE if the host is not deleted.
480          *
481          */
IsDeleted(void) const482         bool IsDeleted(void) const { return (mLease == 0); }
483 
484         /**
485          * Returns the full name of the host.
486          *
487          * @returns  A pointer to the null-terminated full host name.
488          *
489          */
GetFullName(void) const490         const char *GetFullName(void) const { return mFullName.AsCString(); }
491 
492         /**
493          * Returns addresses of the host.
494          *
495          * @param[out]  aAddressesNum  The number of the addresses.
496          *
497          * @returns  A pointer to the addresses array or `nullptr` if no addresses.
498          *
499          */
GetAddresses(uint8_t & aAddressesNum) const500         const Ip6::Address *GetAddresses(uint8_t &aAddressesNum) const
501         {
502             aAddressesNum = ClampToUint8(mAddresses.GetLength());
503 
504             return mAddresses.AsCArray();
505         }
506 
507         /**
508          * Returns the TTL of the host.
509          *
510          * @returns The TTL of the host.
511          *
512          */
GetTtl(void) const513         uint32_t GetTtl(void) const { return mTtl; }
514 
515         /**
516          * Returns the LEASE time of the host.
517          *
518          * @returns  The LEASE time in seconds.
519          *
520          */
GetLease(void) const521         uint32_t GetLease(void) const { return mLease; }
522 
523         /**
524          * Returns the KEY-LEASE time of the key of the host.
525          *
526          * @returns  The KEY-LEASE time in seconds.
527          *
528          */
GetKeyLease(void) const529         uint32_t GetKeyLease(void) const { return mKeyLease; }
530 
531         /**
532          * Gets the LEASE and KEY-LEASE information of a given host.
533          *
534          * @param[out]  aLeaseInfo  A reference to a LeaseInfo instance. It contains the LEASE time, KEY-LEASE time,
535          *                          remaining LEASE time and the remaining KEY-LEASE time.
536          *
537          */
538         void GetLeaseInfo(LeaseInfo &aLeaseInfo) const;
539 
540         /**
541          * Returns the key associated with this host.
542          *
543          * @returns  The host key.
544          *
545          */
GetKey(void) const546         const Key &GetKey(void) const { return mKey; }
547 
548         /**
549          * Returns the expire time (in milliseconds) of the host.
550          *
551          * @returns  The expire time in milliseconds.
552          *
553          */
554         TimeMilli GetExpireTime(void) const;
555 
556         /**
557          * Returns the expire time (in milliseconds) of the key of the host.
558          *
559          * @returns  The expire time of the key in milliseconds.
560          *
561          */
562         TimeMilli GetKeyExpireTime(void) const;
563 
564         /**
565          * Returns the `Service` linked list associated with the host.
566          *
567          * @returns The `Service` linked list.
568          *
569          */
GetServices(void) const570         const LinkedList<Service> &GetServices(void) const { return mServices; }
571 
572         /*
573          * Returns the next service.
574          *
575          * @param[in] aPrevService   A pointer to the previous service or `nullptr` to start from beginning of the list.
576          *
577          * @returns  A pointer to the next service or `nullptr` if no more services can be found.
578          *
579          */
580         const Service *GetNextService(const Service *aPrevService) const;
581 
582         /**
583          * Tells whether the host matches a given full name.
584          *
585          * @param[in]  aFullName  The full name.
586          *
587          * @returns  A boolean that indicates whether the host matches the given name.
588          *
589          */
590         bool Matches(const char *aFullName) const;
591 
592     private:
593         Host(Instance &aInstance, TimeMilli aUpdateTime);
594         ~Host(void);
595 
596         Error SetFullName(const char *aFullName);
SetTtl(uint32_t aTtl)597         void  SetTtl(uint32_t aTtl) { mTtl = aTtl; }
SetLease(uint32_t aLease)598         void  SetLease(uint32_t aLease) { mLease = aLease; }
SetKeyLease(uint32_t aKeyLease)599         void  SetKeyLease(uint32_t aKeyLease) { mKeyLease = aKeyLease; }
SetUseShortLeaseOption(bool aUse)600         void  SetUseShortLeaseOption(bool aUse) { mUseShortLeaseOption = aUse; }
ShouldUseShortLeaseOption(void) const601         bool  ShouldUseShortLeaseOption(void) const { return mUseShortLeaseOption; }
602         Error ProcessTtl(uint32_t aTtl);
603 
604         Service       *AddNewService(const char *aInstanceName, const char *aInstanceLabel, TimeMilli aUpdateTime);
605         void           AddService(Service &aService);
606         void           RemoveService(Service *aService, RetainName aRetainName, NotifyMode aNotifyServiceHandler);
607         bool           HasService(const char *aInstanceName) const;
608         Service       *FindService(const char *aInstanceName);
609         const Service *FindService(const char *aInstanceName) const;
610         void           FreeAllServices(void);
611         void           ClearResources(void);
612         Error          AddIp6Address(const Ip6::Address &aIp6Address);
613 
614         Host                     *mNext;
615         Heap::String              mFullName;
616         Heap::Array<Ip6::Address> mAddresses;
617         Key                       mKey;
618         uint32_t                  mTtl;      // The TTL in seconds.
619         uint32_t                  mLease;    // The LEASE time in seconds.
620         uint32_t                  mKeyLease; // The KEY-LEASE time in seconds.
621         TimeMilli                 mUpdateTime;
622         LinkedList<Service>       mServices;
623         bool                      mParsedKey : 1;
624         bool                      mUseShortLeaseOption : 1; // Use short lease option (lease only 4 bytes).
625 #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE
626         bool                  mIsRegistered : 1;
627         bool                  mIsKeyRegistered : 1;
628         bool                  mIsReplaced : 1;
629         bool                  mShouldAdvertise : 1;
630         bool                  mShouldRegisterKey : 1;
631         Dnssd::RequestId      mAdvId;
632         Dnssd::RequestId      mKeyAdvId;
633         Dnssd::RequestIdRange mAdvIdRange;
634 #endif
635     };
636 
637     /**
638      * Handles TTL configuration.
639      *
640      */
641     class TtlConfig : public otSrpServerTtlConfig
642     {
643         friend class Server;
644 
645     public:
646         /**
647          * Initializes to default TTL configuration.
648          *
649          */
650         TtlConfig(void);
651 
652     private:
IsValid(void) const653         bool     IsValid(void) const { return mMinTtl <= mMaxTtl; }
654         uint32_t GrantTtl(uint32_t aLease, uint32_t aTtl) const;
655     };
656 
657     /**
658      * Handles LEASE and KEY-LEASE configurations.
659      *
660      */
661     class LeaseConfig : public otSrpServerLeaseConfig
662     {
663         friend class Server;
664 
665     public:
666         /**
667          * Initialize to default LEASE and KEY-LEASE configurations.
668          *
669          */
670         LeaseConfig(void);
671 
672     private:
673         bool     IsValid(void) const;
674         uint32_t GrantLease(uint32_t aLease) const;
675         uint32_t GrantKeyLease(uint32_t aKeyLease) const;
676     };
677 
678     /**
679      * Initializes the SRP server object.
680      *
681      * @param[in]  aInstance  A reference to the OpenThread instance.
682      *
683      */
684     explicit Server(Instance &aInstance);
685 
686     /**
687      * Sets the SRP service events handler.
688      *
689      * @param[in]  aServiceHandler         A service events handler.
690      * @param[in]  aServiceHandlerContext  A pointer to arbitrary context information.
691      *
692      * @note  The handler SHOULD call HandleServiceUpdateResult to report the result of its processing.
693      *        Otherwise, a SRP update will be considered failed.
694      *
695      * @sa  HandleServiceUpdateResult
696      *
697      */
SetServiceHandler(otSrpServerServiceUpdateHandler aServiceHandler,void * aServiceHandlerContext)698     void SetServiceHandler(otSrpServerServiceUpdateHandler aServiceHandler, void *aServiceHandlerContext)
699     {
700         mServiceUpdateHandler.Set(aServiceHandler, aServiceHandlerContext);
701     }
702 
703     /**
704      * Returns the domain authorized to the SRP server.
705      *
706      * If the domain if not set by SetDomain, "default.service.arpa." will be returned.
707      * A trailing dot is always appended even if the domain is set without it.
708      *
709      * @returns A pointer to the dot-joined domain string.
710      *
711      */
GetDomain(void) const712     const char *GetDomain(void) const { return mDomain.AsCString(); }
713 
714     /**
715      * Sets the domain on the SRP server.
716      *
717      * A trailing dot will be appended to @p aDomain if it is not already there.
718      * Should only be called before the SRP server is enabled.
719      *
720      * @param[in]  aDomain  The domain to be set. MUST NOT be `nullptr`.
721      *
722      * @retval  kErrorNone          Successfully set the domain to @p aDomain.
723      * @retval  kErrorInvalidState  The SRP server is already enabled and the Domain cannot be changed.
724      * @retval  kErrorInvalidArgs   The argument @p aDomain is not a valid DNS domain name.
725      * @retval  kErrorNoBufs        There is no memory to store content of @p aDomain.
726      *
727      */
728     Error SetDomain(const char *aDomain);
729 
730     /**
731      * Returns the address mode being used by the SRP server.
732      *
733      * @returns The SRP server's address mode.
734      *
735      */
GetAddressMode(void) const736     AddressMode GetAddressMode(void) const { return mAddressMode; }
737 
738     /**
739      * Sets the address mode to be used by the SRP server.
740      *
741      * @param[in] aMode      The address mode to use.
742      *
743      * @retval kErrorNone           Successfully set the address mode.
744      * @retval kErrorInvalidState   The SRP server is enabled and the address mode cannot be changed.
745      *
746      */
747     Error SetAddressMode(AddressMode aMode);
748 
749     /**
750      * Gets the sequence number used with anycast address mode.
751      *
752      * The sequence number is included in "DNS/SRP Service Anycast Address" entry published in the Network Data.
753      *
754      * @returns The anycast sequence number.
755      *
756      */
GetAnycastModeSequenceNumber(void) const757     uint8_t GetAnycastModeSequenceNumber(void) const { return mAnycastSequenceNumber; }
758 
759     /**
760      * Sets the sequence number used with anycast address mode.
761      *
762      * @param[in] aSequenceNumber  The sequence number to use.
763      *
764      * @retval kErrorNone           Successfully set the address mode.
765      * @retval kErrorInvalidState   The SRP server is enabled and the sequence number cannot be changed.
766      *
767      */
768     Error SetAnycastModeSequenceNumber(uint8_t aSequenceNumber);
769 
770     /**
771      * Returns the state of the SRP server.
772      *
773      * @returns  The state of the server.
774      *
775      */
GetState(void) const776     State GetState(void) const { return mState; }
777 
778     /**
779      * Tells the port the SRP server is listening to.
780      *
781      * @returns  The port of the server or 0 if the SRP server is not running.
782      *
783      */
GetPort(void) const784     uint16_t GetPort(void) const { return (mState == kStateRunning) ? mPort : 0; }
785 
786     /**
787      * Enables/disables the SRP server.
788      *
789      * @param[in]  aEnabled  A boolean to enable/disable the SRP server.
790      *
791      */
792     void SetEnabled(bool aEnabled);
793 
794 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
795     /**
796      * Enables/disables the auto-enable mode on SRP server.
797      *
798      * When this mode is enabled, the Border Routing Manager controls if/when to enable or disable the SRP server.
799      * SRP sever is auto-enabled if/when Border Routing is started it is done with the initial prefix and route
800      * configurations (when the OMR and on-link prefixes are determined, advertised in emitted Router Advert message on
801      * infrastructure side and published in the Thread Network Data). The SRP server is auto-disabled when BR is
802      * stopped (e.g., if the infrastructure network interface is brought down or if BR gets detached).
803      *
804      * This mode can be disabled by a `SetAutoEnableMode(false)` call or if the SRP server is explicitly enabled or
805      * disabled by a call to `SetEnabled()` method. Disabling auto-enable mode using `SetAutoEnableMode(false` call
806      * will not change the current state of SRP sever (e.g., if it is enabled it stays enabled).
807      *
808      * @param[in] aEnabled    A boolean to enable/disable the auto-enable mode.
809      *
810      */
811     void SetAutoEnableMode(bool aEnabled);
812 
813     /**
814      * Indicates whether the auto-enable mode is enabled or disabled.
815      *
816      * @retval TRUE   The auto-enable mode is enabled.
817      * @retval FALSE  The auto-enable mode is disabled.
818      *
819      */
IsAutoEnableMode(void) const820     bool IsAutoEnableMode(void) const { return mAutoEnable; }
821 #endif
822 
823     /**
824      * Returns the TTL configuration.
825      *
826      * @param[out]  aTtlConfig  A reference to the `TtlConfig` instance.
827      *
828      */
GetTtlConfig(TtlConfig & aTtlConfig) const829     void GetTtlConfig(TtlConfig &aTtlConfig) const { aTtlConfig = mTtlConfig; }
830 
831     /**
832      * Sets the TTL configuration.
833      *
834      * @param[in]  aTtlConfig  A reference to the `TtlConfig` instance.
835      *
836      * @retval  kErrorNone         Successfully set the TTL configuration
837      * @retval  kErrorInvalidArgs  The TTL range is not valid.
838      *
839      */
840     Error SetTtlConfig(const TtlConfig &aTtlConfig);
841 
842     /**
843      * Returns the LEASE and KEY-LEASE configurations.
844      *
845      * @param[out]  aLeaseConfig  A reference to the `LeaseConfig` instance.
846      *
847      */
GetLeaseConfig(LeaseConfig & aLeaseConfig) const848     void GetLeaseConfig(LeaseConfig &aLeaseConfig) const { aLeaseConfig = mLeaseConfig; }
849 
850     /**
851      * Sets the LEASE and KEY-LEASE configurations.
852      *
853      * When a LEASE time is requested from a client, the granted value will be
854      * limited in range [aMinLease, aMaxLease]; and a KEY-LEASE will be granted
855      * in range [aMinKeyLease, aMaxKeyLease].
856      *
857      * @param[in]  aLeaseConfig  A reference to the `LeaseConfig` instance.
858      *
859      * @retval  kErrorNone         Successfully set the LEASE and KEY-LEASE ranges.
860      * @retval  kErrorInvalidArgs  The LEASE or KEY-LEASE range is not valid.
861      *
862      */
863     Error SetLeaseConfig(const LeaseConfig &aLeaseConfig);
864 
865     /**
866      * Returns the `Host` linked list.
867      *
868      * @returns The `Host` linked list.
869      *
870      */
GetHosts(void) const871     const LinkedList<Host> &GetHosts(void) const { return mHosts; }
872 
873     /**
874      * Returns the next registered SRP host.
875      *
876      * @param[in]  aHost  The current SRP host; use `nullptr` to get the first SRP host.
877      *
878      * @returns  A pointer to the next SRP host or `nullptr` if no more SRP hosts can be found.
879      *
880      */
881     const Host *GetNextHost(const Host *aHost);
882 
883     /**
884      * Returns the response counters of the SRP server.
885      *
886      * @returns  A pointer to the response counters of the SRP server.
887      *
888      */
GetResponseCounters(void) const889     const otSrpServerResponseCounters *GetResponseCounters(void) const { return &mResponseCounters; }
890 
891     /**
892      * Receives the service update result from service handler set by
893      * SetServiceHandler.
894      *
895      * @param[in]  aId     The ID of the service update transaction.
896      * @param[in]  aError  The service update result.
897      *
898      */
899     void HandleServiceUpdateResult(ServiceUpdateId aId, Error aError);
900 
901 private:
902     static constexpr uint16_t kUdpPayloadSize = Ip6::kMaxDatagramLength - sizeof(Ip6::Udp::Header);
903 
904     static constexpr uint32_t kDefaultMinLease             = 30;          // 30 seconds.
905     static constexpr uint32_t kDefaultMaxLease             = 27u * 3600;  // 27 hours (in seconds).
906     static constexpr uint32_t kDefaultMinKeyLease          = 30;          // 30 seconds.
907     static constexpr uint32_t kDefaultMaxKeyLease          = 189u * 3600; // 189 hours (in seconds).
908     static constexpr uint32_t kDefaultMinTtl               = kDefaultMinLease;
909     static constexpr uint32_t kDefaultMaxTtl               = kDefaultMaxLease;
910     static constexpr uint32_t kDefaultEventsHandlerTimeout = OPENTHREAD_CONFIG_SRP_SERVER_SERVICE_UPDATE_TIMEOUT;
911 
912     static constexpr AddressMode kDefaultAddressMode =
913         static_cast<AddressMode>(OPENTHREAD_CONFIG_SRP_SERVER_DEFAULT_ADDRESS_MODE);
914 
915     static constexpr uint16_t kUninitializedPort      = 0;
916     static constexpr uint16_t kAnycastAddressModePort = 53;
917 
918     // Metadata for a received SRP Update message.
919     struct MessageMetadata
920     {
921         // Indicates whether the `Message` is received directly from a
922         // client or from an SRPL partner.
IsDirectRxFromClientot::Srp::Server::MessageMetadata923         bool IsDirectRxFromClient(void) const { return (mMessageInfo != nullptr); }
924 
925         Dns::UpdateHeader       mDnsHeader;
926         Dns::Zone               mDnsZone;
927         uint16_t                mOffset;
928         TimeMilli               mRxTime;
929         TtlConfig               mTtlConfig;
930         LeaseConfig             mLeaseConfig;
931         const Ip6::MessageInfo *mMessageInfo; // Set to `nullptr` when from SRPL.
932     };
933 
934     // This class includes metadata for processing a SRP update (register, deregister)
935     // and sending DNS response to the client.
936     class UpdateMetadata : public InstanceLocator,
937                            public LinkedListEntry<UpdateMetadata>,
938                            public Heap::Allocatable<UpdateMetadata>
939     {
940         friend class LinkedListEntry<UpdateMetadata>;
941         friend class Heap::Allocatable<UpdateMetadata>;
942 
943     public:
GetExpireTime(void) const944         TimeMilli                GetExpireTime(void) const { return mExpireTime; }
GetDnsHeader(void) const945         const Dns::UpdateHeader &GetDnsHeader(void) const { return mDnsHeader; }
GetId(void) const946         ServiceUpdateId          GetId(void) const { return mId; }
GetTtlConfig(void) const947         const TtlConfig         &GetTtlConfig(void) const { return mTtlConfig; }
GetLeaseConfig(void) const948         const LeaseConfig       &GetLeaseConfig(void) const { return mLeaseConfig; }
GetHost(void)949         Host                    &GetHost(void) { return mHost; }
GetMessageInfo(void) const950         const Ip6::MessageInfo  &GetMessageInfo(void) const { return mMessageInfo; }
GetError(void) const951         Error                    GetError(void) const { return mError; }
SetError(Error aError)952         void                     SetError(Error aError) { mError = aError; }
IsDirectRxFromClient(void) const953         bool                     IsDirectRxFromClient(void) const { return mIsDirectRxFromClient; }
Matches(ServiceUpdateId aId) const954         bool                     Matches(ServiceUpdateId aId) const { return mId == aId; }
955 
956     private:
957         UpdateMetadata(Instance &aInstance, Host &aHost, const MessageMetadata &aMessageMetadata);
958 
959         UpdateMetadata   *mNext;
960         TimeMilli         mExpireTime;
961         Dns::UpdateHeader mDnsHeader;
962         ServiceUpdateId   mId;          // The ID of this service update transaction.
963         TtlConfig         mTtlConfig;   // TTL config to use when processing the message.
964         LeaseConfig       mLeaseConfig; // Lease config to use when processing the message.
965         Host             &mHost;        // The `UpdateMetadata` has no ownership of this host.
966         Ip6::MessageInfo  mMessageInfo; // Valid when `mIsDirectRxFromClient` is true.
967         Error             mError;
968         bool              mIsDirectRxFromClient;
969     };
970 
971     void              Enable(void);
972     void              Disable(void);
973     void              Start(void);
974     void              Stop(void);
975     void              InitPort(void);
976     void              SelectPort(void);
977     Error             PrepareSocket(void);
978     Ip6::Udp::Socket &GetSocket(void);
GetHosts(void)979     LinkedList<Host> &GetHosts(void) { return mHosts; }
980 
981 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
982     void  HandleDnssdServerStateChange(void);
983     Error HandleDnssdServerUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
984 #endif
985 
986     void HandleNetDataPublisherEvent(NetworkData::Publisher::Event aEvent);
987 
AllocateServiceUpdateId(void)988     ServiceUpdateId AllocateServiceUpdateId(void) { return mServiceUpdateId++; }
989 
990     void  InformUpdateHandlerOrCommit(Error aError, Host &aHost, const MessageMetadata &aMetadata);
991     void  CommitSrpUpdate(Error aError, Host &aHost, const MessageMetadata &aMessageMetadata);
992     void  CommitSrpUpdate(UpdateMetadata &aUpdateMetadata);
993     void  CommitSrpUpdate(Error                    aError,
994                           Host                    &aHost,
995                           const Dns::UpdateHeader &aDnsHeader,
996                           const Ip6::MessageInfo  *aMessageInfo,
997                           const TtlConfig         &aTtlConfig,
998                           const LeaseConfig       &aLeaseConfig);
999     Error ProcessMessage(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
1000     Error ProcessMessage(Message                &aMessage,
1001                          TimeMilli               aRxTime,
1002                          const TtlConfig        &aTtlConfig,
1003                          const LeaseConfig      &aLeaseConfig,
1004                          const Ip6::MessageInfo *aMessageInfo);
1005     void  ProcessDnsUpdate(Message &aMessage, MessageMetadata &aMetadata);
1006     Error ProcessUpdateSection(Host &aHost, const Message &aMessage, MessageMetadata &aMetadata) const;
1007     Error ProcessAdditionalSection(Host *aHost, const Message &aMessage, MessageMetadata &aMetadata) const;
1008     Error VerifySignature(const Host::Key  &aKey,
1009                           const Message    &aMessage,
1010                           Dns::UpdateHeader aDnsHeader,
1011                           uint16_t          aSigOffset,
1012                           uint16_t          aSigRdataOffset,
1013                           uint16_t          aSigRdataLength,
1014                           const char       *aSignerName) const;
1015     Error ProcessZoneSection(const Message &aMessage, MessageMetadata &aMetadata) const;
1016     Error ProcessHostDescriptionInstruction(Host                  &aHost,
1017                                             const Message         &aMessage,
1018                                             const MessageMetadata &aMetadata) const;
1019     Error ProcessServiceDiscoveryInstructions(Host                  &aHost,
1020                                               const Message         &aMessage,
1021                                               const MessageMetadata &aMetadata) const;
1022     Error ProcessServiceDescriptionInstructions(Host &aHost, const Message &aMessage, MessageMetadata &aMetadata) const;
1023 
1024     static bool IsValidDeleteAllRecord(const Dns::ResourceRecord &aRecord);
1025 
1026     void        HandleUpdate(Host &aHost, const MessageMetadata &aMetadata);
1027     void        RemoveHost(Host *aHost, RetainName aRetainName);
1028     bool        HasNameConflictsWith(Host &aHost) const;
1029     void        SendResponse(const Dns::UpdateHeader    &aHeader,
1030                              Dns::UpdateHeader::Response aResponseCode,
1031                              const Ip6::MessageInfo     &aMessageInfo);
1032     void        SendResponse(const Dns::UpdateHeader &aHeader,
1033                              uint32_t                 aLease,
1034                              uint32_t                 aKeyLease,
1035                              bool                     mUseShortLeaseOption,
1036                              const Ip6::MessageInfo  &aMessageInfo);
1037     void        HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
1038     void        HandleLeaseTimer(void);
1039     static void HandleOutstandingUpdatesTimer(Timer &aTimer);
1040     void        HandleOutstandingUpdatesTimer(void);
1041     void        ProcessCompletedUpdates(void);
1042 
1043     const UpdateMetadata *FindOutstandingUpdate(const MessageMetadata &aMessageMetadata) const;
1044     static const char    *AddressModeToString(AddressMode aMode);
1045 
1046     void UpdateResponseCounters(Dns::Header::Response aResponseCode);
1047     void UpdateAddrResolverCacheTable(const Ip6::MessageInfo &aMessageInfo, const Host &aHost);
1048 
1049     using LeaseTimer           = TimerMilliIn<Server, &Server::HandleLeaseTimer>;
1050     using UpdateTimer          = TimerMilliIn<Server, &Server::HandleOutstandingUpdatesTimer>;
1051     using CompletedUpdatesTask = TaskletIn<Server, &Server::ProcessCompletedUpdates>;
1052     using ServerSocket         = Ip6::Udp::SocketIn<Server, &Server::HandleUdpReceive>;
1053 
1054     ServerSocket mSocket;
1055 
1056     Callback<otSrpServerServiceUpdateHandler> mServiceUpdateHandler;
1057 
1058     Heap::String mDomain;
1059 
1060     TtlConfig   mTtlConfig;
1061     LeaseConfig mLeaseConfig;
1062 
1063     LinkedList<Host> mHosts;
1064     LeaseTimer       mLeaseTimer;
1065 
1066     UpdateTimer                mOutstandingUpdatesTimer;
1067     LinkedList<UpdateMetadata> mOutstandingUpdates;
1068     LinkedList<UpdateMetadata> mCompletedUpdates;
1069     CompletedUpdatesTask       mCompletedUpdateTask;
1070 
1071     ServiceUpdateId mServiceUpdateId;
1072     uint16_t        mPort;
1073     State           mState;
1074     AddressMode     mAddressMode;
1075     uint8_t         mAnycastSequenceNumber;
1076     bool            mHasRegisteredAnyService : 1;
1077 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
1078     bool mAutoEnable : 1;
1079 #endif
1080 
1081     otSrpServerResponseCounters mResponseCounters;
1082 };
1083 
1084 } // namespace Srp
1085 
1086 DefineCoreType(otSrpServerTtlConfig, Srp::Server::TtlConfig);
1087 DefineCoreType(otSrpServerLeaseConfig, Srp::Server::LeaseConfig);
1088 DefineCoreType(otSrpServerHost, Srp::Server::Host);
1089 DefineCoreType(otSrpServerService, Srp::Server::Service);
1090 DefineMapEnum(otSrpServerState, Srp::Server::State);
1091 DefineMapEnum(otSrpServerAddressMode, Srp::Server::AddressMode);
1092 
1093 } // namespace ot
1094 
1095 #endif // OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
1096 #endif // NET_SRP_SERVER_HPP_
1097