DNS Name Compression

In this article I explain how the DNS name compression works and I give an example.

DNS name compression

A DNS response message can contain the same domain name multiple times. This repetition is a waste of bits in the message. A compression technique can be used to reduce the number used bits and replace the repeated domain name by a pointer.

A compression label is a pointer that appears in the packet instead of the count byte and the following label items. It is written on 16 bits and has the following format:

DNS-11
Figure: the DNS Offset field © IBM.com

Remember when I said that the label length of a data label is one byte long, and its value is between 0 and 63? 63 is 00111111 in binary. The pointer, however, has the first two bits set to 1, to differentiate it from data labels.

Here is how the pointer is structured : the first 2 bits are set to 1, the 14 remaining bits describe the offset, i.e. the position of the compression target from the beginning of the DNS message.

The pointer can only be used if the pointed domain name (called compression target) has already been mentioned in the DNS message (you can’t point to a something that does not exist already).

DNS name compression example

Here is an example to illustrate the use of name compression. Let’s assume a DNS UDP datagram needs to use the following domain names: F.ISI.ARPA, FOO.F.ISI.ARPA and ARPA. In this example we assume that bytes start at offset 0.

The first domain name F.ISI.ARPA will be encoded as a data label because it appeared for the first time in the DNS packet. Here is the coding of the domain name F.ISI.ARPA :

dns-12
Figure: DNS domain name F.ISI.ARPA as it appears in the datagram. It’s il all data labels. Notice the label of length 0 at the end.

Now here is how FOO.F.ISI.ARPA is encoded: FOO appears for the first time so it is encoded as a data label. Then, since F.ISI.ARPA was already mentioned in the datagram, we can simply leverage compression and point to F.ISI.ARPA by indicating the offset (here F.ISI.ARPA appeared the first time at byte number 20).

Also, ARPA appeared before at byte number 26. So we simply point to it by its offset:

dns-13
Figure: An example in using DNS compression labels in a DNS packet

Comments

Popular posts from this blog

The Thing With CUCM Device Packs

En Bloc Dialing vs Digit-By-Digit Dialing

Call Forward And Call Hunt on CUCM