An MX record names a mail server plus a preference number that orders competing servers. Today you parse its two-part RDATA - a 16-bit preference followed by a domain name.
Parse an MX record's preference and exchange name from its RDATA.
An MX record points mail for a domain at a mail server, and it has two parts: a 16-bit preference and an exchange name (the server). The preference orders alternatives - lower numbers are tried first - so a domain can list several mail servers with different preferences. On the wire the preference is the first two bytes of RDATA, and the exchange name follows immediately, encoded just like any other name and eligible for compression.
So parsing MX is: read a 16-bit value, then decode a name starting two bytes into
the RDATA (against the full message, so pointers resolve). The RDATA here gives
preference 10 and exchange mail.example.com. This preference-then-name shape is
a common pattern - a couple of fixed fields followed by a name - and it is good
practice for the multi-field record coming next.
type MX struct {Preference uint16Exchange string}// first two bytes are the preference, then a name (which may compress)pref := uint16At(rdata, 0)name, _ := decodeName(msg, rdataOff+2)