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-2024 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.4.4
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 //Additional record generation
125 #ifndef DNS_SD_ADDITIONAL_RECORDS_SUPPORT
126  #define DNS_SD_ADDITIONAL_RECORDS_SUPPORT ENABLED
127 #elif (DNS_SD_ADDITIONAL_RECORDS_SUPPORT != ENABLED && DNS_SD_ADDITIONAL_RECORDS_SUPPORT != DISABLED)
128  #error DNS_SD_ADDITIONAL_RECORDS_SUPPORT parameter is not valid
129 #endif
130 
131 //Forward declaration of DnsSdContext structure
132 struct _MdnsResponderContext;
133 #define MdnsResponderContext struct _MdnsResponderContext
134 
135 //C++ guard
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139 
140 
141 /**
142  * @brief mDNS responder states
143  **/
144 
145 typedef enum
146 {
151  MDNS_STATE_IDLE = 4
153 
154 
155 /**
156  * @brief FSM state change callback
157  **/
158 
160  NetInterface *interface, MdnsState state);
161 
162 
163 /**
164  * @brief IPv4 address entry
165  **/
166 
167 typedef struct
168 {
169  bool_t valid; ///<Valid entry
170  DnsIpv4AddrResourceRecord record; ///<A resource record
171  char_t reverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv4
173 
174 
175 /**
176  * @brief IPv6 address entry
177  **/
178 
179 typedef struct
180 {
181  bool_t valid; ///<Valid entry
182  DnsIpv6AddrResourceRecord record; ///<AAAA resource record
183  char_t reverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv6
185 
186 
187 /**
188  * @brief mDNS responder settings
189  **/
190 
191 typedef struct
192 {
193  NetInterface *interface; ///<Underlying network interface
194  uint_t numAnnouncements; ///<Number of announcement packets
195  uint32_t ttl; ///<TTL resource record
198 
199 
200 /**
201  * @brief mDNS responder context
202  **/
203 
205 {
206  MdnsResponderSettings settings; ///<DNS-SD settings
207  bool_t running; ///<mDNS responder is currently running
208  MdnsState state; ///<FSM state
209  bool_t conflict; ///<Conflict detected
210  bool_t tieBreakLost; ///<Tie-break lost
211  systime_t timestamp; ///<Timestamp to manage retransmissions
212  systime_t timeout; ///<Timeout value
213  uint_t retransmitCount; ///<Retransmission counter
215  bool_t ipv4AddrCount; ///<Number of valid IPv4 addresses
216  bool_t ipv6AddrCount; ///<Number of valid IPv6 addresses
217 #if (IPV4_SUPPORT == ENABLED)
219  MdnsMessage ipv4Response; ///<IPv4 response message
220 #endif
221 #if (IPV6_SUPPORT == ENABLED)
223  MdnsMessage ipv6Response; ///<IPv6 response message
224 #endif
225 };
226 
227 
228 //Tick counter to handle periodic operations
230 
231 //mDNS related functions
233 
235  const MdnsResponderSettings *settings);
236 
240 
242  const char_t *hostname);
243 
245 
248 
249 //C++ guard
250 #ifdef __cplusplus
251 }
252 #endif
253 
254 #endif
#define MdnsResponderContext
MdnsIpv6AddrEntry ipv6AddrList[IPV6_ADDR_LIST_SIZE]
IPv6 address list.
int bool_t
Definition: compiler_port.h:53
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
DnsIpv4AddrResourceRecord
Definition: dns_common.h:238
MdnsMessage ipv6Response
IPv6 response message.
MdnsState state
FSM state.
MdnsState
mDNS responder states
MdnsState mdnsResponderGetState(MdnsResponderContext *context)
Retrieve current state.
DnsIpv6AddrResourceRecord
Definition: dns_common.h:252
Definitions common to mDNS client and mDNS responder.
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:69
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:48
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:65
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:1606
#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:50
TCP/IP stack core.
error_t mdnsResponderStartProbing(MdnsResponderContext *context)
Restart probing process.
Common DNS routines.
DnsIpv6AddrResourceRecord record
AAAA resource record.
char_t hostname[MDNS_RESPONDER_MAX_HOSTNAME_LEN+1]
Host name.