forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdns.h
1202 lines (1046 loc) · 37.8 KB
/
mdns.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* https://github.com/mjansson/mdns/blob/6381bc09ae32b66de913fa0c982c41d3e67b63d2/mdns.h
* mdns.h - mDNS/DNS-SD library - Public Domain - 2017 Mattias Jansson
*
* This library provides a cross-platform mDNS and DNS-SD library in C.
* The implementation is based on RFC 6762 and RFC 6763.
*
* The latest source code is always available at
*
* https://github.com/mjansson/mdns
*
* This library is put in the public domain; you can redistribute it and/or modify it without any
* restrictions.
*
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define strncasecmp _strnicmp
#else
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define MDNS_INVALID_POS ((size_t)-1)
#define MDNS_STRING_CONST(s) (s), (sizeof((s)) - 1)
#define MDNS_STRING_FORMAT(s) (int)((s).length), s.str
#define MDNS_POINTER_OFFSET(p, ofs) ((void*)((char*)(p) + (ptrdiff_t)(ofs)))
#define MDNS_POINTER_OFFSET_CONST(p, ofs) ((const void*)((const char*)(p) + (ptrdiff_t)(ofs)))
#define MDNS_POINTER_DIFF(a, b) ((size_t)((const char*)(a) - (const char*)(b)))
#define MDNS_PORT 5353
enum mdns_record_type {
MDNS_RECORDTYPE_IGNORE = 0,
// Address
MDNS_RECORDTYPE_A = 1,
// Domain Name pointer
MDNS_RECORDTYPE_PTR = 12,
// Arbitrary text string
MDNS_RECORDTYPE_TXT = 16,
// IP6 Address [Thomson]
MDNS_RECORDTYPE_AAAA = 28,
// Server Selection [RFC2782]
MDNS_RECORDTYPE_SRV = 33
};
enum mdns_entry_type {
MDNS_ENTRYTYPE_QUESTION = 0,
MDNS_ENTRYTYPE_ANSWER = 1,
MDNS_ENTRYTYPE_AUTHORITY = 2,
MDNS_ENTRYTYPE_ADDITIONAL = 3
};
enum mdns_class { MDNS_CLASS_IN = 1 };
typedef enum mdns_record_type mdns_record_type_t;
typedef enum mdns_entry_type mdns_entry_type_t;
typedef enum mdns_class mdns_class_t;
typedef int (*mdns_record_callback_fn)(int sock, const struct sockaddr* from, size_t addrlen,
mdns_entry_type_t entry, uint16_t transaction_id,
uint16_t rtype, uint16_t rclass, uint32_t ttl,
const void* data, size_t size, size_t offset, size_t length,
void* user_data);
typedef struct mdns_string_t mdns_string_t;
typedef struct mdns_string_pair_t mdns_string_pair_t;
typedef struct mdns_record_srv_t mdns_record_srv_t;
typedef struct mdns_record_txt_t mdns_record_txt_t;
#ifdef _WIN32
typedef int mdns_size_t;
#else
typedef size_t mdns_size_t;
#endif
struct mdns_string_t {
const char* str;
size_t length;
};
struct mdns_string_pair_t {
size_t offset;
size_t length;
int ref;
};
struct mdns_record_srv_t {
uint16_t priority;
uint16_t weight;
uint16_t port;
mdns_string_t name;
};
struct mdns_record_txt_t {
mdns_string_t key;
mdns_string_t value;
};
struct mdns_header_t {
uint16_t transaction_id;
uint16_t flags;
uint16_t questions;
uint16_t answer_rrs;
uint16_t authority_rrs;
uint16_t additional_rrs;
};
// mDNS/DNS-SD public API
//! Open and setup a IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
// pass in the appropriate socket address in saddr, otherwise pass a null pointer for INADDR_ANY.
// To send discovery requests and queries set 0 as port to assign a random user level port (also
// done if passing a null saddr). To run discovery service listening for incoming
// discoveries and queries, set MDNS_PORT as port.
static int
mdns_socket_open_ipv4(struct sockaddr_in* saddr);
//! Setup an already opened IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
// pass in the appropriate socket address in saddr, otherwise pass a null pointer for INADDR_ANY.
// To send discovery requests and queries set 0 as port to assign a random user level port (also
// done if passing a null saddr). To run discovery service listening for incoming
// discoveries and queries, set MDNS_PORT as port.
static int
mdns_socket_setup_ipv4(int sock, struct sockaddr_in* saddr);
//! Open and setup a IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
// pass in the appropriate socket address in saddr, otherwise pass a null pointer for in6addr_any.
// To send discovery requests and queries set 0 as port to assign a random user level port (also
// done if passing a null saddr). To run discovery service listening for incoming
// discoveries and queries, set MDNS_PORT as port.
static int
mdns_socket_open_ipv6(struct sockaddr_in6* saddr);
//! Setup an already opened IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,
// pass in the appropriate socket address in saddr, otherwise pass a null pointer for in6addr_any.
// To send discovery requests and queries set 0 as port to assign a random user level port (also
// done if passing a null saddr). To run discovery service listening for incoming
// discoveries and queries, set MDNS_PORT as port.
static int
mdns_socket_setup_ipv6(int sock, struct sockaddr_in6* saddr);
//! Close a socket opened with mdns_socket_open_ipv4 and mdns_socket_open_ipv6.
static void
mdns_socket_close(int sock);
//! Listen for incoming multicast DNS-SD and mDNS query requests. The socket should have been
// opened on port MDNS_PORT using one of the mdns open or setup socket functions. Returns the
// number of queries parsed.
static size_t
mdns_socket_listen(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
void* user_data);
//! Send a multicast DNS-SD reqeuest on the given socket to discover available services. Returns
// 0 on success, or <0 if error.
static int
mdns_discovery_send(int sock);
//! Recieve unicast responses to a DNS-SD sent with mdns_discovery_send. Any data will be piped to
// the given callback for parsing. Returns the number of responses parsed.
static size_t
mdns_discovery_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
void* user_data);
//! Send a unicast DNS-SD answer with a single record to the given address. Returns 0 if success,
// or <0 if error.
static int
mdns_discovery_answer(int sock, const void* address, size_t address_size, void* buffer,
size_t capacity, const char* record, size_t length);
//! Send a multicast mDNS query on the given socket for the given service name. The supplied buffer
// will be used to build the query packet. Returns the transaction ID for the query sent (always >0),
// or <0 if error.
static int
mdns_query_send(int sock, mdns_record_type_t type, const char* name, size_t length, void* buffer,
size_t capacity);
//! Receive unicast responses to a mDNS query sent with mdns_discovery_recv, optionally filtering
// out any responses not matching the given transaction ID. Set the transaction ID to 0 to parse
// all responses, even if it is not matching any sent query. Any data will be piped to the given
// callback for parsing. Returns the number of responses parsed.
static size_t
mdns_query_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
void* user_data, int only_last_query);
//! Send a unicast mDNS query answer with a single record to the given address. Returns 0 if
// success, or <0 if error.
static int
mdns_query_answer(int sock, const void* address, size_t address_size, void* buffer, size_t capacity,
uint16_t transaction_id, const char* service, size_t service_length,
const char* hostname, size_t hostname_length, uint32_t ipv4, const uint8_t* ipv6,
uint16_t port, const char* txt, size_t txt_length);
// Internal functions
static mdns_string_t
mdns_string_extract(const void* buffer, size_t size, size_t* offset, char* str, size_t capacity);
static int
mdns_string_skip(const void* buffer, size_t size, size_t* offset);
static int
mdns_string_equal(const void* buffer_lhs, size_t size_lhs, size_t* ofs_lhs, const void* buffer_rhs,
size_t size_rhs, size_t* ofs_rhs);
static void*
mdns_string_make(void* data, size_t capacity, const char* name, size_t length);
static void*
mdns_string_make_ref(void* data, size_t capacity, size_t ref_offset);
static void*
mdns_string_make_with_ref(void* data, size_t capacity, const char* name, size_t length,
size_t ref_offset);
static mdns_string_t
mdns_record_parse_ptr(const void* buffer, size_t size, size_t offset, size_t length,
char* strbuffer, size_t capacity);
static mdns_record_srv_t
mdns_record_parse_srv(const void* buffer, size_t size, size_t offset, size_t length,
char* strbuffer, size_t capacity);
static struct sockaddr_in*
mdns_record_parse_a(const void* buffer, size_t size, size_t offset, size_t length,
struct sockaddr_in* addr);
static struct sockaddr_in6*
mdns_record_parse_aaaa(const void* buffer, size_t size, size_t offset, size_t length,
struct sockaddr_in6* addr);
static size_t
mdns_record_parse_txt(const void* buffer, size_t size, size_t offset, size_t length,
mdns_record_txt_t* records, size_t capacity);
// Implementations
static int
mdns_socket_open_ipv4(struct sockaddr_in* saddr) {
int sock = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0)
return -1;
if (mdns_socket_setup_ipv4(sock, saddr)) {
mdns_socket_close(sock);
return -1;
}
return sock;
}
static int
mdns_socket_setup_ipv4(int sock, struct sockaddr_in* saddr) {
unsigned char ttl = 1;
unsigned char loopback = 1;
unsigned int reuseaddr = 1;
struct ip_mreq req;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(reuseaddr));
#ifdef SO_REUSEPORT
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuseaddr, sizeof(reuseaddr));
#endif
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&ttl, sizeof(ttl));
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&loopback, sizeof(loopback));
memset(&req, 0, sizeof(req));
req.imr_multiaddr.s_addr = htonl((((uint32_t)224U) << 24U) | ((uint32_t)251U));
req.imr_interface.s_addr = INADDR_ANY;
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&req, sizeof(req)))
return -1;
struct sockaddr_in sock_addr;
if (!saddr) {
saddr = &sock_addr;
memset(saddr, 0, sizeof(struct sockaddr_in));
saddr->sin_family = AF_INET;
saddr->sin_addr.s_addr = INADDR_ANY;
#ifdef __APPLE__
saddr->sin_len = sizeof(struct sockaddr_in);
#endif
}
if (bind(sock, (struct sockaddr*)saddr, sizeof(struct sockaddr_in)))
return -1;
#ifdef _WIN32
unsigned long param = 1;
ioctlsocket(sock, FIONBIO, ¶m);
#else
const int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
#endif
return 0;
}
static int
mdns_socket_open_ipv6(struct sockaddr_in6* saddr) {
int sock = (int)socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0)
return -1;
if (mdns_socket_setup_ipv6(sock, saddr)) {
mdns_socket_close(sock);
return -1;
}
return sock;
}
static int
mdns_socket_setup_ipv6(int sock, struct sockaddr_in6* saddr) {
int hops = 1;
unsigned int loopback = 1;
unsigned int reuseaddr = 1;
struct ipv6_mreq req;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(reuseaddr));
#ifdef SO_REUSEPORT
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuseaddr, sizeof(reuseaddr));
#endif
setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (const char*)&hops, sizeof(hops));
setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const char*)&loopback, sizeof(loopback));
memset(&req, 0, sizeof(req));
req.ipv6mr_multiaddr.s6_addr[0] = 0xFF;
req.ipv6mr_multiaddr.s6_addr[1] = 0x02;
req.ipv6mr_multiaddr.s6_addr[15] = 0xFB;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char*)&req, sizeof(req)))
return -1;
struct sockaddr_in6 sock_addr;
if (!saddr) {
saddr = &sock_addr;
memset(saddr, 0, sizeof(struct sockaddr_in6));
saddr->sin6_family = AF_INET6;
saddr->sin6_addr = in6addr_any;
#ifdef __APPLE__
saddr->sin6_len = sizeof(struct sockaddr_in6);
#endif
}
if (bind(sock, (struct sockaddr*)saddr, sizeof(struct sockaddr_in6)))
return -1;
#ifdef _WIN32
unsigned long param = 1;
ioctlsocket(sock, FIONBIO, ¶m);
#else
const int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
#endif
return 0;
}
static void
mdns_socket_close(int sock) {
#ifdef _WIN32
closesocket(sock);
#else
close(sock);
#endif
}
static int
mdns_is_string_ref(uint8_t val) {
return (0xC0 == (val & 0xC0));
}
static mdns_string_pair_t
mdns_get_next_substring(const void* rawdata, size_t size, size_t offset) {
const uint8_t* buffer = (const uint8_t*)rawdata;
mdns_string_pair_t pair = {MDNS_INVALID_POS, 0, 0};
if (!buffer[offset]) {
pair.offset = offset;
return pair;
}
if (mdns_is_string_ref(buffer[offset])) {
if (size < offset + 2)
return pair;
offset = 0x3fff & ntohs(*(uint16_t*)MDNS_POINTER_OFFSET(buffer, offset));
if (offset >= size)
return pair;
pair.ref = 1;
}
size_t length = (size_t)buffer[offset++];
if (size < offset + length)
return pair;
pair.offset = offset;
pair.length = length;
return pair;
}
static int
mdns_string_skip(const void* buffer, size_t size, size_t* offset) {
size_t cur = *offset;
mdns_string_pair_t substr;
do {
substr = mdns_get_next_substring(buffer, size, cur);
if (substr.offset == MDNS_INVALID_POS)
return 0;
if (substr.ref) {
*offset = cur + 2;
return 1;
}
cur = substr.offset + substr.length;
} while (substr.length);
*offset = cur + 1;
return 1;
}
static int
mdns_string_equal(const void* buffer_lhs, size_t size_lhs, size_t* ofs_lhs, const void* buffer_rhs,
size_t size_rhs, size_t* ofs_rhs) {
size_t lhs_cur = *ofs_lhs;
size_t rhs_cur = *ofs_rhs;
size_t lhs_end = MDNS_INVALID_POS;
size_t rhs_end = MDNS_INVALID_POS;
mdns_string_pair_t lhs_substr;
mdns_string_pair_t rhs_substr;
do {
lhs_substr = mdns_get_next_substring(buffer_lhs, size_lhs, lhs_cur);
rhs_substr = mdns_get_next_substring(buffer_rhs, size_rhs, rhs_cur);
if ((lhs_substr.offset == MDNS_INVALID_POS) || (rhs_substr.offset == MDNS_INVALID_POS))
return 0;
if (lhs_substr.length != rhs_substr.length)
return 0;
if (strncasecmp((const char*)buffer_rhs + rhs_substr.offset,
(const char*)buffer_lhs + lhs_substr.offset, rhs_substr.length))
return 0;
if (lhs_substr.ref && (lhs_end == MDNS_INVALID_POS))
lhs_end = lhs_cur + 2;
if (rhs_substr.ref && (rhs_end == MDNS_INVALID_POS))
rhs_end = rhs_cur + 2;
lhs_cur = lhs_substr.offset + lhs_substr.length;
rhs_cur = rhs_substr.offset + rhs_substr.length;
} while (lhs_substr.length);
if (lhs_end == MDNS_INVALID_POS)
lhs_end = lhs_cur + 1;
*ofs_lhs = lhs_end;
if (rhs_end == MDNS_INVALID_POS)
rhs_end = rhs_cur + 1;
*ofs_rhs = rhs_end;
return 1;
}
static mdns_string_t
mdns_string_extract(const void* buffer, size_t size, size_t* offset, char* str, size_t capacity) {
size_t cur = *offset;
size_t end = MDNS_INVALID_POS;
mdns_string_pair_t substr;
mdns_string_t result;
result.str = str;
result.length = 0;
char* dst = str;
size_t remain = capacity;
do {
substr = mdns_get_next_substring(buffer, size, cur);
if (substr.offset == MDNS_INVALID_POS)
return result;
if (substr.ref && (end == MDNS_INVALID_POS))
end = cur + 2;
if (substr.length) {
size_t to_copy = (substr.length < remain) ? substr.length : remain;
memcpy(dst, (const char*)buffer + substr.offset, to_copy);
dst += to_copy;
remain -= to_copy;
if (remain) {
*dst++ = '.';
--remain;
}
}
cur = substr.offset + substr.length;
} while (substr.length);
if (end == MDNS_INVALID_POS)
end = cur + 1;
*offset = end;
result.length = capacity - remain;
return result;
}
static size_t
mdns_string_find(const char* str, size_t length, char c, size_t offset) {
const void* found;
if (offset >= length)
return MDNS_INVALID_POS;
found = memchr(str + offset, c, length - offset);
if (found)
return (size_t)((const char*)found - str);
return MDNS_INVALID_POS;
}
static void*
mdns_string_make(void* data, size_t capacity, const char* name, size_t length) {
size_t pos = 0;
size_t last_pos = 0;
size_t remain = capacity;
unsigned char* dest = (unsigned char*)data;
while ((last_pos < length) &&
((pos = mdns_string_find(name, length, '.', last_pos)) != MDNS_INVALID_POS)) {
size_t sublength = pos - last_pos;
if (sublength < remain) {
*dest = (unsigned char)sublength;
memcpy(dest + 1, name + last_pos, sublength);
dest += sublength + 1;
remain -= sublength + 1;
} else {
return 0;
}
last_pos = pos + 1;
}
if (last_pos < length) {
size_t sublength = length - last_pos;
if (sublength < remain) {
*dest = (unsigned char)sublength;
memcpy(dest + 1, name + last_pos, sublength);
dest += sublength + 1;
remain -= sublength + 1;
} else {
return 0;
}
}
if (!remain)
return 0;
*dest++ = 0;
return dest;
}
static void*
mdns_string_make_ref(void* data, size_t capacity, size_t ref_offset) {
if (capacity < 2)
return 0;
uint16_t* udata = (uint16_t*)data;
*udata++ = htons(0xC000 | (uint16_t)ref_offset);
return udata;
}
static void*
mdns_string_make_with_ref(void* data, size_t capacity, const char* name, size_t length,
size_t ref_offset) {
void* remaindata = mdns_string_make(data, capacity, name, length);
capacity -= MDNS_POINTER_DIFF(remaindata, data);
if (!data || !capacity)
return 0;
return mdns_string_make_ref(MDNS_POINTER_OFFSET(remaindata, -1), capacity + 1, ref_offset);
}
static size_t
mdns_records_parse(int sock, const struct sockaddr* from, size_t addrlen, const void* buffer,
size_t size, size_t* offset, mdns_entry_type_t type, uint16_t transaction_id,
size_t records, mdns_record_callback_fn callback, void* user_data) {
size_t parsed = 0;
int do_callback = (callback ? 1 : 0);
for (size_t i = 0; i < records; ++i) {
mdns_string_skip(buffer, size, offset);
const uint16_t* data = (const uint16_t*)((const char*)buffer + (*offset));
uint16_t rtype = ntohs(*data++);
uint16_t rclass = ntohs(*data++);
uint32_t ttl = ntohl(*(const uint32_t*)(const void*)data);
data += 2;
uint16_t length = ntohs(*data++);
*offset += 10;
if (do_callback) {
++parsed;
if (callback(sock, from, addrlen, type, transaction_id, rtype, rclass, ttl, buffer,
size, *offset, length, user_data))
do_callback = 0;
}
*offset += length;
}
return parsed;
}
static int
mdns_unicast_send(int sock, const void* address, size_t address_size, const void* buffer,
size_t size) {
if (sendto(sock, (const char*)buffer, (mdns_size_t)size, 0, (const struct sockaddr*)address,
(socklen_t)address_size) < 0)
return -1;
return 0;
}
static int
mdns_multicast_send(int sock, const void* buffer, size_t size) {
struct sockaddr_storage addr_storage;
struct sockaddr_in addr;
struct sockaddr_in6 addr6;
struct sockaddr* saddr = (struct sockaddr*)&addr_storage;
socklen_t saddrlen = sizeof(struct sockaddr_storage);
if (getsockname(sock, saddr, &saddrlen))
return -1;
if (saddr->sa_family == AF_INET6) {
memset(&addr6, 0, sizeof(struct sockaddr_in6));
addr6.sin6_family = AF_INET6;
#ifdef __APPLE__
addr6.sin6_len = sizeof(struct sockaddr_in6);
#endif
addr6.sin6_addr.s6_addr[0] = 0xFF;
addr6.sin6_addr.s6_addr[1] = 0x02;
addr6.sin6_addr.s6_addr[15] = 0xFB;
addr6.sin6_port = htons((unsigned short)MDNS_PORT);
saddr = (struct sockaddr*)&addr6;
saddrlen = sizeof(struct sockaddr_in6);
} else {
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
#ifdef __APPLE__
addr.sin_len = sizeof(struct sockaddr_in);
#endif
addr.sin_addr.s_addr = htonl((((uint32_t)224U) << 24U) | ((uint32_t)251U));
addr.sin_port = htons((unsigned short)MDNS_PORT);
saddr = (struct sockaddr*)&addr;
saddrlen = sizeof(struct sockaddr_in);
}
if (sendto(sock, (const char*)buffer, (mdns_size_t)size, 0, saddr, saddrlen) < 0)
return -1;
return 0;
}
static const uint8_t mdns_services_query[] = {
// Transaction ID
0x00, 0x00,
// Flags
0x00, 0x00,
// 1 question
0x00, 0x01,
// No answer, authority or additional RRs
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// _services._dns-sd._udp.local.
0x09, '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', 0x07, '_', 'd', 'n', 's', '-', 's', 'd',
0x04, '_', 'u', 'd', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00,
// PTR record
0x00, MDNS_RECORDTYPE_PTR,
// QU (unicast response) and class IN
0x80, MDNS_CLASS_IN};
static int
mdns_discovery_send(int sock) {
return mdns_multicast_send(sock, mdns_services_query, sizeof(mdns_services_query));
}
static size_t
mdns_discovery_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
void* user_data) {
struct sockaddr_in6 addr;
struct sockaddr* saddr = (struct sockaddr*)&addr;
socklen_t addrlen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
#ifdef __APPLE__
saddr->sa_len = sizeof(addr);
#endif
int ret = recvfrom(sock, (char*)buffer, (mdns_size_t)capacity, 0, saddr, &addrlen);
if (ret <= 0)
return 0;
size_t data_size = (size_t)ret;
size_t records = 0;
uint16_t* data = (uint16_t*)buffer;
uint16_t transaction_id = ntohs(*data++);
uint16_t flags = ntohs(*data++);
uint16_t questions = ntohs(*data++);
uint16_t answer_rrs = ntohs(*data++);
uint16_t authority_rrs = ntohs(*data++);
uint16_t additional_rrs = ntohs(*data++);
if (transaction_id || (flags != 0x8400))
return 0; // Not a reply to our question
if (questions != 1)
return 0;
int i;
for (i = 0; i < questions; ++i) {
size_t ofs = (size_t)((char*)data - (char*)buffer);
size_t verify_ofs = 12;
// Verify it's our question, _services._dns-sd._udp.local.
if (!mdns_string_equal(buffer, data_size, &ofs, mdns_services_query,
sizeof(mdns_services_query), &verify_ofs))
return 0;
data = (uint16_t*)((char*)buffer + ofs);
uint16_t rtype = ntohs(*data++);
uint16_t rclass = ntohs(*data++);
// Make sure we get a reply based on our PTR question for class IN
if ((rtype != MDNS_RECORDTYPE_PTR) || ((rclass & 0x7FFF) != MDNS_CLASS_IN))
return 0;
}
int do_callback = 1;
for (i = 0; i < answer_rrs; ++i) {
size_t ofs = (size_t)((char*)data - (char*)buffer);
size_t verify_ofs = 12;
// Verify it's an answer to our question, _services._dns-sd._udp.local.
int is_answer = mdns_string_equal(buffer, data_size, &ofs, mdns_services_query,
sizeof(mdns_services_query), &verify_ofs);
data = (uint16_t*)((char*)buffer + ofs);
uint16_t rtype = ntohs(*data++);
uint16_t rclass = ntohs(*data++);
uint32_t ttl = ntohl(*(uint32_t*)(void*)data);
data += 2;
uint16_t length = ntohs(*data++);
if (length >= (data_size - ofs))
return 0;
if (is_answer && do_callback) {
++records;
if (callback(sock, saddr, addrlen, MDNS_ENTRYTYPE_ANSWER, transaction_id, rtype, rclass,
ttl, buffer, data_size, (size_t)((char*)data - (char*)buffer), length,
user_data))
do_callback = 0;
}
data = (uint16_t*)((char*)data + length);
}
size_t offset = (size_t)((char*)data - (char*)buffer);
records += mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset,
MDNS_ENTRYTYPE_AUTHORITY, transaction_id, authority_rrs, callback,
user_data);
records += mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset,
MDNS_ENTRYTYPE_ADDITIONAL, transaction_id, additional_rrs,
callback, user_data);
return records;
}
static size_t
mdns_socket_listen(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
void* user_data) {
struct sockaddr_in6 addr;
struct sockaddr* saddr = (struct sockaddr*)&addr;
socklen_t addrlen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
#ifdef __APPLE__
saddr->sa_len = sizeof(addr);
#endif
int ret = recvfrom(sock, (char*)buffer, (mdns_size_t)capacity, 0, saddr, &addrlen);
if (ret <= 0)
return 0;
size_t data_size = (size_t)ret;
uint16_t* data = (uint16_t*)buffer;
uint16_t transaction_id = ntohs(*data++);
uint16_t flags = ntohs(*data++);
uint16_t questions = ntohs(*data++);
/*
This data is unused at the moment, skip
uint16_t answer_rrs = ntohs(*data++);
uint16_t authority_rrs = ntohs(*data++);
uint16_t additional_rrs = ntohs(*data++);
*/
data += 3;
size_t parsed = 0;
for (int iquestion = 0; iquestion < questions; ++iquestion) {
size_t question_offset = (size_t)((char*)data - (char*)buffer);
size_t offset = question_offset;
size_t verify_ofs = 12;
if (mdns_string_equal(buffer, data_size, &offset, mdns_services_query,
sizeof(mdns_services_query), &verify_ofs)) {
if (transaction_id || flags || (questions != 1))
return 0;
} else {
offset = question_offset;
if (!mdns_string_skip(buffer, data_size, &offset))
break;
}
size_t length = offset - question_offset;
data = (uint16_t*)((char*)buffer + offset);
uint16_t rtype = ntohs(*data++);
uint16_t rclass = ntohs(*data++);
// Make sure we get a PTR question of class IN
if ((rtype != MDNS_RECORDTYPE_PTR) || ((rclass & 0x7FFF) != MDNS_CLASS_IN))
return 0;
if (callback)
callback(sock, saddr, addrlen, MDNS_ENTRYTYPE_QUESTION, transaction_id, rtype, rclass,
0, buffer, data_size, question_offset, length, user_data);
++parsed;
}
return parsed;
}
static int
mdns_discovery_answer(int sock, const void* address, size_t address_size, void* buffer,
size_t capacity, const char* record, size_t length) {
if (capacity < (sizeof(mdns_services_query) + 32 + length))
return -1;
uint16_t* data = (uint16_t*)buffer;
// Basic reply structure
memcpy(data, mdns_services_query, sizeof(mdns_services_query));
// Flags
uint16_t* flags = data + 1;
*flags = htons(0x8400);
// One answer
uint16_t* answers = data + 3;
*answers = htons(1);
// Fill in answer PTR record
data = (uint16_t*)((char*)buffer + sizeof(mdns_services_query));
// Reference _services._dns-sd._udp.local. string in question
*data++ = htons(0xC000 | 12);
// Type
*data++ = htons(MDNS_RECORDTYPE_PTR);
// Rclass
*data++ = htons(MDNS_CLASS_IN);
// TTL
*(uint32_t*)data = htonl(10);
data += 2;
// Record string length
uint16_t* record_length = data++;
uint8_t* record_data = (uint8_t*)data;
size_t remain = capacity - (sizeof(mdns_services_query) + 10);
record_data = (uint8_t*)mdns_string_make(record_data, remain, record, length);
*record_length = htons((uint16_t)(record_data - (uint8_t*)data));
*record_data++ = 0;
ptrdiff_t tosend = (char*)record_data - (char*)buffer;
return mdns_unicast_send(sock, address, address_size, buffer, (size_t)tosend);
}
static uint16_t mdns_transaction_id = 0;
static int
mdns_query_send(int sock, mdns_record_type_t type, const char* name, size_t length, void* buffer,
size_t capacity) {
if (capacity < (17 + length))
return -1;
uint16_t transaction_id = ++mdns_transaction_id;
if (!transaction_id)
transaction_id = ++mdns_transaction_id;
uint16_t* data = (uint16_t*)buffer;
// Transaction ID
*data++ = htons(transaction_id);
// Flags
*data++ = 0;
// Questions
*data++ = htons(1);
// No answer, authority or additional RRs
*data++ = 0;
*data++ = 0;
*data++ = 0;
// Name string
data = (uint16_t*)mdns_string_make(data, capacity - 17, name, length);
if (!data)
return -1;
// Record type
*data++ = htons(type);
//! Unicast response, class IN
*data++ = htons(0x8000U | MDNS_CLASS_IN);
ptrdiff_t tosend = (char*)data - (char*)buffer;
if (mdns_multicast_send(sock, buffer, (size_t)tosend))
return -1;
return transaction_id;
}
static size_t
mdns_query_recv(int sock, void* buffer, size_t capacity, mdns_record_callback_fn callback,
void* user_data, int only_transaction_id) {
struct sockaddr_in6 addr;
struct sockaddr* saddr = (struct sockaddr*)&addr;
socklen_t addrlen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
#ifdef __APPLE__
saddr->sa_len = sizeof(addr);
#endif
int ret = recvfrom(sock, (char*)buffer, (mdns_size_t)capacity, 0, saddr, &addrlen);
if (ret <= 0)
return 0;
size_t data_size = (size_t)ret;
uint16_t* data = (uint16_t*)buffer;
uint16_t transaction_id = ntohs(*data++);
++data; // uint16_t flags = ntohs(*data++);
uint16_t questions = ntohs(*data++);
uint16_t answer_rrs = ntohs(*data++);
uint16_t authority_rrs = ntohs(*data++);
uint16_t additional_rrs = ntohs(*data++);
if ((only_transaction_id > 0) && (transaction_id != only_transaction_id)) // || (flags != 0x8400))
return 0; // Not a reply to the wanted query
if (questions > 1)
return 0;
// Skip questions part
int i;
for (i = 0; i < questions; ++i) {
size_t ofs = (size_t)((char*)data - (char*)buffer);
if (!mdns_string_skip(buffer, data_size, &ofs))
return 0;
data = (uint16_t*)((char*)buffer + ofs);
++data;
++data;
}
size_t records = 0;
size_t offset = MDNS_POINTER_DIFF(data, buffer);
records +=
mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset, MDNS_ENTRYTYPE_ANSWER,
transaction_id, answer_rrs, callback, user_data);
records += mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset,
MDNS_ENTRYTYPE_AUTHORITY, transaction_id, authority_rrs, callback,
user_data);
records += mdns_records_parse(sock, saddr, addrlen, buffer, data_size, &offset,
MDNS_ENTRYTYPE_ADDITIONAL, transaction_id, additional_rrs,
callback, user_data);
return records;
}
static int
mdns_query_answer(int sock, const void* address, size_t address_size, void* buffer, size_t capacity,
uint16_t transaction_id, const char* service, size_t service_length,
const char* hostname, size_t hostname_length, uint32_t ipv4, const uint8_t* ipv6,
uint16_t port, const char* txt, size_t txt_length) {
if (capacity < (sizeof(struct mdns_header_t) + 32 + service_length + hostname_length))
return -1;
int use_ipv4 = (ipv4 != 0);
int use_ipv6 = (ipv6 != 0);
int use_txt = (txt && txt_length && (txt_length <= 255));
// Basic answer structure
struct mdns_header_t* header = (struct mdns_header_t*)buffer;
header->transaction_id = htons(transaction_id);
header->flags = htons(0x8400);
header->questions = htons(1);
header->answer_rrs = htons(2 + (u_short)use_ipv4 + (u_short)use_ipv6 + (u_short)use_txt);
header->authority_rrs = 0;
header->additional_rrs = 0;
// Fill in question
void* data = MDNS_POINTER_OFFSET(buffer, sizeof(struct mdns_header_t));
size_t service_offset = MDNS_POINTER_DIFF(data, buffer);
size_t remain = capacity - service_offset;
data = mdns_string_make(data, remain, service, service_length);
size_t local_offset = MDNS_POINTER_DIFF(data, buffer) - 7;
remain = capacity - MDNS_POINTER_DIFF(data, buffer);
if (!data || (remain <= 4))
return -1;
uint16_t* udata = (uint16_t*)data;
*udata++ = htons(MDNS_RECORDTYPE_PTR);
*udata++ = htons(MDNS_CLASS_IN);
data = udata;
remain = capacity - MDNS_POINTER_DIFF(data, buffer);
// Fill in answers
// PTR record for service
data = mdns_string_make_ref(data, remain, service_offset);
remain = capacity - MDNS_POINTER_DIFF(data, buffer);
if (!data || (remain <= 10))
return -1;
udata = (uint16_t*)data;
*udata++ = htons(MDNS_RECORDTYPE_PTR); // type
*udata++ = htons(MDNS_CLASS_IN); // rclass
*(uint32_t*)udata = htonl(10); // ttl
udata += 2;
uint16_t* record_length = udata++; // length
data = udata;