@@ -55,9 +55,9 @@ extern "C" {
55
55
56
56
57
57
58
- // #define MDNS_DEBUG_ERR
59
- // #define MDNS_DEBUG_TX
60
- // #define MDNS_DEBUG_RX
58
+ #define MDNS_DEBUG_ERR
59
+ #define MDNS_DEBUG_TX
60
+ #define MDNS_DEBUG_RX
61
61
62
62
#define MDNS_NAME_REF 0xC000
63
63
@@ -88,19 +88,22 @@ static const int MDNS_PORT = 5353;
88
88
MDNSResponder::MDNSResponder () : _conn(0 ) { _services = 0 ; }
89
89
MDNSResponder::~MDNSResponder () {}
90
90
91
- bool MDNSResponder::begin (const char * domain ){
91
+ bool MDNSResponder::begin (const char * hostname ){
92
92
// Open the MDNS socket if it isn't already open.
93
93
94
- size_t n = strlen (domain );
95
- if (n > 255 ) { // Can only handle domains that are 255 chars in length .
94
+ size_t n = strlen (hostname );
95
+ if (n > 63 ) { // max size for a single label .
96
96
return false ;
97
97
}
98
98
99
- // Copy in domain characters as lowercase
99
+ // Copy in hostname characters as lowercase
100
100
for (size_t i = 0 ; i < n; ++i)
101
- _hostName[i] = tolower (domain [i]);
101
+ _hostName[i] = tolower (hostname [i]);
102
102
_hostName[n] = ' \0 ' ;
103
103
104
+ // Copy hostname to default instance name
105
+ os_strcpy (_instanceName,hostname);
106
+
104
107
// Open the MDNS socket if it isn't already open.
105
108
if (!_conn) {
106
109
uint32_t ourIp = _getOurIp ();
@@ -139,10 +142,10 @@ void MDNSResponder::update() {
139
142
}
140
143
141
144
142
-
143
-
144
-
145
-
145
+ void MDNSResponder::setInstanceName ( char * name){
146
+ if ( os_strlen (name) > 63 ) return ;
147
+ else os_strcpy (_instanceName,name);
148
+ }
146
149
147
150
148
151
bool MDNSResponder::addServiceTxt (char *name, char *proto, char *key, char *value){
@@ -317,9 +320,11 @@ void MDNSResponder::_parsePacket(){
317
320
hostNameLen = 0 ;
318
321
}
319
322
320
- if (hostNameLen > 0 && strcmp (_hostName, hostName) != 0 ){
323
+ if (hostNameLen > 0 && strcmp (_hostName, hostName) != 0 && strcmp (_instanceName, hostName) != 0 ){
321
324
#ifdef MDNS_DEBUG_ERR
322
325
Serial.printf (" ERR_NO_HOST: %s\n " , hostName);
326
+ Serial.printf (" hostname: %s\n " , hostName);
327
+ Serial.printf (" instance: %s\n " , _instanceName);
323
328
#endif
324
329
_conn->flush ();
325
330
return ;
@@ -495,6 +500,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
495
500
Serial.printf (" TX: mask:%01X, service:%s, proto:%s, port:%u\n " , replyMask, service, proto, port);
496
501
#endif
497
502
503
+ char * instanceName = _instanceName;
504
+ size_t instanceNameLen = os_strlen (instanceName);
505
+
498
506
char * hostName = _hostName;
499
507
size_t hostNameLen = os_strlen (hostName);
500
508
@@ -549,7 +557,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
549
557
_conn->append (reinterpret_cast <const char *>(&terminator), 1 ); // terminator
550
558
551
559
// Send the type, class, ttl and rdata length
552
- uint8_t ptrDataLen = hostNameLen + serviceNameLen + protoNameLen + localNameLen + 5 ; // 5 is four label sizes and the terminator
560
+ uint8_t ptrDataLen = instanceNameLen + serviceNameLen + protoNameLen + localNameLen + 5 ; // 5 is four label sizes and the terminator
553
561
uint8_t ptrAttrs[10 ] = {
554
562
0x00 , 0x0c , // PTR record query
555
563
0x00 , 0x01 , // Class IN
@@ -558,9 +566,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
558
566
};
559
567
_conn->append (reinterpret_cast <const char *>(ptrAttrs), 10 );
560
568
561
- // Send the RData (ie. "esp8266 ._http._tcp.local")
562
- _conn->append (reinterpret_cast <const char *>(&hostNameLen ), 1 ); // lenght of "esp8266 "
563
- _conn->append (reinterpret_cast <const char *>(hostName ), hostNameLen); // "esp8266 "
569
+ // Send the RData (ie. "My IOT device ._http._tcp.local")
570
+ _conn->append (reinterpret_cast <const char *>(&instanceNameLen ), 1 ); // lenght of "My IOT device "
571
+ _conn->append (reinterpret_cast <const char *>(instanceName ), instanceNameLen); // "My IOT device "
564
572
_conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
565
573
_conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
566
574
_conn->append (reinterpret_cast <const char *>(&protoNameLen), 1 ); // lenght of "_tcp"
@@ -572,9 +580,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
572
580
573
581
// TXT Responce
574
582
if (replyMask & 0x4 ){
575
- // Send the name field (ie. "esp8266 ._http._tcp.local")
576
- _conn->append (reinterpret_cast <const char *>(&hostNameLen ), 1 ); // lenght of "esp8266 "
577
- _conn->append (reinterpret_cast <const char *>(hostName ), hostNameLen); // "esp8266 "
583
+ // Send the name field (ie. "My IOT device ._http._tcp.local")
584
+ _conn->append (reinterpret_cast <const char *>(&instanceNameLen ), 1 ); // lenght of "My IOT device "
585
+ _conn->append (reinterpret_cast <const char *>(instanceName ), instanceNameLen); // "My IOT device "
578
586
_conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
579
587
_conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
580
588
_conn->append (reinterpret_cast <const char *>(&protoNameLen), 1 ); // lenght of "_tcp"
@@ -608,9 +616,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
608
616
609
617
// SRV Responce
610
618
if (replyMask & 0x2 ){
611
- // Send the name field (ie. "esp8266 ._http._tcp.local")
612
- _conn->append (reinterpret_cast <const char *>(&hostNameLen ), 1 ); // lenght of "esp8266 "
613
- _conn->append (reinterpret_cast <const char *>(hostName ), hostNameLen); // "esp8266 "
619
+ // Send the name field (ie. "My IOT device ._http._tcp.local")
620
+ _conn->append (reinterpret_cast <const char *>(&instanceNameLen ), 1 ); // lenght of "My IOT device "
621
+ _conn->append (reinterpret_cast <const char *>(instanceName ), instanceNameLen); // "My IOT device "
614
622
_conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
615
623
_conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
616
624
_conn->append (reinterpret_cast <const char *>(&protoNameLen), 1 ); // lenght of "_tcp"
0 commit comments