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