The term endianness refers to the order in which individual bytes are stored in memory or sent over a network.
Whenever we read a multi-byte number from a DNS message, we should be aware that it's in big-endian format (or so-called network byte order). The computer you're using likely uses little-endian format, although we are careful to write our code in an endian-independent manner throughout this book. We accomplish this by avoiding the conversion of multiple bytes directly to integers, and instead we interpret bytes one at a time.
For example, consider a message with a single 8-bit value, such as 0x05. We know that the value of that message is 5. Bytes are sent atomically over a network link, so we also know that anyone receiving our message can unambiguously interpret that message as 5.
The issue of endianness comes into play when we need more than one byte to store our number. Imagine that we want to send the number 999. This number is too big to fit into 1 byte, so we have to break it up into 2 bytes—a 16-bit value. Because 999 = (3 * 28) + 231, we know that the high-order byte stores 3 while the low-order byte stores 231. In hexadecimal, the number 999 is 0x03E7. The question is whether to send the high-order or the low-order byte over the network first.
Network byte order, which is used by the DNS protocol, specifies that the high-order byte is sent first. Therefore, the number 999 is sent over the network as a 0x03 followed by 0xE7.
See the Further reading section of this chapter for more information.
Let's now look at encoding an entire DNS query.