1 /* 2 * Copyright (c) 2020, 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 Advertising Proxy. 32 */ 33 34 #ifndef OTBR_SRP_ADVERTISING_PROXY_HPP_ 35 #define OTBR_SRP_ADVERTISING_PROXY_HPP_ 36 37 #include "openthread-br/config.h" 38 39 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY 40 41 #include <stdint.h> 42 43 #include <openthread/instance.h> 44 #include <openthread/srp_server.h> 45 46 #include "common/code_utils.hpp" 47 #include "host/rcp_host.hpp" 48 #include "mdns/mdns.hpp" 49 50 namespace otbr { 51 52 /** 53 * This class implements the Advertising Proxy. 54 */ 55 class AdvertisingProxy : public Mdns::StateObserver, private NonCopyable 56 { 57 public: 58 /** 59 * This constructor initializes the Advertising Proxy object. 60 * 61 * @param[in] aHost A reference to the NCP controller. 62 * @param[in] aPublisher A reference to the mDNS publisher. 63 */ 64 explicit AdvertisingProxy(Host::RcpHost &aHost, Mdns::Publisher &aPublisher); 65 66 /** 67 * This method enables/disables the Advertising Proxy. 68 * 69 * @param[in] aIsEnabled Whether to enable the Advertising Proxy. 70 */ 71 void SetEnabled(bool aIsEnabled); 72 73 /** Sets `true` to allow advertising ML-EID. */ SetAllowMlEid(bool aAllowMlEid)74 void SetAllowMlEid(bool aAllowMlEid) { mAllowMlEid = aAllowMlEid; } 75 76 /** 77 * This method publishes all registered hosts and services. 78 */ 79 void PublishAllHostsAndServices(void); 80 81 /** 82 * This method handles mDNS publisher's state changes. 83 * 84 * @param[in] aState The state of mDNS publisher. 85 */ 86 void HandleMdnsState(Mdns::Publisher::State aState) override; 87 88 private: 89 struct OutstandingUpdate 90 { 91 otSrpServerServiceUpdateId mId; // The ID of the SRP service update transaction. 92 std::string mHostName; // The host name. 93 uint32_t mCallbackCount = 0; // The number of callbacks which we are waiting for. 94 }; 95 96 static void AdvertisingHandler(otSrpServerServiceUpdateId aId, 97 const otSrpServerHost *aHost, 98 uint32_t aTimeout, 99 void *aContext); 100 void AdvertisingHandler(otSrpServerServiceUpdateId aId, const otSrpServerHost *aHost, uint32_t aTimeout); 101 102 static Mdns::Publisher::TxtData MakeTxtData(const otSrpServerService *aSrpService); 103 static Mdns::Publisher::SubTypeList MakeSubTypeList(const otSrpServerService *aSrpService); 104 void OnMdnsPublishResult(otSrpServerServiceUpdateId aUpdateId, otbrError aError); 105 106 std::vector<Ip6Address> GetEligibleAddresses(const otIp6Address *aHostAddresses, uint8_t aHostAddressNum); 107 108 void Start(void); 109 void Stop(void); IsEnabled(void) const110 bool IsEnabled(void) const { return mIsEnabled; } 111 112 /** 113 * This method publishes a specified host and its services. 114 * 115 * It also makes a OutstandingUpdate object when needed. 116 * 117 * @param[in] aHost A pointer to the host. 118 * @param[in] aUpdate A pointer to the output OutstandingUpdate object. When it's not null, the method will 119 * fill its fields, otherwise it's ignored. 120 * 121 * @retval OTBR_ERROR_NONE Successfully published the host and its services. 122 * @retval ... Failed to publish the host and/or its services. 123 */ 124 otbrError PublishHostAndItsServices(const otSrpServerHost *aHost, OutstandingUpdate *aUpdate); 125 GetInstance(void)126 otInstance *GetInstance(void) { return mHost.GetInstance(); } 127 128 // A reference to the NCP controller, has no ownership. 129 Host::RcpHost &mHost; 130 131 // A reference to the mDNS publisher, has no ownership. 132 Mdns::Publisher &mPublisher; 133 134 bool mIsEnabled; 135 bool mAllowMlEid; 136 137 // A vector that tracks outstanding updates. 138 std::vector<OutstandingUpdate> mOutstandingUpdates; 139 }; 140 141 } // namespace otbr 142 143 #endif // OTBR_ENABLE_SRP_ADVERTISING_PROXY 144 145 #endif // OTBR_SRP_ADVERTISING_PROXY_HPP_ 146