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.
|
|
|
|
// Extended and bugfixes by Miek Gieben
|
|
|
|
|
2011-01-18 07:10:48 +11:00
|
|
|
// Package dns implements a full featured interface to the DNS.
|
2011-01-27 19:29:11 +11:00
|
|
|
// The package allows full control over what is send out to the DNS.
|
|
|
|
//
|
2011-02-25 01:29:36 +11:00
|
|
|
// Resource Records are native types. They are not stored in wire format.
|
2011-03-24 05:37:07 +11:00
|
|
|
// Basic usage pattern for creating a new Resource Record:
|
2010-12-30 23:42:52 +11:00
|
|
|
//
|
2011-01-27 19:29:11 +11:00
|
|
|
// r := new(RR_TXT)
|
|
|
|
// r.Hdr = RR_Header{Name: "a.miek.nl", Rrtype: TypeTXT, Class: ClassINET, Ttl: 3600}
|
|
|
|
// r.TXT = "This is the content of the TXT record"
|
|
|
|
//
|
2011-03-24 05:37:07 +11:00
|
|
|
// The package dns supports normal querying, incoming/outgoing Axfr/Ixfr, TSIG, EDNS0,
|
|
|
|
// dynamic updates, notifies and DNSSEC validation/signing.
|
|
|
|
//
|
|
|
|
// Basic use pattern for creating a resolver:
|
|
|
|
//
|
|
|
|
// res := new(Resolver)
|
|
|
|
// res.Servers = []string{"127.0.0.1"}
|
|
|
|
// m := new(Msg)
|
|
|
|
// m.MsgHdr.Recursion_desired = true
|
|
|
|
// m.Question = make([]Question, 1)
|
|
|
|
// m.Question[0] = Question{"miek.nl", TypeSOA, ClassINET}
|
|
|
|
// in, err := res.Query(m)
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Basic use pattern for creating an UDP DNS server:
|
|
|
|
//
|
|
|
|
// func handle(d *dns.Conn, i *dns.Msg) { /* handle request */ }
|
|
|
|
//
|
|
|
|
// func listen(addr string, e chan os.Error) {
|
|
|
|
// err := dns.ListenAndServeUDP(addr, handle)
|
|
|
|
// e <- err
|
|
|
|
// }
|
|
|
|
// err := make(chan os.Error)
|
|
|
|
// go listen("127.0.0.1:8053", err)
|
|
|
|
//
|
2010-12-30 23:42:52 +11:00
|
|
|
package dns
|
|
|
|
|
2011-03-21 02:52:10 +11:00
|
|
|
// ErrShortWrite is defined in io, use that!
|
|
|
|
|
2010-12-31 06:50:31 +11:00
|
|
|
import (
|
2011-03-21 02:52:10 +11:00
|
|
|
"os"
|
|
|
|
"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 (
|
|
|
|
Year68 = 2 << (32 - 1) // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
|
|
|
|
DefaultMsgSize = 4096 // A standard default for larger than 512 packets.
|
|
|
|
MaxMsgSize = 65536 // Largest possible DNS packet.
|
2011-03-24 05:37:07 +11:00
|
|
|
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-01-14 21:57:28 +11:00
|
|
|
Error string
|
|
|
|
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-01-03 21:29:04 +11:00
|
|
|
func (e *Error) String() string {
|
2011-01-14 21:57:28 +11:00
|
|
|
if e == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return e.Error
|
2011-01-02 04:47:38 +11:00
|
|
|
}
|
|
|
|
|
2011-03-24 05:37:07 +11:00
|
|
|
// A Conn is the lowest primative in this dns package.
|
2011-03-24 03:41:52 +11:00
|
|
|
// A Conn holds both the UDP and TCP connection, but only one
|
2011-03-24 05:37:07 +11:00
|
|
|
// can be active any given time.
|
2011-03-21 02:52:10 +11:00
|
|
|
type Conn struct {
|
|
|
|
// The current UDP connection.
|
|
|
|
UDP *net.UDPConn
|
2011-03-21 05:58:55 +11:00
|
|
|
|
2011-03-21 02:52:10 +11:00
|
|
|
// The current TCP connection.
|
|
|
|
TCP *net.TCPConn
|
2011-03-21 05:58:55 +11:00
|
|
|
|
2011-03-21 02:52:10 +11:00
|
|
|
// The remote side of the connection.
|
|
|
|
Addr net.Addr
|
|
|
|
|
2011-03-22 08:53:15 +11:00
|
|
|
// The remote port number of the connection.
|
|
|
|
Port int
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// If TSIG is used, this holds all the information.
|
2011-03-21 06:55:27 +11:00
|
|
|
Tsig *Tsig
|
2011-03-21 05:58:55 +11:00
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Timeout in sec before giving up on a connection.
|
2011-03-21 02:52:10 +11:00
|
|
|
Timeout int
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Number of attempts to try to Read/Write from/to a
|
|
|
|
// connection.
|
2011-03-21 02:52:10 +11:00
|
|
|
Attempts int
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Create a new buffer of the appropiate size. With
|
|
|
|
// TCP the buffer is 64K, with UDP the returned buffer
|
|
|
|
// has a length of 4K bytes.
|
2011-03-22 08:53:15 +11:00
|
|
|
func (d *Conn) NewBuffer() []byte {
|
|
|
|
if d.TCP != nil {
|
|
|
|
b := make([]byte, MaxMsgSize)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
if d.UDP != nil {
|
|
|
|
b := make([]byte, DefaultMsgSize)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// ReadMsg reads a dns message m from d.
|
|
|
|
// Any errors of the underlaying Read call are returned.
|
2011-03-23 19:50:38 +11:00
|
|
|
func (d *Conn) ReadMsg(m *Msg) os.Error {
|
|
|
|
in := d.NewBuffer()
|
|
|
|
n, err := d.Read(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
in = in[:n]
|
|
|
|
ok := m.Unpack(in)
|
|
|
|
if !ok {
|
|
|
|
return &Error{Error: "Failed to unpack"}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// WriteMsg writes dns message m to d.
|
|
|
|
// Any errors of the underlaying Write call are returned.
|
2011-03-23 19:50:38 +11:00
|
|
|
func (d *Conn) WriteMsg(m *Msg) os.Error {
|
|
|
|
out, ok := m.Pack()
|
|
|
|
if !ok {
|
|
|
|
return &Error{Error: "Failed to pack"}
|
|
|
|
}
|
|
|
|
n, err := d.Write(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(out) {
|
|
|
|
return &Error{Error: "Short write"}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2011-03-22 08:53:15 +11:00
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Read implements the standard Read interface:
|
|
|
|
// it reads from d. If there was an error
|
|
|
|
// reading that error is returned; otherwise err is nil.
|
2011-03-21 02:52:10 +11:00
|
|
|
func (d *Conn) Read(p []byte) (n int, err os.Error) {
|
|
|
|
if d.UDP != nil && d.TCP != nil {
|
|
|
|
return 0, &Error{Error: "UDP and TCP or both non-nil"}
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case d.UDP != nil:
|
2011-03-21 05:58:55 +11:00
|
|
|
var addr net.Addr
|
|
|
|
n, addr, err = d.UDP.ReadFromUDP(p)
|
2011-03-21 02:52:10 +11:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2011-03-21 05:58:55 +11:00
|
|
|
d.Addr = addr
|
2011-03-22 08:53:15 +11:00
|
|
|
d.Port = addr.(*net.UDPAddr).Port
|
2011-03-21 02:52:10 +11:00
|
|
|
case d.TCP != nil:
|
2011-03-21 05:58:55 +11:00
|
|
|
if len(p) < 1 {
|
|
|
|
return 0, &Error{Error: "Buffer too small to read"}
|
|
|
|
}
|
|
|
|
n, err = d.TCP.Read(p[0:2])
|
2011-03-21 02:52:10 +11:00
|
|
|
if err != nil || n != 2 {
|
|
|
|
return n, err
|
|
|
|
}
|
2011-03-22 08:53:15 +11:00
|
|
|
d.Addr = d.TCP.RemoteAddr()
|
|
|
|
d.Port = d.TCP.RemoteAddr().(*net.TCPAddr).Port
|
2011-03-21 05:58:55 +11:00
|
|
|
l, _ := unpackUint16(p[0:2], 0)
|
2011-03-21 02:52:10 +11:00
|
|
|
if l == 0 {
|
|
|
|
return 0, &Error{Error: "received nil msg length", Server: d.Addr}
|
|
|
|
}
|
|
|
|
if int(l) > len(p) {
|
|
|
|
return int(l), &Error{Error: "Buffer too small to read"}
|
|
|
|
}
|
|
|
|
n, err = d.TCP.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
i := n
|
|
|
|
for i < int(l) {
|
|
|
|
n, err = d.TCP.Read(p[i:])
|
|
|
|
if err != nil {
|
2011-03-22 00:20:46 +11:00
|
|
|
return i, err
|
2011-03-21 02:52:10 +11:00
|
|
|
}
|
|
|
|
i += n
|
|
|
|
}
|
2011-03-22 00:20:46 +11:00
|
|
|
n = i
|
2011-03-21 02:52:10 +11:00
|
|
|
}
|
2011-03-21 06:55:27 +11:00
|
|
|
if d.Tsig != nil {
|
|
|
|
// Check the TSIG that we should be read
|
2011-03-21 20:51:28 +11:00
|
|
|
_, err = d.Tsig.Verify(p)
|
2011-03-22 03:43:03 +11:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
2011-03-21 02:52:10 +11:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Write implements the standard Write interface:
|
|
|
|
// It write data to d. If there was an error writing
|
|
|
|
// that error is returned; otherwise err is nil
|
2011-03-21 02:52:10 +11:00
|
|
|
func (d *Conn) Write(p []byte) (n int, err os.Error) {
|
|
|
|
if d.UDP != nil && d.TCP != nil {
|
|
|
|
return 0, &Error{Error: "UDP and TCP or both non-nil"}
|
|
|
|
}
|
|
|
|
|
|
|
|
var attempts int
|
2011-03-21 20:51:28 +11:00
|
|
|
var q []byte
|
2011-03-21 02:52:10 +11:00
|
|
|
if d.Attempts == 0 {
|
|
|
|
attempts = 1
|
|
|
|
} else {
|
|
|
|
attempts = d.Attempts
|
|
|
|
}
|
|
|
|
d.SetTimeout()
|
2011-03-21 20:51:28 +11:00
|
|
|
if d.Tsig != nil {
|
|
|
|
// Create a new buffer with the TSIG added.
|
2011-03-22 03:43:03 +11:00
|
|
|
q, err = d.Tsig.Generate(p)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2011-03-21 20:51:28 +11:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
q = p
|
|
|
|
}
|
|
|
|
|
2011-03-21 02:52:10 +11:00
|
|
|
switch {
|
|
|
|
case d.UDP != nil:
|
|
|
|
for a := 0; a < attempts; a++ {
|
2011-03-21 20:51:28 +11:00
|
|
|
n, err = d.UDP.WriteTo(q, d.Addr)
|
2011-03-21 02:52:10 +11:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(net.Error); ok && e.Timeout() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case d.TCP != nil:
|
|
|
|
for a := 0; a < attempts; a++ {
|
|
|
|
l := make([]byte, 2)
|
2011-03-21 20:51:28 +11:00
|
|
|
l[0], l[1] = packUint16(uint16(len(q)))
|
2011-03-21 02:52:10 +11:00
|
|
|
n, err = d.TCP.Write(l)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(net.Error); ok && e.Timeout() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if n != 2 {
|
|
|
|
return n, &Error{Error: "Write failure"}
|
|
|
|
}
|
2011-03-21 20:51:28 +11:00
|
|
|
n, err = d.TCP.Write(q)
|
2011-03-21 02:52:10 +11:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(net.Error); ok && e.Timeout() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2011-03-22 00:20:46 +11:00
|
|
|
i := n
|
|
|
|
if i < len(q) {
|
|
|
|
n, err = d.TCP.Write(q)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(net.Error); ok && e.Timeout() {
|
|
|
|
// We are half way in our write...
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
i += n
|
|
|
|
}
|
|
|
|
n = i
|
2011-03-21 02:52:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Close closes the connection in d. Possible
|
|
|
|
// errors are returned in err; otherwise it is nil.
|
2011-03-21 02:52:10 +11:00
|
|
|
func (d *Conn) Close() (err os.Error) {
|
|
|
|
if d.UDP != nil && d.TCP != nil {
|
|
|
|
return &Error{Error: "UDP and TCP or both non-nil"}
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case d.UDP != nil:
|
|
|
|
err = d.UDP.Close()
|
|
|
|
case d.TCP != nil:
|
|
|
|
err = d.TCP.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// SetTimeout sets the timeout of the socket
|
|
|
|
// that is contained in d.
|
2011-03-21 02:52:10 +11:00
|
|
|
func (d *Conn) SetTimeout() (err os.Error) {
|
|
|
|
var sec int64
|
|
|
|
if d.UDP != nil && d.TCP != nil {
|
|
|
|
return &Error{Error: "UDP and TCP or both non-nil"}
|
|
|
|
}
|
|
|
|
sec = int64(d.Timeout)
|
|
|
|
if sec == 0 {
|
|
|
|
sec = 1
|
|
|
|
}
|
|
|
|
if d.UDP != nil {
|
|
|
|
err = d.TCP.SetTimeout(sec * 1e9)
|
|
|
|
}
|
|
|
|
if d.TCP != nil {
|
|
|
|
err = d.TCP.SetTimeout(sec * 1e9)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-24 03:41:52 +11:00
|
|
|
// Exchange combines a Write and a Read.
|
2011-03-24 05:37:07 +11:00
|
|
|
// First the request is written to d and then it waits
|
2011-03-24 03:41:52 +11:00
|
|
|
// for a reply with Read.
|
|
|
|
// If nosend is true, the write is skipped.
|
2011-03-21 02:52:10 +11:00
|
|
|
func (d *Conn) Exchange(request []byte, nosend bool) (reply []byte, err os.Error) {
|
|
|
|
var n int
|
2011-03-21 05:58:55 +11:00
|
|
|
if !nosend {
|
|
|
|
n, err = d.Write(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Layer violation to save memory. Its okay then...
|
2011-03-22 08:53:15 +11:00
|
|
|
reply = d.NewBuffer()
|
2011-03-21 02:52:10 +11:00
|
|
|
n, err = d.Read(reply)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reply = reply[:n]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-12-30 23:42:52 +11:00
|
|
|
type RR interface {
|
2010-12-31 06:50:31 +11:00
|
|
|
Header() *RR_Header
|
|
|
|
String() string
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// An RRset is a slice of RRs.
|
|
|
|
type RRset []RR
|
|
|
|
|
|
|
|
func (r RRset) Len() int { return len(r) }
|
|
|
|
func (r RRset) Less(i, j int) bool { return r[i].Header().Name < r[j].Header().Name }
|
|
|
|
func (r RRset) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
|
2011-01-18 07:10:48 +11:00
|
|
|
// Check if the RRset is RFC 2181 compliant
|
2011-01-17 06:07:17 +11:00
|
|
|
func (r RRset) Ok() bool {
|
|
|
|
ttl := r[0].Header().Ttl
|
|
|
|
name := r[0].Header().Name
|
|
|
|
class := r[0].Header().Class
|
|
|
|
for _, rr := range r[1:] {
|
2011-01-19 05:14:26 +11:00
|
|
|
if rr.Header().Ttl != ttl {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if rr.Header().Name != name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if rr.Header().Class != class {
|
|
|
|
return false
|
|
|
|
}
|
2011-01-17 06:07:17 +11:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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 {
|
2010-12-31 06:50:31 +11:00
|
|
|
Name string "domain-name"
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
s = s + strconv.Itoa(int(h.Ttl)) + "\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-02-25 02:13:23 +11:00
|
|
|
// Return the number of labels in a domain name.
|
2011-01-15 04:55:18 +11:00
|
|
|
func LabelCount(a string) (c uint8) {
|
2011-02-25 02:22:14 +11:00
|
|
|
// walk the string and count the dots
|
|
|
|
// except when it is escaped
|
|
|
|
esc := false
|
2011-01-14 21:57:28 +11:00
|
|
|
for _, v := range a {
|
2011-02-25 02:22:14 +11:00
|
|
|
switch v {
|
|
|
|
case '.':
|
|
|
|
if esc {
|
|
|
|
esc = !esc
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c++
|
|
|
|
case '\\':
|
|
|
|
esc = true
|
|
|
|
}
|
2010-12-31 06:50:31 +11:00
|
|
|
}
|
2011-01-14 21:57:28 +11:00
|
|
|
return
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|