Skip to content

Commit 85bbb38

Browse files
Add support for DNS compressed messages
1 parent 5b0a1df commit 85bbb38

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ boards.local.txt
1919
*.gcno
2020
*.gcda
2121
*.o
22+
*.a

libraries/ESP8266mDNS/ESP8266mDNS.cpp

+49-19
Original file line numberDiff line numberDiff line change
@@ -542,15 +542,24 @@ void MDNSResponder::_parsePacket(){
542542
while (numAnswers--) {
543543
// Read name
544544
stringsRead = 0;
545+
size_t last_bufferpos = 0;
545546
do {
546547
tmp8 = _conn_read8();
547-
if (tmp8 & 0xC0) { // Compressed pointer (not supported)
548-
tmp8 = _conn_read8();
549-
break;
550-
}
551548
if (tmp8 == 0x00) { // End of name
552549
break;
553550
}
551+
if (tmp8 & 0xC0) { // Compressed pointer
552+
uint16_t offset = ((((uint16_t)tmp8) & ~0xC0) << 8) | _conn_read8();
553+
last_bufferpos = _conn->tell();
554+
#ifdef DEBUG_ESP_MDNS_RX
555+
DEBUG_ESP_PORT.print("Compressed pointer, jumping from ");
556+
DEBUG_ESP_PORT.print(last_bufferpos);
557+
DEBUG_ESP_PORT.print(" to ");
558+
DEBUG_ESP_PORT.println(offset);
559+
#endif
560+
_conn->seek(offset);
561+
tmp8 = _conn_read8();
562+
}
554563
if(stringsRead > 3){
555564
#ifdef DEBUG_ESP_MDNS_RX
556565
DEBUG_ESP_PORT.println("failed to read the response name");
@@ -577,6 +586,14 @@ void MDNSResponder::_parsePacket(){
577586
}
578587
stringsRead++;
579588
} while (true);
589+
if (last_bufferpos > 0)
590+
{
591+
_conn->seek(last_bufferpos);
592+
#ifdef DEBUG_ESP_MDNS_RX
593+
DEBUG_ESP_PORT.print("Compressed pointer, jumping back to ");
594+
DEBUG_ESP_PORT.println(last_bufferpos);
595+
#endif
596+
}
580597

581598
uint16_t answerType = _conn_read16(); // Read type
582599
uint16_t answerClass = _conn_read16(); // Read class
@@ -635,32 +652,45 @@ void MDNSResponder::_parsePacket(){
635652
uint16_t answerPrio = _conn_read16(); // Read priority
636653
uint16_t answerWeight = _conn_read16(); // Read weight
637654
answerPort = _conn_read16(); // Read port
655+
last_bufferpos = 0;
638656

639657
(void) answerPrio;
640658
(void) answerWeight;
641659

642660
// Read hostname
643661
tmp8 = _conn_read8();
644-
if (tmp8 & 0xC0) { // Compressed pointer (not supported)
662+
if (tmp8 & 0xC0) { // Compressed pointer
663+
uint16_t offset = ((((uint16_t)tmp8) & ~0xC0) << 8) | _conn_read8();
664+
last_bufferpos = _conn->tell();
645665
#ifdef DEBUG_ESP_MDNS_RX
646-
DEBUG_ESP_PORT.println("Skipping compressed pointer");
666+
DEBUG_ESP_PORT.print("Compressed pointer, jumping from ");
667+
DEBUG_ESP_PORT.print(last_bufferpos);
668+
DEBUG_ESP_PORT.print(" to ");
669+
DEBUG_ESP_PORT.println(offset);
647670
#endif
671+
_conn->seek(offset);
648672
tmp8 = _conn_read8();
649673
}
650-
651-
else {
652-
_conn_readS(answerHostName, tmp8);
653-
answerHostName[tmp8] = '\0';
674+
_conn_readS(answerHostName, tmp8);
675+
answerHostName[tmp8] = '\0';
654676
#ifdef DEBUG_ESP_MDNS_RX
655-
DEBUG_ESP_PORT.printf("SRV %d ", tmp8);
656-
for (int n = 0; n < tmp8; n++) {
657-
DEBUG_ESP_PORT.printf("%02x ", answerHostName[n]);
658-
}
659-
DEBUG_ESP_PORT.printf("\n%s\n", answerHostName);
677+
DEBUG_ESP_PORT.printf("SRV %d ", tmp8);
678+
for (int n = 0; n < tmp8; n++) {
679+
DEBUG_ESP_PORT.printf("%02x ", answerHostName[n]);
680+
}
681+
DEBUG_ESP_PORT.printf("\n%s\n", answerHostName);
660682
#endif
661-
if (answerRdlength - (6 + 1 + tmp8) > 0) { // Skip any remaining rdata
662-
_conn_readS(hostName, answerRdlength - (6 + 1 + tmp8));
663-
}
683+
if (last_bufferpos > 0)
684+
{
685+
_conn->seek(last_bufferpos);
686+
tmp8 = 2; // Size of compression octets
687+
#ifdef DEBUG_ESP_MDNS_RX
688+
DEBUG_ESP_PORT.print("Compressed pointer, jumping back to ");
689+
DEBUG_ESP_PORT.println(last_bufferpos);
690+
#endif
691+
}
692+
if (answerRdlength - (6 + 1 + tmp8) > 0) { // Skip any remaining rdata
693+
_conn_readS(hostName, answerRdlength - (6 + 1 + tmp8));
664694
}
665695
}
666696

@@ -675,7 +705,7 @@ void MDNSResponder::_parsePacket(){
675705
DEBUG_ESP_PORT.printf("Ignoring unsupported type %02x\n", tmp8);
676706
#endif
677707
for (int n = 0; n < answerRdlength; n++)
678-
(void)_conn_read8();
708+
(void)_conn_read8();
679709
}
680710

681711
if ((partsCollected == 0x0F) && serviceMatch) {

0 commit comments

Comments
 (0)