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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _MDNS_RESPONDER_H
30 #define _MDNS_RESPONDER_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "core/udp.h"
35 #include "dns/dns_common.h"
36 #include "mdns/mdns_common.h"
37 
38 //mDNS responder support
39 #ifndef MDNS_RESPONDER_SUPPORT
40  #define MDNS_RESPONDER_SUPPORT DISABLED
41 #elif (MDNS_RESPONDER_SUPPORT != ENABLED && MDNS_RESPONDER_SUPPORT != DISABLED)
42  #error MDNS_RESPONDER_SUPPORT parameter is not valid
43 #endif
44 
45 //mDNS responder tick interval
46 #ifndef MDNS_RESPONDER_TICK_INTERVAL
47  #define MDNS_RESPONDER_TICK_INTERVAL 250
48 #elif (MDNS_RESPONDER_TICK_INTERVAL < 10)
49  #error MDNS_RESPONDER_TICK_INTERVAL parameter is not valid
50 #endif
51 
52 //Maximum length of host name
53 #ifndef MDNS_RESPONDER_MAX_HOSTNAME_LEN
54  #define MDNS_RESPONDER_MAX_HOSTNAME_LEN 32
55 #elif (MDNS_RESPONDER_MAX_HOSTNAME_LEN < 1)
56  #error MDNS_RESPONDER_MAX_HOSTNAME_LEN parameter is not valid
57 #endif
58 
59 //Maximum waiting delay
60 #ifndef MDNS_MAX_WAITING_DELAY
61  #define MDNS_MAX_WAITING_DELAY 10000
62 #elif (MDNS_MAX_WAITING_DELAY < 0)
63  #error MDNS_MAX_WAITING_DELAY parameter is not valid
64 #endif
65 
66 //Initial random delay (minimum value)
67 #ifndef MDNS_RAND_DELAY_MIN
68  #define MDNS_RAND_DELAY_MIN 0
69 #elif (MDNS_RAND_DELAY_MIN < 0)
70  #error MDNS_RAND_DELAY_MIN parameter is not valid
71 #endif
72 
73 //Initial random delay (maximum value)
74 #ifndef MDNS_RAND_DELAY_MAX
75  #define MDNS_RAND_DELAY_MAX 250
76 #elif (MDNS_RAND_DELAY_MAX < 0)
77  #error MDNS_RAND_DELAY_MAX parameter is not valid
78 #endif
79 
80 //Number of probe packets
81 #ifndef MDNS_PROBE_NUM
82  #define MDNS_PROBE_NUM 3
83 #elif (MDNS_PROBE_NUM < 1)
84  #error MDNS_PROBE_NUM parameter is not valid
85 #endif
86 
87 //Time interval between subsequent probe packets
88 #ifndef MDNS_PROBE_DELAY
89  #define MDNS_PROBE_DELAY 250
90 #elif (MDNS_PROBE_DELAY < 100)
91  #error MDNS_PROBE_DELAY parameter is not valid
92 #endif
93 
94 //Delay before probing again when deferring to the winning host
95 #ifndef MDNS_PROBE_DEFER
96  #define MDNS_PROBE_DEFER 1000
97 #elif (MDNS_PROBE_DEFER < 100)
98  #error MDNS_PROBE_DEFER parameter is not valid
99 #endif
100 
101 //Number of announcement packets
102 #ifndef MDNS_ANNOUNCE_NUM
103  #define MDNS_ANNOUNCE_NUM 2
104 #elif (MDNS_ANNOUNCE_NUM < 1)
105  #error MDNS_ANNOUNCE_NUM parameter is not valid
106 #endif
107 
108 //Time interval between subsequent announcement packets
109 #ifndef MDNS_ANNOUNCE_DELAY
110  #define MDNS_ANNOUNCE_DELAY 1000
111 #elif (MDNS_ANNOUNCE_DELAY < 100)
112  #error MDNS_ANNOUNCE_DELAY parameter is not valid
113 #endif
114 
115 //Forward declaration of DnsSdContext structure
116 struct _MdnsResponderContext;
117 #define MdnsResponderContext struct _MdnsResponderContext
118 
119 //C++ guard
120 #ifdef __cplusplus
121  extern "C" {
122 #endif
123 
124 
125 /**
126  * @brief mDNS responder states
127  **/
128 
129 typedef enum
130 {
136 } MdnsState;
137 
138 
139 /**
140  * @brief FSM state change callback
141  **/
142 
144  NetInterface *interface, MdnsState state);
145 
146 
147 /**
148  * @brief mDNS responder settings
149  **/
150 
151 typedef struct
152 {
153  NetInterface *interface; ///<Underlying network interface
154  uint_t numAnnouncements; ///<Number of announcement packets
155  uint32_t ttl; ///<TTL resource record
158 
159 
160 /**
161  * @brief mDNS responder context
162  **/
163 
165 {
166  MdnsResponderSettings settings; ///<DNS-SD settings
167  bool_t running; ///<mDNS responder is currently running
168  MdnsState state; ///<FSM state
169  bool_t conflict; ///<Conflict detected
170  bool_t tieBreakLost; ///<Tie-break lost
171  systime_t timestamp; ///<Timestamp to manage retransmissions
172  systime_t timeout; ///<Timeout value
173  uint_t retransmitCount; ///<Retransmission counter
175 #if (IPV4_SUPPORT == ENABLED)
176  char_t ipv4ReverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv4
177  MdnsMessage ipv4Response; ///<IPv4 response message
178 #endif
179 #if (IPV6_SUPPORT == ENABLED)
180  char_t ipv6ReverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv6
181  MdnsMessage ipv6Response; ///<IPv6 response message
182 #endif
183 };
184 
185 
186 //Tick counter to handle periodic operations
188 
189 //mDNS related functions
191 
193  const MdnsResponderSettings *settings);
194 
198 
200  const char_t *hostname);
201 
204 
206 
209 
211  MdnsState newState, systime_t delay);
212 
214 
218 
219 void mdnsResponderProcessQuery(NetInterface *interface, MdnsMessage *query);
220 
222  size_t offset, const DnsQuestion *question, MdnsMessage *response);
223 
224 void mdnsResponderParseKnownAnRecord(NetInterface *interface, const MdnsMessage *query,
225  size_t queryOffset, const DnsResourceRecord *queryRecord, MdnsMessage *response);
226 
228  const MdnsMessage *query, size_t offset, const DnsResourceRecord *record);
229 
231  const MdnsMessage *response, size_t offset, const DnsResourceRecord *record);
232 
234  MdnsMessage *response, bool_t legacyUnicast);
235 
237  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
238 
240  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
241 
243  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
244 
246  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
247 
249  MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
250 
251 //C++ guard
252 #ifdef __cplusplus
253  }
254 #endif
255 
256 #endif
MdnsResponderSettings settings
DNS-SD settings.
__start_packed struct @151 DnsQuestion
Question format.
systime_t timestamp
Timestamp to manage retransmissions.
uint32_t systime_t
Definition: compiler_port.h:44
void mdnsResponderLinkChangeEvent(MdnsResponderContext *context)
Callback function for link change event.
#define DNS_MAX_IPV6_REVERSE_NAME_LEN
Definition: dns_common.h:52
char_t ipv4ReverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN+1]
Reverse DNS lookup for IPv4.
char char_t
Definition: compiler_port.h:41
NetInterface * interface
Underlying network interface.
TCP/IP stack core.
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.
uint_t numAnnouncements
Number of announcement packets.
uint8_t message[]
Definition: chap.h:150
error_t mdnsResponderStart(MdnsResponderContext *context)
Start mDNS responder.
mDNS message
Definition: mdns_common.h:75
error_t mdnsResponderSetIpv6ReverseName(MdnsResponderContext *context)
Generate domain name for reverse DNS lookup (IPv6)
#define DNS_MAX_IPV4_REVERSE_NAME_LEN
Definition: dns_common.h:50
error_t mdnsResponderStartProbing(MdnsResponderContext *context)
Restart probing process.
error_t mdnsResponderInit(MdnsResponderContext *context, const MdnsResponderSettings *settings)
mDNS responder initialization
error_t mdnsResponderSendProbe(MdnsResponderContext *context)
Send probe packet.
void mdnsResponderParseAnRecord(NetInterface *interface, const MdnsMessage *response, size_t offset, const DnsResourceRecord *record)
Parse a resource record from the Answer Section.
MdnsMessage ipv4Response
IPv4 response message.
#define MdnsResponderContext
systime_t mdnsResponderTickCounter
mDNS responder context
MdnsState state
FSM state.
error_t mdnsResponderSendGoodbye(MdnsResponderContext *context)
Send goodbye packet.
bool_t tieBreakLost
Tie-break lost.
uint_t retransmitCount
Retransmission counter.
MdnsState mdnsResponderGetState(MdnsResponderContext *context)
Retrieve current state.
error_t mdnsResponderStop(MdnsResponderContext *context)
Stop mDNS responder.
void mdnsResponderChangeHostname(MdnsResponderContext *context)
Programmatically change the host name.
void mdnsResponderTick(MdnsResponderContext *context)
mDNS responder timer handler
void mdnsResponderGetDefaultSettings(MdnsResponderSettings *settings)
Initialize settings with default values.
bool_t running
mDNS responder is currently running
bool_t conflict
Conflict detected.
mDNS responder settings
uint32_t ttl
Definition: dns_common.h:203
error_t mdnsResponderSetIpv4ReverseName(MdnsResponderContext *context)
Generate domain name for reverse DNS lookup (IPv4)
void mdnsResponderGenerateAdditionalRecords(NetInterface *interface, MdnsMessage *response, bool_t legacyUnicast)
Additional record generation.
MdnsResponderStateChangeCallback stateChangeEvent
FSM state change event.
MdnsState
mDNS responder states
char_t ipv6ReverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN+1]
Reverse DNS lookup for IPv6.
void mdnsResponderParseKnownAnRecord(NetInterface *interface, const MdnsMessage *query, size_t queryOffset, const DnsResourceRecord *queryRecord, MdnsMessage *response)
Parse a resource record from the Known-Answer Section.
uint32_t ttl
TTL resource record.
void(* MdnsResponderStateChangeCallback)(MdnsResponderContext *context, NetInterface *interface, MdnsState state)
FSM state change callback.
error_t mdnsResponderAddIpv4AddrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add A record to a mDNS message.
error_t
Error codes.
Definition: error.h:40
error_t mdnsResponderSendAnnouncement(MdnsResponderContext *context)
Send announcement packet.
error_t mdnsResponderAddIpv6AddrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add AAAA record to a mDNS message.
systime_t timeout
Timeout value.
unsigned int uint_t
Definition: compiler_port.h:43
error_t mdnsResponderSetHostname(MdnsResponderContext *context, const char_t *hostname)
Set hostname.
#define NetInterface
Definition: net.h:34
error_t mdnsResponderAddIpv6ReversePtrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add reverse address mapping PTR record (IPv6)
void mdnsResponderChangeState(MdnsResponderContext *context, MdnsState newState, systime_t delay)
Update FSM state.
error_t mdnsResponderAddIpv4ReversePtrRecord(NetInterface *interface, MdnsMessage *message, bool_t cacheFlush, uint32_t ttl)
Add reverse address mapping PTR record (IPv4)
__start_packed struct @152 DnsResourceRecord
Resource record format.
Definitions common to mDNS client and mDNS responder.
char_t hostname[]
Definition: tls.h:1443
error_t mdnsResponderParseQuestion(NetInterface *interface, const MdnsMessage *query, size_t offset, const DnsQuestion *question, MdnsMessage *response)
Parse a question.
char_t hostname[MDNS_RESPONDER_MAX_HOSTNAME_LEN+1]
Hostname.
void mdnsResponderProcessQuery(NetInterface *interface, MdnsMessage *query)
Process mDNS query message.
Common DNS routines.
#define MDNS_RESPONDER_MAX_HOSTNAME_LEN
int bool_t
Definition: compiler_port.h:47
UDP (User Datagram Protocol)
MdnsMessage ipv6Response
IPv6 response message.