mdns_responder.h
Go to the documentation of this file.
1 /**
2  * @file mdns_responder.h
3  * @brief mDNS responder (Multicast DNS)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2022 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.2.0
29  **/
30 
31 #ifndef _MDNS_RESPONDER_H
32 #define _MDNS_RESPONDER_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/udp.h"
37 #include "dns/dns_common.h"
38 #include "mdns/mdns_common.h"
39 
40 //mDNS responder support
41 #ifndef MDNS_RESPONDER_SUPPORT
42  #define MDNS_RESPONDER_SUPPORT DISABLED
43 #elif (MDNS_RESPONDER_SUPPORT != ENABLED && MDNS_RESPONDER_SUPPORT != DISABLED)
44  #error MDNS_RESPONDER_SUPPORT parameter is not valid
45 #endif
46 
47 //mDNS responder tick interval
48 #ifndef MDNS_RESPONDER_TICK_INTERVAL
49  #define MDNS_RESPONDER_TICK_INTERVAL 250
50 #elif (MDNS_RESPONDER_TICK_INTERVAL < 10)
51  #error MDNS_RESPONDER_TICK_INTERVAL parameter is not valid
52 #endif
53 
54 //Maximum length of host name
55 #ifndef MDNS_RESPONDER_MAX_HOSTNAME_LEN
56  #define MDNS_RESPONDER_MAX_HOSTNAME_LEN 32
57 #elif (MDNS_RESPONDER_MAX_HOSTNAME_LEN < 1)
58  #error MDNS_RESPONDER_MAX_HOSTNAME_LEN parameter is not valid
59 #endif
60 
61 //Initial delay
62 #ifndef MDNS_INIT_DELAY
63  #define MDNS_INIT_DELAY 1000
64 #elif (MDNS_INIT_DELAY < 0)
65  #error MDNS_INIT_DELAY parameter is not valid
66 #endif
67 
68 //Initial random delay (minimum value)
69 #ifndef MDNS_RAND_DELAY_MIN
70  #define MDNS_RAND_DELAY_MIN 0
71 #elif (MDNS_RAND_DELAY_MIN < 0)
72  #error MDNS_RAND_DELAY_MIN parameter is not valid
73 #endif
74 
75 //Initial random delay (maximum value)
76 #ifndef MDNS_RAND_DELAY_MAX
77  #define MDNS_RAND_DELAY_MAX 250
78 #elif (MDNS_RAND_DELAY_MAX < 0)
79  #error MDNS_RAND_DELAY_MAX parameter is not valid
80 #endif
81 
82 //Number of probe packets
83 #ifndef MDNS_PROBE_NUM
84  #define MDNS_PROBE_NUM 3
85 #elif (MDNS_PROBE_NUM < 1)
86  #error MDNS_PROBE_NUM parameter is not valid
87 #endif
88 
89 //Time interval between subsequent probe packets
90 #ifndef MDNS_PROBE_DELAY
91  #define MDNS_PROBE_DELAY 250
92 #elif (MDNS_PROBE_DELAY < 100)
93  #error MDNS_PROBE_DELAY parameter is not valid
94 #endif
95 
96 //Delay before probing again after any failed probe attempt
97 #ifndef MDNS_PROBE_CONFLICT_DELAY
98  #define MDNS_PROBE_CONFLICT_DELAY 1000
99 #elif (MDNS_PROBE_CONFLICT_DELAY < 100)
100  #error MDNS_PROBE_CONFLICT_DELAY parameter is not valid
101 #endif
102 
103 //Delay before probing again when deferring to the winning host
104 #ifndef MDNS_PROBE_DEFER_DELAY
105  #define MDNS_PROBE_DEFER_DELAY 1000
106 #elif (MDNS_PROBE_DEFER_DELAY < 100)
107  #error MDNS_PROBE_DEFER_DELAY parameter is not valid
108 #endif
109 
110 //Number of announcement packets
111 #ifndef MDNS_ANNOUNCE_NUM
112  #define MDNS_ANNOUNCE_NUM 2
113 #elif (MDNS_ANNOUNCE_NUM < 1)
114  #error MDNS_ANNOUNCE_NUM parameter is not valid
115 #endif
116 
117 //Time interval between subsequent announcement packets
118 #ifndef MDNS_ANNOUNCE_DELAY
119  #define MDNS_ANNOUNCE_DELAY 1000
120 #elif (MDNS_ANNOUNCE_DELAY < 100)
121  #error MDNS_ANNOUNCE_DELAY parameter is not valid
122 #endif
123 
124 //Forward declaration of DnsSdContext structure
125 struct _MdnsResponderContext;
126 #define MdnsResponderContext struct _MdnsResponderContext
127 
128 //C++ guard
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 
133 
134 /**
135  * @brief mDNS responder states
136  **/
137 
138 typedef enum
139 {
144  MDNS_STATE_IDLE = 4
146 
147 
148 /**
149  * @brief FSM state change callback
150  **/
151 
153  NetInterface *interface, MdnsState state);
154 
155 
156 /**
157  * @brief IPv4 address entry
158  **/
159 
160 typedef struct
161 {
162  bool_t valid; ///<Valid entry
163  DnsIpv4AddrResourceRecord record; ///<A resource record
164  char_t reverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv4
166 
167 
168 /**
169  * @brief IPv6 address entry
170  **/
171 
172 typedef struct
173 {
174  bool_t valid; ///<Valid entry
175  DnsIpv6AddrResourceRecord record; ///<AAAA resource record
176  char_t reverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv6
178 
179 
180 /**
181  * @brief mDNS responder settings
182  **/
183 
184 typedef struct
185 {
186  NetInterface *interface; ///<Underlying network interface
187  uint_t numAnnouncements; ///<Number of announcement packets
188  uint32_t ttl; ///<TTL resource record
191 
192 
193 /**
194  * @brief mDNS responder context
195  **/
196 
198 {
199  MdnsResponderSettings settings; ///<DNS-SD settings
200  bool_t running; ///<mDNS responder is currently running
201  MdnsState state; ///<FSM state
202  bool_t conflict; ///<Conflict detected
203  bool_t tieBreakLost; ///<Tie-break lost
204  systime_t timestamp; ///<Timestamp to manage retransmissions
205  systime_t timeout; ///<Timeout value
206  uint_t retransmitCount; ///<Retransmission counter
208  bool_t ipv4AddrCount; ///<Number of valid IPv4 addresses
209  bool_t ipv6AddrCount; ///<Number of valid IPv6 addresses
210 #if (IPV4_SUPPORT == ENABLED)
212  MdnsMessage ipv4Response; ///<IPv4 response message
213 #endif
214 #if (IPV6_SUPPORT == ENABLED)
216  MdnsMessage ipv6Response; ///<IPv6 response message
217 #endif
218 };
219 
220 
221 //Tick counter to handle periodic operations
223 
224 //mDNS related functions
226 
228  const MdnsResponderSettings *settings);
229 
233 
235  const char_t *hostname);
236 
238 
241 
242 //C++ guard
243 #ifdef __cplusplus
244 }
245 #endif
246 
247 #endif
#define MdnsResponderContext
MdnsIpv6AddrEntry ipv6AddrList[IPV6_ADDR_LIST_SIZE]
IPv6 address list.
int bool_t
Definition: compiler_port.h:48
DnsIpv4AddrResourceRecord record
A resource record.
systime_t timeout
Timeout value.
error_t mdnsResponderInit(MdnsResponderContext *context, const MdnsResponderSettings *settings)
mDNS responder initialization
void mdnsResponderGetDefaultSettings(MdnsResponderSettings *settings)
Initialize settings with default values.
MdnsIpv4AddrEntry ipv4AddrList[IPV4_ADDR_LIST_SIZE]
IPv4 address list.
@ MDNS_STATE_IDLE
systime_t timestamp
Timestamp to manage retransmissions.
mDNS responder settings
MdnsMessage ipv6Response
IPv6 response message.
MdnsState state
FSM state.
MdnsState
mDNS responder states
MdnsState mdnsResponderGetState(MdnsResponderContext *context)
Retrieve current state.
Definitions common to mDNS client and mDNS responder.
__start_packed struct @4 DnsIpv6AddrResourceRecord
AAAA resource record format.
bool_t valid
Valid entry.
#define DNS_MAX_IPV4_REVERSE_NAME_LEN
Definition: dns_common.h:52
error_t
Error codes.
Definition: error.h:43
bool_t valid
Valid entry.
uint32_t ttl
TTL resource record.
error_t mdnsResponderSetHostname(MdnsResponderContext *context, const char_t *hostname)
Set host name.
@ MDNS_STATE_PROBING
#define NetInterface
Definition: net.h:36
MdnsMessage ipv4Response
IPv4 response message.
bool_t ipv6AddrCount
Number of valid IPv6 addresses.
MdnsResponderSettings settings
DNS-SD settings.
void mdnsResponderLinkChangeEvent(MdnsResponderContext *context)
Callback function for link change event.
#define IPV4_ADDR_LIST_SIZE
Definition: ipv4.h:63
void mdnsResponderTick(MdnsResponderContext *context)
mDNS responder timer handler
bool_t tieBreakLost
Tie-break lost.
@ MDNS_STATE_WAITING
uint_t numAnnouncements
Number of announcement packets.
@ MDNS_STATE_INIT
bool_t ipv4AddrCount
Number of valid IPv4 addresses.
NetInterface * interface
Underlying network interface.
systime_t mdnsResponderTickCounter
uint32_t systime_t
System time.
mDNS message
Definition: mdns_common.h:78
#define MDNS_RESPONDER_MAX_HOSTNAME_LEN
mDNS responder context
char char_t
Definition: compiler_port.h:43
bool_t running
mDNS responder is currently running
error_t mdnsResponderStart(MdnsResponderContext *context)
Start mDNS responder.
@ MDNS_STATE_ANNOUNCING
#define IPV6_ADDR_LIST_SIZE
Definition: ipv6.h:66
MdnsResponderStateChangeCallback stateChangeEvent
FSM state change event.
error_t mdnsResponderStop(MdnsResponderContext *context)
Stop mDNS responder.
UDP (User Datagram Protocol)
bool_t conflict
Conflict detected.
IPv6 address entry.
void(* MdnsResponderStateChangeCallback)(MdnsResponderContext *context, NetInterface *interface, MdnsState state)
FSM state change callback.
char_t hostname[]
Definition: tls.h:1520
#define DNS_MAX_IPV6_REVERSE_NAME_LEN
Definition: dns_common.h:54
IPv4 address entry.
uint_t retransmitCount
Retransmission counter.
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
error_t mdnsResponderStartProbing(MdnsResponderContext *context)
Restart probing process.
Common DNS routines.
DnsIpv6AddrResourceRecord record
AAAA resource record.
__start_packed struct @3 DnsIpv4AddrResourceRecord
A resource record format.
char_t hostname[MDNS_RESPONDER_MAX_HOSTNAME_LEN+1]
Host name.