dns/defaults.go

336 lines
9.0 KiB
Go
Raw Normal View History

2011-03-16 22:19:15 +11:00
package dns
2012-07-04 17:48:59 +10:00
import (
"net"
"strconv"
)
const hexDigit = "0123456789abcdef"
// Everything is assumed in the ClassINET class. If
// you need other classes you are on your own.
2011-03-23 06:12:36 +11:00
2011-09-11 20:57:06 +10:00
// SetReply creates a reply packet from a request message.
func (dns *Msg) SetReply(request *Msg) *Msg {
2011-03-24 19:24:24 +11:00
dns.MsgHdr.Id = request.MsgHdr.Id
2012-08-05 07:16:29 +10:00
dns.MsgHdr.RecursionDesired = request.MsgHdr.RecursionDesired // Copy rd bit
2011-03-24 19:24:24 +11:00
dns.MsgHdr.Response = true
dns.MsgHdr.Opcode = OpcodeQuery
dns.MsgHdr.Rcode = RcodeSuccess
dns.Question = make([]Question, 1)
dns.Question[0] = request.Question[0]
return dns
2011-03-22 08:53:15 +11:00
}
2011-09-11 21:01:18 +10:00
// SetQuestion creates a question packet.
func (dns *Msg) SetQuestion(z string, t uint16) *Msg {
dns.MsgHdr.Id = Id()
dns.MsgHdr.RecursionDesired = true
2011-04-15 06:22:24 +10:00
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, t, ClassINET}
return dns
2011-04-15 06:22:24 +10:00
}
2011-09-11 21:01:18 +10:00
// SetNotify creates a notify packet.
func (dns *Msg) SetNotify(z string) *Msg {
2011-03-16 22:19:15 +11:00
dns.MsgHdr.Opcode = OpcodeNotify
dns.MsgHdr.Authoritative = true
dns.MsgHdr.Id = Id()
dns.Question = make([]Question, 1)
2011-03-31 02:50:07 +11:00
dns.Question[0] = Question{z, TypeSOA, ClassINET}
return dns
2011-03-16 22:19:15 +11:00
}
2011-09-11 21:01:18 +10:00
// SetRcode creates an error packet.
func (dns *Msg) SetRcode(request *Msg, rcode int) *Msg {
2011-11-28 16:03:21 +11:00
dns.MsgHdr.Rcode = rcode
dns.MsgHdr.Opcode = OpcodeQuery
dns.MsgHdr.Response = true
dns.MsgHdr.Authoritative = false
dns.MsgHdr.Id = request.MsgHdr.Id
dns.Question = make([]Question, 1)
dns.Question[0] = request.Question[0]
return dns
2011-07-27 03:16:28 +10:00
}
2011-09-11 21:01:18 +10:00
// SetRcodeFormatError creates a packet with FormError set.
func (dns *Msg) SetRcodeFormatError(request *Msg) *Msg {
2011-11-28 16:03:21 +11:00
dns.MsgHdr.Rcode = RcodeFormatError
dns.MsgHdr.Opcode = OpcodeQuery
dns.MsgHdr.Response = true
dns.MsgHdr.Authoritative = false
dns.MsgHdr.Id = request.MsgHdr.Id
return dns
2011-07-27 03:16:28 +10:00
}
2011-09-02 22:28:40 +10:00
// SetUpdate makes the message a dynamic update packet. It
// sets the ZONE section to: z, TypeSOA, classINET.
func (dns *Msg) SetUpdate(z string) *Msg {
2012-01-20 22:24:20 +11:00
dns.MsgHdr.Id = Id()
dns.MsgHdr.Response = false
2012-01-20 22:24:20 +11:00
dns.MsgHdr.Opcode = OpcodeUpdate
2012-06-01 21:35:52 +10:00
dns.Compress = false // BIND9 cannot handle compression
2012-01-20 22:24:20 +11:00
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, TypeSOA, ClassINET}
return dns
2011-09-02 22:28:40 +10:00
}
// SetIxfr creates dns msg suitable for requesting an ixfr.
func (dns *Msg) SetIxfr(z string, serial uint32) *Msg {
2011-09-02 22:28:40 +10:00
dns.MsgHdr.Id = Id()
dns.Question = make([]Question, 1)
dns.Ns = make([]RR, 1)
s := new(RR_SOA)
s.Hdr = RR_Header{z, TypeSOA, ClassINET, DefaultTtl, 0}
2011-09-02 22:28:40 +10:00
s.Serial = serial
dns.Question[0] = Question{z, TypeIXFR, ClassINET}
dns.Ns[0] = s
return dns
2011-09-02 22:28:40 +10:00
}
2011-09-11 21:01:18 +10:00
// SetAxfr creates dns msg suitable for requesting an axfr.
func (dns *Msg) SetAxfr(z string) *Msg {
2011-09-02 22:28:40 +10:00
dns.MsgHdr.Id = Id()
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, TypeAXFR, ClassINET}
return dns
2011-09-02 22:28:40 +10:00
}
// SetTsig appends a TSIG RR to the message.
2011-09-11 21:01:18 +10:00
// This is only a skeleton Tsig RR that is added as the last RR in the
// additional section. The Tsig is calculated when the message is being send.
func (dns *Msg) SetTsig(z, algo string, fudge, timesigned int64) *Msg {
2011-09-02 22:28:40 +10:00
t := new(RR_TSIG)
t.Hdr = RR_Header{z, TypeTSIG, ClassANY, 0, 0}
t.Algorithm = algo
t.Fudge = 300
2012-03-05 02:35:21 +11:00
t.TimeSigned = uint64(timesigned)
t.OrigId = dns.MsgHdr.Id
2011-09-02 22:28:40 +10:00
dns.Extra = append(dns.Extra, t)
return dns
2011-09-02 22:28:40 +10:00
}
2011-09-11 21:01:18 +10:00
// SetEdns0 appends a EDNS0 OPT RR to the message.
// TSIG should always the last RR in a message.
func (dns *Msg) SetEdns0(udpsize uint16, do bool) *Msg {
2011-11-28 16:03:21 +11:00
e := new(RR_OPT)
e.Hdr.Name = "."
e.Hdr.Rrtype = TypeOPT
e.SetUDPSize(udpsize)
if do {
e.SetDo()
}
2011-09-11 21:01:18 +10:00
dns.Extra = append(dns.Extra, e)
return dns
2011-09-11 21:01:18 +10:00
}
2011-09-02 22:28:40 +10:00
// IsRcode checks if the header of the packet has rcode set.
func (dns *Msg) IsRcode(rcode int) (ok bool) {
if len(dns.Question) == 0 {
return false
}
2011-11-28 16:03:21 +11:00
ok = dns.MsgHdr.Rcode == rcode
return
2011-09-02 22:28:40 +10:00
}
2011-09-11 21:01:18 +10:00
// IsQuestion returns true if the packet is a question.
2011-08-22 20:52:13 +10:00
func (dns *Msg) IsQuestion() (ok bool) {
2011-11-28 16:03:21 +11:00
if len(dns.Question) == 0 {
return false
}
ok = dns.MsgHdr.Response == false
return
2011-08-22 20:52:13 +10:00
}
2011-09-11 21:01:18 +10:00
// IsRcodeFormatError checks if the message has FormErr set.
2011-07-27 05:41:24 +10:00
func (dns *Msg) IsRcodeFormatError() (ok bool) {
if len(dns.Question) == 0 {
return false
}
2011-11-28 16:03:21 +11:00
ok = dns.MsgHdr.Rcode == RcodeFormatError
return
2011-07-27 05:41:24 +10:00
}
2011-09-02 22:28:40 +10:00
// IsUpdate checks if the message is a dynamic update packet.
2011-03-25 20:16:55 +11:00
func (dns *Msg) IsUpdate() (ok bool) {
if len(dns.Question) == 0 {
return false
}
ok = dns.MsgHdr.Opcode == OpcodeUpdate
ok = ok && dns.Question[0].Qtype == TypeSOA
return
2011-03-25 20:16:55 +11:00
}
2011-09-02 22:28:40 +10:00
// IsNotify checks if the message is a valid notify packet.
2011-03-23 19:50:38 +11:00
func (dns *Msg) IsNotify() (ok bool) {
2011-03-16 22:19:15 +11:00
if len(dns.Question) == 0 {
2011-03-23 19:50:38 +11:00
return false
2011-03-16 22:19:15 +11:00
}
2011-03-23 19:50:38 +11:00
ok = dns.MsgHdr.Opcode == OpcodeNotify
2011-03-16 22:19:15 +11:00
ok = ok && dns.Question[0].Qclass == ClassINET
ok = ok && dns.Question[0].Qtype == TypeSOA
2011-04-19 06:08:12 +10:00
return
2011-03-16 22:19:15 +11:00
}
2011-09-02 22:28:40 +10:00
// IsAxfr checks if the message is a valid axfr request packet.
2011-03-23 20:48:21 +11:00
func (dns *Msg) IsAxfr() (ok bool) {
if len(dns.Question) == 0 {
return false
}
ok = dns.MsgHdr.Opcode == OpcodeQuery
ok = ok && dns.Question[0].Qclass == ClassINET
ok = ok && dns.Question[0].Qtype == TypeAXFR
2011-04-19 06:08:12 +10:00
return
2011-03-23 20:48:21 +11:00
}
2011-09-02 22:28:40 +10:00
// IsIXfr checks if the message is a valid ixfr request packet.
2011-03-23 20:48:21 +11:00
func (dns *Msg) IsIxfr() (ok bool) {
if len(dns.Question) == 0 {
return false
}
ok = dns.MsgHdr.Opcode == OpcodeQuery
ok = ok && dns.Question[0].Qclass == ClassINET
ok = ok && dns.Question[0].Qtype == TypeIXFR
2011-04-19 06:08:12 +10:00
return
}
// IsTsig checks if the message has a TSIG record as the last record
// in the additional section.
2011-04-19 06:08:12 +10:00
func (dns *Msg) IsTsig() (ok bool) {
if len(dns.Extra) > 0 {
return dns.Extra[len(dns.Extra)-1].Header().Rrtype == TypeTSIG
}
return
2011-04-19 06:08:12 +10:00
}
2012-06-01 21:11:17 +10:00
// IsEdns0 checks if the message has a EDNS0 (OPT) record, any EDNS0
2012-01-27 08:47:54 +11:00
// record in the additional section will do.
2011-09-11 21:01:18 +10:00
func (dns *Msg) IsEdns0() (ok bool) {
2011-11-28 16:03:21 +11:00
for _, r := range dns.Extra {
if r.Header().Rrtype == TypeOPT {
return true
}
}
return
2011-09-11 21:01:18 +10:00
}
// IsDomainName checks if s is a valid domainname, it returns
// the number of labels, total length and true, when a domain name is valid.
2012-02-12 21:37:52 +11:00
// When false is returned the labelcount and length are not defined.
func IsDomainName(s string) (uint8, uint8, bool) { // copied from net package.
// See RFC 1035, RFC 3696.
l := len(s)
if l == 0 || l > 255 {
return 0, 0, false
}
longer := 0
// Simplify checking loop: make the name end in a dot.
// Don't call Fqdn() to save another len(s).
2012-02-15 08:55:14 +11:00
// Keep in mind that if we do this, otherwise we report a length+1
if s[l-1] != '.' {
s += "."
l++
longer = 1
}
2012-02-24 05:37:08 +11:00
// Preloop check for root label
if s == "." {
return 0, 1, true
}
last := byte('.')
2012-02-14 03:58:43 +11:00
ok := false // ok once we've seen a letter or digit
partlen := 0
2012-01-09 01:56:19 +11:00
labels := uint8(0)
for i := 0; i < l; i++ {
c := s[i]
switch {
default:
return 0, uint8(l - longer), false
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_' || c == '*' || c == '/':
ok = true
partlen++
case c == '\\':
// Ok
case '0' <= c && c <= '9':
2012-02-14 03:58:43 +11:00
ok = true
partlen++
case c == '-':
// byte before dash cannot be dot
if last == '.' {
return 0, uint8(l - longer), false
}
partlen++
case c == '.':
2012-02-29 06:47:25 +11:00
// byte before dot cannot be dot
if last == '.' {
return 0, uint8(l - longer), false
}
if last == '\\' { // Ok, escaped dot.
partlen++
break
}
if partlen > 63 || partlen == 0 {
return 0, uint8(l - longer), false
}
partlen = 0
labels++
}
last = c
}
return labels, uint8(l - longer), ok
}
2012-02-15 20:22:52 +11:00
// IsSubDomain checks if child is indeed a child of the parent.
2012-02-15 08:55:14 +11:00
func IsSubDomain(parent, child string) bool {
2012-07-17 03:16:36 +10:00
// Entire child is contained in parent
2012-08-08 23:16:22 +10:00
return CompareLabels(parent, child) == LenLabels(parent)
2012-02-15 08:55:14 +11:00
}
2012-02-12 21:37:52 +11:00
// IsFqdn checks if a domain name is fully qualified.
func IsFqdn(s string) bool {
2012-02-24 05:37:08 +11:00
l := len(s)
2012-02-15 08:55:14 +11:00
if l == 0 {
return false // ?
}
2012-02-15 08:55:14 +11:00
return s[l-1] == '.'
}
// Fqdns return the fully qualified domain name from s.
2012-02-12 21:37:52 +11:00
// If s is already fully qualified, it behaves as the identity function.
func Fqdn(s string) string {
if IsFqdn(s) {
return s
}
return s + "."
}
2012-07-04 17:48:59 +10:00
// Copied from the official Go code
// ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
// address addr suitable for rDNS (PTR) record lookup or an error if it fails
// to parse the IP address.
func ReverseAddr(addr string) (arpa string, err error) {
ip := net.ParseIP(addr)
if ip == nil {
return "", &Error{Err: "unrecognized address", Name: addr}
}
if ip.To4() != nil {
return strconv.Itoa(int(ip[15])) + "." + strconv.Itoa(int(ip[14])) + "." + strconv.Itoa(int(ip[13])) + "." +
strconv.Itoa(int(ip[12])) + ".in-addr.arpa.", nil
}
// Must be IPv6
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
// Add it, in reverse, to the buffer
for i := len(ip) - 1; i >= 0; i-- {
v := ip[i]
buf = append(buf, hexDigit[v&0xF])
buf = append(buf, '.')
buf = append(buf, hexDigit[v>>4])
buf = append(buf, '.')
}
// Append "ip6.arpa." and return (buf already has the final .)
buf = append(buf, "ip6.arpa."...)
return string(buf), nil
}