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/rcp_host.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] aHost A reference to the OpenThread Controller instance. 70 * @param[in] aPublisher A reference to the mDNS Publisher. 71 */ 72 explicit TrelDnssd(Ncp::RcpHost &aHost, Mdns::Publisher &aPublisher); 73 74 /** 75 * This method initializes the TrelDnssd instance. 76 * 77 * @param[in] aTrelNetif The network interface for discovering TREL peers. 78 */ 79 void Initialize(std::string aTrelNetif); 80 81 /** 82 * This method starts browsing for TREL peers. 83 */ 84 void StartBrowse(void); 85 86 /** 87 * This method stops browsing for TREL peers. 88 */ 89 void StopBrowse(void); 90 91 /** 92 * This method registers the TREL service to DNS-SD. 93 * 94 * @param[in] aPort The UDP port of TREL service. 95 * @param[in] aTxtData The TXT data of TREL service. 96 * @param[in] aTxtLength The TXT length of TREL service. 97 */ 98 void RegisterService(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 99 100 /** 101 * This method removes the TREL service from DNS-SD. 102 */ 103 void UnregisterService(void); 104 105 /** 106 * This method handles mDNS publisher's state changes. 107 * 108 * @param[in] aState The state of mDNS publisher. 109 */ 110 void HandleMdnsState(Mdns::Publisher::State aState); 111 112 private: 113 static constexpr size_t kPeerCacheSize = 256; 114 static constexpr uint16_t kCheckNetifReadyIntervalMs = 5000; 115 116 struct RegisterInfo 117 { 118 uint16_t mPort = 0; 119 Mdns::Publisher::TxtData mTxtData; 120 std::string mInstanceName; 121 IsValidotbr::TrelDnssd::TrelDnssd::RegisterInfo122 bool IsValid(void) const { return mPort > 0; } IsPublishedotbr::TrelDnssd::TrelDnssd::RegisterInfo123 bool IsPublished(void) const { return !mInstanceName.empty(); } 124 void Assign(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 125 void Clear(void); 126 }; 127 128 using Clock = std::chrono::system_clock; 129 130 struct Peer 131 { 132 static const char kTxtRecordExtAddressKey[]; 133 Peerotbr::TrelDnssd::TrelDnssd::Peer134 explicit Peer(std::vector<uint8_t> aTxtData, const otSockAddr &aSockAddr) 135 : mDiscoverTime(Clock::now()) 136 , mTxtData(std::move(aTxtData)) 137 , mSockAddr(aSockAddr) 138 { 139 ReadExtAddrFromTxtData(); 140 } 141 142 void ReadExtAddrFromTxtData(void); 143 144 Clock::time_point mDiscoverTime; 145 std::vector<uint8_t> mTxtData; 146 otSockAddr mSockAddr; 147 otExtAddress mExtAddr; 148 bool mValid = false; 149 }; 150 151 using PeerMap = std::map<std::string, Peer>; 152 IsInitialized(void) const153 bool IsInitialized(void) const { return !mTrelNetif.empty(); } 154 bool IsReady(void) const; 155 void OnBecomeReady(void); 156 void CheckTrelNetifReady(void); 157 std::string GetTrelInstanceName(void); 158 void PublishTrelService(void); 159 void UnpublishTrelService(void); 160 static void HandlePublishTrelServiceError(otbrError aError); 161 static void HandleUnpublishTrelServiceError(otbrError aError); 162 void OnTrelServiceInstanceResolved(const std::string &aType, 163 const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo); 164 void OnTrelServiceInstanceAdded(const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo); 165 void OnTrelServiceInstanceRemoved(const std::string &aInstanceName); 166 167 void NotifyRemovePeer(const Peer &aPeer); 168 void CheckPeersNumLimit(void); 169 void RemoveAllPeers(void); 170 uint16_t CountDuplicatePeers(const Peer &aPeer); 171 172 Mdns::Publisher &mPublisher; 173 Ncp::RcpHost &mHost; 174 TaskRunner mTaskRunner; 175 std::string mTrelNetif; 176 uint32_t mTrelNetifIndex = 0; 177 uint64_t mSubscriberId = 0; 178 RegisterInfo mRegisterInfo; 179 PeerMap mPeers; 180 bool mMdnsPublisherReady = false; 181 }; 182 183 /** 184 * @} 185 */ 186 187 } // namespace TrelDnssd 188 189 } // namespace otbr 190 191 #endif // OTBR_ENABLE_TREL 192 193 #endif // OTBR_AGENT_TREL_DNSSD_HPP_ 194