dns/edns.go

143 lines
3.4 KiB
Go
Raw Normal View History

package dns
2010-12-28 00:31:31 +11:00
import (
2011-02-25 02:22:14 +11:00
"encoding/hex"
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.
const (
2011-03-22 19:44:33 +11:00
_ = iota
OptionCodeLLQ // not used
OptionCodeUL // not used
OptionCodeNSID // NSID, RFC5001
2010-12-31 04:42:40 +11:00
_DO = 1 << 7 // dnssec ok
)
2010-12-25 23:09:27 +11:00
// An ENDS0 option rdata element.
2010-12-22 22:26:50 +11:00
type Option struct {
2010-12-23 21:02:01 +11:00
Code uint16
Data string "hex"
}
/*
* 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
*/
2011-02-25 02:13:23 +11:00
// Adding an EDNS0 record to a message is done as follows:
// opt := new(RR_OPT)
// opt.Hdr = dns.RR_Header{Name: "", Rrtype: TypeOPT}
// opt.SetVersion(0) // set version to zero
// opt.SetDo() // set the DO bit
// opt.SetUDPSize(4096) // set the message size
// m.Extra = make([]RR, 1)
// m.Extra[0] = opt // add OPT RR to the message
type RR_OPT struct {
Hdr RR_Header
2011-01-28 00:28:04 +11:00
Option []Option "OPT" // tag is used in Pack and Unpack
}
func (rr *RR_OPT) Header() *RR_Header {
2010-12-23 21:02:01 +11:00
return &rr.Hdr
}
func (rr *RR_OPT) String() string {
s := "\n;; OPT PSEUDOSECTION:\n; EDNS: version " + strconv.Itoa(int(rr.Version())) + "; "
if rr.Do() {
2010-12-28 00:31:31 +11:00
s += "flags: do; "
} else {
s += "flags: ; "
}
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 {
switch o.Code {
case OptionCodeNSID:
s += "\n; NSID: " + o.Data
2011-02-25 02:22:14 +11:00
h, e := hex.DecodeString(o.Data)
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
}
func (rr *RR_OPT) Len() int {
2012-01-13 06:16:00 +11:00
l := rr.Hdr.Len()
for i := 0; i < len(rr.Option); i++ {
l += 2 + len(rr.Option[i].Data)/2
}
2012-01-13 06:16:16 +11:00
return l
}
2011-03-24 05:15:30 +11:00
// TODO(mg)
2011-01-28 00:28:04 +11:00
// Get the EDNS version (always 0 currently).
func (rr *RR_OPT) Version() uint8 {
2011-02-25 02:22:14 +11:00
return 0
}
2011-01-28 00:28:04 +11:00
// Set the version of EDNS.
func (rr *RR_OPT) SetVersion(v uint8) {
return
2010-12-28 00:31:31 +11:00
}
2011-01-28 00:28:04 +11:00
// Get the UDP buffer size.
func (rr *RR_OPT) UDPSize() uint16 {
2010-12-28 00:31:31 +11:00
return rr.Hdr.Class
}
2011-01-28 00:28:04 +11:00
// Set the UDP buffer size/
func (rr *RR_OPT) SetUDPSize(size uint16) {
rr.Hdr.Class = size
}
2010-12-31 04:42:40 +11:00
/* from RFC 3225
+0 (MSB) +1 (LSB)
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
0: | EXTENDED-RCODE | VERSION |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
2: |DO| Z |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
2010-12-31 04:42:40 +11:00
*/
2011-01-28 00:28:04 +11:00
// Get the DO bit.
func (rr *RR_OPT) Do() bool {
2011-02-25 02:22:14 +11:00
return byte(rr.Hdr.Ttl>>8)&_DO == _DO
}
2011-09-20 20:52:23 +10:00
// SetDo sets the DO (DNSSEC OK) bit.
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)
}
2011-09-20 20:52:23 +10:00
// Nsid returns the NSID as hex character string.
func (rr *RR_OPT) Nsid() string {
return "NSID: " + rr.Option[0].Data
}
2011-09-20 20:52:23 +10:00
// SetNsid sets the NSID from a hex character string.
// Use the empty string when requesting NSID.
2011-01-18 07:10:48 +11:00
func (rr *RR_OPT) SetNsid(hexnsid string) {
2011-02-25 02:22:14 +11:00
rr.Option = make([]Option, 1) // TODO(mg) check length first?
rr.Option[0].Code = OptionCodeNSID
rr.Option[0].Data = hexnsid
2011-01-07 01:24:18 +11:00
}