2010-12-22 20:30:41 +11:00
|
|
|
package dns
|
|
|
|
|
2010-12-28 00:31:31 +11:00
|
|
|
import (
|
2011-02-25 02:22:14 +11:00
|
|
|
"encoding/hex"
|
2012-06-01 02:36:27 +10:00
|
|
|
"net"
|
2011-12-10 07:45:57 +11:00
|
|
|
"strconv"
|
2010-12-28 00:31:31 +11:00
|
|
|
)
|
|
|
|
|
2011-03-25 21:19:35 +11:00
|
|
|
// EDNS0 Option codes.
|
2010-12-22 20:30:41 +11:00
|
|
|
const (
|
2012-06-01 02:36:27 +10:00
|
|
|
_ = iota
|
|
|
|
OptionLLQ // not used
|
|
|
|
OptionUL // not used
|
|
|
|
OptionNSID // NSID, RFC5001
|
|
|
|
OptionSUBNET = 0x50fa // client-subnet draft
|
|
|
|
_DO = 1 << 7 // dnssec ok
|
2010-12-22 20:30:41 +11:00
|
|
|
)
|
|
|
|
|
2010-12-28 19:41:54 +11:00
|
|
|
/*
|
|
|
|
* EDNS extended RR.
|
|
|
|
* This is the EDNS0 Header
|
|
|
|
* Name string "domain-name"
|
|
|
|
* Opt uint16 // was type, but is always TypeOPT
|
|
|
|
* UDPSize uint16 // was class
|
|
|
|
* ExtendedRcode uint8 // was TTL
|
|
|
|
* Version uint8 // was TTL
|
|
|
|
* Z uint16 // was TTL (all flags should be put here)
|
|
|
|
* Rdlength uint16 // length of data after the header
|
|
|
|
*/
|
2010-12-22 20:30:41 +11:00
|
|
|
|
2010-12-23 06:53:18 +11:00
|
|
|
type RR_OPT struct {
|
2010-12-27 19:58:45 +11:00
|
|
|
Hdr RR_Header
|
2012-06-01 02:36:27 +10:00
|
|
|
Option []EDNS0 `dns:"opt"` // tag is used in Pack and Unpack
|
2010-12-27 19:58:45 +11:00
|
|
|
}
|
|
|
|
|
2010-12-23 06:53:18 +11:00
|
|
|
func (rr *RR_OPT) Header() *RR_Header {
|
2010-12-23 21:02:01 +11:00
|
|
|
return &rr.Hdr
|
2010-12-22 20:30:41 +11:00
|
|
|
}
|
|
|
|
|
2010-12-23 06:53:18 +11:00
|
|
|
func (rr *RR_OPT) String() string {
|
2011-09-16 05:57:10 +10:00
|
|
|
s := "\n;; OPT PSEUDOSECTION:\n; EDNS: version " + strconv.Itoa(int(rr.Version())) + "; "
|
2011-01-06 20:24:00 +11:00
|
|
|
if rr.Do() {
|
2010-12-28 00:31:31 +11:00
|
|
|
s += "flags: do; "
|
|
|
|
} else {
|
|
|
|
s += "flags: ; "
|
|
|
|
}
|
2011-01-07 01:06:06 +11:00
|
|
|
s += "udp: " + strconv.Itoa(int(rr.UDPSize()))
|
2010-12-28 00:31:31 +11:00
|
|
|
|
2010-12-23 21:02:01 +11:00
|
|
|
for _, o := range rr.Option {
|
2012-06-01 02:36:27 +10:00
|
|
|
switch o.(type) {
|
|
|
|
case *EDNS0_NSID:
|
|
|
|
s += "\n; NSID: " + o.String()
|
|
|
|
h, e := o.Bytes()
|
2011-02-25 02:22:14 +11:00
|
|
|
var r string
|
|
|
|
if e == nil {
|
|
|
|
for _, c := range h {
|
|
|
|
r += "(" + string(c) + ")"
|
|
|
|
}
|
|
|
|
s += " " + r
|
|
|
|
}
|
2010-12-23 21:02:01 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
2010-12-22 20:30:41 +11:00
|
|
|
}
|
2010-12-24 00:27:04 +11:00
|
|
|
|
2012-01-10 20:43:28 +11:00
|
|
|
func (rr *RR_OPT) Len() int {
|
2012-01-13 06:39:31 +11:00
|
|
|
l := rr.Hdr.Len()
|
|
|
|
for i := 0; i < len(rr.Option); i++ {
|
2012-06-01 02:36:27 +10:00
|
|
|
lo, _ := rr.Option[i].Bytes()
|
|
|
|
l += 2 + len(lo)
|
2012-01-13 06:39:31 +11:00
|
|
|
}
|
|
|
|
return l
|
2012-01-10 20:43:28 +11:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:39:31 +11:00
|
|
|
// Version returns the EDNS version.
|
2011-01-06 20:24:00 +11:00
|
|
|
func (rr *RR_OPT) Version() uint8 {
|
2012-01-13 06:39:31 +11:00
|
|
|
return uint8(rr.Hdr.Ttl & 0x00FF00FFFF)
|
2011-01-06 20:24:00 +11:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:39:31 +11:00
|
|
|
// SetVersion sets the version of EDNS. This is usually zero.
|
2011-01-06 20:24:00 +11:00
|
|
|
func (rr *RR_OPT) SetVersion(v uint8) {
|
2012-01-13 06:39:31 +11:00
|
|
|
rr.Hdr.Ttl = rr.Hdr.Ttl&0xFF00FFFF | uint32(v)
|
2010-12-28 00:31:31 +11:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:39:31 +11:00
|
|
|
// UDPSize gets the UDP buffer size.
|
2011-01-06 20:24:00 +11:00
|
|
|
func (rr *RR_OPT) UDPSize() uint16 {
|
2010-12-28 00:31:31 +11:00
|
|
|
return rr.Hdr.Class
|
2010-12-24 00:27:04 +11:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:39:31 +11:00
|
|
|
// SetUDPSize sets the UDP buffer size.
|
2011-01-06 20:24:00 +11:00
|
|
|
func (rr *RR_OPT) SetUDPSize(size uint16) {
|
|
|
|
rr.Hdr.Class = size
|
|
|
|
}
|
|
|
|
|
2010-12-31 04:42:40 +11:00
|
|
|
/* from RFC 3225
|
2011-01-07 00:58:58 +11:00
|
|
|
+0 (MSB) +1 (LSB)
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
0: | EXTENDED-RCODE | VERSION |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
2: |DO| Z |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
2010-12-31 04:42:40 +11:00
|
|
|
*/
|
|
|
|
|
2012-01-13 06:39:31 +11:00
|
|
|
// Do gets the value of the DO (DNSSEC OK) bit.
|
2011-01-06 20:24:00 +11:00
|
|
|
func (rr *RR_OPT) Do() bool {
|
2011-02-25 02:22:14 +11:00
|
|
|
return byte(rr.Hdr.Ttl>>8)&_DO == _DO
|
2011-01-06 20:24:00 +11:00
|
|
|
}
|
|
|
|
|
2011-09-20 20:52:23 +10:00
|
|
|
// SetDo sets the DO (DNSSEC OK) bit.
|
2011-01-06 20:24:00 +11:00
|
|
|
func (rr *RR_OPT) SetDo() {
|
2011-02-25 02:22:14 +11:00
|
|
|
b1 := byte(rr.Hdr.Ttl >> 24)
|
|
|
|
b2 := byte(rr.Hdr.Ttl >> 16)
|
|
|
|
b3 := byte(rr.Hdr.Ttl >> 8)
|
|
|
|
b4 := byte(rr.Hdr.Ttl)
|
|
|
|
b3 |= _DO // Set it
|
|
|
|
rr.Hdr.Ttl = uint32(b1)<<24 | uint32(b2)<<16 | uint32(b3)<<8 | uint32(b4)
|
2010-12-24 00:27:04 +11:00
|
|
|
}
|
|
|
|
|
2012-06-01 02:36:27 +10:00
|
|
|
// EDNS0 defines a EDNS0 Option
|
|
|
|
type EDNS0 interface {
|
|
|
|
// Option return the option code for the option.
|
|
|
|
Option() uint16
|
|
|
|
// Bytes returns the bytes of the option data.
|
|
|
|
Bytes() ([]byte, error)
|
|
|
|
// String returns the string representation of the option.
|
|
|
|
String() string
|
|
|
|
// SetBytes sets the data as found in the packet. Is also sets
|
|
|
|
// the length of the slice as the length of the option data.
|
|
|
|
SetBytes([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
type EDNS0_NSID struct {
|
|
|
|
Code uint16
|
|
|
|
Nsid string // This string must be encoded as Hex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EDNS0_NSID) Option() uint16 {
|
|
|
|
return e.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EDNS0_NSID) Bytes() ([]byte, error) {
|
|
|
|
h, err := hex.DecodeString(e.Nsid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-01-20 22:24:20 +11:00
|
|
|
}
|
2012-06-01 02:36:27 +10:00
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EDNS0_NSID) String() string {
|
|
|
|
return string(e.Nsid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EDNS0_NSID) SetBytes(b []byte) {
|
|
|
|
e.Code = OptionNSID
|
|
|
|
e.Nsid = hex.EncodeToString(b)
|
2010-12-24 00:27:04 +11:00
|
|
|
}
|
2011-01-06 20:24:00 +11:00
|
|
|
|
2012-06-01 02:36:27 +10:00
|
|
|
type EDNS0_SUBNET struct {
|
|
|
|
Code uint16
|
|
|
|
Family uint16
|
|
|
|
SourceNetmask uint8
|
|
|
|
SourceScope uint8
|
|
|
|
Address []net.IP
|
2011-01-07 01:24:18 +11:00
|
|
|
}
|