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.
|
2013-05-13 00:07:06 +10:00
|
|
|
// Extensions of the original work are copyright (c) 2011 Miek Gieben
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2011-07-24 07:15:40 +10:00
|
|
|
// Package dns implements a full featured interface to the Domain Name System.
|
2012-05-02 16:04:33 +10:00
|
|
|
// Server- and client-side programming is supported.
|
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
|
|
|
//
|
2013-10-16 00:42:24 +11:00
|
|
|
// The package dns supports (asynchronous) querying/replying, incoming/outgoing zone transfers,
|
2012-05-02 15:57:35 +10:00
|
|
|
// TSIG, EDNS0, dynamic updates, notifies and DNSSEC validation/signing.
|
2012-09-01 19:35:00 +10:00
|
|
|
// Note that domain names MUST be fully qualified, before sending them, unqualified
|
|
|
|
// names in a message will result in a packing failure.
|
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
|
|
|
//
|
2012-12-10 06:20:16 +11:00
|
|
|
// r := new(dns.MX)
|
2012-05-08 22:17:17 +10:00
|
|
|
// r.Hdr = dns.RR_Header{Name: "miek.nl.", Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 3600}
|
2014-01-26 10:57:16 +11:00
|
|
|
// r.Preference = 10
|
2012-02-12 21:17:52 +11:00
|
|
|
// r.Mx = "mx.miek.nl."
|
2011-07-24 07:15:40 +10:00
|
|
|
//
|
|
|
|
// Or directly from a string:
|
|
|
|
//
|
2012-05-08 22:17:17 +10:00
|
|
|
// mx, err := dns.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-05-08 22:17:17 +10:00
|
|
|
// mx, err := dns.NewRR("miek.nl. MX 10 mx.miek.nl.")
|
2012-02-15 23:10:02 +11:00
|
|
|
//
|
|
|
|
// Or even:
|
|
|
|
//
|
2012-05-08 22:17:17 +10:00
|
|
|
// mx, err := dns.NewRR("$ORIGIN nl.\nmiek 1H IN MX 10 mx.miek")
|
2011-03-24 05:37:07 +11:00
|
|
|
//
|
2012-05-08 21:39:57 +10:00
|
|
|
// In the DNS messages are exchanged, these messages contain resource
|
|
|
|
// records (sets). Use pattern for creating a message:
|
2011-03-29 19:15:42 +11:00
|
|
|
//
|
2013-09-29 20:50:09 +10:00
|
|
|
// m := new(dns.Msg)
|
2012-05-08 22:17:17 +10:00
|
|
|
// m.SetQuestion("miek.nl.", dns.TypeMX)
|
2011-03-29 19:15:42 +11:00
|
|
|
//
|
2012-09-01 06:23:49 +10:00
|
|
|
// Or when not certain if the domain name is fully qualified:
|
2012-09-01 05:00:58 +10:00
|
|
|
//
|
|
|
|
// m.SetQuestion(dns.Fqdn("miek.nl"), dns.TypeMX)
|
|
|
|
//
|
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
|
|
|
//
|
2012-05-08 22:17:17 +10:00
|
|
|
// m1 := new(dns.Msg)
|
2014-07-30 16:35:06 +10:00
|
|
|
// m1.Id = dns.Id()
|
2012-09-06 00:30:04 +10:00
|
|
|
// m1.RecursionDesired = true
|
2011-07-05 05:28:30 +10:00
|
|
|
// m1.Question = make([]Question, 1)
|
2012-05-08 22:17:17 +10:00
|
|
|
// m1.Question[0] = dns.Question{"miek.nl.", dns.TypeMX, dns.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
|
|
|
//
|
2014-07-30 16:35:06 +10:00
|
|
|
// c := new(dns.Client)
|
2012-11-20 02:22:54 +11:00
|
|
|
// in, rtt, err := c.Exchange(m1, "127.0.0.1:53")
|
2011-03-24 05:37:07 +11:00
|
|
|
//
|
2013-10-16 00:42:24 +11:00
|
|
|
// Suppressing
|
2013-09-06 19:45:30 +10:00
|
|
|
// multiple outstanding queries (with the same question, type and class) is as easy as setting:
|
|
|
|
//
|
2013-09-06 19:49:07 +10:00
|
|
|
// c.SingleInflight = true
|
2012-08-24 16:56:28 +10:00
|
|
|
//
|
2013-09-29 07:05:48 +10:00
|
|
|
// If these "advanced" features are not needed, a simple UDP query can be send,
|
|
|
|
// with:
|
|
|
|
//
|
|
|
|
// in, err := dns.Exchange(m1, "127.0.0.1:53")
|
|
|
|
//
|
2013-10-16 00:42:24 +11:00
|
|
|
// When this functions returns you will get dns message. A dns message consists
|
|
|
|
// out of four sections.
|
2012-08-24 17:35:33 +10:00
|
|
|
// The question section: in.Question, the answer section: in.Answer,
|
2012-08-24 16:57:33 +10:00
|
|
|
// the authority section: in.Ns and the additional section: in.Extra.
|
2012-08-24 16:56:28 +10:00
|
|
|
//
|
|
|
|
// Each of these sections (except the Question section) contain a []RR. Basic
|
2013-05-06 04:30:44 +10:00
|
|
|
// use pattern for accessing the rdata of a TXT RR as the first RR in
|
2012-08-24 16:56:28 +10:00
|
|
|
// the Answer section:
|
|
|
|
//
|
2013-06-04 19:37:11 +10:00
|
|
|
// if t, ok := in.Answer[0].(*dns.TXT); ok {
|
2012-08-24 16:56:28 +10:00
|
|
|
// // do something with t.Txt
|
|
|
|
// }
|
2014-03-02 21:38:46 +11:00
|
|
|
//
|
|
|
|
// Domain Name and TXT Character String Representations
|
|
|
|
//
|
|
|
|
// Both domain names and TXT character strings are converted to presentation
|
|
|
|
// form both when unpacked and when converted to strings.
|
|
|
|
//
|
|
|
|
// For TXT character strings, tabs, carriage returns and line feeds will be
|
|
|
|
// converted to \t, \r and \n respectively. Back slashes and quotations marks
|
|
|
|
// will be escaped. Bytes below 32 and above 127 will be converted to \DDD
|
|
|
|
// form.
|
|
|
|
//
|
|
|
|
// For domain names, in addition to the above rules brackets, periods,
|
|
|
|
// spaces, semicolons and the at symbol are escaped.
|
2010-12-30 23:42:52 +11:00
|
|
|
package dns
|
|
|
|
|
2010-12-31 06:50:31 +11:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2011-02-28 20:42:03 +11:00
|
|
|
const (
|
2012-08-17 16:29:45 +10:00
|
|
|
year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
|
2014-01-12 21:43:59 +11:00
|
|
|
DefaultMsgSize = 4096 // Standard default for larger than 512 bytes.
|
2013-09-29 20:21:18 +10:00
|
|
|
MinMsgSize = 512 // Minimal size of a DNS packet.
|
2012-02-05 21:33:55 +11:00
|
|
|
MaxMsgSize = 65536 // Largest possible DNS packet.
|
2012-08-17 16:31:38 +10: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
|
2013-06-20 16:25:29 +10:00
|
|
|
type Error struct{ err string }
|
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
|
|
|
}
|
2013-06-20 16:25:29 +10:00
|
|
|
return "dns: " + 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
|
2013-02-09 18:35:17 +11:00
|
|
|
// copy returns a copy of the RR
|
|
|
|
copy() RR
|
|
|
|
// len returns the length (in octects) of the uncompressed RR in wire format.
|
|
|
|
len() int
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// DNS resource records.
|
2012-08-27 06:40:00 +10:00
|
|
|
// There are many types of RRs,
|
2010-12-30 23:42:52 +11:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2012-11-30 23:25:01 +11:00
|
|
|
func (h *RR_Header) Header() *RR_Header { return h }
|
|
|
|
|
|
|
|
// Just to imlement the RR interface
|
2013-02-09 18:35:17 +11:00
|
|
|
func (h *RR_Header) copy() RR { return nil }
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2013-02-09 18:35:17 +11:00
|
|
|
func (h *RR_Header) copyHeader() *RR_Header {
|
2012-05-11 23:03:49 +10:00
|
|
|
r := new(RR_Header)
|
|
|
|
r.Name = h.Name
|
|
|
|
r.Rrtype = h.Rrtype
|
|
|
|
r.Class = h.Class
|
|
|
|
r.Ttl = h.Ttl
|
|
|
|
r.Rdlength = h.Rdlength
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-04-24 06:06:17 +10:00
|
|
|
s += sprintName(h.Name) + "\t"
|
2013-06-08 23:24:20 +10:00
|
|
|
s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
|
|
|
|
s += Class(h.Class).String() + "\t"
|
|
|
|
s += Type(h.Rrtype).String() + "\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
|
|
|
|
2013-02-09 18:35:17 +11:00
|
|
|
func (h *RR_Header) len() int {
|
2014-02-15 07:20:22 +11:00
|
|
|
l := len(h.Name) + 1
|
2012-01-13 09:17:34 +11:00
|
|
|
l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
|
|
|
|
return l
|
2012-01-10 20:43:28 +11:00
|
|
|
}
|
|
|
|
|
2013-01-31 01:55:50 +11:00
|
|
|
// ToRFC3597 converts a known RR to the unknown RR representation
|
2013-01-31 01:56:29 +11:00
|
|
|
// from RFC 3597.
|
2013-01-31 19:52:34 +11:00
|
|
|
func (rr *RFC3597) ToRFC3597(r RR) error {
|
2013-02-09 18:35:17 +11:00
|
|
|
buf := make([]byte, r.len()*2)
|
2013-01-31 01:55:50 +11:00
|
|
|
off, err := PackStruct(r, buf, 0)
|
|
|
|
if err != nil {
|
2013-01-31 19:52:34 +11:00
|
|
|
return err
|
2013-01-31 01:55:50 +11:00
|
|
|
}
|
|
|
|
buf = buf[:off]
|
|
|
|
rawSetRdlength(buf, 0, off)
|
2013-01-31 19:52:34 +11:00
|
|
|
_, err = UnpackStruct(rr, buf, 0)
|
2013-01-31 01:55:50 +11:00
|
|
|
if err != nil {
|
2013-01-31 19:52:34 +11:00
|
|
|
return err
|
2013-01-31 01:55:50 +11:00
|
|
|
}
|
2013-01-31 19:52:34 +11:00
|
|
|
return nil
|
2013-01-31 01:55:50 +11:00
|
|
|
}
|