dns/dns.go

246 lines
5.4 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.
2011-03-29 20:17:43 +11:00
// Extended and bugfixes by Miek Gieben.
2011-09-12 04:47:25 +10:00
// DOMAIN NAME SYSTEM
//
// Package dns implements a full featured interface to the Domain Name System.
2011-03-25 21:19:35 +11:00
// The package allows complete control over what is send out to the DNS.
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:
//
// r := new(RR_TXT)
2011-12-10 08:45:13 +11:00
// r.Hdr = RR_Header{Name: "a.miek.nl.", Rrtype: TypeTXT, Class: ClassINET, Ttl: 3600}
// r.TXT = "This is the content of the TXT record"
//
// Or directly from a string:
//
2011-12-16 08:40:07 +11:00
// mx := NewRR("miek.nl. IN MX 10 mx.miek.nl.")
2011-01-27 19:29:11 +11:00
//
2011-03-29 19:41:57 +11:00
// The package dns supports (async) querying/replying, incoming/outgoing Axfr/Ixfr,
// TSIG, EDNS0, dynamic updates, notifies and DNSSEC validation/signing.
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-04-18 06:14:26 +10:00
// m := new(Msg)
// m.SetQuestion("miek.nl.", TypeMX)
//
2011-09-12 04:47:25 +10:00
// The message m is now a messages with the Question section set to ask
// the MX records for the miek.nl. zone.
//
// This is slightly more verbose, but more flexible:
//
// 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}
//
// After creating a message it can be send.
2011-09-12 04:47:25 +10:00
// Basic use pattern for synchronous querying the DNS. We are
// sending the message 'm' to the server 127.0.0.1 on port 53 and
// waiting for the reply.
//
2011-04-18 06:14:26 +10:00
// c := dns.NewClient()
2011-07-05 05:32:39 +10:00
// // c.Net = "tcp" // If you want to use TCP
2011-04-18 06:14:26 +10:00
// in := c.Exchange(m, "127.0.0.1:53")
2011-03-24 05:37:07 +11:00
//
2011-09-12 04:47:25 +10:00
// An asynchronous query ... TODO(mg)
package dns
2010-12-31 06:50:31 +11:00
import (
"net"
2010-12-31 06:50:31 +11:00
"strconv"
)
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.
DefaultTtl = 3600 // Default TTL.
2011-02-28 20:42:03 +11:00
)
2011-01-18 07:10:48 +11:00
// Error represents a DNS error
type Error struct {
2011-11-03 09:06:54 +11:00
Err string
Name string
Server net.Addr
Timeout bool
}
2011-11-03 09:06:54 +11:00
func (e *Error) Error() string {
if e == nil {
return "<nil>"
}
2011-11-03 09:06:54 +11:00
return e.Err
}
type RR interface {
2010-12-31 06:50:31 +11:00
Header() *RR_Header
String() string
}
// An RRset is a slice of RRs.
type RRset []RR
func NewRRset() RRset {
2011-07-31 17:53:54 +10:00
s := make([]RR, 0)
return s
}
func (s RRset) String() string {
2011-07-31 17:53:54 +10:00
str := ""
for _, r := range s {
str += r.String() + "\n"
}
return str
}
2011-09-09 03:28:00 +10:00
// Pop removes the last pushed RR from the RRset. Returns nil
// when there is nothing to remove.
func (s *RRset) Pop() RR {
2011-07-31 17:53:54 +10:00
if len(*s) == 0 {
return nil
}
// Pop and remove the entry
r := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return r
}
2011-09-09 03:28:00 +10:00
// Push pushes the RR r to the RRset.
func (s *RRset) Push(r RR) bool {
if len(*s) == 0 {
2011-07-31 17:53:54 +10:00
*s = append(*s, r)
return true
}
// For RRSIGs this is not true (RFC???)
// Don't make it a failure if this happens
// if (*s)[0].Header().Ttl != r.Header().Ttl {
// return false
// }
if (*s)[0].Header().Name != r.Header().Name {
return false
}
if (*s)[0].Header().Class != r.Header().Class {
return false
}
*s = append(*s, r)
return true
}
2011-09-09 03:28:00 +10:00
// Ok checks if the RRSet is RFC 2181 compliant.
func (s RRset) Ok() bool {
ttl := s[0].Header().Ttl
name := s[0].Header().Name
class := s[0].Header().Class
for _, rr := range s[1:] {
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
}
// Exchange is used in communicating with the resolver.
type Exchange struct {
2011-11-03 09:06:54 +11:00
Request *Msg // The question sent.
Reply *Msg // The answer to the question that was sent.
2011-12-16 20:26:32 +11:00
Error error // If something when wrong, this contains the error.
}
// 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
}
func (h *RR_Header) Header() *RR_Header {
2010-12-31 06:50:31 +11:00
return h
}
func (h *RR_Header) String() string {
2010-12-31 06:50:31 +11:00
var s string
2010-12-31 06:50:31 +11:00
if h.Rrtype == TypeOPT {
s = ";"
// and maybe other things
}
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
}
2011-07-31 17:53:54 +10:00
func zoneMatch(pattern, zone string) (ok bool) {
if len(pattern) == 0 {
return
}
if pattern[len(pattern)-1] != '.' {
pattern += "."
}
if zone[len(zone)-1] != '.' {
zone += "."
}
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
}
2011-09-09 03:28:00 +10:00
// DnameLength returns the length of a packed dname.
func DomainNameLength(s string) int { // TODO better name
2011-11-03 09:06:54 +11:00
// Special case for '.'
if s == "." {
return 1
}
2011-09-26 17:11:14 +10:00
// Add trailing dot to canonicalize name.
if n := len(s); n == 0 || s[n-1] != '.' {
return n + 1
} else {
return n + 1
}
panic("not reached")
return 0
}