dns/dns.go

156 lines
3.5 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-01-18 07:10:48 +11:00
// Package dns implements a full featured interface to the DNS.
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:
//
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-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.", dns.TypeMX)
2011-03-30 23:54:43 +11:00
// // Set the desired options.
2011-04-18 06:14:26 +10:00
// m.MsgHdr.RecursionDesired = true
//
// Basic use pattern for synchronize querying of the DNS:
//
2011-04-18 06:14:26 +10:00
// c := dns.NewClient()
// in := c.Exchange(m, "127.0.0.1:53")
2011-03-24 05:37:07 +11:00
//
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.
2011-03-26 03:47:35 +11:00
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 {
Error string
Name string
Server net.Addr
Timeout bool
}
func (e *Error) String() string {
if e == nil {
return "<nil>"
}
return e.Error
}
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 (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:] {
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
}
// 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-02-25 02:13:23 +11:00
// Return the number of labels in a domain name.
2011-03-31 00:35:49 +11:00
// Need to add these kind of function in a structured way. TODO(mg)
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
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
}
return
}