dns/defaults.go

243 lines
6.5 KiB
Go
Raw Normal View History

2011-03-16 11:19:15 +00:00
package dns
2012-07-04 07:48:59 +00:00
import (
"errors"
2012-07-04 07:48:59 +00:00
"net"
"strconv"
)
const hexDigit = "0123456789abcdef"
// Everything is assumed in ClassINET.
2011-03-22 19:12:36 +00:00
2014-07-31 08:48:26 +00:00
// SetReply creates a reply message from a request message.
func (dns *Msg) SetReply(request *Msg) *Msg {
2012-09-05 14:31:13 +00:00
dns.Id = request.Id
dns.RecursionDesired = request.RecursionDesired // Copy rd bit
dns.Response = true
dns.Opcode = OpcodeQuery
dns.Rcode = RcodeSuccess
if len(request.Question) > 0 {
dns.Question = make([]Question, 1)
dns.Question[0] = request.Question[0]
}
return dns
2011-03-21 21:53:15 +00:00
}
2014-07-31 08:48:26 +00:00
// SetQuestion creates a question message.
func (dns *Msg) SetQuestion(z string, t uint16) *Msg {
2012-09-05 14:31:13 +00:00
dns.Id = Id()
dns.RecursionDesired = true
2011-04-14 20:22:24 +00:00
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, t, ClassINET}
return dns
2011-04-14 20:22:24 +00:00
}
2014-07-31 08:48:26 +00:00
// SetNotify creates a notify message.
func (dns *Msg) SetNotify(z string) *Msg {
2012-09-05 14:31:13 +00:00
dns.Opcode = OpcodeNotify
dns.Authoritative = true
dns.Id = Id()
2011-03-16 11:19:15 +00:00
dns.Question = make([]Question, 1)
2011-03-30 15:50:07 +00:00
dns.Question[0] = Question{z, TypeSOA, ClassINET}
return dns
2011-03-16 11:19:15 +00:00
}
2014-07-31 08:48:26 +00:00
// SetRcode creates an error message suitable for the request.
func (dns *Msg) SetRcode(request *Msg, rcode int) *Msg {
dns.SetReply(request)
2012-09-05 14:31:13 +00:00
dns.Rcode = rcode
return dns
2011-07-26 17:16:28 +00:00
}
2014-07-31 08:48:26 +00:00
// SetRcodeFormatError creates a message with FormError set.
func (dns *Msg) SetRcodeFormatError(request *Msg) *Msg {
2012-09-05 14:31:13 +00:00
dns.Rcode = RcodeFormatError
dns.Opcode = OpcodeQuery
dns.Response = true
dns.Authoritative = false
dns.Id = request.Id
return dns
2011-07-26 17:16:28 +00:00
}
2014-07-31 08:48:26 +00:00
// SetUpdate makes the message a dynamic update message. It
2012-08-21 15:36:58 +00:00
// sets the ZONE section to: z, TypeSOA, ClassINET.
func (dns *Msg) SetUpdate(z string) *Msg {
2012-09-05 14:31:13 +00:00
dns.Id = Id()
dns.Response = false
dns.Opcode = OpcodeUpdate
2012-06-01 11:35:52 +00:00
dns.Compress = false // BIND9 cannot handle compression
2012-01-20 11:24:20 +00:00
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, TypeSOA, ClassINET}
return dns
2011-09-02 12:28:40 +00:00
}
2014-07-31 08:48:26 +00:00
// SetIxfr creates message for requesting an IXFR.
func (dns *Msg) SetIxfr(z string, serial uint32) *Msg {
2012-09-05 14:31:13 +00:00
dns.Id = Id()
2011-09-02 12:28:40 +00:00
dns.Question = make([]Question, 1)
dns.Ns = make([]RR, 1)
s := new(SOA)
2012-08-17 06:31:38 +00:00
s.Hdr = RR_Header{z, TypeSOA, ClassINET, defaultTtl, 0}
2011-09-02 12:28:40 +00:00
s.Serial = serial
dns.Question[0] = Question{z, TypeIXFR, ClassINET}
dns.Ns[0] = s
return dns
2011-09-02 12:28:40 +00:00
}
2014-07-31 08:48:26 +00:00
// SetAxfr creates message for requesting an AXFR.
func (dns *Msg) SetAxfr(z string) *Msg {
2012-09-05 14:31:13 +00:00
dns.Id = Id()
2011-09-02 12:28:40 +00:00
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, TypeAXFR, ClassINET}
return dns
2011-09-02 12:28:40 +00:00
}
// SetTsig appends a TSIG RR to the message.
2013-05-05 18:30:44 +00: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 {
t := new(TSIG)
2011-09-02 12:28:40 +00:00
t.Hdr = RR_Header{z, TypeTSIG, ClassANY, 0, 0}
t.Algorithm = algo
t.Fudge = 300
2012-03-04 15:35:21 +00:00
t.TimeSigned = uint64(timesigned)
2012-09-05 14:31:13 +00:00
t.OrigId = dns.Id
2011-09-02 12:28:40 +00:00
dns.Extra = append(dns.Extra, t)
return dns
2011-09-02 12:28:40 +00:00
}
2013-05-05 18:30:44 +00:00
// SetEdns0 appends a EDNS0 OPT RR to the message.
2011-09-11 11:01:18 +00:00
// TSIG should always the last RR in a message.
func (dns *Msg) SetEdns0(udpsize uint16, do bool) *Msg {
e := new(OPT)
2011-11-28 05:03:21 +00:00
e.Hdr.Name = "."
e.Hdr.Rrtype = TypeOPT
e.SetUDPSize(udpsize)
if do {
e.SetDo()
}
2011-09-11 11:01:18 +00:00
dns.Extra = append(dns.Extra, e)
return dns
2011-09-11 11:01:18 +00:00
}
// IsTsig checks if the message has a TSIG record as the last record
// in the additional section. It returns the TSIG record found or nil.
func (dns *Msg) IsTsig() *TSIG {
if len(dns.Extra) > 0 {
if dns.Extra[len(dns.Extra)-1].Header().Rrtype == TypeTSIG {
return dns.Extra[len(dns.Extra)-1].(*TSIG)
}
}
return nil
2011-04-18 20:08:12 +00:00
}
2012-06-01 11:11:17 +00:00
// IsEdns0 checks if the message has a EDNS0 (OPT) record, any EDNS0
// record in the additional section will do. It returns the OPT record
// found or nil.
func (dns *Msg) IsEdns0() *OPT {
2011-11-28 05:03:21 +00:00
for _, r := range dns.Extra {
if r.Header().Rrtype == TypeOPT {
return r.(*OPT)
2011-11-28 05:03:21 +00:00
}
}
return nil
2011-09-11 11:01:18 +00:00
}
// IsDomainName checks if s is a valid domainname, it returns
2013-09-10 13:21:55 +00:00
// the number of labels and true, when a domain name is valid.
2013-09-10 13:13:10 +00:00
// Note that non fully qualified domain name is considered valid, in this case the
// last label is counted in the number of labels.
2013-09-10 13:13:10 +00:00
// When false is returned the number of labels is not defined.
func IsDomainName(s string) (labels int, ok bool) {
_, labels, err := packDomainName(s, nil, 0, nil, false)
return labels, err == nil
}
// IsSubDomain checks if child is indeed a child of the parent. Both child and
// parent are *not* downcased before doing the comparison.
2012-02-14 21:55:14 +00:00
func IsSubDomain(parent, child string) bool {
2012-07-16 17:16:36 +00:00
// Entire child is contained in parent
return CompareDomainName(parent, child) == CountLabel(parent)
2012-02-14 21:55:14 +00:00
}
// IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
// The checking is performed on the binary payload.
func IsMsg(buf []byte) error {
// Header
if len(buf) < 12 {
return errors.New("dns: bad message header")
}
// Header: Opcode
return nil
}
2012-02-12 10:37:52 +00:00
// IsFqdn checks if a domain name is fully qualified.
func IsFqdn(s string) bool {
2012-02-23 18:37:08 +00:00
l := len(s)
2012-02-14 21:55:14 +00:00
if l == 0 {
2013-06-23 20:41:49 +00:00
return false
}
2012-02-14 21:55:14 +00:00
return s[l-1] == '.'
}
// Fqdns return the fully qualified domain name from s.
2012-02-12 10:37:52 +00: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 07:48:59 +00:00
2013-09-10 13:13:10 +00:00
// Copied from the official Go code.
2012-07-04 07:48:59 +00:00
2013-05-05 18:30:44 +00:00
// ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
2014-01-12 10:43:59 +00:00
// address suitable for reverse DNS (PTR) record lookups or an error if it fails
2013-05-05 18:30:44 +00:00
// to parse the IP address.
2012-07-04 07:48:59 +00:00
func ReverseAddr(addr string) (arpa string, err error) {
ip := net.ParseIP(addr)
if ip == nil {
2013-06-20 06:27:28 +00:00
return "", &Error{err: "unrecognized address: " + addr}
2012-07-04 07:48:59 +00:00
}
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
}
2013-05-05 18:30:44 +00:00
// Must be IPv6
2012-07-04 07:48:59 +00:00
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
2013-05-05 18:30:44 +00:00
// Add it, in reverse, to the buffer
2012-07-04 07:48:59 +00:00
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, '.')
}
2013-05-05 18:30:44 +00:00
// Append "ip6.arpa." and return (buf already has the final .)
2012-07-04 07:48:59 +00:00
buf = append(buf, "ip6.arpa."...)
return string(buf), nil
}
// String returns the string representation for the type t.
func (t Type) String() string {
if t1, ok := TypeToString[uint16(t)]; ok {
return t1
2013-10-15 14:21:47 +00:00
}
2013-10-15 14:21:32 +00:00
return "TYPE" + strconv.Itoa(int(t))
}
// String returns the string representation for the class c.
func (c Class) String() string {
if c1, ok := ClassToString[uint16(c)]; ok {
return c1
}
2013-10-15 14:21:32 +00:00
return "CLASS" + strconv.Itoa(int(c))
}
// String returns the string representation for the name n.
func (n Name) String() string {
return sprintName(string(n))
}