1*4a64e381SAndroid Build Coastguard Worker /* 2*4a64e381SAndroid Build Coastguard Worker * Copyright (c) 2021, The OpenThread Authors. 3*4a64e381SAndroid Build Coastguard Worker * All rights reserved. 4*4a64e381SAndroid Build Coastguard Worker * 5*4a64e381SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*4a64e381SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met: 7*4a64e381SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright 8*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 9*4a64e381SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright 10*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the 11*4a64e381SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution. 12*4a64e381SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the 13*4a64e381SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products 14*4a64e381SAndroid Build Coastguard Worker * derived from this software without specific prior written permission. 15*4a64e381SAndroid Build Coastguard Worker * 16*4a64e381SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17*4a64e381SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*4a64e381SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*4a64e381SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20*4a64e381SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21*4a64e381SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22*4a64e381SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23*4a64e381SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24*4a64e381SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25*4a64e381SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26*4a64e381SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 27*4a64e381SAndroid Build Coastguard Worker */ 28*4a64e381SAndroid Build Coastguard Worker 29*4a64e381SAndroid Build Coastguard Worker /** 30*4a64e381SAndroid Build Coastguard Worker * @file 31*4a64e381SAndroid Build Coastguard Worker * This file includes definitions for TREL DNS-SD over mDNS. 32*4a64e381SAndroid Build Coastguard Worker */ 33*4a64e381SAndroid Build Coastguard Worker 34*4a64e381SAndroid Build Coastguard Worker #ifndef OTBR_AGENT_TREL_DNSSD_HPP_ 35*4a64e381SAndroid Build Coastguard Worker #define OTBR_AGENT_TREL_DNSSD_HPP_ 36*4a64e381SAndroid Build Coastguard Worker 37*4a64e381SAndroid Build Coastguard Worker #include "openthread-br/config.h" 38*4a64e381SAndroid Build Coastguard Worker 39*4a64e381SAndroid Build Coastguard Worker #if OTBR_ENABLE_TREL 40*4a64e381SAndroid Build Coastguard Worker 41*4a64e381SAndroid Build Coastguard Worker #include <assert.h> 42*4a64e381SAndroid Build Coastguard Worker #include <utility> 43*4a64e381SAndroid Build Coastguard Worker 44*4a64e381SAndroid Build Coastguard Worker #include <openthread/instance.h> 45*4a64e381SAndroid Build Coastguard Worker 46*4a64e381SAndroid Build Coastguard Worker #include "common/types.hpp" 47*4a64e381SAndroid Build Coastguard Worker #include "mdns/mdns.hpp" 48*4a64e381SAndroid Build Coastguard Worker #include "ncp/rcp_host.hpp" 49*4a64e381SAndroid Build Coastguard Worker 50*4a64e381SAndroid Build Coastguard Worker namespace otbr { 51*4a64e381SAndroid Build Coastguard Worker 52*4a64e381SAndroid Build Coastguard Worker namespace TrelDnssd { 53*4a64e381SAndroid Build Coastguard Worker 54*4a64e381SAndroid Build Coastguard Worker /** 55*4a64e381SAndroid Build Coastguard Worker * @addtogroup border-router-trel-dnssd 56*4a64e381SAndroid Build Coastguard Worker * 57*4a64e381SAndroid Build Coastguard Worker * @brief 58*4a64e381SAndroid Build Coastguard Worker * This module includes definition for TREL DNS-SD over mDNS. 59*4a64e381SAndroid Build Coastguard Worker * 60*4a64e381SAndroid Build Coastguard Worker * @{ 61*4a64e381SAndroid Build Coastguard Worker */ 62*4a64e381SAndroid Build Coastguard Worker 63*4a64e381SAndroid Build Coastguard Worker class TrelDnssd 64*4a64e381SAndroid Build Coastguard Worker { 65*4a64e381SAndroid Build Coastguard Worker public: 66*4a64e381SAndroid Build Coastguard Worker /** 67*4a64e381SAndroid Build Coastguard Worker * This constructor initializes the TrelDnssd instance. 68*4a64e381SAndroid Build Coastguard Worker * 69*4a64e381SAndroid Build Coastguard Worker * @param[in] aHost A reference to the OpenThread Controller instance. 70*4a64e381SAndroid Build Coastguard Worker * @param[in] aPublisher A reference to the mDNS Publisher. 71*4a64e381SAndroid Build Coastguard Worker */ 72*4a64e381SAndroid Build Coastguard Worker explicit TrelDnssd(Ncp::RcpHost &aHost, Mdns::Publisher &aPublisher); 73*4a64e381SAndroid Build Coastguard Worker 74*4a64e381SAndroid Build Coastguard Worker /** 75*4a64e381SAndroid Build Coastguard Worker * This method initializes the TrelDnssd instance. 76*4a64e381SAndroid Build Coastguard Worker * 77*4a64e381SAndroid Build Coastguard Worker * @param[in] aTrelNetif The network interface for discovering TREL peers. 78*4a64e381SAndroid Build Coastguard Worker */ 79*4a64e381SAndroid Build Coastguard Worker void Initialize(std::string aTrelNetif); 80*4a64e381SAndroid Build Coastguard Worker 81*4a64e381SAndroid Build Coastguard Worker /** 82*4a64e381SAndroid Build Coastguard Worker * This method starts browsing for TREL peers. 83*4a64e381SAndroid Build Coastguard Worker */ 84*4a64e381SAndroid Build Coastguard Worker void StartBrowse(void); 85*4a64e381SAndroid Build Coastguard Worker 86*4a64e381SAndroid Build Coastguard Worker /** 87*4a64e381SAndroid Build Coastguard Worker * This method stops browsing for TREL peers. 88*4a64e381SAndroid Build Coastguard Worker */ 89*4a64e381SAndroid Build Coastguard Worker void StopBrowse(void); 90*4a64e381SAndroid Build Coastguard Worker 91*4a64e381SAndroid Build Coastguard Worker /** 92*4a64e381SAndroid Build Coastguard Worker * This method registers the TREL service to DNS-SD. 93*4a64e381SAndroid Build Coastguard Worker * 94*4a64e381SAndroid Build Coastguard Worker * @param[in] aPort The UDP port of TREL service. 95*4a64e381SAndroid Build Coastguard Worker * @param[in] aTxtData The TXT data of TREL service. 96*4a64e381SAndroid Build Coastguard Worker * @param[in] aTxtLength The TXT length of TREL service. 97*4a64e381SAndroid Build Coastguard Worker */ 98*4a64e381SAndroid Build Coastguard Worker void RegisterService(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 99*4a64e381SAndroid Build Coastguard Worker 100*4a64e381SAndroid Build Coastguard Worker /** 101*4a64e381SAndroid Build Coastguard Worker * This method removes the TREL service from DNS-SD. 102*4a64e381SAndroid Build Coastguard Worker */ 103*4a64e381SAndroid Build Coastguard Worker void UnregisterService(void); 104*4a64e381SAndroid Build Coastguard Worker 105*4a64e381SAndroid Build Coastguard Worker /** 106*4a64e381SAndroid Build Coastguard Worker * This method handles mDNS publisher's state changes. 107*4a64e381SAndroid Build Coastguard Worker * 108*4a64e381SAndroid Build Coastguard Worker * @param[in] aState The state of mDNS publisher. 109*4a64e381SAndroid Build Coastguard Worker */ 110*4a64e381SAndroid Build Coastguard Worker void HandleMdnsState(Mdns::Publisher::State aState); 111*4a64e381SAndroid Build Coastguard Worker 112*4a64e381SAndroid Build Coastguard Worker private: 113*4a64e381SAndroid Build Coastguard Worker static constexpr size_t kPeerCacheSize = 256; 114*4a64e381SAndroid Build Coastguard Worker static constexpr uint16_t kCheckNetifReadyIntervalMs = 5000; 115*4a64e381SAndroid Build Coastguard Worker 116*4a64e381SAndroid Build Coastguard Worker struct RegisterInfo 117*4a64e381SAndroid Build Coastguard Worker { 118*4a64e381SAndroid Build Coastguard Worker uint16_t mPort = 0; 119*4a64e381SAndroid Build Coastguard Worker Mdns::Publisher::TxtData mTxtData; 120*4a64e381SAndroid Build Coastguard Worker std::string mInstanceName; 121*4a64e381SAndroid Build Coastguard Worker IsValidotbr::TrelDnssd::TrelDnssd::RegisterInfo122*4a64e381SAndroid Build Coastguard Worker bool IsValid(void) const { return mPort > 0; } IsPublishedotbr::TrelDnssd::TrelDnssd::RegisterInfo123*4a64e381SAndroid Build Coastguard Worker bool IsPublished(void) const { return !mInstanceName.empty(); } 124*4a64e381SAndroid Build Coastguard Worker void Assign(uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 125*4a64e381SAndroid Build Coastguard Worker void Clear(void); 126*4a64e381SAndroid Build Coastguard Worker }; 127*4a64e381SAndroid Build Coastguard Worker 128*4a64e381SAndroid Build Coastguard Worker using Clock = std::chrono::system_clock; 129*4a64e381SAndroid Build Coastguard Worker 130*4a64e381SAndroid Build Coastguard Worker struct Peer 131*4a64e381SAndroid Build Coastguard Worker { 132*4a64e381SAndroid Build Coastguard Worker static const char kTxtRecordExtAddressKey[]; 133*4a64e381SAndroid Build Coastguard Worker Peerotbr::TrelDnssd::TrelDnssd::Peer134*4a64e381SAndroid Build Coastguard Worker explicit Peer(std::vector<uint8_t> aTxtData, const otSockAddr &aSockAddr) 135*4a64e381SAndroid Build Coastguard Worker : mDiscoverTime(Clock::now()) 136*4a64e381SAndroid Build Coastguard Worker , mTxtData(std::move(aTxtData)) 137*4a64e381SAndroid Build Coastguard Worker , mSockAddr(aSockAddr) 138*4a64e381SAndroid Build Coastguard Worker { 139*4a64e381SAndroid Build Coastguard Worker ReadExtAddrFromTxtData(); 140*4a64e381SAndroid Build Coastguard Worker } 141*4a64e381SAndroid Build Coastguard Worker 142*4a64e381SAndroid Build Coastguard Worker void ReadExtAddrFromTxtData(void); 143*4a64e381SAndroid Build Coastguard Worker 144*4a64e381SAndroid Build Coastguard Worker Clock::time_point mDiscoverTime; 145*4a64e381SAndroid Build Coastguard Worker std::vector<uint8_t> mTxtData; 146*4a64e381SAndroid Build Coastguard Worker otSockAddr mSockAddr; 147*4a64e381SAndroid Build Coastguard Worker otExtAddress mExtAddr; 148*4a64e381SAndroid Build Coastguard Worker bool mValid = false; 149*4a64e381SAndroid Build Coastguard Worker }; 150*4a64e381SAndroid Build Coastguard Worker 151*4a64e381SAndroid Build Coastguard Worker using PeerMap = std::map<std::string, Peer>; 152*4a64e381SAndroid Build Coastguard Worker IsInitialized(void) const153*4a64e381SAndroid Build Coastguard Worker bool IsInitialized(void) const { return !mTrelNetif.empty(); } 154*4a64e381SAndroid Build Coastguard Worker bool IsReady(void) const; 155*4a64e381SAndroid Build Coastguard Worker void OnBecomeReady(void); 156*4a64e381SAndroid Build Coastguard Worker void CheckTrelNetifReady(void); 157*4a64e381SAndroid Build Coastguard Worker std::string GetTrelInstanceName(void); 158*4a64e381SAndroid Build Coastguard Worker void PublishTrelService(void); 159*4a64e381SAndroid Build Coastguard Worker void UnpublishTrelService(void); 160*4a64e381SAndroid Build Coastguard Worker static void HandlePublishTrelServiceError(otbrError aError); 161*4a64e381SAndroid Build Coastguard Worker static void HandleUnpublishTrelServiceError(otbrError aError); 162*4a64e381SAndroid Build Coastguard Worker void OnTrelServiceInstanceResolved(const std::string &aType, 163*4a64e381SAndroid Build Coastguard Worker const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo); 164*4a64e381SAndroid Build Coastguard Worker void OnTrelServiceInstanceAdded(const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo); 165*4a64e381SAndroid Build Coastguard Worker void OnTrelServiceInstanceRemoved(const std::string &aInstanceName); 166*4a64e381SAndroid Build Coastguard Worker 167*4a64e381SAndroid Build Coastguard Worker void NotifyRemovePeer(const Peer &aPeer); 168*4a64e381SAndroid Build Coastguard Worker void CheckPeersNumLimit(void); 169*4a64e381SAndroid Build Coastguard Worker void RemoveAllPeers(void); 170*4a64e381SAndroid Build Coastguard Worker uint16_t CountDuplicatePeers(const Peer &aPeer); 171*4a64e381SAndroid Build Coastguard Worker 172*4a64e381SAndroid Build Coastguard Worker Mdns::Publisher &mPublisher; 173*4a64e381SAndroid Build Coastguard Worker Ncp::RcpHost &mHost; 174*4a64e381SAndroid Build Coastguard Worker TaskRunner mTaskRunner; 175*4a64e381SAndroid Build Coastguard Worker std::string mTrelNetif; 176*4a64e381SAndroid Build Coastguard Worker uint32_t mTrelNetifIndex = 0; 177*4a64e381SAndroid Build Coastguard Worker uint64_t mSubscriberId = 0; 178*4a64e381SAndroid Build Coastguard Worker RegisterInfo mRegisterInfo; 179*4a64e381SAndroid Build Coastguard Worker PeerMap mPeers; 180*4a64e381SAndroid Build Coastguard Worker bool mMdnsPublisherReady = false; 181*4a64e381SAndroid Build Coastguard Worker }; 182*4a64e381SAndroid Build Coastguard Worker 183*4a64e381SAndroid Build Coastguard Worker /** 184*4a64e381SAndroid Build Coastguard Worker * @} 185*4a64e381SAndroid Build Coastguard Worker */ 186*4a64e381SAndroid Build Coastguard Worker 187*4a64e381SAndroid Build Coastguard Worker } // namespace TrelDnssd 188*4a64e381SAndroid Build Coastguard Worker 189*4a64e381SAndroid Build Coastguard Worker } // namespace otbr 190*4a64e381SAndroid Build Coastguard Worker 191*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_ENABLE_TREL 192*4a64e381SAndroid Build Coastguard Worker 193*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_AGENT_TREL_DNSSD_HPP_ 194