| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"math" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
"sync/atomic" |
| 9 |
|
| 10 |
"codeberg.org/miekg/dns/pkg/pool" |
| 11 |
) |
| 12 |
|
| 13 |
//go:generate go run rr_generate.go |
| 14 |
//go:generate go run rdata_generate.go |
| 15 |
//go:generate go run ednsrr_generate.go |
| 16 |
//go:generate go run dsorr_generate.go |
| 17 |
//go:generate go run msg_generate.go |
| 18 |
//go:generate go run pack_generate.go |
| 19 |
//go:generate go run parse_generate.go |
| 20 |
//go:generate go run len_generate.go |
| 21 |
//go:generate go run dsolen_generate.go |
| 22 |
//go:generate go run dnsutil_generate.go |
| 23 |
//go:generate go run dnstest_generate.go |
| 24 |
//go:generate go run compare_generate.go |
| 25 |
//go:generate go run clone_generate.go |
| 26 |
//go:generate go run rdata_generate.go |
| 27 |
//go:generate go run scan_generate.go |
| 28 |
|
| 29 |
const ( |
| 30 |
// DefaultMsgSize is the standard default for messages larger than 512 bytes. This limit is the |
| 31 |
// recommendation from RFC 9715. |
| 32 |
DefaultMsgSize = 1400 |
| 33 |
// MinMsgSize is the minimal size of a DNS message. |
| 34 |
MinMsgSize = 512 |
| 35 |
// MaxMsgSize is the largest possible DNS message. |
| 36 |
MaxMsgSize = math.MaxUint16 |
| 37 |
// MsgHeaderSize is the length of the header in the DNS message. |
| 38 |
MsgHeaderSize = 12 |
| 39 |
// MaxSerialIncrement is the maximum difference between two serial numbers. See RFC 1982. |
| 40 |
MaxSerialIncrement = math.MaxUint32 / 2 // 2147483647 |
| 41 |
) |
| 42 |
|
| 43 |
// An RR represents a DNS resource record. |
| 44 |
type RR interface { |
| 45 |
// Header returns the header of a resource record. The header contains everything up to the rdata. |
| 46 |
Header() *Header |
| 47 |
// Data return the rdata of a resource record. The data contains everything after the header. |
| 48 |
Data() RDATA |
| 49 |
// String returns the text representation of the resource record. |
| 50 |
String() string |
| 51 |
// Len is the length of the RR when encoded in wire format, this is not a perfect metric and returning |
| 52 |
// a slightly too large value is OK. |
| 53 |
Len() int |
| 54 |
|
| 55 |
Cloner |
| 56 |
} |
| 57 |
|
| 58 |
// An RDATA represents a DNS rdata element, this is the part of the RR minus the [Header]. |
| 59 |
type RDATA interface { |
| 60 |
// Len is the length of the resource data when encoded in wire format. |
| 61 |
Len() int |
| 62 |
// String returns the text representation of the rdata only. |
| 63 |
String() string |
| 64 |
} |
| 65 |
|
| 66 |
// The Typer interface is used to return the type of RR in the RRToType function or the EDNS0 option |
| 67 |
// code when the "RR" is an EDNS0 option. This is only needed for RRs that are defined outside of this package. |
| 68 |
// Once this method is defined the following extra registration needs to happen: |
| 69 |
// |
| 70 |
// dns.TypeToRR[codepoint] = func() dns.RR { return new(T) } |
| 71 |
// dns.TypeToString[codepoint] = "TYPE" |
| 72 |
// dns.StringToType["TYPE"] = codepoint |
| 73 |
// |
| 74 |
// For EDNS0 registration use, [CodeToRR], [CodeToString] and [StringToType]. See the file rr_test.go for an |
| 75 |
// example on how to register a private RR and an EDNS0 type. |
| 76 |
type Typer interface { |
| 77 |
Type() uint16 |
| 78 |
} |
| 79 |
|
| 80 |
// Comparer interface defines a compare function that returns -1, 0, or +1. Only externally defined RRs must |
| 81 |
// implement this interface. |
| 82 |
type Comparer interface { |
| 83 |
Compare(b RR) int |
| 84 |
} |
| 85 |
|
| 86 |
// The Packer interface defines the Pack and Unpack methods that are used to convert RRs to and from wire format. |
| 87 |
type Packer interface { |
| 88 |
// Pack packs the RR into msg at offset off. This method only needs to deals with the RR's rdata, as the |
| 89 |
// header is taken care off. For examples of such code look in zmsg.go. The returned int is the new offset in |
| 90 |
// msg after this RR is packed. For EDNS0 types this only need to pack the data, not the type-length-value |
| 91 |
// (TLV) header. |
| 92 |
Pack(msg []byte, off int) (int, error) |
| 93 |
// Unpack unpacks the RR. Data is the byte slice that should contain the all the data for the RR. |
| 94 |
Unpack(data []byte) error |
| 95 |
} |
| 96 |
|
| 97 |
// Parser is used for custom RR types that are parsed from their text presentation. |
| 98 |
type Parser interface { |
| 99 |
// Parse gets the current origin and a slice of tokens left on the current line. The tokens contains all |
| 100 |
// non-blank works and quotes left on the current line, i.e. if your have "this" and "that" tokens will |
| 101 |
// contain: " this " " that " |
| 102 |
Parse(tokens []string, origin string) error |
| 103 |
} |
| 104 |
|
| 105 |
// The Cloner interface defines a clone function that returns a deep copy of the RR. |
| 106 |
type Cloner interface { |
| 107 |
Clone() RR |
| 108 |
} |
| 109 |
|
| 110 |
// RRset is a just list of RRs. There is no guarantee that this is an official RRset as defined in |
| 111 |
// RFC 7719, Section 4 "RRset", use [dnsutil.IsRRset] to make that determination. |
| 112 |
// The type is defined here to implement the [sort.Interface]. |
| 113 |
// |
| 114 |
// Typical use for sorting a slice of RRs: sort.Sort(dns.RRset(....)). |
| 115 |
type RRset []RR |
| 116 |
|
| 117 |
// Header is the header in a DNS resource record. It implements the RR interface, as a header is the RR |
| 118 |
// without any data. |
| 119 |
type Header struct { |
| 120 |
Name string `dns:"cname"` // Name is the owner name of the RR. |
| 121 |
TTL uint32 // TTL is the time-to-live of the RR. |
| 122 |
Class uint16 // Class is the class of the RR, this is almost always [ClassINET]. |
| 123 |
} |
| 124 |
|
| 125 |
func (h *Header) Len() int { return len(h.Name) + 1 + 10 } // +1 because miek.nl. is actually .miek.nl. |
| 126 |
func (h *Header) Header() *Header { return h } |
| 127 |
func (h *Header) Data() RDATA { return nil } |
| 128 |
func (h *Header) Clone() RR { return &Header{h.Name, h.TTL, h.Class} } |
| 129 |
|
| 130 |
// String returns the string representation of h. |
| 131 |
// Note that as the RR type is derived from the [RR] containing this header, getting the text |
| 132 |
// representation of just the header will show TYPE0 instead of the actual type. As this not that useful |
| 133 |
// the TYPE0 is not even added, leaving name, ttl and class. |
| 134 |
// |
| 135 |
// For correctly printing the header you need the RR type to correctly print it. See [codeberg.org/miekg/dns/dnsutil.TypeToString] among others. |
| 136 |
// For a RR to be completely printed use: |
| 137 |
// |
| 138 |
// s := rr.Header().String() + " " + dnsutil.TypeToString(dns.RRToType(rr)) + "\t" + rr.Data().String) |
| 139 |
func (h *Header) String() string { |
| 140 |
sb := builderPool.Get() |
| 141 |
defer builderPool.Put(sb) |
| 142 |
sb.WriteString(h.Name) |
| 143 |
sb.WriteByte('\t') |
| 144 |
|
| 145 |
sb.WriteString(strconv.FormatInt(int64(h.TTL), 10)) |
| 146 |
sb.WriteByte('\t') |
| 147 |
|
| 148 |
sb.WriteString(classToString(h.Class)) |
| 149 |
return sb.String() |
| 150 |
} |
| 151 |
|
| 152 |
// EDNS0 determines if the "RR" is posing as an EDNS0 option. EDNS0 options are considered just RRs and must |
| 153 |
// be added to the [Pseudo] section of a DNS message. The Len method must return the length of the octets in |
| 154 |
// the [OPT] [RR], which is four (2 octets for the type, and 2 octets for the length) plus the encoded lengh of the option itself. |
| 155 |
// |
| 156 |
// Note that these types has (in this package) a presentation format and can also be parsed from a string via |
| 157 |
// [New]. That means you can create EDNS0 options directly from a string. |
| 158 |
type EDNS0 interface { |
| 159 |
RR |
| 160 |
// Pseudo signal that the type implementing this interface is an EDNS0 sub-type. |
| 161 |
Pseudo() bool |
| 162 |
} |
| 163 |
|
| 164 |
// DSO determines if the "RR" is posing as an DSO option. DSO options are considered just RRs and must |
| 165 |
// be added to the [Stateful] section of a DNS message. The Len method must return the the length of the |
| 166 |
// octets for the entire option, which is four (2 octets for the type, and 2 octets for the length) plus the |
| 167 |
// encoded length of the option itself. |
| 168 |
type DSO interface { |
| 169 |
RR |
| 170 |
Stateful() bool |
| 171 |
} |
| 172 |
|
| 173 |
// MsgHeader is the header of a DNS message. This contains most header bits, except Rcode as that needs to be |
| 174 |
// set via a function because of the extended Rcode that lives in the pseudo section. |
| 175 |
type MsgHeader struct { |
| 176 |
offset uint16 |
| 177 |
|
| 178 |
// Both qtype and Options are moved there to aid in struct alignment. |
| 179 |
// aligo -s Msg view . shows 4 bytes padding for the hijacked field |
| 180 |
|
| 181 |
// optimization to put the qtype directly in the message, shortcuts needing to actually have a question |
| 182 |
// section (this will then be zero) and avoid RRToType which is slightly slower in the hot path. |
| 183 |
qtype uint16 |
| 184 |
qclass uint16 |
| 185 |
// Option is a bit mask of options that control the unpacking. When zero the entire message is unpacked. |
| 186 |
Options MsgOption |
| 187 |
|
| 188 |
Opcode uint8 |
| 189 |
|
| 190 |
ID uint16 |
| 191 |
|
| 192 |
Rcode uint16 // Rcode is the message response code, extended rcodes can be set here as well. |
| 193 |
|
| 194 |
// Extended DNS (version 0) option that can be set directly on the message. The package takes care of |
| 195 |
// putting the bits in the right places and creating an OPT RR if needed. |
| 196 |
UDPSize uint16 // UDPSize is the OPT's RR advertised UDP size. |
| 197 |
Version uint8 // Version is the EDNS version, always zero. |
| 198 |
|
| 199 |
Response bool |
| 200 |
Authoritative bool |
| 201 |
Truncated bool |
| 202 |
RecursionDesired bool |
| 203 |
RecursionAvailable bool |
| 204 |
Zero bool |
| 205 |
AuthenticatedData bool |
| 206 |
CheckingDisabled bool |
| 207 |
|
| 208 |
// Extended DNS. |
| 209 |
Security bool // Security is the DNSSEC OK bit, see RFC 403{3,4,5}, see [OPT.security]. |
| 210 |
CompactAnswers bool // Compact Answers OK, see RFC 9824, see [OPT.compactAnswers]. |
| 211 |
Delegation bool // Delegation is the DELEG OK bit, see https://datatracker.ietf.org/doc/draft-ietf-deleg/ and [OPT.delegation]. |
| 212 |
Z uint16 // Z holds the OPT's unassigned Z bits, see [OPT.z]. |
| 213 |
} |
| 214 |
|
| 215 |
// Msg is a DNS message. Each message has a Data field that contains the binary data buffer. This is filled when |
| 216 |
// calling [Msg.Pack], it is read and parsed into a Msg by [Msg.Unpack]. When the server allocated Data when reading |
| 217 |
// from the wire, the server owns the allocation. Whenever the message is written to the default |
| 218 |
// [ResponseWriter] it is returned to the server's pool. If you need to make the Msg the sole owner of the |
| 219 |
// allocation call [Msg.Hijack], the allocation will then not be returned. When you create a new Msg, you are |
| 220 |
// in full control over the buffer as there is no relation to whatever server. |
| 221 |
// |
| 222 |
// Msg implements [iter.Seq], so you can range over it, when doing so the RRs of each section are returned, |
| 223 |
// this includes the pseudo section. |
| 224 |
type Msg struct { |
| 225 |
MsgHeader |
| 226 |
|
| 227 |
// Question holds a single "RR", in quotes because it is only the domain name, type and class that is |
| 228 |
// actually encoded here. This package takes care of taking and returning the right bit of an RR. |
| 229 |
// Setting the question is done like so: msg.Question = []RR{&MX{Hdr: Header{Name: "miek.nl.", Class: ClassINET}}} |
| 230 |
// This sets it to "miek.nl.", TypeMX, ClassINET. Just like all the other sections. |
| 231 |
Question []RR |
| 232 |
|
| 233 |
Answer []RR // Holds the RR(s) of the answer section. |
| 234 |
Ns []RR // Holds the RR(s) of the authority section. |
| 235 |
Extra []RR // Holds the RR(s) of the additional section, except records that go into the pseudo section. |
| 236 |
|
| 237 |
// The Pseudo section is a virtual section that holds the OPT EDNS0 options, that are interpreted (and shown) as RRs. |
| 238 |
// The OPT RR itself will never be visible in Extra, nor in the Pseudo section, this is all handled transparently. |
| 239 |
Pseudo []RR // Holds the RR(s) of the (virtual) pseudo section. |
| 240 |
|
| 241 |
// The Stateful section is a virtual section that holds the DSO option, that are interpreted (and shown) |
| 242 |
// as RRs. There is no OPT like record that holds these, the whole message format is slightly different. |
| 243 |
// Stateful []RR // Holds the DSO RR(s) for Stateful operations, see RFC 8490. |
| 244 |
|
| 245 |
// msgPool is the [Pooler] from the server, *iff* the message was created by reading data from the wire. |
| 246 |
msgPool pool.Pooler |
| 247 |
|
| 248 |
// Data is the data of the message that was either received from the wire or is about to be send |
| 249 |
// over the wire. Note that this data is a snapshot of the Msg when it was packed or unpacked. |
| 250 |
Data []byte |
| 251 |
hijacked atomic.Bool // pool's allocation has been hijacked by caller |
| 252 |
} |
| 253 |
|
| 254 |
// Option is an option on how to handle a message. The options are ordered, MsgOptionUnpackQuestion will also |
| 255 |
// unpack the header of the message. If MsgOptionUnpackQuestion is used, Unpack will track where it left off |
| 256 |
// and then skip unpacking the question section in a subsequent Unpack that is done to get the entire message |
| 257 |
// of which the header and question section where previously deemed valid. |
| 258 |
type MsgOption uint8 |
| 259 |
|
| 260 |
const ( |
| 261 |
MsgOptionUnpack MsgOption = 0 // Unpack the entire message, mostly defined to serve as documentation. |
| 262 |
MsgOptionUnpackHeader MsgOption = 1 << iota // Unpack only the header of the message. |
| 263 |
MsgOptionUnpackQuestion // Unpack up the question section of the message. |
| 264 |
MsgOptionUnpackAnswer // Unpack up to the answer section of the message. |
| 265 |
|
| 266 |
) |
| 267 |
|
| 268 |
// Convert a MsgHeader to a string, with dig-like headers: |
| 269 |
// |
| 270 |
// ;; QUERY, rcode: NOERROR, id: 51664, flags: qr rd ra do co |
| 271 |
// ;; EDNS, version: 0, udp: 512 |
| 272 |
// ;; QUESTION: 1, PSEUDO: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0 |
| 273 |
func (h *MsgHeader) String() string { |
| 274 |
sb := strings.Builder{} |
| 275 |
sb.WriteString(";; ") |
| 276 |
sb.WriteString(opcodeToString(h.Opcode)) |
| 277 |
sb.WriteString(", rcode: ") |
| 278 |
sb.WriteString(rcodeToString(h.Rcode)) |
| 279 |
sb.WriteString(", id: ") |
| 280 |
sb.WriteString(strconv.Itoa(int(h.ID))) |
| 281 |
sb.WriteByte(',') |
| 282 |
|
| 283 |
sb.WriteString(" flags:") |
| 284 |
if h.Response { |
| 285 |
sb.WriteString(" qr") |
| 286 |
} |
| 287 |
if h.Authoritative { |
| 288 |
sb.WriteString(" aa") |
| 289 |
} |
| 290 |
if h.Truncated { |
| 291 |
sb.WriteString(" tc") |
| 292 |
} |
| 293 |
if h.RecursionDesired { |
| 294 |
sb.WriteString(" rd") |
| 295 |
} |
| 296 |
if h.RecursionAvailable { |
| 297 |
sb.WriteString(" ra") |
| 298 |
} |
| 299 |
if h.Zero { |
| 300 |
sb.WriteString(" z") |
| 301 |
} |
| 302 |
if h.AuthenticatedData { |
| 303 |
sb.WriteString(" ad") |
| 304 |
} |
| 305 |
if h.CheckingDisabled { |
| 306 |
sb.WriteString(" cd") |
| 307 |
} |
| 308 |
if h.Security { |
| 309 |
sb.WriteString(" do") |
| 310 |
} |
| 311 |
if h.CompactAnswers { |
| 312 |
sb.WriteString(" co") |
| 313 |
} |
| 314 |
if h.Delegation { |
| 315 |
sb.WriteString(" de") |
| 316 |
} |
| 317 |
sb.WriteByte('\n') |
| 318 |
return sb.String() |
| 319 |
} |
| 320 |
|
| 321 |
// ToRFC3597 converts a known RR to the unknown RR representation from RFC 3597. |
| 322 |
func (rr *RFC3597) ToRFC3597(r RR) error { |
| 323 |
buf := make([]byte, r.Len()) |
| 324 |
headerEnd, off, err := packRR(r, buf, 0, map[string]uint16{}) |
| 325 |
if err != nil { |
| 326 |
return err |
| 327 |
} |
| 328 |
buf = buf[:off] |
| 329 |
*rr = RFC3597{Hdr: *r.Header()} |
| 330 |
rr.RRType = RRToType(r) |
| 331 |
|
| 332 |
if off-headerEnd == 0 { // rdlength |
| 333 |
return nil |
| 334 |
} |
| 335 |
|
| 336 |
return rr.unpack(buf[headerEnd:], buf) |
| 337 |
} |
| 338 |
|
| 339 |
// fromRFC3597 converts an unknown RR representation from RFC 3597 to the known RR type. |
| 340 |
func (rr *RFC3597) fromRFC3597(r RR) error { |
| 341 |
hdr := r.Header() |
| 342 |
*hdr = rr.Hdr |
| 343 |
|
| 344 |
// Can't overflow uint16 as the length of Rdata is validated in (*RFC3597).parse. |
| 345 |
// We can only get here when rr was constructed with that method. |
| 346 |
|
| 347 |
// rr.pack requires an extra allocation and a copy so we just decode Rdata manually, it's simpler anyway. |
| 348 |
msg, err := hex.DecodeString(rr.RFC3597.Data) |
| 349 |
if err != nil { |
| 350 |
return err |
| 351 |
} |
| 352 |
if len(msg) == 0 { // no rdata |
| 353 |
return nil |
| 354 |
} |
| 355 |
return zunpack(r, msg, msg) |
| 356 |
} |
| 357 |
|
| 358 |
const msgArcount = 10 // offset in the message where the Arcount is, 2 octets long. |