-
Notifications
You must be signed in to change notification settings - Fork 2
/
ESP8266mDNS Control.cpp
executable file
·1756 lines (1534 loc) · 90.3 KB
/
ESP8266mDNS Control.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
/*
* ESP8266mDNS Control.cpp
*
* License (MIT license):
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <arch/cc.h>
#include <HardwareSerial.h>
#include <IPAddress.h>
#include <lwip/ip_addr.h>
#include <WString.h>
#include <cstdint>
#include "ESP8266mDNS.h"
/*
* ESP8266mDNS Control.cpp
*/
extern "C" {
#include "user_interface.h"
}
#include "lwip/prot/dns.h" // DNS_RRTYPE_xxx, DNS_MQUERY_PORT
#include "ESP8266mDNS Priv.h"
/*
* namespace LEA_MDNSResponder
*/
namespace LEA_MDNSResponder {
/**
* CONTROL
*/
/**
* MAINTENANCE
*/
/*
* MDNSResponder::_process
*
* Run the MDNS process. Should be called in every 'loop'.
*
*/
bool MDNSResponder::_process(bool p_bUserContext) {
bool bResult = true;
if ((m_pUDPContext) && // UDPContext available AND
(m_pUDPContext->next())) { // has content
//DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _update: Calling _parseMessage\n")););
bResult = _parseMessage();
if (p_bUserContext) {
yield();
}
//DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parsePacket %s\n"), (bResult ? "succeeded" : "FAILED")););
}
else if (p_bUserContext) {
if (bResult) { // Probing
bResult = _updateProbeStatus();
}
if (bResult) { // Service query cache check
bResult = _checkServiceQueryCache();
}
}
return bResult;
}
/*
* MDNSResponder::_restart
*/
bool MDNSResponder::_restart(void) {
return ((_resetProbeStatus(true)) && // Stop and restart probing
(_allocUDPContext())); // Restart UDP
}
/**
* RECEIVING
*/
/*
* MDNSResponder::_parseMessage
*/
bool MDNSResponder::_parseMessage(void) {
DEBUG_EX_INFO(
unsigned long ulStartTime = millis();
unsigned uStartMemory = ESP.getFreeHeap();
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseMessage (Time: %lu ms, heap: %u bytes, from %s(%u), to %s(%u))\n"), ulStartTime, uStartMemory,
IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), m_pUDPContext->getRemotePort(),
IPAddress(m_pUDPContext->getDestAddress()).toString().c_str(), m_pUDPContext->getLocalPort());
);
//DEBUG_EX_INFO(_udpDump(););
bool bResult = false;
stcMDNS_MsgHeader header;
if (_readMDNSMsgHeader(header)) {
if (0 == header.m_4bOpcode) { // A standard query
if (header.m_1bQR) { // Received a response -> answers to a query
//DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseMessage: Reading answers: ID:%u, Q:%u, A:%u, NS:%u, AR:%u\n"), header.m_u16ID, header.m_u16QDCount, header.m_u16ANCount, header.m_u16NSCount, header.m_u16ARCount););
bResult = _parseResponse(header);
}
else { // Received a query (Questions)
//DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseMessage: Reading query: ID:%u, Q:%u, A:%u, NS:%u, AR:%u\n"), header.m_u16ID, header.m_u16QDCount, header.m_u16ANCount, header.m_u16NSCount, header.m_u16ARCount););
bResult = _parseQuery(header);
}
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseMessage: Received UNEXPECTED opcode:%u. Ignoring message!\n"), header.m_4bOpcode););
m_pUDPContext->flush();
}
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseMessage: FAILED to read header\n")););
m_pUDPContext->flush();
}
DEBUG_EX_INFO(
unsigned uFreeHeap = ESP.getFreeHeap();
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseMessage: Done (%s after %lu ms, ate %i bytes, remaining %u)\n\n"), (bResult ? "Succeeded" : "FAILED"), (millis() - ulStartTime), (uStartMemory - uFreeHeap), uFreeHeap);
);
return bResult;
}
/*
* MDNSResponder::_parseQuery
*
* Queries are of interest in two cases:
* 1. allow for tiebreaking while probing in the case of a race condition between two instances probing for
* the same name at the same time
* 2. provide answers to questions for our host domain or any presented service
*
* When reading the questions, a set of (planned) responses is created, eg. a reverse PTR question for the host domain
* gets an A (IP address) response, a PTR question for the _services._dns-sd domain gets a PTR (type) response for any
* registered service, ...
*
* As any mDNS responder should be able to handle 'legacy' queries (from DNS clients), this case is handled here also.
* Legacy queries have got only one (unicast) question and are directed to the local DNS port (not the multicast port).
*
* 1.
*/
bool MDNSResponder::_parseQuery(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader) {
bool bResult = true;
stcMDNSSendParameter sendParameter;
uint8_t u8HostOrServiceReplies = 0;
for (uint16_t qd=0; ((bResult) && (qd<p_MsgHeader.m_u16QDCount)); ++qd) {
stcMDNS_RRQuestion questionRR;
if ((bResult = _readRRQuestion(questionRR))) {
// Define host replies, BUT only answer queries after probing is done
u8HostOrServiceReplies =
sendParameter.m_u8HostReplyMask = ((ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus)
? _replyMaskForHost(questionRR.m_Header, 0)
: 0);
DEBUG_EX_INFO(if (u8HostOrServiceReplies) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Host reply needed %u\n"), u8HostOrServiceReplies); });
// Check tiebreak need for host domain
if (ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) {
bool bFullNameMatch = false;
if ((_replyMaskForHost(questionRR.m_Header, &bFullNameMatch)) &&
(bFullNameMatch)) {
// We're in 'probing' state and someone is asking for our host domain: this might be
// a race-condition: Two host with the same domain names try simutanously to probe their domains
// See: RFC 6762, 8.2 (Tiebraking)
// However, we're using a max. reduced approach for tiebreaking here: The higher IP-address wins!
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Possible race-condition for host domain detected while probing.\n")););
Serial.printf(F("[MDNSResponder] _parseQuery: Possible race-condition for host domain detected while probing.\n"));
m_HostProbeInformation.m_bTiebreakNeeded = true;
}
}
// Define service replies
for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) {
// Define service replies, BUT only answer queries after probing is done
uint8_t u8ReplyMaskForQuestion = ((ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus)
? _replyMaskForService(questionRR.m_Header, *pService, 0)
: 0);
u8HostOrServiceReplies |= (pService->m_u8ReplyMask |= u8ReplyMaskForQuestion);
DEBUG_EX_INFO(if (u8ReplyMaskForQuestion) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Service reply needed for (%s.%s.%s): %u (%s)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, u8ReplyMaskForQuestion, IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str()); } );
/*if ((u8ReplyMaskForQuestion) &&
(0 == os_strcmp("hap", pService->m_pcService))) {
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Service reply needed for (%s.%s.%s): %u (%s)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, u8ReplyMaskForQuestion, IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());
}*/
// Check tiebreak need for service domain
if (ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) {
bool bFullNameMatch = false;
if ((_replyMaskForService(questionRR.m_Header, *pService, &bFullNameMatch)) &&
(bFullNameMatch)) {
// We're in 'probing' state and someone is asking for this service domain: this might be
// a race-condition: Two services with the same domain names try simutanously to probe their domains
// See: RFC 6762, 8.2 (Tiebraking)
// However, we're using a max. reduced approach for tiebreaking here: The 'higher' SRV host wins!
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Possible race-condition for service domain %s.%s.%s detected while probing.\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
Serial.printf(F("[MDNSResponder] _parseQuery: Possible race-condition for service domain %s.%s.%s detected while probing.\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol);
pService->m_ProbeInformation.m_bTiebreakNeeded = true;
}
}
}
// Handle unicast and legacy specialities
// If only one question asks for unicast reply, the whole reply packet is send unicast
if (((DNS_MQUERY_PORT != m_pUDPContext->getRemotePort()) || // Unicast (maybe legacy) query OR
(questionRR.m_bUnicast)) && // Expressivly unicast query
(!sendParameter.m_bUnicast)) {
sendParameter.m_bUnicast = true;
sendParameter.m_bCacheFlush = false;
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Unicast response for %s!\n"), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str()););
if ((DNS_MQUERY_PORT != m_pUDPContext->getRemotePort()) && // Unicast (maybe legacy) query AND
(1 == p_MsgHeader.m_u16QDCount) && // Only one question AND
((sendParameter.m_u8HostReplyMask) || // Host replies OR
(u8HostOrServiceReplies))) { // Host or service replies available
// We're a match for this legacy query, BUT
// make sure, that the query comes from a local host
ip_info IPInfo_Local;
ip_info IPInfo_Remote;
if (((IPInfo_Remote.ip.addr = m_pUDPContext->getRemoteAddress())) &&
(((wifi_get_ip_info(SOFTAP_IF, &IPInfo_Local)) &&
(ip_addr_netcmp(&IPInfo_Remote.ip, &IPInfo_Local.ip, &IPInfo_Local.netmask))) || // Remote IP in SOFTAP's subnet OR
((wifi_get_ip_info(STATION_IF, &IPInfo_Local)) &&
(ip_addr_netcmp(&IPInfo_Remote.ip, &IPInfo_Local.ip, &IPInfo_Local.netmask))))) { // Remote IP in STATION's subnet
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Legacy query from local host %s!\n"), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str()););
sendParameter.m_u16ID = p_MsgHeader.m_u16ID;
sendParameter.m_bLegacyQuery = true;
sendParameter.m_pQuestions = new stcMDNS_RRQuestion;
if ((bResult = (0 != sendParameter.m_pQuestions))) {
sendParameter.m_pQuestions->m_Header.m_Domain = questionRR.m_Header.m_Domain;
sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = questionRR.m_Header.m_Attributes.m_u16Type;
sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = questionRR.m_Header.m_Attributes.m_u16Class;
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: FAILED to add legacy question!\n")););
}
}
else {
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Legacy query from NON-LOCAL host!\n")););
bResult = false;
}
}
}
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: FAILED to read question!\n")););
}
esp_yield();
} // for questions
//DEBUG_EX_INFO(if (u8HostOrServiceReplies) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Reply needed: %u (%s: %s->%s)\n"), u8HostOrServiceReplies, clsTimeSyncer::timestr(), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), IPAddress(m_pUDPContext->getDestAddress()).toString().c_str()); } );
// Handle known answers
uint32_t u32Answers = (p_MsgHeader.m_u16ANCount + p_MsgHeader.m_u16NSCount + p_MsgHeader.m_u16ARCount);
DEBUG_EX_INFO(if ((u8HostOrServiceReplies) && (u32Answers)) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Known answers(%u):\n"), u32Answers); } );
for (uint32_t an=0; ((bResult) && (an<u32Answers)); ++an) {
stcMDNS_RRAnswer* pKnownRRAnswer = 0;
if (((bResult = _readRRAnswer(pKnownRRAnswer))) &&
(pKnownRRAnswer)) {
if ((DNS_RRTYPE_ANY != pKnownRRAnswer->m_Header.m_Attributes.m_u16Type) && // No ANY type answer
(DNS_RRCLASS_ANY != pKnownRRAnswer->m_Header.m_Attributes.m_u16Class)) { // No ANY class answer
// Find match between planned answer (sendParameter.m_u8HostReplyMask) and this 'known answer'
uint8_t u8HostMatchMask = (sendParameter.m_u8HostReplyMask & _replyMaskForHost(pKnownRRAnswer->m_Header));
if ((u8HostMatchMask) && // The RR in the known answer matches an RR we are planning to send, AND
((MDNS_HOST_TTL / 2) <= pKnownRRAnswer->m_u32TTL)) { // The TTL of the known answer is longer than half of the new host TTL (120s)
// Compare contents
if (AnswerType_PTR == pKnownRRAnswer->answerType()) {
stcMDNS_RRDomain hostDomain;
if ((_buildDomainForHost(m_pcHostname, hostDomain)) &&
(((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain == hostDomain)) {
// Host domain match
#ifdef MDNS_IP4_SUPPORT
if (u8HostMatchMask & ContentFlag_PTR_IP4) {
// IP4 PTR was asked for, but is already known -> skipping
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: IP4 PTR already known... skipping!\n")););
sendParameter.m_u8HostReplyMask &= ~ContentFlag_PTR_IP4;
}
#endif
#ifdef MDNS_IP6_SUPPORT
if (u8HostMatchMask & ContentFlag_PTR_IP6) {
// IP6 PTR was asked for, but is already known -> skipping
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: IP6 PTR already known... skipping!\n")););
sendParameter.m_u8HostReplyMask &= ~ContentFlag_PTR_IP6;
}
#endif
}
}
else if (u8HostMatchMask & ContentFlag_A) {
// IP4 address was asked for
#ifdef MDNS_IP4_SUPPORT
if ((AnswerType_A == pKnownRRAnswer->answerType()) &&
(((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress == _getResponseMulticastInterface(SOFTAP_MODE | STATION_MODE))) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: IP4 address already known... skipping!\n")););
sendParameter.m_u8HostReplyMask &= ~ContentFlag_A;
} // else: RData NOT IP4 length !!
#endif
}
else if (u8HostMatchMask & ContentFlag_AAAA) {
// IP6 address was asked for
#ifdef MDNS_IP6_SUPPORT
if ((AnswerType_AAAA == pAnswerRR->answerType()) &&
(((stcMDNS_RRAnswerAAAA*)pAnswerRR)->m_IPAddress == _getResponseMulticastInterface(SOFTAP_MODE | STATION_MODE))) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: IP6 address already known... skipping!\n")););
sendParameter.m_u8HostReplyMask &= ~ContentFlag_AAAA;
} // else: RData NOT IP6 length !!
#endif
}
} // Host match /*and TTL*/
//
// Check host tiebreak possibility
if (m_HostProbeInformation.m_bTiebreakNeeded) {
stcMDNS_RRDomain hostDomain;
if ((_buildDomainForHost(m_pcHostname, hostDomain)) &&
(pKnownRRAnswer->m_Header.m_Domain == hostDomain)) {
// Host domain match
#ifdef MDNS_IP4_SUPPORT
if (AnswerType_A == pKnownRRAnswer->answerType()) {
IPAddress localIPAddress(_getResponseMulticastInterface(SOFTAP_MODE | STATION_MODE));
if (((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress == localIPAddress) {
// SAME IP address -> We've received an old message from ourselfs (same IP)
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Tiebreak (IP4) WON (was an old message)!\n")););
m_HostProbeInformation.m_bTiebreakNeeded = false;
}
else {
if (((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress > localIPAddress) { // The OTHER IP is 'higher' -> LOST
// LOST tiebreak
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Tiebreak (IP4) LOST (lower)!\n")););
_cancelProbingForHost();
m_HostProbeInformation.m_bTiebreakNeeded = false;
}
else { // WON tiebreak
//TiebreakState = TiebreakState_Won; // We received an 'old' message from ourselfs -> Just ignore
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Tiebreak (IP4) WON (higher IP)!\n")););
m_HostProbeInformation.m_bTiebreakNeeded = false;
}
}
}
#endif
#ifdef MDNS_IP6_SUPPORT
if (AnswerType_AAAA == pAnswerRR->answerType()) {
// TODO
}
#endif
}
} // Host tiebreak possibility
// Check service answers
for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) {
uint8_t u8ServiceMatchMask = (pService->m_u8ReplyMask & _replyMaskForService(pKnownRRAnswer->m_Header, *pService));
if ((u8ServiceMatchMask) && // The RR in the known answer matches an RR we are planning to send, AND
((MDNS_SERVICE_TTL / 2) <= pKnownRRAnswer->m_u32TTL)) { // The TTL of the known answer is longer than half of the new service TTL (4500s)
/*if ((0 == os_strcmp("hap", pService->m_pcService))) {
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Known answer for (%s.%s.%s): %u (%s) %u\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, pKnownRRAnswer->answerType(), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), pKnownRRAnswer->m_u32TTL);
}*/
if (AnswerType_PTR == pKnownRRAnswer->answerType()) {
stcMDNS_RRDomain serviceDomain;
if ((u8ServiceMatchMask & ContentFlag_PTR_TYPE) &&
(_buildDomainForService(*pService, false, serviceDomain)) &&
(serviceDomain == ((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain)) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Service type PTR already known... skipping!\n")););
pService->m_u8ReplyMask &= ~ContentFlag_PTR_TYPE;
}
if ((u8ServiceMatchMask & ContentFlag_PTR_NAME) &&
(_buildDomainForService(*pService, true, serviceDomain)) &&
(serviceDomain == ((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain)) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Service name PTR already known... skipping!\n")););
pService->m_u8ReplyMask &= ~ContentFlag_PTR_NAME;
}
}
else if (u8ServiceMatchMask & ContentFlag_SRV) {
DEBUG_EX_ERR(if (AnswerType_SRV != pKnownRRAnswer->answerType()) DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: ERROR! INVALID answer type (SRV)!\n")););
stcMDNS_RRDomain hostDomain;
if ((_buildDomainForHost(m_pcHostname, hostDomain)) &&
(hostDomain == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain)) { // Host domain match
if ((MDNS_SRV_PRIORITY == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Priority) &&
(MDNS_SRV_WEIGHT == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Weight) &&
(pService->m_u16Port == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Port)) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Service SRV answer already known... skipping!\n")););
pService->m_u8ReplyMask &= ~ContentFlag_SRV;
} // else: Small differences -> send update message
}
}
else if (u8ServiceMatchMask & ContentFlag_TXT) {
DEBUG_EX_ERR(if (AnswerType_TXT != pKnownRRAnswer->answerType()) DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: ERROR! INVALID answer type (TXT)!\n")););
_collectServiceTxts(*pService);
if (pService->m_Txts == ((stcMDNS_RRAnswerTXT*)pKnownRRAnswer)->m_Txts) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Service TXT answer already known... skipping!\n")););
pService->m_u8ReplyMask &= ~ContentFlag_TXT;
}
_releaseTempServiceTxts(*pService);
}
} // Service match and enough TTL
//
// Check service tiebreak possibility
if (pService->m_ProbeInformation.m_bTiebreakNeeded) {
stcMDNS_RRDomain serviceDomain;
if ((_buildDomainForService(*pService, true, serviceDomain)) &&
(pKnownRRAnswer->m_Header.m_Domain == serviceDomain)) {
// Service domain match
if (AnswerType_SRV == pKnownRRAnswer->answerType()) {
stcMDNS_RRDomain hostDomain;
if ((_buildDomainForHost(m_pcHostname, hostDomain)) &&
(hostDomain == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain)) { // Host domain match
// We've received an old message from ourselfs (same SRV)
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Tiebreak (SRV) won (was an old message)!\n")););
pService->m_ProbeInformation.m_bTiebreakNeeded = false;
}
else {
if (((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain > hostDomain) { // The OTHER domain is 'higher' -> LOST
// LOST tiebreak
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Tiebreak (SRV) LOST (lower)!\n")););
_cancelProbingForService(*pService);
pService->m_ProbeInformation.m_bTiebreakNeeded = false;
}
else { // WON tiebreak
//TiebreakState = TiebreakState_Won; // We received an 'old' message from ourselfs -> Just ignore
DEBUG_EX_RX(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Tiebreak (SRV) won (higher)!\n")););
pService->m_ProbeInformation.m_bTiebreakNeeded = false;
}
}
}
}
} // service tiebreak possibility
} // for services
} // ANY answers
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: FAILED to read known answer!\n")););
}
if (pKnownRRAnswer) {
delete pKnownRRAnswer;
pKnownRRAnswer = 0;
}
esp_yield();
} // for answers
if (bResult) {
// Check, if a reply is needed
uint8_t u8ReplyNeeded = sendParameter.m_u8HostReplyMask;
for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) {
u8ReplyNeeded |= pService->m_u8ReplyMask;
if ((u8ReplyNeeded) &&
(0 == os_strcmp("hap", pService->m_pcService))) {
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Sending service reply for (%s.%s.%s): %u (%s)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, u8ReplyNeeded, IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());
}
}
if (u8ReplyNeeded) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Sending answer(%u)...\n"), u8ReplyNeeded););
sendParameter.m_bResponse = true;
sendParameter.m_bAuthorative = true;
bResult = _sendMDNSMessage(sendParameter);
}
else {
//DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: No reply needed\n")););
}
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: Something FAILED!\n")););
m_pUDPContext->flush();
}
//
// Check and reset tiebreak-states
if (m_HostProbeInformation.m_bTiebreakNeeded) {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: UNSOLVED tiebreak-need for host domain!\n")););
m_HostProbeInformation.m_bTiebreakNeeded = false;
}
for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) {
if (pService->m_ProbeInformation.m_bTiebreakNeeded) {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: UNSOLVED tiebreak-need for service domain (%s.%s.%s)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
pService->m_ProbeInformation.m_bTiebreakNeeded = false;
}
}
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseQuery: FAILED!\n")); });
return bResult;
}
/*
* MDNSResponder::_parseResponse
*
* Responses are of interest in two cases:
* 1. find domain name conflicts while probing
* 2. get answers to service queries
*
* In both cases any included questions are ignored
*
* 1. If any answer has a domain name similar to one of the domain names we're planning to use (and are probing for),
* then we've got a 'probing conflict'. The conflict has to be solved on our side of the conflict (eg. by
* setting a new hostname and restart probing). The callback 'm_fnProbeResultCallback' is called with
* 'p_bProbeResult=false' in this case.
*
* 2. Service queries like '_http._tcp.local' will (if available) produce PTR, SRV, TXT and A/AAAA answers.
* All stored answers are pivoted by the service instance name (from the PTR record). Other answer parts,
* like host domain or IP address are than attached to this element.
* Any answer part carries a TTL, this is also stored (incl. the reception time); if the TTL is '0' the
* answer (part) is withdrawn by the sender and should be removed from any cache. RFC 6762, 10.1 proposes to
* set the caches TTL-value to 1 second in such a case and to delete the item only, if no update has
* has taken place in this second.
* Answer parts may arrive in 'unsorted' order, so they are grouped into three levels:
* Level 1: PRT - names the service instance (and is used as pivot), voids all other parts if is withdrawn or outdates
* Level 2: SRV - links the instance name to a host domain and port, voids A/AAAA parts if is withdrawn or outdates
* TXT - links the instance name to services TXTs
* Level 3: A/AAAA - links the host domain to an IP address
*/
bool MDNSResponder::_parseResponse(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader) {
//DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse\n")););
//DEBUG_EX_INFO(_udpDump(););
bool bResult = false;
// A response should be the result of a query or a probe
if ((_hasServiceQueriesWaitingForAnswers()) || // Waiting for query answers OR
(_hasProbesWaitingForAnswers())) { // Probe responses
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: Received a response\n"));
//_udpDump();
);
bResult = true;
//
// Ignore questions here
stcMDNS_RRQuestion dummyRRQ;
for (uint16_t qd=0; ((bResult) && (qd<p_MsgHeader.m_u16QDCount)); ++qd) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: Received a response containing a question... ignoring!\n")););
bResult = _readRRQuestion(dummyRRQ);
esp_yield();
} // for queries
//
// Read and collect answers
stcMDNS_RRAnswer* pCollectedRRAnswers = 0;
uint32_t u32NumberOfAnswerRRs = (p_MsgHeader.m_u16ANCount + p_MsgHeader.m_u16NSCount + p_MsgHeader.m_u16ARCount);
for (uint32_t an=0; ((bResult) && (an<u32NumberOfAnswerRRs)); ++an) {
stcMDNS_RRAnswer* pRRAnswer = 0;
if (((bResult = _readRRAnswer(pRRAnswer))) &&
(pRRAnswer)) {
//DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: ADDING answer!\n")););
pRRAnswer->m_pNext = pCollectedRRAnswers;
pCollectedRRAnswers = pRRAnswer;
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: FAILED to read answer!\n")););
if (pRRAnswer) {
delete pRRAnswer;
pRRAnswer = 0;
}
bResult = false;
}
esp_yield();
} // for answers
//
// Process answers
if (bResult) {
bResult = ((!pCollectedRRAnswers) ||
(_processAnswers(pCollectedRRAnswers)));
}
else { // Some failure while reading answers
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: FAILED to read answers!\n")););
m_pUDPContext->flush();
}
// Delete collected answers
while (pCollectedRRAnswers) {
//DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: DELETING answer!\n")););
stcMDNS_RRAnswer* pNextAnswer = pCollectedRRAnswers->m_pNext;
delete pCollectedRRAnswers;
pCollectedRRAnswers = pNextAnswer;
}
}
else { // Received an unexpected response -> ignore
/*DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: Received an unexpected response... ignoring!\nDUMP:\n"));
bool bDumpResult = true;
for (uint16_t qd=0; ((bDumpResult) && (qd<p_MsgHeader.m_u16QDCount)); ++qd) {
stcMDNS_RRQuestion questionRR;
bDumpResult = _readRRQuestion(questionRR);
esp_yield();
} // for questions
// Handle known answers
uint32_t u32Answers = (p_MsgHeader.m_u16ANCount + p_MsgHeader.m_u16NSCount + p_MsgHeader.m_u16ARCount);
for (uint32_t an=0; ((bDumpResult) && (an<u32Answers)); ++an) {
stcMDNS_RRAnswer* pRRAnswer = 0;
bDumpResult = _readRRAnswer(pRRAnswer);
if (pRRAnswer) {
delete pRRAnswer;
pRRAnswer = 0;
}
esp_yield();
}
);*/
m_pUDPContext->flush();
bResult = true;
}
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _parseResponse: FAILED!\n")); });
return bResult;
}
/*
* MDNSResponder::_processAnswers
* Host:
* A (0x01): eg. esp8266.local A OP TTL 123.456.789.012
* AAAA (01Cx): eg. esp8266.local AAAA OP TTL 1234:5678::90
* PTR (0x0C, IP4): eg. 012.789.456.123.in-addr.arpa PTR OP TTL esp8266.local
* PTR (0x0C, IP6): eg. 90.0.0.0.0.0.0.0.0.0.0.0.78.56.34.12.ip6.arpa PTR OP TTL esp8266.local
* Service:
* PTR (0x0C, srv name): eg. _http._tcp.local PTR OP TTL MyESP._http._tcp.local
* PTR (0x0C, srv type): eg. _services._dns-sd._udp.local PTR OP TTL _http._tcp.local
* SRV (0x21): eg. MyESP._http._tcp.local SRV OP TTL PRIORITY WEIGHT PORT esp8266.local
* TXT (0x10): eg. MyESP._http._tcp.local TXT OP TTL c#=1
*
*/
bool MDNSResponder::_processAnswers(const MDNSResponder::stcMDNS_RRAnswer* p_pAnswers) {
bool bResult = false;
if (p_pAnswers) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAnswers: Processing answers...\n")););
bResult = true;
// Answers may arrive in an unexpected order. So we loop our answers as long, as we
// can connect new information to service queries
bool bFoundNewKeyAnswer;
do {
bFoundNewKeyAnswer = false;
const stcMDNS_RRAnswer* pRRAnswer = p_pAnswers;
while ((pRRAnswer) &&
(bResult)) {
// 1. level answer (PTR)
if (AnswerType_PTR == pRRAnswer->answerType()) {
// eg. _http._tcp.local PTR xxxx xx MyESP._http._tcp.local
bResult = _processPTRAnswer((stcMDNS_RRAnswerPTR*)pRRAnswer, bFoundNewKeyAnswer); // May 'enable' new SRV or TXT answers to be linked to queries
}
// 2. level answers
// SRV -> host domain and port
else if (AnswerType_SRV == pRRAnswer->answerType()) {
// eg. MyESP_http._tcp.local SRV xxxx xx yy zz 5000 esp8266.local
bResult = _processSRVAnswer((stcMDNS_RRAnswerSRV*)pRRAnswer, bFoundNewKeyAnswer); // May 'enable' new A/AAAA answers to be linked to queries
}
// TXT -> Txts
else if (AnswerType_TXT == pRRAnswer->answerType()) {
// eg. MyESP_http._tcp.local TXT xxxx xx c#=1
bResult = _processTXTAnswer((stcMDNS_RRAnswerTXT*)pRRAnswer);
}
// 3. level answers
#ifdef MDNS_IP4_SUPPORT
// A -> IP4Address
else if (AnswerType_A == pRRAnswer->answerType()) {
// eg. esp8266.local A xxxx xx 192.168.2.120
bResult = _processAAnswer((stcMDNS_RRAnswerA*)pRRAnswer);
}
#endif
#ifdef MDNS_IP6_SUPPORT
// AAAA -> IP6Address
else if (AnswerType_AAAA == pRRAnswer->answerType()) {
// eg. esp8266.local AAAA xxxx xx 09cf::0c
bResult = _processAAAAAnswer((stcMDNS_RRAnswerAAAA*)pRRAnswer);
}
#endif
// Finally check for probing conflicts
// Host domain
if ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) &&
((AnswerType_A == pRRAnswer->answerType()) ||
(AnswerType_AAAA == pRRAnswer->answerType()))) {
stcMDNS_RRDomain hostDomain;
if ((_buildDomainForHost(m_pcHostname, hostDomain)) &&
(pRRAnswer->m_Header.m_Domain == hostDomain)) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAnswers: Probing CONFLICT found with: %s.local\n"), m_pcHostname););
_cancelProbingForHost();
}
}
// Service domains
for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) {
if ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) &&
((AnswerType_TXT == pRRAnswer->answerType()) ||
(AnswerType_SRV == pRRAnswer->answerType()))) {
stcMDNS_RRDomain serviceDomain;
if ((_buildDomainForService(*pService, true, serviceDomain)) &&
(pRRAnswer->m_Header.m_Domain == serviceDomain)) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAnswers: Probing CONFLICT found with: %s.%s.%s\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
_cancelProbingForService(*pService);
}
}
}
pRRAnswer = pRRAnswer->m_pNext; // Next collected answer
} // while (answers)
esp_yield();
} while ((bFoundNewKeyAnswer) &&
(bResult));
} // else: No answers provided
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAnswers: FAILED!\n")); });
return bResult;
}
/*
* MDNSResponder::_processPTRAnswer
*/
bool MDNSResponder::_processPTRAnswer(const MDNSResponder::stcMDNS_RRAnswerPTR* p_pPTRAnswer,
bool& p_rbFoundNewKeyAnswer) {
bool bResult = false;
if ((bResult = (0 != p_pPTRAnswer))) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf(F("[MDNSResponder] _processPTRAnswer: Processing PTR answers...\n")););
// eg. _http._tcp.local PTR xxxx xx MyESP._http._tcp.local
// Check pending service queries for '_http._tcp'
stcMDNSServiceQuery* pServiceQuery = _findNextServiceQueryByServiceType(p_pPTRAnswer->m_Header.m_Domain, 0);
while (pServiceQuery) {
if (pServiceQuery->m_bAwaitingAnswers) {
// Find answer for service domain (eg. MyESP._http._tcp.local)
stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pPTRAnswer->m_PTRDomain);
if (pSQAnswer) { // existing answer
if (p_pPTRAnswer->m_u32TTL) { // Received update message
pSQAnswer->m_TTLServiceDomain.set(p_pPTRAnswer->m_u32TTL, millis()); // Update TTL tag
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processPTRAnswer: Updated TTL for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F("\n"));
);
}
else { // received goodbye-message
pSQAnswer->m_TTLServiceDomain.set(1, millis()); // See RFC 6762, 10.1
pSQAnswer->m_TTLServiceDomain.m_bUpdateScheduled = true; // Avoid 'cache update' query
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processPTRAnswer: 'Goodbye' received for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F("\n"));
);
}
}
else if ((p_pPTRAnswer->m_u32TTL) && // Not just a goodbye-message
((pSQAnswer = new stcMDNSServiceQuery::stcAnswer))) { // Not yet included -> add answer
pSQAnswer->m_ServiceDomain = p_pPTRAnswer->m_PTRDomain;
pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_ServiceDomain;
pSQAnswer->m_TTLServiceDomain.set(p_pPTRAnswer->m_u32TTL, millis());
pSQAnswer->releaseServiceDomain();
bResult = pServiceQuery->addAnswer(pSQAnswer);
p_rbFoundNewKeyAnswer = true;
if (pServiceQuery->m_fnCallback) {
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_ServiceDomain, true, pServiceQuery->m_pUserdata);
}
}
}
pServiceQuery = _findNextServiceQueryByServiceType(p_pPTRAnswer->m_Header.m_Domain, pServiceQuery);
}
} // else: No p_pPTRAnswer
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _processPTRAnswer: FAILED!\n")); });
return bResult;
}
/*
* MDNSResponder::_processSRVAnswer
*/
bool MDNSResponder::_processSRVAnswer(const MDNSResponder::stcMDNS_RRAnswerSRV* p_pSRVAnswer,
bool& p_rbFoundNewKeyAnswer) {
bool bResult = false;
if ((bResult = (0 != p_pSRVAnswer))) {
// eg. MyESP._http._tcp.local SRV xxxx xx yy zz 5000 esp8266.local
stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries;
while (pServiceQuery) {
stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pSRVAnswer->m_Header.m_Domain);
if (pSQAnswer) { // Answer for this service domain (eg. MyESP._http._tcp.local) available
if (p_pSRVAnswer->m_u32TTL) { // First or update message (TTL != 0)
pSQAnswer->m_TTLHostDomainAndPort.set(p_pSRVAnswer->m_u32TTL, millis()); // Update TTL tag
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processSRVAnswer: Updated TTL for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" host domain and port\n"));
);
// Host domain & Port
if ((pSQAnswer->m_HostDomain != p_pSRVAnswer->m_SRVDomain) ||
(pSQAnswer->m_u16Port != p_pSRVAnswer->m_u16Port)) {
pSQAnswer->m_HostDomain = p_pSRVAnswer->m_SRVDomain;
pSQAnswer->releaseHostDomain();
pSQAnswer->m_u16Port = p_pSRVAnswer->m_u16Port;
pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_HostDomainAndPort;
p_rbFoundNewKeyAnswer = true;
if (pServiceQuery->m_fnCallback) {
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_HostDomainAndPort, true, pServiceQuery->m_pUserdata);
}
}
}
else { // Goodby message
pSQAnswer->m_TTLHostDomainAndPort.set(1, millis()); // See RFC 6762, 10.1
pSQAnswer->m_TTLHostDomainAndPort.m_bUpdateScheduled = true; // Avoid 'cache update' query
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processSRVAnswer: 'Goodbye' received for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" host domain and port\n"));
);
}
}
pServiceQuery = pServiceQuery->m_pNext;
} // while(service query)
} // else: No p_pSRVAnswer
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _processSRVAnswer: FAILED!\n")); });
return bResult;
}
/*
* MDNSResponder::_processTXTAnswer
*/
bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT* p_pTXTAnswer) {
bool bResult = false;
if ((bResult = (0 != p_pTXTAnswer))) {
// eg. MyESP._http._tcp.local TXT xxxx xx c#=1
stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries;
while (pServiceQuery) {
stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pTXTAnswer->m_Header.m_Domain);
if (pSQAnswer) { // Answer for this service domain (eg. MyESP._http._tcp.local) available
if (p_pTXTAnswer->m_u32TTL) { // First or update message
pSQAnswer->m_TTLTxts.set(p_pTXTAnswer->m_u32TTL, millis()); // Update TTL tag
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processTXTAnswer: Updated TTL for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" TXTs\n"));
);
if (!pSQAnswer->m_Txts.compare(p_pTXTAnswer->m_Txts)) {
pSQAnswer->m_Txts = p_pTXTAnswer->m_Txts;
pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_Txts;
pSQAnswer->releaseTxts();
if (pServiceQuery->m_fnCallback) {
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_Txts, true, pServiceQuery->m_pUserdata);
}
}
}
else { // Goodby message
pSQAnswer->m_TTLTxts.set(1, millis()); // See RFC 6762, 10.1
pSQAnswer->m_TTLTxts.m_bUpdateScheduled = true; // Avoid 'cache update' query
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processTXTAnswer: 'Goodbye' received for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" TXTs\n"));
);
}
}
pServiceQuery = pServiceQuery->m_pNext;
} // while(service query)
} // else: No p_pTXTAnswer
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _processTXTAnswer: FAILED!\n")); });
return bResult;
}
#ifdef MDNS_IP4_SUPPORT
/*
* MDNSResponder::_processAAnswer
*/
bool MDNSResponder::_processAAnswer(const MDNSResponder::stcMDNS_RRAnswerA* p_pAAnswer) {
bool bResult = false;
if ((bResult = (0 != p_pAAnswer))) {
// eg. esp8266.local A xxxx xx 192.168.2.120
stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries;
while (pServiceQuery) {
stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForHostDomain(p_pAAnswer->m_Header.m_Domain);
if (pSQAnswer) { // Answer for this host domain (eg. esp8266.local) available
stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = pSQAnswer->findIP4Address(p_pAAnswer->m_IPAddress);
if (pIP4Address) {
// Already known IP4 address
if (p_pAAnswer->m_u32TTL) { // Valid TTL -> Update answers TTL
pIP4Address->m_TTL.set(p_pAAnswer->m_u32TTL, millis());
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAAnswer: Updated TTL for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" IP4Address (%s)\n"), pIP4Address->m_IPAddress.toString().c_str());
);
}
else { // 'Goodbye' message for known IP4 address
pIP4Address->m_TTL.set(1, millis()); // See RFC 6762, 10.1
pIP4Address->m_TTL.m_bUpdateScheduled = true; // Avoid 'cache update' query
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAAnswer: 'Goodbye' received for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" IP4 address (%s)\n"), pIP4Address->m_IPAddress.toString().c_str());
);
}
}
else {
// Until now unknown IP4 address -> Add (if the message isn't just a 'Goodbye' note)
if (p_pAAnswer->m_u32TTL) { // NOT just a 'Goodbye' message
pIP4Address = new stcMDNSServiceQuery::stcAnswer::stcIP4Address(p_pAAnswer->m_IPAddress, p_pAAnswer->m_u32TTL, millis());
if ((pIP4Address) &&
(pSQAnswer->addIP4Address(pIP4Address))) {
pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP4Address;
if (pServiceQuery->m_fnCallback) {
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP4Address, true, pServiceQuery->m_pUserdata);
}
}
else {
DEBUG_EX_ERR(DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAAnswer: FAILED to add IP4 address (%s)!\n"), p_pAAnswer->m_IPAddress.toString().c_str()););
}
}
}
}
pServiceQuery = pServiceQuery->m_pNext;
} // while(service query)
} // else: No p_pAAnswer
DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAAnswer: FAILED!\n")); });
return bResult;
}
#endif
#ifdef MDNS_IP6_SUPPORT
/*
* MDNSResponder::_processAAAAAnswer
*/
bool MDNSResponder::_processAAAAAnswer(const MDNSResponder::stcMDNS_RRAnswerAAAA* p_pAAAAAnswer) {
bool bResult = false;
if ((bResult = (0 != p_pAAAAAnswer))) {
// eg. esp8266.local AAAA xxxx xx 0bf3::0c
stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries;
while (pServiceQuery) {
stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForHostDomain(p_pAAAAAnswer->m_Header.m_Domain);
if (pSQAnswer) { // Answer for this host domain (eg. esp8266.local) available
stcIP6Address* pIP6Address = pSQAnswer->findIP6Address(p_pAAAAAnswer->m_IPAddress);
if (pIP6Address) {
// Already known IP6 address
if (p_pAAAAAnswer->m_u32TTL) { // Valid TTL -> Update answers TTL
pIP6Address->m_TTL.set(p_pAAAAAnswer->m_u32TTL, millis());
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf(F("[MDNSResponder] _processAAnswer: Updated TTL for "));
_printRRDomain(pSQAnswer->m_ServiceDomain);
DEBUG_OUTPUT.printf(F(" IP6 address (%s)\n"), pIP6Address->m_IPAddress.toString().c_str());
);
}
else { // 'Goodbye' message for known IP6 address