dns/msg.go

1666 lines
44 KiB
Go
Raw Normal View History

2010-08-04 07:57:59 +10: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.
// Extensions of the original work are copyright (c) 2011 Miek Gieben
2010-08-04 07:57:59 +10:00
2011-01-27 19:29:11 +11:00
// DNS packet assembly, see RFC 1035. Converting from - Unpack() -
// and to - Pack() - wire format.
// All the packers and unpackers take a (msg []byte, off int)
// and return (off1 int, ok bool). If they return ok==false, they
// also return off1==len(msg), so that the next unpacker will
// also fail. This lets us avoid checks of ok until the end of a
// packing sequence.
2010-08-04 07:57:59 +10:00
package dns
import (
"encoding/base32"
"encoding/base64"
2010-08-04 07:57:59 +10:00
"encoding/hex"
2012-11-16 00:26:08 +11:00
"fmt"
"math/rand"
"net"
"reflect"
"strconv"
"time"
2010-08-04 07:57:59 +10:00
)
2012-01-11 21:44:44 +11:00
const maxCompressionOffset = 2 << 13 // We have 14 bits for the compression pointer
2012-01-11 21:13:17 +11:00
2011-03-25 21:20:52 +11:00
var (
2013-06-20 16:27:28 +10:00
ErrFqdn error = &Error{err: "domain must be fully qualified"}
ErrId error = &Error{err: "id mismatch"}
ErrRdata error = &Error{err: "bad rdata"}
ErrBuf error = &Error{err: "buffer size too small"}
ErrShortRead error = &Error{err: "short read"}
ErrConn error = &Error{err: "conn holds both UDP and TCP connection"}
ErrConnEmpty error = &Error{err: "conn has no connection"}
ErrServ error = &Error{err: "no servers could be reached"}
ErrKey error = &Error{err: "bad key"}
ErrPrivKey error = &Error{err: "bad private key"}
ErrKeySize error = &Error{err: "bad key size"}
ErrKeyAlg error = &Error{err: "bad key algorithm"}
ErrAlg error = &Error{err: "bad algorithm"}
ErrTime error = &Error{err: "bad time"}
ErrNoSig error = &Error{err: "no signature found"}
ErrSig error = &Error{err: "bad signature"}
ErrSecret error = &Error{err: "no secrets defined"}
ErrSigGen error = &Error{err: "bad signature generation"}
ErrAuth error = &Error{err: "bad authentication"}
ErrSoa error = &Error{err: "no SOA"}
ErrRRset error = &Error{err: "bad rrset"}
2011-03-25 21:20:52 +11:00
)
2011-03-25 21:19:35 +11:00
2011-01-01 03:05:11 +11:00
// A manually-unpacked version of (id, bits).
// This is in its own struct for easy printing.
type MsgHdr struct {
Id uint16
Response bool
Opcode int
Authoritative bool
Truncated bool
RecursionDesired bool
RecursionAvailable bool
Zero bool
AuthenticatedData bool
CheckingDisabled bool
Rcode int
}
// The layout of a DNS message.
type Msg struct {
MsgHdr
2013-01-25 06:46:07 +11:00
Compress bool `json:"-"` // If true, the message will be compressed when converted to wire format.
2012-08-08 19:13:28 +10:00
Question []Question // Holds the RR(s) of the question section.
Answer []RR // Holds the RR(s) of the answer section.
Ns []RR // Holds the RR(s) of the authority section.
Extra []RR // Holds the RR(s) of the additional section.
2011-01-01 03:05:11 +11:00
}
2010-08-04 07:57:59 +10:00
// Map of strings for each RR wire type.
var TypeToString = map[uint16]string{
2011-01-02 05:51:25 +11:00
TypeCNAME: "CNAME",
TypeHINFO: "HINFO",
2013-01-21 04:16:47 +11:00
TypeTLSA: "TLSA",
2011-01-02 05:51:25 +11:00
TypeMB: "MB",
TypeMG: "MG",
2012-06-21 04:16:36 +10:00
TypeRP: "RP",
TypeMD: "MD",
TypeMF: "MF",
2011-01-02 05:51:25 +11:00
TypeMINFO: "MINFO",
TypeMR: "MR",
TypeMX: "MX",
2012-06-21 04:16:36 +10:00
TypeWKS: "WKS",
2011-01-02 05:51:25 +11:00
TypeNS: "NS",
2012-11-22 09:20:56 +11:00
TypeNULL: "NULL",
TypeAFSDB: "AFSDB",
TypeX25: "X25",
TypeISDN: "ISDN",
2011-01-02 05:51:25 +11:00
TypePTR: "PTR",
2012-08-17 16:29:45 +10:00
TypeRT: "RT",
2011-01-02 05:51:25 +11:00
TypeSOA: "SOA",
TypeTXT: "TXT",
TypeSRV: "SRV",
2012-11-22 09:20:56 +11:00
TypeATMA: "ATMA",
2011-01-02 05:51:25 +11:00
TypeNAPTR: "NAPTR",
TypeKX: "KX",
2011-01-26 01:09:30 +11:00
TypeCERT: "CERT",
TypeDNAME: "DNAME",
2011-01-02 05:51:25 +11:00
TypeA: "A",
TypeAAAA: "AAAA",
TypeLOC: "LOC",
TypeOPT: "OPT",
TypeDS: "DS",
TypeDHCID: "DHCID",
2012-02-20 01:33:12 +11:00
TypeHIP: "HIP",
2012-11-22 09:20:56 +11:00
TypeNINFO: "NINFO",
TypeRKEY: "RKEY",
TypeCDS: "CDS",
TypeCAA: "CAA",
TypeIPSECKEY: "IPSECKEY",
2011-01-26 01:09:30 +11:00
TypeSSHFP: "SSHFP",
2011-01-02 05:51:25 +11:00
TypeRRSIG: "RRSIG",
TypeNSEC: "NSEC",
TypeDNSKEY: "DNSKEY",
TypeNSEC3: "NSEC3",
2011-01-27 19:29:11 +11:00
TypeNSEC3PARAM: "NSEC3PARAM",
TypeTALINK: "TALINK",
2011-01-26 01:09:30 +11:00
TypeSPF: "SPF",
2012-11-19 22:26:13 +11:00
TypeNID: "NID",
TypeL32: "L32",
TypeL64: "L64",
TypeLP: "LP",
2013-05-01 00:42:04 +10:00
TypeUINFO: "UINFO",
TypeUID: "UID",
TypeGID: "GID",
TypeUNSPEC: "UNSPEC",
TypeEUI48: "EUI48",
TypeEUI64: "EUI64",
2011-01-26 01:09:30 +11:00
TypeTKEY: "TKEY", // Meta RR
TypeTSIG: "TSIG", // Meta RR
TypeAXFR: "AXFR", // Meta RR
TypeIXFR: "IXFR", // Meta RR
2011-08-22 22:11:41 +10:00
TypeANY: "ANY", // Meta RR
2011-02-25 02:22:14 +11:00
TypeURI: "URI",
2011-02-21 23:16:07 +11:00
TypeTA: "TA",
TypeDLV: "DLV",
}
2011-07-15 05:25:15 +10:00
// Reverse, needed for string parsing.
var StringToType = reverseInt16(TypeToString)
var StringToClass = reverseInt16(ClassToString)
2011-09-20 06:27:56 +10:00
// Map of opcodes strings.
var StringToOpcode = reverseInt(OpcodeToString)
2011-09-20 06:27:56 +10:00
// Map of rcodes strings.
var StringToRcode = reverseInt(RcodeToString)
2011-01-27 19:29:11 +11:00
// Map of strings for each CLASS wire type.
var ClassToString = map[uint16]string{
2010-08-04 07:57:59 +10:00
ClassINET: "IN",
ClassCSNET: "CS",
ClassCHAOS: "CH",
ClassHESIOD: "HS",
2011-07-24 07:43:43 +10:00
ClassNONE: "NONE",
2010-08-04 07:57:59 +10:00
ClassANY: "ANY",
}
// Map of strings for opcodes.
var OpcodeToString = map[int]string{
2011-01-02 05:51:25 +11:00
OpcodeQuery: "QUERY",
OpcodeIQuery: "IQUERY",
OpcodeStatus: "STATUS",
OpcodeNotify: "NOTIFY",
OpcodeUpdate: "UPDATE",
2010-08-04 07:57:59 +10:00
}
// Map of strings for rcodes.
var RcodeToString = map[int]string{
2011-01-02 05:51:25 +11:00
RcodeSuccess: "NOERROR",
2011-01-02 06:51:34 +11:00
RcodeFormatError: "FORMERR",
2011-01-02 05:51:25 +11:00
RcodeServerFailure: "SERVFAIL",
RcodeNameError: "NXDOMAIN",
RcodeNotImplemented: "NOTIMPL",
RcodeRefused: "REFUSED",
2011-01-03 20:44:00 +11:00
RcodeYXDomain: "YXDOMAIN", // From RFC 2136
RcodeYXRrset: "YXRRSET",
RcodeNXRrset: "NXRRSET",
RcodeNotAuth: "NOTAUTH",
RcodeNotZone: "NOTZONE",
RcodeBadSig: "BADSIG", // Also known as RcodeBadVers, see RFC 6891
// RcodeBadVers: "BADVERS",
RcodeBadKey: "BADKEY",
RcodeBadTime: "BADTIME",
RcodeBadMode: "BADMODE",
RcodeBadName: "BADNAME",
RcodeBadAlg: "BADALG",
RcodeBadTrunc: "BADTRUNC",
2010-08-04 07:57:59 +10:00
}
2011-01-27 19:29:11 +11:00
// Rather than write the usual handful of routines to pack and
// unpack every message that can appear on the wire, we use
// reflection to write a generic pack/unpack for structs and then
// use it. Thus, if in the future we need to define new message
// structs, no new pack/unpack/printing code needs to be written.
2010-08-04 07:57:59 +10:00
// Domain names are a sequence of counted strings
2011-12-17 05:29:24 +11:00
// split at the dots. They end with a zero-length string.
2012-01-17 07:49:17 +11:00
// PackDomainName packs a domain name s into msg[off:].
2012-01-17 07:57:57 +11:00
// If compression is wanted compress must be true and the compression
// map needs to hold a mapping between domain names and offsets
2012-01-17 07:57:57 +11:00
// pointing into msg[].
func PackDomainName(s string, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) {
lenmsg := len(msg)
ls := len(s)
if ls == 0 { // Ok, for instance when dealing with update RR without any rdata.
return off, nil
}
// If not fully qualified, error out
if s[ls-1] != '.' {
return lenmsg, ErrFqdn
2010-08-04 07:57:59 +10:00
}
// Each dot ends a segment of the name.
// We trade each dot byte for a length byte.
// Except for escaped dots (\.), which are normal dots.
2010-08-04 07:57:59 +10:00
// There is also a trailing zero.
2012-01-11 21:44:44 +11:00
// Compression
nameoffset := -1
pointer := -1
2010-08-04 07:57:59 +10:00
// Emit sequence of counted strings, chopping at dots.
begin := 0
2011-02-25 02:22:14 +11:00
bs := []byte(s)
for i := 0; i < ls; i++ {
if bs[i] == '\\' {
for j := i; j < ls-1; j++ {
2011-02-25 02:22:14 +11:00
bs[j] = bs[j+1]
}
ls--
if off+1 > lenmsg {
return lenmsg, ErrBuf
}
// check for \DDD
if i+2 < ls && bs[i] >= '0' && bs[i] <= '9' &&
bs[i+1] >= '0' && bs[i+1] <= '9' &&
bs[i+2] >= '0' && bs[i+2] <= '9' {
bs[i] = byte((bs[i]-'0')*100 + (bs[i+1]-'0')*10 + (bs[i+2] - '0'))
2012-11-19 22:26:13 +11:00
for j := i + 1; j < ls-2; j++ {
bs[j] = bs[j+2]
}
ls -= 2
}
2011-02-25 02:22:14 +11:00
continue
}
if bs[i] == '.' {
2011-02-25 02:22:14 +11:00
if i-begin >= 1<<6 { // top two bits of length must be clear
return lenmsg, ErrRdata
2011-02-25 02:22:14 +11:00
}
// off can already (we're in a loop) be bigger than len(msg)
// this happens when a name isn't fully qualified
2012-10-11 22:06:27 +11:00
if off+1 > lenmsg {
return lenmsg, ErrBuf
}
2011-02-25 02:22:14 +11:00
msg[off] = byte(i - begin)
2012-01-11 21:19:07 +11:00
offset := off
2011-02-25 02:22:14 +11:00
off++
for j := begin; j < i; j++ {
2012-10-11 22:06:27 +11:00
if off+1 > lenmsg {
return lenmsg, ErrBuf
}
2011-02-25 02:22:14 +11:00
msg[off] = bs[j]
off++
}
2012-01-12 02:22:58 +11:00
// Dont try to compress '.'
2012-10-10 07:01:57 +11:00
if compression != nil && string(bs[begin:]) != "." {
if p, ok := compression[string(bs[begin:])]; !ok {
2012-01-12 00:09:47 +11:00
// Only offsets smaller than this can be used.
2012-01-11 21:44:44 +11:00
if offset < maxCompressionOffset {
compression[string(bs[begin:])] = offset
2012-01-11 21:44:44 +11:00
}
} else {
// The first hit is the longest matching dname
// keep the pointer offset we get back and store
// the offset of the current name, because that's
// where we need to insert the pointer later
// If compress is true, we're allowed to compress this dname
if pointer == -1 && compress {
2012-01-11 21:44:44 +11:00
pointer = p // Where to point to
nameoffset = offset // Where to point from
break
2012-01-11 21:44:44 +11:00
}
2012-01-11 21:19:07 +11:00
}
}
2011-02-25 02:22:14 +11:00
begin = i + 1
2010-08-04 07:57:59 +10:00
}
}
2010-12-24 03:10:06 +11:00
// Root label is special
2013-06-14 21:38:04 +10:00
if len(bs) == 1 && bs[0] == '.' {
return off, nil
2010-12-24 03:10:06 +11:00
}
// If we did compression and we find something add the pointer here
2012-01-11 21:44:44 +11:00
if pointer != -1 {
2012-01-12 00:09:47 +11:00
// We have two bytes (14 bits) to put the pointer in
2012-01-11 21:44:44 +11:00
msg[nameoffset], msg[nameoffset+1] = packUint16(uint16(pointer ^ 0xC000))
off = nameoffset + 1
goto End
2012-01-11 21:44:44 +11:00
}
2010-08-04 07:57:59 +10:00
msg[off] = 0
End:
2010-08-04 07:57:59 +10:00
off++
return off, nil
2010-08-04 07:57:59 +10:00
}
// Unpack a domain name.
// In addition to the simple sequences of counted strings above,
// domain names are allowed to refer to strings elsewhere in the
// packet, to avoid repeating common suffixes when returning
// many entries in a single domain. The pointers are marked
// by a length byte with the top two bits set. Ignoring those
// two bits, that byte and the next give a 14 bit offset from msg[0]
// where we should pick up the trail.
// Note that if we jump elsewhere in the packet,
// we return off1 == the offset after the first pointer we found,
// which is where the next record will start.
// In theory, the pointers are only allowed to jump backward.
// We let them jump anywhere and stop jumping after a while.
2012-01-17 07:49:17 +11:00
2012-01-27 08:52:29 +11:00
// UnpackDomainName unpacks a domain name into a string.
func UnpackDomainName(msg []byte, off int) (s string, off1 int, err error) {
2010-08-04 07:57:59 +10:00
s = ""
lenmsg := len(msg)
2010-08-04 07:57:59 +10:00
ptr := 0 // number of pointers followed
Loop:
for {
2011-08-09 19:41:44 +10:00
if off >= lenmsg {
return "", lenmsg, ErrBuf
2010-08-04 07:57:59 +10:00
}
c := int(msg[off])
off++
switch c & 0xC0 {
case 0x00:
if c == 0x00 {
// end of name
2012-03-18 17:43:12 +11:00
if s == "" {
return ".", off, nil
2012-03-18 17:43:12 +11:00
}
2010-08-04 07:57:59 +10:00
break Loop
}
// literal string
2011-08-09 19:41:44 +10:00
if off+c > lenmsg {
return "", lenmsg, ErrBuf
2010-08-04 07:57:59 +10:00
}
2011-02-25 02:22:14 +11:00
for j := off; j < off+c; j++ {
2012-11-16 00:26:08 +11:00
switch {
2012-11-19 22:26:13 +11:00
case msg[j] == '.': // literal dots
2011-02-25 02:22:14 +11:00
s += "\\."
2012-11-19 22:26:13 +11:00
case msg[j] < 32: // unprintable use \DDD
2012-11-16 00:26:08 +11:00
fallthrough
case msg[j] >= 127:
s += fmt.Sprintf("\\%03d", msg[j])
default:
2011-02-25 02:22:14 +11:00
s += string(msg[j])
}
}
s += "."
2010-08-04 07:57:59 +10:00
off += c
case 0xC0:
// pointer to somewhere else in msg.
// remember location after first ptr,
// since that's how many bytes we consumed.
// also, don't follow too many pointers --
// maybe there's a loop.
2011-08-09 19:41:44 +10:00
if off >= lenmsg {
return "", lenmsg, ErrBuf
2010-08-04 07:57:59 +10:00
}
c1 := msg[off]
off++
if ptr == 0 {
off1 = off
}
if ptr++; ptr > 10 {
2013-06-20 16:27:28 +10:00
return "", lenmsg, &Error{err: "too many compression pointers"}
2010-08-04 07:57:59 +10:00
}
off = (c^0xC0)<<8 | int(c1)
default:
// 0x80 and 0x40 are reserved
return "", lenmsg, ErrRdata
2010-08-04 07:57:59 +10:00
}
}
if ptr == 0 {
off1 = off
}
return s, off1, nil
2010-08-04 07:57:59 +10:00
}
2010-08-05 08:06:25 +10:00
// Pack a reflect.StructValue into msg. Struct members can only be uint8, uint16, uint32, string,
// slices and other (often anonymous) structs.
func packStructValue(val reflect.Value, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) {
lenmsg := len(msg)
2010-08-04 07:57:59 +10:00
for i := 0; i < val.NumField(); i++ {
switch fv := val.Field(i); fv.Kind() {
2010-08-04 07:57:59 +10:00
default:
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "bad kind packing"}
case reflect.Slice:
switch val.Type().Field(i).Tag {
2010-08-05 07:41:05 +10:00
default:
2013-06-20 16:25:29 +10:00
return lenmsg, &Error{"bad tag packing slice: " + val.Type().Field(i).Tag.Get("dns")}
case `dns:"domain-name"`:
2012-02-20 01:37:27 +11:00
for j := 0; j < val.Field(i).Len(); j++ {
element := val.Field(i).Index(j).String()
off, err = PackDomainName(element, msg, off, compression, false && compress)
if err != nil {
return lenmsg, err
2012-02-23 08:11:40 +11:00
}
}
case `dns:"txt"`:
for j := 0; j < val.Field(i).Len(); j++ {
2012-02-20 01:33:12 +11:00
element := val.Field(i).Index(j).String()
// Counted string: 1 byte length.
if len(element) > 255 || off+1+len(element) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing txt"}
2012-02-20 01:33:12 +11:00
}
msg[off] = byte(len(element))
off++
for i := 0; i < len(element); i++ {
msg[off+i] = element[i]
}
off += len(element)
}
case `dns:"opt"`: // edns
for j := 0; j < val.Field(i).Len(); j++ {
element := val.Field(i).Index(j).Interface()
2012-09-09 21:24:21 +10:00
b, e := element.(EDNS0).pack()
2011-01-09 09:47:24 +11:00
if e != nil {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing opt"}
2011-01-09 09:47:24 +11:00
}
// Option code
msg[off], msg[off+1] = packUint16(element.(EDNS0).Option())
2010-12-23 21:02:01 +11:00
// Length
msg[off+2], msg[off+3] = packUint16(uint16(len(b)))
2011-11-03 09:06:54 +11:00
off += 4
// Actual data
copy(msg[off:off+len(b)], b)
off += len(b)
2010-12-23 21:02:01 +11:00
}
case `dns:"a"`:
2011-01-26 01:09:30 +11:00
// It must be a slice of 4, even if it is 16, we encode
// only the first 4
if off+net.IPv4len > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing a"}
}
2011-12-10 07:42:49 +11:00
switch fv.Len() {
case net.IPv6len:
msg[off] = byte(fv.Index(12).Uint())
msg[off+1] = byte(fv.Index(13).Uint())
msg[off+2] = byte(fv.Index(14).Uint())
msg[off+3] = byte(fv.Index(15).Uint())
2012-01-09 21:02:45 +11:00
off += net.IPv4len
2011-12-10 07:42:49 +11:00
case net.IPv4len:
msg[off] = byte(fv.Index(0).Uint())
msg[off+1] = byte(fv.Index(1).Uint())
msg[off+2] = byte(fv.Index(2).Uint())
msg[off+3] = byte(fv.Index(3).Uint())
2012-01-09 21:02:45 +11:00
off += net.IPv4len
case 0:
// Allowed, for dynamic updates
2011-12-10 07:42:49 +11:00
default:
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing a"}
2011-01-26 01:09:30 +11:00
}
case `dns:"aaaa"`:
if fv.Len() == 0 {
break
}
if fv.Len() > net.IPv6len || off+fv.Len() > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing aaaa"}
2010-08-05 07:41:05 +10:00
}
for j := 0; j < net.IPv6len; j++ {
msg[off] = byte(fv.Index(j).Uint())
2010-08-05 07:41:05 +10:00
off++
}
case `dns:"wks"`:
if off == lenmsg {
break // dyn. updates
}
2012-06-02 06:23:21 +10:00
if val.Field(i).Len() == 0 {
break
}
var bitmapbyte uint16
for j := 0; j < val.Field(i).Len(); j++ {
serv := uint16((fv.Index(j).Uint()))
bitmapbyte = uint16(serv / 8)
if int(bitmapbyte) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing wks"}
2012-06-02 06:23:21 +10:00
}
bit := uint16(serv) - bitmapbyte*8
msg[bitmapbyte] = byte(1 << (7 - bit))
}
off += int(bitmapbyte)
case `dns:"nsec"`: // NSEC/NSEC3
// This is the uint16 type bitmap
2012-01-27 08:46:59 +11:00
if val.Field(i).Len() == 0 {
// Do absolutely nothing
break
}
2012-01-23 21:30:26 +11:00
lastwindow := uint16(0)
2012-01-15 22:15:05 +11:00
length := uint16(0)
2012-02-20 01:37:27 +11:00
if off+2 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing nsecx"}
2012-01-15 23:53:32 +11:00
}
for j := 0; j < val.Field(i).Len(); j++ {
t := uint16((fv.Index(j).Uint()))
window := uint16(t / 256)
if lastwindow != window {
2012-01-15 23:53:32 +11:00
// New window, jump to the new offset
off += int(length) + 3
if off > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing nsecx bitmap"}
2012-01-15 23:53:32 +11:00
}
}
2012-01-15 23:20:53 +11:00
length = (t - window*256) / 8
2012-01-15 22:15:05 +11:00
bit := t - (window * 256) - (length * 8)
2012-01-27 08:46:59 +11:00
if off+2+int(length) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing nsecx bitmap"}
2012-01-27 08:46:59 +11:00
}
2012-01-15 23:53:32 +11:00
// Setting the window #
msg[off] = byte(window)
2012-01-15 23:53:32 +11:00
// Setting the octets length
msg[off+1] = byte(length + 1)
// Setting the bit value for the type in the right octet
msg[off+2+int(length)] |= byte(1 << (7 - bit))
lastwindow = window
}
2012-01-15 23:53:32 +11:00
off += 2 + int(length)
off++
if off > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing nsecx bitmap"}
2012-01-15 23:53:32 +11:00
}
2010-08-05 07:41:05 +10:00
}
case reflect.Struct:
off, err = packStructValue(fv, msg, off, compression, compress)
if err != nil {
return lenmsg, err
}
2011-08-08 22:09:05 +10:00
case reflect.Uint8:
if off+1 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing uint8"}
2011-08-08 22:09:05 +10:00
}
msg[off] = byte(fv.Uint())
off++
case reflect.Uint16:
if off+2 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing uint16"}
2011-08-08 22:09:05 +10:00
}
i := fv.Uint()
msg[off] = byte(i >> 8)
msg[off+1] = byte(i)
off += 2
case reflect.Uint32:
if off+4 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing uint32"}
2011-08-08 22:09:05 +10:00
}
i := fv.Uint()
msg[off] = byte(i >> 24)
msg[off+1] = byte(i >> 16)
msg[off+2] = byte(i >> 8)
msg[off+3] = byte(i)
off += 4
case reflect.Uint64:
2012-11-19 22:26:13 +11:00
switch val.Type().Field(i).Tag {
default:
if off+8 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing uint64"}
2012-11-19 22:26:13 +11:00
}
i := fv.Uint()
msg[off] = byte(i >> 56)
msg[off+1] = byte(i >> 48)
msg[off+2] = byte(i >> 40)
msg[off+3] = byte(i >> 32)
msg[off+4] = byte(i >> 24)
msg[off+5] = byte(i >> 16)
msg[off+6] = byte(i >> 8)
msg[off+7] = byte(i)
off += 8
case `dns:"uint48"`:
// Used in TSIG, where it stops at 48 bits, so we discard the upper 16
if off+6 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing uint64 as uint48"}
2012-11-19 22:26:13 +11:00
}
i := fv.Uint()
msg[off] = byte(i >> 40)
msg[off+1] = byte(i >> 32)
msg[off+2] = byte(i >> 24)
msg[off+3] = byte(i >> 16)
msg[off+4] = byte(i >> 8)
msg[off+5] = byte(i)
off += 6
2011-08-08 22:09:05 +10:00
}
case reflect.String:
2010-08-04 07:57:59 +10:00
// There are multiple string encodings.
// The tag distinguishes ordinary strings from domain names.
s := fv.String()
switch val.Type().Field(i).Tag {
2010-08-04 07:57:59 +10:00
default:
2013-06-20 16:25:29 +10:00
return lenmsg, &Error{"bad tag packing string: " + val.Type().Field(i).Tag.Get("dns")}
case `dns:"base64"`:
b64, err := packBase64([]byte(s))
2010-12-21 08:20:13 +11:00
if err != nil {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing base64"}
2010-12-21 08:20:13 +11:00
}
copy(msg[off:off+len(b64)], b64)
off += len(b64)
case `dns:"domain-name"`:
if off, err = PackDomainName(s, msg, off, compression, false && compress); err != nil {
return lenmsg, err
2012-01-11 21:19:07 +11:00
}
case `dns:"cdomain-name"`:
if off, err = PackDomainName(s, msg, off, compression, true && compress); err != nil {
return lenmsg, err
2010-08-04 07:57:59 +10:00
}
case `dns:"size-base32"`:
// This is purely for NSEC3 atm, the previous byte must
// holds the length of the encoded string. As NSEC3
// is only defined to SHA1, the hashlength is 20 (160 bits)
2012-02-20 01:38:21 +11:00
msg[off-1] = 20
fallthrough
case `dns:"base32"`:
b32, err := packBase32([]byte(s))
if err != nil {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing base32"}
}
copy(msg[off:off+len(b32)], b32)
off += len(b32)
case `dns:"size-hex"`:
fallthrough
case `dns:"hex"`:
2011-02-22 05:29:42 +11:00
// There is no length encoded here
h, e := hex.DecodeString(s)
if e != nil {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing hex"}
}
2012-01-11 21:19:07 +11:00
if off+hex.DecodedLen(len(s)) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing hex"}
2012-01-11 21:19:07 +11:00
}
copy(msg[off:off+hex.DecodedLen(len(s))], h)
2011-01-26 08:05:16 +11:00
off += hex.DecodedLen(len(s))
case `dns:"size"`:
// the size is already encoded in the RR, we can safely use the
2011-01-26 08:05:16 +11:00
// length of string. String is RAW (not encoded in hex, nor base64)
copy(msg[off:off+len(s)], s)
off += len(s)
case `dns:"txt"`:
fallthrough
2010-08-04 07:57:59 +10:00
case "":
// Counted string: 1 byte length.
if len(s) > 255 || off+1+len(s) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow packing string"}
2010-08-04 07:57:59 +10:00
}
msg[off] = byte(len(s))
off++
for i := 0; i < len(s); i++ {
msg[off+i] = s[i]
}
off += len(s)
}
}
}
return off, nil
2010-08-04 07:57:59 +10:00
}
func structValue(any interface{}) reflect.Value {
return reflect.ValueOf(any).Elem()
2010-08-04 07:57:59 +10:00
}
2013-05-07 06:23:13 +10:00
// PackStruct packs any structure to wire format.
func PackStruct(any interface{}, msg []byte, off int) (off1 int, err error) {
off, err = packStructValue(structValue(any), msg, off, nil, false)
return off, err
}
func packStructCompress(any interface{}, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) {
off, err = packStructValue(structValue(any), msg, off, compression, compress)
return off, err
2010-08-04 07:57:59 +10:00
}
// TODO(mg): Fix use of rdlength here
2010-08-04 07:57:59 +10:00
// Unpack a reflect.StructValue from msg.
// Same restrictions as packStructValue.
func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, err error) {
2012-03-18 17:43:12 +11:00
var rdstart int
lenmsg := len(msg)
2010-08-04 07:57:59 +10:00
for i := 0; i < val.NumField(); i++ {
switch fv := val.Field(i); fv.Kind() {
2010-08-04 07:57:59 +10:00
default:
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "bad kind unpacking"}
case reflect.Slice:
switch val.Type().Field(i).Tag {
2010-08-04 07:57:59 +10:00
default:
2013-06-20 16:25:29 +10:00
return lenmsg, &Error{"bad tag unpacking slice: " + val.Type().Field(i).Tag.Get("dns")}
case `dns:"domain-name"`:
2012-02-20 01:33:12 +11:00
// HIP record slice of name (or none)
2012-02-23 08:11:40 +11:00
servers := make([]string, 0)
var s string
for off < lenmsg {
s, off, err = UnpackDomainName(msg, off)
if err != nil {
return lenmsg, err
2012-02-23 08:11:40 +11:00
}
servers = append(servers, s)
}
2012-02-20 01:33:12 +11:00
fv.Set(reflect.ValueOf(servers))
case `dns:"txt"`:
2012-02-20 01:33:12 +11:00
txt := make([]string, 0)
2012-02-23 08:11:40 +11:00
rdlength := off + int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
2012-02-20 01:33:12 +11:00
Txts:
if off == lenmsg { // dyn. updates, no rdata is OK
break
}
2012-02-20 01:33:12 +11:00
l := int(msg[off])
if off+l+1 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking txt"}
2012-02-20 01:33:12 +11:00
}
txt = append(txt, string(msg[off+1:off+l+1]))
off += l + 1
if off < rdlength {
// More
goto Txts
}
fv.Set(reflect.ValueOf(txt))
case `dns:"opt"`: // edns0
2012-03-07 05:28:34 +11:00
rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
2013-01-23 02:41:41 +11:00
endrr := off + rdlength
2012-03-07 05:28:34 +11:00
if rdlength == 0 {
2012-02-14 02:12:26 +11:00
// This is an EDNS0 (OPT Record) with no rdata
// We can safely return here.
2011-01-09 09:47:24 +11:00
break
}
edns := make([]EDNS0, 0)
Option:
code := uint16(0)
if off+2 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking opt"}
}
code, off = unpackUint16(msg, off)
optlen, off1 := unpackUint16(msg, off)
2012-03-07 05:28:34 +11:00
if off1+int(optlen) > off+rdlength {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking opt"}
}
switch code {
2012-06-01 03:33:12 +10:00
case EDNS0NSID:
e := new(EDNS0_NSID)
2012-09-09 21:24:21 +10:00
e.unpack(msg[off1 : off1+int(optlen)])
edns = append(edns, e)
off = off1 + int(optlen)
2012-06-01 03:33:12 +10:00
case EDNS0SUBNET:
2012-06-01 05:40:07 +10:00
e := new(EDNS0_SUBNET)
2012-09-09 21:24:21 +10:00
e.unpack(msg[off1 : off1+int(optlen)])
2012-06-01 05:40:07 +10:00
edns = append(edns, e)
off = off1 + int(optlen)
case EDNS0UL:
e := new(EDNS0_UL)
e.unpack(msg[off1 : off1+int(optlen)])
edns = append(edns, e)
off = off1 + int(optlen)
case EDNS0LLQ:
e := new(EDNS0_LLQ)
e.unpack(msg[off1 : off1+int(optlen)])
edns = append(edns, e)
off = off1 + int(optlen)
2013-05-12 05:02:17 +10:00
case EDNS0DAU:
e := new(EDNS0_DAU)
e.unpack(msg[off1 : off1+int(optlen)])
edns = append(edns, e)
off = off1 + int(optlen)
case EDNS0DHU:
e := new(EDNS0_DHU)
e.unpack(msg[off1 : off1+int(optlen)])
edns = append(edns, e)
off = off1 + int(optlen)
case EDNS0N3U:
e := new(EDNS0_N3U)
e.unpack(msg[off1 : off1+int(optlen)])
edns = append(edns, e)
off = off1 + int(optlen)
2013-01-23 02:41:41 +11:00
default:
// do nothing?
off = off1 + int(optlen)
}
2013-01-23 02:41:41 +11:00
if off < endrr {
goto Option
}
fv.Set(reflect.ValueOf(edns))
case `dns:"a"`:
if off == lenmsg {
break // dyn. update
}
2012-10-11 22:06:27 +11:00
if off+net.IPv4len > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking a"}
}
fv.Set(reflect.ValueOf(net.IPv4(msg[off], msg[off+1], msg[off+2], msg[off+3])))
off += net.IPv4len
case `dns:"aaaa"`:
if off == lenmsg {
break
}
if off+net.IPv6len > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking aaaa"}
}
fv.Set(reflect.ValueOf(net.IP{msg[off], msg[off+1], msg[off+2], msg[off+3], msg[off+4],
msg[off+5], msg[off+6], msg[off+7], msg[off+8], msg[off+9], msg[off+10],
msg[off+11], msg[off+12], msg[off+13], msg[off+14], msg[off+15]}))
off += net.IPv6len
case `dns:"wks"`:
2012-06-02 06:23:21 +10:00
// Rest of the record is the bitmap
rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
endrr := rdstart + rdlength
serv := make([]uint16, 0)
2012-06-03 01:33:49 +10:00
j := 0
2012-06-02 06:23:21 +10:00
for off < endrr {
2012-06-03 01:33:49 +10:00
b := msg[off]
// Check the bits one by one, and set the type
if b&0x80 == 0x80 {
serv = append(serv, uint16(j*8+0))
}
if b&0x40 == 0x40 {
serv = append(serv, uint16(j*8+1))
}
if b&0x20 == 0x20 {
serv = append(serv, uint16(j*8+2))
}
if b&0x10 == 0x10 {
serv = append(serv, uint16(j*8+3))
}
if b&0x8 == 0x8 {
serv = append(serv, uint16(j*8+4))
}
if b&0x4 == 0x4 {
serv = append(serv, uint16(j*8+5))
}
if b&0x2 == 0x2 {
serv = append(serv, uint16(j*8+6))
}
if b&0x1 == 0x1 {
serv = append(serv, uint16(j*8+7))
}
j++
off++
2012-06-02 06:23:21 +10:00
}
2012-06-03 01:33:49 +10:00
fv.Set(reflect.ValueOf(serv))
case `dns:"nsec"`: // NSEC/NSEC3
if off == lenmsg {
break
}
2012-06-02 06:23:21 +10:00
// Rest of the record is the type bitmap
2011-12-10 02:07:17 +11:00
rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
2012-03-18 17:43:12 +11:00
endrr := rdstart + rdlength
if off+2 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking nsecx"}
2011-01-10 08:02:25 +11:00
}
2011-12-10 02:07:17 +11:00
nsec := make([]uint16, 0)
length := 0
window := 0
for off+2 < endrr {
2011-12-10 02:07:17 +11:00
window = int(msg[off])
length = int(msg[off+1])
//println("off, windows, length, end", off, window, length, endrr)
2012-01-16 06:46:11 +11:00
if length == 0 {
// A length window of zero is strange. If there
// the window should not have been specified. Bail out
// println("dns: length == 0 when unpacking NSEC")
return lenmsg, ErrRdata
2012-01-16 06:46:11 +11:00
}
2012-01-16 03:18:02 +11:00
if length > 32 {
return lenmsg, ErrRdata
2011-01-10 08:02:25 +11:00
}
2011-12-10 02:07:17 +11:00
// Walk the bytes in the window - and check the bit settings...
2011-12-10 02:07:17 +11:00
off += 2
for j := 0; j < length; j++ {
b := msg[off+j]
// Check the bits one by one, and set the type
if b&0x80 == 0x80 {
nsec = append(nsec, uint16(window*256+j*8+0))
}
if b&0x40 == 0x40 {
nsec = append(nsec, uint16(window*256+j*8+1))
}
if b&0x20 == 0x20 {
nsec = append(nsec, uint16(window*256+j*8+2))
}
if b&0x10 == 0x10 {
nsec = append(nsec, uint16(window*256+j*8+3))
}
if b&0x8 == 0x8 {
nsec = append(nsec, uint16(window*256+j*8+4))
}
if b&0x4 == 0x4 {
nsec = append(nsec, uint16(window*256+j*8+5))
}
if b&0x2 == 0x2 {
nsec = append(nsec, uint16(window*256+j*8+6))
}
if b&0x1 == 0x1 {
nsec = append(nsec, uint16(window*256+j*8+7))
}
2011-01-10 08:02:25 +11:00
}
2011-12-10 02:07:17 +11:00
off += length
2011-01-10 08:02:25 +11:00
}
fv.Set(reflect.ValueOf(nsec))
2010-08-04 07:57:59 +10:00
}
case reflect.Struct:
off, err = unpackStructValue(fv, msg, off)
if err != nil {
return lenmsg, err
}
2012-03-18 17:43:12 +11:00
if val.Type().Field(i).Name == "Hdr" {
rdstart = off
}
2011-08-08 22:09:05 +10:00
case reflect.Uint8:
if off == lenmsg {
break
}
if off+1 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking uint8"}
2011-08-08 22:09:05 +10:00
}
fv.SetUint(uint64(uint8(msg[off])))
2011-08-08 22:09:05 +10:00
off++
case reflect.Uint16:
if off == lenmsg {
break
}
2011-08-08 22:09:05 +10:00
var i uint16
if off+2 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking uint16"}
2011-08-08 22:09:05 +10:00
}
i, off = unpackUint16(msg, off)
fv.SetUint(uint64(i))
case reflect.Uint32:
if off == lenmsg {
break
}
if off+4 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking uint32"}
2011-08-08 22:09:05 +10:00
}
fv.SetUint(uint64(uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])))
2011-08-08 22:09:05 +10:00
off += 4
case reflect.Uint64:
switch val.Type().Field(i).Tag {
default:
if off+8 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking uint64"}
}
fv.SetUint(uint64(uint64(msg[off])<<56 | uint64(msg[off+1])<<48 | uint64(msg[off+2])<<40 |
uint64(msg[off+3])<<32 | uint64(msg[off+4])<<24 | uint64(msg[off+5])<<16 | uint64(msg[off+6])<<8 | uint64(msg[off+7])))
off += 8
case `dns:"uint48"`:
// Used in TSIG where the last 48 bits are occupied, so for now, assume a uint48 (6 bytes)
if off+6 > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking uint64 as uint48"}
}
fv.SetUint(uint64(uint64(msg[off])<<40 | uint64(msg[off+1])<<32 | uint64(msg[off+2])<<24 | uint64(msg[off+3])<<16 |
uint64(msg[off+4])<<8 | uint64(msg[off+5])))
off += 6
2011-08-08 22:09:05 +10:00
}
case reflect.String:
2010-08-04 07:57:59 +10:00
var s string
if off == lenmsg {
break
}
switch val.Type().Field(i).Tag {
2010-08-04 07:57:59 +10:00
default:
2013-06-20 16:25:29 +10:00
return lenmsg, &Error{"bad tag unpacking string: " + val.Type().Field(i).Tag.Get("dns")}
case `dns:"hex"`:
// Rest of the RR is hex encoded, network order an issue here?
rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
2012-03-18 17:43:12 +11:00
endrr := rdstart + rdlength
if endrr > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking hex"}
2012-01-24 06:29:47 +11:00
}
s = hex.EncodeToString(msg[off:endrr])
2012-03-18 17:43:12 +11:00
off = endrr
case `dns:"base64"`:
2010-08-04 07:57:59 +10:00
// Rest of the RR is base64 encoded value
rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
2012-03-18 17:43:12 +11:00
endrr := rdstart + rdlength
if endrr > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking base64"}
}
s = unpackBase64(msg[off:endrr])
2012-03-18 17:43:12 +11:00
off = endrr
case `dns:"cdomain-name"`:
fallthrough
case `dns:"domain-name"`:
if off == lenmsg {
// zero rdata foo, OK for dyn. updates
break
}
s, off, err = UnpackDomainName(msg, off)
if err != nil {
return lenmsg, err
2011-01-26 01:09:30 +11:00
}
case `dns:"size-base32"`:
2011-02-04 06:39:43 +11:00
var size int
switch val.Type().Name() {
2012-12-10 06:16:46 +11:00
case "NSEC3":
switch val.Type().Field(i).Name {
2011-02-04 06:39:43 +11:00
case "NextDomain":
name := val.FieldByName("HashLength")
size = int(name.Uint())
2011-02-04 06:39:43 +11:00
}
}
if off+size > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking base32"}
2011-02-04 06:39:43 +11:00
}
s = unpackBase32(msg[off : off+size])
off += size
case `dns:"size-hex"`:
// a "size" string, but it must be encoded in hex in the string
2011-02-04 06:39:43 +11:00
var size int
switch val.Type().Name() {
2012-12-10 06:16:46 +11:00
case "NSEC3":
switch val.Type().Field(i).Name {
2011-02-04 06:39:43 +11:00
case "Salt":
name := val.FieldByName("SaltLength")
size = int(name.Uint())
2011-02-04 06:39:43 +11:00
case "NextDomain":
name := val.FieldByName("HashLength")
size = int(name.Uint())
2011-02-04 06:39:43 +11:00
}
2012-12-10 06:16:46 +11:00
case "TSIG":
switch val.Type().Field(i).Name {
case "MAC":
name := val.FieldByName("MACSize")
size = int(name.Uint())
case "OtherData":
name := val.FieldByName("OtherLen")
size = int(name.Uint())
}
}
if off+size > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking hex"}
2011-02-04 06:39:43 +11:00
}
s = hex.EncodeToString(msg[off : off+size])
off += size
case `dns:"txt"`:
// 1 txt piece
rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint())
Txt:
if off >= lenmsg || off+1+int(msg[off]) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking txt"}
}
n := int(msg[off])
off++
for i := 0; i < n; i++ {
2011-08-09 18:29:30 +10:00
s += string(msg[off+i])
}
off += n
if off < rdlength {
// More to
goto Txt
}
2010-08-04 07:57:59 +10:00
case "":
if off >= lenmsg || off+1+int(msg[off]) > lenmsg {
2013-06-20 16:27:28 +10:00
return lenmsg, &Error{err: "overflow unpacking string"}
2010-08-04 07:57:59 +10:00
}
n := int(msg[off])
off++
for i := 0; i < n; i++ {
2011-08-09 18:29:30 +10:00
s += string(msg[off+i])
2010-08-04 07:57:59 +10:00
}
off += n
}
fv.SetString(s)
2010-08-04 07:57:59 +10:00
}
}
return off, nil
2010-08-04 07:57:59 +10:00
}
2010-12-25 23:09:27 +11:00
// Helper function for unpacking
func unpackUint16(msg []byte, off int) (v uint16, off1 int) {
v = uint16(msg[off])<<8 | uint16(msg[off+1])
off1 = off + 2
return
2010-12-25 23:09:27 +11:00
}
2013-05-07 06:23:13 +10:00
// UnpackStruct unpacks a binary message from offset off to the interface
// value given.
func UnpackStruct(any interface{}, msg []byte, off int) (off1 int, err error) {
off, err = unpackStructValue(structValue(any), msg, off)
return off, err
2010-08-04 07:57:59 +10:00
}
2011-02-04 06:39:43 +11:00
func unpackBase32(b []byte) string {
b32 := make([]byte, base32.HexEncoding.EncodedLen(len(b)))
base32.HexEncoding.Encode(b32, b)
return string(b32)
}
func unpackBase64(b []byte) string {
b64 := make([]byte, base64.StdEncoding.EncodedLen(len(b)))
base64.StdEncoding.Encode(b64, b)
return string(b64)
}
// Helper function for packing
func packUint16(i uint16) (byte, byte) {
2011-03-24 19:24:49 +11:00
return byte(i >> 8), byte(i)
}
2011-11-03 09:06:54 +11:00
func packBase64(s []byte) ([]byte, error) {
b64len := base64.StdEncoding.DecodedLen(len(s))
buf := make([]byte, b64len)
n, err := base64.StdEncoding.Decode(buf, []byte(s))
if err != nil {
return nil, err
}
buf = buf[:n]
return buf, nil
}
2011-02-04 06:39:43 +11:00
// Helper function for packing, mostly used in dnssec.go
2011-11-03 09:06:54 +11:00
func packBase32(s []byte) ([]byte, error) {
2011-02-04 06:39:43 +11:00
b32len := base32.HexEncoding.DecodedLen(len(s))
buf := make([]byte, b32len)
n, err := base32.HexEncoding.Decode(buf, []byte(s))
if err != nil {
return nil, err
}
buf = buf[:n]
return buf, nil
}
2012-12-02 20:14:53 +11:00
// Resource record packer, pack rr into msg[off:]. See PackDomainName for documentation
// about the compression.
func PackRR(rr RR, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) {
2011-01-02 05:51:25 +11:00
if rr == nil {
2013-06-20 16:27:28 +10:00
return len(msg), &Error{err: "nil rr"}
2011-01-02 05:51:25 +11:00
}
2010-12-31 03:15:59 +11:00
off1, err = packStructCompress(rr, msg, off, compression, compress)
if err != nil {
return len(msg), err
}
rawSetRdlength(msg, off, off1)
return off1, nil
2010-08-04 07:57:59 +10:00
}
2012-12-02 20:14:53 +11:00
// Resource record unpacker, unpack msg[off:] into an RR.
func UnpackRR(msg []byte, off int) (rr RR, off1 int, err error) {
2010-08-04 07:57:59 +10:00
// unpack just the header, to find the rr type and length
var h RR_Header
off0 := off
if off, err = UnpackStruct(&h, msg, off); err != nil {
return nil, len(msg), err
2010-08-04 07:57:59 +10:00
}
end := off + int(h.Rdlength)
// make an rr of that type and re-unpack.
2011-08-04 23:13:10 +10:00
mk, known := rr_mk[h.Rrtype]
if !known {
2012-12-10 06:16:46 +11:00
rr = new(RFC3597)
2011-02-22 01:44:42 +11:00
} else {
rr = mk()
}
off, err = UnpackStruct(rr, msg, off0)
2010-08-04 07:57:59 +10:00
if off != end {
2013-06-20 16:27:28 +10:00
return &h, end, &Error{err: "bad rdlength"}
2010-08-04 07:57:59 +10:00
}
return rr, off, err
2010-08-04 07:57:59 +10:00
}
2011-01-18 07:10:48 +11:00
// Reverse a map
func reverseInt8(m map[uint8]string) map[string]uint8 {
n := make(map[string]uint8)
for u, s := range m {
n[s] = u
}
return n
}
2011-09-20 06:27:56 +10:00
func reverseInt16(m map[uint16]string) map[string]uint16 {
2011-01-26 01:09:30 +11:00
n := make(map[string]uint16)
for u, s := range m {
n[s] = u
}
return n
}
2011-09-20 06:27:56 +10:00
func reverseInt(m map[int]string) map[string]int {
n := make(map[string]int)
for u, s := range m {
n[s] = u
}
return n
}
2012-08-22 01:36:58 +10:00
// Convert a MsgHdr to a string, with dig-like headers:
//
//;; opcode: QUERY, status: NOERROR, id: 48404
//
2010-08-04 07:57:59 +10:00
//;; flags: qr aa rd ra;
func (h *MsgHdr) String() string {
if h == nil {
return "<nil> MsgHdr"
}
s := ";; opcode: " + OpcodeToString[h.Opcode]
s += ", status: " + RcodeToString[h.Rcode]
s += ", id: " + strconv.Itoa(int(h.Id)) + "\n"
2010-08-04 07:57:59 +10:00
s += ";; flags:"
if h.Response {
s += " qr"
}
if h.Authoritative {
s += " aa"
2010-08-04 07:57:59 +10:00
}
if h.Truncated {
s += " tc"
2010-08-04 07:57:59 +10:00
}
if h.RecursionDesired {
s += " rd"
2010-08-04 07:57:59 +10:00
}
if h.RecursionAvailable {
s += " ra"
2010-08-04 07:57:59 +10:00
}
if h.Zero { // Hmm
s += " z"
}
if h.AuthenticatedData {
s += " ad"
}
if h.CheckingDisabled {
s += " cd"
}
2010-08-04 07:57:59 +10:00
s += ";"
return s
}
2012-01-27 08:52:29 +11:00
// Pack packs a Msg: it is converted to to wire format.
// If the dns.Compress is true the message will be in compressed wire format.
func (dns *Msg) Pack() (msg []byte, err error) {
2010-08-04 07:57:59 +10:00
var dh Header
2012-10-10 07:06:35 +11:00
var compression map[string]int
if dns.Compress {
compression = make(map[string]int) // Compression pointer mappings
} else {
compression = nil
}
2010-08-04 07:57:59 +10:00
// Convert convenient Msg into wire-like Header.
dh.Id = dns.Id
dh.Bits = uint16(dns.Opcode)<<11 | uint16(dns.Rcode)
if dns.Response {
dh.Bits |= _QR
2010-08-04 07:57:59 +10:00
}
if dns.Authoritative {
dh.Bits |= _AA
2010-08-04 07:57:59 +10:00
}
if dns.Truncated {
2010-08-04 07:57:59 +10:00
dh.Bits |= _TC
}
if dns.RecursionDesired {
dh.Bits |= _RD
2010-08-04 07:57:59 +10:00
}
if dns.RecursionAvailable {
dh.Bits |= _RA
}
if dns.Zero {
dh.Bits |= _Z
}
if dns.AuthenticatedData {
dh.Bits |= _AD
}
if dns.CheckingDisabled {
dh.Bits |= _CD
2010-08-04 07:57:59 +10:00
}
// Prepare variable sized arrays.
question := dns.Question
answer := dns.Answer
ns := dns.Ns
extra := dns.Extra
2010-08-04 07:57:59 +10:00
dh.Qdcount = uint16(len(question))
dh.Ancount = uint16(len(answer))
dh.Nscount = uint16(len(ns))
dh.Arcount = uint16(len(extra))
2013-06-29 18:50:43 +10:00
msg = make([]byte, dns.packLen()+1)
2010-08-04 07:57:59 +10:00
// Pack it in: header and then the pieces.
off := 0
off, err = packStructCompress(&dh, msg, off, compression, dns.Compress)
if err != nil {
return nil, err
}
2010-08-04 07:57:59 +10:00
for i := 0; i < len(question); i++ {
off, err = packStructCompress(&question[i], msg, off, compression, dns.Compress)
if err != nil {
return nil, err
}
2010-08-04 07:57:59 +10:00
}
for i := 0; i < len(answer); i++ {
off, err = PackRR(answer[i], msg, off, compression, dns.Compress)
if err != nil {
return nil, err
}
2010-08-04 07:57:59 +10:00
}
for i := 0; i < len(ns); i++ {
off, err = PackRR(ns[i], msg, off, compression, dns.Compress)
if err != nil {
return nil, err
}
2010-08-04 07:57:59 +10:00
}
for i := 0; i < len(extra); i++ {
off, err = PackRR(extra[i], msg, off, compression, dns.Compress)
if err != nil {
return nil, err
}
2010-08-04 07:57:59 +10:00
}
return msg[:off], nil
2010-08-04 07:57:59 +10:00
}
2012-01-27 08:52:29 +11:00
// Unpack unpacks a binary message to a Msg structure.
func (dns *Msg) Unpack(msg []byte) (err error) {
2010-08-04 07:57:59 +10:00
// Header.
var dh Header
off := 0
if off, err = UnpackStruct(&dh, msg, off); err != nil {
return err
2010-08-04 07:57:59 +10:00
}
dns.Id = dh.Id
dns.Response = (dh.Bits & _QR) != 0
dns.Opcode = int(dh.Bits>>11) & 0xF
dns.Authoritative = (dh.Bits & _AA) != 0
dns.Truncated = (dh.Bits & _TC) != 0
dns.RecursionDesired = (dh.Bits & _RD) != 0
dns.RecursionAvailable = (dh.Bits & _RA) != 0
dns.Zero = (dh.Bits & _Z) != 0
dns.AuthenticatedData = (dh.Bits & _AD) != 0
dns.CheckingDisabled = (dh.Bits & _CD) != 0
dns.Rcode = int(dh.Bits & 0xF)
2010-08-04 07:57:59 +10:00
// Arrays.
dns.Question = make([]Question, dh.Qdcount)
dns.Answer = make([]RR, dh.Ancount)
dns.Ns = make([]RR, dh.Nscount)
dns.Extra = make([]RR, dh.Arcount)
2010-08-04 07:57:59 +10:00
for i := 0; i < len(dns.Question); i++ {
off, err = UnpackStruct(&dns.Question[i], msg, off)
if err != nil {
return err
}
2010-08-04 07:57:59 +10:00
}
for i := 0; i < len(dns.Answer); i++ {
dns.Answer[i], off, err = UnpackRR(msg, off)
if err != nil {
return err
}
2010-08-04 07:57:59 +10:00
}
for i := 0; i < len(dns.Ns); i++ {
dns.Ns[i], off, err = UnpackRR(msg, off)
if err != nil {
return err
}
2010-08-04 07:57:59 +10:00
}
for i := 0; i < len(dns.Extra); i++ {
dns.Extra[i], off, err = UnpackRR(msg, off)
if err != nil {
return err
}
2010-08-04 07:57:59 +10:00
}
if off != len(msg) {
2013-06-20 16:25:29 +10:00
// TODO(miek) make this an error?
2013-06-29 18:50:43 +10:00
// use PackOpt to let people tell how detailed the error reporting should be?
2013-06-20 16:25:29 +10:00
// println("dns: extra bytes in dns packet", off, "<", len(msg))
2010-08-04 07:57:59 +10:00
}
return nil
2010-08-04 07:57:59 +10:00
}
2011-01-27 19:29:11 +11:00
// Convert a complete message to a string with dig-like output.
2010-08-04 07:57:59 +10:00
func (dns *Msg) String() string {
2010-10-07 17:46:46 +11:00
if dns == nil {
return "<nil> MsgHdr"
}
2010-08-04 07:57:59 +10:00
s := dns.MsgHdr.String() + " "
s += "QUERY: " + strconv.Itoa(len(dns.Question)) + ", "
s += "ANSWER: " + strconv.Itoa(len(dns.Answer)) + ", "
s += "AUTHORITY: " + strconv.Itoa(len(dns.Ns)) + ", "
s += "ADDITIONAL: " + strconv.Itoa(len(dns.Extra)) + "\n"
if len(dns.Question) > 0 {
2010-08-04 07:57:59 +10:00
s += "\n;; QUESTION SECTION:\n"
for i := 0; i < len(dns.Question); i++ {
2011-03-24 19:24:49 +11:00
s += dns.Question[i].String() + "\n"
2010-08-04 07:57:59 +10:00
}
}
if len(dns.Answer) > 0 {
2010-08-04 07:57:59 +10:00
s += "\n;; ANSWER SECTION:\n"
for i := 0; i < len(dns.Answer); i++ {
if dns.Answer[i] != nil {
s += dns.Answer[i].String() + "\n"
}
2010-08-04 07:57:59 +10:00
}
}
if len(dns.Ns) > 0 {
2010-08-04 07:57:59 +10:00
s += "\n;; AUTHORITY SECTION:\n"
for i := 0; i < len(dns.Ns); i++ {
if dns.Ns[i] != nil {
s += dns.Ns[i].String() + "\n"
}
2010-08-04 07:57:59 +10:00
}
}
if len(dns.Extra) > 0 {
2010-08-04 07:57:59 +10:00
s += "\n;; ADDITIONAL SECTION:\n"
for i := 0; i < len(dns.Extra); i++ {
if dns.Extra[i] != nil {
s += dns.Extra[i].String() + "\n"
}
2010-08-04 07:57:59 +10:00
}
}
return s
}
// packLen returns the message length when in UNcompressed wire format.
func (dns *Msg) packLen() int {
// Message header is always 12 bytes
l := 12
for i := 0; i < len(dns.Question); i++ {
l += dns.Question[i].len()
}
for i := 0; i < len(dns.Answer); i++ {
l += dns.Answer[i].len()
}
for i := 0; i < len(dns.Ns); i++ {
l += dns.Ns[i].len()
}
for i := 0; i < len(dns.Extra); i++ {
l += dns.Extra[i].len()
}
return l
}
// Len returns the message length when in (un)compressed wire format.
// If dns.Compress is true compression it is taken into account. Len()
// is provided to be a faster way to get the size of the resulting packet,
// than packing it, measuring the size and discarding the buffer.
func (dns *Msg) Len() int {
// Message header is always 12 bytes
l := 12
var compression map[string]int
if dns.Compress {
compression = make(map[string]int)
}
2012-08-26 06:40:32 +10:00
2012-02-29 06:43:08 +11:00
for i := 0; i < len(dns.Question); i++ {
l += dns.Question[i].len()
if dns.Compress {
compressionLenHelper(compression, dns.Question[i].Name)
}
}
2012-02-29 06:43:08 +11:00
for i := 0; i < len(dns.Answer); i++ {
l += dns.Answer[i].len()
if dns.Compress {
k, ok := compressionLenSearch(compression, dns.Answer[i].Header().Name)
if ok {
l += 1 - k
} else {
compressionLenHelper(compression, dns.Answer[i].Header().Name)
}
l += 1 - compressionLenType(compression, dns.Answer[i])
}
}
2012-02-29 06:43:08 +11:00
for i := 0; i < len(dns.Ns); i++ {
l += dns.Ns[i].len()
if dns.Compress {
k, ok := compressionLenSearch(compression, dns.Ns[i].Header().Name)
if ok {
l += 1 - k
} else {
compressionLenHelper(compression, dns.Ns[i].Header().Name)
}
l += 1 - compressionLenType(compression, dns.Ns[i])
}
}
2012-02-29 06:43:08 +11:00
for i := 0; i < len(dns.Extra); i++ {
if dns.Compress {
k, ok := compressionLenSearch(compression, dns.Extra[i].Header().Name)
if ok {
l += 1 - k
} else {
compressionLenHelper(compression, dns.Extra[i].Header().Name)
}
l += 1 - compressionLenType(compression, dns.Extra[i])
}
}
return l
}
func (dns *Msg) copy() *Msg {
r1 := new(Msg)
r1.MsgHdr = dns.MsgHdr
r1.Compress = dns.Compress
r1.Question = make([]Question, len(dns.Question))
r1.Answer = make([]RR, len(dns.Answer))
r1.Ns = make([]RR, len(dns.Ns))
r1.Extra = make([]RR, len(dns.Extra))
copy(r1.Question, dns.Question)
copy(r1.Answer, dns.Answer)
copy(r1.Ns, dns.Ns)
copy(r1.Extra, dns.Extra)
return r1
}
// Put the parts of the name in the compression map.
func compressionLenHelper(c map[string]int, s string) {
pref := ""
lbs := SplitDomainName(s)
for j := len(lbs) - 1; j >= 0; j-- {
c[lbs[j]+"."+pref] = 1 + len(pref) + len(lbs[j])
pref = lbs[j] + "." + pref
}
}
// Look for each part in the compression map and returns its length
func compressionLenSearch(c map[string]int, s string) (int, bool) {
off := 0
end := false
for {
if end {
break
}
if _, ok := c[s[off:]]; ok {
return len(s[off:]), true
}
off, end = NextLabel(s, off)
}
// TODO(miek): not sure if need, leave this for later debugging
if _, ok := c[s[off:]]; ok {
return len(s[off:]), true
}
return 0, false
}
// Check the ownernames too of the types that have cdomain, do
// this manually to avoid reflection.
func compressionLenType(c map[string]int, r RR) int {
switch x := r.(type) {
case *NS:
k, ok := compressionLenSearch(c, x.Ns)
if ok {
return k
} else {
compressionLenHelper(c, x.Ns)
}
case *MX:
k, ok := compressionLenSearch(c, x.Mx)
if ok {
return k
} else {
compressionLenHelper(c, x.Mx)
}
case *CNAME:
k, ok := compressionLenSearch(c, x.Target)
if ok {
return k
} else {
compressionLenHelper(c, x.Target)
}
case *PTR:
k, ok := compressionLenSearch(c, x.Ptr)
if ok {
return k
} else {
compressionLenHelper(c, x.Ptr)
}
case *SOA:
k, ok := compressionLenSearch(c, x.Ns)
if ok {
return k
} else {
compressionLenHelper(c, x.Ns)
}
k, ok = compressionLenSearch(c, x.Mbox)
if ok {
return k
} else {
compressionLenHelper(c, x.Mbox)
}
case *MB:
k, ok := compressionLenSearch(c, x.Mb)
if ok {
return k
} else {
compressionLenHelper(c, x.Mb)
}
case *MG:
k, ok := compressionLenSearch(c, x.Mg)
if ok {
return k
} else {
compressionLenHelper(c, x.Mg)
}
case *MR:
k, ok := compressionLenSearch(c, x.Mr)
if ok {
return k
} else {
compressionLenHelper(c, x.Mr)
}
case *MF:
k, ok := compressionLenSearch(c, x.Mf)
if ok {
return k
} else {
compressionLenHelper(c, x.Mf)
}
case *MD:
k, ok := compressionLenSearch(c, x.Md)
if ok {
return k
} else {
compressionLenHelper(c, x.Md)
}
case *RT:
k, ok := compressionLenSearch(c, x.Host)
if ok {
return k
} else {
compressionLenHelper(c, x.Host)
}
case *MINFO:
k, ok := compressionLenSearch(c, x.Rmail)
if ok {
return k
} else {
compressionLenHelper(c, x.Rmail)
}
k, ok = compressionLenSearch(c, x.Email)
if ok {
return k
} else {
compressionLenHelper(c, x.Email)
}
case *AFSDB:
k, ok := compressionLenSearch(c, x.Hostname)
if ok {
return k
} else {
compressionLenHelper(c, x.Hostname)
}
}
return 1 // noop when nothing is found
}
// Id return a 16 bits random number to be used as a
2011-03-24 05:37:07 +11:00
// message id. The random provided should be good enough.
func Id() uint16 {
return uint16(rand.Int()) ^ uint16(time.Now().Nanosecond())
}