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 /** 30 * @file 31 * This file includes definitions for TREL DNS-SD over mDNS. 32 */ 33 34 #ifndef OTBR_AGENT_TREL_DNSSD_HPP_ 35 #define OTBR_AGENT_TREL_DNSSD_HPP_ 36 37 #include "openthread-br/config.h" 38 39 #if OTBR_ENABLE_TREL 40 41 #include <assert.h> 42 #include <utility> 43 44 #include <openthread/instance.h> 45 46 #include "common/types.hpp" 47 #include "mdns/mdns.hpp" 48 #include "ncp/ncp_openthread.hpp" 49 50 namespace otbr { 51 52 namespace TrelDnssd { 53 54 /** 55 * @addtogroup border-router-trel-dnssd 56 * 57 * @brief 58 * This module includes definition for TREL DNS-SD over mDNS. 59 * 60 * @{ 61 */ 62 63 class TrelDnssd 64 { 65 public: 66 /** 67 * This constructor initializes the TrelDnssd instance. 68 * 69 * @param[in] aNcp A reference to the OpenThread Controller instance. 70 * @param[in] aPublisher A reference to the mDNS Publisher. 71 * 72 */ 73 explicit TrelDnssd(Ncp::ControllerOpenThread &aNcp, Mdns::Publisher &aPublisher); 74 75 /** 76 * This method initializes the TrelDnssd instance. 77 * 78 * @param[in] aTrelNetif The network interface for discovering TREL peers. 79 * 80 */ 81 void Initialize(std::string aTrelNetif); 82 83 /** 84 * This method starts browsing for TREL peers. 85 * 86 */ 87 void StartBrowse(void); 88 89 /** 90 * This method stops browsing for TREL peers. 91 * 92 */ 93 void StopBrowse(void); 94 95 /** 96 * This method registers the TREL service to DNS-SD. 97 * 98 * @param[in] aPort The UDP port of TREL service. 99 * @param[in] aTxtData The TXT data of TREL service. 100 * @param[in] aTxtLength The TXT length of TREL service. 101 * 102 */ 103 void RegisterService(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 104 105 /** 106 * This method removes the TREL service from DNS-SD. 107 * 108 */ 109 void UnregisterService(void); 110 111 /** 112 * This method handles mDNS publisher's state changes. 113 * 114 * @param[in] aState The state of mDNS publisher. 115 * 116 */ 117 void HandleMdnsState(Mdns::Publisher::State aState); 118 119 private: 120 static constexpr size_t kPeerCacheSize = 256; 121 static constexpr uint16_t kCheckNetifReadyIntervalMs = 5000; 122 123 struct RegisterInfo 124 { 125 uint16_t mPort = 0; 126 Mdns::Publisher::TxtData mTxtData; 127 std::string mInstanceName; 128 IsValidotbr::TrelDnssd::TrelDnssd::RegisterInfo129 bool IsValid(void) const { return mPort > 0; } IsPublishedotbr::TrelDnssd::TrelDnssd::RegisterInfo130 bool IsPublished(void) const { return !mInstanceName.empty(); } 131 void Assign(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 132 void Clear(void); 133 }; 134 135 using Clock = std::chrono::system_clock; 136 137 struct Peer 138 { 139 static const char kTxtRecordExtAddressKey[]; 140 Peerotbr::TrelDnssd::TrelDnssd::Peer141 explicit Peer(std::vector<uint8_t> aTxtData, const otSockAddr &aSockAddr) 142 : mDiscoverTime(Clock::now()) 143 , mTxtData(std::move(aTxtData)) 144 , mSockAddr(aSockAddr) 145 { 146 ReadExtAddrFromTxtData(); 147 } 148 149 void ReadExtAddrFromTxtData(void); 150 151 Clock::time_point mDiscoverTime; 152 std::vector<uint8_t> mTxtData; 153 otSockAddr mSockAddr; 154 otExtAddress mExtAddr; 155 bool mValid = false; 156 }; 157 158 using PeerMap = std::map<std::string, Peer>; 159 IsInitialized(void) const160 bool IsInitialized(void) const { return !mTrelNetif.empty(); } 161 bool IsReady(void) const; 162 void OnBecomeReady(void); 163 void CheckTrelNetifReady(void); 164 std::string GetTrelInstanceName(void); 165 void PublishTrelService(void); 166 void UnpublishTrelService(void); 167 static void HandlePublishTrelServiceError(otbrError aError); 168 static void HandleUnpublishTrelServiceError(otbrError aError); 169 void OnTrelServiceInstanceResolved(const std::string &aType, 170 const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo); 171 void OnTrelServiceInstanceAdded(const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo); 172 void OnTrelServiceInstanceRemoved(const std::string &aInstanceName); 173 174 void NotifyRemovePeer(const Peer &aPeer); 175 void CheckPeersNumLimit(void); 176 void RemoveAllPeers(void); 177 uint16_t CountDuplicatePeers(const Peer &aPeer); 178 179 Mdns::Publisher &mPublisher; 180 Ncp::ControllerOpenThread &mNcp; 181 TaskRunner mTaskRunner; 182 std::string mTrelNetif; 183 uint32_t mTrelNetifIndex = 0; 184 uint64_t mSubscriberId = 0; 185 RegisterInfo mRegisterInfo; 186 PeerMap mPeers; 187 bool mMdnsPublisherReady = false; 188 }; 189 190 /** 191 * @} 192 */ 193 194 } // namespace TrelDnssd 195 196 } // namespace otbr 197 198 #endif // OTBR_ENABLE_TREL 199 200 #endif // OTBR_AGENT_TREL_DNSSD_HPP_ 201