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-2019 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 1.9.6
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 //Maximum waiting delay
62 #ifndef MDNS_MAX_WAITING_DELAY
63  #define MDNS_MAX_WAITING_DELAY 10000
64 #elif (MDNS_MAX_WAITING_DELAY < 0)
65  #error MDNS_MAX_WAITING_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 when deferring to the winning host
97 #ifndef MDNS_PROBE_DEFER
98  #define MDNS_PROBE_DEFER 1000
99 #elif (MDNS_PROBE_DEFER < 100)
100  #error MDNS_PROBE_DEFER parameter is not valid
101 #endif
102 
103 //Number of announcement packets
104 #ifndef MDNS_ANNOUNCE_NUM
105  #define MDNS_ANNOUNCE_NUM 2
106 #elif (MDNS_ANNOUNCE_NUM < 1)
107  #error MDNS_ANNOUNCE_NUM parameter is not valid
108 #endif
109 
110 //Time interval between subsequent announcement packets
111 #ifndef MDNS_ANNOUNCE_DELAY
112  #define MDNS_ANNOUNCE_DELAY 1000
113 #elif (MDNS_ANNOUNCE_DELAY < 100)
114  #error MDNS_ANNOUNCE_DELAY parameter is not valid
115 #endif
116 
117 //Forward declaration of DnsSdContext structure
118 struct _MdnsResponderContext;
119 #define MdnsResponderContext struct _MdnsResponderContext
120 
121 //C++ guard
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125 
126 
127 /**
128  * @brief mDNS responder states
129  **/
130 
131 typedef enum
132 {
138 } MdnsState;
139 
140 
141 /**
142  * @brief FSM state change callback
143  **/
144 
146  NetInterface *interface, MdnsState state);
147 
148 
149 /**
150  * @brief mDNS responder settings
151  **/
152 
153 typedef struct
154 {
155  NetInterface *interface; ///<Underlying network interface
156  uint_t numAnnouncements; ///<Number of announcement packets
157  uint32_t ttl; ///<TTL resource record
160 
161 
162 /**
163  * @brief mDNS responder context
164  **/
165 
167 {
168  MdnsResponderSettings settings; ///<DNS-SD settings
169  bool_t running; ///<mDNS responder is currently running
170  MdnsState state; ///<FSM state
171  bool_t conflict; ///<Conflict detected
172  bool_t tieBreakLost; ///<Tie-break lost
173  systime_t timestamp; ///<Timestamp to manage retransmissions
174  systime_t timeout; ///<Timeout value
175  uint_t retransmitCount; ///<Retransmission counter
177 #if (IPV4_SUPPORT == ENABLED)
178  char_t ipv4ReverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv4
179  MdnsMessage ipv4Response; ///<IPv4 response message
180 #endif
181 #if (IPV6_SUPPORT == ENABLED)
182  char_t ipv6ReverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv6
183  MdnsMessage ipv6Response; ///<IPv6 response message
184 #endif
185 };
186 
187 
188 //Tick counter to handle periodic operations
190 
191 //mDNS related functions
193 
195  const MdnsResponderSettings *settings);
196 
200 
202  const char_t *hostname);
203 
206 
208 
211 
213  MdnsState newState, systime_t delay);
214 
216 
220 
221 void mdnsResponderProcessQuery(NetInterface *interface, MdnsMessage *query);
222 
224  size_t offset, const DnsQuestion *question, MdnsMessage *response);
225 
226 void mdnsResponderParseKnownAnRecord(NetInterface *interface, const MdnsMessage *query,
227  size_t queryOffset, const DnsResourceRecord *queryRecord, MdnsMessage *response);
228 
230  const MdnsMessage *query, size_t offset, const DnsResourceRecord *record);
231 
233  const MdnsMessage *response, size_t offset, const DnsResourceRecord *record);
234 
236  MdnsMessage *response, bool_t legacyUnicast);
237 
239  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
240 
242  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
243 
245  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
246 
248  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
249 
251  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
252 
253 //C++ guard
254 #ifdef __cplusplus
255 }
256 #endif
257 
258 #endif
#define MdnsResponderContext
error_t mdnsResponderAddIpv6AddrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add AAAA record to a mDNS message.
int bool_t
Definition: compiler_port.h:49
error_t mdnsResponderAddIpv6ReversePtrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add reverse address mapping PTR record (IPv6)
systime_t timeout
Timeout value.
error_t mdnsResponderInit(MdnsResponderContext *context, const MdnsResponderSettings *settings)
mDNS responder initialization
void mdnsResponderChangeState(MdnsResponderContext *context, MdnsState newState, systime_t delay)
Update FSM state.
void mdnsResponderParseAnRecord(NetInterface *interface, const MdnsMessage *response, size_t offset, const DnsResourceRecord *record)
Parse a resource record from the Answer Section.
void mdnsResponderChangeHostname(MdnsResponderContext *context)
Programmatically change the host name.
char_t ipv4ReverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN+1]
Reverse DNS lookup for IPv4.
void mdnsResponderGetDefaultSettings(MdnsResponderSettings *settings)
Initialize settings with default values.
error_t mdnsResponderParseQuestion(NetInterface *interface, const MdnsMessage *query, size_t offset, const DnsQuestion *question, MdnsMessage *response)
Parse a question.
void mdnsResponderProcessQuery(NetInterface *interface, MdnsMessage *query)
Process mDNS query message.
@ MDNS_STATE_IDLE
systime_t timestamp
Timestamp to manage retransmissions.
error_t mdnsResponderSendGoodbye(MdnsResponderContext *context)
Send goodbye packet.
mDNS responder settings
MdnsMessage ipv6Response
IPv6 response message.
MdnsState state
FSM state.
MdnsState
mDNS responder states
MdnsState mdnsResponderGetState(MdnsResponderContext *context)
Retrieve current state.
error_t mdnsResponderAddIpv4ReversePtrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add reverse address mapping PTR record (IPv4)
__start_packed struct @148 DnsResourceRecord
Resource record format.
Definitions common to mDNS client and mDNS responder.
void mdnsResponderParseKnownAnRecord(NetInterface *interface, const MdnsMessage *query, size_t queryOffset, const DnsResourceRecord *queryRecord, MdnsMessage *response)
Parse a resource record from the Known-Answer Section.
#define DNS_MAX_IPV4_REVERSE_NAME_LEN
Definition: dns_common.h:52
error_t
Error codes.
Definition: error.h:42
error_t mdnsResponderSendAnnouncement(MdnsResponderContext *context)
Send announcement packet.
uint32_t ttl
TTL resource record.
error_t mdnsResponderSetHostname(MdnsResponderContext *context, const char_t *hostname)
Set hostname.
@ MDNS_STATE_PROBING
char_t ipv6ReverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN+1]
Reverse DNS lookup for IPv6.
#define NetInterface
Definition: net.h:36
MdnsMessage ipv4Response
IPv4 response message.
error_t mdnsResponderSetIpv6ReverseName(MdnsResponderContext *context)
Generate domain name for reverse DNS lookup (IPv6)
MdnsResponderSettings settings
DNS-SD settings.
error_t mdnsResponderSendProbe(MdnsResponderContext *context)
Send probe packet.
void mdnsResponderLinkChangeEvent(MdnsResponderContext *context)
Callback function for link change event.
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
NetInterface * interface
Underlying network interface.
systime_t mdnsResponderTickCounter
error_t mdnsResponderAddNsecRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add NSEC record to a mDNS message.
void mdnsResponderParseNsRecord(NetInterface *interface, const MdnsMessage *query, size_t offset, const DnsResourceRecord *record)
Parse a resource record from the Authority Section.
mDNS message
Definition: mdns_common.h:77
#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
MdnsResponderStateChangeCallback stateChangeEvent
FSM state change event.
error_t mdnsResponderStop(MdnsResponderContext *context)
Stop mDNS responder.
UDP (User Datagram Protocol)
bool_t conflict
Conflict detected.
void(* MdnsResponderStateChangeCallback)(MdnsResponderContext *context, NetInterface *interface, MdnsState state)
FSM state change callback.
uint8_t message[]
Definition: chap.h:152
char_t hostname[]
Definition: tls.h:1449
#define DNS_MAX_IPV6_REVERSE_NAME_LEN
Definition: dns_common.h:54
__start_packed struct @147 DnsQuestion
Question format.
void mdnsResponderGenerateAdditionalRecords(NetInterface *interface, MdnsMessage *response, bool_t legacyUnicast)
Additional record generation.
error_t mdnsResponderSetIpv4ReverseName(MdnsResponderContext *context)
Generate domain name for reverse DNS lookup (IPv4)
uint_t retransmitCount
Retransmission counter.
uint32_t ttl
Definition: dns_common.h:205
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
error_t mdnsResponderStartProbing(MdnsResponderContext *context)
Restart probing process.
Common DNS routines.
uint32_t systime_t
Definition: compiler_port.h:46
error_t mdnsResponderAddIpv4AddrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add A record to a mDNS message.
char_t hostname[MDNS_RESPONDER_MAX_HOSTNAME_LEN+1]
Hostname.