Documentation ¶
Overview ¶
Package dns implements a fast and natural interface to the Domain Name System.
Index ¶
- Variables
- func NewIPv4(v net.IP, buf ...[]byte) [4]byte
- type A
- type CNAME
- type COOKIE
- type Class
- type Code
- type Flag
- type Header
- type MX
- type Msg
- func (m *Msg) Count(s Section) uint16
- func (m *Msg) Flag(f Flag) bool
- func (m *Msg) GoString() string
- func (m *Msg) ID() uint16
- func (m *Msg) Len() int
- func (m *Msg) Opcode() Opcode
- func (m *Msg) RR(s Section) (RR, error)
- func (m *Msg) RRs(s Section) ([]RR, error)
- func (m *Msg) Rcode() Rcode
- func (m *Msg) Reset()
- func (m *Msg) SetCount(s Section, i uint16)
- func (m *Msg) SetFlag(f Flag, v ...bool)
- func (m *Msg) SetID(i ...uint16)
- func (m *Msg) SetOpcode(o Opcode)
- func (m *Msg) SetRR(s Section, rr RR) error
- func (m *Msg) SetRRs(s Section, rrs []RR) error
- func (m *Msg) SetRcode(r Rcode)
- func (m *Msg) String() string
- func (m *Msg) Strip(n int) ([]RR, error)
- func (m *Msg) Walk(d WalkDirection, fn WalkFunc) (err error)
- type NS
- type NSID
- type Name
- type OPT
- func (rr *OPT) Data(i int) []byte
- func (rr *OPT) Do() bool
- func (rr *OPT) ExtendedRcode() uint8
- func (rr *OPT) Hdr() *Header
- func (rr *OPT) Len() int
- func (rr *OPT) SetDo(do ...bool)
- func (rr *OPT) SetSize(s uint16)
- func (rr *OPT) Size() uint16
- func (rr *OPT) String() string
- func (rr *OPT) Version() uint8
- func (rr *OPT) Write(msg []byte, offset, n int) error
- type Opcode
- type Option
- type PTR
- type RR
- type Rcode
- type SOA
- type Section
- type TTL
- type Type
- type Unknown
- type UnknownEDNS0
- type WalkDirection
- type WalkFunc
- type WireError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( CodeNone = Code{0, 0} CodeNSID = Code{0, 3} CodeCookie = Code{0, 10} )
Supported EDNS0 Option Codes.
var ( // Valid Classes. IN = Class{0, 1} NONE = Class{0, 254} ANY = Class{0, 255} // Supported RR Types. TypeNone = Type{0, 0} TypeA = Type{0, 1} TypeNS = Type{0, 2} TypeCNAME = Type{0, 5} TypeSOA = Type{0, 6} TypePTR = Type{0, 12} TypeMX = Type{0, 15} TypeOPT = Type{0, 41} )
var ClassToString = map[Class]string{
ANY: "ANY",
IN: "IN",
NONE: "NONE",
}
ClassToString maps Classes to strings.
var IDFunc = id
IDFunc returns a 16-bit random number that is used as the message's ID. This can be overriden if so desired.
var OpcodeToString = map[Opcode]string{
OpcodeIQuery: "IQUERY",
OpcodeNotify: "NOTIFY",
OpcodeQuery: "QUERY",
OpcodeStatus: "STATUS",
OpcodeUpdate: "UPDATE",
}
OpcodeToString maps Opcodes to strings.
var RcodeToString = map[Rcode]string{
RcodeFormErr: "FORMERR",
RcodeNXDomain: "NXDOMAIN",
RcodeNoError: "NOERROR",
RcodeNotImp: "NOTIMP",
RcodeRefused: "REFUSED",
RcodeServFail: "SERVFAIL",
}
RcodeToString maps Rcodes to strings.
var StringToClass = reverseClass(ClassToString)
StringToClass maps strings to Classes.
var StringToOpcode = reverseOpcode(OpcodeToString)
StringToOpcode maps strings to Opcodes.
var StringToRcode = reverseRcode(RcodeToString)
StringToRcode maps strings to Rcodes.
var StringToType = reverseType(TypeToString)
StringToType maps strings to Types.
var TypeToRR = map[Type]func() RR{ TypeA: func() RR { return new(A) }, TypeCNAME: func() RR { return new(CNAME) }, TypeMX: func() RR { return new(MX) }, TypeNS: func() RR { return new(NS) }, TypeOPT: func() RR { return new(OPT) }, TypePTR: func() RR { return new(PTR) }, TypeSOA: func() RR { return new(SOA) }, }
TypeToRR is a mapping from Type to a function that returns the type.
var TypeToString = map[Type]string{
TypeA: "A",
TypeCNAME: "CNAME",
TypeMX: "MX",
TypeNS: "NS",
TypeOPT: "OPT",
TypePTR: "PTR",
TypeSOA: "SOA",
}
TypeToString maps Types to strings.
Functions ¶
Types ¶
type COOKIE ¶
type COOKIE struct {
Cookie []byte // Cookie is a hex encoded string.
}
Cookie Option.
type Class ¶
type Class [2]byte // Class is the class of the RR. Usually every RR has IN as its class.
type Flag ¶
type Flag uint16
Flag is a (boolean) message header flag.
const ( QR Flag = 1 << 15 // Query Response AA Flag = 1 << 10 // Authoritative Answer TC Flag = 1 << 9 // TrunCated RD Flag = 1 << 8 // Recursion Desired RA Flag = 1 << 7 // Recussion Available Z Flag = 1 << 6 // Zero bits AD Flag = 1 << 5 // Authenticated Data CD Flag = 1 << 4 // Checking Disabled )
These are the flags currently defined for a DNS message's header.
type Header ¶
type Header struct { Name // Owner name of the Resource Record. // Type is implicit and retrieved via the RR's Go type. Class // Class of the Resource Record. TTL // Time to Live of the Resource Record. }
Header is the header each RR has.
type Msg ¶
type Msg struct { Buf []byte // Buf is the message as read from the wire or as created. // contains filtered or unexported fields }
Msg is a DNS message which is used in the query and the response. It's defined as follows:
+---------------------+ | Message Header | +---------------------+ | Question | the question for the name server, [Qd]. +---------------------+ | Answer | RRs answering the question, [An]. +---------------------+ | Authority | RRs pointing toward an authority, [Ns]. +---------------------+ | Additional | RRs holding additional information, [Ar]. +---------------------+
A Msg allows RRs to be added (in order) or retrieved (in order per section, but sections can accessed in any order).
Even though the protocol allows multiple questions, in practice only 1 is allowed, this package enforces that convention. After setting any RR, the message's buffer may be written to the wire as it will contain a valid DNS message. In this package the question section's RR is handled as a normal RR, just without any rdata - as is also done in dynamic updates (RFC 2136).
The message header is defined as follows:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA|Z |AD|CD| RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
If the buffer in Msg is too small it will be resized while creating a message. Domain names that can be compressed will be compressed. If you assign a new byte slice to Buf, [Reset()] must be called to reset all internal counters and the compression map.
func NewMsg ¶
NewMsg returns a pointer to a new Msg. Optionally a buffer can be given here, NewMsg will not allocate a buffer on behalf of the caller, it will enlarge a buffer when the need arises. After assigning a new buffer to a Msg you must call [Reset].
func (*Msg) RR ¶
RR returns the next RR from the specified section. If none are found, nil is returned. If there is an error, a partial RR may be returned. When first called a message will be walked to find the indices of the sections.
func (*Msg) RRs ¶
RRs returns all the RRs from the section s.
Example ¶
m := &Msg{Buf: tmpbuf(reply)} answer, err := m.RRs(An) if err != nil { return } for _, rr := range answer { fmt.Printf("%s %s\n", rr.Hdr(), rr) }
Output: miek.nl. 900 IN MX 5 alt2.aspmx.l.google.com. miek.nl. 900 IN MX 1 aspmx.l.google.com. miek.nl. 900 IN MX 10 aspmx2.googlemail.com. miek.nl. 900 IN MX 10 aspmx3.googlemail.com. miek.nl. 900 IN MX 5 alt1.aspmx.l.google.com.
func (*Msg) Reset ¶
func (m *Msg) Reset()
Reset resets all internal counters in m. This must be called after a new buffer is assigned to m.
func (*Msg) SetRR ¶
SetRR adds rr's wireformat to the message m in the specified section. Any RR can be used to set the question section; it will then just use the name, type and class and ignore the
func (*Msg) SetRRs ¶
SetRR adds each rr's wireformat n rrs to the message m in the specified section.
func (*Msg) String ¶
String returns the text representation of the message m. Note this method _parses_ the message and extracts the RRs for printing, this makes it an expensive method. It can also error, in that case the empty string is returned. Mostly useful for debugging.
Example ¶
m := &Msg{Buf: tmpbuf(reply)} fmt.Printf("%s\n", m) /* Ouput: ;; MESSAGE HEADER: opcode: QUERY, status: NOERROR, id: 59761 ;; flags: qr rd ra ad; QUESTION 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1 ;; QUESTION SECTION: miek.nl. IN MX ;; ANSWER SECTION: miek.nl. 900 IN MX 5 alt2.aspmx.l.google.com. miek.nl. 900 IN MX 1 aspmx.l.google.com. miek.nl. 900 IN MX 10 aspmx2.googlemail.com. miek.nl. 900 IN MX 10 aspmx3.googlemail.com. miek.nl. 900 IN MX 5 alt1.aspmx.l.google.com. ;; ADDITIONAL SECTION: ;; EDNS: version: 0, flags:; udp: 512 */
Output:
func (*Msg) Strip ¶
Strip strips the last n RRs from the message. The stripped RRs are returned, the section counters are adjusted as necessary. Strips disregards any section boundaries. After a call to Strip any reads from m start from the beginning again. The buffer in m is not downsized. Compression pointers are NOT updated.
func (*Msg) Walk ¶
func (m *Msg) Walk(d WalkDirection, fn WalkFunc) (err error)
Walk walks the section s in the message m in the direction of d. Each rr is filtered by fn. If WalkFunc returns an error the walk is stopped. Note due to DNS messages are structed an WalkBackward requires to walk forward and then backwards.
type Name ¶
type Name []byte // Name is the owner name of the RR.
func NewName ¶
NewName returns a name from s. If s is not a valid name nil is returned. If buf is not nil the name is written into it.
type OPT ¶
OPT is the EDNS0 RR appended to messages to convey extra (meta) information. See RFC 6891. Each option is encoded as:
+0 (MSB) +1 (LSB) +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 0: | OPTION-CODE | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 2: | OPTION-LENGTH | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 4: | | / OPTION-DATA / / / +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
func NewOPT ¶
func NewOPT() *OPT
NewOPT returns a pointer to a new OPT that has the correct properties to be used for EDNS0 options.
func (*OPT) ExtendedRcode ¶
ExtendedRcode returns the extended Rcode.
type Opcode ¶
type Opcode uint8
Opcode is the operation code for a message.
type Option ¶
type Option interface { // Len returns the option's value length. This is excluding the option code and length (which is always // 4 octets): len(o.Data()) = o.Len() + 4. Len() int // Data returns the option's values. The buffer returned is in wire format, all options require option // code and length, this is prepended in the buffer. Data() []byte // String returns the string representation of the EDNS0 option. String() string // Write writes the rdata encoded in buf into the EDNS0 option. Write(buf []byte) error }
An Option represents an OPT RR rdata value. Basic usage for adding an option to an OPT RR:
opt := dns.NewOPT() nsid := &dns.NSID{ID: []byte("AA")} opt.Options = []Option{nsid}
type RR ¶
type RR interface { // Hdr returns a pointer to the header of the RR. Hdr() *Header // Len returns the number of rdata elements the RR has. For RRs with a dynamic number of elements (i.e. // OPT, and others), this is not a constant number. Len() int // Data returns the rdata at position i (zero based). If there is no data at that position nil is // returned. The buffer returned is in wire format, i.e. if some data requires a length, that length is // prepended to the buffer. Data(i int) []byte // FromString transforms the string s in the the wire data suitable for the rdata at position i. // FromString(i int, s string) []byte // String returns the string representation of the rdata(!) only. String() string // Write writes the rdata encoded in msg starting at index offset and length n to the RR. Some rdata // needs access to the message's data msg to resolve compression pointers. If msg buffer is too small to // fit the data it is enlarged. Write(msg []byte, offset, n int) error }
RR defines a Resource Record. Note that even the special RR in the question section is handled as a normal Resource Record (i.e with a zero TTL and no rdata).
type Rcode ¶
type Rcode uint16
Rcode is the return (status) code for a message.
type SOA ¶
type SOA struct { Header Ns Name Mbox Name Serial [4]byte Refresh [4]byte Retry [4]byte Expire [4]byte MinTTL [4]byte }
SOA RR. See RFC 1035.
type Section ¶
type Section uint8
Section signifies a message's section. Four sections are defined (in order): Qd, An, Ns, and Ar.
type TTL ¶
type TTL [4]byte // TTL is the time to live of the RR.
type Type ¶
type Type [2]byte // Type is the Type of an RR. An RR in this package is implicitly typed via it's Go type.
type Unknown ¶
type Unknown struct { Header Type // Type holds the type number of the unknown type we're holding. Unknown []byte // Data is as-is. }
Unknown represents an unknown/generic RR. See RFC 3597.
type UnknownEDNS0 ¶
type UnknownEDNS0 struct { Code // Code holds the option code number of the unknown option code we're holding. Unknown []byte // Data as-is. }
UnknownEDNS0 is an unknown EDNS0 Option. Similar in style to RFC 3597 handling.
func (*UnknownEDNS0) Data ¶
func (o *UnknownEDNS0) Data() []byte
func (*UnknownEDNS0) Len ¶
func (o *UnknownEDNS0) Len() int
func (*UnknownEDNS0) String ¶
func (o *UnknownEDNS0) String() string
func (*UnknownEDNS0) Write ¶
func (o *UnknownEDNS0) Write(buf []byte) error
type WalkDirection ¶
type WalkDirection int
WalkDirection tells in what order to walk the message.
const ( WalkForward WalkDirection = iota WalkBackward )