12 // EDNS0 Option codes.
14 EDNS0LLQ = 0x1 // long lived queries: http://tools.ietf.org/html/draft-sekar-dns-llq-01
15 EDNS0UL = 0x2 // update lease draft: http://files.dns-sd.org/draft-sekar-dns-ul.txt
16 EDNS0NSID = 0x3 // nsid (See RFC 5001)
17 EDNS0DAU = 0x5 // DNSSEC Algorithm Understood
18 EDNS0DHU = 0x6 // DS Hash Understood
19 EDNS0N3U = 0x7 // NSEC3 Hash Understood
20 EDNS0SUBNET = 0x8 // client-subnet (See RFC 7871)
21 EDNS0EXPIRE = 0x9 // EDNS0 expire
22 EDNS0COOKIE = 0xa // EDNS0 Cookie
23 EDNS0TCPKEEPALIVE = 0xb // EDNS0 tcp keep alive (See RFC 7828)
24 EDNS0PADDING = 0xc // EDNS0 padding (See RFC 7830)
25 EDNS0LOCALSTART = 0xFDE9 // Beginning of range reserved for local/experimental use (See RFC 6891)
26 EDNS0LOCALEND = 0xFFFE // End of range reserved for local/experimental use (See RFC 6891)
27 _DO = 1 << 15 // DNSSEC OK
30 // OPT is the EDNS0 RR appended to messages to convey extra (meta) information.
34 Option []EDNS0 `dns:"opt"`
37 func (rr *OPT) String() string {
38 s := "\n;; OPT PSEUDOSECTION:\n; EDNS: version " + strconv.Itoa(int(rr.Version())) + "; "
44 s += "udp: " + strconv.Itoa(int(rr.UDPSize()))
46 for _, o := range rr.Option {
49 s += "\n; NSID: " + o.String()
54 r += "(" + string(c) + ")"
59 s += "\n; SUBNET: " + o.String()
61 s += "\n; COOKIE: " + o.String()
63 s += "\n; UPDATE LEASE: " + o.String()
65 s += "\n; LONG LIVED QUERIES: " + o.String()
67 s += "\n; DNSSEC ALGORITHM UNDERSTOOD: " + o.String()
69 s += "\n; DS HASH UNDERSTOOD: " + o.String()
71 s += "\n; NSEC3 HASH UNDERSTOOD: " + o.String()
73 s += "\n; LOCAL OPT: " + o.String()
75 s += "\n; PADDING: " + o.String()
81 func (rr *OPT) len(off int, compression map[string]struct{}) int {
82 l := rr.Hdr.len(off, compression)
83 for i := 0; i < len(rr.Option); i++ {
84 l += 4 // Account for 2-byte option code and 2-byte option length.
85 lo, _ := rr.Option[i].pack()
91 func (rr *OPT) parse(c *zlexer, origin, file string) *ParseError {
92 panic("dns: internal error: parse should never be called on OPT")
95 func (r1 *OPT) isDuplicate(r2 RR) bool { return false }
97 // return the old value -> delete SetVersion?
99 // Version returns the EDNS version used. Only zero is defined.
100 func (rr *OPT) Version() uint8 {
101 return uint8(rr.Hdr.Ttl & 0x00FF0000 >> 16)
104 // SetVersion sets the version of EDNS. This is usually zero.
105 func (rr *OPT) SetVersion(v uint8) {
106 rr.Hdr.Ttl = rr.Hdr.Ttl&0xFF00FFFF | uint32(v)<<16
109 // ExtendedRcode returns the EDNS extended RCODE field (the upper 8 bits of the TTL).
110 func (rr *OPT) ExtendedRcode() int {
111 return int(rr.Hdr.Ttl&0xFF000000>>24) << 4
114 // SetExtendedRcode sets the EDNS extended RCODE field.
116 // If the RCODE is not an extended RCODE, will reset the extended RCODE field to 0.
117 func (rr *OPT) SetExtendedRcode(v uint16) {
118 rr.Hdr.Ttl = rr.Hdr.Ttl&0x00FFFFFF | uint32(v>>4)<<24
121 // UDPSize returns the UDP buffer size.
122 func (rr *OPT) UDPSize() uint16 {
126 // SetUDPSize sets the UDP buffer size.
127 func (rr *OPT) SetUDPSize(size uint16) {
131 // Do returns the value of the DO (DNSSEC OK) bit.
132 func (rr *OPT) Do() bool {
133 return rr.Hdr.Ttl&_DO == _DO
136 // SetDo sets the DO (DNSSEC OK) bit.
137 // If we pass an argument, set the DO bit to that value.
138 // It is possible to pass 2 or more arguments. Any arguments after the 1st is silently ignored.
139 func (rr *OPT) SetDo(do ...bool) {
151 // EDNS0 defines an EDNS0 Option. An OPT RR can have multiple options appended to it.
152 type EDNS0 interface {
153 // Option returns the option code for the option.
155 // pack returns the bytes of the option data.
156 pack() ([]byte, error)
157 // unpack sets the data as found in the buffer. Is also sets
158 // the length of the slice as the length of the option data.
160 // String returns the string representation of the option.
162 // copy returns a deep-copy of the option.
166 // EDNS0_NSID option is used to retrieve a nameserver
167 // identifier. When sending a request Nsid must be set to the empty string
168 // The identifier is an opaque string encoded as hex.
169 // Basic use pattern for creating an nsid option:
173 // o.Hdr.Rrtype = dns.TypeOPT
174 // e := new(dns.EDNS0_NSID)
175 // e.Code = dns.EDNS0NSID
177 // o.Option = append(o.Option, e)
178 type EDNS0_NSID struct {
179 Code uint16 // Always EDNS0NSID
180 Nsid string // This string needs to be hex encoded
183 func (e *EDNS0_NSID) pack() ([]byte, error) {
184 h, err := hex.DecodeString(e.Nsid)
191 // Option implements the EDNS0 interface.
192 func (e *EDNS0_NSID) Option() uint16 { return EDNS0NSID } // Option returns the option code.
193 func (e *EDNS0_NSID) unpack(b []byte) error { e.Nsid = hex.EncodeToString(b); return nil }
194 func (e *EDNS0_NSID) String() string { return e.Nsid }
195 func (e *EDNS0_NSID) copy() EDNS0 { return &EDNS0_NSID{e.Code, e.Nsid} }
197 // EDNS0_SUBNET is the subnet option that is used to give the remote nameserver
198 // an idea of where the client lives. See RFC 7871. It can then give back a different
199 // answer depending on the location or network topology.
200 // Basic use pattern for creating an subnet option:
204 // o.Hdr.Rrtype = dns.TypeOPT
205 // e := new(dns.EDNS0_SUBNET)
206 // e.Code = dns.EDNS0SUBNET
207 // e.Family = 1 // 1 for IPv4 source address, 2 for IPv6
208 // e.SourceNetmask = 32 // 32 for IPV4, 128 for IPv6
210 // e.Address = net.ParseIP("127.0.0.1").To4() // for IPv4
211 // // e.Address = net.ParseIP("2001:7b8:32a::2") // for IPV6
212 // o.Option = append(o.Option, e)
214 // This code will parse all the available bits when unpacking (up to optlen).
215 // When packing it will apply SourceNetmask. If you need more advanced logic,
216 // patches welcome and good luck.
217 type EDNS0_SUBNET struct {
218 Code uint16 // Always EDNS0SUBNET
219 Family uint16 // 1 for IP, 2 for IP6
225 // Option implements the EDNS0 interface.
226 func (e *EDNS0_SUBNET) Option() uint16 { return EDNS0SUBNET }
228 func (e *EDNS0_SUBNET) pack() ([]byte, error) {
230 binary.BigEndian.PutUint16(b[0:], e.Family)
231 b[2] = e.SourceNetmask
235 // "dig" sets AddressFamily to 0 if SourceNetmask is also 0
236 // We might don't need to complain either
237 if e.SourceNetmask != 0 {
238 return nil, errors.New("dns: bad address family")
241 if e.SourceNetmask > net.IPv4len*8 {
242 return nil, errors.New("dns: bad netmask")
244 if len(e.Address.To4()) != net.IPv4len {
245 return nil, errors.New("dns: bad address")
247 ip := e.Address.To4().Mask(net.CIDRMask(int(e.SourceNetmask), net.IPv4len*8))
248 needLength := (e.SourceNetmask + 8 - 1) / 8 // division rounding up
249 b = append(b, ip[:needLength]...)
251 if e.SourceNetmask > net.IPv6len*8 {
252 return nil, errors.New("dns: bad netmask")
254 if len(e.Address) != net.IPv6len {
255 return nil, errors.New("dns: bad address")
257 ip := e.Address.Mask(net.CIDRMask(int(e.SourceNetmask), net.IPv6len*8))
258 needLength := (e.SourceNetmask + 8 - 1) / 8 // division rounding up
259 b = append(b, ip[:needLength]...)
261 return nil, errors.New("dns: bad address family")
266 func (e *EDNS0_SUBNET) unpack(b []byte) error {
270 e.Family = binary.BigEndian.Uint16(b)
271 e.SourceNetmask = b[2]
275 // "dig" sets AddressFamily to 0 if SourceNetmask is also 0
276 // It's okay to accept such a packet
277 if e.SourceNetmask != 0 {
278 return errors.New("dns: bad address family")
280 e.Address = net.IPv4(0, 0, 0, 0)
282 if e.SourceNetmask > net.IPv4len*8 || e.SourceScope > net.IPv4len*8 {
283 return errors.New("dns: bad netmask")
285 addr := make(net.IP, net.IPv4len)
287 e.Address = addr.To16()
289 if e.SourceNetmask > net.IPv6len*8 || e.SourceScope > net.IPv6len*8 {
290 return errors.New("dns: bad netmask")
292 addr := make(net.IP, net.IPv6len)
296 return errors.New("dns: bad address family")
301 func (e *EDNS0_SUBNET) String() (s string) {
302 if e.Address == nil {
304 } else if e.Address.To4() != nil {
305 s = e.Address.String()
307 s = "[" + e.Address.String() + "]"
309 s += "/" + strconv.Itoa(int(e.SourceNetmask)) + "/" + strconv.Itoa(int(e.SourceScope))
313 func (e *EDNS0_SUBNET) copy() EDNS0 {
314 return &EDNS0_SUBNET{
323 // The EDNS0_COOKIE option is used to add a DNS Cookie to a message.
327 // o.Hdr.Rrtype = dns.TypeOPT
328 // e := new(dns.EDNS0_COOKIE)
329 // e.Code = dns.EDNS0COOKIE
330 // e.Cookie = "24a5ac.."
331 // o.Option = append(o.Option, e)
333 // The Cookie field consists out of a client cookie (RFC 7873 Section 4), that is
334 // always 8 bytes. It may then optionally be followed by the server cookie. The server
335 // cookie is of variable length, 8 to a maximum of 32 bytes. In other words:
337 // cCookie := o.Cookie[:16]
338 // sCookie := o.Cookie[16:]
340 // There is no guarantee that the Cookie string has a specific length.
341 type EDNS0_COOKIE struct {
342 Code uint16 // Always EDNS0COOKIE
343 Cookie string // Hex-encoded cookie data
346 func (e *EDNS0_COOKIE) pack() ([]byte, error) {
347 h, err := hex.DecodeString(e.Cookie)
354 // Option implements the EDNS0 interface.
355 func (e *EDNS0_COOKIE) Option() uint16 { return EDNS0COOKIE }
356 func (e *EDNS0_COOKIE) unpack(b []byte) error { e.Cookie = hex.EncodeToString(b); return nil }
357 func (e *EDNS0_COOKIE) String() string { return e.Cookie }
358 func (e *EDNS0_COOKIE) copy() EDNS0 { return &EDNS0_COOKIE{e.Code, e.Cookie} }
360 // The EDNS0_UL (Update Lease) (draft RFC) option is used to tell the server to set
361 // an expiration on an update RR. This is helpful for clients that cannot clean
362 // up after themselves. This is a draft RFC and more information can be found at
363 // http://files.dns-sd.org/draft-sekar-dns-ul.txt
367 // o.Hdr.Rrtype = dns.TypeOPT
368 // e := new(dns.EDNS0_UL)
369 // e.Code = dns.EDNS0UL
370 // e.Lease = 120 // in seconds
371 // o.Option = append(o.Option, e)
372 type EDNS0_UL struct {
373 Code uint16 // Always EDNS0UL
377 // Option implements the EDNS0 interface.
378 func (e *EDNS0_UL) Option() uint16 { return EDNS0UL }
379 func (e *EDNS0_UL) String() string { return strconv.FormatUint(uint64(e.Lease), 10) }
380 func (e *EDNS0_UL) copy() EDNS0 { return &EDNS0_UL{e.Code, e.Lease} }
382 // Copied: http://golang.org/src/pkg/net/dnsmsg.go
383 func (e *EDNS0_UL) pack() ([]byte, error) {
385 binary.BigEndian.PutUint32(b, e.Lease)
389 func (e *EDNS0_UL) unpack(b []byte) error {
393 e.Lease = binary.BigEndian.Uint32(b)
397 // EDNS0_LLQ stands for Long Lived Queries: http://tools.ietf.org/html/draft-sekar-dns-llq-01
398 // Implemented for completeness, as the EDNS0 type code is assigned.
399 type EDNS0_LLQ struct {
400 Code uint16 // Always EDNS0LLQ
408 // Option implements the EDNS0 interface.
409 func (e *EDNS0_LLQ) Option() uint16 { return EDNS0LLQ }
411 func (e *EDNS0_LLQ) pack() ([]byte, error) {
412 b := make([]byte, 18)
413 binary.BigEndian.PutUint16(b[0:], e.Version)
414 binary.BigEndian.PutUint16(b[2:], e.Opcode)
415 binary.BigEndian.PutUint16(b[4:], e.Error)
416 binary.BigEndian.PutUint64(b[6:], e.Id)
417 binary.BigEndian.PutUint32(b[14:], e.LeaseLife)
421 func (e *EDNS0_LLQ) unpack(b []byte) error {
425 e.Version = binary.BigEndian.Uint16(b[0:])
426 e.Opcode = binary.BigEndian.Uint16(b[2:])
427 e.Error = binary.BigEndian.Uint16(b[4:])
428 e.Id = binary.BigEndian.Uint64(b[6:])
429 e.LeaseLife = binary.BigEndian.Uint32(b[14:])
433 func (e *EDNS0_LLQ) String() string {
434 s := strconv.FormatUint(uint64(e.Version), 10) + " " + strconv.FormatUint(uint64(e.Opcode), 10) +
435 " " + strconv.FormatUint(uint64(e.Error), 10) + " " + strconv.FormatUint(e.Id, 10) +
436 " " + strconv.FormatUint(uint64(e.LeaseLife), 10)
439 func (e *EDNS0_LLQ) copy() EDNS0 {
440 return &EDNS0_LLQ{e.Code, e.Version, e.Opcode, e.Error, e.Id, e.LeaseLife}
443 // EDNS0_DUA implements the EDNS0 "DNSSEC Algorithm Understood" option. See RFC 6975.
444 type EDNS0_DAU struct {
445 Code uint16 // Always EDNS0DAU
449 // Option implements the EDNS0 interface.
450 func (e *EDNS0_DAU) Option() uint16 { return EDNS0DAU }
451 func (e *EDNS0_DAU) pack() ([]byte, error) { return e.AlgCode, nil }
452 func (e *EDNS0_DAU) unpack(b []byte) error { e.AlgCode = b; return nil }
454 func (e *EDNS0_DAU) String() string {
456 for i := 0; i < len(e.AlgCode); i++ {
457 if a, ok := AlgorithmToString[e.AlgCode[i]]; ok {
460 s += " " + strconv.Itoa(int(e.AlgCode[i]))
465 func (e *EDNS0_DAU) copy() EDNS0 { return &EDNS0_DAU{e.Code, e.AlgCode} }
467 // EDNS0_DHU implements the EDNS0 "DS Hash Understood" option. See RFC 6975.
468 type EDNS0_DHU struct {
469 Code uint16 // Always EDNS0DHU
473 // Option implements the EDNS0 interface.
474 func (e *EDNS0_DHU) Option() uint16 { return EDNS0DHU }
475 func (e *EDNS0_DHU) pack() ([]byte, error) { return e.AlgCode, nil }
476 func (e *EDNS0_DHU) unpack(b []byte) error { e.AlgCode = b; return nil }
478 func (e *EDNS0_DHU) String() string {
480 for i := 0; i < len(e.AlgCode); i++ {
481 if a, ok := HashToString[e.AlgCode[i]]; ok {
484 s += " " + strconv.Itoa(int(e.AlgCode[i]))
489 func (e *EDNS0_DHU) copy() EDNS0 { return &EDNS0_DHU{e.Code, e.AlgCode} }
491 // EDNS0_N3U implements the EDNS0 "NSEC3 Hash Understood" option. See RFC 6975.
492 type EDNS0_N3U struct {
493 Code uint16 // Always EDNS0N3U
497 // Option implements the EDNS0 interface.
498 func (e *EDNS0_N3U) Option() uint16 { return EDNS0N3U }
499 func (e *EDNS0_N3U) pack() ([]byte, error) { return e.AlgCode, nil }
500 func (e *EDNS0_N3U) unpack(b []byte) error { e.AlgCode = b; return nil }
502 func (e *EDNS0_N3U) String() string {
503 // Re-use the hash map
505 for i := 0; i < len(e.AlgCode); i++ {
506 if a, ok := HashToString[e.AlgCode[i]]; ok {
509 s += " " + strconv.Itoa(int(e.AlgCode[i]))
514 func (e *EDNS0_N3U) copy() EDNS0 { return &EDNS0_N3U{e.Code, e.AlgCode} }
516 // EDNS0_EXPIRE implementes the EDNS0 option as described in RFC 7314.
517 type EDNS0_EXPIRE struct {
518 Code uint16 // Always EDNS0EXPIRE
522 // Option implements the EDNS0 interface.
523 func (e *EDNS0_EXPIRE) Option() uint16 { return EDNS0EXPIRE }
524 func (e *EDNS0_EXPIRE) String() string { return strconv.FormatUint(uint64(e.Expire), 10) }
525 func (e *EDNS0_EXPIRE) copy() EDNS0 { return &EDNS0_EXPIRE{e.Code, e.Expire} }
527 func (e *EDNS0_EXPIRE) pack() ([]byte, error) {
529 binary.BigEndian.PutUint32(b, e.Expire)
533 func (e *EDNS0_EXPIRE) unpack(b []byte) error {
537 e.Expire = binary.BigEndian.Uint32(b)
541 // The EDNS0_LOCAL option is used for local/experimental purposes. The option
542 // code is recommended to be within the range [EDNS0LOCALSTART, EDNS0LOCALEND]
543 // (RFC6891), although any unassigned code can actually be used. The content of
544 // the option is made available in Data, unaltered.
545 // Basic use pattern for creating a local option:
549 // o.Hdr.Rrtype = dns.TypeOPT
550 // e := new(dns.EDNS0_LOCAL)
551 // e.Code = dns.EDNS0LOCALSTART
552 // e.Data = []byte{72, 82, 74}
553 // o.Option = append(o.Option, e)
554 type EDNS0_LOCAL struct {
559 // Option implements the EDNS0 interface.
560 func (e *EDNS0_LOCAL) Option() uint16 { return e.Code }
561 func (e *EDNS0_LOCAL) String() string {
562 return strconv.FormatInt(int64(e.Code), 10) + ":0x" + hex.EncodeToString(e.Data)
564 func (e *EDNS0_LOCAL) copy() EDNS0 {
565 b := make([]byte, len(e.Data))
567 return &EDNS0_LOCAL{e.Code, b}
570 func (e *EDNS0_LOCAL) pack() ([]byte, error) {
571 b := make([]byte, len(e.Data))
572 copied := copy(b, e.Data)
573 if copied != len(e.Data) {
579 func (e *EDNS0_LOCAL) unpack(b []byte) error {
580 e.Data = make([]byte, len(b))
581 copied := copy(e.Data, b)
582 if copied != len(b) {
588 // EDNS0_TCP_KEEPALIVE is an EDNS0 option that instructs the server to keep
589 // the TCP connection alive. See RFC 7828.
590 type EDNS0_TCP_KEEPALIVE struct {
591 Code uint16 // Always EDNSTCPKEEPALIVE
592 Length uint16 // the value 0 if the TIMEOUT is omitted, the value 2 if it is present;
593 Timeout uint16 // an idle timeout value for the TCP connection, specified in units of 100 milliseconds, encoded in network byte order.
596 // Option implements the EDNS0 interface.
597 func (e *EDNS0_TCP_KEEPALIVE) Option() uint16 { return EDNS0TCPKEEPALIVE }
599 func (e *EDNS0_TCP_KEEPALIVE) pack() ([]byte, error) {
600 if e.Timeout != 0 && e.Length != 2 {
601 return nil, errors.New("dns: timeout specified but length is not 2")
603 if e.Timeout == 0 && e.Length != 0 {
604 return nil, errors.New("dns: timeout not specified but length is not 0")
606 b := make([]byte, 4+e.Length)
607 binary.BigEndian.PutUint16(b[0:], e.Code)
608 binary.BigEndian.PutUint16(b[2:], e.Length)
610 binary.BigEndian.PutUint16(b[4:], e.Timeout)
615 func (e *EDNS0_TCP_KEEPALIVE) unpack(b []byte) error {
619 e.Length = binary.BigEndian.Uint16(b[2:4])
620 if e.Length != 0 && e.Length != 2 {
621 return errors.New("dns: length mismatch, want 0/2 but got " + strconv.FormatUint(uint64(e.Length), 10))
627 e.Timeout = binary.BigEndian.Uint16(b[4:6])
632 func (e *EDNS0_TCP_KEEPALIVE) String() (s string) {
633 s = "use tcp keep-alive"
635 s += ", timeout omitted"
637 s += fmt.Sprintf(", timeout %dms", e.Timeout*100)
641 func (e *EDNS0_TCP_KEEPALIVE) copy() EDNS0 { return &EDNS0_TCP_KEEPALIVE{e.Code, e.Length, e.Timeout} }
643 // EDNS0_PADDING option is used to add padding to a request/response. The default
644 // value of padding SHOULD be 0x0 but other values MAY be used, for instance if
645 // compression is applied before encryption which may break signatures.
646 type EDNS0_PADDING struct {
650 // Option implements the EDNS0 interface.
651 func (e *EDNS0_PADDING) Option() uint16 { return EDNS0PADDING }
652 func (e *EDNS0_PADDING) pack() ([]byte, error) { return e.Padding, nil }
653 func (e *EDNS0_PADDING) unpack(b []byte) error { e.Padding = b; return nil }
654 func (e *EDNS0_PADDING) String() string { return fmt.Sprintf("%0X", e.Padding) }
655 func (e *EDNS0_PADDING) copy() EDNS0 {
656 b := make([]byte, len(e.Padding))
658 return &EDNS0_PADDING{b}