1 /*
2  *  Copyright (c) 2024, 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 #ifndef OT_POSIX_PLATFORM_MDNS_SOCKET_HPP_
29 #define OT_POSIX_PLATFORM_MDNS_SOCKET_HPP_
30 
31 #include "openthread-posix-config.h"
32 
33 #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE
34 
35 #include <openthread/nat64.h>
36 #include <openthread/platform/mdns_socket.h>
37 
38 #include "logger.hpp"
39 #include "mainloop.hpp"
40 
41 #include "core/common/non_copyable.hpp"
42 
43 namespace ot {
44 namespace Posix {
45 
46 /**
47  * Implements platform mDNS socket APIs.
48  *
49  */
50 class MdnsSocket : public Mainloop::Source, public Logger<MdnsSocket>, private NonCopyable
51 {
52 public:
53     static const char kLogModuleName[]; ///< Module name used for logging.
54 
55     /**
56      * Gets the `MdnsSocket` singleton.
57      *
58      * @returns The singleton object.
59      *
60      */
61     static MdnsSocket &Get(void);
62 
63     /**
64      * Initializes the `MdnsSocket`.
65      *
66      * Called before OpenThread instance is created.
67      *
68      */
69     void Init(void);
70 
71     /**
72      * Sets up the `MdnsSocket`.
73      *
74      * Called after OpenThread instance is created.
75      *
76      */
77     void SetUp(void);
78 
79     /**
80      * Tears down the `MdnsSocket`.
81      *
82      * Called before OpenThread instance is destructed.
83      *
84      */
85     void TearDown(void);
86 
87     /**
88      * Deinitializes the `MdnsSocket`.
89      *
90      * Called after OpenThread instance is destructed.
91      *
92      */
93     void Deinit(void);
94 
95     /**
96      * Updates the fd_set and timeout for mainloop.
97      *
98      * @param[in,out]   aContext    A reference to the mainloop context.
99      *
100      */
101     void Update(otSysMainloopContext &aContext) override;
102 
103     /**
104      * Performs `MdnsSocket` processing.
105      *
106      * @param[in]   aContext   A reference to the mainloop context.
107      *
108      */
109     void Process(const otSysMainloopContext &aContext) override;
110 
111     // otPlatMdns APIs
112     otError SetListeningEnabled(otInstance *aInstance, bool aEnable, uint32_t aInfraIfIndex);
113     void    SendMulticast(otMessage *aMessage, uint32_t aInfraIfIndex);
114     void    SendUnicast(otMessage *aMessage, const otPlatMdnsAddressInfo *aAddress);
115 
116 private:
117     static constexpr uint16_t kMaxMessageLength = 2000;
118     static constexpr uint16_t kMdnsPort         = 5353;
119 
120     enum MsgType : uint8_t
121     {
122         kIp6Msg,
123         kIp4Msg,
124     };
125 
126     struct Metadata
127     {
CanFreeMessageot::Posix::MdnsSocket::Metadata128         bool CanFreeMessage(void) const { return (mIp6Port == 0) && (mIp4Port == 0); }
129 
130         otIp6Address mIp6Address;
131         uint16_t     mIp6Port;
132         otIp4Address mIp4Address;
133         uint16_t     mIp4Port;
134     };
135 
136     bool           mEnabled;
137     uint32_t       mInfraIfIndex;
138     int            mFd4;
139     int            mFd6;
140     uint32_t       mPendingIp6Tx;
141     uint32_t       mPendingIp4Tx;
142     otMessageQueue mTxQueue;
143     otIp6Address   mMulticastIp6Address;
144     otIp4Address   mMulticastIp4Address;
145     otInstance    *mInstance;
146 
147     otError Enable(uint32_t aInfraIfIndex);
148     void    Disable(uint32_t aInfraIfIndex);
149     void    ClearTxQueue(void);
150     void    SendQueuedMessages(MsgType aMsgType);
151     void    ReceiveMessage(MsgType aMsgType);
152 
153     otError OpenIp4Socket(uint32_t aInfraIfIndex);
154     otError JoinOrLeaveIp4MulticastGroup(bool aJoin, uint32_t aInfraIfIndex);
155     void    CloseIp4Socket(void);
156     otError OpenIp6Socket(uint32_t aInfraIfIndex);
157     otError JoinOrLeaveIp6MulticastGroup(bool aJoin, uint32_t aInfraIfIndex);
158     void    CloseIp6Socket(void);
159 
160     static otError SetReuseAddrPortOptions(int aFd);
161 
162     template <typename ValueType>
SetSocketOption(int aFd,int aLevel,int aOption,const ValueType & aValue,const char * aOptionName)163     static otError SetSocketOption(int aFd, int aLevel, int aOption, const ValueType &aValue, const char *aOptionName)
164     {
165         return SetSocketOptionValue(aFd, aLevel, aOption, &aValue, sizeof(ValueType), aOptionName);
166     }
167 
168     static otError SetSocketOptionValue(int         aFd,
169                                         int         aLevel,
170                                         int         aOption,
171                                         const void *aValue,
172                                         uint32_t    aValueLength,
173                                         const char *aOptionName);
174 };
175 
176 } // namespace Posix
177 } // namespace ot
178 
179 #endif // OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE
180 
181 #endif // OT_POSIX_PLATFORM_MDNS_SOCKET_HPP_
182