Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressed dns #3769

Merged
merged 4 commits into from
Mar 16, 2018
Merged

Conversation

jonatanolofsson
Copy link
Contributor

According to (RFC 1035), clients must understand compressed dns messages. These commits aim to add the support of this to the ESP8266mDNS library.

@@ -539,15 +539,24 @@ void MDNSResponder::_parsePacket(){
while (numAnswers--) {
// Read name
stringsRead = 0;
size_t here = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this into something more meaningful

@@ -632,31 +649,36 @@ void MDNSResponder::_parsePacket(){
uint16_t answerPrio = _conn_read16(); // Read priority
uint16_t answerWeight = _conn_read16(); // Read weight
answerPort = _conn_read16(); // Read port
size_t here = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this declaration of here shadow the declaration on line 542?

@jonatanolofsson jonatanolofsson force-pushed the develop/compressed-dns branch 2 times, most recently from e3a3a00 to 873a173 Compare December 12, 2017 08:25
@jonatanolofsson
Copy link
Contributor Author

Thank you for the feedback, I updated the code accordingly.

Copy link
Collaborator

@devyte devyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the changes of compressed dns, but the code changes look ok.

@devyte
Copy link
Collaborator

devyte commented Jan 3, 2018

Help needed with testing this!

@devyte devyte added the help wanted Help needed from the community label Jan 5, 2018
@jonatanolofsson
Copy link
Contributor Author

The way I discovered this was missing was that I got errors with mDNS when connecting to a mac (which is compressing its data). Thus, to test this, set up a local domain with dns-sd with and without this patch and monitor the esp8266 errors on the serial port as it tries to connect (compile with DEBUG_ESP_MDNS_RX)

@devyte
Copy link
Collaborator

devyte commented Jan 7, 2018

@jonatanolofsson my wife has a mac. I hate the thing, but I could use it to test this, if you provide detailed instructions for the setup on it.

if (tmp8 == 0x00) { // End of name
break;
}
if (tmp8 & 0xC0) { // Compressed pointer
uint16_t offset = ((((uint16_t)tmp8) & 0xC0) << 8) | _conn_read8();
Copy link
Collaborator

@d-a-v d-a-v Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Would the resulting offset's boundaries be checked here ?

  • If the first byte is 0xca and the second 0xaa, then the offset would be 0xcaaa.
    Would it not be 0xaaa like with (((uint16_t)(tmp8 & ~0xC0)) << 8) | _conn_read8() ?


(void) answerPrio;
(void) answerWeight;

// Read hostname
tmp8 = _conn_read8();
if (tmp8 & 0xC0) { // Compressed pointer (not supported)
if (tmp8 & 0xC0) { // Compressed pointer
uint16_t offset = ((((uint16_t)tmp8) & 0xC0) << 8) | _conn_read8();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it &~0xc0 too here ?


(void) answerPrio;
(void) answerWeight;

// Read hostname
tmp8 = _conn_read8();
if (tmp8 & 0xC0) { // Compressed pointer (not supported)
if (tmp8 & 0xC0) { // Compressed pointer
uint16_t offset = ((((uint16_t)tmp8) & 0xC0) << 8) | _conn_read8();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the calculated offset can be checked beeing out of bound to prevent crashing with malformed or crafted dns replies ?

@jonatanolofsson jonatanolofsson force-pushed the develop/compressed-dns branch 2 times, most recently from dcd569f to 1adb42f Compare January 8, 2018 15:02
}

bool isValidOffset(const size_t pos) const {
return (pos <= _rx_buf->len);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
Since you are going to use this offset, would'nt it be pos < _rx_buf->len ?
Sorry I don't mean to be pushy :]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all, I appreciate your comments, I just havn't had much time to do more than fix them during breaks at work today =D
The check on line 222 in the 'read' function checks if the offset is at least equal to the limit, which should give that to set the buffer as "fully read", the offset should be set just past the limit, i.e. equal to the length. I figured "finished reading" was a valid use-case and thus a valid offset.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation

DEBUG_ESP_PORT.println(offset);
#endif
_conn->seek(offset);
tmp8 = _conn_read8();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you reading tmp8 twice (here and in L568) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I forgot to remove the second one. Fixed.

@d-a-v
Copy link
Collaborator

d-a-v commented Jan 10, 2018

@jonatanolofsson can you tell us more about the steps to test your PR ?
Wife also has a mac. Wives...

@jonatanolofsson
Copy link
Contributor Author

I'll try to find time to make a test description. Not going to happen this week unfortunately. My computer is a mac, but I'm only ever booting into linux anymore :)

@d-a-v
Copy link
Collaborator

d-a-v commented Jan 10, 2018

Linux is no problem if you know how to update avahi's conffile so.

@jonatanolofsson
Copy link
Contributor Author

Right, I wasn't sure if avahi compressed the payload

@devyte
Copy link
Collaborator

devyte commented Mar 16, 2018

@jonatanolofsson I now understand your code changes, and I think they are correct. I am currently rewriting the mdns code based on the idf functionality, and was struggling a bit with compression (it's very different in idf due to fundamental differences in how things work at low level). This helps quite a bit. I'll merge this, and integrate it into my current work.

@devyte devyte merged commit 461c922 into esp8266:master Mar 16, 2018
bryceschober pushed a commit to bryceschober/Arduino that referenced this pull request Apr 5, 2018
* Add UdpContext seek and tell

* Add support for DNS compressed messages

* mDNS compressed pointer: Validate offset before jumping

(cherry picked from commit 461c922)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Help needed from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants