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
|
|
|
|
|
|
|
|
// Package dns implements a full featured interface to the DNS.
|
|
|
|
// Supported RFCs and features include:
|
2011-01-09 05:10:47 +11:00
|
|
|
// * 1034/1035 - DNS standard
|
2010-12-31 00:13:28 +11:00
|
|
|
// * 1982 - Serial Arithmetic
|
2010-12-31 06:50:31 +11:00
|
|
|
// * 1876 - LOC record (incomplete)
|
2011-01-01 00:11:52 +11:00
|
|
|
// * 1995 - IXFR
|
2011-01-02 06:01:49 +11:00
|
|
|
// * 1996 - DNS notify
|
2011-01-14 21:57:28 +11:00
|
|
|
// * 2181 - RRset definition
|
2011-01-09 05:10:47 +11:00
|
|
|
// * 2537 - RSAMD5 DNS keys
|
2011-01-14 21:57:28 +11:00
|
|
|
// * 2065 - DNSSEC (updated in later RFCs)
|
2010-12-30 23:42:52 +11:00
|
|
|
// * 2671 - EDNS
|
2011-01-17 20:30:20 +11:00
|
|
|
// * 2782 - SRV record
|
2011-01-01 01:10:42 +11:00
|
|
|
// * 2845 - TSIG
|
2011-01-17 20:30:20 +11:00
|
|
|
// * 2915 - NAPTR record
|
2011-01-09 05:10:47 +11:00
|
|
|
// * 3110 - RSASHA1 DNS keys
|
2010-12-31 04:42:40 +11:00
|
|
|
// * 3225 - DO bit (DNSSEC OK)
|
2010-12-30 23:42:52 +11:00
|
|
|
// * 4033/4034/4035 - DNSSEC + validation functions
|
2011-01-17 20:30:20 +11:00
|
|
|
// * 4255 - SSHFP record
|
|
|
|
// * 5011 - NSID
|
2011-01-01 00:11:52 +11:00
|
|
|
// * 5936 - AXFR
|
2010-12-30 23:42:52 +11:00
|
|
|
// * IP6 support
|
2011-01-02 06:01:49 +11:00
|
|
|
//
|
2010-12-30 23:42:52 +11:00
|
|
|
// The package allows full control over what is send out to the DNS.
|
|
|
|
//
|
|
|
|
package dns
|
|
|
|
|
2010-12-31 06:50:31 +11:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
2010-12-30 23:42:52 +11:00
|
|
|
|
|
|
|
const Year68 = 2 << (32 - 1)
|
|
|
|
|
2011-01-03 21:29:04 +11:00
|
|
|
type Error struct {
|
2011-01-14 21:57:28 +11:00
|
|
|
Error string
|
|
|
|
Name string
|
|
|
|
Server string
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-17 06:07:17 +11:00
|
|
|
// Check if the RRset is 2181 compliant
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
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"
|
|
|
|
s = s + Class_str[h.Class] + "\t"
|
|
|
|
s = s + Rr_str[h.Rrtype] + "\t"
|
|
|
|
return s
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
2011-01-17 06:07:17 +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-01-14 21:57:28 +11:00
|
|
|
for _, v := range a {
|
|
|
|
if v == '.' {
|
|
|
|
c++
|
|
|
|
}
|
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
|
|
|
}
|