2010-12-30 23:42:52 +11:00
|
|
|
package dns
|
|
|
|
|
2015-01-13 21:20:34 +11:00
|
|
|
import "strconv"
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2011-02-28 20:42:03 +11:00
|
|
|
const (
|
2016-06-14 04:44:38 +10:00
|
|
|
year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
|
|
|
|
defaultTtl = 3600 // Default internal TTL.
|
|
|
|
|
|
|
|
DefaultMsgSize = 4096 // DefaultMsgSize is the standard default for messages larger than 512 bytes.
|
|
|
|
MinMsgSize = 512 // MinMsgSize is the minimal size of a DNS packet.
|
|
|
|
MaxMsgSize = 65535 // MaxMsgSize is the largest possible DNS packet.
|
2011-02-28 20:42:03 +11:00
|
|
|
)
|
2010-12-30 23:42:52 +11:00
|
|
|
|
2016-06-14 04:44:38 +10: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
|
2016-06-14 04:44:38 +10:00
|
|
|
|
2013-02-09 18:35:17 +11:00
|
|
|
// copy returns a copy of the RR
|
|
|
|
copy() RR
|
Implement custom verbs for fmt.Printf use
The dns package implements String() for all RR types, but sometimes you will
need more flexibility. The functions Printf, Sprintf, etc. implemented formatted I/O
for the RR type.
Printing
The verbs:
Generic part of RRs:
%N the owner name of the RR
%C the class: IN, CH, CLASS15, etc.
%D the TTL in seconds
%Y the type: MX, A, etc.
The rdata of each RR differs, we allow each field to be printed as a string.
Rdata:
%0 the first rdata field
%1 the second rdata field
%2 the third rdata field
.. ...
%9 the nineth rdata field
%R all rdata fields
The rdata fields remain a TODO, but will be implemented using
reflection.
2015-01-12 04:23:02 +11:00
|
|
|
// len returns the length (in octets) of the uncompressed RR in wire format.
|
2013-02-09 18:35:17 +11:00
|
|
|
len() int
|
2016-06-05 16:53:12 +10:00
|
|
|
// pack packs an RR into wire format.
|
|
|
|
pack([]byte, int, map[string]int, bool) (int, error)
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
2015-08-23 17:03:13 +10:00
|
|
|
// RR_Header is the header all DNS resource records share.
|
2010-12-30 23:42:52 +11:00
|
|
|
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
|
2016-06-14 04:44:38 +10:00
|
|
|
Rdlength uint16 // Length of data after header.
|
2010-12-30 23:42:52 +11:00
|
|
|
}
|
|
|
|
|
2016-06-14 04:44:38 +10:00
|
|
|
// Header returns itself. This is here to make RR_Header implements the RR interface.
|
2012-11-30 23:25:01 +11:00
|
|
|
func (h *RR_Header) Header() *RR_Header { return h }
|
|
|
|
|
2016-06-04 17:11:36 +10:00
|
|
|
// Just to implement 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
|
|
|
}
|
|
|
|
|
2016-06-13 06:06:46 +10:00
|
|
|
// ToRFC3597 converts a known RR to the unknown RR representation 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)
|
2016-06-13 06:06:46 +10:00
|
|
|
off, err := PackRR(r, buf, 0, nil, false)
|
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
|
|
|
}
|
|
|
|
buf = buf[:off]
|
2016-06-13 06:06:46 +10:00
|
|
|
if int(r.Header().Rdlength) > off {
|
|
|
|
return ErrBuf
|
|
|
|
}
|
|
|
|
|
|
|
|
rfc3597, _, err := unpackRFC3597(*r.Header(), buf, off-int(r.Header().Rdlength))
|
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
|
|
|
}
|
2016-06-13 06:06:46 +10:00
|
|
|
*rr = *rfc3597.(*RFC3597)
|
2013-01-31 19:52:34 +11:00
|
|
|
return nil
|
2013-01-31 01:55:50 +11:00
|
|
|
}
|