dns/dns.go

227 lines
5.9 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Extensions of the original work are copyright (c) 2011 Miek Gieben
// Package dns implements a full featured interface to the Domain Name System.
2012-05-02 06:04:33 +00:00
// Server- and client-side programming is supported.
2012-02-12 10:17:52 +00:00
// The package allows complete control over what is send out to the DNS. The package
2012-02-19 21:38:13 +00:00
// API follows the less-is-more principle, by presenting a small, clean interface.
2012-05-02 06:00:07 +00:00
//
2013-05-05 18:30:44 +00:00
// The package dns supports (asynchronous) querying/replying, incoming/outgoing AXFR/IXFR,
2012-05-02 05:57:35 +00:00
// TSIG, EDNS0, dynamic updates, notifies and DNSSEC validation/signing.
2012-09-01 09:35:00 +00:00
// Note that domain names MUST be fully qualified, before sending them, unqualified
// names in a message will result in a packing failure.
2011-01-27 08:29:11 +00:00
//
2011-03-24 08:16:33 +00:00
// Resource records are native types. They are not stored in wire format.
// Basic usage pattern for creating a new resource record:
//
2012-12-09 19:20:16 +00:00
// r := new(dns.MX)
2012-05-08 12:17:17 +00:00
// r.Hdr = dns.RR_Header{Name: "miek.nl.", Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 3600}
2012-02-12 10:17:52 +00:00
// r.Pref = 10
// r.Mx = "mx.miek.nl."
//
// Or directly from a string:
//
2012-05-08 12:17:17 +00:00
// mx, err := dns.NewRR("miek.nl. 3600 IN MX 10 mx.miek.nl.")
2012-02-15 12:10:02 +00:00
//
2012-02-12 10:17:52 +00:00
// Or when the default TTL (3600) and class (IN) suit you:
//
2012-05-08 12:17:17 +00:00
// mx, err := dns.NewRR("miek.nl. MX 10 mx.miek.nl.")
2012-02-15 12:10:02 +00:00
//
// Or even:
//
2012-05-08 12:17:17 +00:00
// mx, err := dns.NewRR("$ORIGIN nl.\nmiek 1H IN MX 10 mx.miek")
2011-03-23 18:37:07 +00:00
//
2012-05-08 11:39:57 +00:00
// In the DNS messages are exchanged, these messages contain resource
// records (sets). Use pattern for creating a message:
//
2012-05-08 12:17:17 +00:00
// m := dns.new(Msg)
// m.SetQuestion("miek.nl.", dns.TypeMX)
//
2012-08-31 20:23:49 +00:00
// Or when not certain if the domain name is fully qualified:
2012-08-31 19:00:58 +00:00
//
// m.SetQuestion(dns.Fqdn("miek.nl"), dns.TypeMX)
//
// The message m is now a message with the question section set to ask
2011-09-11 18:47:25 +00:00
// the MX records for the miek.nl. zone.
//
// The following is slightly more verbose, but more flexible:
//
2012-05-08 12:17:17 +00:00
// m1 := new(dns.Msg)
2012-09-05 14:30:04 +00:00
// m1.Id = Id()
// m1.RecursionDesired = true
// m1.Question = make([]Question, 1)
2012-05-08 12:17:17 +00:00
// m1.Question[0] = dns.Question{"miek.nl.", dns.TypeMX, dns.ClassINET}
//
// After creating a message it can be send.
2012-03-03 17:07:36 +00:00
// Basic use pattern for synchronous querying the DNS at a
// server configured on 127.0.0.1 and port 53:
//
2012-05-26 08:28:32 +00:00
// c := new(Client)
2012-11-19 15:22:54 +00:00
// in, rtt, err := c.Exchange(m1, "127.0.0.1:53")
2011-03-23 18:37:07 +00:00
//
2012-12-01 21:54:35 +00:00
// For asynchronous queries it is easy to wrap Exchange() in a goroutine.
2012-08-24 06:56:28 +00:00
//
2013-04-17 07:47:07 +00:00
// A dns message consists out of four sections.
2012-08-24 07:35:33 +00:00
// The question section: in.Question, the answer section: in.Answer,
2012-08-24 06:57:33 +00:00
// the authority section: in.Ns and the additional section: in.Extra.
2012-08-24 06:56:28 +00:00
//
// Each of these sections (except the Question section) contain a []RR. Basic
2013-05-05 18:30:44 +00:00
// use pattern for accessing the rdata of a TXT RR as the first RR in
2012-08-24 06:56:28 +00:00
// the Answer section:
//
2012-12-09 19:20:16 +00:00
// if t, ok := in.Answer[0].(*TXT); ok {
2012-08-24 06:56:28 +00:00
// // do something with t.Txt
// }
package dns
2010-12-30 19:50:31 +00:00
import (
"net"
2010-12-30 19:50:31 +00:00
"strconv"
)
2011-02-28 09:42:03 +00:00
const (
2012-08-17 06:29:45 +00:00
year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
DefaultMsgSize = 4096 // Standard default for larger than 512 packets.
2012-08-17 06:31:38 +00:00
udpMsgSize = 512 // Default buffer size for servers receiving UDP packets.
MaxMsgSize = 65536 // Largest possible DNS packet.
2012-08-17 06:31:38 +00:00
defaultTtl = 3600 // Default TTL.
2011-02-28 09:42:03 +00:00
)
2011-01-17 20:10:48 +00:00
// Error represents a DNS error
type Error struct {
2011-11-02 22:06:54 +00:00
Err string
Name string
Server net.Addr
Timeout bool
}
2011-11-02 22:06:54 +00:00
func (e *Error) Error() string {
if e == nil {
2012-04-18 12:06:10 +00:00
return "dns: <nil>"
}
2012-01-22 09:52:06 +00:00
if e.Name == "" {
2012-10-09 20:16:28 +00:00
return "dns: " + e.Err
2012-01-22 09:52:06 +00:00
}
2012-10-09 18:25:25 +00:00
return "dns: " + e.Name + ": " + e.Err
2012-01-22 09:52:06 +00:00
}
2012-02-15 22:34:41 +00:00
// An RR represents a resource record.
type RR interface {
2012-02-19 17:36:59 +00:00
// Header returns the header of an resource record. The header contains
// everything up to the rdata.
2010-12-30 19:50:31 +00:00
Header() *RR_Header
2012-02-19 17:36:59 +00:00
// String returns the text representation of the resource record.
2010-12-30 19:50:31 +00:00
String() string
// copy returns a copy of the RR
copy() RR
// len returns the length (in octects) of the uncompressed RR in wire format.
len() int
}
// DNS resource records.
2012-08-26 20:40:00 +00:00
// There are many types of RRs,
// but they all share the same header.
type RR_Header struct {
Name string `dns:"cdomain-name"`
2010-12-30 19:50:31 +00:00
Rrtype uint16
Class uint16
Ttl uint32
Rdlength uint16 // length of data after header
}
2012-11-30 12:25:01 +00:00
func (h *RR_Header) Header() *RR_Header { return h }
// Just to imlement the RR interface
func (h *RR_Header) copy() RR { return nil }
func (h *RR_Header) copyHeader() *RR_Header {
r := new(RR_Header)
r.Name = h.Name
r.Rrtype = h.Rrtype
r.Class = h.Class
r.Ttl = h.Ttl
r.Rdlength = h.Rdlength
return r
}
func (h *RR_Header) String() string {
2010-12-30 19:50:31 +00:00
var s string
2010-12-30 19:50:31 +00:00
if h.Rrtype == TypeOPT {
s = ";"
// and maybe other things
}
2010-12-30 19:50:31 +00:00
if len(h.Name) == 0 {
s += ".\t"
} else {
s += h.Name + "\t"
}
2012-02-19 17:36:59 +00:00
s = s + strconv.FormatInt(int64(h.Ttl), 10) + "\t"
2011-02-21 14:44:42 +00:00
if _, ok := ClassToString[h.Class]; ok {
s += ClassToString[h.Class] + "\t"
2011-02-24 15:22:14 +00:00
} else {
s += "CLASS" + strconv.Itoa(int(h.Class)) + "\t"
}
if _, ok := TypeToString[h.Rrtype]; ok {
s += TypeToString[h.Rrtype] + "\t"
2011-02-24 15:22:14 +00:00
} else {
s += "TYPE" + strconv.Itoa(int(h.Rrtype)) + "\t"
}
2010-12-30 19:50:31 +00:00
return s
}
2011-07-31 07:53:54 +00:00
func (h *RR_Header) len() int {
2012-01-12 22:17:34 +00:00
l := len(h.Name) + 1
l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
return l
}
2012-08-05 02:40:09 +00:00
// find best matching pattern for zone
2011-07-31 07:53:54 +00:00
func zoneMatch(pattern, zone string) (ok bool) {
if len(pattern) == 0 {
return
}
if len(zone) == 0 {
zone = "."
}
2012-08-05 02:40:09 +00:00
// pattern = Fqdn(pattern) // should already be a fqdn
zone = Fqdn(zone)
2011-07-31 07:53:54 +00:00
i := 0
for {
ok = pattern[len(pattern)-1-i] == zone[len(zone)-1-i]
i++
if !ok {
break
}
if len(pattern)-1-i < 0 || len(zone)-1-i < 0 {
break
}
}
return
}
// ToRFC3597 converts a known RR to the unknown RR representation
2013-01-30 14:56:29 +00:00
// from RFC 3597.
2013-01-31 08:52:34 +00:00
func (rr *RFC3597) ToRFC3597(r RR) error {
buf := make([]byte, r.len()*2)
off, err := PackStruct(r, buf, 0)
if err != nil {
2013-01-31 08:52:34 +00:00
return err
}
buf = buf[:off]
rawSetRdlength(buf, 0, off)
2013-01-31 08:52:34 +00:00
_, err = UnpackStruct(rr, buf, 0)
if err != nil {
2013-01-31 08:52:34 +00:00
return err
}
2013-01-31 08:52:34 +00:00
return nil
}