BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file |
||
3 | * MDNS responder implementation |
||
4 | * |
||
5 | * @defgroup mdns MDNS |
||
6 | * @ingroup apps |
||
7 | * |
||
8 | * RFC 6762 - Multicast DNS\n |
||
9 | * RFC 6763 - DNS-Based Service Discovery\n |
||
10 | * |
||
11 | * @verbinclude mdns.txt |
||
12 | * |
||
13 | * Things left to implement: |
||
14 | * ------------------------- |
||
15 | * |
||
16 | * - Probing/conflict resolution |
||
17 | * - Sending goodbye messages (zero ttl) - shutdown, DHCP lease about to expire, DHCP turned off... |
||
18 | * - Checking that source address of unicast requests are on the same network |
||
19 | * - Limiting multicast responses to 1 per second per resource record |
||
20 | * - Fragmenting replies if required |
||
21 | * - Handling multi-packet known answers |
||
22 | * - Individual known answer detection for all local IPv6 addresses |
||
23 | * - Dynamic size of outgoing packet |
||
24 | */ |
||
25 | |||
26 | /* |
||
27 | * Copyright (c) 2015 Verisure Innovation AB |
||
28 | * All rights reserved. |
||
29 | * |
||
30 | * Redistribution and use in source and binary forms, with or without modification, |
||
31 | * are permitted provided that the following conditions are met: |
||
32 | * |
||
33 | * 1. Redistributions of source code must retain the above copyright notice, |
||
34 | * this list of conditions and the following disclaimer. |
||
35 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
36 | * this list of conditions and the following disclaimer in the documentation |
||
37 | * and/or other materials provided with the distribution. |
||
38 | * 3. The name of the author may not be used to endorse or promote products |
||
39 | * derived from this software without specific prior written permission. |
||
40 | * |
||
41 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
||
42 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
43 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
||
44 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||
45 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
||
46 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
47 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
48 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
49 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
||
50 | * OF SUCH DAMAGE. |
||
51 | * |
||
52 | * This file is part of the lwIP TCP/IP stack. |
||
53 | * |
||
54 | * Author: Erik Ekman <erik@kryo.se> |
||
55 | * |
||
56 | */ |
||
57 | |||
58 | #include "lwip/apps/mdns.h" |
||
59 | #include "lwip/apps/mdns_priv.h" |
||
60 | #include "lwip/netif.h" |
||
61 | #include "lwip/udp.h" |
||
62 | #include "lwip/ip_addr.h" |
||
63 | #include "lwip/mem.h" |
||
64 | #include "lwip/prot/dns.h" |
||
65 | #include "lwip/prot/iana.h" |
||
66 | |||
67 | #include <string.h> |
||
68 | |||
69 | #if LWIP_MDNS_RESPONDER |
||
70 | |||
71 | #if (LWIP_IPV4 && !LWIP_IGMP) |
||
72 | #error "If you want to use MDNS with IPv4, you have to define LWIP_IGMP=1 in your lwipopts.h" |
||
73 | #endif |
||
74 | #if (LWIP_IPV6 && !LWIP_IPV6_MLD) |
||
75 | #error "If you want to use MDNS with IPv6, you have to define LWIP_IPV6_MLD=1 in your lwipopts.h" |
||
76 | #endif |
||
77 | #if (!LWIP_UDP) |
||
78 | #error "If you want to use MDNS, you have to define LWIP_UDP=1 in your lwipopts.h" |
||
79 | #endif |
||
80 | |||
81 | #if LWIP_IPV4 |
||
82 | #include "lwip/igmp.h" |
||
83 | /* IPv4 multicast group 224.0.0.251 */ |
||
84 | static const ip_addr_t v4group = DNS_MQUERY_IPV4_GROUP_INIT; |
||
85 | #endif |
||
86 | |||
87 | #if LWIP_IPV6 |
||
88 | #include "lwip/mld6.h" |
||
89 | /* IPv6 multicast group FF02::FB */ |
||
90 | static const ip_addr_t v6group = DNS_MQUERY_IPV6_GROUP_INIT; |
||
91 | #endif |
||
92 | |||
93 | #define MDNS_TTL 255 |
||
94 | |||
95 | /* Stored offsets to beginning of domain names |
||
96 | * Used for compression. |
||
97 | */ |
||
98 | #define NUM_DOMAIN_OFFSETS 10 |
||
99 | #define DOMAIN_JUMP_SIZE 2 |
||
100 | #define DOMAIN_JUMP 0xc000 |
||
101 | |||
102 | static u8_t mdns_netif_client_id; |
||
103 | static struct udp_pcb *mdns_pcb; |
||
104 | NETIF_DECLARE_EXT_CALLBACK(netif_callback) |
||
105 | |||
106 | #define NETIF_TO_HOST(netif) (struct mdns_host*)(netif_get_client_data(netif, mdns_netif_client_id)) |
||
107 | |||
108 | #define TOPDOMAIN_LOCAL "local" |
||
109 | |||
110 | #define REVERSE_PTR_TOPDOMAIN "arpa" |
||
111 | #define REVERSE_PTR_V4_DOMAIN "in-addr" |
||
112 | #define REVERSE_PTR_V6_DOMAIN "ip6" |
||
113 | |||
114 | #define SRV_PRIORITY 0 |
||
115 | #define SRV_WEIGHT 0 |
||
116 | |||
117 | /* Payload size allocated for each outgoing UDP packet */ |
||
118 | #define OUTPACKET_SIZE 500 |
||
119 | |||
120 | /* Lookup from hostname -> IPv4 */ |
||
121 | #define REPLY_HOST_A 0x01 |
||
122 | /* Lookup from IPv4/v6 -> hostname */ |
||
123 | #define REPLY_HOST_PTR_V4 0x02 |
||
124 | /* Lookup from hostname -> IPv6 */ |
||
125 | #define REPLY_HOST_AAAA 0x04 |
||
126 | /* Lookup from hostname -> IPv6 */ |
||
127 | #define REPLY_HOST_PTR_V6 0x08 |
||
128 | |||
129 | /* Lookup for service types */ |
||
130 | #define REPLY_SERVICE_TYPE_PTR 0x10 |
||
131 | /* Lookup for instances of service */ |
||
132 | #define REPLY_SERVICE_NAME_PTR 0x20 |
||
133 | /* Lookup for location of service instance */ |
||
134 | #define REPLY_SERVICE_SRV 0x40 |
||
135 | /* Lookup for text info on service instance */ |
||
136 | #define REPLY_SERVICE_TXT 0x80 |
||
137 | |||
138 | static const char *dnssd_protos[] = { |
||
139 | "_udp", /* DNSSD_PROTO_UDP */ |
||
140 | "_tcp", /* DNSSD_PROTO_TCP */ |
||
141 | }; |
||
142 | |||
143 | /** Description of a service */ |
||
144 | struct mdns_service { |
||
145 | /** TXT record to answer with */ |
||
146 | struct mdns_domain txtdata; |
||
147 | /** Name of service, like 'myweb' */ |
||
148 | char name[MDNS_LABEL_MAXLEN + 1]; |
||
149 | /** Type of service, like '_http' */ |
||
150 | char service[MDNS_LABEL_MAXLEN + 1]; |
||
151 | /** Callback function and userdata |
||
152 | * to update txtdata buffer */ |
||
153 | service_get_txt_fn_t txt_fn; |
||
154 | void *txt_userdata; |
||
155 | /** TTL in seconds of SRV/TXT replies */ |
||
156 | u32_t dns_ttl; |
||
157 | /** Protocol, TCP or UDP */ |
||
158 | u16_t proto; |
||
159 | /** Port of the service */ |
||
160 | u16_t port; |
||
161 | }; |
||
162 | |||
163 | /** Description of a host/netif */ |
||
164 | struct mdns_host { |
||
165 | /** Hostname */ |
||
166 | char name[MDNS_LABEL_MAXLEN + 1]; |
||
167 | /** Pointer to services */ |
||
168 | struct mdns_service *services[MDNS_MAX_SERVICES]; |
||
169 | /** TTL in seconds of A/AAAA/PTR replies */ |
||
170 | u32_t dns_ttl; |
||
171 | }; |
||
172 | |||
173 | /** Information about received packet */ |
||
174 | struct mdns_packet { |
||
175 | /** Sender IP/port */ |
||
176 | ip_addr_t source_addr; |
||
177 | u16_t source_port; |
||
178 | /** If packet was received unicast */ |
||
179 | u16_t recv_unicast; |
||
180 | /** Netif that received the packet */ |
||
181 | struct netif *netif; |
||
182 | /** Packet data */ |
||
183 | struct pbuf *pbuf; |
||
184 | /** Current parsing offset in packet */ |
||
185 | u16_t parse_offset; |
||
186 | /** Identifier. Used in legacy queries */ |
||
187 | u16_t tx_id; |
||
188 | /** Number of questions in packet, |
||
189 | * read from packet header */ |
||
190 | u16_t questions; |
||
191 | /** Number of unparsed questions */ |
||
192 | u16_t questions_left; |
||
193 | /** Number of answers in packet, |
||
194 | * (sum of normal, authorative and additional answers) |
||
195 | * read from packet header */ |
||
196 | u16_t answers; |
||
197 | /** Number of unparsed answers */ |
||
198 | u16_t answers_left; |
||
199 | }; |
||
200 | |||
201 | /** Information about outgoing packet */ |
||
202 | struct mdns_outpacket { |
||
203 | /** Netif to send the packet on */ |
||
204 | struct netif *netif; |
||
205 | /** Packet data */ |
||
206 | struct pbuf *pbuf; |
||
207 | /** Current write offset in packet */ |
||
208 | u16_t write_offset; |
||
209 | /** Identifier. Used in legacy queries */ |
||
210 | u16_t tx_id; |
||
211 | /** Destination IP/port if sent unicast */ |
||
212 | ip_addr_t dest_addr; |
||
213 | u16_t dest_port; |
||
214 | /** Number of questions written */ |
||
215 | u16_t questions; |
||
216 | /** Number of normal answers written */ |
||
217 | u16_t answers; |
||
218 | /** Number of additional answers written */ |
||
219 | u16_t additional; |
||
220 | /** Offsets for written domain names in packet. |
||
221 | * Used for compression */ |
||
222 | u16_t domain_offsets[NUM_DOMAIN_OFFSETS]; |
||
223 | /** If all answers in packet should set cache_flush bit */ |
||
224 | u8_t cache_flush; |
||
225 | /** If reply should be sent unicast */ |
||
226 | u8_t unicast_reply; |
||
227 | /** If legacy query. (tx_id needed, and write |
||
228 | * question again in reply before answer) */ |
||
229 | u8_t legacy_query; |
||
230 | /* Reply bitmask for host information */ |
||
231 | u8_t host_replies; |
||
232 | /* Bitmask for which reverse IPv6 hosts to answer */ |
||
233 | u8_t host_reverse_v6_replies; |
||
234 | /* Reply bitmask per service */ |
||
235 | u8_t serv_replies[MDNS_MAX_SERVICES]; |
||
236 | }; |
||
237 | |||
238 | /** Domain, type and class. |
||
239 | * Shared between questions and answers */ |
||
240 | struct mdns_rr_info { |
||
241 | struct mdns_domain domain; |
||
242 | u16_t type; |
||
243 | u16_t klass; |
||
244 | }; |
||
245 | |||
246 | struct mdns_question { |
||
247 | struct mdns_rr_info info; |
||
248 | /** unicast reply requested */ |
||
249 | u16_t unicast; |
||
250 | }; |
||
251 | |||
252 | struct mdns_answer { |
||
253 | struct mdns_rr_info info; |
||
254 | /** cache flush command bit */ |
||
255 | u16_t cache_flush; |
||
256 | /* Validity time in seconds */ |
||
257 | u32_t ttl; |
||
258 | /** Length of variable answer */ |
||
259 | u16_t rd_length; |
||
260 | /** Offset of start of variable answer in packet */ |
||
261 | u16_t rd_offset; |
||
262 | }; |
||
263 | |||
264 | static err_t |
||
265 | mdns_domain_add_label_base(struct mdns_domain *domain, u8_t len) |
||
266 | { |
||
267 | if (len > MDNS_LABEL_MAXLEN) { |
||
268 | return ERR_VAL; |
||
269 | } |
||
270 | if (len > 0 && (1 + len + domain->length >= MDNS_DOMAIN_MAXLEN)) { |
||
271 | return ERR_VAL; |
||
272 | } |
||
273 | /* Allow only zero marker on last byte */ |
||
274 | if (len == 0 && (1 + domain->length > MDNS_DOMAIN_MAXLEN)) { |
||
275 | return ERR_VAL; |
||
276 | } |
||
277 | domain->name[domain->length] = len; |
||
278 | domain->length++; |
||
279 | return ERR_OK; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Add a label part to a domain |
||
284 | * @param domain The domain to add a label to |
||
285 | * @param label The label to add, like <hostname>, 'local', 'com' or '' |
||
286 | * @param len The length of the label |
||
287 | * @return ERR_OK on success, an err_t otherwise if label too long |
||
288 | */ |
||
289 | err_t |
||
290 | mdns_domain_add_label(struct mdns_domain *domain, const char *label, u8_t len) |
||
291 | { |
||
292 | err_t err = mdns_domain_add_label_base(domain, len); |
||
293 | if (err != ERR_OK) { |
||
294 | return err; |
||
295 | } |
||
296 | if (len) { |
||
297 | MEMCPY(&domain->name[domain->length], label, len); |
||
298 | domain->length += len; |
||
299 | } |
||
300 | return ERR_OK; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Add a label part to a domain (@see mdns_domain_add_label but copy directly from pbuf) |
||
305 | */ |
||
306 | static err_t |
||
307 | mdns_domain_add_label_pbuf(struct mdns_domain *domain, const struct pbuf *p, u16_t offset, u8_t len) |
||
308 | { |
||
309 | err_t err = mdns_domain_add_label_base(domain, len); |
||
310 | if (err != ERR_OK) { |
||
311 | return err; |
||
312 | } |
||
313 | if (len) { |
||
314 | if (pbuf_copy_partial(p, &domain->name[domain->length], len, offset) != len) { |
||
315 | /* take back the ++ done before */ |
||
316 | domain->length--; |
||
317 | return ERR_ARG; |
||
318 | } |
||
319 | domain->length += len; |
||
320 | } |
||
321 | return ERR_OK; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Internal readname function with max 6 levels of recursion following jumps |
||
326 | * while decompressing name |
||
327 | */ |
||
328 | static u16_t |
||
329 | mdns_readname_loop(struct pbuf *p, u16_t offset, struct mdns_domain *domain, unsigned depth) |
||
330 | { |
||
331 | u8_t c; |
||
332 | |||
333 | do { |
||
334 | if (depth > 5) { |
||
335 | /* Too many jumps */ |
||
336 | return MDNS_READNAME_ERROR; |
||
337 | } |
||
338 | |||
339 | c = pbuf_get_at(p, offset); |
||
340 | offset++; |
||
341 | |||
342 | /* is this a compressed label? */ |
||
343 | if ((c & 0xc0) == 0xc0) { |
||
344 | u16_t jumpaddr; |
||
345 | if (offset >= p->tot_len) { |
||
346 | /* Make sure both jump bytes fit in the packet */ |
||
347 | return MDNS_READNAME_ERROR; |
||
348 | } |
||
349 | jumpaddr = (((c & 0x3f) << 8) | (pbuf_get_at(p, offset) & 0xff)); |
||
350 | offset++; |
||
351 | if (jumpaddr >= SIZEOF_DNS_HDR && jumpaddr < p->tot_len) { |
||
352 | u16_t res; |
||
353 | /* Recursive call, maximum depth will be checked */ |
||
354 | res = mdns_readname_loop(p, jumpaddr, domain, depth + 1); |
||
355 | /* Dont return offset since new bytes were not read (jumped to somewhere in packet) */ |
||
356 | if (res == MDNS_READNAME_ERROR) { |
||
357 | return res; |
||
358 | } |
||
359 | } else { |
||
360 | return MDNS_READNAME_ERROR; |
||
361 | } |
||
362 | break; |
||
363 | } |
||
364 | |||
365 | /* normal label */ |
||
366 | if (c <= MDNS_LABEL_MAXLEN) { |
||
367 | err_t res; |
||
368 | |||
369 | if (c + domain->length >= MDNS_DOMAIN_MAXLEN) { |
||
370 | return MDNS_READNAME_ERROR; |
||
371 | } |
||
372 | res = mdns_domain_add_label_pbuf(domain, p, offset, c); |
||
373 | if (res != ERR_OK) { |
||
374 | return MDNS_READNAME_ERROR; |
||
375 | } |
||
376 | offset += c; |
||
377 | } else { |
||
378 | /* bad length byte */ |
||
379 | return MDNS_READNAME_ERROR; |
||
380 | } |
||
381 | } while (c != 0); |
||
382 | |||
383 | return offset; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Read possibly compressed domain name from packet buffer |
||
388 | * @param p The packet |
||
389 | * @param offset start position of domain name in packet |
||
390 | * @param domain The domain name destination |
||
391 | * @return The new offset after the domain, or MDNS_READNAME_ERROR |
||
392 | * if reading failed |
||
393 | */ |
||
394 | u16_t |
||
395 | mdns_readname(struct pbuf *p, u16_t offset, struct mdns_domain *domain) |
||
396 | { |
||
397 | memset(domain, 0, sizeof(struct mdns_domain)); |
||
398 | return mdns_readname_loop(p, offset, domain, 0); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Print domain name to debug output |
||
403 | * @param domain The domain name |
||
404 | */ |
||
405 | static void |
||
406 | mdns_domain_debug_print(struct mdns_domain *domain) |
||
407 | { |
||
408 | u8_t *src = domain->name; |
||
409 | u8_t i; |
||
410 | |||
411 | while (*src) { |
||
412 | u8_t label_len = *src; |
||
413 | src++; |
||
414 | for (i = 0; i < label_len; i++) { |
||
415 | LWIP_DEBUGF(MDNS_DEBUG, ("%c", src[i])); |
||
416 | } |
||
417 | src += label_len; |
||
418 | LWIP_DEBUGF(MDNS_DEBUG, (".")); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Return 1 if contents of domains match (case-insensitive) |
||
424 | * @param a Domain name to compare 1 |
||
425 | * @param b Domain name to compare 2 |
||
426 | * @return 1 if domains are equal ignoring case, 0 otherwise |
||
427 | */ |
||
428 | int |
||
429 | mdns_domain_eq(struct mdns_domain *a, struct mdns_domain *b) |
||
430 | { |
||
431 | u8_t *ptra, *ptrb; |
||
432 | u8_t len; |
||
433 | int res; |
||
434 | |||
435 | if (a->length != b->length) { |
||
436 | return 0; |
||
437 | } |
||
438 | |||
439 | ptra = a->name; |
||
440 | ptrb = b->name; |
||
441 | while (*ptra && *ptrb && ptra < &a->name[a->length]) { |
||
442 | if (*ptra != *ptrb) { |
||
443 | return 0; |
||
444 | } |
||
445 | len = *ptra; |
||
446 | ptra++; |
||
447 | ptrb++; |
||
448 | res = lwip_strnicmp((char *) ptra, (char *) ptrb, len); |
||
449 | if (res != 0) { |
||
450 | return 0; |
||
451 | } |
||
452 | ptra += len; |
||
453 | ptrb += len; |
||
454 | } |
||
455 | if (*ptra != *ptrb && ptra < &a->name[a->length]) { |
||
456 | return 0; |
||
457 | } |
||
458 | return 1; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Call user supplied function to setup TXT data |
||
463 | * @param service The service to build TXT record for |
||
464 | */ |
||
465 | static void |
||
466 | mdns_prepare_txtdata(struct mdns_service *service) |
||
467 | { |
||
468 | memset(&service->txtdata, 0, sizeof(struct mdns_domain)); |
||
469 | if (service->txt_fn) { |
||
470 | service->txt_fn(service, service->txt_userdata); |
||
471 | } |
||
472 | } |
||
473 | |||
474 | #if LWIP_IPV4 |
||
475 | /** |
||
476 | * Build domain for reverse lookup of IPv4 address |
||
477 | * like 12.0.168.192.in-addr.arpa. for 192.168.0.12 |
||
478 | * @param domain Where to write the domain name |
||
479 | * @param addr Pointer to an IPv4 address to encode |
||
480 | * @return ERR_OK if domain was written, an err_t otherwise |
||
481 | */ |
||
482 | static err_t |
||
483 | mdns_build_reverse_v4_domain(struct mdns_domain *domain, const ip4_addr_t *addr) |
||
484 | { |
||
485 | int i; |
||
486 | err_t res; |
||
487 | const u8_t *ptr; |
||
488 | if (!domain || !addr) { |
||
489 | return ERR_ARG; |
||
490 | } |
||
491 | memset(domain, 0, sizeof(struct mdns_domain)); |
||
492 | ptr = (const u8_t *) addr; |
||
493 | for (i = sizeof(ip4_addr_t) - 1; i >= 0; i--) { |
||
494 | char buf[4]; |
||
495 | u8_t val = ptr[i]; |
||
496 | |||
497 | lwip_itoa(buf, sizeof(buf), val); |
||
498 | res = mdns_domain_add_label(domain, buf, (u8_t)strlen(buf)); |
||
499 | LWIP_ERROR("mdns_build_reverse_v4_domain: Failed to add label", (res == ERR_OK), return res); |
||
500 | } |
||
501 | res = mdns_domain_add_label(domain, REVERSE_PTR_V4_DOMAIN, (u8_t)(sizeof(REVERSE_PTR_V4_DOMAIN) - 1)); |
||
502 | LWIP_ERROR("mdns_build_reverse_v4_domain: Failed to add label", (res == ERR_OK), return res); |
||
503 | res = mdns_domain_add_label(domain, REVERSE_PTR_TOPDOMAIN, (u8_t)(sizeof(REVERSE_PTR_TOPDOMAIN) - 1)); |
||
504 | LWIP_ERROR("mdns_build_reverse_v4_domain: Failed to add label", (res == ERR_OK), return res); |
||
505 | res = mdns_domain_add_label(domain, NULL, 0); |
||
506 | LWIP_ERROR("mdns_build_reverse_v4_domain: Failed to add label", (res == ERR_OK), return res); |
||
507 | |||
508 | return ERR_OK; |
||
509 | } |
||
510 | #endif |
||
511 | |||
512 | #if LWIP_IPV6 |
||
513 | /** |
||
514 | * Build domain for reverse lookup of IP address |
||
515 | * like b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa. for 2001:db8::567:89ab |
||
516 | * @param domain Where to write the domain name |
||
517 | * @param addr Pointer to an IPv6 address to encode |
||
518 | * @return ERR_OK if domain was written, an err_t otherwise |
||
519 | */ |
||
520 | static err_t |
||
521 | mdns_build_reverse_v6_domain(struct mdns_domain *domain, const ip6_addr_t *addr) |
||
522 | { |
||
523 | int i; |
||
524 | err_t res; |
||
525 | const u8_t *ptr; |
||
526 | if (!domain || !addr) { |
||
527 | return ERR_ARG; |
||
528 | } |
||
529 | memset(domain, 0, sizeof(struct mdns_domain)); |
||
530 | ptr = (const u8_t *) addr; |
||
531 | for (i = sizeof(ip6_addr_p_t) - 1; i >= 0; i--) { |
||
532 | char buf; |
||
533 | u8_t byte = ptr[i]; |
||
534 | int j; |
||
535 | for (j = 0; j < 2; j++) { |
||
536 | if ((byte & 0x0F) < 0xA) { |
||
537 | buf = '0' + (byte & 0x0F); |
||
538 | } else { |
||
539 | buf = 'a' + (byte & 0x0F) - 0xA; |
||
540 | } |
||
541 | res = mdns_domain_add_label(domain, &buf, sizeof(buf)); |
||
542 | LWIP_ERROR("mdns_build_reverse_v6_domain: Failed to add label", (res == ERR_OK), return res); |
||
543 | byte >>= 4; |
||
544 | } |
||
545 | } |
||
546 | res = mdns_domain_add_label(domain, REVERSE_PTR_V6_DOMAIN, (u8_t)(sizeof(REVERSE_PTR_V6_DOMAIN) - 1)); |
||
547 | LWIP_ERROR("mdns_build_reverse_v6_domain: Failed to add label", (res == ERR_OK), return res); |
||
548 | res = mdns_domain_add_label(domain, REVERSE_PTR_TOPDOMAIN, (u8_t)(sizeof(REVERSE_PTR_TOPDOMAIN) - 1)); |
||
549 | LWIP_ERROR("mdns_build_reverse_v6_domain: Failed to add label", (res == ERR_OK), return res); |
||
550 | res = mdns_domain_add_label(domain, NULL, 0); |
||
551 | LWIP_ERROR("mdns_build_reverse_v6_domain: Failed to add label", (res == ERR_OK), return res); |
||
552 | |||
553 | return ERR_OK; |
||
554 | } |
||
555 | #endif |
||
556 | |||
557 | /* Add .local. to domain */ |
||
558 | static err_t |
||
559 | mdns_add_dotlocal(struct mdns_domain *domain) |
||
560 | { |
||
561 | err_t res = mdns_domain_add_label(domain, TOPDOMAIN_LOCAL, (u8_t)(sizeof(TOPDOMAIN_LOCAL) - 1)); |
||
562 | LWIP_ERROR("mdns_add_dotlocal: Failed to add label", (res == ERR_OK), return res); |
||
563 | return mdns_domain_add_label(domain, NULL, 0); |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * Build the <hostname>.local. domain name |
||
568 | * @param domain Where to write the domain name |
||
569 | * @param mdns TMDNS netif descriptor. |
||
570 | * @return ERR_OK if domain <hostname>.local. was written, an err_t otherwise |
||
571 | */ |
||
572 | static err_t |
||
573 | mdns_build_host_domain(struct mdns_domain *domain, struct mdns_host *mdns) |
||
574 | { |
||
575 | err_t res; |
||
576 | memset(domain, 0, sizeof(struct mdns_domain)); |
||
577 | LWIP_ERROR("mdns_build_host_domain: mdns != NULL", (mdns != NULL), return ERR_VAL); |
||
578 | res = mdns_domain_add_label(domain, mdns->name, (u8_t)strlen(mdns->name)); |
||
579 | LWIP_ERROR("mdns_build_host_domain: Failed to add label", (res == ERR_OK), return res); |
||
580 | return mdns_add_dotlocal(domain); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Build the lookup-all-services special DNS-SD domain name |
||
585 | * @param domain Where to write the domain name |
||
586 | * @return ERR_OK if domain _services._dns-sd._udp.local. was written, an err_t otherwise |
||
587 | */ |
||
588 | static err_t |
||
589 | mdns_build_dnssd_domain(struct mdns_domain *domain) |
||
590 | { |
||
591 | err_t res; |
||
592 | memset(domain, 0, sizeof(struct mdns_domain)); |
||
593 | res = mdns_domain_add_label(domain, "_services", (u8_t)(sizeof("_services") - 1)); |
||
594 | LWIP_ERROR("mdns_build_dnssd_domain: Failed to add label", (res == ERR_OK), return res); |
||
595 | res = mdns_domain_add_label(domain, "_dns-sd", (u8_t)(sizeof("_dns-sd") - 1)); |
||
596 | LWIP_ERROR("mdns_build_dnssd_domain: Failed to add label", (res == ERR_OK), return res); |
||
597 | res = mdns_domain_add_label(domain, dnssd_protos[DNSSD_PROTO_UDP], (u8_t)strlen(dnssd_protos[DNSSD_PROTO_UDP])); |
||
598 | LWIP_ERROR("mdns_build_dnssd_domain: Failed to add label", (res == ERR_OK), return res); |
||
599 | return mdns_add_dotlocal(domain); |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Build domain name for a service |
||
604 | * @param domain Where to write the domain name |
||
605 | * @param service The service struct, containing service name, type and protocol |
||
606 | * @param include_name Whether to include the service name in the domain |
||
607 | * @return ERR_OK if domain was written. If service name is included, |
||
608 | * <name>.<type>.<proto>.local. will be written, otherwise <type>.<proto>.local. |
||
609 | * An err_t is returned on error. |
||
610 | */ |
||
611 | static err_t |
||
612 | mdns_build_service_domain(struct mdns_domain *domain, struct mdns_service *service, int include_name) |
||
613 | { |
||
614 | err_t res; |
||
615 | memset(domain, 0, sizeof(struct mdns_domain)); |
||
616 | if (include_name) { |
||
617 | res = mdns_domain_add_label(domain, service->name, (u8_t)strlen(service->name)); |
||
618 | LWIP_ERROR("mdns_build_service_domain: Failed to add label", (res == ERR_OK), return res); |
||
619 | } |
||
620 | res = mdns_domain_add_label(domain, service->service, (u8_t)strlen(service->service)); |
||
621 | LWIP_ERROR("mdns_build_service_domain: Failed to add label", (res == ERR_OK), return res); |
||
622 | res = mdns_domain_add_label(domain, dnssd_protos[service->proto], (u8_t)strlen(dnssd_protos[service->proto])); |
||
623 | LWIP_ERROR("mdns_build_service_domain: Failed to add label", (res == ERR_OK), return res); |
||
624 | return mdns_add_dotlocal(domain); |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Check which replies we should send for a host/netif based on question |
||
629 | * @param netif The network interface that received the question |
||
630 | * @param rr Domain/type/class from a question |
||
631 | * @param reverse_v6_reply Bitmask of which IPv6 addresses to send reverse PTRs for |
||
632 | * if reply bit has REPLY_HOST_PTR_V6 set |
||
633 | * @return Bitmask of which replies to send |
||
634 | */ |
||
635 | static int |
||
636 | check_host(struct netif *netif, struct mdns_rr_info *rr, u8_t *reverse_v6_reply) |
||
637 | { |
||
638 | err_t res; |
||
639 | int replies = 0; |
||
640 | struct mdns_domain mydomain; |
||
641 | |||
642 | LWIP_UNUSED_ARG(reverse_v6_reply); /* if ipv6 is disabled */ |
||
643 | |||
644 | if (rr->klass != DNS_RRCLASS_IN && rr->klass != DNS_RRCLASS_ANY) { |
||
645 | /* Invalid class */ |
||
646 | return replies; |
||
647 | } |
||
648 | |||
649 | /* Handle PTR for our addresses */ |
||
650 | if (rr->type == DNS_RRTYPE_PTR || rr->type == DNS_RRTYPE_ANY) { |
||
651 | #if LWIP_IPV6 |
||
652 | int i; |
||
653 | for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { |
||
654 | if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) { |
||
655 | res = mdns_build_reverse_v6_domain(&mydomain, netif_ip6_addr(netif, i)); |
||
656 | if (res == ERR_OK && mdns_domain_eq(&rr->domain, &mydomain)) { |
||
657 | replies |= REPLY_HOST_PTR_V6; |
||
658 | /* Mark which addresses where requested */ |
||
659 | if (reverse_v6_reply) { |
||
660 | *reverse_v6_reply |= (1 << i); |
||
661 | } |
||
662 | } |
||
663 | } |
||
664 | } |
||
665 | #endif |
||
666 | #if LWIP_IPV4 |
||
667 | if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) { |
||
668 | res = mdns_build_reverse_v4_domain(&mydomain, netif_ip4_addr(netif)); |
||
669 | if (res == ERR_OK && mdns_domain_eq(&rr->domain, &mydomain)) { |
||
670 | replies |= REPLY_HOST_PTR_V4; |
||
671 | } |
||
672 | } |
||
673 | #endif |
||
674 | } |
||
675 | |||
676 | res = mdns_build_host_domain(&mydomain, NETIF_TO_HOST(netif)); |
||
677 | /* Handle requests for our hostname */ |
||
678 | if (res == ERR_OK && mdns_domain_eq(&rr->domain, &mydomain)) { |
||
679 | /* TODO return NSEC if unsupported protocol requested */ |
||
680 | #if LWIP_IPV4 |
||
681 | if (!ip4_addr_isany_val(*netif_ip4_addr(netif)) |
||
682 | && (rr->type == DNS_RRTYPE_A || rr->type == DNS_RRTYPE_ANY)) { |
||
683 | replies |= REPLY_HOST_A; |
||
684 | } |
||
685 | #endif |
||
686 | #if LWIP_IPV6 |
||
687 | if (rr->type == DNS_RRTYPE_AAAA || rr->type == DNS_RRTYPE_ANY) { |
||
688 | replies |= REPLY_HOST_AAAA; |
||
689 | } |
||
690 | #endif |
||
691 | } |
||
692 | |||
693 | return replies; |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * Check which replies we should send for a service based on question |
||
698 | * @param service A registered MDNS service |
||
699 | * @param rr Domain/type/class from a question |
||
700 | * @return Bitmask of which replies to send |
||
701 | */ |
||
702 | static int |
||
703 | check_service(struct mdns_service *service, struct mdns_rr_info *rr) |
||
704 | { |
||
705 | err_t res; |
||
706 | int replies = 0; |
||
707 | struct mdns_domain mydomain; |
||
708 | |||
709 | if (rr->klass != DNS_RRCLASS_IN && rr->klass != DNS_RRCLASS_ANY) { |
||
710 | /* Invalid class */ |
||
711 | return 0; |
||
712 | } |
||
713 | |||
714 | res = mdns_build_dnssd_domain(&mydomain); |
||
715 | if (res == ERR_OK && mdns_domain_eq(&rr->domain, &mydomain) && |
||
716 | (rr->type == DNS_RRTYPE_PTR || rr->type == DNS_RRTYPE_ANY)) { |
||
717 | /* Request for all service types */ |
||
718 | replies |= REPLY_SERVICE_TYPE_PTR; |
||
719 | } |
||
720 | |||
721 | res = mdns_build_service_domain(&mydomain, service, 0); |
||
722 | if (res == ERR_OK && mdns_domain_eq(&rr->domain, &mydomain) && |
||
723 | (rr->type == DNS_RRTYPE_PTR || rr->type == DNS_RRTYPE_ANY)) { |
||
724 | /* Request for the instance of my service */ |
||
725 | replies |= REPLY_SERVICE_NAME_PTR; |
||
726 | } |
||
727 | |||
728 | res = mdns_build_service_domain(&mydomain, service, 1); |
||
729 | if (res == ERR_OK && mdns_domain_eq(&rr->domain, &mydomain)) { |
||
730 | /* Request for info about my service */ |
||
731 | if (rr->type == DNS_RRTYPE_SRV || rr->type == DNS_RRTYPE_ANY) { |
||
732 | replies |= REPLY_SERVICE_SRV; |
||
733 | } |
||
734 | if (rr->type == DNS_RRTYPE_TXT || rr->type == DNS_RRTYPE_ANY) { |
||
735 | replies |= REPLY_SERVICE_TXT; |
||
736 | } |
||
737 | } |
||
738 | |||
739 | return replies; |
||
740 | } |
||
741 | |||
742 | /** |
||
743 | * Return bytes needed to write before jump for best result of compressing supplied domain |
||
744 | * against domain in outpacket starting at specified offset. |
||
745 | * If a match is found, offset is updated to where to jump to |
||
746 | * @param pbuf Pointer to pbuf with the partially constructed DNS packet |
||
747 | * @param offset Start position of a domain written earlier. If this location is suitable |
||
748 | * for compression, the pointer is updated to where in the domain to jump to. |
||
749 | * @param domain The domain to write |
||
750 | * @return Number of bytes to write of the new domain before writing a jump to the offset. |
||
751 | * If compression can not be done against this previous domain name, the full new |
||
752 | * domain length is returned. |
||
753 | */ |
||
754 | u16_t |
||
755 | mdns_compress_domain(struct pbuf *pbuf, u16_t *offset, struct mdns_domain *domain) |
||
756 | { |
||
757 | struct mdns_domain target; |
||
758 | u16_t target_end; |
||
759 | u8_t target_len; |
||
760 | u8_t writelen = 0; |
||
761 | u8_t *ptr; |
||
762 | if (pbuf == NULL) { |
||
763 | return domain->length; |
||
764 | } |
||
765 | target_end = mdns_readname(pbuf, *offset, &target); |
||
766 | if (target_end == MDNS_READNAME_ERROR) { |
||
767 | return domain->length; |
||
768 | } |
||
769 | target_len = (u8_t)(target_end - *offset); |
||
770 | ptr = domain->name; |
||
771 | while (writelen < domain->length) { |
||
772 | u8_t domainlen = (u8_t)(domain->length - writelen); |
||
773 | u8_t labellen; |
||
774 | if (domainlen <= target.length && domainlen > DOMAIN_JUMP_SIZE) { |
||
775 | /* Compare domains if target is long enough, and we have enough left of the domain */ |
||
776 | u8_t targetpos = (u8_t)(target.length - domainlen); |
||
777 | if ((targetpos + DOMAIN_JUMP_SIZE) >= target_len) { |
||
778 | /* We are checking at or beyond a jump in the original, stop looking */ |
||
779 | break; |
||
780 | } |
||
781 | if (target.length >= domainlen && |
||
782 | memcmp(&domain->name[writelen], &target.name[targetpos], domainlen) == 0) { |
||
783 | *offset += targetpos; |
||
784 | return writelen; |
||
785 | } |
||
786 | } |
||
787 | /* Skip to next label in domain */ |
||
788 | labellen = *ptr; |
||
789 | writelen += 1 + labellen; |
||
790 | ptr += 1 + labellen; |
||
791 | } |
||
792 | /* Nothing found */ |
||
793 | return domain->length; |
||
794 | } |
||
795 | |||
796 | /** |
||
797 | * Write domain to outpacket. Compression will be attempted, |
||
798 | * unless domain->skip_compression is set. |
||
799 | * @param outpkt The outpacket to write to |
||
800 | * @param domain The domain name to write |
||
801 | * @return ERR_OK on success, an err_t otherwise |
||
802 | */ |
||
803 | static err_t |
||
804 | mdns_write_domain(struct mdns_outpacket *outpkt, struct mdns_domain *domain) |
||
805 | { |
||
806 | int i; |
||
807 | err_t res; |
||
808 | u16_t writelen = domain->length; |
||
809 | u16_t jump_offset = 0; |
||
810 | u16_t jump; |
||
811 | |||
812 | if (!domain->skip_compression) { |
||
813 | for (i = 0; i < NUM_DOMAIN_OFFSETS; ++i) { |
||
814 | u16_t offset = outpkt->domain_offsets[i]; |
||
815 | if (offset) { |
||
816 | u16_t len = mdns_compress_domain(outpkt->pbuf, &offset, domain); |
||
817 | if (len < writelen) { |
||
818 | writelen = len; |
||
819 | jump_offset = offset; |
||
820 | } |
||
821 | } |
||
822 | } |
||
823 | } |
||
824 | |||
825 | if (writelen) { |
||
826 | /* Write uncompressed part of name */ |
||
827 | res = pbuf_take_at(outpkt->pbuf, domain->name, writelen, outpkt->write_offset); |
||
828 | if (res != ERR_OK) { |
||
829 | return res; |
||
830 | } |
||
831 | |||
832 | /* Store offset of this new domain */ |
||
833 | for (i = 0; i < NUM_DOMAIN_OFFSETS; ++i) { |
||
834 | if (outpkt->domain_offsets[i] == 0) { |
||
835 | outpkt->domain_offsets[i] = outpkt->write_offset; |
||
836 | break; |
||
837 | } |
||
838 | } |
||
839 | |||
840 | outpkt->write_offset += writelen; |
||
841 | } |
||
842 | if (jump_offset) { |
||
843 | /* Write jump */ |
||
844 | jump = lwip_htons(DOMAIN_JUMP | jump_offset); |
||
845 | res = pbuf_take_at(outpkt->pbuf, &jump, DOMAIN_JUMP_SIZE, outpkt->write_offset); |
||
846 | if (res != ERR_OK) { |
||
847 | return res; |
||
848 | } |
||
849 | outpkt->write_offset += DOMAIN_JUMP_SIZE; |
||
850 | } |
||
851 | return ERR_OK; |
||
852 | } |
||
853 | |||
854 | /** |
||
855 | * Write a question to an outpacket |
||
856 | * A question contains domain, type and class. Since an answer also starts with these fields this function is also |
||
857 | * called from mdns_add_answer(). |
||
858 | * @param outpkt The outpacket to write to |
||
859 | * @param domain The domain name the answer is for |
||
860 | * @param type The DNS type of the answer (like 'AAAA', 'SRV') |
||
861 | * @param klass The DNS type of the answer (like 'IN') |
||
862 | * @param unicast If highest bit in class should be set, to instruct the responder to |
||
863 | * reply with a unicast packet |
||
864 | * @return ERR_OK on success, an err_t otherwise |
||
865 | */ |
||
866 | static err_t |
||
867 | mdns_add_question(struct mdns_outpacket *outpkt, struct mdns_domain *domain, u16_t type, u16_t klass, u16_t unicast) |
||
868 | { |
||
869 | u16_t question_len; |
||
870 | u16_t field16; |
||
871 | err_t res; |
||
872 | |||
873 | if (!outpkt->pbuf) { |
||
874 | /* If no pbuf is active, allocate one */ |
||
875 | outpkt->pbuf = pbuf_alloc(PBUF_TRANSPORT, OUTPACKET_SIZE, PBUF_RAM); |
||
876 | if (!outpkt->pbuf) { |
||
877 | return ERR_MEM; |
||
878 | } |
||
879 | outpkt->write_offset = SIZEOF_DNS_HDR; |
||
880 | } |
||
881 | |||
882 | /* Worst case calculation. Domain string might be compressed */ |
||
883 | question_len = domain->length + sizeof(type) + sizeof(klass); |
||
884 | if (outpkt->write_offset + question_len > outpkt->pbuf->tot_len) { |
||
885 | /* No space */ |
||
886 | return ERR_MEM; |
||
887 | } |
||
888 | |||
889 | /* Write name */ |
||
890 | res = mdns_write_domain(outpkt, domain); |
||
891 | if (res != ERR_OK) { |
||
892 | return res; |
||
893 | } |
||
894 | |||
895 | /* Write type */ |
||
896 | field16 = lwip_htons(type); |
||
897 | res = pbuf_take_at(outpkt->pbuf, &field16, sizeof(field16), outpkt->write_offset); |
||
898 | if (res != ERR_OK) { |
||
899 | return res; |
||
900 | } |
||
901 | outpkt->write_offset += sizeof(field16); |
||
902 | |||
903 | /* Write class */ |
||
904 | if (unicast) { |
||
905 | klass |= 0x8000; |
||
906 | } |
||
907 | field16 = lwip_htons(klass); |
||
908 | res = pbuf_take_at(outpkt->pbuf, &field16, sizeof(field16), outpkt->write_offset); |
||
909 | if (res != ERR_OK) { |
||
910 | return res; |
||
911 | } |
||
912 | outpkt->write_offset += sizeof(field16); |
||
913 | |||
914 | return ERR_OK; |
||
915 | } |
||
916 | |||
917 | /** |
||
918 | * Write answer to reply packet. |
||
919 | * buf or answer_domain can be null. The rd_length written will be buf_length + |
||
920 | * size of (compressed) domain. Most uses will need either buf or answer_domain, |
||
921 | * special case is SRV that starts with 3 u16 and then a domain name. |
||
922 | * @param reply The outpacket to write to |
||
923 | * @param domain The domain name the answer is for |
||
924 | * @param type The DNS type of the answer (like 'AAAA', 'SRV') |
||
925 | * @param klass The DNS type of the answer (like 'IN') |
||
926 | * @param cache_flush If highest bit in class should be set, to instruct receiver that |
||
927 | * this reply replaces any earlier answer for this domain/type/class |
||
928 | * @param ttl Validity time in seconds to send out for IP address data in DNS replies |
||
929 | * @param buf Pointer to buffer of answer data |
||
930 | * @param buf_length Length of variable data |
||
931 | * @param answer_domain A domain to write after any buffer data as answer |
||
932 | * @return ERR_OK on success, an err_t otherwise |
||
933 | */ |
||
934 | static err_t |
||
935 | mdns_add_answer(struct mdns_outpacket *reply, struct mdns_domain *domain, u16_t type, u16_t klass, u16_t cache_flush, |
||
936 | u32_t ttl, const u8_t *buf, size_t buf_length, struct mdns_domain *answer_domain) |
||
937 | { |
||
938 | u16_t answer_len; |
||
939 | u16_t field16; |
||
940 | u16_t rdlen_offset; |
||
941 | u16_t answer_offset; |
||
942 | u32_t field32; |
||
943 | err_t res; |
||
944 | |||
945 | if (!reply->pbuf) { |
||
946 | /* If no pbuf is active, allocate one */ |
||
947 | reply->pbuf = pbuf_alloc(PBUF_TRANSPORT, OUTPACKET_SIZE, PBUF_RAM); |
||
948 | if (!reply->pbuf) { |
||
949 | return ERR_MEM; |
||
950 | } |
||
951 | reply->write_offset = SIZEOF_DNS_HDR; |
||
952 | } |
||
953 | |||
954 | /* Worst case calculation. Domain strings might be compressed */ |
||
955 | answer_len = domain->length + sizeof(type) + sizeof(klass) + sizeof(ttl) + sizeof(field16)/*rd_length*/; |
||
956 | if (buf) { |
||
957 | answer_len += (u16_t)buf_length; |
||
958 | } |
||
959 | if (answer_domain) { |
||
960 | answer_len += answer_domain->length; |
||
961 | } |
||
962 | if (reply->write_offset + answer_len > reply->pbuf->tot_len) { |
||
963 | /* No space */ |
||
964 | return ERR_MEM; |
||
965 | } |
||
966 | |||
967 | /* Answer starts with same data as question, then more fields */ |
||
968 | mdns_add_question(reply, domain, type, klass, cache_flush); |
||
969 | |||
970 | /* Write TTL */ |
||
971 | field32 = lwip_htonl(ttl); |
||
972 | res = pbuf_take_at(reply->pbuf, &field32, sizeof(field32), reply->write_offset); |
||
973 | if (res != ERR_OK) { |
||
974 | return res; |
||
975 | } |
||
976 | reply->write_offset += sizeof(field32); |
||
977 | |||
978 | /* Store offsets and skip forward to the data */ |
||
979 | rdlen_offset = reply->write_offset; |
||
980 | reply->write_offset += sizeof(field16); |
||
981 | answer_offset = reply->write_offset; |
||
982 | |||
983 | if (buf) { |
||
984 | /* Write static data */ |
||
985 | res = pbuf_take_at(reply->pbuf, buf, (u16_t)buf_length, reply->write_offset); |
||
986 | if (res != ERR_OK) { |
||
987 | return res; |
||
988 | } |
||
989 | reply->write_offset += (u16_t)buf_length; |
||
990 | } |
||
991 | |||
992 | if (answer_domain) { |
||
993 | /* Write name answer (compressed if possible) */ |
||
994 | res = mdns_write_domain(reply, answer_domain); |
||
995 | if (res != ERR_OK) { |
||
996 | return res; |
||
997 | } |
||
998 | } |
||
999 | |||
1000 | /* Write rd_length after when we know the answer size */ |
||
1001 | field16 = lwip_htons(reply->write_offset - answer_offset); |
||
1002 | res = pbuf_take_at(reply->pbuf, &field16, sizeof(field16), rdlen_offset); |
||
1003 | |||
1004 | return res; |
||
1005 | } |
||
1006 | |||
1007 | /** |
||
1008 | * Helper function for mdns_read_question/mdns_read_answer |
||
1009 | * Reads a domain, type and class from the packet |
||
1010 | * @param pkt The MDNS packet to read from. The parse_offset field will be |
||
1011 | * incremented to point to the next unparsed byte. |
||
1012 | * @param info The struct to fill with domain, type and class |
||
1013 | * @return ERR_OK on success, an err_t otherwise |
||
1014 | */ |
||
1015 | static err_t |
||
1016 | mdns_read_rr_info(struct mdns_packet *pkt, struct mdns_rr_info *info) |
||
1017 | { |
||
1018 | u16_t field16, copied; |
||
1019 | pkt->parse_offset = mdns_readname(pkt->pbuf, pkt->parse_offset, &info->domain); |
||
1020 | if (pkt->parse_offset == MDNS_READNAME_ERROR) { |
||
1021 | return ERR_VAL; |
||
1022 | } |
||
1023 | |||
1024 | copied = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), pkt->parse_offset); |
||
1025 | if (copied != sizeof(field16)) { |
||
1026 | return ERR_VAL; |
||
1027 | } |
||
1028 | pkt->parse_offset += copied; |
||
1029 | info->type = lwip_ntohs(field16); |
||
1030 | |||
1031 | copied = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), pkt->parse_offset); |
||
1032 | if (copied != sizeof(field16)) { |
||
1033 | return ERR_VAL; |
||
1034 | } |
||
1035 | pkt->parse_offset += copied; |
||
1036 | info->klass = lwip_ntohs(field16); |
||
1037 | |||
1038 | return ERR_OK; |
||
1039 | } |
||
1040 | |||
1041 | /** |
||
1042 | * Read a question from the packet. |
||
1043 | * All questions have to be read before the answers. |
||
1044 | * @param pkt The MDNS packet to read from. The questions_left field will be decremented |
||
1045 | * and the parse_offset will be updated. |
||
1046 | * @param question The struct to fill with question data |
||
1047 | * @return ERR_OK on success, an err_t otherwise |
||
1048 | */ |
||
1049 | static err_t |
||
1050 | mdns_read_question(struct mdns_packet *pkt, struct mdns_question *question) |
||
1051 | { |
||
1052 | /* Safety check */ |
||
1053 | if (pkt->pbuf->tot_len < pkt->parse_offset) { |
||
1054 | return ERR_VAL; |
||
1055 | } |
||
1056 | |||
1057 | if (pkt->questions_left) { |
||
1058 | err_t res; |
||
1059 | pkt->questions_left--; |
||
1060 | |||
1061 | memset(question, 0, sizeof(struct mdns_question)); |
||
1062 | res = mdns_read_rr_info(pkt, &question->info); |
||
1063 | if (res != ERR_OK) { |
||
1064 | return res; |
||
1065 | } |
||
1066 | |||
1067 | /* Extract unicast flag from class field */ |
||
1068 | question->unicast = question->info.klass & 0x8000; |
||
1069 | question->info.klass &= 0x7FFF; |
||
1070 | |||
1071 | return ERR_OK; |
||
1072 | } |
||
1073 | return ERR_VAL; |
||
1074 | } |
||
1075 | |||
1076 | /** |
||
1077 | * Read an answer from the packet |
||
1078 | * The variable length reply is not copied, its pbuf offset and length is stored instead. |
||
1079 | * @param pkt The MDNS packet to read. The answers_left field will be decremented and |
||
1080 | * the parse_offset will be updated. |
||
1081 | * @param answer The struct to fill with answer data |
||
1082 | * @return ERR_OK on success, an err_t otherwise |
||
1083 | */ |
||
1084 | static err_t |
||
1085 | mdns_read_answer(struct mdns_packet *pkt, struct mdns_answer *answer) |
||
1086 | { |
||
1087 | /* Read questions first */ |
||
1088 | if (pkt->questions_left) { |
||
1089 | return ERR_VAL; |
||
1090 | } |
||
1091 | |||
1092 | /* Safety check */ |
||
1093 | if (pkt->pbuf->tot_len < pkt->parse_offset) { |
||
1094 | return ERR_VAL; |
||
1095 | } |
||
1096 | |||
1097 | if (pkt->answers_left) { |
||
1098 | u16_t copied, field16; |
||
1099 | u32_t ttl; |
||
1100 | err_t res; |
||
1101 | pkt->answers_left--; |
||
1102 | |||
1103 | memset(answer, 0, sizeof(struct mdns_answer)); |
||
1104 | res = mdns_read_rr_info(pkt, &answer->info); |
||
1105 | if (res != ERR_OK) { |
||
1106 | return res; |
||
1107 | } |
||
1108 | |||
1109 | /* Extract cache_flush flag from class field */ |
||
1110 | answer->cache_flush = answer->info.klass & 0x8000; |
||
1111 | answer->info.klass &= 0x7FFF; |
||
1112 | |||
1113 | copied = pbuf_copy_partial(pkt->pbuf, &ttl, sizeof(ttl), pkt->parse_offset); |
||
1114 | if (copied != sizeof(ttl)) { |
||
1115 | return ERR_VAL; |
||
1116 | } |
||
1117 | pkt->parse_offset += copied; |
||
1118 | answer->ttl = lwip_ntohl(ttl); |
||
1119 | |||
1120 | copied = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), pkt->parse_offset); |
||
1121 | if (copied != sizeof(field16)) { |
||
1122 | return ERR_VAL; |
||
1123 | } |
||
1124 | pkt->parse_offset += copied; |
||
1125 | answer->rd_length = lwip_ntohs(field16); |
||
1126 | |||
1127 | answer->rd_offset = pkt->parse_offset; |
||
1128 | pkt->parse_offset += answer->rd_length; |
||
1129 | |||
1130 | return ERR_OK; |
||
1131 | } |
||
1132 | return ERR_VAL; |
||
1133 | } |
||
1134 | |||
1135 | #if LWIP_IPV4 |
||
1136 | /** Write an IPv4 address (A) RR to outpacket */ |
||
1137 | static err_t |
||
1138 | mdns_add_a_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif) |
||
1139 | { |
||
1140 | struct mdns_domain host; |
||
1141 | mdns_build_host_domain(&host, NETIF_TO_HOST(netif)); |
||
1142 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with A record\n")); |
||
1143 | return mdns_add_answer(reply, &host, DNS_RRTYPE_A, DNS_RRCLASS_IN, cache_flush, (NETIF_TO_HOST(netif))->dns_ttl, (const u8_t *) netif_ip4_addr(netif), sizeof(ip4_addr_t), NULL); |
||
1144 | } |
||
1145 | |||
1146 | /** Write a 4.3.2.1.in-addr.arpa -> hostname.local PTR RR to outpacket */ |
||
1147 | static err_t |
||
1148 | mdns_add_hostv4_ptr_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif) |
||
1149 | { |
||
1150 | struct mdns_domain host, revhost; |
||
1151 | mdns_build_host_domain(&host, NETIF_TO_HOST(netif)); |
||
1152 | mdns_build_reverse_v4_domain(&revhost, netif_ip4_addr(netif)); |
||
1153 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with v4 PTR record\n")); |
||
1154 | return mdns_add_answer(reply, &revhost, DNS_RRTYPE_PTR, DNS_RRCLASS_IN, cache_flush, (NETIF_TO_HOST(netif))->dns_ttl, NULL, 0, &host); |
||
1155 | } |
||
1156 | #endif |
||
1157 | |||
1158 | #if LWIP_IPV6 |
||
1159 | /** Write an IPv6 address (AAAA) RR to outpacket */ |
||
1160 | static err_t |
||
1161 | mdns_add_aaaa_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif, int addrindex) |
||
1162 | { |
||
1163 | struct mdns_domain host; |
||
1164 | mdns_build_host_domain(&host, NETIF_TO_HOST(netif)); |
||
1165 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with AAAA record\n")); |
||
1166 | return mdns_add_answer(reply, &host, DNS_RRTYPE_AAAA, DNS_RRCLASS_IN, cache_flush, (NETIF_TO_HOST(netif))->dns_ttl, (const u8_t *) netif_ip6_addr(netif, addrindex), sizeof(ip6_addr_p_t), NULL); |
||
1167 | } |
||
1168 | |||
1169 | /** Write a x.y.z.ip6.arpa -> hostname.local PTR RR to outpacket */ |
||
1170 | static err_t |
||
1171 | mdns_add_hostv6_ptr_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct netif *netif, int addrindex) |
||
1172 | { |
||
1173 | struct mdns_domain host, revhost; |
||
1174 | mdns_build_host_domain(&host, NETIF_TO_HOST(netif)); |
||
1175 | mdns_build_reverse_v6_domain(&revhost, netif_ip6_addr(netif, addrindex)); |
||
1176 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with v6 PTR record\n")); |
||
1177 | return mdns_add_answer(reply, &revhost, DNS_RRTYPE_PTR, DNS_RRCLASS_IN, cache_flush, (NETIF_TO_HOST(netif))->dns_ttl, NULL, 0, &host); |
||
1178 | } |
||
1179 | #endif |
||
1180 | |||
1181 | /** Write an all-services -> servicetype PTR RR to outpacket */ |
||
1182 | static err_t |
||
1183 | mdns_add_servicetype_ptr_answer(struct mdns_outpacket *reply, struct mdns_service *service) |
||
1184 | { |
||
1185 | struct mdns_domain service_type, service_dnssd; |
||
1186 | mdns_build_service_domain(&service_type, service, 0); |
||
1187 | mdns_build_dnssd_domain(&service_dnssd); |
||
1188 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with service type PTR record\n")); |
||
1189 | return mdns_add_answer(reply, &service_dnssd, DNS_RRTYPE_PTR, DNS_RRCLASS_IN, 0, service->dns_ttl, NULL, 0, &service_type); |
||
1190 | } |
||
1191 | |||
1192 | /** Write a servicetype -> servicename PTR RR to outpacket */ |
||
1193 | static err_t |
||
1194 | mdns_add_servicename_ptr_answer(struct mdns_outpacket *reply, struct mdns_service *service) |
||
1195 | { |
||
1196 | struct mdns_domain service_type, service_instance; |
||
1197 | mdns_build_service_domain(&service_type, service, 0); |
||
1198 | mdns_build_service_domain(&service_instance, service, 1); |
||
1199 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with service name PTR record\n")); |
||
1200 | return mdns_add_answer(reply, &service_type, DNS_RRTYPE_PTR, DNS_RRCLASS_IN, 0, service->dns_ttl, NULL, 0, &service_instance); |
||
1201 | } |
||
1202 | |||
1203 | /** Write a SRV RR to outpacket */ |
||
1204 | static err_t |
||
1205 | mdns_add_srv_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct mdns_host *mdns, struct mdns_service *service) |
||
1206 | { |
||
1207 | struct mdns_domain service_instance, srvhost; |
||
1208 | u16_t srvdata[3]; |
||
1209 | mdns_build_service_domain(&service_instance, service, 1); |
||
1210 | mdns_build_host_domain(&srvhost, mdns); |
||
1211 | if (reply->legacy_query) { |
||
1212 | /* RFC 6762 section 18.14: |
||
1213 | * In legacy unicast responses generated to answer legacy queries, |
||
1214 | * name compression MUST NOT be performed on SRV records. |
||
1215 | */ |
||
1216 | srvhost.skip_compression = 1; |
||
1217 | } |
||
1218 | srvdata[0] = lwip_htons(SRV_PRIORITY); |
||
1219 | srvdata[1] = lwip_htons(SRV_WEIGHT); |
||
1220 | srvdata[2] = lwip_htons(service->port); |
||
1221 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with SRV record\n")); |
||
1222 | return mdns_add_answer(reply, &service_instance, DNS_RRTYPE_SRV, DNS_RRCLASS_IN, cache_flush, service->dns_ttl, |
||
1223 | (const u8_t *) &srvdata, sizeof(srvdata), &srvhost); |
||
1224 | } |
||
1225 | |||
1226 | /** Write a TXT RR to outpacket */ |
||
1227 | static err_t |
||
1228 | mdns_add_txt_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct mdns_service *service) |
||
1229 | { |
||
1230 | struct mdns_domain service_instance; |
||
1231 | mdns_build_service_domain(&service_instance, service, 1); |
||
1232 | mdns_prepare_txtdata(service); |
||
1233 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with TXT record\n")); |
||
1234 | return mdns_add_answer(reply, &service_instance, DNS_RRTYPE_TXT, DNS_RRCLASS_IN, cache_flush, service->dns_ttl, |
||
1235 | (u8_t *) &service->txtdata.name, service->txtdata.length, NULL); |
||
1236 | } |
||
1237 | |||
1238 | /** |
||
1239 | * Setup outpacket as a reply to the incoming packet |
||
1240 | */ |
||
1241 | static void |
||
1242 | mdns_init_outpacket(struct mdns_outpacket *out, struct mdns_packet *in) |
||
1243 | { |
||
1244 | memset(out, 0, sizeof(struct mdns_outpacket)); |
||
1245 | out->cache_flush = 1; |
||
1246 | out->netif = in->netif; |
||
1247 | |||
1248 | /* Copy source IP/port to use when responding unicast, or to choose |
||
1249 | * which pcb to use for multicast (IPv4/IPv6) |
||
1250 | */ |
||
1251 | SMEMCPY(&out->dest_addr, &in->source_addr, sizeof(ip_addr_t)); |
||
1252 | out->dest_port = in->source_port; |
||
1253 | |||
1254 | if (in->source_port != LWIP_IANA_PORT_MDNS) { |
||
1255 | out->unicast_reply = 1; |
||
1256 | out->cache_flush = 0; |
||
1257 | if (in->questions == 1) { |
||
1258 | out->legacy_query = 1; |
||
1259 | out->tx_id = in->tx_id; |
||
1260 | } |
||
1261 | } |
||
1262 | |||
1263 | if (in->recv_unicast) { |
||
1264 | out->unicast_reply = 1; |
||
1265 | } |
||
1266 | } |
||
1267 | |||
1268 | /** |
||
1269 | * Send chosen answers as a reply |
||
1270 | * |
||
1271 | * Add all selected answers (first write will allocate pbuf) |
||
1272 | * Add additional answers based on the selected answers |
||
1273 | * Send the packet |
||
1274 | */ |
||
1275 | static void |
||
1276 | mdns_send_outpacket(struct mdns_outpacket *outpkt) |
||
1277 | { |
||
1278 | struct mdns_service *service; |
||
1279 | err_t res; |
||
1280 | int i; |
||
1281 | struct mdns_host *mdns = NETIF_TO_HOST(outpkt->netif); |
||
1282 | |||
1283 | /* Write answers to host questions */ |
||
1284 | #if LWIP_IPV4 |
||
1285 | if (outpkt->host_replies & REPLY_HOST_A) { |
||
1286 | res = mdns_add_a_answer(outpkt, outpkt->cache_flush, outpkt->netif); |
||
1287 | if (res != ERR_OK) { |
||
1288 | goto cleanup; |
||
1289 | } |
||
1290 | outpkt->answers++; |
||
1291 | } |
||
1292 | if (outpkt->host_replies & REPLY_HOST_PTR_V4) { |
||
1293 | res = mdns_add_hostv4_ptr_answer(outpkt, outpkt->cache_flush, outpkt->netif); |
||
1294 | if (res != ERR_OK) { |
||
1295 | goto cleanup; |
||
1296 | } |
||
1297 | outpkt->answers++; |
||
1298 | } |
||
1299 | #endif |
||
1300 | #if LWIP_IPV6 |
||
1301 | if (outpkt->host_replies & REPLY_HOST_AAAA) { |
||
1302 | int addrindex; |
||
1303 | for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; ++addrindex) { |
||
1304 | if (ip6_addr_isvalid(netif_ip6_addr_state(outpkt->netif, addrindex))) { |
||
1305 | res = mdns_add_aaaa_answer(outpkt, outpkt->cache_flush, outpkt->netif, addrindex); |
||
1306 | if (res != ERR_OK) { |
||
1307 | goto cleanup; |
||
1308 | } |
||
1309 | outpkt->answers++; |
||
1310 | } |
||
1311 | } |
||
1312 | } |
||
1313 | if (outpkt->host_replies & REPLY_HOST_PTR_V6) { |
||
1314 | u8_t rev_addrs = outpkt->host_reverse_v6_replies; |
||
1315 | int addrindex = 0; |
||
1316 | while (rev_addrs) { |
||
1317 | if (rev_addrs & 1) { |
||
1318 | res = mdns_add_hostv6_ptr_answer(outpkt, outpkt->cache_flush, outpkt->netif, addrindex); |
||
1319 | if (res != ERR_OK) { |
||
1320 | goto cleanup; |
||
1321 | } |
||
1322 | outpkt->answers++; |
||
1323 | } |
||
1324 | addrindex++; |
||
1325 | rev_addrs >>= 1; |
||
1326 | } |
||
1327 | } |
||
1328 | #endif |
||
1329 | |||
1330 | /* Write answers to service questions */ |
||
1331 | for (i = 0; i < MDNS_MAX_SERVICES; ++i) { |
||
1332 | service = mdns->services[i]; |
||
1333 | if (!service) { |
||
1334 | continue; |
||
1335 | } |
||
1336 | |||
1337 | if (outpkt->serv_replies[i] & REPLY_SERVICE_TYPE_PTR) { |
||
1338 | res = mdns_add_servicetype_ptr_answer(outpkt, service); |
||
1339 | if (res != ERR_OK) { |
||
1340 | goto cleanup; |
||
1341 | } |
||
1342 | outpkt->answers++; |
||
1343 | } |
||
1344 | |||
1345 | if (outpkt->serv_replies[i] & REPLY_SERVICE_NAME_PTR) { |
||
1346 | res = mdns_add_servicename_ptr_answer(outpkt, service); |
||
1347 | if (res != ERR_OK) { |
||
1348 | goto cleanup; |
||
1349 | } |
||
1350 | outpkt->answers++; |
||
1351 | } |
||
1352 | |||
1353 | if (outpkt->serv_replies[i] & REPLY_SERVICE_SRV) { |
||
1354 | res = mdns_add_srv_answer(outpkt, outpkt->cache_flush, mdns, service); |
||
1355 | if (res != ERR_OK) { |
||
1356 | goto cleanup; |
||
1357 | } |
||
1358 | outpkt->answers++; |
||
1359 | } |
||
1360 | |||
1361 | if (outpkt->serv_replies[i] & REPLY_SERVICE_TXT) { |
||
1362 | res = mdns_add_txt_answer(outpkt, outpkt->cache_flush, service); |
||
1363 | if (res != ERR_OK) { |
||
1364 | goto cleanup; |
||
1365 | } |
||
1366 | outpkt->answers++; |
||
1367 | } |
||
1368 | } |
||
1369 | |||
1370 | /* All answers written, add additional RRs */ |
||
1371 | for (i = 0; i < MDNS_MAX_SERVICES; ++i) { |
||
1372 | service = mdns->services[i]; |
||
1373 | if (!service) { |
||
1374 | continue; |
||
1375 | } |
||
1376 | |||
1377 | if (outpkt->serv_replies[i] & REPLY_SERVICE_NAME_PTR) { |
||
1378 | /* Our service instance requested, include SRV & TXT |
||
1379 | * if they are already not requested. */ |
||
1380 | if (!(outpkt->serv_replies[i] & REPLY_SERVICE_SRV)) { |
||
1381 | res = mdns_add_srv_answer(outpkt, outpkt->cache_flush, mdns, service); |
||
1382 | if (res != ERR_OK) { |
||
1383 | goto cleanup; |
||
1384 | } |
||
1385 | outpkt->additional++; |
||
1386 | } |
||
1387 | |||
1388 | if (!(outpkt->serv_replies[i] & REPLY_SERVICE_TXT)) { |
||
1389 | res = mdns_add_txt_answer(outpkt, outpkt->cache_flush, service); |
||
1390 | if (res != ERR_OK) { |
||
1391 | goto cleanup; |
||
1392 | } |
||
1393 | outpkt->additional++; |
||
1394 | } |
||
1395 | } |
||
1396 | |||
1397 | /* If service instance, SRV, record or an IP address is requested, |
||
1398 | * supply all addresses for the host |
||
1399 | */ |
||
1400 | if ((outpkt->serv_replies[i] & (REPLY_SERVICE_NAME_PTR | REPLY_SERVICE_SRV)) || |
||
1401 | (outpkt->host_replies & (REPLY_HOST_A | REPLY_HOST_AAAA))) { |
||
1402 | #if LWIP_IPV6 |
||
1403 | if (!(outpkt->host_replies & REPLY_HOST_AAAA)) { |
||
1404 | int addrindex; |
||
1405 | for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; ++addrindex) { |
||
1406 | if (ip6_addr_isvalid(netif_ip6_addr_state(outpkt->netif, addrindex))) { |
||
1407 | res = mdns_add_aaaa_answer(outpkt, outpkt->cache_flush, outpkt->netif, addrindex); |
||
1408 | if (res != ERR_OK) { |
||
1409 | goto cleanup; |
||
1410 | } |
||
1411 | outpkt->additional++; |
||
1412 | } |
||
1413 | } |
||
1414 | } |
||
1415 | #endif |
||
1416 | #if LWIP_IPV4 |
||
1417 | if (!(outpkt->host_replies & REPLY_HOST_A)) { |
||
1418 | res = mdns_add_a_answer(outpkt, outpkt->cache_flush, outpkt->netif); |
||
1419 | if (res != ERR_OK) { |
||
1420 | goto cleanup; |
||
1421 | } |
||
1422 | outpkt->additional++; |
||
1423 | } |
||
1424 | #endif |
||
1425 | } |
||
1426 | } |
||
1427 | |||
1428 | if (outpkt->pbuf) { |
||
1429 | const ip_addr_t *mcast_destaddr; |
||
1430 | struct dns_hdr hdr; |
||
1431 | |||
1432 | /* Write header */ |
||
1433 | memset(&hdr, 0, sizeof(hdr)); |
||
1434 | hdr.flags1 = DNS_FLAG1_RESPONSE | DNS_FLAG1_AUTHORATIVE; |
||
1435 | hdr.numanswers = lwip_htons(outpkt->answers); |
||
1436 | hdr.numextrarr = lwip_htons(outpkt->additional); |
||
1437 | if (outpkt->legacy_query) { |
||
1438 | hdr.numquestions = lwip_htons(1); |
||
1439 | hdr.id = lwip_htons(outpkt->tx_id); |
||
1440 | } |
||
1441 | pbuf_take(outpkt->pbuf, &hdr, sizeof(hdr)); |
||
1442 | |||
1443 | /* Shrink packet */ |
||
1444 | pbuf_realloc(outpkt->pbuf, outpkt->write_offset); |
||
1445 | |||
1446 | if (IP_IS_V6_VAL(outpkt->dest_addr)) { |
||
1447 | #if LWIP_IPV6 |
||
1448 | mcast_destaddr = &v6group; |
||
1449 | #endif |
||
1450 | } else { |
||
1451 | #if LWIP_IPV4 |
||
1452 | mcast_destaddr = &v4group; |
||
1453 | #endif |
||
1454 | } |
||
1455 | /* Send created packet */ |
||
1456 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Sending packet, len=%d, unicast=%d\n", outpkt->write_offset, outpkt->unicast_reply)); |
||
1457 | if (outpkt->unicast_reply) { |
||
1458 | udp_sendto_if(mdns_pcb, outpkt->pbuf, &outpkt->dest_addr, outpkt->dest_port, outpkt->netif); |
||
1459 | } else { |
||
1460 | udp_sendto_if(mdns_pcb, outpkt->pbuf, mcast_destaddr, LWIP_IANA_PORT_MDNS, outpkt->netif); |
||
1461 | } |
||
1462 | } |
||
1463 | |||
1464 | cleanup: |
||
1465 | if (outpkt->pbuf) { |
||
1466 | pbuf_free(outpkt->pbuf); |
||
1467 | outpkt->pbuf = NULL; |
||
1468 | } |
||
1469 | } |
||
1470 | |||
1471 | /** |
||
1472 | * Send unsolicited answer containing all our known data |
||
1473 | * @param netif The network interface to send on |
||
1474 | * @param destination The target address to send to (usually multicast address) |
||
1475 | */ |
||
1476 | static void |
||
1477 | mdns_announce(struct netif *netif, const ip_addr_t *destination) |
||
1478 | { |
||
1479 | struct mdns_outpacket announce; |
||
1480 | int i; |
||
1481 | struct mdns_host *mdns = NETIF_TO_HOST(netif); |
||
1482 | |||
1483 | memset(&announce, 0, sizeof(announce)); |
||
1484 | announce.netif = netif; |
||
1485 | announce.cache_flush = 1; |
||
1486 | #if LWIP_IPV4 |
||
1487 | if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) { |
||
1488 | announce.host_replies = REPLY_HOST_A | REPLY_HOST_PTR_V4; |
||
1489 | } |
||
1490 | #endif |
||
1491 | #if LWIP_IPV6 |
||
1492 | for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { |
||
1493 | if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) { |
||
1494 | announce.host_replies |= REPLY_HOST_AAAA | REPLY_HOST_PTR_V6; |
||
1495 | announce.host_reverse_v6_replies |= (1 << i); |
||
1496 | } |
||
1497 | } |
||
1498 | #endif |
||
1499 | |||
1500 | for (i = 0; i < MDNS_MAX_SERVICES; i++) { |
||
1501 | struct mdns_service *serv = mdns->services[i]; |
||
1502 | if (serv) { |
||
1503 | announce.serv_replies[i] = REPLY_SERVICE_TYPE_PTR | REPLY_SERVICE_NAME_PTR | |
||
1504 | REPLY_SERVICE_SRV | REPLY_SERVICE_TXT; |
||
1505 | } |
||
1506 | } |
||
1507 | |||
1508 | announce.dest_port = LWIP_IANA_PORT_MDNS; |
||
1509 | SMEMCPY(&announce.dest_addr, destination, sizeof(announce.dest_addr)); |
||
1510 | mdns_send_outpacket(&announce); |
||
1511 | } |
||
1512 | |||
1513 | /** |
||
1514 | * Handle question MDNS packet |
||
1515 | * 1. Parse all questions and set bits what answers to send |
||
1516 | * 2. Clear pending answers if known answers are supplied |
||
1517 | * 3. Put chosen answers in new packet and send as reply |
||
1518 | */ |
||
1519 | static void |
||
1520 | mdns_handle_question(struct mdns_packet *pkt) |
||
1521 | { |
||
1522 | struct mdns_service *service; |
||
1523 | struct mdns_outpacket reply; |
||
1524 | int replies = 0; |
||
1525 | int i; |
||
1526 | err_t res; |
||
1527 | struct mdns_host *mdns = NETIF_TO_HOST(pkt->netif); |
||
1528 | |||
1529 | mdns_init_outpacket(&reply, pkt); |
||
1530 | |||
1531 | while (pkt->questions_left) { |
||
1532 | struct mdns_question q; |
||
1533 | |||
1534 | res = mdns_read_question(pkt, &q); |
||
1535 | if (res != ERR_OK) { |
||
1536 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Failed to parse question, skipping query packet\n")); |
||
1537 | return; |
||
1538 | } |
||
1539 | |||
1540 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Query for domain ")); |
||
1541 | mdns_domain_debug_print(&q.info.domain); |
||
1542 | LWIP_DEBUGF(MDNS_DEBUG, (" type %d class %d\n", q.info.type, q.info.klass)); |
||
1543 | |||
1544 | if (q.unicast) { |
||
1545 | /* Reply unicast if any question is unicast */ |
||
1546 | reply.unicast_reply = 1; |
||
1547 | } |
||
1548 | |||
1549 | reply.host_replies |= check_host(pkt->netif, &q.info, &reply.host_reverse_v6_replies); |
||
1550 | replies |= reply.host_replies; |
||
1551 | |||
1552 | for (i = 0; i < MDNS_MAX_SERVICES; ++i) { |
||
1553 | service = mdns->services[i]; |
||
1554 | if (!service) { |
||
1555 | continue; |
||
1556 | } |
||
1557 | reply.serv_replies[i] |= check_service(service, &q.info); |
||
1558 | replies |= reply.serv_replies[i]; |
||
1559 | } |
||
1560 | |||
1561 | if (replies && reply.legacy_query) { |
||
1562 | /* Add question to reply packet (legacy packet only has 1 question) */ |
||
1563 | res = mdns_add_question(&reply, &q.info.domain, q.info.type, q.info.klass, 0); |
||
1564 | if (res != ERR_OK) { |
||
1565 | goto cleanup; |
||
1566 | } |
||
1567 | } |
||
1568 | } |
||
1569 | |||
1570 | /* Handle known answers */ |
||
1571 | while (pkt->answers_left) { |
||
1572 | struct mdns_answer ans; |
||
1573 | u8_t rev_v6; |
||
1574 | int match; |
||
1575 | |||
1576 | res = mdns_read_answer(pkt, &ans); |
||
1577 | if (res != ERR_OK) { |
||
1578 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Failed to parse answer, skipping query packet\n")); |
||
1579 | goto cleanup; |
||
1580 | } |
||
1581 | |||
1582 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Known answer for domain ")); |
||
1583 | mdns_domain_debug_print(&ans.info.domain); |
||
1584 | LWIP_DEBUGF(MDNS_DEBUG, (" type %d class %d\n", ans.info.type, ans.info.klass)); |
||
1585 | |||
1586 | |||
1587 | if (ans.info.type == DNS_RRTYPE_ANY || ans.info.klass == DNS_RRCLASS_ANY) { |
||
1588 | /* Skip known answers for ANY type & class */ |
||
1589 | continue; |
||
1590 | } |
||
1591 | |||
1592 | rev_v6 = 0; |
||
1593 | match = reply.host_replies & check_host(pkt->netif, &ans.info, &rev_v6); |
||
1594 | if (match && (ans.ttl > (mdns->dns_ttl / 2))) { |
||
1595 | /* The RR in the known answer matches an RR we are planning to send, |
||
1596 | * and the TTL is less than half gone. |
||
1597 | * If the payload matches we should not send that answer. |
||
1598 | */ |
||
1599 | if (ans.info.type == DNS_RRTYPE_PTR) { |
||
1600 | /* Read domain and compare */ |
||
1601 | struct mdns_domain known_ans, my_ans; |
||
1602 | u16_t len; |
||
1603 | len = mdns_readname(pkt->pbuf, ans.rd_offset, &known_ans); |
||
1604 | res = mdns_build_host_domain(&my_ans, mdns); |
||
1605 | if (len != MDNS_READNAME_ERROR && res == ERR_OK && mdns_domain_eq(&known_ans, &my_ans)) { |
||
1606 | #if LWIP_IPV4 |
||
1607 | if (match & REPLY_HOST_PTR_V4) { |
||
1608 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: v4 PTR\n")); |
||
1609 | reply.host_replies &= ~REPLY_HOST_PTR_V4; |
||
1610 | } |
||
1611 | #endif |
||
1612 | #if LWIP_IPV6 |
||
1613 | if (match & REPLY_HOST_PTR_V6) { |
||
1614 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: v6 PTR\n")); |
||
1615 | reply.host_reverse_v6_replies &= ~rev_v6; |
||
1616 | if (reply.host_reverse_v6_replies == 0) { |
||
1617 | reply.host_replies &= ~REPLY_HOST_PTR_V6; |
||
1618 | } |
||
1619 | } |
||
1620 | #endif |
||
1621 | } |
||
1622 | } else if (match & REPLY_HOST_A) { |
||
1623 | #if LWIP_IPV4 |
||
1624 | if (ans.rd_length == sizeof(ip4_addr_t) && |
||
1625 | pbuf_memcmp(pkt->pbuf, ans.rd_offset, netif_ip4_addr(pkt->netif), ans.rd_length) == 0) { |
||
1626 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: A\n")); |
||
1627 | reply.host_replies &= ~REPLY_HOST_A; |
||
1628 | } |
||
1629 | #endif |
||
1630 | } else if (match & REPLY_HOST_AAAA) { |
||
1631 | #if LWIP_IPV6 |
||
1632 | if (ans.rd_length == sizeof(ip6_addr_p_t) && |
||
1633 | /* TODO this clears all AAAA responses if first addr is set as known */ |
||
1634 | pbuf_memcmp(pkt->pbuf, ans.rd_offset, netif_ip6_addr(pkt->netif, 0), ans.rd_length) == 0) { |
||
1635 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: AAAA\n")); |
||
1636 | reply.host_replies &= ~REPLY_HOST_AAAA; |
||
1637 | } |
||
1638 | #endif |
||
1639 | } |
||
1640 | } |
||
1641 | |||
1642 | for (i = 0; i < MDNS_MAX_SERVICES; ++i) { |
||
1643 | service = mdns->services[i]; |
||
1644 | if (!service) { |
||
1645 | continue; |
||
1646 | } |
||
1647 | match = reply.serv_replies[i] & check_service(service, &ans.info); |
||
1648 | if (match && (ans.ttl > (service->dns_ttl / 2))) { |
||
1649 | /* The RR in the known answer matches an RR we are planning to send, |
||
1650 | * and the TTL is less than half gone. |
||
1651 | * If the payload matches we should not send that answer. |
||
1652 | */ |
||
1653 | if (ans.info.type == DNS_RRTYPE_PTR) { |
||
1654 | /* Read domain and compare */ |
||
1655 | struct mdns_domain known_ans, my_ans; |
||
1656 | u16_t len; |
||
1657 | len = mdns_readname(pkt->pbuf, ans.rd_offset, &known_ans); |
||
1658 | if (len != MDNS_READNAME_ERROR) { |
||
1659 | if (match & REPLY_SERVICE_TYPE_PTR) { |
||
1660 | res = mdns_build_service_domain(&my_ans, service, 0); |
||
1661 | if (res == ERR_OK && mdns_domain_eq(&known_ans, &my_ans)) { |
||
1662 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: service type PTR\n")); |
||
1663 | reply.serv_replies[i] &= ~REPLY_SERVICE_TYPE_PTR; |
||
1664 | } |
||
1665 | } |
||
1666 | if (match & REPLY_SERVICE_NAME_PTR) { |
||
1667 | res = mdns_build_service_domain(&my_ans, service, 1); |
||
1668 | if (res == ERR_OK && mdns_domain_eq(&known_ans, &my_ans)) { |
||
1669 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: service name PTR\n")); |
||
1670 | reply.serv_replies[i] &= ~REPLY_SERVICE_NAME_PTR; |
||
1671 | } |
||
1672 | } |
||
1673 | } |
||
1674 | } else if (match & REPLY_SERVICE_SRV) { |
||
1675 | /* Read and compare to my SRV record */ |
||
1676 | u16_t field16, len, read_pos; |
||
1677 | struct mdns_domain known_ans, my_ans; |
||
1678 | read_pos = ans.rd_offset; |
||
1679 | do { |
||
1680 | /* Check priority field */ |
||
1681 | len = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), read_pos); |
||
1682 | if (len != sizeof(field16) || lwip_ntohs(field16) != SRV_PRIORITY) { |
||
1683 | break; |
||
1684 | } |
||
1685 | read_pos += len; |
||
1686 | /* Check weight field */ |
||
1687 | len = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), read_pos); |
||
1688 | if (len != sizeof(field16) || lwip_ntohs(field16) != SRV_WEIGHT) { |
||
1689 | break; |
||
1690 | } |
||
1691 | read_pos += len; |
||
1692 | /* Check port field */ |
||
1693 | len = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), read_pos); |
||
1694 | if (len != sizeof(field16) || lwip_ntohs(field16) != service->port) { |
||
1695 | break; |
||
1696 | } |
||
1697 | read_pos += len; |
||
1698 | /* Check host field */ |
||
1699 | len = mdns_readname(pkt->pbuf, read_pos, &known_ans); |
||
1700 | mdns_build_host_domain(&my_ans, mdns); |
||
1701 | if (len == MDNS_READNAME_ERROR || !mdns_domain_eq(&known_ans, &my_ans)) { |
||
1702 | break; |
||
1703 | } |
||
1704 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: SRV\n")); |
||
1705 | reply.serv_replies[i] &= ~REPLY_SERVICE_SRV; |
||
1706 | } while (0); |
||
1707 | } else if (match & REPLY_SERVICE_TXT) { |
||
1708 | mdns_prepare_txtdata(service); |
||
1709 | if (service->txtdata.length == ans.rd_length && |
||
1710 | pbuf_memcmp(pkt->pbuf, ans.rd_offset, service->txtdata.name, ans.rd_length) == 0) { |
||
1711 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Skipping known answer: TXT\n")); |
||
1712 | reply.serv_replies[i] &= ~REPLY_SERVICE_TXT; |
||
1713 | } |
||
1714 | } |
||
1715 | } |
||
1716 | } |
||
1717 | } |
||
1718 | |||
1719 | mdns_send_outpacket(&reply); |
||
1720 | |||
1721 | cleanup: |
||
1722 | if (reply.pbuf) { |
||
1723 | /* This should only happen if we fail to alloc/write question for legacy query */ |
||
1724 | pbuf_free(reply.pbuf); |
||
1725 | reply.pbuf = NULL; |
||
1726 | } |
||
1727 | } |
||
1728 | |||
1729 | /** |
||
1730 | * Handle response MDNS packet |
||
1731 | * Only prints debug for now. Will need more code to do conflict resolution. |
||
1732 | */ |
||
1733 | static void |
||
1734 | mdns_handle_response(struct mdns_packet *pkt) |
||
1735 | { |
||
1736 | /* Ignore all questions */ |
||
1737 | while (pkt->questions_left) { |
||
1738 | struct mdns_question q; |
||
1739 | err_t res; |
||
1740 | |||
1741 | res = mdns_read_question(pkt, &q); |
||
1742 | if (res != ERR_OK) { |
||
1743 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Failed to parse question, skipping response packet\n")); |
||
1744 | return; |
||
1745 | } |
||
1746 | } |
||
1747 | |||
1748 | while (pkt->answers_left) { |
||
1749 | struct mdns_answer ans; |
||
1750 | err_t res; |
||
1751 | |||
1752 | res = mdns_read_answer(pkt, &ans); |
||
1753 | if (res != ERR_OK) { |
||
1754 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Failed to parse answer, skipping response packet\n")); |
||
1755 | return; |
||
1756 | } |
||
1757 | |||
1758 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Answer for domain ")); |
||
1759 | mdns_domain_debug_print(&ans.info.domain); |
||
1760 | LWIP_DEBUGF(MDNS_DEBUG, (" type %d class %d\n", ans.info.type, ans.info.klass)); |
||
1761 | } |
||
1762 | } |
||
1763 | |||
1764 | /** |
||
1765 | * Receive input function for MDNS packets. |
||
1766 | * Handles both IPv4 and IPv6 UDP pcbs. |
||
1767 | */ |
||
1768 | static void |
||
1769 | mdns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) |
||
1770 | { |
||
1771 | struct dns_hdr hdr; |
||
1772 | struct mdns_packet packet; |
||
1773 | struct netif *recv_netif = ip_current_input_netif(); |
||
1774 | u16_t offset = 0; |
||
1775 | |||
1776 | LWIP_UNUSED_ARG(arg); |
||
1777 | LWIP_UNUSED_ARG(pcb); |
||
1778 | |||
1779 | LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Received IPv%d MDNS packet, len %d\n", IP_IS_V6(addr) ? 6 : 4, p->tot_len)); |
||
1780 | |||
1781 | if (NETIF_TO_HOST(recv_netif) == NULL) { |
||
1782 | /* From netif not configured for MDNS */ |
||
1783 | goto dealloc; |
||
1784 | } |
||
1785 | |||
1786 | if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, offset) < SIZEOF_DNS_HDR) { |
||
1787 | /* Too small */ |
||
1788 | goto dealloc; |
||
1789 | } |
||
1790 | offset += SIZEOF_DNS_HDR; |
||
1791 | |||
1792 | if (DNS_HDR_GET_OPCODE(&hdr)) { |
||
1793 | /* Ignore non-standard queries in multicast packets (RFC 6762, section 18.3) */ |
||
1794 | goto dealloc; |
||
1795 | } |
||
1796 | |||
1797 | memset(&packet, 0, sizeof(packet)); |
||
1798 | SMEMCPY(&packet.source_addr, addr, sizeof(packet.source_addr)); |
||
1799 | packet.source_port = port; |
||
1800 | packet.netif = recv_netif; |
||
1801 | packet.pbuf = p; |
||
1802 | packet.parse_offset = offset; |
||
1803 | packet.tx_id = lwip_ntohs(hdr.id); |
||
1804 | packet.questions = packet.questions_left = lwip_ntohs(hdr.numquestions); |
||
1805 | packet.answers = packet.answers_left = lwip_ntohs(hdr.numanswers) + lwip_ntohs(hdr.numauthrr) + lwip_ntohs(hdr.numextrarr); |
||
1806 | |||
1807 | #if LWIP_IPV6 |
||
1808 | if (IP_IS_V6(ip_current_dest_addr())) { |
||
1809 | if (!ip_addr_cmp(ip_current_dest_addr(), &v6group)) { |
||
1810 | packet.recv_unicast = 1; |
||
1811 | } |
||
1812 | } |
||
1813 | #endif |
||
1814 | #if LWIP_IPV4 |
||
1815 | if (!IP_IS_V6(ip_current_dest_addr())) { |
||
1816 | if (!ip_addr_cmp(ip_current_dest_addr(), &v4group)) { |
||
1817 | packet.recv_unicast = 1; |
||
1818 | } |
||
1819 | } |
||
1820 | #endif |
||
1821 | |||
1822 | if (hdr.flags1 & DNS_FLAG1_RESPONSE) { |
||
1823 | mdns_handle_response(&packet); |
||
1824 | } else { |
||
1825 | mdns_handle_question(&packet); |
||
1826 | } |
||
1827 | |||
1828 | dealloc: |
||
1829 | pbuf_free(p); |
||
1830 | } |
||
1831 | |||
1832 | /** |
||
1833 | * @ingroup mdns |
||
1834 | * Announce IP settings have changed on netif. |
||
1835 | * Call this in your callback registered by netif_set_status_callback(). |
||
1836 | * No need to call this function when LWIP_NETIF_EXT_STATUS_CALLBACK==1, |
||
1837 | * this handled automatically for you. |
||
1838 | * @param netif The network interface where settings have changed. |
||
1839 | */ |
||
1840 | void |
||
1841 | mdns_resp_netif_settings_changed(struct netif *netif) |
||
1842 | { |
||
1843 | LWIP_ERROR("mdns_resp_netif_ip_changed: netif != NULL", (netif != NULL), return); |
||
1844 | |||
1845 | if (NETIF_TO_HOST(netif) == NULL) { |
||
1846 | return; |
||
1847 | } |
||
1848 | |||
1849 | /* Announce on IPv6 and IPv4 */ |
||
1850 | #if LWIP_IPV6 |
||
1851 | mdns_announce(netif, IP6_ADDR_ANY); |
||
1852 | #endif |
||
1853 | #if LWIP_IPV4 |
||
1854 | mdns_announce(netif, IP4_ADDR_ANY); |
||
1855 | #endif |
||
1856 | } |
||
1857 | |||
1858 | #if LWIP_NETIF_EXT_STATUS_CALLBACK |
||
1859 | static void |
||
1860 | mdns_netif_ext_status_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args) |
||
1861 | { |
||
1862 | LWIP_UNUSED_ARG(args); |
||
1863 | |||
1864 | /* MDNS enabled on netif? */ |
||
1865 | if (NETIF_TO_HOST(netif) == NULL) { |
||
1866 | return; |
||
1867 | } |
||
1868 | |||
1869 | switch (reason) { |
||
1870 | case LWIP_NSC_STATUS_CHANGED: |
||
1871 | if (args->status_changed.state != 0) { |
||
1872 | mdns_resp_netif_settings_changed(netif); |
||
1873 | } |
||
1874 | /* TODO: send goodbye message */ |
||
1875 | break; |
||
1876 | case LWIP_NSC_LINK_CHANGED: |
||
1877 | if (args->link_changed.state != 0) { |
||
1878 | mdns_resp_netif_settings_changed(netif); |
||
1879 | } |
||
1880 | break; |
||
1881 | case LWIP_NSC_IPV4_ADDRESS_CHANGED: /* fall through */ |
||
1882 | case LWIP_NSC_IPV4_GATEWAY_CHANGED: /* fall through */ |
||
1883 | case LWIP_NSC_IPV4_NETMASK_CHANGED: /* fall through */ |
||
1884 | case LWIP_NSC_IPV4_SETTINGS_CHANGED: /* fall through */ |
||
1885 | case LWIP_NSC_IPV6_SET: /* fall through */ |
||
1886 | case LWIP_NSC_IPV6_ADDR_STATE_CHANGED: |
||
1887 | mdns_resp_netif_settings_changed(netif); |
||
1888 | break; |
||
1889 | default: |
||
1890 | break; |
||
1891 | } |
||
1892 | } |
||
1893 | #endif |
||
1894 | |||
1895 | /** |
||
1896 | * @ingroup mdns |
||
1897 | * Activate MDNS responder for a network interface and send announce packets. |
||
1898 | * @param netif The network interface to activate. |
||
1899 | * @param hostname Name to use. Queries for <hostname>.local will be answered |
||
1900 | * with the IP addresses of the netif. The hostname will be copied, the |
||
1901 | * given pointer can be on the stack. |
||
1902 | * @param dns_ttl Validity time in seconds to send out for IP address data in DNS replies |
||
1903 | * @return ERR_OK if netif was added, an err_t otherwise |
||
1904 | */ |
||
1905 | err_t |
||
1906 | mdns_resp_add_netif(struct netif *netif, const char *hostname, u32_t dns_ttl) |
||
1907 | { |
||
1908 | err_t res; |
||
1909 | struct mdns_host *mdns; |
||
1910 | |||
1911 | LWIP_ERROR("mdns_resp_add_netif: netif != NULL", (netif != NULL), return ERR_VAL); |
||
1912 | LWIP_ERROR("mdns_resp_add_netif: Hostname too long", (strlen(hostname) <= MDNS_LABEL_MAXLEN), return ERR_VAL); |
||
1913 | |||
1914 | LWIP_ASSERT("mdns_resp_add_netif: Double add", NETIF_TO_HOST(netif) == NULL); |
||
1915 | mdns = (struct mdns_host *) mem_calloc(1, sizeof(struct mdns_host)); |
||
1916 | LWIP_ERROR("mdns_resp_add_netif: Alloc failed", (mdns != NULL), return ERR_MEM); |
||
1917 | |||
1918 | netif_set_client_data(netif, mdns_netif_client_id, mdns); |
||
1919 | |||
1920 | MEMCPY(&mdns->name, hostname, LWIP_MIN(MDNS_LABEL_MAXLEN, strlen(hostname))); |
||
1921 | mdns->dns_ttl = dns_ttl; |
||
1922 | |||
1923 | /* Join multicast groups */ |
||
1924 | #if LWIP_IPV4 |
||
1925 | res = igmp_joingroup_netif(netif, ip_2_ip4(&v4group)); |
||
1926 | if (res != ERR_OK) { |
||
1927 | goto cleanup; |
||
1928 | } |
||
1929 | #endif |
||
1930 | #if LWIP_IPV6 |
||
1931 | res = mld6_joingroup_netif(netif, ip_2_ip6(&v6group)); |
||
1932 | if (res != ERR_OK) { |
||
1933 | goto cleanup; |
||
1934 | } |
||
1935 | #endif |
||
1936 | |||
1937 | mdns_resp_netif_settings_changed(netif); |
||
1938 | return ERR_OK; |
||
1939 | |||
1940 | cleanup: |
||
1941 | mem_free(mdns); |
||
1942 | netif_set_client_data(netif, mdns_netif_client_id, NULL); |
||
1943 | return res; |
||
1944 | } |
||
1945 | |||
1946 | /** |
||
1947 | * @ingroup mdns |
||
1948 | * Stop responding to MDNS queries on this interface, leave multicast groups, |
||
1949 | * and free the helper structure and any of its services. |
||
1950 | * @param netif The network interface to remove. |
||
1951 | * @return ERR_OK if netif was removed, an err_t otherwise |
||
1952 | */ |
||
1953 | err_t |
||
1954 | mdns_resp_remove_netif(struct netif *netif) |
||
1955 | { |
||
1956 | int i; |
||
1957 | struct mdns_host *mdns; |
||
1958 | |||
1959 | LWIP_ASSERT("mdns_resp_remove_netif: Null pointer", netif); |
||
1960 | mdns = NETIF_TO_HOST(netif); |
||
1961 | LWIP_ERROR("mdns_resp_remove_netif: Not an active netif", (mdns != NULL), return ERR_VAL); |
||
1962 | |||
1963 | for (i = 0; i < MDNS_MAX_SERVICES; i++) { |
||
1964 | struct mdns_service *service = mdns->services[i]; |
||
1965 | if (service) { |
||
1966 | mem_free(service); |
||
1967 | } |
||
1968 | } |
||
1969 | |||
1970 | /* Leave multicast groups */ |
||
1971 | #if LWIP_IPV4 |
||
1972 | igmp_leavegroup_netif(netif, ip_2_ip4(&v4group)); |
||
1973 | #endif |
||
1974 | #if LWIP_IPV6 |
||
1975 | mld6_leavegroup_netif(netif, ip_2_ip6(&v6group)); |
||
1976 | #endif |
||
1977 | |||
1978 | mem_free(mdns); |
||
1979 | netif_set_client_data(netif, mdns_netif_client_id, NULL); |
||
1980 | return ERR_OK; |
||
1981 | } |
||
1982 | |||
1983 | /** |
||
1984 | * @ingroup mdns |
||
1985 | * Add a service to the selected network interface. |
||
1986 | * @param netif The network interface to publish this service on |
||
1987 | * @param name The name of the service |
||
1988 | * @param service The service type, like "_http" |
||
1989 | * @param proto The service protocol, DNSSD_PROTO_TCP for TCP ("_tcp") and DNSSD_PROTO_UDP |
||
1990 | * for others ("_udp") |
||
1991 | * @param port The port the service listens to |
||
1992 | * @param dns_ttl Validity time in seconds to send out for service data in DNS replies |
||
1993 | * @param txt_fn Callback to get TXT data. Will be called each time a TXT reply is created to |
||
1994 | * allow dynamic replies. |
||
1995 | * @param txt_data Userdata pointer for txt_fn |
||
1996 | * @return service_id if the service was added to the netif, an err_t otherwise |
||
1997 | */ |
||
1998 | s8_t |
||
1999 | mdns_resp_add_service(struct netif *netif, const char *name, const char *service, enum mdns_sd_proto proto, u16_t port, u32_t dns_ttl, service_get_txt_fn_t txt_fn, void *txt_data) |
||
2000 | { |
||
2001 | s8_t i; |
||
2002 | s8_t slot = -1; |
||
2003 | struct mdns_service *srv; |
||
2004 | struct mdns_host *mdns; |
||
2005 | |||
2006 | LWIP_ASSERT("mdns_resp_add_service: netif != NULL", netif); |
||
2007 | mdns = NETIF_TO_HOST(netif); |
||
2008 | LWIP_ERROR("mdns_resp_add_service: Not an mdns netif", (mdns != NULL), return ERR_VAL); |
||
2009 | |||
2010 | LWIP_ERROR("mdns_resp_add_service: Name too long", (strlen(name) <= MDNS_LABEL_MAXLEN), return ERR_VAL); |
||
2011 | LWIP_ERROR("mdns_resp_add_service: Service too long", (strlen(service) <= MDNS_LABEL_MAXLEN), return ERR_VAL); |
||
2012 | LWIP_ERROR("mdns_resp_add_service: Bad proto (need TCP or UDP)", (proto == DNSSD_PROTO_TCP || proto == DNSSD_PROTO_UDP), return ERR_VAL); |
||
2013 | |||
2014 | for (i = 0; i < MDNS_MAX_SERVICES; i++) { |
||
2015 | if (mdns->services[i] == NULL) { |
||
2016 | slot = i; |
||
2017 | break; |
||
2018 | } |
||
2019 | } |
||
2020 | LWIP_ERROR("mdns_resp_add_service: Service list full (increase MDNS_MAX_SERVICES)", (slot >= 0), return ERR_MEM); |
||
2021 | |||
2022 | srv = (struct mdns_service *)mem_calloc(1, sizeof(struct mdns_service)); |
||
2023 | LWIP_ERROR("mdns_resp_add_service: Alloc failed", (srv != NULL), return ERR_MEM); |
||
2024 | |||
2025 | MEMCPY(&srv->name, name, LWIP_MIN(MDNS_LABEL_MAXLEN, strlen(name))); |
||
2026 | MEMCPY(&srv->service, service, LWIP_MIN(MDNS_LABEL_MAXLEN, strlen(service))); |
||
2027 | srv->txt_fn = txt_fn; |
||
2028 | srv->txt_userdata = txt_data; |
||
2029 | srv->proto = (u16_t)proto; |
||
2030 | srv->port = port; |
||
2031 | srv->dns_ttl = dns_ttl; |
||
2032 | |||
2033 | mdns->services[slot] = srv; |
||
2034 | |||
2035 | /* Announce on IPv6 and IPv4 */ |
||
2036 | #if LWIP_IPV6 |
||
2037 | mdns_announce(netif, IP6_ADDR_ANY); |
||
2038 | #endif |
||
2039 | #if LWIP_IPV4 |
||
2040 | mdns_announce(netif, IP4_ADDR_ANY); |
||
2041 | #endif |
||
2042 | return slot; |
||
2043 | } |
||
2044 | |||
2045 | /** |
||
2046 | * @ingroup mdns |
||
2047 | * Delete a service on the selected network interface. |
||
2048 | * @param netif The network interface on which service should be removed |
||
2049 | * @param slot The service slot number returned by mdns_resp_add_service |
||
2050 | * @return ERR_OK if the service was removed from the netif, an err_t otherwise |
||
2051 | */ |
||
2052 | err_t |
||
2053 | mdns_resp_del_service(struct netif *netif, s8_t slot) |
||
2054 | { |
||
2055 | struct mdns_host *mdns; |
||
2056 | struct mdns_service *srv; |
||
2057 | LWIP_ASSERT("mdns_resp_del_service: netif != NULL", netif); |
||
2058 | mdns = NETIF_TO_HOST(netif); |
||
2059 | LWIP_ERROR("mdns_resp_del_service: Not an mdns netif", (mdns != NULL), return ERR_VAL); |
||
2060 | LWIP_ERROR("mdns_resp_del_service: Invalid Service ID", (slot >= 0) && (slot < MDNS_MAX_SERVICES), return ERR_VAL); |
||
2061 | LWIP_ERROR("mdns_resp_del_service: Invalid Service ID", (mdns->services[slot] != NULL), return ERR_VAL); |
||
2062 | |||
2063 | srv = mdns->services[slot]; |
||
2064 | mdns->services[slot] = NULL; |
||
2065 | mem_free(srv); |
||
2066 | return ERR_OK; |
||
2067 | } |
||
2068 | |||
2069 | /** |
||
2070 | * @ingroup mdns |
||
2071 | * Call this function from inside the service_get_txt_fn_t callback to add text data. |
||
2072 | * Buffer for TXT data is 256 bytes, and each field is prefixed with a length byte. |
||
2073 | * @param service The service provided to the get_txt callback |
||
2074 | * @param txt String to add to the TXT field. |
||
2075 | * @param txt_len Length of string |
||
2076 | * @return ERR_OK if the string was added to the reply, an err_t otherwise |
||
2077 | */ |
||
2078 | err_t |
||
2079 | mdns_resp_add_service_txtitem(struct mdns_service *service, const char *txt, u8_t txt_len) |
||
2080 | { |
||
2081 | LWIP_ASSERT("mdns_resp_add_service_txtitem: service != NULL", service); |
||
2082 | |||
2083 | /* Use a mdns_domain struct to store txt chunks since it is the same encoding */ |
||
2084 | return mdns_domain_add_label(&service->txtdata, txt, txt_len); |
||
2085 | } |
||
2086 | |||
2087 | /** |
||
2088 | * @ingroup mdns |
||
2089 | * Initiate MDNS responder. Will open UDP sockets on port 5353 |
||
2090 | */ |
||
2091 | void |
||
2092 | mdns_resp_init(void) |
||
2093 | { |
||
2094 | err_t res; |
||
2095 | |||
2096 | mdns_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); |
||
2097 | LWIP_ASSERT("Failed to allocate pcb", mdns_pcb != NULL); |
||
2098 | #if LWIP_MULTICAST_TX_OPTIONS |
||
2099 | udp_set_multicast_ttl(mdns_pcb, MDNS_TTL); |
||
2100 | #else |
||
2101 | mdns_pcb->ttl = MDNS_TTL; |
||
2102 | #endif |
||
2103 | res = udp_bind(mdns_pcb, IP_ANY_TYPE, LWIP_IANA_PORT_MDNS); |
||
2104 | LWIP_UNUSED_ARG(res); /* in case of LWIP_NOASSERT */ |
||
2105 | LWIP_ASSERT("Failed to bind pcb", res == ERR_OK); |
||
2106 | udp_recv(mdns_pcb, mdns_recv, NULL); |
||
2107 | |||
2108 | mdns_netif_client_id = netif_alloc_client_data_id(); |
||
2109 | |||
2110 | /* register for netif events when started on first netif */ |
||
2111 | netif_add_ext_callback(&netif_callback, mdns_netif_ext_status_callback); |
||
2112 | } |
||
2113 | |||
2114 | #endif /* LWIP_MDNS_RESPONDER */ |