@@ -85,9 +85,25 @@ static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251);
8585static const int MDNS_MULTICAST_TTL = 1 ;
8686static const int MDNS_PORT = 5353 ;
8787
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+
88104MDNSResponder::MDNSResponder () : _conn(0 ) {
89105 _services = 0 ;
90- _instanceName[ 0 ] = 0 ;
106+ _instanceName = " " ;
91107}
92108MDNSResponder::~MDNSResponder () {}
93109
@@ -100,12 +116,11 @@ bool MDNSResponder::begin(const char* hostname){
100116 }
101117
102118 // 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 ();
106121
107122 // 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;
109124
110125 // Open the MDNS socket if it isn't already open.
111126 if (!_conn) {
@@ -145,9 +160,9 @@ void MDNSResponder::update() {
145160}
146161
147162
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;
151166}
152167
153168
@@ -156,29 +171,26 @@ bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *valu
156171
157172 uint8_t txtLen = os_strlen (key) + os_strlen (value) + 1 ; // Add one for equals sign
158173 txtLen+=1 ; // accounts for length byte added when building the txt responce
159- if ( txtLen > 128 ) return false ;
160174 // Find the service
161175 for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next ) {
162176 // Checking Service names
163177 if (strcmp (servicePtr->_name , name) == 0 && strcmp (servicePtr->_proto , proto) == 0 ){
178+ // found a service name match
164179 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 ;
176193 }
177- else {
178- MDNSTxt * txtPtr = servicePtr->_txts ;
179- while (txtPtr->_next !=0 ) {
180- txtPtr = txtPtr->_next ;
181- }
182194 // adding another TXT to service
183195 txtPtr->_next = newtxt;
184196 servicePtr->_txtLen += txtLen;
@@ -302,11 +314,11 @@ void MDNSResponder::_parsePacket(){
302314 hostNameLen = 0 ;
303315 }
304316
305- if (hostNameLen > 0 && strcmp ( _hostName, hostName) != 0 && strcmp ( _instanceName, hostName) != 0 ){
317+ if (hostNameLen > 0 && ! _hostName. equals ( hostName) && ! _instanceName. equals ( hostName)){
306318#ifdef MDNS_DEBUG_ERR
307319 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 () );
310322#endif
311323 _conn->flush ();
312324 return ;
@@ -474,11 +486,12 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
474486 Serial.printf (" TX: mask:%01X, service:%s, proto:%s, port:%u\n " , replyMask, service, proto, port);
475487#endif
476488
477- char * instanceName = _instanceName;
478- size_t instanceNameLen = os_strlen (instanceName);
489+
490+ String instanceName = _instanceName;
491+ size_t instanceNameLen = instanceName.length ();
479492
480- char * hostName = _hostName;
481- size_t hostNameLen = os_strlen ( hostName);
493+ String hostName = _hostName;
494+ size_t hostNameLen = hostName. length ( );
482495
483496 char underscore[] = " _" ;
484497
@@ -542,7 +555,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
542555
543556 // Send the RData (ie. "My IOT device._http._tcp.local")
544557 _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"
546559 _conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
547560 _conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
548561 _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
556569 if (replyMask & 0x4 ){
557570 // Send the name field (ie. "My IOT device._http._tcp.local")
558571 _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"
560573 _conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
561574 _conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
562575 _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
578591 // Send the RData
579592 MDNSTxt * txtPtr = _getServiceTxt (service,proto);
580593 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
586597 txtPtr = txtPtr->_next ;
587598 }
588599 }
@@ -592,7 +603,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
592603 if (replyMask & 0x2 ){
593604 // Send the name field (ie. "My IOT device._http._tcp.local")
594605 _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"
596607 _conn->append (reinterpret_cast <const char *>(&serviceNameLen), 1 ); // lenght of "_http"
597608 _conn->append (reinterpret_cast <const char *>(serviceName), serviceNameLen); // "_http"
598609 _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
621632 _conn->append (reinterpret_cast <const char *>(srvRData), 6 );
622633 // Send the RData (ie. "esp8266.local")
623634 _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"
625636 _conn->append (reinterpret_cast <const char *>(&localNameLen), 1 ); // lenght "local"
626637 _conn->append (reinterpret_cast <const char *>(localName), localNameLen); // "local"
627638 _conn->append (reinterpret_cast <const char *>(&terminator), 1 ); // terminator
@@ -632,7 +643,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
632643 if (replyMask & 0x1 ){
633644 // Send the RData (ie. "esp8266.local")
634645 _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"
636647 _conn->append (reinterpret_cast <const char *>(&localNameLen), 1 ); // lenght "local"
637648 _conn->append (reinterpret_cast <const char *>(localName), localNameLen); // "local"
638649 _conn->append (reinterpret_cast <const char *>(&terminator), 1 ); // terminator
0 commit comments