Skip to content

Commit d45294e

Browse files
author
Eric Wilkison
committed
fix invalid packets, removes compression
1 parent e7024fb commit d45294e

File tree

1 file changed

+135
-115
lines changed

1 file changed

+135
-115
lines changed

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 135 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,40 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
380380
if(replyMask == 0) return;
381381

382382
#ifdef MDNS_DEBUG_TX
383-
os_printf("TX: mask:%01X, service:%s, proto:%s, port:%u\n", replyMask, service, proto, port);
383+
Serial.printf("TX: mask:%01X, service:%s, proto:%s, port:%u\n", replyMask, service, proto, port);
384384
#endif
385385

386-
char nameLen = os_strlen(_hostName);
387-
size_t serviceLen = os_strlen(service);
386+
char * hostName = _hostName;
387+
size_t hostNameLen = os_strlen(hostName);
388+
389+
char underscore[] = "_";
390+
391+
// build service name with _
392+
char serviceName[os_strlen(service)+2];
393+
os_strcpy(serviceName,underscore);
394+
os_strcat(serviceName, service);
395+
size_t serviceNameLen = os_strlen(serviceName);
396+
397+
//build proto name with _
398+
char protoName[5];
399+
os_strcpy(protoName,underscore);
400+
os_strcat(protoName, proto);
401+
size_t protoNameLen = 4;
402+
403+
//local string
404+
char localName[] = "local";
405+
size_t localNameLen = 5;
406+
407+
//terminator
408+
char terminator[] = "\0";
388409

389410
uint8_t answerCount = 0;
390411
for(i=0;i<4;i++){
391412
if(replyMask & (1 << i)) answerCount++;
392413
}
393414

415+
416+
//Write the header
394417
_conn->flush();
395418
uint8_t head[12] = {
396419
0x00, 0x00, //ID = 0
@@ -402,139 +425,136 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
402425
};
403426
_conn->append(reinterpret_cast<const char*>(head), 12);
404427

405-
if((replyMask & 0x8) == 0){
406-
_conn->append(reinterpret_cast<const char*>(&nameLen), 1);
407-
_conn->append(reinterpret_cast<const char*>(_hostName), nameLen);
408-
}
409-
410-
if(replyMask & 0xE){
411-
uint8_t servHead[2] = {(uint8_t)(serviceLen+1), '_'};
412-
uint8_t protoHead[2] = {0x4, '_'};
413-
_conn->append(reinterpret_cast<const char*>(servHead), 2);
414-
_conn->append(reinterpret_cast<const char*>(service), serviceLen);
415-
_conn->append(reinterpret_cast<const char*>(protoHead), 2);
416-
_conn->append(reinterpret_cast<const char*>(proto), 3);
417-
}
418-
419-
uint8_t local[7] = {
420-
0x05, //strlen(_local)
421-
0x6C, 0x6F, 0x63, 0x61, 0x6C, //local
422-
0x00, //End of domain
423-
};
424-
_conn->append(reinterpret_cast<const char*>(local), 7);
425-
426428
// PTR Response
427429
if(replyMask & 0x8){
428-
uint8_t ptr[10] = {
429-
0x00, 0x0c, //PTR record query
430-
0x00, 0x01, //Class IN
430+
// Send the Name field (ie. "_http._tcp.local")
431+
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
432+
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
433+
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
434+
_conn->append(reinterpret_cast<const char*>(protoName), protoNameLen); // "_tcp"
435+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght "local"
436+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
437+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
438+
439+
//Send the type, class, ttl and rdata length
440+
uint8_t ptrDataLen = hostNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator
441+
uint8_t ptrAttrs[10] = {
442+
0x00, 0x0c, //PTR record query
443+
0x00, 0x01, //Class IN
431444
0x00, 0x00, 0x11, 0x94, //TTL 4500
432-
0x00, (uint8_t)(3 + nameLen), //***DATA LEN (3 + strlen(host))
445+
0x00, ptrDataLen, //RData length
433446
};
434-
_conn->append(reinterpret_cast<const char*>(ptr), 10);
435-
_conn->append(reinterpret_cast<const char*>(&nameLen), 1);
436-
_conn->append(reinterpret_cast<const char*>(_hostName), nameLen);
437-
uint8_t ptrTail[2] = {0xC0, 0x0C};
438-
_conn->append(reinterpret_cast<const char*>(ptrTail), 2);
447+
_conn->append(reinterpret_cast<const char*>(ptrAttrs), 10);
448+
449+
//Send the RData (ie. "esp8266._http._tcp.local")
450+
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
451+
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
452+
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
453+
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
454+
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
455+
_conn->append(reinterpret_cast<const char*>(protoName), protoNameLen); // "_tcp"
456+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght "local"
457+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
458+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
439459
}
440460

441-
// TXT Response
461+
//TXT Responce
442462
if(replyMask & 0x4){
443-
if(replyMask & 0x8){
444-
uint8_t txtHead[10] = {
445-
0xC0, (uint8_t)(36 + serviceLen),//send the name
446-
0x00, 0x10, //Type TXT
447-
0x80, 0x01, //Class IN, with cache flush
448-
0x00, 0x00, 0x11, 0x94, //TTL 4500
449-
};
450-
_conn->append(reinterpret_cast<const char*>(txtHead), 10);
451-
}
463+
//Send the name field (ie. "esp8266._http._tcp.local")
464+
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
465+
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
466+
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
467+
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
468+
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
469+
_conn->append(reinterpret_cast<const char*>(protoName), protoNameLen); // "_tcp"
470+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght "local"
471+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
472+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
473+
474+
//Send the type, class, ttl and rdata length
475+
uint8_t txtDataLen = 0;
476+
uint8_t txtAttrs[10] = {
477+
0x00, 0x10, //TXT record query
478+
0x00, 0x01, //Class IN
479+
0x00, 0x00, 0x11, 0x94, //TTL 4500
480+
0x00, txtDataLen, //RData length
481+
};
482+
_conn->append(reinterpret_cast<const char*>(txtAttrs), 10);
452483

453-
if(strcmp(reinterpret_cast<const char*>("arduino"), service) == 0){
454-
//arduino
455-
//arduino service dependance should be removed and properties abstracted
456-
const char *tcpCheckExtra = "tcp_check=no";
457-
uint8_t tcpCheckExtraLen = os_strlen(tcpCheckExtra);
458-
459-
const char *sshUploadExtra = "ssh_upload=no";
460-
uint8_t sshUploadExtraLen = os_strlen(sshUploadExtra);
461-
462-
char boardName[64];
463-
const char *boardExtra = "board=";
464-
os_sprintf(boardName, "%s%s", boardExtra, ARDUINO_BOARD);
465-
uint8_t boardNameLen = os_strlen(boardName);
466-
467-
char authUpload[16];
468-
const char *authUploadExtra = "auth_upload=";
469-
os_sprintf(authUpload, "%s%s", authUploadExtra, reinterpret_cast<const char*>((_arduinoAuth)?"yes":"no"));
470-
uint8_t authUploadLen = os_strlen(authUpload);
471-
472-
uint16_t textDataLen = (1 + boardNameLen) + (1 + tcpCheckExtraLen) + (1 + sshUploadExtraLen) + (1 + authUploadLen);
473-
uint8_t txt[2] = {(uint8_t)(textDataLen >> 8), (uint8_t)(textDataLen)}; //DATA LEN
474-
_conn->append(reinterpret_cast<const char*>(txt), 2);
475-
476-
_conn->append(reinterpret_cast<const char*>(&boardNameLen), 1);
477-
_conn->append(reinterpret_cast<const char*>(boardName), boardNameLen);
478-
_conn->append(reinterpret_cast<const char*>(&authUploadLen), 1);
479-
_conn->append(reinterpret_cast<const char*>(authUpload), authUploadLen);
480-
_conn->append(reinterpret_cast<const char*>(&tcpCheckExtraLen), 1);
481-
_conn->append(reinterpret_cast<const char*>(tcpCheckExtra), tcpCheckExtraLen);
482-
_conn->append(reinterpret_cast<const char*>(&sshUploadExtraLen), 1);
483-
_conn->append(reinterpret_cast<const char*>(sshUploadExtra), sshUploadExtraLen);
484-
} else {
485-
//not arduino
486-
//we should figure out an API so TXT properties can be added for services
487-
uint8_t txt[2] = {0,0};
488-
_conn->append(reinterpret_cast<const char*>(txt), 2);
489-
}
484+
//Send the RData
485+
//TODO - Build TXT Redords
490486
}
491487

492-
// SRV Response
493-
if(replyMask & 0x2){
494-
if(replyMask & 0xC){//send the name
495-
uint8_t srvHead[2] = {0xC0, 0x0C};
496-
if(replyMask & 0x8)
497-
srvHead[1] = 36 + serviceLen;
498-
_conn->append(reinterpret_cast<const char*>(srvHead), 2);
499-
}
500488

501-
uint8_t srv[16] = {
502-
0x00, 0x21, //Type SRV
503-
0x80, 0x01, //Class IN, with cache flush
489+
//SRV Responce
490+
if(replyMask & 0x2){
491+
//Send the name field (ie. "esp8266._http._tcp.local")
492+
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
493+
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
494+
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
495+
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
496+
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
497+
_conn->append(reinterpret_cast<const char*>(protoName), protoNameLen); // "_tcp"
498+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght "local"
499+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
500+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
501+
502+
//Send the type, class, ttl, rdata length, priority and weight
503+
uint8_t srvDataSize = hostNameLen + localNameLen + 3; // 3 is 2 lable size bytes and the terminator
504+
srvDataSize += 6; // Size of Priority, weight and port
505+
uint8_t srvAttrs[10] = {
506+
0x00, 0x21, //Type SRV
507+
0x80, 0x01, //Class IN, with cache flush
504508
0x00, 0x00, 0x00, 0x78, //TTL 120
505-
0x00, (uint8_t)(9 + nameLen), //DATA LEN (9 + strlen(host))
506-
0x00, 0x00, //Priority 0
507-
0x00, 0x00, //Weight 0
508-
(uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF)
509+
0x00, srvDataSize, //RData length
510+
};
511+
_conn->append(reinterpret_cast<const char*>(srvAttrs), 10);
512+
513+
//Send the RData Priority weight and port
514+
uint8_t srvRData[6] = {
515+
0x00, 0x00, //Priority 0
516+
0x00, 0x00, //Weight 0
517+
(uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF)
509518
};
510-
_conn->append(reinterpret_cast<const char*>(srv), 16);
511-
_conn->append(reinterpret_cast<const char*>(&nameLen), 1);
512-
_conn->append(reinterpret_cast<const char*>(_hostName), nameLen);
513-
uint8_t srvTail[2] = {0xC0, (uint8_t)(20 + serviceLen + nameLen)};
514-
if(replyMask & 0x8)
515-
srvTail[1] = 19 + serviceLen;
516-
_conn->append(reinterpret_cast<const char*>(srvTail), 2);
519+
_conn->append(reinterpret_cast<const char*>(srvRData), 6);
520+
//Send the RData (ie. "esp8266.local")
521+
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
522+
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
523+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght "local"
524+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
525+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
526+
517527
}
518528

519529
// A Response
520530
if(replyMask & 0x1){
521-
uint32_t ip = _getOurIp();
522-
if(replyMask & 0x2){//send the name (no compression for now)
523-
_conn->append(reinterpret_cast<const char*>(&nameLen), 1);
524-
_conn->append(reinterpret_cast<const char*>(_hostName), nameLen);
525-
_conn->append(reinterpret_cast<const char*>(local), 7);
526-
}
531+
//Send the RData (ie. "esp8266.local")
532+
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
533+
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
534+
_conn->append(reinterpret_cast<const char*>(&localNameLen), 1); // lenght "local"
535+
_conn->append(reinterpret_cast<const char*>(localName), localNameLen); // "local"
536+
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
527537

528-
uint8_t aaa[14] = {
529-
0x00, 0x01, //TYPE A
530-
0x80, 0x01, //Class IN, with cache flush
538+
uint32_t ip = _getOurIp();
539+
uint8_t aaaAttrs[10] = {
540+
0x00, 0x01, //TYPE A
541+
0x80, 0x01, //Class IN, with cache flush
531542
0x00, 0x00, 0x00, 0x78, //TTL 120
532-
0x00, 0x04, //DATA LEN
533-
(uint8_t)(ip & 0xFF), (uint8_t)((ip >> 8) & 0xFF), (uint8_t)((ip >> 16) & 0xFF), (uint8_t)((ip >> 24) & 0xFF)
543+
0x00, 0x04, //DATA LEN
544+
};
545+
_conn->append(reinterpret_cast<const char*>(aaaAttrs), 10);
546+
547+
// Send RData
548+
uint8_t aaaRData[4] = {
549+
(uint8_t)(ip & 0xFF), //IP first octet
550+
(uint8_t)((ip >> 8) & 0xFF), //IP second octet
551+
(uint8_t)((ip >> 16) & 0xFF), //IP third octet
552+
(uint8_t)((ip >> 24) & 0xFF) //IP fourth octet
534553
};
535-
_conn->append(reinterpret_cast<const char*>(aaa), 14);
554+
_conn->append(reinterpret_cast<const char*>(aaaRData), 4);
536555
}
537-
_conn->send();
556+
557+
_conn->send();
538558
}
539559

540560
MDNSResponder MDNS = MDNSResponder();

0 commit comments

Comments
 (0)