dns/edns.go

138 lines
3.2 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 `dns:"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
*/
type RR_OPT struct {
Hdr RR_Header
Option []Option `dns:"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:39:31 +11:00
l := rr.Hdr.Len()
for i := 0; i < len(rr.Option); i++ {
l += 2 + len(rr.Option[i].Data)/2
}
return l
}
2012-01-13 06:39:31 +11:00
// Version returns the EDNS version.
func (rr *RR_OPT) Version() uint8 {
2012-01-13 06:39:31 +11:00
return uint8(rr.Hdr.Ttl & 0x00FF00FFFF)
}
2012-01-13 06:39:31 +11:00
// SetVersion sets the version of EDNS. This is usually zero.
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.
func (rr *RR_OPT) UDPSize() uint16 {
2010-12-28 00:31:31 +11:00
return rr.Hdr.Class
}
2012-01-13 06:39:31 +11:00
// SetUDPSize sets 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
*/
2012-01-13 06:39:31 +11:00
// Do gets the value of the DO (DNSSEC OK) 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 {
2012-01-20 22:24:20 +11:00
for i := 0; i < len(rr.Option); i++ {
if rr.Option[i].Code == OptionCodeNSID {
return "NSID: " + rr.Option[i].Data
}
}
// TODO: error or nil string?
2012-01-20 22:24:20 +11:00
return "Not found"
}
2011-09-20 20:52:23 +10:00
// SetNsid sets the NSID from a hex character string.
2012-01-13 06:39:31 +11:00
// Use the empty string when requesting an NSID.
2011-01-18 07:10:48 +11:00
func (rr *RR_OPT) SetNsid(hexnsid string) {
2012-01-16 01:30:12 +11:00
rr.Option = append(rr.Option, Option{OptionCodeNSID, hexnsid})
2011-01-07 01:24:18 +11:00
}