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 * @brief 32 * This file includes the platform abstraction for Thread Radio Encapsulation Link (TREL) using DNS-SD and UDP/IPv6. 33 * 34 */ 35 36 #ifndef OPENTHREAD_PLATFORM_TREL_H_ 37 #define OPENTHREAD_PLATFORM_TREL_H_ 38 39 #include <stdint.h> 40 41 #include <openthread/error.h> 42 #include <openthread/instance.h> 43 #include <openthread/ip6.h> 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 /** 50 * @addtogroup plat-trel 51 * 52 * @brief 53 * This module includes the platform abstraction for Thread Radio Encapsulation Link (TREL) using DNS-SD and 54 * UDP/IPv6. 55 * 56 * @{ 57 * 58 */ 59 60 /** 61 * This function initializes and enables TREL platform layer. 62 * 63 * Upon this call, the platform layer MUST perform the following: 64 * 65 * 1) TREL platform layer MUST open a UDP socket to listen for and receive TREL messages from peers. The socket is 66 * bound to an ephemeral port number chosen by the platform layer. The port number MUST be returned in @p aUdpPort. 67 * The socket is also bound to network interface(s) on which TREL is to be supported. The socket and the chosen port 68 * should stay valid while TREL is enabled. 69 * 70 * 2) Platform layer MUST initiate an ongoing DNS-SD browse on the service name "_trel._udp" within the local browsing 71 * domain to discover other devices supporting TREL. The ongoing browse will produce two different types of events: 72 * "add" events and "remove" events. When the browse is started, it should produce an "add" event for every TREL peer 73 * currently present on the network. Whenever a TREL peer goes offline, a "remove" event should be produced. "remove" 74 * events are not guaranteed, however. When a TREL service instance is discovered, a new ongoing DNS-SD query for an 75 * AAAA record should be started on the hostname indicated in the SRV record of the discovered instance. If multiple 76 * host IPv6 addressees are discovered for a peer, one with highest scope among all addresses MUST be reported (if 77 * there are multiple address at same scope, one must be selected randomly). 78 * 79 * TREL platform MUST signal back the discovered peer info using `otPlatTrelHandleDiscoveredPeerInfo()` callback. This 80 * callback MUST be invoked when a new peer is discovered, when there is a change in an existing entry (e.g., new 81 * TXT record or new port number or new IPv6 address), or when the peer is removed. 82 * 83 * @param[in] aInstance The OpenThread instance. 84 * @param[out] aUdpPort A pointer to return the selected port number by platform layer. 85 * 86 */ 87 void otPlatTrelEnable(otInstance *aInstance, uint16_t *aUdpPort); 88 89 /** 90 * This function disables TREL platform layer. 91 * 92 * After this call, the platform layer MUST stop DNS-SD browse on the service name "_trel._udp", stop advertising the 93 * TREL DNS-SD service (from `otPlatTrelRegisterService()`) and MUST close the UDP socket used to receive TREL messages. 94 * 95 * @pram[in] aInstance The OpenThread instance. 96 * 97 */ 98 void otPlatTrelDisable(otInstance *aInstance); 99 100 /** 101 * This structure represents a TREL peer info discovered using DNS-SD browse on the service name "_trel._udp". 102 * 103 */ 104 typedef struct otPlatTrelPeerInfo 105 { 106 /** 107 * This boolean flag indicates whether the entry is being removed or added. 108 * 109 * - TRUE indicates that peer is removed. 110 * - FALSE indicates that it is a new entry or an update to an existing entry. 111 * 112 */ 113 bool mRemoved; 114 115 /** 116 * The TXT record data (encoded as specified by DNS-SD) from the SRV record of the discovered TREL peer service 117 * instance. 118 * 119 */ 120 const uint8_t *mTxtData; 121 122 uint16_t mTxtLength; ///< Number of bytes in @p mTxtData buffer. 123 124 /** 125 * The TREL peer socket address (IPv6 address and port number). 126 * 127 * The port number is determined from the SRV record of the discovered TREL peer service instance. The IPv6 address 128 * is determined from the DNS-SD query for AAAA records on the hostname indicated in the SRV record of the 129 * discovered service instance. If multiple host IPv6 addressees are discovered, one with highest scope is used. 130 * 131 */ 132 otSockAddr mSockAddr; 133 } otPlatTrelPeerInfo; 134 135 /** 136 * This is a callback function from platform layer to report a discovered TREL peer info. 137 * 138 * @note The @p aInfo structure and its content (e.g., the `mTxtData` buffer) does not need to persist after returning 139 * from this call. OpenThread code will make a copy of all the info it needs. 140 * 141 * @param[in] aInstance The OpenThread instance. 142 * @param[in] aInfo A pointer to the TREL peer info. 143 * 144 */ 145 extern void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo); 146 147 /** 148 * This function registers a new service to be advertised using DNS-SD [RFC6763]. 149 * 150 * The service name is "_trel._udp". The platform should use its own hostname, which when combined with the service 151 * name and the local DNS-SD domain name will produce the full service instance name, for example 152 * "example-host._trel._udp.local.". 153 * 154 * The domain under which the service instance name appears will be 'local' for mDNS, and will be whatever domain is 155 * used for service registration in the case of a non-mDNS local DNS-SD service. 156 * 157 * A subsequent call to this function updates the previous service. It is used to update the TXT record data and/or the 158 * port number. 159 * 160 * The @p aTxtData buffer is not persisted after the return from this function. The platform layer MUST NOT keep the 161 * pointer and instead copy the content if needed. 162 * 163 * @param[in] aInstance The OpenThread instance. 164 * @param[in] aPort The port number to include in the SRV record of the advertised service. 165 * @param[in] aTxtData A pointer to the TXT record data (encoded) to be include in the advertised service. 166 * @param[in] aTxtLength The length of @p aTxtData (number of bytes). 167 * 168 * 169 */ 170 void otPlatTrelRegisterService(otInstance *aInstance, uint16_t aPort, const uint8_t *aTxtData, uint8_t aTxtLength); 171 172 /** 173 * This function requests a TREL UDP packet to be sent to a given destination. 174 * 175 * @param[in] aInstance The OpenThread instance structure. 176 * @param[in] aUdpPayload A pointer to UDP payload. 177 * @param[in] aUdpPayloadLen The payload length (number of bytes). 178 * @param[in] aDestSockAddr The destination socket address. 179 * 180 */ 181 void otPlatTrelSend(otInstance *aInstance, 182 const uint8_t *aUdpPayload, 183 uint16_t aUdpPayloadLen, 184 const otSockAddr *aDestSockAddr); 185 186 /** 187 * This function is a callback from platform to notify of a received TREL UDP packet. 188 * 189 * @note The buffer content (up to its specified length) may get changed during processing by OpenThread core (e.g., 190 * decrypted in place), so the platform implementation should expect that after returning from this function the 191 * @p aBuffer content may have been altered. 192 * 193 * @param[in] aInstance The OpenThread instance structure. 194 * @param[in] aBuffer A buffer containing the received UDP payload. 195 * @param[in] aLength UDP payload length (number of bytes). 196 * 197 */ 198 extern void otPlatTrelHandleReceived(otInstance *aInstance, uint8_t *aBuffer, uint16_t aLength); 199 200 /** 201 * @} 202 * 203 */ 204 205 #ifdef __cplusplus 206 } // end of extern "C" 207 #endif 208 209 #endif // OPENTHREAD_PLATFORM_TREL_H_ 210