1 /*
2  *  Copyright (c) 2016, 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 UDP/IPv6 sockets.
32  */
33 
34 #ifndef UDP6_HPP_
35 #define UDP6_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <openthread/udp.h>
40 #include <openthread/platform/udp.h>
41 
42 #include "common/as_core_type.hpp"
43 #include "common/callback.hpp"
44 #include "common/clearable.hpp"
45 #include "common/linked_list.hpp"
46 #include "common/locator.hpp"
47 #include "common/non_copyable.hpp"
48 #include "net/ip6_headers.hpp"
49 
50 namespace ot {
51 namespace Ip6 {
52 
53 class Udp;
54 
55 /**
56  * @addtogroup core-udp
57  *
58  * @brief
59  *   This module includes definitions for UDP/IPv6 sockets.
60  *
61  * @{
62  *
63  */
64 
65 #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE && OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
66 #error "OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE and OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE must not both be set."
67 #endif
68 
69 /**
70  * Defines the network interface identifiers.
71  *
72  */
73 enum NetifIdentifier : uint8_t
74 {
75     kNetifUnspecified = OT_NETIF_UNSPECIFIED, ///< Unspecified network interface.
76     kNetifThread      = OT_NETIF_THREAD,      ///< The Thread interface.
77     kNetifBackbone    = OT_NETIF_BACKBONE,    ///< The Backbone interface.
78 };
79 
80 /**
81  * Implements core UDP message handling.
82  *
83  */
84 class Udp : public InstanceLocator, private NonCopyable
85 {
86 public:
87     typedef otUdpReceive ReceiveHandler; ///< Receive handler callback.
88 
89     /**
90      * Implements a UDP/IPv6 socket.
91      *
92      */
93     class SocketHandle : public otUdpSocket, public LinkedListEntry<SocketHandle>, public Clearable<SocketHandle>
94     {
95         friend class Udp;
96         friend class LinkedList<SocketHandle>;
97 
98     public:
99         /**
100          * Indicates whether or not the socket is bound.
101          *
102          * @retval TRUE if the socket is bound (i.e. source port is non-zero).
103          * @retval FALSE if the socket is not bound (source port is zero).
104          *
105          */
IsBound(void) const106         bool IsBound(void) const { return mSockName.mPort != 0; }
107 
108         /**
109          * Returns the local socket address.
110          *
111          * @returns A reference to the local socket address.
112          *
113          */
GetSockName(void)114         SockAddr &GetSockName(void) { return AsCoreType(&mSockName); }
115 
116         /**
117          * Returns the local socket address.
118          *
119          * @returns A reference to the local socket address.
120          *
121          */
GetSockName(void) const122         const SockAddr &GetSockName(void) const { return AsCoreType(&mSockName); }
123 
124         /**
125          * Returns the peer's socket address.
126          *
127          * @returns A reference to the peer's socket address.
128          *
129          */
GetPeerName(void)130         SockAddr &GetPeerName(void) { return AsCoreType(&mPeerName); }
131 
132         /**
133          * Returns the peer's socket address.
134          *
135          * @returns A reference to the peer's socket address.
136          *
137          */
GetPeerName(void) const138         const SockAddr &GetPeerName(void) const { return AsCoreType(&mPeerName); }
139 
140     private:
141         bool Matches(const MessageInfo &aMessageInfo) const;
142 
HandleUdpReceive(Message & aMessage,const MessageInfo & aMessageInfo)143         void HandleUdpReceive(Message &aMessage, const MessageInfo &aMessageInfo)
144         {
145             mHandler(mContext, &aMessage, &aMessageInfo);
146         }
147     };
148 
149     /**
150      * Implements a UDP/IPv6 socket.
151      *
152      */
153     class Socket : public InstanceLocator, public SocketHandle
154     {
155         friend class Udp;
156 
157     public:
158         /**
159          * Initializes the object.
160          *
161          * @param[in]  aInstance  A reference to OpenThread instance.
162          * @param[in]  aHandler  A pointer to a function that is called when receiving UDP messages.
163          * @param[in]  aContext  A pointer to arbitrary context information.
164          *
165          */
166         Socket(Instance &aInstance, ReceiveHandler aHandler, void *aContext);
167 
168         /**
169          * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
170          *
171          * @returns A pointer to the message or `nullptr` if no buffers are available.
172          *
173          */
174         Message *NewMessage(void);
175 
176         /**
177          * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
178          *
179          * @param[in]  aReserved  The number of header bytes to reserve after the UDP header.
180          *
181          * @returns A pointer to the message or `nullptr` if no buffers are available.
182          *
183          */
184         Message *NewMessage(uint16_t aReserved);
185 
186         /**
187          * Returns a new UDP message with sufficient header space reserved.
188          *
189          * @param[in]  aReserved  The number of header bytes to reserve after the UDP header.
190          * @param[in]  aSettings  The message settings (default is used if not provided).
191          *
192          * @returns A pointer to the message or `nullptr` if no buffers are available.
193          *
194          */
195         Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings);
196 
197         /**
198          * Opens the UDP socket.
199          *
200          * @retval kErrorNone     Successfully opened the socket.
201          * @retval kErrorFailed   Failed to open the socket.
202          *
203          */
204         Error Open(void);
205 
206         /**
207          * Returns if the UDP socket is open.
208          *
209          * @returns If the UDP socket is open.
210          *
211          */
212         bool IsOpen(void) const;
213 
214         /**
215          * Binds the UDP socket.
216          *
217          * @param[in]  aSockAddr            A reference to the socket address.
218          * @param[in]  aNetifIdentifier     The network interface identifier.
219          *
220          * @retval kErrorNone            Successfully bound the socket.
221          * @retval kErrorInvalidArgs     Unable to bind to Thread network interface with the given address.
222          * @retval kErrorFailed          Failed to bind UDP Socket.
223          *
224          */
225         Error Bind(const SockAddr &aSockAddr, NetifIdentifier aNetifIdentifier = kNetifThread);
226 
227         /**
228          * Binds the UDP socket.
229          *
230          * @param[in]  aPort                A port number.
231          * @param[in]  aNetifIdentifier     The network interface identifier.
232          *
233          * @retval kErrorNone            Successfully bound the socket.
234          * @retval kErrorFailed          Failed to bind UDP Socket.
235          *
236          */
237         Error Bind(uint16_t aPort, NetifIdentifier aNetifIdentifier = kNetifThread);
238 
239         /**
240          * Binds the UDP socket.
241          *
242          * @retval kErrorNone    Successfully bound the socket.
243          * @retval kErrorFailed  Failed to bind UDP Socket.
244          *
245          */
Bind(void)246         Error Bind(void) { return Bind(0); }
247 
248         /**
249          * Connects the UDP socket.
250          *
251          * @param[in]  aSockAddr  A reference to the socket address.
252          *
253          * @retval kErrorNone    Successfully connected the socket.
254          * @retval kErrorFailed  Failed to connect UDP Socket.
255          *
256          */
257         Error Connect(const SockAddr &aSockAddr);
258 
259         /**
260          * Connects the UDP socket.
261          *
262          * @param[in]  aPort        A port number.
263          *
264          * @retval kErrorNone    Successfully connected the socket.
265          * @retval kErrorFailed  Failed to connect UDP Socket.
266          *
267          */
268         Error Connect(uint16_t aPort);
269 
270         /**
271          * Connects the UDP socket.
272          *
273          * @retval kErrorNone    Successfully connected the socket.
274          * @retval kErrorFailed  Failed to connect UDP Socket.
275          *
276          */
Connect(void)277         Error Connect(void) { return Connect(0); }
278 
279         /**
280          * Closes the UDP socket.
281          *
282          * @retval kErrorNone    Successfully closed the UDP socket.
283          * @retval kErrorFailed  Failed to close UDP Socket.
284          *
285          */
286         Error Close(void);
287 
288         /**
289          * Sends a UDP message.
290          *
291          * @param[in]  aMessage      The message to send.
292          * @param[in]  aMessageInfo  The message info associated with @p aMessage.
293          *
294          * @retval kErrorNone         Successfully sent the UDP message.
295          * @retval kErrorInvalidArgs  If no peer is specified in @p aMessageInfo or by Connect().
296          * @retval kErrorNoBufs       Insufficient available buffer to add the UDP and IPv6 headers.
297          *
298          */
299         Error SendTo(Message &aMessage, const MessageInfo &aMessageInfo);
300 
301 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
302         /**
303          * Configures the UDP socket to join a multicast group on a Host network interface.
304          *
305          * @param[in]  aNetifIdentifier     The network interface identifier.
306          * @param[in]  aAddress             The multicast group address.
307          *
308          * @retval  kErrorNone    Successfully joined the multicast group.
309          * @retval  kErrorFailed  Failed to join the multicast group.
310          *
311          */
312         Error JoinNetifMulticastGroup(NetifIdentifier aNetifIdentifier, const Address &aAddress);
313 
314         /**
315          * Configures the UDP socket to leave a multicast group on a Host network interface.
316          *
317          * @param[in]  aNetifIdentifier     The network interface identifier.
318          * @param[in]  aAddress             The multicast group address.
319          *
320          * @retval  kErrorNone   Successfully left the multicast group.
321          * @retval  kErrorFailed Failed to leave the multicast group.
322          *
323          */
324         Error LeaveNetifMulticastGroup(NetifIdentifier aNetifIdentifier, const Address &aAddress);
325 #endif
326     };
327 
328     /**
329      * A socket owned by a specific type with a given  owner type as the callback.
330      *
331      * @tparam Owner                The type of the owner of this socket.
332      * @tparam HandleUdpReceivePtr  A pointer to a non-static member method of `Owner` to handle received messages.
333      *
334      */
335     template <typename Owner, void (Owner::*HandleUdpReceivePtr)(Message &aMessage, const MessageInfo &aMessageInfo)>
336     class SocketIn : public Socket
337     {
338     public:
339         /**
340          * Initializes the socket.
341          *
342          * @param[in]  aInstance   The OpenThread instance.
343          * @param[in]  aOnwer      The owner of the socket, providing the `HandleUdpReceivePtr` callback.
344          *
345          */
SocketIn(Instance & aInstance,Owner & aOwner)346         explicit SocketIn(Instance &aInstance, Owner &aOwner)
347             : Socket(aInstance, HandleUdpReceive, &aOwner)
348         {
349         }
350 
351     private:
HandleUdpReceive(void * aContext,otMessage * aMessage,const otMessageInfo * aMessageInfo)352         static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
353         {
354             (reinterpret_cast<Owner *>(aContext)->*HandleUdpReceivePtr)(AsCoreType(aMessage), AsCoreType(aMessageInfo));
355         }
356     };
357 
358     /**
359      * Implements a UDP receiver.
360      *
361      */
362     class Receiver : public otUdpReceiver, public LinkedListEntry<Receiver>
363     {
364         friend class Udp;
365 
366     public:
367         /**
368          * Initializes the UDP receiver.
369          *
370          * @param[in]   aHandler     A pointer to the function to handle UDP message.
371          * @param[in]   aContext     A pointer to arbitrary context information.
372          *
373          */
Receiver(otUdpHandler aHandler,void * aContext)374         Receiver(otUdpHandler aHandler, void *aContext)
375         {
376             mNext    = nullptr;
377             mHandler = aHandler;
378             mContext = aContext;
379         }
380 
381     private:
HandleMessage(Message & aMessage,const MessageInfo & aMessageInfo)382         bool HandleMessage(Message &aMessage, const MessageInfo &aMessageInfo)
383         {
384             return mHandler(mContext, &aMessage, &aMessageInfo);
385         }
386     };
387 
388     /**
389      * Implements UDP header generation and parsing.
390      *
391      */
392     OT_TOOL_PACKED_BEGIN
393     class Header : public Clearable<Header>
394     {
395     public:
396         static constexpr uint16_t kSourcePortFieldOffset = 0; ///< Byte offset of Source Port field in UDP header.
397         static constexpr uint16_t kDestPortFieldOffset   = 2; ///< Byte offset of Destination Port field in UDP header.
398         static constexpr uint16_t kLengthFieldOffset     = 4; ///< Byte offset of Length field in UDP header.
399         static constexpr uint16_t kChecksumFieldOffset   = 6; ///< Byte offset of Checksum field in UDP header.
400 
401         /**
402          * Returns the UDP Source Port.
403          *
404          * @returns The UDP Source Port.
405          *
406          */
GetSourcePort(void) const407         uint16_t GetSourcePort(void) const { return BigEndian::HostSwap16(mSourcePort); }
408 
409         /**
410          * Sets the UDP Source Port.
411          *
412          * @param[in]  aPort  The UDP Source Port.
413          *
414          */
SetSourcePort(uint16_t aPort)415         void SetSourcePort(uint16_t aPort) { mSourcePort = BigEndian::HostSwap16(aPort); }
416 
417         /**
418          * Returns the UDP Destination Port.
419          *
420          * @returns The UDP Destination Port.
421          *
422          */
GetDestinationPort(void) const423         uint16_t GetDestinationPort(void) const { return BigEndian::HostSwap16(mDestinationPort); }
424 
425         /**
426          * Sets the UDP Destination Port.
427          *
428          * @param[in]  aPort  The UDP Destination Port.
429          *
430          */
SetDestinationPort(uint16_t aPort)431         void SetDestinationPort(uint16_t aPort) { mDestinationPort = BigEndian::HostSwap16(aPort); }
432 
433         /**
434          * Returns the UDP Length.
435          *
436          * @returns The UDP Length.
437          *
438          */
GetLength(void) const439         uint16_t GetLength(void) const { return BigEndian::HostSwap16(mLength); }
440 
441         /**
442          * Sets the UDP Length.
443          *
444          * @param[in]  aLength  The UDP Length.
445          *
446          */
SetLength(uint16_t aLength)447         void SetLength(uint16_t aLength) { mLength = BigEndian::HostSwap16(aLength); }
448 
449         /**
450          * Returns the UDP Checksum.
451          *
452          * @returns The UDP Checksum.
453          *
454          */
GetChecksum(void) const455         uint16_t GetChecksum(void) const { return BigEndian::HostSwap16(mChecksum); }
456 
457         /**
458          * Sets the UDP Checksum.
459          *
460          * @param[in]  aChecksum  The UDP Checksum.
461          *
462          */
SetChecksum(uint16_t aChecksum)463         void SetChecksum(uint16_t aChecksum) { mChecksum = BigEndian::HostSwap16(aChecksum); }
464 
465     private:
466         uint16_t mSourcePort;
467         uint16_t mDestinationPort;
468         uint16_t mLength;
469         uint16_t mChecksum;
470 
471     } OT_TOOL_PACKED_END;
472 
473     /**
474      * Initializes the object.
475      *
476      * @param[in]  aInstance  A reference to OpenThread instance.
477      *
478      */
479     explicit Udp(Instance &aInstance);
480 
481     /**
482      * Adds a UDP receiver.
483      *
484      * @param[in]  aReceiver  A reference to the UDP receiver.
485      *
486      * @retval kErrorNone    Successfully added the UDP receiver.
487      * @retval kErrorAlready The UDP receiver was already added.
488      *
489      */
490     Error AddReceiver(Receiver &aReceiver);
491 
492     /**
493      * Removes a UDP receiver.
494      *
495      * @param[in]  aReceiver  A reference to the UDP receiver.
496      *
497      * @retval kErrorNone       Successfully removed the UDP receiver.
498      * @retval kErrorNotFound   The UDP receiver was not added.
499      *
500      */
501     Error RemoveReceiver(Receiver &aReceiver);
502 
503     /**
504      * Opens a UDP socket.
505      *
506      * @param[in]  aSocket   A reference to the socket.
507      * @param[in]  aHandler  A pointer to a function that is called when receiving UDP messages.
508      * @param[in]  aContext  A pointer to arbitrary context information.
509      *
510      * @retval kErrorNone     Successfully opened the socket.
511      * @retval kErrorFailed   Failed to open the socket.
512      *
513      */
514     Error Open(SocketHandle &aSocket, ReceiveHandler aHandler, void *aContext);
515 
516     /**
517      * Returns if a UDP socket is open.
518      *
519      * @param[in]  aSocket   A reference to the socket.
520      *
521      * @returns If the UDP socket is open.
522      *
523      */
IsOpen(const SocketHandle & aSocket) const524     bool IsOpen(const SocketHandle &aSocket) const { return mSockets.Contains(aSocket); }
525 
526     /**
527      * Binds a UDP socket.
528      *
529      * @param[in]  aSocket          A reference to the socket.
530      * @param[in]  aSockAddr        A reference to the socket address.
531      * @param[in]  aNetifIdentifier The network interface identifier.
532      *
533      * @retval kErrorNone            Successfully bound the socket.
534      * @retval kErrorInvalidArgs     Unable to bind to Thread network interface with the given address.
535      * @retval kErrorFailed          Failed to bind UDP Socket.
536      *
537      */
538     Error Bind(SocketHandle &aSocket, const SockAddr &aSockAddr, NetifIdentifier aNetifIdentifier);
539 
540     /**
541      * Connects a UDP socket.
542      *
543      * @param[in]  aSocket    A reference to the socket.
544      * @param[in]  aSockAddr  A reference to the socket address.
545      *
546      * @retval kErrorNone    Successfully connected the socket.
547      * @retval kErrorFailed  Failed to connect UDP Socket.
548      *
549      */
550     Error Connect(SocketHandle &aSocket, const SockAddr &aSockAddr);
551 
552     /**
553      * Closes the UDP socket.
554      *
555      * @param[in]  aSocket    A reference to the socket.
556      *
557      * @retval kErrorNone    Successfully closed the UDP socket.
558      * @retval kErrorFailed  Failed to close UDP Socket.
559      *
560      */
561     Error Close(SocketHandle &aSocket);
562 
563     /**
564      * Sends a UDP message using a socket.
565      *
566      * @param[in]  aSocket       A reference to the socket.
567      * @param[in]  aMessage      The message to send.
568      * @param[in]  aMessageInfo  The message info associated with @p aMessage.
569      *
570      * @retval kErrorNone         Successfully sent the UDP message.
571      * @retval kErrorInvalidArgs  If no peer is specified in @p aMessageInfo or by Connect().
572      * @retval kErrorNoBufs       Insufficient available buffer to add the UDP and IPv6 headers.
573      *
574      */
575     Error SendTo(SocketHandle &aSocket, Message &aMessage, const MessageInfo &aMessageInfo);
576 
577     /**
578      * Returns a new ephemeral port.
579      *
580      * @returns A new ephemeral port.
581      *
582      */
583     uint16_t GetEphemeralPort(void);
584 
585     /**
586      * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
587      *
588      * @returns A pointer to the message or `nullptr` if no buffers are available.
589      *
590      */
591     Message *NewMessage(void);
592 
593     /**
594      * Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
595      *
596      * @param[in]  aReserved  The number of header bytes to reserve after the UDP header.
597      *
598      * @returns A pointer to the message or `nullptr` if no buffers are available.
599      *
600      */
601     Message *NewMessage(uint16_t aReserved);
602 
603     /**
604      * Returns a new UDP message with sufficient header space reserved.
605      *
606      * @param[in]  aReserved  The number of header bytes to reserve after the UDP header.
607      * @param[in]  aSettings  The message settings.
608      *
609      * @returns A pointer to the message or `nullptr` if no buffers are available.
610      *
611      */
612     Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings);
613 
614     /**
615      * Sends an IPv6 datagram.
616      *
617      * @param[in]  aMessage      A reference to the message.
618      * @param[in]  aMessageInfo  A reference to the message info associated with @p aMessage.
619      *
620      * @retval kErrorNone    Successfully enqueued the message into an output interface.
621      * @retval kErrorNoBufs  Insufficient available buffer to add the IPv6 headers.
622      *
623      */
624     Error SendDatagram(Message &aMessage, MessageInfo &aMessageInfo);
625 
626     /**
627      * Handles a received UDP message.
628      *
629      * @param[in]  aMessage      A reference to the UDP message to process.
630      * @param[in]  aMessageInfo  A reference to the message info associated with @p aMessage.
631      *
632      * @retval kErrorNone  Successfully processed the UDP message.
633      * @retval kErrorDrop  Could not fully process the UDP message.
634      *
635      */
636     Error HandleMessage(Message &aMessage, MessageInfo &aMessageInfo);
637 
638     /**
639      * Handles a received UDP message with offset set to the payload.
640      *
641      * @param[in]  aMessage      A reference to the UDP message to process.
642      * @param[in]  aMessageInfo  A reference to the message info associated with @p aMessage.
643      *
644      */
645     void HandlePayload(Message &aMessage, MessageInfo &aMessageInfo);
646 
647     /**
648      * Returns the head of UDP Sockets list.
649      *
650      * @returns A pointer to the head of UDP Socket linked list.
651      *
652      */
GetUdpSockets(void)653     SocketHandle *GetUdpSockets(void) { return mSockets.GetHead(); }
654 
655 #if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
656     /**
657      * Sets the forward sender.
658      *
659      * @param[in]   aForwarder  A function pointer to forward UDP packets.
660      * @param[in]   aContext    A pointer to arbitrary context information.
661      *
662      */
SetUdpForwarder(otUdpForwarder aForwarder,void * aContext)663     void SetUdpForwarder(otUdpForwarder aForwarder, void *aContext) { mUdpForwarder.Set(aForwarder, aContext); }
664 #endif
665 
666     /**
667      * Returns whether a udp port is being used by OpenThread or any of it's optional
668      * features, e.g. CoAP API.
669      *
670      * @param[in]   aPort       The udp port
671      *
672      * @retval True when port is used by the OpenThread.
673      * @retval False when the port is not used by OpenThread.
674      *
675      */
676     bool IsPortInUse(uint16_t aPort) const;
677 
678     /**
679      * Returns whether a udp port belongs to the platform or the stack.
680      *
681      * @param[in]   aPort       The udp port
682      *
683      * @retval True when the port belongs to the platform.
684      * @retval False when the port belongs to the stack.
685      *
686      */
687     bool ShouldUsePlatformUdp(uint16_t aPort) const;
688 
689 private:
690     static constexpr uint16_t kDynamicPortMin = 49152; // Service Name and Transport Protocol Port Number Registry
691     static constexpr uint16_t kDynamicPortMax = 65535; // Service Name and Transport Protocol Port Number Registry
692 
693     // Reserved range for use by SRP server
694     static constexpr uint16_t kSrpServerPortMin = OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT_MIN;
695     static constexpr uint16_t kSrpServerPortMax = OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT_MAX;
696 
697     static bool IsPortReserved(uint16_t aPort);
698 
699     void AddSocket(SocketHandle &aSocket);
700     void RemoveSocket(SocketHandle &aSocket);
701 #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE
702     bool ShouldUsePlatformUdp(const SocketHandle &aSocket) const;
703 #endif
704 
705 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
706     void                SetBackboneSocket(SocketHandle &aSocket);
707     const SocketHandle *GetBackboneSockets(void) const;
708     bool                IsBackboneSocket(const SocketHandle &aSocket) const;
709 #endif
710 
711     uint16_t                 mEphemeralPort;
712     LinkedList<Receiver>     mReceivers;
713     LinkedList<SocketHandle> mSockets;
714 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
715     SocketHandle *mPrevBackboneSockets;
716 #endif
717 #if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
718     Callback<otUdpForwarder> mUdpForwarder;
719 #endif
720 };
721 
722 /**
723  * @}
724  *
725  */
726 
727 } // namespace Ip6
728 
729 DefineCoreType(otUdpSocket, Ip6::Udp::SocketHandle);
730 DefineCoreType(otUdpReceiver, Ip6::Udp::Receiver);
731 DefineMapEnum(otNetifIdentifier, Ip6::NetifIdentifier);
732 
733 } // namespace ot
734 
735 #endif // UDP6_HPP_
736