codeplea/Hands-On-Network-Programming-with-C

DNS domain name should not be longer than 253 characters

Closed this issue · 2 comments

Hi!

I found I minor issue with information about domain name lengths in the book and it's reflected int the dns_query.c code from chapter 5.

Specifically, the book mentions:

It also checks that the hostname isn't more than 255
characters long. Hostnames longer than that aren't allowed by the DNS standard, and
checking it now ensures that we don't need to allocate too much memory.

Which is reflected in the code here:

if (strlen(argv[1]) > 255) {
fprintf(stderr, "Hostname too long.");
exit(1);
}

But the DNS specification mentions:

To simplify implementations, the total number of octets that represent a
domain name (i.e., the sum of all label octets and label lengths) is
limited to 255.

See https://datatracker.ietf.org/doc/html/rfc1034#section-3.1

That effectively limits readable ASCII domain names to only 253 characters (excluding the optional . at the end).

For example using the dns_query program from the book as such:
./dns_query 123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789a.xyz a

Leads to a response indicating a format error:

QR = 1 response
OPCODE = 0 standard
AA = 0 
TC = 0 
RD = 1 recursion desired
RCODE = 1 format error

That is a readable domain name of length 254, which is more than the allowed 253 readable characters. Encoded in the DNS binary format, the total number of octets that it's represented by is 256 which is more than the allowed 255.

So as expected trying with a domain name that's only 253 characters long works just fine (notice the second level domain name is changed from 123456789a to 123456789):

./dns_query 123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.xyz a

QR = 1 response
OPCODE = 0 standard
AA = 0 
TC = 0 
RD = 1 recursion desired
RCODE = 0 success
QDCOUNT = 1
ANCOUNT = 1
NSCOUNT = 0
ARCOUNT = 0

Reference to a practical explanation: https://devblogs.microsoft.com/oldnewthing/?p=7873

P.S.: I'm not a complete newbie in network programming but I still found your book useful, and I quite like it as a reference material.

Thank you for your feedback! This is a great, well-written report with tons of detail. I really appreciate the effort you put into it!

Now I need to figure out how to best fix/update the issue in the code and errata.

P.S.: I'm not a complete newbie in network programming but I still found your book useful, and I quite like it as a reference material.

Thank you for the kind words too.

I fixed it in the code and added to errata. https://handsonnetworkprogramming.com/errata/