2 * Copyright (c) 2017, The OpenThread Authors.
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.
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.
31 * This file includes definitions for mDNS publisher.
34 #ifndef OTBR_AGENT_MDNS_HPP_
35 #define OTBR_AGENT_MDNS_HPP_
39 #include <sys/select.h>
41 #include "common/types.hpp"
48 * @addtogroup border-router-mdns
51 * This module includes definition for MDNS service.
57 * This interface defines the functionality of MDNS service.
64 * This structure represents a name/value pair of the TXT record.
69 const char * mName; ///< The name of the TXT entry.
70 size_t mNameLength; ///< The length of the name of the TXT entry.
71 const uint8_t *mValue; ///< The value of the TXT entry.
72 size_t mValueLength; ///< The length of the value of the TXT entry.
74 TxtEntry(const char *aName, const char *aValue)
75 : TxtEntry(aName, reinterpret_cast<const uint8_t *>(aValue), strlen(aValue))
79 TxtEntry(const char *aName, const uint8_t *aValue, size_t aValueLength)
80 : TxtEntry(aName, strlen(aName), aValue, aValueLength)
84 TxtEntry(const char *aName, size_t aNameLength, const uint8_t *aValue, size_t aValueLength)
86 , mNameLength(aNameLength)
88 , mValueLength(aValueLength)
93 typedef std::vector<TxtEntry> TxtList;
101 kIdle, ///< Unable to publishing service.
102 kReady, ///< Ready for publishing service.
106 * This function pointer is called when MDNS service state changed.
108 * @param[in] aContext A pointer to application-specific context.
109 * @param[in] aState The new state.
112 typedef void (*StateHandler)(void *aContext, State aState);
115 * This method reports the result of a service publication.
117 * @param[in] aName The service instance name.
118 * @param[in] aType The service type.
119 * @param[in] aError An error that indicates whether the service publication succeeds.
120 * @param[in] aContext A user context.
123 typedef void (*PublishServiceHandler)(const char *aName, const char *aType, otbrError aError, void *aContext);
126 * This method reports the result of a host publication.
128 * @param[in] aName The host name.
129 * @param[in] aError An OTBR error that indicates whether the host publication succeeds.
130 * @param[in] aContext A user context.
133 typedef void (*PublishHostHandler)(const char *aName, otbrError aError, void *aContext);
136 * This method sets the handler for service publication.
138 * @param[in] aHandler A handler which will be called when a service publication is finished.
139 * @param[in] aContext A user context which is associated to @p aHandler.
142 void SetPublishServiceHandler(PublishServiceHandler aHandler, void *aContext)
144 mServiceHandler = aHandler;
145 mServiceHandlerContext = aContext;
149 * This method sets the handler for host publication.
151 * @param[in] aHandler A handler which will be called when a host publication is finished.
152 * @param[in] aContext A user context which is associated to @p aHandler.
155 void SetPublishHostHandler(PublishHostHandler aHandler, void *aContext)
157 mHostHandler = aHandler;
158 mHostHandlerContext = aContext;
162 * This method starts the MDNS service.
164 * @retval OTBR_ERROR_NONE Successfully started MDNS service;
165 * @retval OTBR_ERROR_MDNS Failed to start MDNS service.
168 virtual otbrError Start(void) = 0;
171 * This method stops the MDNS service.
174 virtual void Stop(void) = 0;
177 * This method checks if publisher has been started.
179 * @retval true Already started.
180 * @retval false Not started.
183 virtual bool IsStarted(void) const = 0;
186 * This method publishes or updates a service.
188 * @param[in] aHostName The name of the host which this service resides on. If NULL is provided,
189 * this service resides on local host and it is the implementation to provide
190 * specific host name. Otherwise, the caller MUST publish the host with method
192 * @param[in] aName The name of this service.
193 * @param[in] aType The type of this service.
194 * @param[in] aPort The port number of this service.
195 * @param[in] aTxtList A list of TXT name/value pairs.
197 * @retval OTBR_ERROR_NONE Successfully published or updated the service.
198 * @retval OTBR_ERROR_ERRNO Failed to publish or update the service.
201 virtual otbrError PublishService(const char * aHostName,
205 const TxtList &aTxtList) = 0;
208 * This method un-publishes a service.
210 * @param[in] aName The name of this service.
211 * @param[in] aType The type of this service.
213 * @retval OTBR_ERROR_NONE Successfully un-published the service.
214 * @retval OTBR_ERROR_ERRNO Failed to un-publish the service.
217 virtual otbrError UnpublishService(const char *aName, const char *aType) = 0;
220 * This method publishes or updates a host.
222 * Publishing a host is advertising an A/AAAA RR for the host name. This method should be called
223 * before a service with non-null host name is published.
225 * @param[in] aName The name of the host.
226 * @param[in] aAddress The address of the host.
227 * @param[in] aAddressLength The length of @p aAddress.
229 * @retval OTBR_ERROR_NONE Successfully published or updated the host.
230 * @retval OTBR_ERROR_INVALID_ARGS The arguments are not valid.
231 * @retval OTBR_ERROR_ERRNO Failed to publish or update the host.
234 virtual otbrError PublishHost(const char *aName, const uint8_t *aAddress, uint8_t aAddressLength) = 0;
237 * This method un-publishes a host.
239 * @param[in] aName A host name.
241 * @retval OTBR_ERROR_NONE Successfully un-published the host.
242 * @retval OTBR_ERROR_ERRNO Failed to un-publish the host.
245 virtual otbrError UnpublishHost(const char *aName) = 0;
248 * This method performs the MDNS processing.
250 * @param[in] aReadFdSet A reference to fd_set ready for reading.
251 * @param[in] aWriteFdSet A reference to fd_set ready for writing.
252 * @param[in] aErrorFdSet A reference to fd_set with error occurred.
255 virtual void Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet, const fd_set &aErrorFdSet) = 0;
258 * This method updates the fd_set and timeout for mainloop.
260 * @param[inout] aReadFdSet A reference to fd_set for polling read.
261 * @param[inout] aWriteFdSet A reference to fd_set for polling read.
262 * @param[inout] aErrorFdSet A reference to fd_set for polling error.
263 * @param[inout] aMaxFd A reference to the current max fd in @p aReadFdSet and @p aWriteFdSet.
264 * @param[inout] aTimeout A reference to the timeout. Update this value if the MDNS service has
265 * pending process in less than its current value.
268 virtual void UpdateFdSet(fd_set & aReadFdSet,
269 fd_set & aWriteFdSet,
270 fd_set & aErrorFdSet,
272 timeval &aTimeout) = 0;
274 virtual ~Publisher(void) {}
277 * This function creates a MDNS publisher.
279 * @param[in] aProtocol Protocol to use for publishing. AF_INET6, AF_INET or AF_UNSPEC.
280 * @param[in] aDomain The domain to register in. Set nullptr to use default mDNS domain ("local.").
281 * @param[in] aHandler The function to be called when this service state changed.
282 * @param[in] aContext A pointer to application-specific context.
284 * @returns A pointer to the newly created MDNS publisher.
287 static Publisher *Create(int aProtocol, const char *aDomain, StateHandler aHandler, void *aContext);
290 * This function destroys the MDNS publisher.
292 * @param[in] aPublisher A pointer to the publisher.
295 static void Destroy(Publisher *aPublisher);
298 * This function decides if two service types (names) are equal.
300 * Different implementations may or not append a dot ('.') to the service type (name)
301 * and we can not compare if two service type are equal with `strcmp`. This function
302 * ignores the trailing dot when comparing two service types.
304 * @param[in] aFirstType The first service type.
305 * @param[in] aSecondType The second service type.
307 * returns A boolean that indicates whether the two service types are equal.
310 static bool IsServiceTypeEqual(const char *aFirstType, const char *aSecondType);
313 * This function writes the TXT entry list to a TXT data buffer.
315 * The output data is in standard DNS-SD TXT data format.
316 * See RFC 6763 for details: https://tools.ietf.org/html/rfc6763#section-6.
318 * @param[in] aTxtList A TXT entry list.
319 * @param[out] aTxtData A TXT data buffer.
320 * @param[inout] aTxtLength As input, it is the length of the TXT data buffer;
321 * As output, it is the length of the TXT data written.
323 * @retval OTBR_ERROR_NONE Successfully write the TXT entry list.
324 * @retval OTBR_ERROR_INVALID_ARGS The input TXT data buffer cannot hold the TXT data.
327 static otbrError EncodeTxtData(const TxtList &aTxtList, uint8_t *aTxtData, uint16_t &aTxtLength);
332 kMaxTextEntrySize = 255,
335 PublishServiceHandler mServiceHandler = nullptr;
336 void * mServiceHandlerContext = nullptr;
338 PublishHostHandler mHostHandler = nullptr;
339 void * mHostHandlerContext = nullptr;
350 #endif // OTBR_AGENT_MDNS_HPP_