| 1 |
/* |
| 2 |
Package dns implements a full featured interface to the Domain Name System - DNS (RFC 1033, RFC 1034, RFC 1035). |
| 3 |
Both server- and client-side programming is supported. |
| 4 |
|
| 5 |
The package allows complete control over what is sent out to the DNS. The API follows the less-is-more |
| 6 |
principle, by presenting a small, clean interface. |
| 7 |
|
| 8 |
It supports (asynchronous) querying/replying, incoming/outgoing zone transfers, |
| 9 |
[TSIG], [EDNS0], dynamic updates, notifies and DNSSEC validation/signing. |
| 10 |
|
| 11 |
Resource records ([RR]s) are native types. They are not stored in wire format, but every [Msg] holds the |
| 12 |
wire-format in its [Msg.Data] field. Everything is modelled or made to look like an RR: the question section holds a |
| 13 |
[RR] and the [EDNS0] option codes are also RRs. These EDNS0 options occupy a separate section in |
| 14 |
[Msg], the [Msg.Pseudo] section. |
| 15 |
|
| 16 |
In the DNS, messages ([Msg]) are exchanged, these messages contain RRs ([RR]) and/or RRsets ([RRset]). Basic |
| 17 |
pattern for creating a message: |
| 18 |
|
| 19 |
m := new(dns.Msg) |
| 20 |
m.Question = []dns.RR{mx} |
| 21 |
|
| 22 |
Or faster, with the correct header bits: |
| 23 |
|
| 24 |
m := dns.NewMsg("miek.nl.", dns.TypeMX) |
| 25 |
|
| 26 |
The message m is now a message with the question section set to ask the [MX] records for the miek.nl. zone. |
| 27 |
When making an actual request: |
| 28 |
|
| 29 |
m.ID = dns.ID() |
| 30 |
m.RecursionDesired = true |
| 31 |
|
| 32 |
After creating a message it can be sent. Basic use pattern for synchronous querying the DNS at a server |
| 33 |
configured on 127.0.0.1 and port 53 using UDP: |
| 34 |
|
| 35 |
c := new(dns.Client) |
| 36 |
r, rtt, err := c.Exchange(context.TODO(), m, "udp", "127.0.0.1:53") |
| 37 |
|
| 38 |
When this functions returns you will get DNS message back. A DNS message consists out of four (on the wire), |
| 39 |
but five in this package, sections. |
| 40 |
|
| 41 |
- The question section: r.Question, see [Msg.Question]. |
| 42 |
- The answer section: r.Answer, see [Msg.Answer]. |
| 43 |
- The authority section: r.Ns, see [Msg.Ns]. |
| 44 |
- The additional section: r.Extra, see [Msg.Extra]. |
| 45 |
- And the extra and new, the pseudo section: r.Pseudo, see [Msg.Pseudo]. |
| 46 |
|
| 47 |
The latter was added to make it easier to deal with [EDNS0] option codes, which become more and more prevalent. |
| 48 |
|
| 49 |
Each of these sections contain a []RR. Basic use pattern for accessing the rdata of a [TXT] [RR] as the first |
| 50 |
[RR] in the Answer section: |
| 51 |
|
| 52 |
if t, ok := r.Answer[0].(*dns.TXT); ok { |
| 53 |
// do something with t.TXT.Txt |
| 54 |
} |
| 55 |
|
| 56 |
Or if you sent an NSID EDNS0 option: |
| 57 |
|
| 58 |
if n, ok := r.Pseudo[0].(*dns.NSID); ok { |
| 59 |
// do something with n.Nsid |
| 60 |
} |
| 61 |
|
| 62 |
# Domain Name and TXT Character String Representations |
| 63 |
|
| 64 |
Domain names are converted to presentation form as-is, there is no conversion of unprintable characters, i.e. |
| 65 |
\DDD and \. are left as-is. The only exception is the SOA's Mbox where \. is detected and encoded. |
| 66 |
|
| 67 |
TXT character strings are converted to presentation form both when unpacked and when converted to strings. |
| 68 |
Tabs, carriage returns and line feeds will be converted to \t, \r and \n respectively. Back slashes and |
| 69 |
quotations marks will be escaped. Bytes below 32 and above 127 will be converted to \DDD form. |
| 70 |
|
| 71 |
# DNSSEC |
| 72 |
|
| 73 |
DNSSEC (DNS Security Extension) adds a layer of security to the DNS. It uses public key cryptography to sign |
| 74 |
resource records. The public keys are stored in [DNSKEY] records and the signatures in [RRSIG] records. |
| 75 |
|
| 76 |
Requesting DNSSEC information for a zone is done by adding the DO (DNSSEC OK) bit to a request. |
| 77 |
|
| 78 |
m := new(dns.Msg) |
| 79 |
m.Security = true |
| 80 |
m.UDPSize = 4096 |
| 81 |
|
| 82 |
When sending a message [Msg.Pack] is called, this takes care of allocating an [OPT] [RR] and setting the DO |
| 83 |
bit and the UDP Size in there. |
| 84 |
|
| 85 |
Signature generation, signature verification (see [RRSIG]) and key generation are all supported. |
| 86 |
|
| 87 |
# EDNS0 |
| 88 |
|
| 89 |
[EDNS0] is an extension mechanism for the DNS defined in RFC 2671 and updated by RFC 6891. It defines a RR |
| 90 |
type, the [OPT] [RR], which holds type-length-value sub-types. In this package all EDNS0 options are |
| 91 |
implemented as RRs. Doing basic "EDNS0" things, like setting the DNSSEC OK bit (DO) or the UDP buffer size is |
| 92 |
handled for you and these can be set directly on message as shown above. |
| 93 |
|
| 94 |
The data of an [OPT] [RR] sits in the [Msg] Pseudo section consists out of a slice of [EDNS0] (RFC 6891) |
| 95 |
interfaces. These are just RRs with an extra Pseudo() method. |
| 96 |
|
| 97 |
Basic use pattern for a server to check if (and which) options are set, which is similar to how to deal with |
| 98 |
RRs. |
| 99 |
|
| 100 |
for _, rr := range m.Pseudo { |
| 101 |
switch x := rr.(type) { |
| 102 |
case *dns.NSID: |
| 103 |
// do stuff with x.Nsid |
| 104 |
case *dns.SUBNET: |
| 105 |
// access x.Family, x.Address, etc. |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
Unknown options are dealt with and added as [ERFC3597] RRs, simular to actual unknown RRs are handled with |
| 110 |
[RFC3597]. |
| 111 |
|
| 112 |
# Private Resource Records |
| 113 |
|
| 114 |
Any struct can be used as a private resource record. To make it work you need to implement the following |
| 115 |
interfaces. |
| 116 |
|
| 117 |
- [Typer], to give your RR a code point, and see documentation of that interface. |
| 118 |
- [RR], all RRs implement this, if you want to have a private EDNS0 option, implement [EDNS0] interface, this |
| 119 |
adds a Pseudo() bool method. |
| 120 |
- [Parser], so it can be parsed to and from strings. |
| 121 |
- [Packer], if you need to use your new RR on the wire. |
| 122 |
- [Comparer], if your RR will be signed with DNSSEC. |
| 123 |
|
| 124 |
See rr_test.go for a complete example for both an external [RR] and [EDNS0]. |
| 125 |
*/ |
| 126 |
package dns |