1package dns
2
3import (
4	"encoding/binary"
5	"encoding/hex"
6	"errors"
7	"fmt"
8	"net"
9	"strconv"
10)
11
12// EDNS0 Option codes.
13const (
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
28)
29
30// OPT is the EDNS0 RR appended to messages to convey extra (meta) information.
31// See RFC 6891.
32type OPT struct {
33	Hdr    RR_Header
34	Option []EDNS0 `dns:"opt"`
35}
36
37func (rr *OPT) String() string {
38	s := "\n;; OPT PSEUDOSECTION:\n; EDNS: version " + strconv.Itoa(int(rr.Version())) + "; "
39	if rr.Do() {
40		s += "flags: do; "
41	} else {
42		s += "flags: ; "
43	}
44	s += "udp: " + strconv.Itoa(int(rr.UDPSize()))
45
46	for _, o := range rr.Option {
47		switch o.(type) {
48		case *EDNS0_NSID:
49			s += "\n; NSID: " + o.String()
50			h, e := o.pack()
51			var r string
52			if e == nil {
53				for _, c := range h {
54					r += "(" + string(c) + ")"
55				}
56				s += "  " + r
57			}
58		case *EDNS0_SUBNET:
59			s += "\n; SUBNET: " + o.String()
60		case *EDNS0_COOKIE:
61			s += "\n; COOKIE: " + o.String()
62		case *EDNS0_UL:
63			s += "\n; UPDATE LEASE: " + o.String()
64		case *EDNS0_LLQ:
65			s += "\n; LONG LIVED QUERIES: " + o.String()
66		case *EDNS0_DAU:
67			s += "\n; DNSSEC ALGORITHM UNDERSTOOD: " + o.String()
68		case *EDNS0_DHU:
69			s += "\n; DS HASH UNDERSTOOD: " + o.String()
70		case *EDNS0_N3U:
71			s += "\n; NSEC3 HASH UNDERSTOOD: " + o.String()
72		case *EDNS0_LOCAL:
73			s += "\n; LOCAL OPT: " + o.String()
74		case *EDNS0_PADDING:
75			s += "\n; PADDING: " + o.String()
76		}
77	}
78	return s
79}
80
81func (rr *OPT) len(off int, compression map[string]struct{}) int {
82	l := rr.Hdr.len(off, compression)
83	for _, o := range rr.Option {
84		l += 4 // Account for 2-byte option code and 2-byte option length.
85		lo, _ := o.pack()
86		l += len(lo)
87	}
88	return l
89}
90
91func (rr *OPT) parse(c *zlexer, origin string) *ParseError {
92	panic("dns: internal error: parse should never be called on OPT")
93}
94
95func (r1 *OPT) isDuplicate(r2 RR) bool { return false }
96
97// return the old value -> delete SetVersion?
98
99// Version returns the EDNS version used. Only zero is defined.
100func (rr *OPT) Version() uint8 {
101	return uint8(rr.Hdr.Ttl & 0x00FF0000 >> 16)
102}
103
104// SetVersion sets the version of EDNS. This is usually zero.
105func (rr *OPT) SetVersion(v uint8) {
106	rr.Hdr.Ttl = rr.Hdr.Ttl&0xFF00FFFF | uint32(v)<<16
107}
108
109// ExtendedRcode returns the EDNS extended RCODE field (the upper 8 bits of the TTL).
110func (rr *OPT) ExtendedRcode() int {
111	return int(rr.Hdr.Ttl&0xFF000000>>24) << 4
112}
113
114// SetExtendedRcode sets the EDNS extended RCODE field.
115//
116// If the RCODE is not an extended RCODE, will reset the extended RCODE field to 0.
117func (rr *OPT) SetExtendedRcode(v uint16) {
118	rr.Hdr.Ttl = rr.Hdr.Ttl&0x00FFFFFF | uint32(v>>4)<<24
119}
120
121// UDPSize returns the UDP buffer size.
122func (rr *OPT) UDPSize() uint16 {
123	return rr.Hdr.Class
124}
125
126// SetUDPSize sets the UDP buffer size.
127func (rr *OPT) SetUDPSize(size uint16) {
128	rr.Hdr.Class = size
129}
130
131// Do returns the value of the DO (DNSSEC OK) bit.
132func (rr *OPT) Do() bool {
133	return rr.Hdr.Ttl&_DO == _DO
134}
135
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.
139func (rr *OPT) SetDo(do ...bool) {
140	if len(do) == 1 {
141		if do[0] {
142			rr.Hdr.Ttl |= _DO
143		} else {
144			rr.Hdr.Ttl &^= _DO
145		}
146	} else {
147		rr.Hdr.Ttl |= _DO
148	}
149}
150
151// EDNS0 defines an EDNS0 Option. An OPT RR can have multiple options appended to it.
152type EDNS0 interface {
153	// Option returns the option code for the option.
154	Option() uint16
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.
159	unpack([]byte) error
160	// String returns the string representation of the option.
161	String() string
162	// copy returns a deep-copy of the option.
163	copy() EDNS0
164}
165
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:
170//
171//	o := new(dns.OPT)
172//	o.Hdr.Name = "."
173//	o.Hdr.Rrtype = dns.TypeOPT
174//	e := new(dns.EDNS0_NSID)
175//	e.Code = dns.EDNS0NSID
176//	e.Nsid = "AA"
177//	o.Option = append(o.Option, e)
178type EDNS0_NSID struct {
179	Code uint16 // Always EDNS0NSID
180	Nsid string // This string needs to be hex encoded
181}
182
183func (e *EDNS0_NSID) pack() ([]byte, error) {
184	h, err := hex.DecodeString(e.Nsid)
185	if err != nil {
186		return nil, err
187	}
188	return h, nil
189}
190
191// Option implements the EDNS0 interface.
192func (e *EDNS0_NSID) Option() uint16        { return EDNS0NSID } // Option returns the option code.
193func (e *EDNS0_NSID) unpack(b []byte) error { e.Nsid = hex.EncodeToString(b); return nil }
194func (e *EDNS0_NSID) String() string        { return e.Nsid }
195func (e *EDNS0_NSID) copy() EDNS0           { return &EDNS0_NSID{e.Code, e.Nsid} }
196
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:
201//
202//	o := new(dns.OPT)
203//	o.Hdr.Name = "."
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
209//	e.SourceScope = 0
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)
213//
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.
217type EDNS0_SUBNET struct {
218	Code          uint16 // Always EDNS0SUBNET
219	Family        uint16 // 1 for IP, 2 for IP6
220	SourceNetmask uint8
221	SourceScope   uint8
222	Address       net.IP
223}
224
225// Option implements the EDNS0 interface.
226func (e *EDNS0_SUBNET) Option() uint16 { return EDNS0SUBNET }
227
228func (e *EDNS0_SUBNET) pack() ([]byte, error) {
229	b := make([]byte, 4)
230	binary.BigEndian.PutUint16(b[0:], e.Family)
231	b[2] = e.SourceNetmask
232	b[3] = e.SourceScope
233	switch e.Family {
234	case 0:
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")
239		}
240	case 1:
241		if e.SourceNetmask > net.IPv4len*8 {
242			return nil, errors.New("dns: bad netmask")
243		}
244		if len(e.Address.To4()) != net.IPv4len {
245			return nil, errors.New("dns: bad address")
246		}
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]...)
250	case 2:
251		if e.SourceNetmask > net.IPv6len*8 {
252			return nil, errors.New("dns: bad netmask")
253		}
254		if len(e.Address) != net.IPv6len {
255			return nil, errors.New("dns: bad address")
256		}
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]...)
260	default:
261		return nil, errors.New("dns: bad address family")
262	}
263	return b, nil
264}
265
266func (e *EDNS0_SUBNET) unpack(b []byte) error {
267	if len(b) < 4 {
268		return ErrBuf
269	}
270	e.Family = binary.BigEndian.Uint16(b)
271	e.SourceNetmask = b[2]
272	e.SourceScope = b[3]
273	switch e.Family {
274	case 0:
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")
279		}
280		e.Address = net.IPv4(0, 0, 0, 0)
281	case 1:
282		if e.SourceNetmask > net.IPv4len*8 || e.SourceScope > net.IPv4len*8 {
283			return errors.New("dns: bad netmask")
284		}
285		addr := make(net.IP, net.IPv4len)
286		copy(addr, b[4:])
287		e.Address = addr.To16()
288	case 2:
289		if e.SourceNetmask > net.IPv6len*8 || e.SourceScope > net.IPv6len*8 {
290			return errors.New("dns: bad netmask")
291		}
292		addr := make(net.IP, net.IPv6len)
293		copy(addr, b[4:])
294		e.Address = addr
295	default:
296		return errors.New("dns: bad address family")
297	}
298	return nil
299}
300
301func (e *EDNS0_SUBNET) String() (s string) {
302	if e.Address == nil {
303		s = "<nil>"
304	} else if e.Address.To4() != nil {
305		s = e.Address.String()
306	} else {
307		s = "[" + e.Address.String() + "]"
308	}
309	s += "/" + strconv.Itoa(int(e.SourceNetmask)) + "/" + strconv.Itoa(int(e.SourceScope))
310	return
311}
312
313func (e *EDNS0_SUBNET) copy() EDNS0 {
314	return &EDNS0_SUBNET{
315		e.Code,
316		e.Family,
317		e.SourceNetmask,
318		e.SourceScope,
319		e.Address,
320	}
321}
322
323// The EDNS0_COOKIE option is used to add a DNS Cookie to a message.
324//
325//	o := new(dns.OPT)
326//	o.Hdr.Name = "."
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)
332//
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:
336//
337//	cCookie := o.Cookie[:16]
338//	sCookie := o.Cookie[16:]
339//
340// There is no guarantee that the Cookie string has a specific length.
341type EDNS0_COOKIE struct {
342	Code   uint16 // Always EDNS0COOKIE
343	Cookie string // Hex-encoded cookie data
344}
345
346func (e *EDNS0_COOKIE) pack() ([]byte, error) {
347	h, err := hex.DecodeString(e.Cookie)
348	if err != nil {
349		return nil, err
350	}
351	return h, nil
352}
353
354// Option implements the EDNS0 interface.
355func (e *EDNS0_COOKIE) Option() uint16        { return EDNS0COOKIE }
356func (e *EDNS0_COOKIE) unpack(b []byte) error { e.Cookie = hex.EncodeToString(b); return nil }
357func (e *EDNS0_COOKIE) String() string        { return e.Cookie }
358func (e *EDNS0_COOKIE) copy() EDNS0           { return &EDNS0_COOKIE{e.Code, e.Cookie} }
359
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
364//
365//	o := new(dns.OPT)
366//	o.Hdr.Name = "."
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)
372type EDNS0_UL struct {
373	Code  uint16 // Always EDNS0UL
374	Lease uint32
375}
376
377// Option implements the EDNS0 interface.
378func (e *EDNS0_UL) Option() uint16 { return EDNS0UL }
379func (e *EDNS0_UL) String() string { return strconv.FormatUint(uint64(e.Lease), 10) }
380func (e *EDNS0_UL) copy() EDNS0    { return &EDNS0_UL{e.Code, e.Lease} }
381
382// Copied: http://golang.org/src/pkg/net/dnsmsg.go
383func (e *EDNS0_UL) pack() ([]byte, error) {
384	b := make([]byte, 4)
385	binary.BigEndian.PutUint32(b, e.Lease)
386	return b, nil
387}
388
389func (e *EDNS0_UL) unpack(b []byte) error {
390	if len(b) < 4 {
391		return ErrBuf
392	}
393	e.Lease = binary.BigEndian.Uint32(b)
394	return nil
395}
396
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.
399type EDNS0_LLQ struct {
400	Code      uint16 // Always EDNS0LLQ
401	Version   uint16
402	Opcode    uint16
403	Error     uint16
404	Id        uint64
405	LeaseLife uint32
406}
407
408// Option implements the EDNS0 interface.
409func (e *EDNS0_LLQ) Option() uint16 { return EDNS0LLQ }
410
411func (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)
418	return b, nil
419}
420
421func (e *EDNS0_LLQ) unpack(b []byte) error {
422	if len(b) < 18 {
423		return ErrBuf
424	}
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:])
430	return nil
431}
432
433func (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)
437	return s
438}
439func (e *EDNS0_LLQ) copy() EDNS0 {
440	return &EDNS0_LLQ{e.Code, e.Version, e.Opcode, e.Error, e.Id, e.LeaseLife}
441}
442
443// EDNS0_DUA implements the EDNS0 "DNSSEC Algorithm Understood" option. See RFC 6975.
444type EDNS0_DAU struct {
445	Code    uint16 // Always EDNS0DAU
446	AlgCode []uint8
447}
448
449// Option implements the EDNS0 interface.
450func (e *EDNS0_DAU) Option() uint16        { return EDNS0DAU }
451func (e *EDNS0_DAU) pack() ([]byte, error) { return e.AlgCode, nil }
452func (e *EDNS0_DAU) unpack(b []byte) error { e.AlgCode = b; return nil }
453
454func (e *EDNS0_DAU) String() string {
455	s := ""
456	for _, alg := range e.AlgCode {
457		if a, ok := AlgorithmToString[alg]; ok {
458			s += " " + a
459		} else {
460			s += " " + strconv.Itoa(int(alg))
461		}
462	}
463	return s
464}
465func (e *EDNS0_DAU) copy() EDNS0 { return &EDNS0_DAU{e.Code, e.AlgCode} }
466
467// EDNS0_DHU implements the EDNS0 "DS Hash Understood" option. See RFC 6975.
468type EDNS0_DHU struct {
469	Code    uint16 // Always EDNS0DHU
470	AlgCode []uint8
471}
472
473// Option implements the EDNS0 interface.
474func (e *EDNS0_DHU) Option() uint16        { return EDNS0DHU }
475func (e *EDNS0_DHU) pack() ([]byte, error) { return e.AlgCode, nil }
476func (e *EDNS0_DHU) unpack(b []byte) error { e.AlgCode = b; return nil }
477
478func (e *EDNS0_DHU) String() string {
479	s := ""
480	for _, alg := range e.AlgCode {
481		if a, ok := HashToString[alg]; ok {
482			s += " " + a
483		} else {
484			s += " " + strconv.Itoa(int(alg))
485		}
486	}
487	return s
488}
489func (e *EDNS0_DHU) copy() EDNS0 { return &EDNS0_DHU{e.Code, e.AlgCode} }
490
491// EDNS0_N3U implements the EDNS0 "NSEC3 Hash Understood" option. See RFC 6975.
492type EDNS0_N3U struct {
493	Code    uint16 // Always EDNS0N3U
494	AlgCode []uint8
495}
496
497// Option implements the EDNS0 interface.
498func (e *EDNS0_N3U) Option() uint16        { return EDNS0N3U }
499func (e *EDNS0_N3U) pack() ([]byte, error) { return e.AlgCode, nil }
500func (e *EDNS0_N3U) unpack(b []byte) error { e.AlgCode = b; return nil }
501
502func (e *EDNS0_N3U) String() string {
503	// Re-use the hash map
504	s := ""
505	for _, alg := range e.AlgCode {
506		if a, ok := HashToString[alg]; ok {
507			s += " " + a
508		} else {
509			s += " " + strconv.Itoa(int(alg))
510		}
511	}
512	return s
513}
514func (e *EDNS0_N3U) copy() EDNS0 { return &EDNS0_N3U{e.Code, e.AlgCode} }
515
516// EDNS0_EXPIRE implementes the EDNS0 option as described in RFC 7314.
517type EDNS0_EXPIRE struct {
518	Code   uint16 // Always EDNS0EXPIRE
519	Expire uint32
520}
521
522// Option implements the EDNS0 interface.
523func (e *EDNS0_EXPIRE) Option() uint16 { return EDNS0EXPIRE }
524func (e *EDNS0_EXPIRE) String() string { return strconv.FormatUint(uint64(e.Expire), 10) }
525func (e *EDNS0_EXPIRE) copy() EDNS0    { return &EDNS0_EXPIRE{e.Code, e.Expire} }
526
527func (e *EDNS0_EXPIRE) pack() ([]byte, error) {
528	b := make([]byte, 4)
529	binary.BigEndian.PutUint32(b, e.Expire)
530	return b, nil
531}
532
533func (e *EDNS0_EXPIRE) unpack(b []byte) error {
534	if len(b) < 4 {
535		return ErrBuf
536	}
537	e.Expire = binary.BigEndian.Uint32(b)
538	return nil
539}
540
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:
546//
547//	o := new(dns.OPT)
548//	o.Hdr.Name = "."
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)
554type EDNS0_LOCAL struct {
555	Code uint16
556	Data []byte
557}
558
559// Option implements the EDNS0 interface.
560func (e *EDNS0_LOCAL) Option() uint16 { return e.Code }
561func (e *EDNS0_LOCAL) String() string {
562	return strconv.FormatInt(int64(e.Code), 10) + ":0x" + hex.EncodeToString(e.Data)
563}
564func (e *EDNS0_LOCAL) copy() EDNS0 {
565	b := make([]byte, len(e.Data))
566	copy(b, e.Data)
567	return &EDNS0_LOCAL{e.Code, b}
568}
569
570func (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) {
574		return nil, ErrBuf
575	}
576	return b, nil
577}
578
579func (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) {
583		return ErrBuf
584	}
585	return nil
586}
587
588// EDNS0_TCP_KEEPALIVE is an EDNS0 option that instructs the server to keep
589// the TCP connection alive. See RFC 7828.
590type 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.
594}
595
596// Option implements the EDNS0 interface.
597func (e *EDNS0_TCP_KEEPALIVE) Option() uint16 { return EDNS0TCPKEEPALIVE }
598
599func (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")
602	}
603	if e.Timeout == 0 && e.Length != 0 {
604		return nil, errors.New("dns: timeout not specified but length is not 0")
605	}
606	b := make([]byte, 4+e.Length)
607	binary.BigEndian.PutUint16(b[0:], e.Code)
608	binary.BigEndian.PutUint16(b[2:], e.Length)
609	if e.Length == 2 {
610		binary.BigEndian.PutUint16(b[4:], e.Timeout)
611	}
612	return b, nil
613}
614
615func (e *EDNS0_TCP_KEEPALIVE) unpack(b []byte) error {
616	if len(b) < 4 {
617		return ErrBuf
618	}
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))
622	}
623	if e.Length == 2 {
624		if len(b) < 6 {
625			return ErrBuf
626		}
627		e.Timeout = binary.BigEndian.Uint16(b[4:6])
628	}
629	return nil
630}
631
632func (e *EDNS0_TCP_KEEPALIVE) String() (s string) {
633	s = "use tcp keep-alive"
634	if e.Length == 0 {
635		s += ", timeout omitted"
636	} else {
637		s += fmt.Sprintf(", timeout %dms", e.Timeout*100)
638	}
639	return
640}
641func (e *EDNS0_TCP_KEEPALIVE) copy() EDNS0 { return &EDNS0_TCP_KEEPALIVE{e.Code, e.Length, e.Timeout} }
642
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.
646type EDNS0_PADDING struct {
647	Padding []byte
648}
649
650// Option implements the EDNS0 interface.
651func (e *EDNS0_PADDING) Option() uint16        { return EDNS0PADDING }
652func (e *EDNS0_PADDING) pack() ([]byte, error) { return e.Padding, nil }
653func (e *EDNS0_PADDING) unpack(b []byte) error { e.Padding = b; return nil }
654func (e *EDNS0_PADDING) String() string        { return fmt.Sprintf("%0X", e.Padding) }
655func (e *EDNS0_PADDING) copy() EDNS0 {
656	b := make([]byte, len(e.Padding))
657	copy(b, e.Padding)
658	return &EDNS0_PADDING{b}
659}
660