2015-09-10 14:17:51 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import "strconv"
|
|
|
|
|
|
|
|
const (
|
2016-11-03 13:40:30 +00:00
|
|
|
year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
|
|
|
|
defaultTtl = 3600 // Default internal TTL.
|
|
|
|
|
2017-12-13 08:06:22 +00:00
|
|
|
// DefaultMsgSize is the standard default for messages larger than 512 bytes.
|
|
|
|
DefaultMsgSize = 4096
|
|
|
|
// MinMsgSize is the minimal size of a DNS packet.
|
|
|
|
MinMsgSize = 512
|
|
|
|
// MaxMsgSize is the largest possible DNS packet.
|
|
|
|
MaxMsgSize = 65535
|
2015-09-10 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2016-11-03 13:40:30 +00:00
|
|
|
// Error represents a DNS error.
|
2015-09-10 14:17:51 +00:00
|
|
|
type Error struct{ err string }
|
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
if e == nil {
|
|
|
|
return "dns: <nil>"
|
|
|
|
}
|
|
|
|
return "dns: " + e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// An RR represents a resource record.
|
|
|
|
type RR interface {
|
|
|
|
// Header returns the header of an resource record. The header contains
|
|
|
|
// everything up to the rdata.
|
|
|
|
Header() *RR_Header
|
|
|
|
// String returns the text representation of the resource record.
|
|
|
|
String() string
|
2016-11-03 13:40:30 +00:00
|
|
|
|
2015-09-10 14:17:51 +00:00
|
|
|
// copy returns a copy of the RR
|
|
|
|
copy() RR
|
2019-02-06 21:41:25 +00:00
|
|
|
|
|
|
|
// len returns the length (in octets) of the compressed or uncompressed RR in wire format.
|
|
|
|
//
|
|
|
|
// If compression is nil, the uncompressed size will be returned, otherwise the compressed
|
|
|
|
// size will be returned and domain names will be added to the map for future compression.
|
|
|
|
len(off int, compression map[string]struct{}) int
|
|
|
|
|
|
|
|
// pack packs the records RDATA into wire format. The header will
|
|
|
|
// already have been packed into msg.
|
|
|
|
pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error)
|
|
|
|
|
|
|
|
// unpack unpacks an RR from wire format.
|
|
|
|
//
|
|
|
|
// This will only be called on a new and empty RR type with only the header populated. It
|
|
|
|
// will only be called if the record's RDATA is non-empty.
|
|
|
|
unpack(msg []byte, off int) (off1 int, err error)
|
|
|
|
|
|
|
|
// parse parses an RR from zone file format.
|
|
|
|
//
|
|
|
|
// This will only be called on a new and empty RR type with only the header populated.
|
|
|
|
parse(c *zlexer, origin, file string) *ParseError
|
|
|
|
|
|
|
|
// isDuplicate returns whether the two RRs are duplicates.
|
|
|
|
isDuplicate(r2 RR) bool
|
2015-09-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RR_Header is the header all DNS resource records share.
|
|
|
|
type RR_Header struct {
|
|
|
|
Name string `dns:"cdomain-name"`
|
|
|
|
Rrtype uint16
|
|
|
|
Class uint16
|
|
|
|
Ttl uint32
|
2016-11-03 13:40:30 +00:00
|
|
|
Rdlength uint16 // Length of data after header.
|
2015-09-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 13:40:30 +00:00
|
|
|
// Header returns itself. This is here to make RR_Header implements the RR interface.
|
2015-09-10 14:17:51 +00:00
|
|
|
func (h *RR_Header) Header() *RR_Header { return h }
|
|
|
|
|
2016-11-03 13:40:30 +00:00
|
|
|
// Just to implement the RR interface.
|
2015-09-10 14:17:51 +00:00
|
|
|
func (h *RR_Header) copy() RR { return nil }
|
|
|
|
|
|
|
|
func (h *RR_Header) String() string {
|
|
|
|
var s string
|
|
|
|
|
|
|
|
if h.Rrtype == TypeOPT {
|
|
|
|
s = ";"
|
|
|
|
// and maybe other things
|
|
|
|
}
|
|
|
|
|
|
|
|
s += sprintName(h.Name) + "\t"
|
|
|
|
s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
|
|
|
|
s += Class(h.Class).String() + "\t"
|
|
|
|
s += Type(h.Rrtype).String() + "\t"
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-02-06 21:41:25 +00:00
|
|
|
func (h *RR_Header) len(off int, compression map[string]struct{}) int {
|
|
|
|
l := domainNameLen(h.Name, off, compression, true)
|
2015-09-10 14:17:51 +00:00
|
|
|
l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2019-02-06 21:41:25 +00:00
|
|
|
func (h *RR_Header) pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error) {
|
|
|
|
// RR_Header has no RDATA to pack.
|
|
|
|
return off, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RR_Header) unpack(msg []byte, off int) (int, error) {
|
|
|
|
panic("dns: internal error: unpack should never be called on RR_Header")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RR_Header) parse(c *zlexer, origin, file string) *ParseError {
|
|
|
|
panic("dns: internal error: parse should never be called on RR_Header")
|
|
|
|
}
|
|
|
|
|
2016-11-03 13:40:30 +00:00
|
|
|
// ToRFC3597 converts a known RR to the unknown RR representation from RFC 3597.
|
2015-09-10 14:17:51 +00:00
|
|
|
func (rr *RFC3597) ToRFC3597(r RR) error {
|
2019-02-06 21:41:25 +00:00
|
|
|
buf := make([]byte, Len(r)*2)
|
|
|
|
headerEnd, off, err := packRR(r, buf, 0, compressionMap{}, false)
|
2015-09-10 14:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf = buf[:off]
|
2019-02-06 21:41:25 +00:00
|
|
|
|
|
|
|
*rr = RFC3597{Hdr: *r.Header()}
|
|
|
|
rr.Hdr.Rdlength = uint16(off - headerEnd)
|
|
|
|
|
|
|
|
if noRdata(rr.Hdr) {
|
|
|
|
return nil
|
2016-11-03 13:40:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:41:25 +00:00
|
|
|
_, err = rr.unpack(buf, headerEnd)
|
2015-09-10 14:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-06 21:41:25 +00:00
|
|
|
|
2015-09-10 14:17:51 +00:00
|
|
|
return nil
|
|
|
|
}
|