2010-12-30 23:42:52 +11:00
|
|
|
// 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.
|
2012-02-12 21:17:52 +11:00
|
|
|
// Extended and bugfixes by Miek Gieben. Copyright 2010-2012.
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2011-09-12 04:47:25 +10:00
|
|
|
// DOMAIN NAME SYSTEM
|
|
|
|
//
|
2011-07-24 07:15:40 +10:00
|
|
|
// Package dns implements a full featured interface to the Domain Name System.
|
2012-02-12 21:17:52 +11:00
|
|
|
// The package allows complete control over what is send out to the DNS. The package
|
2012-02-20 08:38:13 +11:00
|
|
|
// API follows the less-is-more principle, by presenting a small, clean interface.
|
2012-05-02 16:00:07 +10:00
|
|
|
//
|
2012-05-02 15:57:35 +10:00
|
|
|
// The package dns supports (async) querying/replying, incoming/outgoing Axfr/Ixfr,
|
|
|
|
// TSIG, EDNS0, dynamic updates, notifies and DNSSEC validation/signing.
|
|
|
|
// Note that domain names MUST be full qualified, before sending them.
|
2011-01-27 19:29:11 +11:00
|
|
|
//
|
2011-03-24 19:16:33 +11:00
|
|
|
// Resource records are native types. They are not stored in wire format.
|
|
|
|
// Basic usage pattern for creating a new resource record:
|
2010-12-30 23:42:52 +11:00
|
|
|
//
|
2011-07-24 07:15:40 +10:00
|
|
|
// r := new(RR_TXT)
|
2012-02-12 21:17:52 +11:00
|
|
|
// r.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeMX, Class: ClassINET, Ttl: 3600}
|
|
|
|
// r.Pref = 10
|
|
|
|
// r.Mx = "mx.miek.nl."
|
2011-07-24 07:15:40 +10:00
|
|
|
//
|
|
|
|
// Or directly from a string:
|
|
|
|
//
|
2012-02-29 06:43:08 +11:00
|
|
|
// mx, err := NewRR("miek.nl. 3600 IN MX 10 mx.miek.nl.")
|
2012-02-15 23:10:02 +11:00
|
|
|
//
|
2012-02-12 21:17:52 +11:00
|
|
|
// Or when the default TTL (3600) and class (IN) suit you:
|
|
|
|
//
|
2012-02-29 06:43:08 +11:00
|
|
|
// mx, err := NewRR("miek.nl. MX 10 mx.miek.nl.")
|
2012-02-15 23:10:02 +11:00
|
|
|
//
|
|
|
|
// Or even:
|
|
|
|
//
|
2012-02-29 06:43:08 +11:00
|
|
|
// mx, err := NewRR("$ORIGIN nl.\nmiek 1H IN MX 10 mx.miek")
|
2011-01-27 19:29:11 +11:00
|
|
|
//
|
2011-03-24 05:37:07 +11:00
|
|
|
//
|
2011-03-29 20:17:43 +11:00
|
|
|
// In the DNS messages are exchanged. Use pattern for creating one:
|
2011-03-29 19:15:42 +11:00
|
|
|
//
|
2011-04-18 06:14:26 +10:00
|
|
|
// m := new(Msg)
|
2011-07-05 05:28:30 +10:00
|
|
|
// m.SetQuestion("miek.nl.", TypeMX)
|
2011-03-29 19:15:42 +11:00
|
|
|
//
|
2011-12-17 05:28:35 +11:00
|
|
|
// The message m is now a message with the question section set to ask
|
2011-09-12 04:47:25 +10:00
|
|
|
// the MX records for the miek.nl. zone.
|
|
|
|
//
|
2011-12-17 05:28:35 +11:00
|
|
|
// The following is slightly more verbose, but more flexible:
|
2011-07-05 05:28:30 +10:00
|
|
|
//
|
|
|
|
// m1 := new(Msg)
|
|
|
|
// m1.MsgHdr.Id = Id()
|
|
|
|
// m1.MsgHdr.RecursionDesired = false
|
|
|
|
// m1.Question = make([]Question, 1)
|
2011-12-10 08:45:13 +11:00
|
|
|
// m1.Question[0] = Question{"miek.nl.", TypeMX, ClassINET}
|
2011-07-05 05:28:30 +10:00
|
|
|
//
|
2011-07-24 07:15:40 +10:00
|
|
|
// After creating a message it can be send.
|
2012-03-04 04:07:36 +11:00
|
|
|
// Basic use pattern for synchronous querying the DNS at a
|
|
|
|
// server configured on 127.0.0.1 and port 53:
|
2011-03-29 19:15:42 +11:00
|
|
|
//
|
2012-01-09 01:33:15 +11:00
|
|
|
// c := NewClient()
|
2012-03-04 04:07:36 +11:00
|
|
|
// in := c.Exchange(m1, "127.0.0.1:53")
|
2011-03-24 05:37:07 +11:00
|
|
|
//
|
2012-01-09 01:33:15 +11:00
|
|
|
// An asynchronous query is also possible, setting up is more elaborate then
|
|
|
|
// a synchronous query. The Basic use pattern is:
|
|
|
|
//
|
2012-03-27 20:02:30 +11:00
|
|
|
// HandleQuery(".", handler)
|
2012-01-09 01:33:15 +11:00
|
|
|
// ListenAndQuery(nil, nil)
|
|
|
|
// c.Do(m1, "127.0.0.1:53")
|
|
|
|
// // Do something else
|
|
|
|
// r := <- DefaultReplyChan
|
|
|
|
// // r.Reply is the answer
|
|
|
|
// // r.Request is the original request
|
2012-03-09 06:47:45 +11:00
|
|
|
// // r.Error is the error (if any)
|
2010-12-30 23:42:52 +11:00
|
|
|
package dns
|
|
|
|
|
2010-12-31 06:50:31 +11:00
|
|
|
import (
|
2011-03-21 02:52:10 +11:00
|
|
|
"net"
|
2010-12-31 06:50:31 +11:00
|
|
|
"strconv"
|
|
|
|
)
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2011-02-28 20:42:03 +11:00
|
|
|
const (
|
2012-02-05 21:33:55 +11:00
|
|
|
Year68 = 1 << 32 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
|
|
|
|
DefaultMsgSize = 4096 // Standard default for larger than 512 packets.
|
|
|
|
UDPMsgSize = 512 // Default buffer size for servers receiving UDP packets.
|
|
|
|
MaxMsgSize = 65536 // Largest possible DNS packet.
|
|
|
|
DefaultTtl = 3600 // Default TTL.
|
2011-02-28 20:42:03 +11:00
|
|
|
)
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2011-01-18 07:10:48 +11:00
|
|
|
// Error represents a DNS error
|
2011-01-03 21:29:04 +11:00
|
|
|
type Error struct {
|
2011-11-03 09:06:54 +11:00
|
|
|
Err string
|
2011-01-14 21:57:28 +11:00
|
|
|
Name string
|
2011-03-21 02:52:10 +11:00
|
|
|
Server net.Addr
|
2011-01-14 21:57:28 +11:00
|
|
|
Timeout bool
|
2011-01-02 04:47:38 +11:00
|
|
|
}
|
|
|
|
|
2011-11-03 09:06:54 +11:00
|
|
|
func (e *Error) Error() string {
|
2011-01-14 21:57:28 +11:00
|
|
|
if e == nil {
|
2012-04-18 22:06:10 +10:00
|
|
|
return "dns: <nil>"
|
2011-01-14 21:57:28 +11:00
|
|
|
}
|
2012-01-22 20:52:06 +11:00
|
|
|
if e.Name == "" {
|
|
|
|
return e.Err
|
|
|
|
}
|
|
|
|
return e.Name + ": " + e.Err
|
|
|
|
|
2011-01-02 04:47:38 +11:00
|
|
|
}
|
|
|
|
|
2012-02-16 09:34:41 +11:00
|
|
|
// An RR represents a resource record.
|
2010-12-30 23:42:52 +11:00
|
|
|
type RR interface {
|
2012-02-20 04:36:59 +11:00
|
|
|
// Header returns the header of an resource record. The header contains
|
|
|
|
// everything up to the rdata.
|
2010-12-31 06:50:31 +11:00
|
|
|
Header() *RR_Header
|
2012-02-20 04:36:59 +11:00
|
|
|
// String returns the text representation of the resource record.
|
2010-12-31 06:50:31 +11:00
|
|
|
String() string
|
2012-02-20 04:36:59 +11:00
|
|
|
// Len returns the length (in octects) of the uncompressed RR in wire format.
|
2012-01-10 20:43:28 +11:00
|
|
|
Len() int
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
2011-09-10 22:26:08 +10:00
|
|
|
// Exchange is used in communicating with the resolver.
|
|
|
|
type Exchange struct {
|
2012-02-16 09:34:41 +11:00
|
|
|
Request *Msg // the question sent
|
|
|
|
Reply *Msg // the answer to the question that was sent
|
|
|
|
Error error // if something went wrong, this contains the error
|
2011-09-10 22:26:08 +10:00
|
|
|
}
|
|
|
|
|
2010-12-30 23:42:52 +11:00
|
|
|
// DNS resource records.
|
|
|
|
// There are many types of messages,
|
|
|
|
// but they all share the same header.
|
|
|
|
type RR_Header struct {
|
2012-04-30 05:55:29 +10:00
|
|
|
Name string `dns:"cdomain-name"`
|
2010-12-31 06:50:31 +11:00
|
|
|
Rrtype uint16
|
|
|
|
Class uint16
|
|
|
|
Ttl uint32
|
|
|
|
Rdlength uint16 // length of data after header
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RR_Header) Header() *RR_Header {
|
2010-12-31 06:50:31 +11:00
|
|
|
return h
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RR_Header) String() string {
|
2010-12-31 06:50:31 +11:00
|
|
|
var s string
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2010-12-31 06:50:31 +11:00
|
|
|
if h.Rrtype == TypeOPT {
|
|
|
|
s = ";"
|
|
|
|
// and maybe other things
|
|
|
|
}
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2010-12-31 06:50:31 +11:00
|
|
|
if len(h.Name) == 0 {
|
|
|
|
s += ".\t"
|
|
|
|
} else {
|
|
|
|
s += h.Name + "\t"
|
|
|
|
}
|
2012-02-20 04:36:59 +11:00
|
|
|
s = s + strconv.FormatInt(int64(h.Ttl), 10) + "\t"
|
2011-02-22 01:44:42 +11:00
|
|
|
|
2011-02-25 02:22:14 +11:00
|
|
|
if _, ok := Class_str[h.Class]; ok {
|
|
|
|
s += Class_str[h.Class] + "\t"
|
|
|
|
} else {
|
|
|
|
s += "CLASS" + strconv.Itoa(int(h.Class)) + "\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := Rr_str[h.Rrtype]; ok {
|
|
|
|
s += Rr_str[h.Rrtype] + "\t"
|
|
|
|
} else {
|
|
|
|
s += "TYPE" + strconv.Itoa(int(h.Rrtype)) + "\t"
|
|
|
|
}
|
2010-12-31 06:50:31 +11:00
|
|
|
return s
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
2011-07-31 17:53:54 +10:00
|
|
|
|
2012-01-10 20:43:28 +11:00
|
|
|
func (h *RR_Header) Len() int {
|
2012-01-13 09:17:34 +11:00
|
|
|
l := len(h.Name) + 1
|
|
|
|
l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
|
|
|
|
return l
|
2012-01-10 20:43:28 +11:00
|
|
|
}
|
|
|
|
|
2012-02-23 22:46:39 +11:00
|
|
|
// Create a copy of the header
|
|
|
|
|
|
|
|
func (h *RR_Header) Copy() *RR_Header {
|
2012-02-24 05:37:08 +11:00
|
|
|
h1 := new(RR_Header)
|
|
|
|
h1.Name = h.Name
|
|
|
|
h1.Rrtype = h.Rrtype
|
|
|
|
h1.Class = h.Class
|
|
|
|
h1.Ttl = h.Ttl
|
|
|
|
h1.Rdlength = h.Rdlength
|
|
|
|
return h1
|
2012-02-23 22:46:39 +11:00
|
|
|
}
|
|
|
|
|
2011-07-31 17:53:54 +10:00
|
|
|
func zoneMatch(pattern, zone string) (ok bool) {
|
|
|
|
if len(pattern) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2012-01-10 20:43:28 +11:00
|
|
|
if len(zone) == 0 {
|
|
|
|
zone = "."
|
|
|
|
}
|
|
|
|
pattern = Fqdn(pattern)
|
|
|
|
zone = Fqdn(zone)
|
2011-07-31 17:53:54 +10: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
|
|
|
|
}
|