dns/types.go

1250 lines
26 KiB
Go
Raw Normal View History

2010-08-04 07:57:59 +10:00
package dns
import (
"fmt"
2010-08-04 07:57:59 +10:00
"net"
"strconv"
2010-12-30 02:11:23 +11:00
"strings"
"time"
2010-08-04 07:57:59 +10:00
)
type (
// Type is a DNS type.
Type uint16
// Class is a DNS class.
Class uint16
// Name is a DNS domain name.
Name string
)
2010-08-04 07:57:59 +10:00
// Packet formats
2011-01-27 19:29:11 +11:00
// Wire constants and supported types.
2010-08-04 07:57:59 +10:00
const (
// valid RR_Header.Rrtype and Question.qtype
2015-02-20 22:39:15 +11:00
TypeNone uint16 = 0
2012-02-19 06:07:25 +11:00
TypeA uint16 = 1
TypeNS uint16 = 2
TypeMD uint16 = 3
TypeMF uint16 = 4
TypeCNAME uint16 = 5
TypeSOA uint16 = 6
TypeMB uint16 = 7
TypeMG uint16 = 8
TypeMR uint16 = 9
TypeNULL uint16 = 10
TypePTR uint16 = 12
TypeHINFO uint16 = 13
TypeMINFO uint16 = 14
TypeMX uint16 = 15
TypeTXT uint16 = 16
2012-05-02 06:57:22 +10:00
TypeRP uint16 = 17
2012-06-01 21:34:14 +10:00
TypeAFSDB uint16 = 18
2012-11-21 00:32:06 +11:00
TypeX25 uint16 = 19
2012-11-21 00:14:00 +11:00
TypeISDN uint16 = 20
2012-08-10 17:22:50 +10:00
TypeRT uint16 = 21
2013-10-13 22:25:08 +11:00
TypeNSAPPTR uint16 = 23
2011-08-22 22:11:41 +10:00
TypeSIG uint16 = 24
TypeKEY uint16 = 25
2013-10-13 23:23:02 +11:00
TypePX uint16 = 26
2013-10-13 23:01:33 +11:00
TypeGPOS uint16 = 27
2012-02-19 06:07:25 +11:00
TypeAAAA uint16 = 28
TypeLOC uint16 = 29
2011-08-22 22:11:41 +10:00
TypeNXT uint16 = 30
2013-10-20 07:31:12 +11:00
TypeEID uint16 = 31
TypeNIMLOC uint16 = 32
2012-02-19 06:07:25 +11:00
TypeSRV uint16 = 33
2012-11-21 00:32:06 +11:00
TypeATMA uint16 = 34
2012-02-19 06:07:25 +11:00
TypeNAPTR uint16 = 35
TypeKX uint16 = 36
TypeCERT uint16 = 37
TypeDNAME uint16 = 39
TypeOPT uint16 = 41 // EDNS
2011-08-22 22:11:41 +10:00
TypeDS uint16 = 43
TypeSSHFP uint16 = 44
TypeRRSIG uint16 = 46
TypeNSEC uint16 = 47
TypeDNSKEY uint16 = 48
TypeDHCID uint16 = 49
TypeNSEC3 uint16 = 50
TypeNSEC3PARAM uint16 = 51
2012-04-14 18:44:32 +10:00
TypeTLSA uint16 = 52
2012-02-19 06:07:25 +11:00
TypeHIP uint16 = 55
2012-11-21 02:42:16 +11:00
TypeNINFO uint16 = 56
2012-11-21 02:48:28 +11:00
TypeRKEY uint16 = 57
2011-08-22 22:11:41 +10:00
TypeTALINK uint16 = 58
2012-11-21 02:52:18 +11:00
TypeCDS uint16 = 59
2014-10-24 08:18:23 +11:00
TypeCDNSKEY uint16 = 60
2014-08-14 18:18:08 +10:00
TypeOPENPGPKEY uint16 = 61
2011-08-22 22:11:41 +10:00
TypeSPF uint16 = 99
TypeUINFO uint16 = 100
TypeUID uint16 = 101
TypeGID uint16 = 102
TypeUNSPEC uint16 = 103
2012-11-18 07:26:48 +11:00
TypeNID uint16 = 104
TypeL32 uint16 = 105
TypeL64 uint16 = 106
TypeLP uint16 = 107
TypeEUI48 uint16 = 108
TypeEUI64 uint16 = 109
2015-08-23 21:55:01 +10:00
TypeURI uint16 = 256
TypeCAA uint16 = 257
2011-08-22 22:11:41 +10:00
TypeTKEY uint16 = 249
TypeTSIG uint16 = 250
2015-02-20 22:39:15 +11:00
2011-03-25 21:19:35 +11:00
// valid Question.Qtype only
2011-08-22 22:11:41 +10:00
TypeIXFR uint16 = 251
TypeAXFR uint16 = 252
TypeMAILB uint16 = 253
TypeMAILA uint16 = 254
TypeANY uint16 = 255
2012-09-02 01:06:24 +10:00
2013-10-13 23:27:50 +11:00
TypeTA uint16 = 32768
TypeDLV uint16 = 32769
TypeReserved uint16 = 65535
2011-02-21 23:16:07 +11:00
2011-03-25 21:19:35 +11:00
// valid Question.Qclass
2010-08-04 07:57:59 +10:00
ClassINET = 1
ClassCSNET = 2
ClassCHAOS = 3
ClassHESIOD = 4
2011-07-23 17:21:24 +10:00
ClassNONE = 254
2010-08-04 07:57:59 +10:00
ClassANY = 255
2015-08-23 21:55:01 +10:00
// Message Response Codes.
2010-08-04 07:57:59 +10:00
RcodeSuccess = 0
RcodeFormatError = 1
RcodeServerFailure = 2
RcodeNameError = 3
RcodeNotImplemented = 4
RcodeRefused = 5
2011-01-10 06:46:21 +11:00
RcodeYXDomain = 6
RcodeYXRrset = 7
RcodeNXRrset = 8
RcodeNotAuth = 9
RcodeNotZone = 10
2011-01-27 01:13:06 +11:00
RcodeBadSig = 16 // TSIG
RcodeBadVers = 16 // EDNS0
2011-01-27 01:13:06 +11:00
RcodeBadKey = 17
RcodeBadTime = 18
RcodeBadMode = 19 // TKEY
RcodeBadName = 20
RcodeBadAlg = 21
RcodeBadTrunc = 22 // TSIG
2016-06-12 22:10:24 +10:00
RcodeBadCookie = 23 // DNS Cookies
2011-01-02 05:51:25 +11:00
2015-08-23 21:55:01 +10:00
// Message Opcodes. There is no 3.
2011-01-02 05:51:25 +11:00
OpcodeQuery = 0
OpcodeIQuery = 1
OpcodeStatus = 2
OpcodeNotify = 4
2011-01-02 06:51:34 +11:00
OpcodeUpdate = 5
2010-08-04 07:57:59 +10:00
)
2015-08-23 19:21:18 +10:00
// Headers is the wire format for the DNS packet header.
2010-08-04 07:57:59 +10:00
type Header struct {
Id uint16
Bits uint16
Qdcount, Ancount, Nscount, Arcount uint16
}
const (
headerSize = 12
2010-08-04 07:57:59 +10:00
// Header.Bits
_QR = 1 << 15 // query/response (response=1)
_AA = 1 << 10 // authoritative
_TC = 1 << 9 // truncated
_RD = 1 << 8 // recursion desired
_RA = 1 << 7 // recursion available
_Z = 1 << 6 // Z
_AD = 1 << 5 // authticated data
_CD = 1 << 4 // checking disabled
LOC_EQUATOR = 1 << 31 // RFC 1876, Section 2.
LOC_PRIMEMERIDIAN = 1 << 31 // RFC 1876, Section 2.
LOC_HOURS = 60 * 1000
LOC_DEGREES = 60 * LOC_HOURS
2010-08-04 07:57:59 +10:00
LOC_ALTITUDEBASE = 100000
)
2015-08-23 19:21:18 +10:00
// Different Certificate Types, see RFC 4398, Section 2.1
const (
CertPKIX = 1 + iota
CertSPKI
CertPGP
CertIPIX
CertISPKI
CertIPGP
CertACPKIX
CertIACPKIX
CertURI = 253
CertOID = 254
)
2015-08-23 19:21:18 +10:00
// CertTypeToString converts the Cert Type to its string representation.
// See RFC 4398 and RFC 6944.
var CertTypeToString = map[uint16]string{
CertPKIX: "PKIX",
CertSPKI: "SPKI",
CertPGP: "PGP",
CertIPIX: "IPIX",
CertISPKI: "ISPKI",
CertIPGP: "IPGP",
CertACPKIX: "ACPKIX",
CertIACPKIX: "IACPKIX",
CertURI: "URI",
CertOID: "OID",
}
2015-08-23 19:21:18 +10:00
// StringToCertType is the reverseof CertTypeToString.
var StringToCertType = reverseInt16(CertTypeToString)
//go:generate go run types_generate.go
2015-02-20 22:42:29 +11:00
// Question holds a DNS question. There can be multiple questions in the
// question section of a message. Usually there is just one.
2010-08-04 07:57:59 +10:00
type Question struct {
Name string `dns:"cdomain-name"` // "cdomain-name" specifies encoding (and may be compressed)
2010-08-04 07:57:59 +10:00
Qtype uint16
Qclass uint16
}
func (q *Question) len() int {
return len(q.Name) + 1 + 2 + 2
}
2011-12-20 22:53:47 +11:00
func (q *Question) String() (s string) {
2010-08-04 07:57:59 +10:00
// prefix with ; (as in dig)
s = ";" + sprintName(q.Name) + "\t"
s += Class(q.Qclass).String() + "\t"
s += " " + Type(q.Qtype).String()
2010-08-04 07:57:59 +10:00
return s
}
2015-08-23 19:21:18 +10:00
// ANY is a wildcard record. See RFC 1035, Section 3.2.3. ANY
// is named "*" there.
type ANY struct {
2011-11-03 09:06:54 +11:00
Hdr RR_Header
// Does not have any rdata
2011-08-22 22:13:43 +10:00
}
func (rr *ANY) String() string { return rr.Hdr.String() }
type CNAME struct {
Hdr RR_Header
Target string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *CNAME) String() string { return rr.Hdr.String() + sprintName(rr.Target) }
type HINFO struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Cpu string
Os string
}
func (rr *HINFO) String() string {
return rr.Hdr.String() + sprintTxt([]string{rr.Cpu, rr.Os})
}
type MB struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Mb string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *MB) String() string { return rr.Hdr.String() + sprintName(rr.Mb) }
type MG struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Mg string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *MG) String() string { return rr.Hdr.String() + sprintName(rr.Mg) }
type MINFO struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Rmail string `dns:"cdomain-name"`
Email string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *MINFO) String() string {
return rr.Hdr.String() + sprintName(rr.Rmail) + " " + sprintName(rr.Email)
2010-08-04 07:57:59 +10:00
}
type MR struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Mr string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *MR) String() string {
return rr.Hdr.String() + sprintName(rr.Mr)
2010-08-04 07:57:59 +10:00
}
type MF struct {
2012-06-01 21:25:54 +10:00
Hdr RR_Header
2012-06-01 21:34:14 +10:00
Mf string `dns:"cdomain-name"`
2012-06-01 21:25:54 +10:00
}
func (rr *MF) String() string {
return rr.Hdr.String() + sprintName(rr.Mf)
2012-06-01 21:25:54 +10:00
}
type MD struct {
2012-06-01 21:25:54 +10:00
Hdr RR_Header
2012-06-01 21:34:14 +10:00
Md string `dns:"cdomain-name"`
2012-06-01 21:25:54 +10:00
}
func (rr *MD) String() string {
return rr.Hdr.String() + sprintName(rr.Md)
2012-06-01 21:25:54 +10:00
}
type MX struct {
2012-11-21 00:32:06 +11:00
Hdr RR_Header
Preference uint16
2012-11-21 00:32:06 +11:00
Mx string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *MX) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Mx)
2010-08-04 07:57:59 +10:00
}
type AFSDB struct {
2012-06-01 21:35:52 +10:00
Hdr RR_Header
Subtype uint16
Hostname string `dns:"cdomain-name"`
2012-06-01 21:34:14 +10:00
}
func (rr *AFSDB) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Subtype)) + " " + sprintName(rr.Hostname)
2012-06-01 21:34:14 +10:00
}
type X25 struct {
2012-11-21 00:32:06 +11:00
Hdr RR_Header
2012-11-21 00:07:16 +11:00
PSDNAddress string
}
func (rr *X25) String() string {
2012-11-21 00:07:16 +11:00
return rr.Hdr.String() + rr.PSDNAddress
}
type RT struct {
2012-08-10 17:22:50 +10:00
Hdr RR_Header
Preference uint16
Host string `dns:"cdomain-name"`
}
func (rr *RT) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Host)
2012-08-10 17:22:50 +10:00
}
type NS struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Ns string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *NS) String() string {
return rr.Hdr.String() + sprintName(rr.Ns)
2010-08-04 07:57:59 +10:00
}
type PTR struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Ptr string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *PTR) String() string {
return rr.Hdr.String() + sprintName(rr.Ptr)
2010-08-04 07:57:59 +10:00
}
type RP struct {
2012-05-02 06:57:22 +10:00
Hdr RR_Header
Mbox string `dns:"domain-name"`
Txt string `dns:"domain-name"`
}
func (rr *RP) String() string {
return rr.Hdr.String() + rr.Mbox + " " + sprintTxt([]string{rr.Txt})
2012-05-02 06:57:22 +10:00
}
type SOA struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Ns string `dns:"cdomain-name"`
Mbox string `dns:"cdomain-name"`
2010-08-04 07:57:59 +10:00
Serial uint32
Refresh uint32
Retry uint32
Expire uint32
Minttl uint32
}
func (rr *SOA) String() string {
return rr.Hdr.String() + sprintName(rr.Ns) + " " + sprintName(rr.Mbox) +
2012-02-20 04:36:59 +11:00
" " + strconv.FormatInt(int64(rr.Serial), 10) +
" " + strconv.FormatInt(int64(rr.Refresh), 10) +
" " + strconv.FormatInt(int64(rr.Retry), 10) +
" " + strconv.FormatInt(int64(rr.Expire), 10) +
" " + strconv.FormatInt(int64(rr.Minttl), 10)
2010-08-04 07:57:59 +10:00
}
type TXT struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Txt []string `dns:"txt"`
2010-08-04 07:57:59 +10:00
}
func (rr *TXT) String() string { return rr.Hdr.String() + sprintTxt(rr.Txt) }
func sprintName(s string) string {
src := []byte(s)
dst := make([]byte, 0, len(src))
for i := 0; i < len(src); {
if i+1 < len(src) && src[i] == '\\' && src[i+1] == '.' {
dst = append(dst, src[i:i+2]...)
i += 2
2012-02-19 06:07:25 +11:00
} else {
b, n := nextByte(src, i)
if n == 0 {
i++ // dangling back slash
} else if b == '.' {
dst = append(dst, b)
} else {
dst = appendDomainNameByte(dst, b)
}
i += n
2012-02-19 06:07:25 +11:00
}
}
return string(dst)
}
func sprintTxtOctet(s string) string {
src := []byte(s)
dst := make([]byte, 0, len(src))
dst = append(dst, '"')
for i := 0; i < len(src); {
if i+1 < len(src) && src[i] == '\\' && src[i+1] == '.' {
dst = append(dst, src[i:i+2]...)
i += 2
} else {
b, n := nextByte(src, i)
if n == 0 {
i++ // dangling back slash
} else if b == '.' {
dst = append(dst, b)
} else {
if b < ' ' || b > '~' {
dst = appendByte(dst, b)
} else {
dst = append(dst, b)
}
}
i += n
}
}
dst = append(dst, '"')
return string(dst)
}
func sprintTxt(txt []string) string {
var out []byte
for i, s := range txt {
if i > 0 {
out = append(out, ` "`...)
} else {
out = append(out, '"')
}
bs := []byte(s)
for j := 0; j < len(bs); {
b, n := nextByte(bs, j)
if n == 0 {
break
}
out = appendTXTStringByte(out, b)
j += n
}
out = append(out, '"')
}
return string(out)
}
func appendDomainNameByte(s []byte, b byte) []byte {
switch b {
case '.', ' ', '\'', '@', ';', '(', ')': // additional chars to escape
return append(s, '\\', b)
}
return appendTXTStringByte(s, b)
}
func appendTXTStringByte(s []byte, b byte) []byte {
switch b {
case '\t':
return append(s, '\\', 't')
case '\r':
return append(s, '\\', 'r')
case '\n':
return append(s, '\\', 'n')
case '"', '\\':
return append(s, '\\', b)
}
if b < ' ' || b > '~' {
return appendByte(s, b)
}
return append(s, b)
}
func appendByte(s []byte, b byte) []byte {
var buf [3]byte
bufs := strconv.AppendInt(buf[:0], int64(b), 10)
s = append(s, '\\')
for i := 0; i < 3-len(bufs); i++ {
s = append(s, '0')
}
for _, r := range bufs {
s = append(s, r)
}
return s
}
func nextByte(b []byte, offset int) (byte, int) {
2014-03-01 13:37:10 +11:00
if offset >= len(b) {
return 0, 0
}
if b[offset] != '\\' {
// not an escape sequence
return b[offset], 1
}
switch len(b) - offset {
case 1: // dangling escape
return 0, 0
case 2, 3: // too short to be \ddd
default: // maybe \ddd
if isDigit(b[offset+1]) && isDigit(b[offset+2]) && isDigit(b[offset+3]) {
return dddToByte(b[offset+1:]), 4
}
}
// not \ddd, maybe a control char
switch b[offset+1] {
case 't':
return '\t', 2
case 'r':
return '\r', 2
case 'n':
return '\n', 2
default:
return b[offset+1], 2
}
2010-08-04 07:57:59 +10:00
}
type SPF struct {
2012-02-19 04:59:19 +11:00
Hdr RR_Header
Txt []string `dns:"txt"`
2012-02-19 04:59:19 +11:00
}
func (rr *SPF) String() string { return rr.Hdr.String() + sprintTxt(rr.Txt) }
2012-02-19 04:59:19 +11:00
type SRV struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Priority uint16
Weight uint16
Port uint16
Target string `dns:"domain-name"`
2010-08-04 07:57:59 +10:00
}
func (rr *SRV) String() string {
2010-08-04 07:57:59 +10:00
return rr.Hdr.String() +
strconv.Itoa(int(rr.Priority)) + " " +
strconv.Itoa(int(rr.Weight)) + " " +
strconv.Itoa(int(rr.Port)) + " " + sprintName(rr.Target)
2010-08-04 07:57:59 +10:00
}
type NAPTR struct {
2010-12-31 06:50:31 +11:00
Hdr RR_Header
Order uint16
2012-11-21 00:32:06 +11:00
Preference uint16
2010-12-31 06:50:31 +11:00
Flags string
Service string
Regexp string
Replacement string `dns:"domain-name"`
2010-12-31 06:50:31 +11:00
}
func (rr *NAPTR) String() string {
2011-01-18 02:03:06 +11:00
return rr.Hdr.String() +
2011-01-17 20:30:20 +11:00
strconv.Itoa(int(rr.Order)) + " " +
strconv.Itoa(int(rr.Preference)) + " " +
2011-01-17 20:30:20 +11:00
"\"" + rr.Flags + "\" " +
"\"" + rr.Service + "\" " +
"\"" + rr.Regexp + "\" " +
rr.Replacement
2010-12-31 06:50:31 +11:00
}
2015-08-23 17:03:13 +10:00
// The CERT resource record, see RFC 4398.
type CERT struct {
Hdr RR_Header
Type uint16
KeyTag uint16
Algorithm uint8
Certificate string `dns:"base64"`
}
func (rr *CERT) String() string {
var (
ok bool
certtype, algorithm string
)
if certtype, ok = CertTypeToString[rr.Type]; !ok {
certtype = strconv.Itoa(int(rr.Type))
}
if algorithm, ok = AlgorithmToString[rr.Algorithm]; !ok {
algorithm = strconv.Itoa(int(rr.Algorithm))
}
return rr.Hdr.String() + certtype +
" " + strconv.Itoa(int(rr.KeyTag)) +
" " + algorithm +
" " + rr.Certificate
}
2015-08-23 17:03:13 +10:00
// The DNAME resource record, see RFC 2672.
type DNAME struct {
Hdr RR_Header
Target string `dns:"domain-name"`
2011-01-25 23:47:12 +11:00
}
func (rr *DNAME) String() string {
return rr.Hdr.String() + sprintName(rr.Target)
2011-01-25 23:47:12 +11:00
}
type A struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
A net.IP `dns:"a"`
2010-08-04 07:57:59 +10:00
}
func (rr *A) String() string {
if rr.A == nil {
return rr.Hdr.String()
}
return rr.Hdr.String() + rr.A.String()
}
type AAAA struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
AAAA net.IP `dns:"aaaa"`
2010-08-04 07:57:59 +10:00
}
func (rr *AAAA) String() string {
if rr.AAAA == nil {
return rr.Hdr.String()
}
return rr.Hdr.String() + rr.AAAA.String()
}
2013-10-13 23:23:02 +11:00
type PX struct {
Hdr RR_Header
Preference uint16
Map822 string `dns:"domain-name"`
Mapx400 string `dns:"domain-name"`
}
func (rr *PX) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Map822) + " " + sprintName(rr.Mapx400)
2013-10-13 23:23:02 +11:00
}
2013-10-13 23:01:33 +11:00
type GPOS struct {
Hdr RR_Header
Longitude string
Latitude string
Altitude string
}
func (rr *GPOS) String() string {
return rr.Hdr.String() + rr.Longitude + " " + rr.Latitude + " " + rr.Altitude
}
type LOC struct {
Hdr RR_Header
Version uint8
Size uint8
HorizPre uint8
VertPre uint8
Latitude uint32
Longitude uint32
Altitude uint32
2010-12-31 06:55:25 +11:00
}
// cmToM takes a cm value expressed in RFC1876 SIZE mantissa/exponent
// format and returns a string in m (two decimals for the cm)
func cmToM(m, e uint8) string {
if e < 2 {
if e == 1 {
m *= 10
}
return fmt.Sprintf("0.%02d", m)
}
s := fmt.Sprintf("%d", m)
for e > 2 {
s += "0"
e--
}
return s
}
func (rr *LOC) String() string {
s := rr.Hdr.String()
lat := rr.Latitude
ns := "N"
if lat > LOC_EQUATOR {
lat = lat - LOC_EQUATOR
} else {
ns = "S"
lat = LOC_EQUATOR - lat
}
h := lat / LOC_DEGREES
lat = lat % LOC_DEGREES
m := lat / LOC_HOURS
lat = lat % LOC_HOURS
s += fmt.Sprintf("%02d %02d %0.3f %s ", h, m, (float64(lat) / 1000), ns)
lon := rr.Longitude
ew := "E"
if lon > LOC_PRIMEMERIDIAN {
lon = lon - LOC_PRIMEMERIDIAN
} else {
ew = "W"
lon = LOC_PRIMEMERIDIAN - lon
}
h = lon / LOC_DEGREES
lon = lon % LOC_DEGREES
m = lon / LOC_HOURS
lon = lon % LOC_HOURS
s += fmt.Sprintf("%02d %02d %0.3f %s ", h, m, (float64(lon) / 1000), ew)
2015-02-19 22:41:12 +11:00
var alt = float64(rr.Altitude) / 100
alt -= LOC_ALTITUDEBASE
if rr.Altitude%100 != 0 {
s += fmt.Sprintf("%.2fm ", alt)
} else {
s += fmt.Sprintf("%.0fm ", alt)
}
s += cmToM((rr.Size&0xf0)>>4, rr.Size&0x0f) + "m "
s += cmToM((rr.HorizPre&0xf0)>>4, rr.HorizPre&0x0f) + "m "
s += cmToM((rr.VertPre&0xf0)>>4, rr.VertPre&0x0f) + "m"
return s
2010-12-31 06:55:25 +11:00
}
// SIG is identical to RRSIG and nowadays only used for SIG(0), RFC2931.
type SIG struct {
2014-11-10 03:09:49 +11:00
RRSIG
}
type RRSIG struct {
2010-12-23 21:02:01 +11:00
Hdr RR_Header
TypeCovered uint16
Algorithm uint8
Labels uint8
OrigTtl uint32
Expiration uint32
Inception uint32
KeyTag uint16
SignerName string `dns:"domain-name"`
Signature string `dns:"base64"`
2010-08-04 07:57:59 +10:00
}
func (rr *RRSIG) String() string {
s := rr.Hdr.String()
s += Type(rr.TypeCovered).String()
s += " " + strconv.Itoa(int(rr.Algorithm)) +
2010-12-21 08:20:13 +11:00
" " + strconv.Itoa(int(rr.Labels)) +
2012-02-20 04:36:59 +11:00
" " + strconv.FormatInt(int64(rr.OrigTtl), 10) +
2012-09-12 05:45:21 +10:00
" " + TimeToString(rr.Expiration) +
" " + TimeToString(rr.Inception) +
2010-12-21 08:20:13 +11:00
" " + strconv.Itoa(int(rr.KeyTag)) +
" " + sprintName(rr.SignerName) +
" " + rr.Signature
return s
2010-08-04 07:57:59 +10:00
}
type NSEC struct {
Hdr RR_Header
NextDomain string `dns:"domain-name"`
TypeBitMap []uint16 `dns:"nsec"`
2010-08-04 07:57:59 +10:00
}
func (rr *NSEC) String() string {
s := rr.Hdr.String() + sprintName(rr.NextDomain)
2011-01-17 20:30:20 +11:00
for i := 0; i < len(rr.TypeBitMap); i++ {
s += " " + Type(rr.TypeBitMap[i]).String()
2011-01-17 20:30:20 +11:00
}
return s
2010-08-04 07:57:59 +10:00
}
func (rr *NSEC) len() int {
l := rr.Hdr.len() + len(rr.NextDomain) + 1
lastwindow := uint32(2 ^ 32 + 1)
for _, t := range rr.TypeBitMap {
window := t / 256
if uint32(window) != lastwindow {
l += 1 + 32
}
lastwindow = uint32(window)
}
return l
}
type DLV struct {
DS
}
type CDS struct {
DS
}
type DS struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
KeyTag uint16
Algorithm uint8
DigestType uint8
Digest string `dns:"hex"`
2010-08-04 07:57:59 +10:00
}
func (rr *DS) String() string {
2011-01-18 02:03:06 +11:00
return rr.Hdr.String() + strconv.Itoa(int(rr.KeyTag)) +
" " + strconv.Itoa(int(rr.Algorithm)) +
2010-08-04 07:57:59 +10:00
" " + strconv.Itoa(int(rr.DigestType)) +
" " + strings.ToUpper(rr.Digest)
2010-08-04 07:57:59 +10:00
}
type KX struct {
2012-11-21 00:32:06 +11:00
Hdr RR_Header
Preference uint16
Exchanger string `dns:"domain-name"`
2011-03-13 23:04:54 +11:00
}
func (rr *KX) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) +
" " + sprintName(rr.Exchanger)
2011-03-13 23:04:54 +11:00
}
type TA struct {
2011-02-21 23:16:07 +11:00
Hdr RR_Header
KeyTag uint16
Algorithm uint8
DigestType uint8
Digest string `dns:"hex"`
2011-02-21 23:16:07 +11:00
}
func (rr *TA) String() string {
2011-02-21 23:16:07 +11:00
return rr.Hdr.String() + strconv.Itoa(int(rr.KeyTag)) +
" " + strconv.Itoa(int(rr.Algorithm)) +
" " + strconv.Itoa(int(rr.DigestType)) +
" " + strings.ToUpper(rr.Digest)
}
2011-01-17 20:30:20 +11:00
type TALINK struct {
2011-02-21 23:24:45 +11:00
Hdr RR_Header
PreviousName string `dns:"domain-name"`
NextName string `dns:"domain-name"`
2011-02-21 23:24:45 +11:00
}
func (rr *TALINK) String() string {
2011-02-21 23:24:45 +11:00
return rr.Hdr.String() +
sprintName(rr.PreviousName) + " " + sprintName(rr.NextName)
2011-02-21 23:24:45 +11:00
}
type SSHFP struct {
2011-01-17 20:30:20 +11:00
Hdr RR_Header
Algorithm uint8
Type uint8
FingerPrint string `dns:"hex"`
2011-01-17 20:30:20 +11:00
}
func (rr *SSHFP) String() string {
2011-01-18 02:03:06 +11:00
return rr.Hdr.String() + strconv.Itoa(int(rr.Algorithm)) +
2011-01-17 20:30:20 +11:00
" " + strconv.Itoa(int(rr.Type)) +
" " + strings.ToUpper(rr.FingerPrint)
}
type KEY struct {
2014-11-10 03:09:49 +11:00
DNSKEY
}
type CDNSKEY struct {
2014-11-10 03:09:49 +11:00
DNSKEY
}
type DNSKEY struct {
2010-08-04 07:57:59 +10:00
Hdr RR_Header
Flags uint16
Protocol uint8
Algorithm uint8
PublicKey string `dns:"base64"`
2010-08-04 07:57:59 +10:00
}
func (rr *DNSKEY) String() string {
2012-02-19 06:07:25 +11:00
return rr.Hdr.String() + strconv.Itoa(int(rr.Flags)) +
2010-08-04 07:57:59 +10:00
" " + strconv.Itoa(int(rr.Protocol)) +
2010-12-21 08:20:13 +11:00
" " + strconv.Itoa(int(rr.Algorithm)) +
" " + rr.PublicKey
2010-08-04 07:57:59 +10:00
}
type RKEY struct {
2012-11-21 02:48:28 +11:00
Hdr RR_Header
Flags uint16
Protocol uint8
Algorithm uint8
PublicKey string `dns:"base64"`
}
func (rr *RKEY) String() string {
2012-11-21 02:48:28 +11:00
return rr.Hdr.String() + strconv.Itoa(int(rr.Flags)) +
" " + strconv.Itoa(int(rr.Protocol)) +
" " + strconv.Itoa(int(rr.Algorithm)) +
" " + rr.PublicKey
}
2013-10-13 22:25:08 +11:00
type NSAPPTR struct {
Hdr RR_Header
Ptr string `dns:"domain-name"`
}
func (rr *NSAPPTR) String() string { return rr.Hdr.String() + sprintName(rr.Ptr) }
2013-10-13 22:25:08 +11:00
type NSEC3 struct {
Hdr RR_Header
Hash uint8
Flags uint8
Iterations uint16
SaltLength uint8
Salt string `dns:"size-hex:SaltLength"`
HashLength uint8
NextDomain string `dns:"size-base32:HashLength"`
TypeBitMap []uint16 `dns:"nsec"`
2010-08-04 07:57:59 +10:00
}
func (rr *NSEC3) String() string {
2011-01-17 20:30:20 +11:00
s := rr.Hdr.String()
2011-01-18 02:03:06 +11:00
s += strconv.Itoa(int(rr.Hash)) +
2011-01-17 20:30:20 +11:00
" " + strconv.Itoa(int(rr.Flags)) +
" " + strconv.Itoa(int(rr.Iterations)) +
" " + saltToString(rr.Salt) +
2011-02-22 02:21:16 +11:00
" " + rr.NextDomain
2011-01-17 20:30:20 +11:00
for i := 0; i < len(rr.TypeBitMap); i++ {
s += " " + Type(rr.TypeBitMap[i]).String()
2011-01-17 20:30:20 +11:00
}
return s
2010-08-04 07:57:59 +10:00
}
func (rr *NSEC3) len() int {
l := rr.Hdr.len() + 6 + len(rr.Salt)/2 + 1 + len(rr.NextDomain) + 1
lastwindow := uint32(2 ^ 32 + 1)
for _, t := range rr.TypeBitMap {
window := t / 256
if uint32(window) != lastwindow {
l += 1 + 32
}
lastwindow = uint32(window)
}
return l
}
type NSEC3PARAM struct {
Hdr RR_Header
Hash uint8
Flags uint8
Iterations uint16
SaltLength uint8
Salt string `dns:"size-hex:SaltLength"`
2010-08-04 07:57:59 +10:00
}
func (rr *NSEC3PARAM) String() string {
2011-01-18 02:03:06 +11:00
s := rr.Hdr.String()
s += strconv.Itoa(int(rr.Hash)) +
" " + strconv.Itoa(int(rr.Flags)) +
" " + strconv.Itoa(int(rr.Iterations)) +
" " + saltToString(rr.Salt)
return s
2010-08-04 07:57:59 +10:00
}
type TKEY struct {
Hdr RR_Header
Algorithm string `dns:"domain-name"`
Inception uint32
Expiration uint32
Mode uint16
Error uint16
KeySize uint16
Key string
2012-06-21 01:12:10 +10:00
OtherLen uint16
OtherData string
}
func (rr *TKEY) String() string {
2011-01-10 06:46:21 +11:00
// It has no presentation format
return ""
}
// RFC3597 represents an unknown/generic RR.
type RFC3597 struct {
2011-02-25 02:22:14 +11:00
Hdr RR_Header
Rdata string `dns:"hex"`
2011-02-22 01:19:52 +11:00
}
func (rr *RFC3597) String() string {
// Let's call it a hack
s := rfc3597Header(rr.Hdr)
2011-02-25 02:22:14 +11:00
s += "\\# " + strconv.Itoa(len(rr.Rdata)/2) + " " + rr.Rdata
return s
2011-02-22 01:19:52 +11:00
}
func rfc3597Header(h RR_Header) string {
var s string
s += sprintName(h.Name) + "\t"
s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
s += "CLASS" + strconv.Itoa(int(h.Class)) + "\t"
s += "TYPE" + strconv.Itoa(int(h.Rrtype)) + "\t"
return s
}
type URI struct {
2011-02-25 02:22:14 +11:00
Hdr RR_Header
Priority uint16
Weight uint16
Target string `dns:"octet"`
}
func (rr *URI) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Priority)) +
2015-07-28 07:12:46 +10:00
" " + strconv.Itoa(int(rr.Weight)) + " " + sprintTxtOctet(rr.Target)
}
type DHCID struct {
2011-07-23 17:21:24 +10:00
Hdr RR_Header
Digest string `dns:"base64"`
2011-03-13 23:11:11 +11:00
}
func (rr *DHCID) String() string { return rr.Hdr.String() + rr.Digest }
type TLSA struct {
2012-01-16 20:11:05 +11:00
Hdr RR_Header
Usage uint8
Selector uint8
MatchingType uint8
Certificate string `dns:"hex"`
2012-01-16 20:06:21 +11:00
}
func (rr *TLSA) String() string {
2012-01-16 20:06:21 +11:00
return rr.Hdr.String() +
strconv.Itoa(int(rr.Usage)) +
2012-01-16 20:06:21 +11:00
" " + strconv.Itoa(int(rr.Selector)) +
" " + strconv.Itoa(int(rr.MatchingType)) +
" " + rr.Certificate
}
type HIP struct {
2012-02-19 06:07:25 +11:00
Hdr RR_Header
HitLength uint8
PublicKeyAlgorithm uint8
PublicKeyLength uint16
Hit string `dns:"size-hex:HitLength"`
PublicKey string `dns:"size-base64:PublicKeyLength"`
RendezvousServers []string `dns:"domain-name"`
2012-02-19 06:07:25 +11:00
}
func (rr *HIP) String() string {
2012-02-20 01:24:26 +11:00
s := rr.Hdr.String() +
strconv.Itoa(int(rr.PublicKeyAlgorithm)) +
2012-02-19 06:07:25 +11:00
" " + rr.Hit +
" " + rr.PublicKey
2012-02-20 01:24:26 +11:00
for _, d := range rr.RendezvousServers {
s += " " + sprintName(d)
2012-02-20 01:24:26 +11:00
}
return s
2012-02-19 06:07:25 +11:00
}
type NINFO struct {
2012-11-21 02:52:18 +11:00
Hdr RR_Header
ZSData []string `dns:"txt"`
2012-11-21 02:42:16 +11:00
}
func (rr *NINFO) String() string { return rr.Hdr.String() + sprintTxt(rr.ZSData) }
2012-11-21 02:42:16 +11:00
type NID struct {
2012-11-18 07:26:48 +11:00
Hdr RR_Header
Preference uint16
NodeID uint64
}
func (rr *NID) String() string {
2012-11-18 07:26:48 +11:00
s := rr.Hdr.String() + strconv.Itoa(int(rr.Preference))
2012-11-18 09:01:35 +11:00
node := fmt.Sprintf("%0.16x", rr.NodeID)
s += " " + node[0:4] + ":" + node[4:8] + ":" + node[8:12] + ":" + node[12:16]
2012-11-18 07:26:48 +11:00
return s
}
type L32 struct {
2012-11-18 07:32:02 +11:00
Hdr RR_Header
Preference uint16
Locator32 net.IP `dns:"a"`
}
func (rr *L32) String() string {
if rr.Locator32 == nil {
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference))
}
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) +
" " + rr.Locator32.String()
2012-11-18 07:32:02 +11:00
}
type L64 struct {
2012-11-18 07:37:15 +11:00
Hdr RR_Header
Preference uint16
Locator64 uint64
}
func (rr *L64) String() string {
2012-11-18 07:37:15 +11:00
s := rr.Hdr.String() + strconv.Itoa(int(rr.Preference))
2012-11-18 19:10:28 +11:00
node := fmt.Sprintf("%0.16X", rr.Locator64)
s += " " + node[0:4] + ":" + node[4:8] + ":" + node[8:12] + ":" + node[12:16]
2012-11-18 07:37:15 +11:00
return s
}
type LP struct {
2012-11-18 07:43:09 +11:00
Hdr RR_Header
Preference uint16
Fqdn string `dns:"domain-name"`
}
func (rr *LP) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Fqdn)
2012-11-18 07:43:09 +11:00
}
type EUI48 struct {
Hdr RR_Header
Address uint64 `dns:"uint48"`
}
func (rr *EUI48) String() string { return rr.Hdr.String() + euiToString(rr.Address, 48) }
type EUI64 struct {
Hdr RR_Header
Address uint64
}
func (rr *EUI64) String() string { return rr.Hdr.String() + euiToString(rr.Address, 64) }
2013-04-30 23:04:07 +10:00
type CAA struct {
Hdr RR_Header
Flag uint8
Tag string
Value string `dns:"octet"`
2013-04-30 23:04:07 +10:00
}
func (rr *CAA) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Flag)) + " " + rr.Tag + " " + sprintTxtOctet(rr.Value)
}
2013-04-30 23:04:07 +10:00
2013-05-01 00:42:04 +10:00
type UID struct {
Hdr RR_Header
Uid uint32
}
func (rr *UID) String() string { return rr.Hdr.String() + strconv.FormatInt(int64(rr.Uid), 10) }
2013-05-01 00:42:04 +10:00
type GID struct {
Hdr RR_Header
Gid uint32
}
func (rr *GID) String() string { return rr.Hdr.String() + strconv.FormatInt(int64(rr.Gid), 10) }
2013-05-01 00:42:04 +10:00
type UINFO struct {
Hdr RR_Header
Uinfo string
}
func (rr *UINFO) String() string { return rr.Hdr.String() + sprintTxt([]string{rr.Uinfo}) }
2013-05-01 00:42:04 +10:00
2013-10-20 07:31:12 +11:00
type EID struct {
Hdr RR_Header
Endpoint string `dns:"hex"`
}
func (rr *EID) String() string { return rr.Hdr.String() + strings.ToUpper(rr.Endpoint) }
2013-10-20 07:31:12 +11:00
type NIMLOC struct {
Hdr RR_Header
Locator string `dns:"hex"`
}
func (rr *NIMLOC) String() string { return rr.Hdr.String() + strings.ToUpper(rr.Locator) }
2013-10-20 07:31:12 +11:00
2014-07-24 18:29:42 +10:00
type OPENPGPKEY struct {
Hdr RR_Header
PublicKey string `dns:"base64"`
}
func (rr *OPENPGPKEY) String() string { return rr.Hdr.String() + rr.PublicKey }
2014-07-24 18:29:42 +10:00
2012-09-12 05:45:21 +10:00
// TimeToString translates the RRSIG's incep. and expir. times to the
2012-04-11 23:13:17 +10:00
// string representation used when printing the record.
2012-05-15 20:27:40 +10:00
// It takes serial arithmetic (RFC 1982) into account.
2012-09-12 05:45:21 +10:00
func TimeToString(t uint32) string {
2012-08-17 16:29:45 +10:00
mod := ((int64(t) - time.Now().Unix()) / year68) - 1
2012-05-15 20:24:57 +10:00
if mod < 0 {
mod = 0
}
2012-08-17 16:29:45 +10:00
ti := time.Unix(int64(t)-(mod*year68), 0).UTC()
2011-07-25 05:29:16 +10:00
return ti.Format("20060102150405")
}
// StringToTime translates the RRSIG's incep. and expir. times from
2012-04-11 23:13:17 +10:00
// string values like "20110403154150" to an 32 bit integer.
2012-05-15 20:24:57 +10:00
// It takes serial arithmetic (RFC 1982) into account.
2012-09-12 05:45:21 +10:00
func StringToTime(s string) (uint32, error) {
t, err := time.Parse("20060102150405", s)
if err != nil {
return 0, err
2011-12-17 00:48:30 +11:00
}
2012-08-17 16:29:45 +10:00
mod := (t.Unix() / year68) - 1
2012-05-15 20:24:57 +10:00
if mod < 0 {
mod = 0
}
2012-08-17 16:29:45 +10:00
return uint32(t.Unix() - (mod * year68)), nil
}
// saltToString converts a NSECX salt to uppercase and returns "-" when it is empty.
func saltToString(s string) string {
if len(s) == 0 {
return "-"
}
return strings.ToUpper(s)
}
func euiToString(eui uint64, bits int) (hex string) {
switch bits {
case 64:
2013-04-16 03:42:07 +10:00
hex = fmt.Sprintf("%16.16x", eui)
hex = hex[0:2] + "-" + hex[2:4] + "-" + hex[4:6] + "-" + hex[6:8] +
"-" + hex[8:10] + "-" + hex[10:12] + "-" + hex[12:14] + "-" + hex[14:16]
case 48:
2013-04-16 03:42:07 +10:00
hex = fmt.Sprintf("%12.12x", eui)
hex = hex[0:2] + "-" + hex[2:4] + "-" + hex[4:6] + "-" + hex[6:8] +
"-" + hex[8:10] + "-" + hex[10:12]
}
return
}
2014-01-12 21:11:23 +11:00
// copyIP returns a copy of ip.
func copyIP(ip net.IP) net.IP {
p := make(net.IP, len(ip))
copy(p, ip)
return p
}