forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEAmDNS.h
1461 lines (1254 loc) · 56.3 KB
/
LEAmDNS.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
/*
LEAmDNS.h
(c) 2018, LaborEtArs
Version 0.9 beta
Some notes (from LaborEtArs, 2018):
Essentially, this is an rewrite of the original EPS8266 Multicast DNS code (ESP8266mDNS).
The target of this rewrite was to keep the existing interface as stable as possible while
adding and extending the supported set of mDNS features.
A lot of the additions were basicly taken from Erik Ekman's lwIP mdns app code.
Supported mDNS features (in some cases somewhat limited):
- Presenting a DNS-SD service to interested observers, eg. a http server by presenting _http._tcp service
- Support for multi-level compressed names in input; in output only a very simple one-leven full-name compression is implemented
- Probing host and service domains for uniqueness in the local network
- Tiebreaking while probing is supportet in a very minimalistic way (the 'higher' IP address wins the tiebreak)
- Announcing available services after successful probing
- Using fixed service TXT items or
- Using dynamic service TXT items for presented services (via callback)
- Remove services (and un-announcing them to the observers by sending goodbye-messages)
- Static queries for DNS-SD services (creating a fixed answer set after a certain timeout period)
- Dynamic queries for DNS-SD services with cached and updated answers and user notifications
Usage:
In most cases, this implementation should work as a 'drop-in' replacement for the original
ESP8266 Multicast DNS code. Adjustments to the existing code would only be needed, if some
of the new features should be used.
For presenting services:
In 'setup()':
Install a callback for the probing of host (and service) domains via 'MDNS.setProbeResultCallback(probeResultCallback, &userData);'
Register DNS-SD services with 'MDNSResponder::hMDNSService hService = MDNS.addService("MyESP", "http", "tcp", 5000);'
(Install additional callbacks for the probing of these service domains via 'MDNS.setServiceProbeResultCallback(hService, probeResultCallback, &userData);')
Add service TXT items with 'MDNS.addServiceTxt(hService, "c#", "1");' or by installing a service TXT callback
using 'MDNS.setDynamicServiceTxtCallback(dynamicServiceTxtCallback, &userData);' or service specific
'MDNS.setDynamicServiceTxtCallback(hService, dynamicServiceTxtCallback, &userData);'
Call MDNS.begin("MyHostname");
In 'probeResultCallback(MDNSResponder* p_MDNSResponder, const char* p_pcDomain, MDNSResponder:hMDNSService p_hService, bool p_bProbeResult, void* p_pUserdata)':
Check the probe result and update the host or service domain name if the probe failed
In 'dynamicServiceTxtCallback(MDNSResponder* p_MDNSResponder, const hMDNSService p_hService, void* p_pUserdata)':
Add dynamic TXT items by calling 'MDNS.addDynamicServiceTxt(p_hService, "c#", "1");'
In loop():
Call 'MDNS.update();'
For querying services:
Static:
Call 'uint32_t u32AnswerCount = MDNS.queryService("http", "tcp");'
Iterate answers by: 'for (uint32_t u=0; u<u32AnswerCount; ++u) { const char* pHostname = MDNS.answerHostname(u); }'
You should call MDNS.removeQuery() sometimes later (when the answers are nott needed anymore)
Dynamic:
Install a dynamic query by calling 'DNSResponder::hMDNSServiceQuery hServiceQuery = MDNS.installServiceQuery("http", "tcp", serviceQueryCallback, &userData);'
The callback 'serviceQueryCallback(MDNSResponder* p_MDNSResponder, const hMDNSServiceQuery p_hServiceQuery, uint32_t p_u32AnswerIndex,
enuServiceQueryAnswerType p_ServiceQueryAnswerType, bool p_bSetContent, void* p_pUserdata)'
is called for any change in the answer set.
Call 'MDNS.removeServiceQuery(hServiceQuery);' when the answers are not needed anymore
Reference:
Used mDNS messages:
A (0x01): eg. esp8266.local A OP TTL 123.456.789.012
AAAA (0x1C): eg. esp8266.local AAAA OP TTL 1234:5678::90
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
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
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
Some NOT used message types:
OPT (0x29): eDNS
NSEC (0x2F): DNSSEC
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.
*/
#ifndef MDNS_H
#define MDNS_H
#include <functional> // for UdpContext.h
#include "WiFiUdp.h"
#include "lwip/udp.h"
#include "debug.h"
#include "include/UdpContext.h"
#include <limits>
#include <PolledTimeout.h>
#include <map>
#include "ESP8266WiFi.h"
namespace esp8266
{
/**
LEAmDNS
*/
namespace MDNSImplementation
{
//this should be defined at build time
#ifndef ARDUINO_BOARD
#define ARDUINO_BOARD "generic"
#endif
#define MDNS_IP4_SUPPORT
//#define MDNS_IP6_SUPPORT
#ifdef MDNS_IP4_SUPPORT
#define MDNS_IP4_SIZE 4
#endif
#ifdef MDNS_IP6_SUPPORT
#define MDNS_IP6_SIZE 16
#endif
/*
Maximum length for all service txts for one service
*/
#define MDNS_SERVICE_TXT_MAXLENGTH 1300
/*
Maximum length for a full domain name eg. MyESP._http._tcp.local
*/
#define MDNS_DOMAIN_MAXLENGTH 256
/*
Maximum length of on label in a domain name (length info fits into 6 bits)
*/
#define MDNS_DOMAIN_LABEL_MAXLENGTH 63
/*
Maximum length of a service name eg. http
*/
#define MDNS_SERVICE_NAME_LENGTH 15
/*
Maximum length of a service protocol name eg. tcp
*/
#define MDNS_SERVICE_PROTOCOL_LENGTH 3
/*
Default timeout for static service queries
*/
#define MDNS_QUERYSERVICES_WAIT_TIME 1000
/**
MDNSResponder
*/
class MDNSResponder
{
public:
/* INTERFACE */
MDNSResponder(void);
virtual ~MDNSResponder(void);
// Start the MDNS responder by setting the default hostname
// Later call MDNS::update() in every 'loop' to run the process loop
// (probing, announcing, responding, ...)
// if interfaceAddress is not specified, default interface is STA, or AP when STA is not set
bool begin(const char* p_pcHostname, const IPAddress& p_IPAddress = INADDR_ANY, uint32_t p_u32TTL = 120 /*ignored*/);
bool begin(const String& p_strHostname, const IPAddress& p_IPAddress = INADDR_ANY, uint32_t p_u32TTL = 120 /*ignored*/)
{
return begin(p_strHostname.c_str(), p_IPAddress, p_u32TTL);
}
// Finish MDNS processing
bool close(void);
// for esp32 compatability
bool end(void);
// Change hostname (probing is restarted)
bool setHostname(const char* p_pcHostname);
// for compatibility...
bool setHostname(const String& p_strHostname);
bool isRunning(void)
{
return (m_pUDPContext != 0);
}
/**
hMDNSService (opaque handle to access the service)
*/
typedef const void* hMDNSService;
// Add a new service to the MDNS responder. If no name (instance name) is given (p_pcName = 0)
// the current hostname is used. If the hostname is changed later, the instance names for
// these 'auto-named' services are changed to the new name also (and probing is restarted).
// The usual '_' before p_pcService (eg. http) and protocol (eg. tcp) may be given.
hMDNSService addService(const char* p_pcName,
const char* p_pcService,
const char* p_pcProtocol,
uint16_t p_u16Port);
// Removes a service from the MDNS responder
bool removeService(const hMDNSService p_hService);
bool removeService(const char* p_pcInstanceName,
const char* p_pcServiceName,
const char* p_pcProtocol);
// for compatibility...
bool addService(const String& p_strServiceName,
const String& p_strProtocol,
uint16_t p_u16Port);
// Change the services instance name (and restart probing).
bool setServiceName(const hMDNSService p_hService,
const char* p_pcInstanceName);
//for compatibility
//Warning: this has the side effect of changing the hostname.
//TODO: implement instancename different from hostname
void setInstanceName(const char* p_pcHostname)
{
setHostname(p_pcHostname);
}
// for esp32 compatibilty
void setInstanceName(const String& s_pcHostname)
{
setInstanceName(s_pcHostname.c_str());
}
/**
hMDNSTxt (opaque handle to access the TXT items)
*/
typedef void* hMDNSTxt;
// Add a (static) MDNS TXT item ('key' = 'value') to the service
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
const char* p_pcValue);
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
uint32_t p_u32Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
uint16_t p_u16Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
uint8_t p_u8Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
int32_t p_i32Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
int16_t p_i16Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService,
const char* p_pcKey,
int8_t p_i8Value);
// Remove an existing (static) MDNS TXT item from the service
bool removeServiceTxt(const hMDNSService p_hService,
const hMDNSTxt p_hTxt);
bool removeServiceTxt(const hMDNSService p_hService,
const char* p_pcKey);
bool removeServiceTxt(const char* p_pcinstanceName,
const char* p_pcServiceName,
const char* p_pcProtocol,
const char* p_pcKey);
// for compatibility...
bool addServiceTxt(const char* p_pcService,
const char* p_pcProtocol,
const char* p_pcKey,
const char* p_pcValue);
bool addServiceTxt(const String& p_strService,
const String& p_strProtocol,
const String& p_strKey,
const String& p_strValue);
/**
MDNSDynamicServiceTxtCallbackFn
Callback function for dynamic MDNS TXT items
*/
typedef std::function<void(const hMDNSService p_hService)> MDNSDynamicServiceTxtCallbackFunc;
// Set a global callback for dynamic MDNS TXT items. The callback function is called
// every time, a TXT item is needed for one of the installed services.
bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFunc p_fnCallback);
// Set a service specific callback for dynamic MDNS TXT items. The callback function
// is called every time, a TXT item is needed for the given service.
bool setDynamicServiceTxtCallback(const hMDNSService p_hService,
MDNSDynamicServiceTxtCallbackFunc p_fnCallback);
// Add a (dynamic) MDNS TXT item ('key' = 'value') to the service
// Dynamic TXT items are removed right after one-time use. So they need to be added
// every time the value s needed (via callback).
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
const char* p_pcValue);
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
uint32_t p_u32Value);
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
uint16_t p_u16Value);
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
uint8_t p_u8Value);
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
int32_t p_i32Value);
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
int16_t p_i16Value);
hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService,
const char* p_pcKey,
int8_t p_i8Value);
// Perform a (static) service query. The function returns after p_u16Timeout milliseconds
// The answers (the number of received answers is returned) can be retrieved by calling
// - answerHostname (or hostname)
// - answerIP (or IP)
// - answerPort (or port)
uint32_t queryService(const char* p_pcService,
const char* p_pcProtocol,
const uint16_t p_u16Timeout = MDNS_QUERYSERVICES_WAIT_TIME);
bool removeQuery(void);
// for compatibility...
uint32_t queryService(const String& p_strService,
const String& p_strProtocol);
const char* answerHostname(const uint32_t p_u32AnswerIndex);
IPAddress answerIP(const uint32_t p_u32AnswerIndex);
uint16_t answerPort(const uint32_t p_u32AnswerIndex);
// for compatibility...
String hostname(const uint32_t p_u32AnswerIndex);
IPAddress IP(const uint32_t p_u32AnswerIndex);
uint16_t port(const uint32_t p_u32AnswerIndex);
/**
hMDNSServiceQuery (opaque handle to access dynamic service queries)
*/
typedef const void* hMDNSServiceQuery;
/**
enuServiceQueryAnswerType
*/
typedef enum _enuServiceQueryAnswerType
{
ServiceQueryAnswerType_ServiceDomain = (1 << 0), // Service instance name
ServiceQueryAnswerType_HostDomainAndPort = (1 << 1), // Host domain and service port
ServiceQueryAnswerType_Txts = (1 << 2), // TXT items
#ifdef MDNS_IP4_SUPPORT
ServiceQueryAnswerType_IP4Address = (1 << 3), // IP4 address
#endif
#ifdef MDNS_IP6_SUPPORT
ServiceQueryAnswerType_IP6Address = (1 << 4), // IP6 address
#endif
} enuServiceQueryAnswerType;
enum class AnswerType : uint32_t
{
Unknown = 0,
ServiceDomain = ServiceQueryAnswerType_ServiceDomain,
HostDomainAndPort = ServiceQueryAnswerType_HostDomainAndPort,
Txt = ServiceQueryAnswerType_Txts,
#ifdef MDNS_IP4_SUPPORT
IP4Address = ServiceQueryAnswerType_IP4Address,
#endif
#ifdef MDNS_IP6_SUPPORT
IP6Address = ServiceQueryAnswerType_IP6Address,
#endif
};
/**
MDNSServiceQueryCallbackFn
Callback function for received answers for dynamic service queries
*/
struct MDNSServiceInfo; // forward declaration
typedef std::function<void(const MDNSServiceInfo& mdnsServiceInfo,
AnswerType answerType, // flag for the updated answer item
bool p_bSetContent // true: Answer component set, false: component deleted
)> MDNSServiceQueryCallbackFunc;
// Install a dynamic service query. For every received answer (part) the given callback
// function is called. The query will be updated every time, the TTL for an answer
// has timed-out.
// The answers can also be retrieved by calling
// - answerCount
// - answerServiceDomain
// - hasAnswerHostDomain/answerHostDomain
// - hasAnswerIP4Address/answerIP4Address
// - hasAnswerIP6Address/answerIP6Address
// - hasAnswerPort/answerPort
// - hasAnswerTxts/answerTxts
hMDNSServiceQuery installServiceQuery(const char* p_pcService,
const char* p_pcProtocol,
MDNSServiceQueryCallbackFunc p_fnCallback);
// Remove a dynamic service query
bool removeServiceQuery(hMDNSServiceQuery p_hServiceQuery);
uint32_t answerCount(const hMDNSServiceQuery p_hServiceQuery);
std::vector<MDNSResponder::MDNSServiceInfo> answerInfo(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery);
const char* answerServiceDomain(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
bool hasAnswerHostDomain(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
const char* answerHostDomain(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
#ifdef MDNS_IP4_SUPPORT
bool hasAnswerIP4Address(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
uint32_t answerIP4AddressCount(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
IPAddress answerIP4Address(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex,
const uint32_t p_u32AddressIndex);
#endif
#ifdef MDNS_IP6_SUPPORT
bool hasAnswerIP6Address(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
uint32_t answerIP6AddressCount(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
IPAddress answerIP6Address(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex,
const uint32_t p_u32AddressIndex);
#endif
bool hasAnswerPort(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
uint16_t answerPort(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
bool hasAnswerTxts(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
// Get the TXT items as a ';'-separated string
const char* answerTxts(const hMDNSServiceQuery p_hServiceQuery,
const uint32_t p_u32AnswerIndex);
/**
MDNSProbeResultCallbackFn
Callback function for (host and service domain) probe results
*/
typedef std::function<void(const char* p_pcDomainName,
bool p_bProbeResult)> MDNSHostProbeFn;
typedef std::function<void(MDNSResponder& resp,
const char* p_pcDomainName,
bool p_bProbeResult)> MDNSHostProbeFn1;
typedef std::function<void(const char* p_pcServiceName,
const hMDNSService p_hMDNSService,
bool p_bProbeResult)> MDNSServiceProbeFn;
typedef std::function<void(MDNSResponder& resp,
const char* p_pcServiceName,
const hMDNSService p_hMDNSService,
bool p_bProbeResult)> MDNSServiceProbeFn1;
// Set a global callback function for host and service probe results
// The callback function is called, when the probing for the host domain
// (or a service domain, which hasn't got a service specific callback)
// Succeeds or fails.
// In case of failure, the failed domain name should be changed.
bool setHostProbeResultCallback(MDNSHostProbeFn p_fnCallback);
bool setHostProbeResultCallback(MDNSHostProbeFn1 p_fnCallback);
// Set a service specific probe result callback
bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
MDNSServiceProbeFn p_fnCallback);
bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
MDNSServiceProbeFn1 p_fnCallback);
// Application should call this whenever AP is configured/disabled
bool notifyAPChange(void);
// 'update' should be called in every 'loop' to run the MDNS processing
bool update(void);
// 'announce' can be called every time, the configuration of some service
// changes. Mainly, this would be changed content of TXT items.
bool announce(void);
// Enable OTA update
hMDNSService enableArduino(uint16_t p_u16Port,
bool p_bAuthUpload = false);
// Domain name helper
static bool indexDomain(char*& p_rpcDomain,
const char* p_pcDivider = "-",
const char* p_pcDefaultDomain = 0);
/** STRUCTS **/
public:
/**
MDNSServiceInfo, used in application callbacks
*/
struct MDNSServiceInfo
{
MDNSServiceInfo(MDNSResponder& p_pM, MDNSResponder::hMDNSServiceQuery p_hS, uint32_t p_u32A)
: p_pMDNSResponder(p_pM),
p_hServiceQuery(p_hS),
p_u32AnswerIndex(p_u32A)
{};
struct CompareKey
{
bool operator()(char const *a, char const *b) const
{
return strcmp(a, b) < 0;
}
};
using KeyValueMap = std::map<const char*, const char*, CompareKey>;
protected:
MDNSResponder& p_pMDNSResponder;
MDNSResponder::hMDNSServiceQuery p_hServiceQuery;
uint32_t p_u32AnswerIndex;
KeyValueMap keyValueMap;
public:
const char* serviceDomain()
{
return p_pMDNSResponder.answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex);
};
bool hostDomainAvailable()
{
return (p_pMDNSResponder.hasAnswerHostDomain(p_hServiceQuery, p_u32AnswerIndex));
}
const char* hostDomain()
{
return (hostDomainAvailable()) ?
p_pMDNSResponder.answerHostDomain(p_hServiceQuery, p_u32AnswerIndex) : nullptr;
};
bool hostPortAvailable()
{
return (p_pMDNSResponder.hasAnswerPort(p_hServiceQuery, p_u32AnswerIndex));
}
uint16_t hostPort()
{
return (hostPortAvailable()) ?
p_pMDNSResponder.answerPort(p_hServiceQuery, p_u32AnswerIndex) : 0;
};
bool IP4AddressAvailable()
{
return (p_pMDNSResponder.hasAnswerIP4Address(p_hServiceQuery, p_u32AnswerIndex));
}
std::vector<IPAddress> IP4Adresses()
{
std::vector<IPAddress> internalIP;
if (IP4AddressAvailable())
{
uint16_t cntIP4Adress = p_pMDNSResponder.answerIP4AddressCount(p_hServiceQuery, p_u32AnswerIndex);
for (uint32_t u2 = 0; u2 < cntIP4Adress; ++u2)
{
internalIP.emplace_back(p_pMDNSResponder.answerIP4Address(p_hServiceQuery, p_u32AnswerIndex, u2));
}
}
return internalIP;
};
bool txtAvailable()
{
return (p_pMDNSResponder.hasAnswerTxts(p_hServiceQuery, p_u32AnswerIndex));
}
const char* strKeyValue()
{
return (txtAvailable()) ?
p_pMDNSResponder.answerTxts(p_hServiceQuery, p_u32AnswerIndex) : nullptr;
};
const KeyValueMap& keyValues()
{
if (txtAvailable() && keyValueMap.size() == 0)
{
for (auto kv = p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex); kv != nullptr; kv = kv->m_pNext)
{
keyValueMap.emplace(std::pair<const char*, const char*>(kv->m_pcKey, kv->m_pcValue));
}
}
return keyValueMap;
}
const char* value(const char* key)
{
char* result = nullptr;
for (stcMDNSServiceTxt* pTxt = p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex); pTxt; pTxt = pTxt->m_pNext)
{
if ((key) &&
(0 == strcmp(pTxt->m_pcKey, key)))
{
result = pTxt->m_pcValue;
break;
}
}
return result;
}
};
protected:
/**
stcMDNSServiceTxt
*/
struct stcMDNSServiceTxt
{
stcMDNSServiceTxt* m_pNext;
char* m_pcKey;
char* m_pcValue;
bool m_bTemp;
stcMDNSServiceTxt(const char* p_pcKey = 0,
const char* p_pcValue = 0,
bool p_bTemp = false);
stcMDNSServiceTxt(const stcMDNSServiceTxt& p_Other);
~stcMDNSServiceTxt(void);
stcMDNSServiceTxt& operator=(const stcMDNSServiceTxt& p_Other);
bool clear(void);
char* allocKey(size_t p_stLength);
bool setKey(const char* p_pcKey,
size_t p_stLength);
bool setKey(const char* p_pcKey);
bool releaseKey(void);
char* allocValue(size_t p_stLength);
bool setValue(const char* p_pcValue,
size_t p_stLength);
bool setValue(const char* p_pcValue);
bool releaseValue(void);
bool set(const char* p_pcKey,
const char* p_pcValue,
bool p_bTemp = false);
bool update(const char* p_pcValue);
size_t length(void) const;
};
/**
stcMDNSTxts
*/
struct stcMDNSServiceTxts
{
stcMDNSServiceTxt* m_pTxts;
stcMDNSServiceTxts(void);
stcMDNSServiceTxts(const stcMDNSServiceTxts& p_Other);
~stcMDNSServiceTxts(void);
stcMDNSServiceTxts& operator=(const stcMDNSServiceTxts& p_Other);
bool clear(void);
bool add(stcMDNSServiceTxt* p_pTxt);
bool remove(stcMDNSServiceTxt* p_pTxt);
bool removeTempTxts(void);
stcMDNSServiceTxt* find(const char* p_pcKey);
const stcMDNSServiceTxt* find(const char* p_pcKey) const;
stcMDNSServiceTxt* find(const stcMDNSServiceTxt* p_pTxt);
uint16_t length(void) const;
size_t c_strLength(void) const;
bool c_str(char* p_pcBuffer);
size_t bufferLength(void) const;
bool buffer(char* p_pcBuffer);
bool compare(const stcMDNSServiceTxts& p_Other) const;
bool operator==(const stcMDNSServiceTxts& p_Other) const;
bool operator!=(const stcMDNSServiceTxts& p_Other) const;
};
/**
enuContentFlags
*/
typedef enum _enuContentFlags
{
// Host
ContentFlag_A = 0x01,
ContentFlag_PTR_IP4 = 0x02,
ContentFlag_PTR_IP6 = 0x04,
ContentFlag_AAAA = 0x08,
// Service
ContentFlag_PTR_TYPE = 0x10,
ContentFlag_PTR_NAME = 0x20,
ContentFlag_TXT = 0x40,
ContentFlag_SRV = 0x80,
} enuContentFlags;
/**
stcMDNS_MsgHeader
*/
struct stcMDNS_MsgHeader
{
uint16_t m_u16ID; // Identifier
bool m_1bQR : 1; // Query/Response flag
unsigned char m_4bOpcode : 4; // Operation code
bool m_1bAA : 1; // Authoritative Answer flag
bool m_1bTC : 1; // Truncation flag
bool m_1bRD : 1; // Recursion desired
bool m_1bRA : 1; // Recursion available
unsigned char m_3bZ : 3; // Zero
unsigned char m_4bRCode : 4; // Response code
uint16_t m_u16QDCount; // Question count
uint16_t m_u16ANCount; // Answer count
uint16_t m_u16NSCount; // Authority Record count
uint16_t m_u16ARCount; // Additional Record count
stcMDNS_MsgHeader(uint16_t p_u16ID = 0,
bool p_bQR = false,
unsigned char p_ucOpcode = 0,
bool p_bAA = false,
bool p_bTC = false,
bool p_bRD = false,
bool p_bRA = false,
unsigned char p_ucRCode = 0,
uint16_t p_u16QDCount = 0,
uint16_t p_u16ANCount = 0,
uint16_t p_u16NSCount = 0,
uint16_t p_u16ARCount = 0);
};
/**
stcMDNS_RRDomain
*/
struct stcMDNS_RRDomain
{
char m_acName[MDNS_DOMAIN_MAXLENGTH]; // Encoded domain name
uint16_t m_u16NameLength; // Length (incl. '\0')
stcMDNS_RRDomain(void);
stcMDNS_RRDomain(const stcMDNS_RRDomain& p_Other);
stcMDNS_RRDomain& operator=(const stcMDNS_RRDomain& p_Other);
bool clear(void);
bool addLabel(const char* p_pcLabel,
bool p_bPrependUnderline = false);
bool compare(const stcMDNS_RRDomain& p_Other) const;
bool operator==(const stcMDNS_RRDomain& p_Other) const;
bool operator!=(const stcMDNS_RRDomain& p_Other) const;
bool operator>(const stcMDNS_RRDomain& p_Other) const;
size_t c_strLength(void) const;
bool c_str(char* p_pcBuffer);
};
/**
stcMDNS_RRAttributes
*/
struct stcMDNS_RRAttributes
{
uint16_t m_u16Type; // Type
uint16_t m_u16Class; // Class, nearly always 'IN'
stcMDNS_RRAttributes(uint16_t p_u16Type = 0,
uint16_t p_u16Class = 1 /*DNS_RRCLASS_IN Internet*/);
stcMDNS_RRAttributes(const stcMDNS_RRAttributes& p_Other);
stcMDNS_RRAttributes& operator=(const stcMDNS_RRAttributes& p_Other);
};
/**
stcMDNS_RRHeader
*/
struct stcMDNS_RRHeader
{
stcMDNS_RRDomain m_Domain;
stcMDNS_RRAttributes m_Attributes;
stcMDNS_RRHeader(void);
stcMDNS_RRHeader(const stcMDNS_RRHeader& p_Other);
stcMDNS_RRHeader& operator=(const stcMDNS_RRHeader& p_Other);
bool clear(void);
};
/**
stcMDNS_RRQuestion
*/
struct stcMDNS_RRQuestion
{
stcMDNS_RRQuestion* m_pNext;
stcMDNS_RRHeader m_Header;
bool m_bUnicast; // Unicast reply requested
stcMDNS_RRQuestion(void);
};
/**
enuAnswerType
*/
typedef enum _enuAnswerType
{
AnswerType_A,
AnswerType_PTR,
AnswerType_TXT,
AnswerType_AAAA,
AnswerType_SRV,
AnswerType_Generic
} enuAnswerType;
/**
stcMDNS_RRAnswer
*/
struct stcMDNS_RRAnswer
{
stcMDNS_RRAnswer* m_pNext;
const enuAnswerType m_AnswerType;
stcMDNS_RRHeader m_Header;
bool m_bCacheFlush; // Cache flush command bit
uint32_t m_u32TTL; // Validity time in seconds
virtual ~stcMDNS_RRAnswer(void);
enuAnswerType answerType(void) const;
bool clear(void);
protected:
stcMDNS_RRAnswer(enuAnswerType p_AnswerType,
const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
};
#ifdef MDNS_IP4_SUPPORT
/**
stcMDNS_RRAnswerA
*/
struct stcMDNS_RRAnswerA : public stcMDNS_RRAnswer
{
IPAddress m_IPAddress;
stcMDNS_RRAnswerA(const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
~stcMDNS_RRAnswerA(void);
bool clear(void);
};
#endif
/**
stcMDNS_RRAnswerPTR
*/
struct stcMDNS_RRAnswerPTR : public stcMDNS_RRAnswer
{
stcMDNS_RRDomain m_PTRDomain;
stcMDNS_RRAnswerPTR(const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
~stcMDNS_RRAnswerPTR(void);
bool clear(void);
};
/**
stcMDNS_RRAnswerTXT
*/
struct stcMDNS_RRAnswerTXT : public stcMDNS_RRAnswer
{
stcMDNSServiceTxts m_Txts;
stcMDNS_RRAnswerTXT(const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
~stcMDNS_RRAnswerTXT(void);
bool clear(void);
};
#ifdef MDNS_IP6_SUPPORT
/**
stcMDNS_RRAnswerAAAA
*/
struct stcMDNS_RRAnswerAAAA : public stcMDNS_RRAnswer
{
//TODO: IP6Address m_IPAddress;
stcMDNS_RRAnswerAAAA(const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
~stcMDNS_RRAnswerAAAA(void);
bool clear(void);
};
#endif
/**
stcMDNS_RRAnswerSRV
*/
struct stcMDNS_RRAnswerSRV : public stcMDNS_RRAnswer
{
uint16_t m_u16Priority;
uint16_t m_u16Weight;
uint16_t m_u16Port;
stcMDNS_RRDomain m_SRVDomain;
stcMDNS_RRAnswerSRV(const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
~stcMDNS_RRAnswerSRV(void);
bool clear(void);
};
/**
stcMDNS_RRAnswerGeneric
*/
struct stcMDNS_RRAnswerGeneric : public stcMDNS_RRAnswer
{
uint16_t m_u16RDLength; // Length of variable answer
uint8_t* m_pu8RDData; // Offset of start of variable answer in packet
stcMDNS_RRAnswerGeneric(const stcMDNS_RRHeader& p_Header,
uint32_t p_u32TTL);
~stcMDNS_RRAnswerGeneric(void);
bool clear(void);
};
/**
enuProbingStatus
*/
typedef enum _enuProbingStatus
{
ProbingStatus_WaitingForData,
ProbingStatus_ReadyToStart,
ProbingStatus_InProgress,
ProbingStatus_Done
} enuProbingStatus;
/**
stcProbeInformation
*/
struct stcProbeInformation
{
enuProbingStatus m_ProbingStatus;
uint8_t m_u8SentCount; // Used for probes and announcements
esp8266::polledTimeout::oneShotMs m_Timeout; // Used for probes and announcements
//clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements
bool m_bConflict;
bool m_bTiebreakNeeded;
MDNSHostProbeFn m_fnHostProbeResultCallback;
MDNSServiceProbeFn m_fnServiceProbeResultCallback;
stcProbeInformation(void);
bool clear(bool p_bClearUserdata = false);
};
/**
stcMDNSService
*/
struct stcMDNSService
{
stcMDNSService* m_pNext;
char* m_pcName;
bool m_bAutoName; // Name was set automatically to hostname (if no name was supplied)
char* m_pcService;
char* m_pcProtocol;
uint16_t m_u16Port;
uint8_t m_u8ReplyMask;
stcMDNSServiceTxts m_Txts;
MDNSDynamicServiceTxtCallbackFunc m_fnTxtCallback;
stcProbeInformation m_ProbeInformation;
stcMDNSService(const char* p_pcName = 0,
const char* p_pcService = 0,
const char* p_pcProtocol = 0);
~stcMDNSService(void);
bool setName(const char* p_pcName);
bool releaseName(void);
bool setService(const char* p_pcService);
bool releaseService(void);
bool setProtocol(const char* p_pcProtocol);
bool releaseProtocol(void);
};
/**
stcMDNSServiceQuery
*/
struct stcMDNSServiceQuery
{
/**
stcAnswer