@@ -85,9 +85,25 @@ static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251);
85
85
static const int MDNS_MULTICAST_TTL = 1 ;
86
86
static const int MDNS_PORT = 5353 ;
87
87
88
+ struct MDNSService {
89
+ MDNSService* _next;
90
+ char _name[32 ];
91
+ char _proto[3 ];
92
+ uint16_t _port;
93
+ struct MDNSTxt * _txts;
94
+ uint16_t _txtLen; // length of all txts
95
+ };
96
+
97
+ struct MDNSTxt {
98
+ MDNSTxt * _next;
99
+ String _txt;
100
+ };
101
+
102
+
103
+
88
104
MDNSResponder::MDNSResponder () : _conn(0 ) {
89
105
_services = 0 ;
90
- _instanceName[ 0 ] = 0 ;
106
+ _instanceName = " " ;
91
107
}
92
108
MDNSResponder::~MDNSResponder () {}
93
109
@@ -100,12 +116,11 @@ bool MDNSResponder::begin(const char* hostname){
100
116
}
101
117
102
118
// Copy in hostname characters as lowercase
103
- for (size_t i = 0 ; i < n; ++i)
104
- _hostName[i] = tolower (hostname[i]);
105
- _hostName[n] = ' \0 ' ;
119
+ _hostName = hostname;
120
+ _hostName.toLowerCase ();
106
121
107
122
// If instance name is not already set copy hostname to instance name
108
- if (os_strlen ( _instanceName) == 0 ) os_strcpy ( _instanceName, hostname);
123
+ if (_instanceName. equals ( " " ) ) _instanceName= hostname;
109
124
110
125
// Open the MDNS socket if it isn't already open.
111
126
if (!_conn) {
@@ -145,9 +160,9 @@ void MDNSResponder::update() {
145
160
}
146
161
147
162
148
- void MDNSResponder::setInstanceName (char * name){
149
- if (os_strlen ( name) > 63 ) return ;
150
- else os_strcpy ( _instanceName, name) ;
163
+ void MDNSResponder::setInstanceName (String name){
164
+ if (name. length ( ) > 63 ) return ;
165
+ else _instanceName = name;
151
166
}
152
167
153
168
@@ -156,29 +171,26 @@ bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *valu
156
171
157
172
uint8_t txtLen = os_strlen (key) + os_strlen (value) + 1 ; // Add one for equals sign
158
173
txtLen+=1 ; // accounts for length byte added when building the txt responce
159
- if ( txtLen > 128 ) return false ;
160
174
// Find the service
161
175
for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next ) {
162
176
// Checking Service names
163
177
if (strcmp (servicePtr->_name , name) == 0 && strcmp (servicePtr->_proto , proto) == 0 ){
178
+ // found a service name match
164
179
if (servicePtr->_txtLen + txtLen > 1300 ) return false ; // max txt record size
165
- // found a service name match
166
- struct MDNSTxt *newtxt = (struct MDNSTxt *)(os_malloc (sizeof (struct MDNSTxt )));
167
- os_strcpy (newtxt->_txt , key);
168
- os_strcat (newtxt->_txt , " =" );
169
- os_strcat (newtxt->_txt , value);
170
- newtxt->_next = 0 ;
171
- if (servicePtr->_txts == 0 ) { // no services have been added
172
- // Adding First TXT to service
173
- servicePtr->_txts = newtxt;
174
- servicePtr->_txtLen += txtLen;
175
- return true ;
180
+ MDNSTxt *newtxt = new MDNSTxt;
181
+ newtxt->_txt = String (key) + " =" + String (value);
182
+ newtxt->_next = 0 ;
183
+ if (servicePtr->_txts == 0 ) { // no services have been added
184
+ // Adding First TXT to service
185
+ servicePtr->_txts = newtxt;
186
+ servicePtr->_txtLen += txtLen;
187
+ return true ;
188
+ }
189
+ else {
190
+ MDNSTxt * txtPtr = servicePtr->_txts ;
191
+ while (txtPtr->_next !=0 ) {
192
+ txtPtr = txtPtr->_next ;
176
193
}
177
- else {
178
- MDNSTxt * txtPtr = servicePtr->_txts ;
179
- while (txtPtr->_next !=0 ) {
180
- txtPtr = txtPtr->_next ;
181
- }
182
194
// adding another TXT to service
183
195
txtPtr->_next = newtxt;
184
196
servicePtr->_txtLen += txtLen;
@@ -302,11 +314,11 @@ void MDNSResponder::_parsePacket(){
302
314
hostNameLen = 0 ;
303
315
}
304
316
305
- if (hostNameLen > 0 && strcmp ( _hostName, hostName) != 0 && strcmp ( _instanceName, hostName) != 0 ){
317
+ if (hostNameLen > 0 && ! _hostName. equals ( hostName) && ! _instanceName. equals ( hostName)){
306
318
#ifdef MDNS_DEBUG_ERR
307
319
Serial.printf (" ERR_NO_HOST: %s\n " , hostName);
308
- Serial.printf (" hostname: %s\n " , hostName );
309
- Serial.printf (" instance: %s\n " , _instanceName);
320
+ Serial.printf (" hostname: %s\n " , _hostName. c_str () );
321
+ Serial.printf (" instance: %s\n " , _instanceName. c_str () );
310
322
#endif
311
323
_conn->flush ();
312
324
return ;
@@ -474,11 +486,12 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
474
486
Serial.printf (" TX: mask:%01X, service:%s, proto:%s, port:%u\n " , replyMask, service, proto, port);
475
487
#endif
476
488
477
- char * instanceName = _instanceName;
478
- size_t instanceNameLen = os_strlen (instanceName);
489
+
490
+ String instanceName = _instanceName;
491
+ size_t instanceNameLen = instanceName.length ();
479
492
480
- char * hostName = _hostName;
481
- size_t hostNameLen = os_strlen ( hostName);
493
+ String hostName = _hostName;
494
+ size_t hostNameLen = hostName. length ( );
482
495
483
496
char underscore[] = " _" ;
484
497
@@ -542,7 +555,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
542
555
543
556
// Send the RData (ie. "My IOT device._http._tcp.local")
544
557
_conn->append (reinterpret_cast <const char *>(&instanceNameLen), 1 ); // lenght of "My IOT device"
545
- _conn->append (reinterpret_cast <const char *>(instanceName), instanceNameLen);// "My IOT device"
558
+ _conn->append (reinterpret_cast <const char *>(instanceName. c_str () ), instanceNameLen);// "My IOT device"
546
559
_conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
547
560
_conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
548
561
_conn->append (reinterpret_cast <const char *>(&protoNameLen), 1 ); // lenght of "_tcp"
@@ -556,7 +569,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
556
569
if (replyMask & 0x4 ){
557
570
// Send the name field (ie. "My IOT device._http._tcp.local")
558
571
_conn->append (reinterpret_cast <const char *>(&instanceNameLen), 1 ); // lenght of "My IOT device"
559
- _conn->append (reinterpret_cast <const char *>(instanceName), instanceNameLen);// "My IOT device"
572
+ _conn->append (reinterpret_cast <const char *>(instanceName. c_str () ), instanceNameLen);// "My IOT device"
560
573
_conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
561
574
_conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
562
575
_conn->append (reinterpret_cast <const char *>(&protoNameLen), 1 ); // lenght of "_tcp"
@@ -578,11 +591,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
578
591
// Send the RData
579
592
MDNSTxt * txtPtr = _getServiceTxt (service,proto);
580
593
while (txtPtr !=0 ){
581
- uint8_t txtLen = os_strlen (txtPtr->_txt );
582
- _conn->append (reinterpret_cast <const char *>(&txtLen), 1 ); // lenght of txt
583
- _conn->append (reinterpret_cast <const char *>(txtPtr->_txt ), txtLen); // the txt
584
- // DEBUG Serial.print("We have txts: ");
585
- // DEBUG Serial.println(txtPtr->_txt);
594
+ uint8_t txtLen = txtPtr->_txt .length ();
595
+ _conn->append (reinterpret_cast <const char *>(&txtLen), 1 ); // lenght of txt
596
+ _conn->append (reinterpret_cast <const char *>(txtPtr->_txt .c_str ()), txtLen);// the txt
586
597
txtPtr = txtPtr->_next ;
587
598
}
588
599
}
@@ -592,7 +603,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
592
603
if (replyMask & 0x2 ){
593
604
// Send the name field (ie. "My IOT device._http._tcp.local")
594
605
_conn->append (reinterpret_cast <const char *>(&instanceNameLen), 1 ); // lenght of "My IOT device"
595
- _conn->append (reinterpret_cast <const char *>(instanceName), instanceNameLen);// "My IOT device"
606
+ _conn->append (reinterpret_cast <const char *>(instanceName. c_str () ), instanceNameLen);// "My IOT device"
596
607
_conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
597
608
_conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
598
609
_conn->append (reinterpret_cast <const char *>(&protoNameLen), 1 ); // lenght of "_tcp"
@@ -621,7 +632,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
621
632
_conn->append (reinterpret_cast <const char *>(srvRData), 6 );
622
633
// Send the RData (ie. "esp8266.local")
623
634
_conn->append (reinterpret_cast <const char *>(&hostNameLen), 1 ); // lenght of "esp8266"
624
- _conn->append (reinterpret_cast <const char *>(hostName) , hostNameLen); // "esp8266"
635
+ _conn->append (reinterpret_cast <const char *>(hostName. c_str ()) , hostNameLen);// "esp8266"
625
636
_conn->append (reinterpret_cast <const char *>(&localNameLen), 1 ); // lenght "local"
626
637
_conn->append (reinterpret_cast <const char *>(localName), localNameLen); // "local"
627
638
_conn->append (reinterpret_cast <const char *>(&terminator), 1 ); // terminator
@@ -632,7 +643,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
632
643
if (replyMask & 0x1 ){
633
644
// Send the RData (ie. "esp8266.local")
634
645
_conn->append (reinterpret_cast <const char *>(&hostNameLen), 1 ); // lenght of "esp8266"
635
- _conn->append (reinterpret_cast <const char *>(hostName) , hostNameLen); // "esp8266"
646
+ _conn->append (reinterpret_cast <const char *>(hostName. c_str ()) , hostNameLen);// "esp8266"
636
647
_conn->append (reinterpret_cast <const char *>(&localNameLen), 1 ); // lenght "local"
637
648
_conn->append (reinterpret_cast <const char *>(localName), localNameLen); // "local"
638
649
_conn->append (reinterpret_cast <const char *>(&terminator), 1 ); // terminator
0 commit comments