-
Notifications
You must be signed in to change notification settings - Fork 1
/
LightMDNS.cpp
1759 lines (1532 loc) · 81.7 KB
/
LightMDNS.cpp
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
// mgream 2024
// Copyright (C) 2010 Georg Kaindl
// http://gkaindl.com
//
// This file is part of Arduino EthernetBonjour.
//
// EthernetBonjour is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// EthernetBonjour is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with EthernetBonjour. If not, see
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <Udp.h>
#include "LightMDNS.hpp"
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#include <esp_mac.h>
__attribute__ ((weak)) String getMacAddressBase (void) {
uint8_t mac [6];
esp_read_mac (mac, ESP_MAC_BASE);
char str [6 * 2 + 1];
snprintf (str, sizeof (str), "%02x%02x%02x%02x%02x%02x", mac [0], mac [1], mac [2], mac [3], mac [4], mac [5]);
return str;
}
// -----------------------------------------------------------------------------------------------
#include <numeric>
static String join (const std::vector<String> &elements, const String &delimiter) {
return elements.empty () ? String () : std::accumulate (std::next (elements.begin ()), elements.end (), elements [0], [&delimiter] (const String &a, const String &b) {
return a + delimiter + b;
});
}
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#define TLD ".local"
static constexpr const char *SERVICE_SD__fqsn = "_services._dns-sd._udp.local";
typedef enum {
PacketTypeCompleteRecord, // All record provide
PacketTypeCompleteRelease, // All record release
PacketTypeAddressRecord, // A record provide
PacketTypeAddressRelease, // A record release
PacketTypeReverseRecord, // Reverse mapping provide
PacketTypeServiceRecord, // Service record provide (SRV/TXT/PTR)
PacketTypeServiceRelease, // Service record release
PacketTypeProbe, // Name probe (conflict detection)
PacketTypeNextSecure, // NextSecure record (indicate no other records exist)
} PacketType;
typedef struct {
uint16_t xid; // Transaction ID: randomly chosen, used to match responses to queries
uint8_t recursionDesired : 1; // RD: Client sets this to request recursive resolution
uint8_t truncated : 1; // TC: Set when message is larger than transmission size allows
uint8_t authoritiveAnswer : 1; // AA: Server sets this when it's authoritative for the domain
uint8_t opCode : 4; // Operation type: 0=Query, 1=IQuery, 2=Status, 4=Notify, 5=Update
uint8_t queryResponse : 1; // QR: 0 for queries, 1 for responses
uint8_t responseCode : 4; // RCODE: 0=No error, 1=Format error, 2=Server fail, 3=Name error
uint8_t checkingDisabled : 1; // CD: Disables DNSSEC validation
uint8_t authenticatedData : 1; // AD: Indicates DNSSEC validation passed
uint8_t zReserved : 1; // Z: Reserved for future use, must be zero
uint8_t recursionAvailable : 1; // RA: Server sets this if it supports recursion
uint16_t queryCount; // QDCOUNT: Number of questions in the query section
uint16_t answerCount; // ANCOUNT: Number of records in the answer section
uint16_t authorityCount; // NSCOUNT: Number of records in the authority section
uint16_t additionalCount; // ARCOUNT: Number of records in the additional section
} __attribute__ ((__packed__)) Header;
// -----------------------------------------------------------------------------------------------
// HEADER
static constexpr uint16_t XID_DEFAULT = 0;
static constexpr uint8_t DNS_BIT_RD = 0; // Recursion Desired
static constexpr uint8_t DNS_BIT_TC = 1; // Truncation flag
static constexpr uint8_t DNS_BIT_AA = 2; // Authoritative Answer
static constexpr uint8_t DNS_AA_NON_AUTHORITATIVE = 0;
static constexpr uint8_t DNS_AA_AUTHORITATIVE = 1;
static constexpr uint8_t DNS_OPCODE_QUERY = 0; // Standard query
static constexpr uint8_t DNS_OPCODE_IQUERY = 1; // Inverse query
static constexpr uint8_t DNS_OPCODE_STATUS = 2; // Server status request
static constexpr uint8_t DNS_OPCODE_NOTIFY = 4; // Zone change notification
static constexpr uint8_t DNS_OPCODE_UPDATE = 5; // Dynamic update
static constexpr uint8_t DNS_BIT_QR = 7; // Query/Response flag
static constexpr uint8_t DNS_QR_QUERY = 0;
static constexpr uint8_t DNS_QR_RESPONSE = 1;
static constexpr uint8_t DNS_RCODE_NOERROR = 0; // No error
static constexpr uint8_t DNS_RCODE_FORMERR = 1; // Format error
static constexpr uint8_t DNS_RCODE_SERVFAIL = 2; // Server failure
static constexpr uint8_t DNS_RCODE_NXDOMAIN = 3; // Non-existent domain
static constexpr uint8_t DNS_RCODE_NOTIMP = 4; // Not implemented
static constexpr uint8_t DNS_RCODE_REFUSED = 5; // Query refused
static constexpr uint8_t DNS_RCODE_YXDOMAIN = 6; // Name exists when it should not
static constexpr uint8_t DNS_RCODE_YXRRSET = 7; // RR set exists when it should not
static constexpr uint8_t DNS_RCODE_NXRRSET = 8; // RR set that should exist does not
static constexpr uint8_t DNS_RCODE_NOTAUTH = 9; // Server not authoritative
static constexpr uint8_t DNS_RCODE_NOTZONE = 10; // Name not contained in zone
static constexpr uint8_t DNS_BIT_CD = 4; // Checking Disabled
static constexpr uint8_t DNS_BIT_AD = 5; // Authenticated Data
static constexpr uint8_t DNS_BIT_Z = 6; // Reserved bit
static constexpr uint8_t DNS_BIT_RA = 7; // Recursion Available
// RR
static constexpr uint8_t DNS_RECORD_HI = 0x00; // High byte of record type
static constexpr uint8_t DNS_RECORD_A = 0x01; // IPv4 host address
static constexpr uint8_t DNS_RECORD_NS = 0x02; // Nameserver
static constexpr uint8_t DNS_RECORD_CNAME = 0x05; // Canonical name (alias)
static constexpr uint8_t DNS_RECORD_SOA = 0x06; // Start of Authority
static constexpr uint8_t DNS_RECORD_PTR = 0x0C; // Domain name pointer
static constexpr uint8_t DNS_RECORD_MX = 0x0F; // Mail exchange
static constexpr uint8_t DNS_RECORD_TXT = 0x10; // Text record
static constexpr uint8_t DNS_RECORD_AAAA = 0x1C; // IPv6 host address
static constexpr uint8_t DNS_RECORD_SRV = 0x21; // Service location
static constexpr uint8_t DNS_RECORD_OPT = 0x29; // EDNS options
static constexpr uint8_t DNS_RECORD_NSEC = 0x2F; // Next Secure record
static constexpr uint8_t DNS_RECORD_ANY = 0xFF; // Any type (query only)
static constexpr uint8_t DNS_CACHE_FLUSH = 0x80; // Flag to tell others to flush cached entries
static constexpr uint8_t DNS_CACHE_NO_FLUSH = 0x00; // Normal caching behavior
static constexpr uint8_t DNS_CLASS_IN = 0x01; // Internet class
static constexpr uint8_t DNS_COMPRESS_MARK = 0xC0; // Marker for compressed names
static constexpr uint16_t DNS_TXT_EMPTY_LENGTH = 0x0001; // Length for empty TXT
static constexpr uint8_t DNS_TXT_EMPTY_CONTENT = 0x00; // Single null byte
// CONSTANTS
static constexpr size_t DNS_LABEL_LENGTH_MAX = 63; // Maximum length of a DNS label section
static constexpr size_t DNS_SERVICE_LENGTH_MAX = 100; // Maximum number of services
static constexpr size_t DNS_PACKET_LENGTH_MAX = 9000; // Maximum size of DNS packet
static constexpr size_t DNS_PACKET_LENGTH_SAFE = 1410; // Safe size of DNS packet
static constexpr size_t DNS_RECORD_HEADER_SIZE = 10; // Type(2) + Class(2) + TTL(4) + Length(2)
static constexpr size_t DNS_SRV_DETAILS_SIZE = 6; // Priority(2) + Weight(2) + Port(2)
static constexpr uint32_t DNS_PROBE_WAIT_MS = 250; // Wait time between probes
static constexpr size_t DNS_PROBE_COUNT = 3; // Number of probes
// -----------------------------------------------------------------------------------------------
enum class DNSRecordUniqueness {
Unique, // A, AAAA, SRV records
Shared, // PTR records
Contextual // TXT records - unique when with SRV
};
static inline uint8_t _configureCacheFlush (const DNSRecordUniqueness uniqueness, const bool isProbing = false) {
if (isProbing)
return DNS_CACHE_NO_FLUSH;
return (uniqueness == DNSRecordUniqueness::Unique || uniqueness == DNSRecordUniqueness::Contextual) ? DNS_CACHE_FLUSH : DNS_CACHE_NO_FLUSH;
}
static inline uint32_t _configureTTL (const DNSRecordUniqueness uniqueness, const MDNS::TTLConfig &ttls, const uint32_t ttl) {
return ttl == 0 ? 0 : (uniqueness == DNSRecordUniqueness::Shared ? std::min (ttl, ttls.shared_max) : ttl);
}
// -----------------------------------------------------------------------------------------------
enum class DNSSection {
Query = 1 << 0,
Answer = 1 << 1,
Authority = 1 << 2,
Additional = 1 << 3,
All = Query | Answer | Authority | Additional
};
static constexpr DNSSection operator| (const DNSSection a, const DNSSection b) {
return static_cast<DNSSection> (static_cast<int> (a) | static_cast<int> (b));
}
static constexpr DNSSection operator& (const DNSSection a, const DNSSection b) {
return static_cast<DNSSection> (static_cast<int> (a) & static_cast<int> (b));
}
static DNSSection getSection (const size_t i, const size_t qd, const size_t an, const size_t ns) {
if (i < qd)
return DNSSection::Query;
if (i < an)
return DNSSection::Answer;
if (i < ns)
return DNSSection::Authority;
return DNSSection::Additional;
}
__attribute__ ((unused)) static const char *getSectionName (const DNSSection section) {
switch (section) {
case DNSSection::Query :
return "query";
case DNSSection::Answer :
return "answer";
case DNSSection::Authority :
return "authority";
default :
return "additional";
}
}
// -----------------------------------------------------------------------------------------------
// clang-format off
__attribute__((unused)) static String parseDNSType(const uint16_t type) {
switch (type) {
// Standard DNS types
case 0x0001: return "A"; // IPv4 host address
case 0x0002: return "NS"; // Authoritative name server
case 0x0005: return "CNAME"; // Canonical name for an alias
case 0x0006: return "SOA"; // Start of authority record
case 0x000C: return "PTR"; // Domain name pointer
case 0x000D: return "HINFO"; // Host information
case 0x000F: return "MX"; // Mail exchange
case 0x0010: return "TXT"; // Text strings
case 0x001C: return "AAAA"; // IPv6 host address
case 0x0021:
return "SRV"; // Service locator
// EDNS and Security
case 0x0029: return "OPT"; // EDNS options (RFC 6891)
case 0x002B: return "DS"; // Delegation signer
case 0x002E: return "RRSIG"; // DNSSEC signature
case 0x002F: return "NSEC"; // Next secure record
case 0x0030: return "DNSKEY"; // DNS public key
case 0x0032: return "NSEC3"; // NSEC version 3
case 0x0033:
return "NSEC3PARAM"; // NSEC3 parameters
// Modern Extensions
case 0x0034: return "TLSA"; // TLS cert association
case 0x0100: return "CAA"; // Cert authority authorization
case 0x0101:
return "DHCID"; // DHCP identifier
// Special Types
case 0x00F9: return "TKEY"; // Transaction key
case 0x00FA: return "TSIG"; // Transaction signature
case 0x00FB: return "DNSKEY_ALT"; // Alternative DNSKEY
case 0x00FC: return "RRSIG_ALT"; // Alternative RRSIG
case 0x00FE: return "AXFR"; // Zone transfer
case 0x00FF:
return "ANY"; // Match any type
// Experimental/Local Use (RFC 6762)
case 0xFF00: return "LLQ"; // Long-lived query
case 0xFF01: return "ULLQ"; // Update leases
case 0xFF02: return "PRIVATE1"; // Private use
case 0xFF03:
return "PRIVATE2"; // Private use
// Meta Queries (RFC 6763)
case 0xFF1F: return "SERVICE_TYPE_ENUM"; // Service type enumeration
case 0xFF20: return "SERVICE_PORT"; // Service port
case 0xFF21: return "SERVICE_TXT"; // Service text
case 0xFF22: return "SERVICE_TARGET"; // Service target host
default:
{
String result = "Unknown(" + String(type, HEX) + ")";
if (type >= 0xFFF0)
result += "/Reserved";
else if (type >= 0xFF00)
result += "/Local";
return result;
}
}
}
__attribute__((unused)) static String parseDNSFlags(const uint8_t flagsByte) {
if (flagsByte & 0x80) return "FLUSH";
return String("NO_FLUSH");
}
__attribute__((unused)) static String parseDNSClassOrEDNS(const uint8_t classByte1, const uint8_t classByte2, const uint16_t type) {
if (type == 0x0029) { // OPT record
const uint16_t payloadSize = (static_cast<uint16_t>(classByte1) << 8) | classByte2;
String result = "UDP_SIZE(" + String(payloadSize) + ")";
if (payloadSize < 512)
result += "/Small";
else if (payloadSize > 1432)
result += "/Large";
return result;
}
switch (classByte2) {
case 0x01: return "IN";
case 0x02: return "CS";
case 0x03: return "CH";
case 0x04: return "HS";
case 0xFE: return "NONE";
case 0xFF: return "ANY";
default: return "Unknown(" + String(classByte2, HEX) + ")";
}
}
// clang-format on
__attribute__ ((unused)) static String parseHeader (const Header &h) {
static const char *opcodes [] = { "QUERY", "IQUERY", "STATUS", "RESERVED", "NOTIFY", "UPDATE", "UNK6", "UNK7", "UNK8", "UNK9", "UNK10", "UNK11", "UNK12", "UNK13", "UNK14", "UNK15" };
static const char *rcodes [] = { "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED", "YXDOMAIN", "YXRRSET", "NXRRSET", "NOTAUTH", "NOTZONE", "UNK11", "UNK12", "UNK13", "UNK14", "UNK15" };
return join ({ "ID=0x" + String (h.xid, HEX),
"QR=" + String (h.queryResponse),
"OPCODE=" + String (opcodes [h.opCode]),
"AA=" + String (h.authoritiveAnswer),
"TC=" + String (h.truncated),
"RD=" + String (h.recursionDesired),
"RA=" + String (h.recursionAvailable),
"Z=" + String (h.zReserved),
"AD=" + String (h.authenticatedData),
"CD=" + String (h.checkingDisabled),
"RCODE=" + String (rcodes [h.responseCode]),
"QDCOUNT=" + String (h.queryCount),
"ANCOUNT=" + String (h.answerCount),
"NSCOUNT=" + String (h.authorityCount),
"ARCOUNT=" + String (h.additionalCount) },
",");
}
__attribute__ ((unused)) static String parseControl (const uint8_t ctrl [4]) {
const uint16_t type = (ctrl [0] << 8) | ctrl [1];
return parseDNSType (type) + "/" + parseDNSFlags (ctrl [2]) + "/" + parseDNSClassOrEDNS (ctrl [2], ctrl [3], type); // Pass both bytes
}
__attribute__ ((unused)) static void parsePacket (const char *label, const uint8_t *data, const size_t size, const size_t offs = 0) {
static constexpr const char lookup [] = "0123456789ABCDEF";
char buffer [(16 * 3 + 2) + 1 + (16 * 1 + 2) + 1];
DEBUG_PRINTF (" %04X: <%s> : %s\n", size, label, parseHeader (*(reinterpret_cast<const Header *> (data))).c_str ());
// should annotate the RHS of the output with some of the details, e.g. using the parse functions above
for (size_t i = 0; i < size; i += 16) {
char *position = buffer;
for (size_t j = 0; j < 16; j++) {
if ((i + j) < size)
*position++ = lookup [(data [i + j] >> 4) & 0x0F], *position++ = lookup [(data [i + j] >> 0) & 0x0F], *position++ = ' ';
else
*position++ = ' ', *position++ = ' ', *position++ = ' ';
if ((j + 1) % 8 == 0)
*position++ = ' ';
}
*position++ = ' ';
for (size_t j = 0; j < 16; j++) {
if ((i + j) < size)
*position++ = isprint (data [i + j]) ? (char) data [i + j] : '.';
else
*position++ = ' ';
if ((j + 1) % 8 == 0)
*position++ = ' ';
}
*position++ = '\0';
DEBUG_PRINTF (" %04X: %s\n", offs + i, buffer);
}
}
// -----------------------------------------------------------------------------------------------
static const IPAddress MDNS_ADDR_MULTICAST (224, 0, 0, 251);
static constexpr uint16_t MDNS_PORT = 5353;
static constexpr uint8_t calcSupportedRecordTypeByte (uint8_t type) {
return (type - 1) / 8;
}
static constexpr uint8_t calcSupportedRecordTypeMask (uint8_t type) {
return 1 << (7 - ((type - 1) % 8));
}
static constexpr struct SupportedRecordType {
uint8_t type;
uint8_t byte;
uint8_t mask;
} SupportedRecordTypes [] = {
{ DNS_RECORD_A, calcSupportedRecordTypeByte (DNS_RECORD_A), calcSupportedRecordTypeMask (DNS_RECORD_A) },
{ DNS_RECORD_PTR, calcSupportedRecordTypeByte (DNS_RECORD_PTR), calcSupportedRecordTypeMask (DNS_RECORD_PTR) },
{ DNS_RECORD_TXT, calcSupportedRecordTypeByte (DNS_RECORD_TXT), calcSupportedRecordTypeMask (DNS_RECORD_TXT) },
{ DNS_RECORD_SRV, calcSupportedRecordTypeByte (DNS_RECORD_SRV), calcSupportedRecordTypeMask (DNS_RECORD_SRV) },
{ DNS_RECORD_NSEC, calcSupportedRecordTypeByte (DNS_RECORD_NSEC), calcSupportedRecordTypeMask (DNS_RECORD_NSEC) }
};
static constexpr const char *protocolPostfix (const MDNS::Service::Protocol proto) {
switch (proto) {
case MDNS::Service::Protocol::TCP :
return "._tcp" TLD;
case MDNS::Service::Protocol::UDP :
return "._udp" TLD;
default :
return "";
}
};
static constexpr bool OPT_DETAILED_CHECKS = true;
static constexpr uint16_t OPT_DETAILED_CHECKS_REASONABLE_COUNT = 100;
static inline String makeReverseArpaName (const IPAddress &addr) {
return String (addr [3]) + "." + String (addr [2]) + "." + String (addr [1]) + "." + String (addr [0]) + ".in-addr.arpa";
}
// -----------------------------------------------------------------------------------------------
struct DNSBitmap {
static constexpr size_t BITMAP_SIZE = 32;
static constexpr uint8_t NSEC_WINDOW_BLOCK_0 = 0x00;
static constexpr uint8_t INITIAL_LENGTH = 2;
std::array<uint8_t, 2 + BITMAP_SIZE> _data;
inline size_t size () const {
return static_cast<size_t> (_data [1]);
}
inline const uint8_t *data () const {
return _data.data ();
}
DNSBitmap (const std::initializer_list<uint8_t> &types = {}) :
_data {} {
_data [0] = NSEC_WINDOW_BLOCK_0;
_data [1] = INITIAL_LENGTH;
for (const auto &type : types)
addType (type);
}
DNSBitmap &addType (const uint8_t type) {
for (const auto &rt : SupportedRecordTypes)
if (rt.type == type) {
const uint8_t offs = 2 + rt.byte;
_data [offs] |= rt.mask;
if (_data [1] < (offs + 1))
_data [1] = (offs + 1);
}
return *this;
}
};
// -----------------------------------------------------------------------------------------------
class Base64 {
private:
static constexpr const char encodingTable [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static constexpr size_t BITS_PER_CHAR = 6; // Base64 uses 6 bits per character
static constexpr size_t BITS_PER_BYTE = 8; // Input uses 8 bits per byte
static constexpr size_t OUTPUT_GROUP_SIZE = 4; // Output characters per group
static constexpr size_t INPUT_GROUP_SIZE = 3; // Input bytes per group
static constexpr char PADDING_CHAR = '='; // Character used for padding
static constexpr uint8_t MASK_6BITS = 0x3F; // Mask for 6 bits (2^6 - 1)
static constexpr uint8_t MASK_4BITS = 0x0F; // Mask for 4 bits (2^4 - 1)
static constexpr uint8_t MASK_2BITS = 0x03; // Mask for 2 bits (2^2 - 1)
static constexpr uint8_t MASK_6BITS_NOT = 0xC0;
static constexpr uint8_t MASK_4BITS_NOT = 0xF0;
public:
static size_t length (const size_t inputLength) {
return OUTPUT_GROUP_SIZE * ((inputLength + INPUT_GROUP_SIZE - 1) / INPUT_GROUP_SIZE);
}
static size_t encode (const uint8_t *input, const size_t inputLength, char *output, const size_t outputLength) {
if (outputLength < OUTPUT_GROUP_SIZE * ((inputLength + INPUT_GROUP_SIZE - 1) / INPUT_GROUP_SIZE))
return 0;
size_t inputIndex, outputIndex = 0;
for (inputIndex = 0; inputIndex + INPUT_GROUP_SIZE - 1 < inputLength; inputIndex += INPUT_GROUP_SIZE) {
output [outputIndex++] = encodingTable [((input [inputIndex + 0] >> 2) & MASK_6BITS)];
output [outputIndex++] = encodingTable [((input [inputIndex + 0] & MASK_2BITS) << 4) | ((input [inputIndex + 1] & MASK_6BITS_NOT) >> 4)];
output [outputIndex++] = encodingTable [((input [inputIndex + 1] & MASK_4BITS) << 2) | ((input [inputIndex + 2] & MASK_6BITS_NOT) >> 6)];
output [outputIndex++] = encodingTable [((input [inputIndex + 2] & MASK_6BITS))];
}
if (inputIndex < inputLength) {
output [outputIndex++] = encodingTable [(input [inputIndex] >> 2) & MASK_6BITS];
if (inputIndex == (inputLength - 1)) {
output [outputIndex++] = encodingTable [(input [inputIndex] & MASK_2BITS) << 4];
output [outputIndex++] = PADDING_CHAR;
} else {
output [outputIndex++] = encodingTable [((input [inputIndex + 0] & MASK_2BITS) << 4) | ((input [inputIndex + 1] & MASK_6BITS_NOT) >> 4)];
output [outputIndex++] = encodingTable [((input [inputIndex + 1] & MASK_4BITS) << 2)];
}
output [outputIndex++] = PADDING_CHAR;
}
output [outputIndex] = '\0';
return outputIndex;
}
};
static inline bool isValidDNSKeyChar (const char c) {
return (c >= 0x20 && c <= 0x7E) && c != '='; // RFC 6763 Section 6.4
}
bool MDNSTXT::validate (const String &key) const {
if (key.isEmpty () || key.length () > KEY_LENGTH_MAX)
return false;
if (key.charAt (0) == '=')
return false;
return std::all_of (key.begin (), key.end (), isValidDNSKeyChar);
}
bool MDNSTXT::insert (const String &key, const void *value, const size_t length, const bool is_binary) {
if (! validate (key))
return false;
auto it = std::find_if (_entries.begin (), _entries.end (), [&key] (const auto &e) {
return e.key.equalsIgnoreCase (key);
});
if (it != _entries.end ()) {
it->value.assign (static_cast<const uint8_t *> (value), static_cast<const uint8_t *> (value) + length);
it->binary = is_binary;
} else
_entries.push_back ({ key, std::vector<uint8_t> (static_cast<const uint8_t *> (value), static_cast<const uint8_t *> (value) + length), is_binary });
length_valid = false;
return true;
}
uint16_t MDNSTXT::length () const {
if (! length_valid) {
cached_length = std::accumulate (_entries.begin (), _entries.end (), 0U, [] (size_t sum, const auto &entry) {
const size_t value_len = entry.value.empty () ? 0 : (entry.binary ? Base64::length (entry.value.size ()) : entry.value.size ());
return sum + 1 + entry.key.length () + (value_len ? value_len + 1 : 0); // length byte
});
length_valid = true;
}
return cached_length;
}
String MDNSTXT::toString () const {
String result;
for (const auto &entry : _entries) {
result += (result.isEmpty () ? "" : ",") + entry.key;
String encoded;
encoded.reserve (TOTAL_LENGTH_MAX + 1);
if (! entry.value.empty ()) {
encoded += '=';
if (entry.binary) {
std::vector<char> buffer (Base64::length (entry.value.size ()) + 1);
if (Base64::encode (entry.value.data (), entry.value.size (), buffer.data (), buffer.size ()))
encoded += String (buffer.data ());
} else
encoded += String (reinterpret_cast<const char *> (entry.value.data ()), entry.value.size ());
}
}
return result;
}
// -----------------------------------------------------------------------------------------------
static inline size_t _sizeofDNSName (const String &name);
static inline size_t _sizeofServiceRecord (const MDNS::Service &service, const String &fqhn);
static inline size_t _sizeofCompleteRecord (const MDNS::Services &services, const MDNS::ServiceTypes &serviceTypes, const String &fqhn);
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#ifdef DEBUG_MDNS_UDP_READ
static const size_t udp_read_buffer_maximum__global = DNS_PACKET_LENGTH_MAX;
static size_t _udp_read_buffer_length__global = 0;
static uint8_t _udp_read_buffer_content__global [udp_read_buffer_maximum__global];
#define DEBUG_MDNS_UDP_READ_RESET() _udp_read_buffer_length__global = 0
#define DEBUG_MDNS_UDP_READ_DUMP() \
if (_udp_read_buffer_length__global > 0) \
parsePacket ("UDP_READ", _udp_read_buffer_content__global, _udp_read_buffer_length__global);
#define DEBUG_MDNS_UDP_READ_BYTE(x) \
if (_udp_read_buffer_length__global < udp_read_buffer_maximum__global) \
_udp_read_buffer_content__global [_udp_read_buffer_length__global++] = x;
#else
#define DEBUG_MDNS_UDP_READ_RESET()
#define DEBUG_MDNS_UDP_READ_DUMP()
#define DEBUG_MDNS_UDP_READ_BYTE(x)
#endif
static UDP *_udp_read_handler__global = nullptr;
static uint16_t _udp_read_offset__global = 0, _udp_read_length__global = 0;
#define UDP_READ_START() _udp->beginMulticast (MDNS_ADDR_MULTICAST, MDNS_PORT)
#define UDP_READ_STOP() _udp->stop ()
#define UDP_READ_BEGIN(u) \
do { \
_udp_read_handler__global = u; \
_udp_read_offset__global = 0; \
_udp_read_length__global = _udp_read_handler__global->parsePacket (); \
DEBUG_MDNS_UDP_READ_RESET (); \
} while (0)
#define UDP_READ_END() \
do { \
_udp_read_handler__global->flush (); \
DEBUG_MDNS_UDP_READ_DUMP (); \
} while (0)
#define UDP_READ_AVAILABLE() (_udp_read_length__global != static_cast<uint16_t> (0))
#define UDP_READ_BYTE_OR_FAIL(t, x, y) \
{ \
if (_udp_read_offset__global >= _udp_read_length__global) \
y; \
const int _udp_byte = _udp_read_handler__global->read (); \
if (_udp_byte < 0) \
y; \
x = static_cast<t> (_udp_byte); \
_udp_read_offset__global++; \
DEBUG_MDNS_UDP_READ_BYTE (static_cast<uint8_t> (_udp_byte)); \
}
#define UDP_SKIP_BYTE_OR_FAIL(y) \
{ \
if (_udp_read_offset__global >= _udp_read_length__global) \
y; \
const int _udp_byte = _udp_read_handler__global->read (); \
if (_udp_byte < 0) \
y; \
_udp_read_offset__global++; \
DEBUG_MDNS_UDP_READ_BYTE (static_cast<uint8_t> (_udp_byte)); \
}
#define UDP_READ_PEEK() _udp_read_handler__global->peek ()
#define UDP_READ_LENGTH() _udp_read_length__global
#define UDP_READ_OFFSET() _udp_read_offset__global
#define UDP_READ_PEER_ADDR() _udp_read_handler__global->remoteIP ()
#define UDP_READ_PEER_PORT() _udp_read_handler__global->remotePort ()
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#ifdef DEBUG_MDNS_UDP_WRITE
static const size_t _udp_write_buffer_maximum__global = DNS_PACKET_LENGTH_MAX;
static size_t _udp_write_buffer_length__global = 0;
static uint8_t _udp_write_buffer_content__global [_udp_write_buffer_maximum__global];
#define DEBUG_MDNS_UDP_WRITE_RESET() _udp_write_buffer_length__global = 0
#define DEBUG_MDNS_UDP_WRITE_DUMP() \
if (_udp_write_buffer_length__global > 0) \
parsePacket ("UDP_WRITE", _udp_write_buffer_content__global, _udp_write_buffer_length__global);
#define DEBUG_MDNS_UDP_WRITE_BYTE(x) \
if (_udp_write_buffer_length__global < _udp_write_buffer_maximum__global) \
_udp_write_buffer_content__global [_udp_write_buffer_length__global++] = x;
#define DEBUG_MDNS_UDP_WRITE_DATA(x, y) \
for (auto yy = 0; yy < y; yy++) { \
if (_udp_write_buffer_length__global < _udp_write_buffer_maximum__global) \
_udp_write_buffer_content__global [_udp_write_buffer_length__global++] = x [yy]; \
}
#else
#define DEBUG_MDNS_UDP_WRITE_RESET()
#define DEBUG_MDNS_UDP_WRITE_DUMP()
#define DEBUG_MDNS_UDP_WRITE_BYTE(x)
#define DEBUG_MDNS_UDP_WRITE_DATA(x, y)
#endif
static uint16_t _udp_write_offset__global = 0;
#define UDP_WRITE_BEGIN() \
do { \
_udp->beginPacket (MDNS_ADDR_MULTICAST, MDNS_PORT); \
_udp_write_offset__global = 0; \
DEBUG_MDNS_UDP_WRITE_RESET (); \
} while (0)
#define UDP_WRITE_END() \
do { \
_udp->endPacket (); \
DEBUG_MDNS_UDP_WRITE_DUMP (); \
} while (0)
#define UDP_WRITE_BYTE(x) \
do { \
_udp->write (x); \
_udp_write_offset__global++; \
DEBUG_MDNS_UDP_WRITE_BYTE ((x)); \
} while (0)
#define UDP_WRITE_DATA(x, y) \
do { \
_udp->write (x, y); \
_udp_write_offset__global += (y); \
DEBUG_MDNS_UDP_WRITE_DATA ((x), (y)); \
} while (0)
#define UDP_WRITE_OFFSET() _udp_write_offset__global
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
// a mess mixed with the specific handlers, this and the Responder need more rework
template <typename Handler>
struct UDP_READ_PACKET_CLASS {
Handler &_handler;
const Header &_header;
UDP_READ_PACKET_CLASS (Handler &handler, const Header &header) :
_handler (handler),
_header (header) { };
~UDP_READ_PACKET_CLASS () {
UDP_READ_END ();
}
bool _extractLabels (const DNSSection section, uint16_t *consumed = nullptr) {
uint8_t size = 0, comp;
uint16_t used = 0;
do {
const uint16_t offset = UDP_READ_OFFSET ();
UDP_READ_BYTE_OR_FAIL (uint8_t, size, break);
used++;
if ((size & DNS_COMPRESS_MARK) == DNS_COMPRESS_MARK) {
UDP_READ_BYTE_OR_FAIL (uint8_t, comp, return false);
used++;
const uint16_t offs = ((static_cast<uint16_t> (size) & ~DNS_COMPRESS_MARK) << 8) | static_cast<uint16_t> (comp);
_handler.process_iscompressed (offs, section, offset);
} else if (size > 0) {
String name;
name.reserve (size + 1);
for (auto z = 0; z < size; z++) {
char c;
UDP_READ_BYTE_OR_FAIL (char, c, return false);
used++;
name += c;
}
_handler.process_nocompressed (name, section, offset);
}
} while (size > 0 && size <= DNS_LABEL_LENGTH_MAX);
if (consumed != nullptr)
(*consumed) += used;
return true;
}
bool _extractControl (uint8_t control [4]) {
for (auto z = 0; z < 4; z++)
UDP_READ_BYTE_OR_FAIL (uint8_t, control [z], return false);
return true;
}
bool _passoverTTL (void) {
for (auto i = 0; i < 4; i++)
UDP_SKIP_BYTE_OR_FAIL (return false);
return true;
}
bool _extractLength (uint16_t *length) {
uint8_t b1, b2;
UDP_READ_BYTE_OR_FAIL (uint8_t, b1, return false);
UDP_READ_BYTE_OR_FAIL (uint8_t, b2, return false);
(*length) = (static_cast<uint16_t> (b1) << 8) | static_cast<uint16_t> (b2);
return true;
}
bool _passbySRVDetails (uint16_t *consumed = nullptr) {
for (auto i = 0; i < 6; i++) // priority, weight, port
UDP_SKIP_BYTE_OR_FAIL (return false);
if (consumed)
(*consumed) += 6;
return true;
}
bool _passbyMXDetails (uint16_t *consumed = nullptr) {
for (auto i = 0; i < 2; i++) // preference
UDP_SKIP_BYTE_OR_FAIL (return false);
if (consumed)
(*consumed) += 2;
return true;
}
bool _passbySOADetails (uint16_t *consumed = nullptr) {
for (auto i = 0; i < 20; i++) // 5 x 32 bit values
UDP_SKIP_BYTE_OR_FAIL (return false);
if (consumed)
(*consumed) += 20;
return true;
}
bool process (void) {
_handler.begin ();
const size_t qd = _header.queryCount, an = qd + _header.answerCount, ns = an + _header.authorityCount, ad = ns + _header.additionalCount;
for (size_t i = 0; i < ad; i++) {
const DNSSection section = getSection (i, qd, an, ns);
DEBUG_PRINTF ("MDNS: packet: %s[%d/%u]: ", getSectionName (section), i + 1, ad);
_handler.process_begin (section, UDP_READ_OFFSET ());
if (! _extractLabels (section))
return false;
uint8_t control [4];
if (! _extractControl (control))
return false;
const uint16_t type = (static_cast<uint16_t> (control [0]) << 8) | static_cast<uint16_t> (control [1]);
_handler.process_update (section, control);
const String name = _handler.name ();
DEBUG_PRINTF ("<%s> [%s] (%s)\n", name.c_str (), parseControl (control).c_str (), getSectionName (section));
if (section != DNSSection::Query) {
if (! _passoverTTL ())
return false;
uint16_t length, consumed = 0;
if (! _extractLength (&length))
return false;
switch (type) {
case DNS_RECORD_CNAME : // possible
case DNS_RECORD_NS : // unlikely
case DNS_RECORD_PTR : // typical
case DNS_RECORD_NSEC : // typical
if (consumed < length && ! _extractLabels (section, &consumed)) // target
return false;
break;
case DNS_RECORD_SRV : // typical
if (consumed < length && ! _passbySRVDetails (&consumed))
return false;
if (consumed < length && ! _extractLabels (section, &consumed)) // target
return false;
break;
case DNS_RECORD_MX : // possible
if (consumed < length && ! _passbyMXDetails (&consumed))
return false;
if (consumed < length && ! _extractLabels (section, &consumed)) // exchanger
return false;
break;
case DNS_RECORD_SOA : // unlikely
if (consumed < length && ! _extractLabels (section, &consumed)) // MNAME
return false;
if (consumed < length && ! _extractLabels (section, &consumed)) // RNAME
return false;
if (consumed < length && ! _passbySOADetails (&consumed))
return false;
break;
}
while (consumed++ < length)
UDP_SKIP_BYTE_OR_FAIL (return false);
}
if (section != DNSSection::Query && type != DNS_RECORD_OPT && name.isEmpty ())
DEBUG_PRINTF ("\n**** EMPTY ****\n");
_handler.process_end (section, UDP_READ_OFFSET ());
}
_handler.end ();
return true;
}
};
struct NameCollector {
MDNS &_mdns;
const Header &_header;
//
using LabelOffset = std::pair<String, uint16_t>;
using Labels = std::vector<LabelOffset>;
struct Name {
DNSSection section;
Labels labels;
};
using Names = std::vector<Name>;
Names _names;
//
String _uncompress (const size_t target) const {
for (const auto &n : _names)
for (const auto &[label, offset] : n.labels)
if (target >= offset && target < (offset + label.length ()))
return (target == offset) ? label : label.substring (target - offset);
DEBUG_PRINTF ("*** WARNING: could not uncompress at %u ***\n", target);
return String ();
}
String _name (const Labels &labels) const {
return labels.empty () ? String () : std::accumulate (labels.begin (), labels.end (), String (), [] (const String &acc, const LabelOffset &label) {
return acc.isEmpty () ? label.first : acc + "." + label.first;
});
}
//
String name () const {
return _names.empty () ? String () : _name (_names.back ().labels);
}
std::vector<String> names (const DNSSection section = DNSSection::All) const {
std::vector<String> names;
for (const auto &n : _names)
if ((n.section & section) == n.section)
names.push_back (_name (n.labels));
return names;
}
virtual void begin () { }
virtual void end () { }
void process_iscompressed (const uint16_t offs, const DNSSection, const uint16_t current) {
_names.back ().labels.push_back (LabelOffset (_uncompress (offs), current));
}
void process_nocompressed (const String &label, const DNSSection, const uint16_t current) {
_names.back ().labels.push_back (LabelOffset (label, current));
}
void process_begin (const DNSSection section, const uint16_t offset) {
_names.push_back ({ .section = section, .labels = Labels () });
}
void process_update (const DNSSection, const uint8_t [4]) {
}
void process_end (const DNSSection, const uint16_t) {
}
NameCollector (MDNS &mdns, const Header &header) :
_mdns (mdns),
_header (header) { };
};
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
MDNS::MDNS (UDP &udp) :
_udp (&udp) {
}
MDNS::~MDNS () {
stop ();
}
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
MDNS::Status MDNS::begin (void) {
DEBUG_PRINTF ("MDNS: begin\n");
return Status::Success;
}
MDNS::Status MDNS::start (const IPAddress &addr, const String &name, const bool checkForConflicts) {
_addr = addr;
_name = name.isEmpty () ? getMacAddressBase () : name;
_fqhn = name + TLD;
_arpa = makeReverseArpaName (_addr);
if (! _sizeofDNSName (_name)) {
DEBUG_PRINTF ("MDNS: start: failed, invalid name %s\n", _name.c_str ());
return Status::InvalidArgument;
}
Status status = Status::Success;
if (! _enabled) {
if (! UDP_READ_START ())
status = Status::Failure;
else
_enabled = true;
}
if (status != Status::Success)
DEBUG_PRINTF ("MDNS: start: failed _udp->beginMulticast error=%s, not active\n", toString (status).c_str ());
else {
DEBUG_PRINTF ("MDNS: start: active ip=%s, name=%s\n", IPAddress (_addr).toString ().c_str (), _fqhn.c_str ());
if (checkForConflicts) {
for (auto i = 0; i < DNS_PROBE_COUNT; i++) {
_messageSend (XID_DEFAULT, PacketTypeProbe);
delay (DNS_PROBE_WAIT_MS);
}
delay (DNS_PROBE_WAIT_MS);
}
_messageSend (XID_DEFAULT, PacketTypeCompleteRecord);
}
return status;
}
MDNS::Status MDNS::stop (void) {
if (_enabled) {
DEBUG_PRINTF ("MDNS: stop\n");
// XXX: should send multiple messages 2 seconds apart
_messageSend (XID_DEFAULT, PacketTypeCompleteRelease);
UDP_READ_STOP ();
_enabled = false;
}
return Status::Success;
}
MDNS::Status MDNS::process (void) {
Status status = Status::Success;
if (_enabled) {
auto count = 0;
do {
count++;
} while ((status = _messageRecv ()) == Status::Success);
if (status == Status::NameConflict)
return _conflicted ();
if (status != Status::Success && status != Status::TryLater)
DEBUG_PRINTF ("MDNS: process: failed _messageRecv error=%s\n", toString (status).c_str ());
else if (status == Status::Success || status == Status::TryLater)
if ((status = _announce ()) != Status::Success)
DEBUG_PRINTF ("MDNS: process: failed _announce error=%s\n", toString (status).c_str ());
if (count > 1)
DEBUG_PRINTF ("MDNS: process [%d]\n", count - 1);
}
return status;
}
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
MDNS::Status MDNS::serviceRecordInsert (const Service::Protocol proto, const uint16_t port, const String &name, const Service::Config &config, const Service::TXT &text) {
DEBUG_PRINTF ("MDNS: serviceRecordInsert: proto=%s, port=%u, name=%s, text.length=%d,text=[%s]\n", Service::toString (proto).c_str (), port, name.c_str (), text.length (), text.toString ().c_str ());
if (name.isEmpty () || port == 0 || (proto != Service::Protocol::TCP && proto != Service::Protocol::UDP))
return Status::InvalidArgument;
if (_services.size () >= DNS_SERVICE_LENGTH_MAX)
return Status::InvalidArgument;
if (! _sizeofDNSName (name))
return Status::InvalidArgument;
if (std::any_of (text.entries ().begin (), text.entries ().end (), [] (const auto &it) {
return it.key.length () > Service::TXT::TOTAL_LENGTH_MAX;
}))
return Status::InvalidArgument;