HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_MDNSImpl.h
Go to the documentation of this file.
1 /* mdns.h - mDNS/DNS-SD library - Public Domain - 2017 Mattias Jansson
2  *
3  * This library provides a cross-platform mDNS and DNS-SD library in C.
4  * The implementation is based on RFC 6762 and RFC 6763.
5  *
6  * The latest source code is always available at
7  *
8  * https://github.com/mjansson/mdns
9  *
10  * This library is put in the public domain; you can redistribute it and/or modify it without any
11  * restrictions.
12  *
13  */
14 
15 #ifndef __NET_MDNSIMPL_H__
16 #define __NET_MDNSIMPL_H__
17 
18 #include <SYS/SYS_String.h>
19 
20 #include <stdint.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <fcntl.h>
26 #ifdef _WIN32
27 #include <Winsock2.h>
28 #include <Ws2tcpip.h>
29 #else
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define MDNS_INVALID_POS ((size_t)-1)
40 
41 #define MDNS_STRING_CONST(s) (s), (sizeof((s)) - 1)
42 #define MDNS_STRING_FORMAT(s) (int)((s).length), s.str
43 
44 #define MDNS_POINTER_OFFSET(p, ofs) ((void*)((char*)(p) + (ptrdiff_t)(ofs)))
45 #define MDNS_POINTER_OFFSET_CONST(p, ofs) ((const void*)((const char*)(p) + (ptrdiff_t)(ofs)))
46 #define MDNS_POINTER_DIFF(a, b) ((size_t)((const char*)(a) - (const char*)(b)))
47 
48 #define MDNS_PORT 5353
49 #define MDNS_UNICAST_RESPONSE 0x8000U
50 #define MDNS_CACHE_FLUSH 0x8000U
51 
53 
54 typedef enum mdns_class mdns_class_t;
55 
56 typedef int (*mdns_record_callback_fn)(int sock, const struct sockaddr* from, size_t addrlen,
57  NET_mDNSEntryType entry, uint16_t query_id, uint16_t rtype,
58  uint16_t rclass, uint32_t ttl, const void* data, size_t size,
59  size_t name_offset, size_t name_length, size_t record_offset,
60  size_t record_length, void* user_data);
61 
66 
67 #ifdef _WIN32
68 typedef int mdns_size_t;
69 #else
70 typedef size_t mdns_size_t;
71 #endif
72 
73 struct mdns_string_t {
74  const char* str;
75  size_t length;
76 };
77 
79  size_t offset;
80  size_t length;
81  int ref;
82 };
83 
85  uint16_t priority;
86  uint16_t weight;
87  uint16_t port;
89 };
90 
94 };
95 
96 struct mdns_header_t {
97  uint16_t query_id;
98  uint16_t flags;
99  uint16_t questions;
100  uint16_t answer_rrs;
101  uint16_t authority_rrs;
102  uint16_t additional_rrs;
103 };
104 
105 // mDNS/DNS-SD public API
106 
107 //! Open and setup a IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
108 // pass in the appropriate socket address in saddr, otherwise pass a null pointer for INADDR_ANY.
109 // To send one-shot discovery requests and queries pass a null pointer or set 0 as port to assign
110 // a random user level ephemeral port. To run discovery service listening for incoming
111 // discoveries and queries, you must set MDNS_PORT as port.
112 static int
113 mdns_socket_open_ipv4(struct sockaddr_in* saddr);
114 
115 //! Setup an already opened IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
116 // pass in the appropriate socket address in saddr, otherwise pass a null pointer for INADDR_ANY.
117 // To send one-shot discovery requests and queries pass a null pointer or set 0 as port to assign
118 // a random user level ephemeral port. To run discovery service listening for incoming
119 // discoveries and queries, you must set MDNS_PORT as port.
120 static int
121 mdns_socket_setup_ipv4(int sock, struct sockaddr_in* saddr);
122 
123 //! Open and setup a IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
124 // pass in the appropriate socket address in saddr, otherwise pass a null pointer for in6addr_any.
125 // To send one-shot discovery requests and queries pass a null pointer or set 0 as port to assign
126 // a random user level ephemeral port. To run discovery service listening for incoming
127 // discoveries and queries, you must set MDNS_PORT as port.
128 static int
129 mdns_socket_open_ipv6(struct sockaddr_in6* saddr);
130 
131 //! Setup an already opened IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
132 // pass in the appropriate socket address in saddr, otherwise pass a null pointer for in6addr_any.
133 // To send one-shot discovery requests and queries pass a null pointer or set 0 as port to assign
134 // a random user level ephemeral port. To run discovery service listening for incoming
135 // discoveries and queries, you must set MDNS_PORT as port.
136 static int
137 mdns_socket_setup_ipv6(int sock, struct sockaddr_in6* saddr);
138 
139 //! Close a socket opened with mdns_socket_open_ipv4 and mdns_socket_open_ipv6.
140 static void
141 mdns_socket_close(int sock);
142 
143 //! Listen for incoming multicast DNS-SD and mDNS query requests. The socket should have been
144 // opened on port MDNS_PORT using one of the mdns open or setup socket functions. Returns the
145 // number of queries parsed.
146 static size_t
147 mdns_socket_listen(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
148  void* user_data);
149 
150 //! Send a multicast DNS-SD reqeuest on the given socket to discover available services. Returns
151 // 0 on success, or <0 if error.
152 static int
153 mdns_discovery_send(int sock);
154 
155 //! Recieve unicast responses to a DNS-SD sent with mdns_discovery_send. Any data will be piped to
156 // the given callback for parsing. Returns the number of responses parsed.
157 static size_t
158 mdns_discovery_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
159  void* user_data);
160 
161 //! Send a unicast DNS-SD answer with a single record to the given address. Returns 0 if success,
162 // or <0 if error.
163 static int
164 mdns_discovery_answer(int sock, const void* address, size_t address_size, void* buffer,
165  size_t capacity, const char* record, size_t length);
166 
167 //! Send a multicast mDNS query on the given socket for the given service name. The supplied buffer
168 // will be used to build the query packet. The query ID can be set to non-zero to filter responses,
169 // however the RFC states that the query ID SHOULD be set to 0 for multicast queries. The query
170 // will request a unicast response if the socket is bound to an ephemeral port, or a multicast
171 // response if the socket is bound to mDNS port 5353.
172 // Returns the used query ID, or <0 if error.
173 static int
174 mdns_query_send(int sock, NET_mDNSRecordType type, const char* name, size_t length, void* buffer,
175  size_t capacity, uint16_t query_id);
176 
177 //! Receive unicast responses to a mDNS query sent with mdns_discovery_recv, optionally filtering
178 // out any responses not matching the given query ID. Set the query ID to 0 to parse
179 // all responses, even if it is not matching the query ID set in a specific query. Any data will
180 // be piped to the given callback for parsing. Returns the number of responses parsed.
181 static size_t
182 mdns_query_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
183  void* user_data, int query_id);
184 
185 //! Parse a reply to a mDNS query that was already received from the given
186 //! address. This is the parsing half of mdns_query_recv.
187 static size_t mdns_query_parse(
188  int sock,
189  const struct sockaddr* from,
190  size_t addrlen,
191  const void* buffer,
192  size_t size,
193  mdns_record_callback_fn callback,
194  void* user_data,
195  int query_id);
196 
197 //! Send a unicast or multicast mDNS query answer with a single record to the given address. The
198 // answer will be sent multicast if address size is 0, otherwise it will be sent unicast to the
199 // given address. Use the top bit of the query class field (MDNS_UNICAST_RESPONSE) to determine
200 // if the answer should be sent unicast (bit set) or multicast (bit not set).
201 // Returns 0 if success, or <0 if error.
202 static int
203 mdns_query_answer(int sock, const void* address, size_t address_size, void* buffer, size_t capacity,
204  uint16_t query_id, const char* service, size_t service_length,
205  const char* hostname, size_t hostname_length, uint32_t ipv4, const uint8_t* ipv6,
206  uint16_t port, const char* txt, size_t txt_length);
207 
208 // Internal functions
209 
210 static mdns_string_t
211 mdns_string_extract(const void* buffer, size_t size, size_t* offset, char* str, size_t capacity);
212 
213 static int
214 mdns_string_skip(const void* buffer, size_t size, size_t* offset);
215 
216 static int
217 mdns_string_equal(const void* buffer_lhs, size_t size_lhs, size_t* ofs_lhs, const void* buffer_rhs,
218  size_t size_rhs, size_t* ofs_rhs);
219 
220 static void*
221 mdns_string_make(void* data, size_t capacity, const char* name, size_t length);
222 
223 static void*
224 mdns_string_make_ref(void* data, size_t capacity, size_t ref_offset);
225 
226 static void*
227 mdns_string_make_with_ref(void* data, size_t capacity, const char* name, size_t length,
228  size_t ref_offset);
229 
230 static mdns_string_t
231 mdns_record_parse_ptr(const void* buffer, size_t size, size_t offset, size_t length,
232  char* strbuffer, size_t capacity);
233 
234 static mdns_record_srv_t
235 mdns_record_parse_srv(const void* buffer, size_t size, size_t offset, size_t length,
236  char* strbuffer, size_t capacity);
237 
238 static struct sockaddr_in*
239 mdns_record_parse_a(const void* buffer, size_t size, size_t offset, size_t length,
240  struct sockaddr_in* addr);
241 
242 static struct sockaddr_in6*
243 mdns_record_parse_aaaa(const void* buffer, size_t size, size_t offset, size_t length,
244  struct sockaddr_in6* addr);
245 
246 static size_t
247 mdns_record_parse_txt(const void* buffer, size_t size, size_t offset, size_t length,
248  mdns_record_txt_t* records, size_t capacity);
249 
250 // Implementations
251 
252 static int
253 mdns_socket_open_ipv4(struct sockaddr_in* saddr) {
254  int sock = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
255  if (sock < 0)
256  return -1;
257  if (mdns_socket_setup_ipv4(sock, saddr)) {
258  mdns_socket_close(sock);
259  return -1;
260  }
261  return sock;
262 }
263 
264 static int
265 mdns_socket_setup_ipv4(int sock, struct sockaddr_in* saddr) {
266  unsigned char ttl = 1;
267  unsigned char loopback = 1;
268  unsigned int reuseaddr = 1;
269  struct ip_mreq req;
270 
271  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(reuseaddr));
272 #ifdef SO_REUSEPORT
273  setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuseaddr, sizeof(reuseaddr));
274 #endif
275  setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&ttl, sizeof(ttl));
276  setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&loopback, sizeof(loopback));
277 
278  memset(&req, 0, sizeof(req));
279  req.imr_multiaddr.s_addr = htonl((((uint32_t)224U) << 24U) | ((uint32_t)251U));
280  if (saddr)
281  req.imr_interface = saddr->sin_addr;
282  if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&req, sizeof(req)))
283  return -1;
284 
285  struct sockaddr_in sock_addr;
286  if (!saddr) {
287  saddr = &sock_addr;
288  memset(saddr, 0, sizeof(struct sockaddr_in));
289  saddr->sin_family = AF_INET;
290  saddr->sin_addr.s_addr = INADDR_ANY;
291 #ifdef __APPLE__
292  saddr->sin_len = sizeof(struct sockaddr_in);
293 #endif
294  } else {
295  setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&saddr->sin_addr,
296  sizeof(saddr->sin_addr));
297 #ifndef _WIN32
298  saddr->sin_addr.s_addr = INADDR_ANY;
299 #endif
300  }
301 
302  if (bind(sock, (struct sockaddr*)saddr, sizeof(struct sockaddr_in)))
303  return -1;
304 
305 #ifdef _WIN32
306  unsigned long param = 1;
307  ioctlsocket(sock, FIONBIO, &param);
308 #else
309  const int flags = fcntl(sock, F_GETFL, 0);
310  fcntl(sock, F_SETFL, flags | O_NONBLOCK);
311 #endif
312 
313  return 0;
314 }
315 
316 static int
317 mdns_socket_open_ipv6(struct sockaddr_in6* saddr) {
318  int sock = (int)socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
319  if (sock < 0)
320  return -1;
321  if (mdns_socket_setup_ipv6(sock, saddr)) {
322  mdns_socket_close(sock);
323  return -1;
324  }
325  return sock;
326 }
327 
328 static int
329 mdns_socket_setup_ipv6(int sock, struct sockaddr_in6* saddr) {
330  int hops = 1;
331  unsigned int loopback = 1;
332  unsigned int reuseaddr = 1;
333  struct ipv6_mreq req;
334 
335  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(reuseaddr));
336 #ifdef SO_REUSEPORT
337  setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuseaddr, sizeof(reuseaddr));
338 #endif
339  setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (const char*)&hops, sizeof(hops));
340  setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const char*)&loopback, sizeof(loopback));
341 
342  memset(&req, 0, sizeof(req));
343  req.ipv6mr_multiaddr.s6_addr[0] = 0xFF;
344  req.ipv6mr_multiaddr.s6_addr[1] = 0x02;
345  req.ipv6mr_multiaddr.s6_addr[15] = 0xFB;
346  if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char*)&req, sizeof(req)))
347  return -1;
348 
349  struct sockaddr_in6 sock_addr;
350  if (!saddr) {
351  saddr = &sock_addr;
352  memset(saddr, 0, sizeof(struct sockaddr_in6));
353  saddr->sin6_family = AF_INET6;
354  saddr->sin6_addr = in6addr_any;
355 #ifdef __APPLE__
356  saddr->sin6_len = sizeof(struct sockaddr_in6);
357 #endif
358  } else {
359  unsigned int ifindex = 0;
360  setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (const char*)&ifindex, sizeof(ifindex));
361 #ifndef _WIN32
362  saddr->sin6_addr = in6addr_any;
363 #endif
364  }
365 
366  if (bind(sock, (struct sockaddr*)saddr, sizeof(struct sockaddr_in6)))
367  return -1;
368 
369 #ifdef _WIN32
370  unsigned long param = 1;
371  ioctlsocket(sock, FIONBIO, &param);
372 #else
373  const int flags = fcntl(sock, F_GETFL, 0);
374  fcntl(sock, F_SETFL, flags | O_NONBLOCK);
375 #endif
376 
377  return 0;
378 }
379 
380 static void
381 mdns_socket_close(int sock) {
382 #ifdef _WIN32
383  closesocket(sock);
384 #else
385  close(sock);
386 #endif
387 }
388 
389 static int
390 mdns_is_string_ref(uint8_t val) {
391  return (0xC0 == (val & 0xC0));
392 }
393 
394 static mdns_string_pair_t
395 mdns_get_next_substring(const void* rawdata, size_t size, size_t offset) {
396  const uint8_t* buffer = (const uint8_t*)rawdata;
397  mdns_string_pair_t pair = {MDNS_INVALID_POS, 0, 0};
398  if (!buffer[offset]) {
399  pair.offset = offset;
400  return pair;
401  }
402  if (mdns_is_string_ref(buffer[offset])) {
403  if (size < offset + 2)
404  return pair;
405 
406  offset = 0x3fff & ntohs(*(uint16_t*)MDNS_POINTER_OFFSET(buffer, offset));
407  if (offset >= size)
408  return pair;
409 
410  pair.ref = 1;
411  }
412 
413  size_t length = (size_t)buffer[offset++];
414  if (size < offset + length)
415  return pair;
416 
417  pair.offset = offset;
418  pair.length = length;
419 
420  return pair;
421 }
422 
423 static int
424 mdns_string_skip(const void* buffer, size_t size, size_t* offset) {
425  size_t cur = *offset;
426  mdns_string_pair_t substr;
427  do {
428  substr = mdns_get_next_substring(buffer, size, cur);
429  if (substr.offset == MDNS_INVALID_POS)
430  return 0;
431  if (substr.ref) {
432  *offset = cur + 2;
433  return 1;
434  }
435  cur = substr.offset + substr.length;
436  } while (substr.length);
437 
438  *offset = cur + 1;
439  return 1;
440 }
441 
442 static int
443 mdns_string_equal(const void* buffer_lhs, size_t size_lhs, size_t* ofs_lhs, const void* buffer_rhs,
444  size_t size_rhs, size_t* ofs_rhs) {
445  size_t lhs_cur = *ofs_lhs;
446  size_t rhs_cur = *ofs_rhs;
447  size_t lhs_end = MDNS_INVALID_POS;
448  size_t rhs_end = MDNS_INVALID_POS;
449  mdns_string_pair_t lhs_substr;
450  mdns_string_pair_t rhs_substr;
451  do {
452  lhs_substr = mdns_get_next_substring(buffer_lhs, size_lhs, lhs_cur);
453  rhs_substr = mdns_get_next_substring(buffer_rhs, size_rhs, rhs_cur);
454  if ((lhs_substr.offset == MDNS_INVALID_POS) || (rhs_substr.offset == MDNS_INVALID_POS))
455  return 0;
456  if (lhs_substr.length != rhs_substr.length)
457  return 0;
458  if (SYSstrncasecmp((const char*)buffer_rhs + rhs_substr.offset,
459  (const char*)buffer_lhs + lhs_substr.offset, rhs_substr.length))
460  return 0;
461  if (lhs_substr.ref && (lhs_end == MDNS_INVALID_POS))
462  lhs_end = lhs_cur + 2;
463  if (rhs_substr.ref && (rhs_end == MDNS_INVALID_POS))
464  rhs_end = rhs_cur + 2;
465  lhs_cur = lhs_substr.offset + lhs_substr.length;
466  rhs_cur = rhs_substr.offset + rhs_substr.length;
467  } while (lhs_substr.length);
468 
469  if (lhs_end == MDNS_INVALID_POS)
470  lhs_end = lhs_cur + 1;
471  *ofs_lhs = lhs_end;
472 
473  if (rhs_end == MDNS_INVALID_POS)
474  rhs_end = rhs_cur + 1;
475  *ofs_rhs = rhs_end;
476 
477  return 1;
478 }
479 
480 static mdns_string_t
481 mdns_string_extract(const void* buffer, size_t size, size_t* offset, char* str, size_t capacity) {
482  size_t cur = *offset;
483  size_t end = MDNS_INVALID_POS;
484  mdns_string_pair_t substr;
486  result.str = str;
487  result.length = 0;
488  char* dst = str;
489  size_t remain = capacity;
490  do {
491  substr = mdns_get_next_substring(buffer, size, cur);
492  if (substr.offset == MDNS_INVALID_POS)
493  return result;
494  if (substr.ref && (end == MDNS_INVALID_POS))
495  end = cur + 2;
496  if (substr.length) {
497  size_t to_copy = (substr.length < remain) ? substr.length : remain;
498  memcpy(dst, (const char*)buffer + substr.offset, to_copy);
499  dst += to_copy;
500  remain -= to_copy;
501  if (remain) {
502  *dst++ = '.';
503  --remain;
504  }
505  }
506  cur = substr.offset + substr.length;
507  } while (substr.length);
508 
509  if (end == MDNS_INVALID_POS)
510  end = cur + 1;
511  *offset = end;
512 
513  result.length = capacity - remain;
514  return result;
515 }
516 
517 static size_t
518 mdns_string_find(const char* str, size_t length, char c, size_t offset) {
519  const void* found;
520  if (offset >= length)
521  return MDNS_INVALID_POS;
522  found = memchr(str + offset, c, length - offset);
523  if (found)
524  return (size_t)((const char*)found - str);
525  return MDNS_INVALID_POS;
526 }
527 
528 static void*
529 mdns_string_make(void* data, size_t capacity, const char* name, size_t length) {
530  size_t pos = 0;
531  size_t last_pos = 0;
532  size_t remain = capacity;
533  unsigned char* dest = (unsigned char*)data;
534  while ((last_pos < length) &&
535  ((pos = mdns_string_find(name, length, '.', last_pos)) != MDNS_INVALID_POS)) {
536  size_t sublength = pos - last_pos;
537  if (sublength < remain) {
538  *dest = (unsigned char)sublength;
539  memcpy(dest + 1, name + last_pos, sublength);
540  dest += sublength + 1;
541  remain -= sublength + 1;
542  } else {
543  return 0;
544  }
545  last_pos = pos + 1;
546  }
547  if (last_pos < length) {
548  size_t sublength = length - last_pos;
549  if (sublength < remain) {
550  *dest = (unsigned char)sublength;
551  memcpy(dest + 1, name + last_pos, sublength);
552  dest += sublength + 1;
553  remain -= sublength + 1;
554  } else {
555  return 0;
556  }
557  }
558  if (!remain)
559  return 0;
560  *dest++ = 0;
561  return dest;
562 }
563 
564 static void*
565 mdns_string_make_ref(void* data, size_t capacity, size_t ref_offset) {
566  if (capacity < 2)
567  return 0;
568  uint16_t* udata = (uint16_t*)data;
569  *udata++ = htons(0xC000 | (uint16_t)ref_offset);
570  return udata;
571 }
572 
573 static void*
574 mdns_string_make_with_ref(void* data, size_t capacity, const char* name, size_t length,
575  size_t ref_offset) {
576  void* remaindata = mdns_string_make(data, capacity, name, length);
577  capacity -= MDNS_POINTER_DIFF(remaindata, data);
578  if (!data || !capacity)
579  return 0;
580  return mdns_string_make_ref(MDNS_POINTER_OFFSET(remaindata, -1), capacity + 1, ref_offset);
581 }
582 
583 static size_t
584 mdns_records_parse(int sock, const struct sockaddr* from, size_t addrlen, const void* buffer,
585  size_t size, size_t* offset, NET_mDNSEntryType type, uint16_t query_id,
586  size_t records, mdns_record_callback_fn callback, void* user_data) {
587  size_t parsed = 0;
588  int do_callback = (callback ? 1 : 0);
589  for (size_t i = 0; i < records; ++i) {
590  size_t name_offset = *offset;
591  mdns_string_skip(buffer, size, offset);
592  size_t name_length = (*offset) - name_offset;
593  const uint16_t* data = (const uint16_t*)((const char*)buffer + (*offset));
594 
595  uint16_t rtype = ntohs(*data++);
596  uint16_t rclass = ntohs(*data++);
597  uint32_t ttl = ntohl(*(const uint32_t*)(const void*)data);
598  data += 2;
599  uint16_t length = ntohs(*data++);
600 
601  *offset += 10;
602 
603  if (do_callback) {
604  ++parsed;
605  if (callback(sock, from, addrlen, type, query_id, rtype, rclass, ttl, buffer, size,
606  name_offset, name_length, *offset, length, user_data))
607  do_callback = 0;
608  }
609 
610  *offset += length;
611  }
612  return parsed;
613 }
614 
615 static int
616 mdns_unicast_send(int sock, const void* address, size_t address_size, const void* buffer,
617  size_t size) {
618  if (sendto(sock, (const char*)buffer, (mdns_size_t)size, 0, (const struct sockaddr*)address,
619  (socklen_t)address_size) < 0)
620  return -1;
621  return 0;
622 }
623 
624 static int
625 mdns_multicast_send(int sock, const void* buffer, size_t size) {
626  struct sockaddr_storage addr_storage;
627  struct sockaddr_in addr;
628  struct sockaddr_in6 addr6;
629  struct sockaddr* saddr = (struct sockaddr*)&addr_storage;
630  socklen_t saddrlen = sizeof(struct sockaddr_storage);
631  if (getsockname(sock, saddr, &saddrlen))
632  return -1;
633  if (saddr->sa_family == AF_INET6) {
634  memset(&addr6, 0, sizeof(addr6));
635  addr6.sin6_family = AF_INET6;
636 #ifdef __APPLE__
637  addr6.sin6_len = sizeof(addr6);
638 #endif
639  addr6.sin6_addr.s6_addr[0] = 0xFF;
640  addr6.sin6_addr.s6_addr[1] = 0x02;
641  addr6.sin6_addr.s6_addr[15] = 0xFB;
642  addr6.sin6_port = htons((unsigned short)MDNS_PORT);
643  saddr = (struct sockaddr*)&addr6;
644  saddrlen = sizeof(addr6);
645  } else {
646  memset(&addr, 0, sizeof(addr));
647  addr.sin_family = AF_INET;
648 #ifdef __APPLE__
649  addr.sin_len = sizeof(addr);
650 #endif
651  addr.sin_addr.s_addr = htonl((((uint32_t)224U) << 24U) | ((uint32_t)251U));
652  addr.sin_port = htons((unsigned short)MDNS_PORT);
653  saddr = (struct sockaddr*)&addr;
654  saddrlen = sizeof(addr);
655  }
656 
657  if (sendto(sock, (const char*)buffer, (mdns_size_t)size, 0, saddr, saddrlen) < 0)
658  return -1;
659  return 0;
660 }
661 
662 static const uint8_t mdns_services_query[] = {
663  // Query ID
664  0x00, 0x00,
665  // Flags
666  0x00, 0x00,
667  // 1 question
668  0x00, 0x01,
669  // No answer, authority or additional RRs
670  0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
671  // _services._dns-sd._udp.local.
672  0x09, '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', 0x07, '_', 'd', 'n', 's', '-', 's', 'd',
673  0x04, '_', 'u', 'd', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00,
674  // PTR record
675  0x00, MDNS_RECORDTYPE_PTR,
676  // QU (unicast response) and class IN
677  0x80, MDNS_CLASS_IN};
678 
679 static int
680 mdns_discovery_send(int sock) {
681  return mdns_multicast_send(sock, mdns_services_query, sizeof(mdns_services_query));
682 }
683 
684 static size_t
685 mdns_discovery_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
686  void* user_data) {
687  struct sockaddr_in6 addr;
688  struct sockaddr* saddr = (struct sockaddr*)&addr;
689  socklen_t addrlen = sizeof(addr);
690  memset(&addr, 0, sizeof(addr));
691 #ifdef __APPLE__
692  saddr->sa_len = sizeof(addr);
693 #endif
694  int ret = recvfrom(sock, (char*)buffer, (mdns_size_t)capacity, 0, saddr, &addrlen);
695  if (ret <= 0)
696  return 0;
697 
698  size_t data_size = (size_t)ret;
699  size_t records = 0;
700  uint16_t* data = (uint16_t*)buffer;
701 
702  uint16_t query_id = ntohs(*data++);
703  uint16_t flags = ntohs(*data++);
704  uint16_t questions = ntohs(*data++);
705  uint16_t answer_rrs = ntohs(*data++);
706  uint16_t authority_rrs = ntohs(*data++);
707  uint16_t additional_rrs = ntohs(*data++);
708 
709  // According to RFC 6762 the query ID MUST match the sent query ID (which is 0 in our case)
710  if (query_id || (flags != 0x8400))
711  return 0; // Not a reply to our question
712 
713  // It seems some implementations do not fill the correct questions field,
714  // so ignore this check for now and only validate answer string
715  /*
716  if (questions != 1)
717  return 0;
718  */
719 
720  int i;
721  for (i = 0; i < questions; ++i) {
722  size_t ofs = (size_t)((char*)data - (char*)buffer);
723  size_t verify_ofs = 12;
724  // Verify it's our question, _services._dns-sd._udp.local.
725  if (!mdns_string_equal(buffer, data_size, &ofs, mdns_services_query,
726  sizeof(mdns_services_query), &verify_ofs))
727  return 0;
728  data = (uint16_t*)((char*)buffer + ofs);
729 
730  uint16_t rtype = ntohs(*data++);
731  uint16_t rclass = ntohs(*data++);
732 
733  // Make sure we get a reply based on our PTR question for class IN
734  if ((rtype != MDNS_RECORDTYPE_PTR) || ((rclass & 0x7FFF) != MDNS_CLASS_IN))
735  return 0;
736  }
737 
738  int do_callback = 1;
739  for (i = 0; i < answer_rrs; ++i) {
740  size_t ofs = (size_t)((char*)data - (char*)buffer);
741  size_t verify_ofs = 12;
742  // Verify it's an answer to our question, _services._dns-sd._udp.local.
743  size_t name_offset = ofs;
744  int is_answer = mdns_string_equal(buffer, data_size, &ofs, mdns_services_query,
745  sizeof(mdns_services_query), &verify_ofs);
746  size_t name_length = ofs - name_offset;
747  data = (uint16_t*)((char*)buffer + ofs);
748 
749  uint16_t rtype = ntohs(*data++);
750  uint16_t rclass = ntohs(*data++);
751  uint32_t ttl = ntohl(*(uint32_t*)(void*)data);
752  data += 2;
753  uint16_t length = ntohs(*data++);
754  if (length >= (data_size - ofs))
755  return 0;
756 
757  if (is_answer && do_callback) {
758  ++records;
759  ofs = (size_t)((char*)data - (char*)buffer);
760  if (callback(sock, saddr, addrlen, MDNS_ENTRYTYPE_ANSWER, query_id, rtype, rclass, ttl,
761  buffer, data_size, name_offset, name_length, ofs, length, user_data))
762  do_callback = 0;
763  }
764  data = (uint16_t*)((char*)data + length);
765  }
766 
767  size_t offset = (size_t)((char*)data - (char*)buffer);
768  records +=
769  mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset,
770  MDNS_ENTRYTYPE_AUTHORITY, query_id, authority_rrs, callback, user_data);
771  records += mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset,
772  MDNS_ENTRYTYPE_ADDITIONAL, query_id, additional_rrs, callback,
773  user_data);
774 
775  return records;
776 }
777 
778 static size_t
779 mdns_socket_listen(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
780  void* user_data) {
781  struct sockaddr_in6 addr;
782  struct sockaddr* saddr = (struct sockaddr*)&addr;
783  socklen_t addrlen = sizeof(addr);
784  memset(&addr, 0, sizeof(addr));
785 #ifdef __APPLE__
786  saddr->sa_len = sizeof(addr);
787 #endif
788  int ret = recvfrom(sock, (char*)buffer, (mdns_size_t)capacity, 0, saddr, &addrlen);
789  if (ret <= 0)
790  return 0;
791 
792  size_t data_size = (size_t)ret;
793  uint16_t* data = (uint16_t*)buffer;
794 
795  uint16_t query_id = ntohs(*data++);
796  uint16_t flags = ntohs(*data++);
797  uint16_t questions = ntohs(*data++);
798  /*
799  This data is unused at the moment, skip
800  uint16_t answer_rrs = ntohs(*data++);
801  uint16_t authority_rrs = ntohs(*data++);
802  uint16_t additional_rrs = ntohs(*data++);
803  */
804  data += 3;
805 
806  size_t parsed = 0;
807  for (int iquestion = 0; iquestion < questions; ++iquestion) {
808  size_t question_offset = (size_t)((char*)data - (char*)buffer);
809  size_t offset = question_offset;
810  size_t verify_ofs = 12;
811  if (mdns_string_equal(buffer, data_size, &offset, mdns_services_query,
812  sizeof(mdns_services_query), &verify_ofs)) {
813  if (flags || (questions != 1))
814  return 0;
815  } else {
816  offset = question_offset;
817  if (!mdns_string_skip(buffer, data_size, &offset))
818  break;
819  }
820  size_t length = offset - question_offset;
821  data = (uint16_t*)((char*)buffer + offset);
822 
823  uint16_t rtype = ntohs(*data++);
824  uint16_t rclass = ntohs(*data++);
825 
826  // Make sure we get a question of class IN
827  if ((rclass & 0x7FFF) != MDNS_CLASS_IN)
828  return 0;
829 
830  if (callback)
831  callback(sock, saddr, addrlen, MDNS_ENTRYTYPE_QUESTION, query_id, rtype, rclass, 0,
832  buffer, data_size, question_offset, length, question_offset, length,
833  user_data);
834 
835  ++parsed;
836  }
837 
838  return parsed;
839 }
840 
841 static int
842 mdns_discovery_answer(int sock, const void* address, size_t address_size, void* buffer,
843  size_t capacity, const char* record, size_t length) {
844  if (capacity < (sizeof(mdns_services_query) + 32 + length))
845  return -1;
846 
847  uint16_t* data = (uint16_t*)buffer;
848  // Basic reply structure
849  memcpy(data, mdns_services_query, sizeof(mdns_services_query));
850  // Flags
851  uint16_t* flags = data + 1;
852  *flags = htons(0x8400U);
853  // One answer
854  uint16_t* answers = data + 3;
855  *answers = htons(1);
856 
857  // Fill in answer PTR record
858  data = (uint16_t*)((char*)buffer + sizeof(mdns_services_query));
859  // Reference _services._dns-sd._udp.local. string in question
860  *data++ = htons(0xC000U | 12U);
861  // Type
862  *data++ = htons(MDNS_RECORDTYPE_PTR);
863  // Rclass
864  *data++ = htons(MDNS_CLASS_IN);
865  // TTL
866  *(uint32_t*)data = htonl(10);
867  data += 2;
868  // Record string length
869  uint16_t* record_length = data++;
870  uint8_t* record_data = (uint8_t*)data;
871  size_t remain = capacity - (sizeof(mdns_services_query) + 10);
872  record_data = (uint8_t*)mdns_string_make(record_data, remain, record, length);
873  *record_length = htons((uint16_t)(record_data - (uint8_t*)data));
874  *record_data++ = 0;
875 
876  ptrdiff_t tosend = (char*)record_data - (char*)buffer;
877  return mdns_unicast_send(sock, address, address_size, buffer, (size_t)tosend);
878 }
879 
880 static int
881 mdns_query_send(int sock, NET_mDNSRecordType type, const char* name, size_t length, void* buffer,
882  size_t capacity, uint16_t query_id) {
883  if (capacity < (17 + length))
884  return -1;
885 
886  uint16_t rclass = MDNS_CLASS_IN | MDNS_UNICAST_RESPONSE;
887 
888  struct sockaddr_storage addr_storage;
889  struct sockaddr* saddr = (struct sockaddr*)&addr_storage;
890  socklen_t saddrlen = sizeof(addr_storage);
891  if (getsockname(sock, saddr, &saddrlen) == 0) {
892  if ((saddr->sa_family == AF_INET) &&
893  (ntohs(((struct sockaddr_in*)saddr)->sin_port) == MDNS_PORT))
894  rclass &= ~MDNS_UNICAST_RESPONSE;
895  else if ((saddr->sa_family == AF_INET6) &&
896  (ntohs(((struct sockaddr_in6*)saddr)->sin6_port) == MDNS_PORT))
897  rclass &= ~MDNS_UNICAST_RESPONSE;
898  }
899 
900  uint16_t* data = (uint16_t*)buffer;
901  // Query ID
902  *data++ = htons(query_id);
903  // Flags
904  *data++ = 0;
905  // Questions
906  *data++ = htons(1);
907  // No answer, authority or additional RRs
908  *data++ = 0;
909  *data++ = 0;
910  *data++ = 0;
911  // Fill in question
912  // Name string
913  data = (uint16_t*)mdns_string_make(data, capacity - 17, name, length);
914  if (!data)
915  return -1;
916  // Record type
917  *data++ = htons(type);
918  //! Optional unicast response based on local port, class IN
919  *data++ = htons(rclass);
920 
921  ptrdiff_t tosend = (char*)data - (char*)buffer;
922  if (mdns_multicast_send(sock, buffer, (size_t)tosend))
923  return -1;
924  return query_id;
925 }
926 
927 static size_t
928 mdns_query_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
929  void* user_data, int only_query_id) {
930  struct sockaddr_in6 addr;
931  struct sockaddr* saddr = (struct sockaddr*)&addr;
932  socklen_t addrlen = sizeof(addr);
933  memset(&addr, 0, sizeof(addr));
934 #ifdef __APPLE__
935  saddr->sa_len = sizeof(addr);
936 #endif
937  int ret = recvfrom(sock, (char*)buffer, (mdns_size_t)capacity, 0, saddr, &addrlen);
938  if (ret <= 0)
939  return 0;
940 
941  return mdns_query_parse(
942  sock, saddr, addrlen, buffer, (size_t)ret, callback, user_data,
943  only_query_id);
944 }
945 
946 static size_t mdns_query_parse(
947  int sock,
948  const struct sockaddr* saddr,
949  size_t addrlen,
950  const void* buffer,
951  size_t data_size,
952  mdns_record_callback_fn callback,
953  void* user_data,
954  int only_query_id)
955 {
956  if (data_size < sizeof(struct mdns_header_t))
957  return 0;
958 
959  const uint16_t* data = (const uint16_t*)buffer;
960 
961  uint16_t query_id = ntohs(*data++);
962  uint16_t flags = ntohs(*data++);
963  uint16_t questions = ntohs(*data++);
964  uint16_t answer_rrs = ntohs(*data++);
965  uint16_t authority_rrs = ntohs(*data++);
966  uint16_t additional_rrs = ntohs(*data++);
967  (void)sizeof(flags);
968 
969  if ((only_query_id > 0) && (query_id != only_query_id))
970  return 0; // Not a reply to the wanted one-shot query
971 
972  if (questions > 1)
973  return 0;
974 
975  // Skip questions part
976  int i;
977  for (i = 0; i < questions; ++i)
978  {
979  size_t ofs = MDNS_POINTER_DIFF(data, buffer);
980  if (!mdns_string_skip(buffer, data_size, &ofs))
981  return 0;
982  data = (const uint16_t*)MDNS_POINTER_OFFSET_CONST(buffer, ofs);
983  uint16_t rtype = ntohs(*data++);
984  uint16_t rclass = ntohs(*data++);
985  (void)sizeof(rtype);
986  (void)sizeof(rclass);
987  }
988 
989  size_t records = 0;
990  size_t offset = MDNS_POINTER_DIFF(data, buffer);
991  records += mdns_records_parse(
992  sock, saddr, addrlen, buffer, data_size, &offset,
993  MDNS_ENTRYTYPE_ANSWER, query_id, answer_rrs, callback, user_data);
994  records += mdns_records_parse(
995  sock, saddr, addrlen, buffer, data_size, &offset,
996  MDNS_ENTRYTYPE_AUTHORITY, query_id, authority_rrs, callback,
997  user_data);
998  records += mdns_records_parse(
999  sock, saddr, addrlen, buffer, data_size, &offset,
1000  MDNS_ENTRYTYPE_ADDITIONAL, query_id, additional_rrs, callback,
1001  user_data);
1002  return records;
1003 }
1004 
1005 static int
1006 mdns_query_answer(int sock, const void* address, size_t address_size, void* buffer, size_t capacity,
1007  uint16_t query_id, const char* service, size_t service_length,
1008  const char* hostname, size_t hostname_length, uint32_t ipv4, const uint8_t* ipv6,
1009  uint16_t port, const char* txt, size_t txt_length) {
1010  if (capacity < (sizeof(struct mdns_header_t) + 32 + service_length + hostname_length))
1011  return -1;
1012 
1013  int unicast = (address_size ? 1 : 0);
1014  int use_ipv4 = (ipv4 != 0);
1015  int use_ipv6 = (ipv6 != 0);
1016  int use_txt = (txt && txt_length && (txt_length <= 255));
1017 
1018  uint16_t question_rclass = (unicast ? MDNS_UNICAST_RESPONSE : 0) | MDNS_CLASS_IN;
1019  uint16_t rclass = (unicast ? MDNS_CACHE_FLUSH : 0) | MDNS_CLASS_IN;
1020  uint32_t ttl = (unicast ? 10 : 60);
1021  uint32_t a_ttl = ttl;
1022 
1023  // Basic answer structure
1024  struct mdns_header_t* header = (struct mdns_header_t*)buffer;
1025  header->query_id = (address_size ? htons(query_id) : 0);
1026  header->flags = htons(0x8400);
1027  header->questions = htons(unicast ? 1 : 0);
1028  header->answer_rrs = htons(1);
1029  header->authority_rrs = 0;
1030  header->additional_rrs = htons((unsigned short)(1 + use_ipv4 + use_ipv6 + use_txt));
1031 
1032  void* data = MDNS_POINTER_OFFSET(buffer, sizeof(struct mdns_header_t));
1033  uint16_t* udata;
1034  size_t remain, service_offset = 0, local_offset = 0, full_offset, host_offset;
1035 
1036  // Fill in question if unicast
1037  if (unicast) {
1038  service_offset = MDNS_POINTER_DIFF(data, buffer);
1039  remain = capacity - service_offset;
1040  data = mdns_string_make(data, remain, service, service_length);
1041  local_offset = MDNS_POINTER_DIFF(data, buffer) - 7;
1042  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1043  if (!data || (remain <= 4))
1044  return -1;
1045 
1046  udata = (uint16_t*)data;
1047  *udata++ = htons(MDNS_RECORDTYPE_PTR);
1048  *udata++ = htons(question_rclass);
1049  data = udata;
1050  }
1051  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1052 
1053  // Fill in answers
1054  // PTR record for service
1055  if (unicast) {
1056  data = mdns_string_make_ref(data, remain, service_offset);
1057  } else {
1058  service_offset = MDNS_POINTER_DIFF(data, buffer);
1059  remain = capacity - service_offset;
1060  data = mdns_string_make(data, remain, service, service_length);
1061  local_offset = MDNS_POINTER_DIFF(data, buffer) - 7;
1062  }
1063  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1064  if (!data || (remain <= 10))
1065  return -1;
1066  udata = (uint16_t*)data;
1067  *udata++ = htons(MDNS_RECORDTYPE_PTR);
1068  *udata++ = htons(rclass);
1069  *(uint32_t*)udata = htonl(ttl);
1070  udata += 2;
1071  uint16_t* record_length = udata++; // length
1072  data = udata;
1073  // Make a string <hostname>.<service>.local.
1074  full_offset = MDNS_POINTER_DIFF(data, buffer);
1075  remain = capacity - full_offset;
1076  data = mdns_string_make_with_ref(data, remain, hostname, hostname_length, service_offset);
1077  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1078  if (!data || (remain <= 10))
1079  return -1;
1080  *record_length = htons((uint16_t)MDNS_POINTER_DIFF(data, record_length + 1));
1081 
1082  // Fill in additional records
1083  // SRV record for <hostname>.<service>.local.
1084  data = mdns_string_make_ref(data, remain, full_offset);
1085  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1086  if (!data || (remain <= 10))
1087  return -1;
1088  udata = (uint16_t*)data;
1089  *udata++ = htons(MDNS_RECORDTYPE_SRV);
1090  *udata++ = htons(rclass);
1091  *(uint32_t*)udata = htonl(ttl);
1092  udata += 2;
1093  record_length = udata++; // length
1094  *udata++ = htons(0); // priority
1095  *udata++ = htons(0); // weight
1096  *udata++ = htons(port); // port
1097  // Make a string <hostname>.local.
1098  data = udata;
1099  host_offset = MDNS_POINTER_DIFF(data, buffer);
1100  remain = capacity - host_offset;
1101  data = mdns_string_make_with_ref(data, remain, hostname, hostname_length, local_offset);
1102  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1103  if (!data || (remain <= 10))
1104  return -1;
1105  *record_length = htons((uint16_t)MDNS_POINTER_DIFF(data, record_length + 1));
1106 
1107  // A record for <hostname>.local.
1108  if (use_ipv4) {
1109  data = mdns_string_make_ref(data, remain, host_offset);
1110  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1111  if (!data || (remain <= 14))
1112  return -1;
1113  udata = (uint16_t*)data;
1114  *udata++ = htons(MDNS_RECORDTYPE_A);
1115  *udata++ = htons(rclass);
1116  *(uint32_t*)udata = htonl(a_ttl);
1117  udata += 2;
1118  *udata++ = htons(4); // length
1119  *(uint32_t*)udata = ipv4; // ipv4 address
1120  udata += 2;
1121  data = udata;
1122  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1123  }
1124 
1125  // AAAA record for <hostname>.local.
1126  if (use_ipv6) {
1127  data = mdns_string_make_ref(data, remain, host_offset);
1128  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1129  if (!data || (remain <= 26))
1130  return -1;
1131  udata = (uint16_t*)data;
1132  *udata++ = htons(MDNS_RECORDTYPE_AAAA);
1133  *udata++ = htons(rclass);
1134  *(uint32_t*)udata = htonl(a_ttl);
1135  udata += 2;
1136  *udata++ = htons(16); // length
1137  memcpy(udata, ipv6, 16); // ipv6 address
1138  data = MDNS_POINTER_OFFSET(udata, 16);
1139  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1140  }
1141 
1142  // TXT record for <hostname>.<service>.local.
1143  if (use_txt) {
1144  data = mdns_string_make_ref(data, remain, full_offset);
1145  remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1146  if (!data || (remain <= (11 + txt_length)))
1147  return -1;
1148  udata = (uint16_t*)data;
1149  *udata++ = htons(MDNS_RECORDTYPE_TXT);
1150  *udata++ = htons(rclass);
1151  *(uint32_t*)udata = htonl(ttl);
1152  udata += 2;
1153  *udata++ = htons((unsigned short)(txt_length + 1)); // length
1154  char* txt_record = (char*)udata;
1155  *txt_record++ = (char)txt_length;
1156  memcpy(txt_record, txt, txt_length); // txt record
1157  data = MDNS_POINTER_OFFSET(txt_record, txt_length);
1158  // Unused until multiple txt records are supported
1159  // remain = capacity - MDNS_POINTER_DIFF(data, buffer);
1160  }
1161 
1162  size_t tosend = MDNS_POINTER_DIFF(data, buffer);
1163  if (address_size)
1164  return mdns_unicast_send(sock, address, address_size, buffer, tosend);
1165  return mdns_multicast_send(sock, buffer, tosend);
1166 }
1167 
1168 static mdns_string_t
1169 mdns_record_parse_ptr(const void* buffer, size_t size, size_t offset, size_t length,
1170  char* strbuffer, size_t capacity) {
1171  // PTR record is just a string
1172  if ((size >= offset + length) && (length >= 2))
1173  return mdns_string_extract(buffer, size, &offset, strbuffer, capacity);
1174  mdns_string_t empty = {0, 0};
1175  return empty;
1176 }
1177 
1178 static mdns_record_srv_t
1179 mdns_record_parse_srv(const void* buffer, size_t size, size_t offset, size_t length,
1180  char* strbuffer, size_t capacity) {
1181  mdns_record_srv_t srv;
1182  memset(&srv, 0, sizeof(mdns_record_srv_t));
1183  // Read the priority, weight, port number and the discovery name
1184  // SRV record format (http://www.ietf.org/rfc/rfc2782.txt):
1185  // 2 bytes network-order unsigned priority
1186  // 2 bytes network-order unsigned weight
1187  // 2 bytes network-order unsigned port
1188  // string: discovery (domain) name, minimum 2 bytes when compressed
1189  if ((size >= offset + length) && (length >= 8)) {
1190  const uint16_t* recorddata = (const uint16_t*)((const char*)buffer + offset);
1191  srv.priority = ntohs(*recorddata++);
1192  srv.weight = ntohs(*recorddata++);
1193  srv.port = ntohs(*recorddata++);
1194  offset += 6;
1195  srv.name = mdns_string_extract(buffer, size, &offset, strbuffer, capacity);
1196  }
1197  return srv;
1198 }
1199 
1200 static struct sockaddr_in*
1201 mdns_record_parse_a(const void* buffer, size_t size, size_t offset, size_t length,
1202  struct sockaddr_in* addr) {
1203  memset(addr, 0, sizeof(struct sockaddr_in));
1204  addr->sin_family = AF_INET;
1205 #ifdef __APPLE__
1206  addr->sin_len = sizeof(struct sockaddr_in);
1207 #endif
1208  if ((size >= offset + length) && (length == 4))
1209  addr->sin_addr.s_addr = *(const uint32_t*)((const char*)buffer + offset);
1210  return addr;
1211 }
1212 
1213 static struct sockaddr_in6*
1214 mdns_record_parse_aaaa(const void* buffer, size_t size, size_t offset, size_t length,
1215  struct sockaddr_in6* addr) {
1216  memset(addr, 0, sizeof(struct sockaddr_in6));
1217  addr->sin6_family = AF_INET6;
1218 #ifdef __APPLE__
1219  addr->sin6_len = sizeof(struct sockaddr_in6);
1220 #endif
1221  if ((size >= offset + length) && (length == 16))
1222  addr->sin6_addr = *(const struct in6_addr*)((const char*)buffer + offset);
1223  return addr;
1224 }
1225 
1226 static size_t
1227 mdns_record_parse_txt(const void* buffer, size_t size, size_t offset, size_t length,
1228  mdns_record_txt_t* records, size_t capacity) {
1229  size_t parsed = 0;
1230  const char* strdata;
1231  size_t separator, sublength;
1232  size_t end = offset + length;
1233 
1234  if (size < end)
1235  end = size;
1236 
1237  while ((offset < end) && (parsed < capacity)) {
1238  strdata = (const char*)buffer + offset;
1239  sublength = *(const unsigned char*)strdata;
1240 
1241  ++strdata;
1242  offset += sublength + 1;
1243 
1244  separator = 0;
1245  for (size_t c = 0; c < sublength; ++c) {
1246  // DNS-SD TXT record keys MUST be printable US-ASCII, [0x20, 0x7E]
1247  if ((strdata[c] < 0x20) || (strdata[c] > 0x7E))
1248  break;
1249  if (strdata[c] == '=') {
1250  separator = c;
1251  break;
1252  }
1253  }
1254 
1255  if (!separator)
1256  continue;
1257 
1258  if (separator < sublength) {
1259  records[parsed].key.str = strdata;
1260  records[parsed].key.length = separator;
1261  records[parsed].value.str = strdata + separator + 1;
1262  records[parsed].value.length = sublength - (separator + 1);
1263  } else {
1264  records[parsed].key.str = strdata;
1265  records[parsed].key.length = sublength;
1266  }
1267 
1268  ++parsed;
1269  }
1270 
1271  return parsed;
1272 }
1273 
1274 #ifdef __cplusplus
1275 }
1276 #endif
1277 
1278 #endif // __NET_MDNSIMPL_H__
1279 
mdns_string_t key
Definition: NET_MDNSImpl.h:92
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GLbitfield flags
Definition: glcorearb.h:1596
#define MDNS_UNICAST_RESPONSE
Definition: NET_MDNSImpl.h:49
#define MDNS_INVALID_POS
Definition: NET_MDNSImpl.h:39
void
Definition: png.h:1083
GLboolean * data
Definition: glcorearb.h:131
mdns_string_t value
Definition: NET_MDNSImpl.h:93
uint16_t query_id
Definition: NET_MDNSImpl.h:97
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
**But if you need a result
Definition: thread.h:622
void close() override
uint16_t questions
Definition: NET_MDNSImpl.h:99
GLuint buffer
Definition: glcorearb.h:660
uint16_t flags
Definition: NET_MDNSImpl.h:98
#define MDNS_POINTER_DIFF(a, b)
Definition: NET_MDNSImpl.h:46
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
int(* mdns_record_callback_fn)(int sock, const struct sockaddr *from, size_t addrlen, NET_mDNSEntryType entry, uint16_t query_id, uint16_t rtype, uint16_t rclass, uint32_t ttl, const void *data, size_t size, size_t name_offset, size_t name_length, size_t record_offset, size_t record_length, void *user_data)
Definition: NET_MDNSImpl.h:56
GLintptr offset
Definition: glcorearb.h:665
size_t mdns_size_t
Definition: NET_MDNSImpl.h:70
int SYSstrncasecmp(const char *a, const char *b, size_t n)
Definition: SYS_String.h:318
NET_mDNSEntryType
Definition: NET_MDNS.h:51
GLuint GLuint end
Definition: glcorearb.h:475
GLuint const GLchar * name
Definition: glcorearb.h:786
uint16_t authority_rrs
Definition: NET_MDNSImpl.h:101
uint16_t additional_rrs
Definition: NET_MDNSImpl.h:102
mdns_string_t name
Definition: NET_MDNSImpl.h:88
GLsizeiptr size
Definition: glcorearb.h:664
GLenum GLenum dst
Definition: glcorearb.h:1793
GLenum GLfloat param
Definition: glcorearb.h:104
#define MDNS_PORT
Definition: NET_MDNSImpl.h:48
#define MDNS_POINTER_OFFSET_CONST(p, ofs)
Definition: NET_MDNSImpl.h:45
GLuint GLfloat * val
Definition: glcorearb.h:1608
mdns_class
Definition: NET_MDNSImpl.h:52
#define MDNS_CACHE_FLUSH
Definition: NET_MDNSImpl.h:50
uint16_t answer_rrs
Definition: NET_MDNSImpl.h:100
enum mdns_class mdns_class_t
Definition: NET_MDNSImpl.h:54
#define MDNS_POINTER_OFFSET(p, ofs)
Definition: NET_MDNSImpl.h:44
NET_mDNSRecordType
Definition: NET_MDNS.h:36
const char * str
Definition: NET_MDNSImpl.h:74