1package dns
2
3import (
4	"errors"
5	"net"
6	"strconv"
7)
8
9const hexDigit = "0123456789abcdef"
10
11// Everything is assumed in ClassINET.
12
13// SetReply creates a reply message from a request message.
14func (dns *Msg) SetReply(request *Msg) *Msg {
15	dns.Id = request.Id
16	dns.RecursionDesired = request.RecursionDesired // Copy rd bit
17	dns.Response = true
18	dns.Opcode = OpcodeQuery
19	dns.Rcode = RcodeSuccess
20	if len(request.Question) > 0 {
21		dns.Question = make([]Question, 1)
22		dns.Question[0] = request.Question[0]
23	}
24	return dns
25}
26
27// SetQuestion creates a question message, it sets the Question
28// section, generates an Id and sets the RecursionDesired (RD)
29// bit to true.
30func (dns *Msg) SetQuestion(z string, t uint16) *Msg {
31	dns.Id = Id()
32	dns.RecursionDesired = true
33	dns.Question = make([]Question, 1)
34	dns.Question[0] = Question{z, t, ClassINET}
35	return dns
36}
37
38// SetNotify creates a notify message, it sets the Question
39// section, generates an Id and sets the Authoritative (AA)
40// bit to true.
41func (dns *Msg) SetNotify(z string) *Msg {
42	dns.Opcode = OpcodeNotify
43	dns.Authoritative = true
44	dns.Id = Id()
45	dns.Question = make([]Question, 1)
46	dns.Question[0] = Question{z, TypeSOA, ClassINET}
47	return dns
48}
49
50// SetRcode creates an error message suitable for the request.
51func (dns *Msg) SetRcode(request *Msg, rcode int) *Msg {
52	dns.SetReply(request)
53	dns.Rcode = rcode
54	return dns
55}
56
57// SetRcodeFormatError creates a message with FormError set.
58func (dns *Msg) SetRcodeFormatError(request *Msg) *Msg {
59	dns.Rcode = RcodeFormatError
60	dns.Opcode = OpcodeQuery
61	dns.Response = true
62	dns.Authoritative = false
63	dns.Id = request.Id
64	return dns
65}
66
67// SetUpdate makes the message a dynamic update message. It
68// sets the ZONE section to: z, TypeSOA, ClassINET.
69func (dns *Msg) SetUpdate(z string) *Msg {
70	dns.Id = Id()
71	dns.Response = false
72	dns.Opcode = OpcodeUpdate
73	dns.Compress = false // BIND9 cannot handle compression
74	dns.Question = make([]Question, 1)
75	dns.Question[0] = Question{z, TypeSOA, ClassINET}
76	return dns
77}
78
79// SetIxfr creates message for requesting an IXFR.
80func (dns *Msg) SetIxfr(z string, serial uint32, ns, mbox string) *Msg {
81	dns.Id = Id()
82	dns.Question = make([]Question, 1)
83	dns.Ns = make([]RR, 1)
84	s := new(SOA)
85	s.Hdr = RR_Header{z, TypeSOA, ClassINET, defaultTtl, 0}
86	s.Serial = serial
87	s.Ns = ns
88	s.Mbox = mbox
89	dns.Question[0] = Question{z, TypeIXFR, ClassINET}
90	dns.Ns[0] = s
91	return dns
92}
93
94// SetAxfr creates message for requesting an AXFR.
95func (dns *Msg) SetAxfr(z string) *Msg {
96	dns.Id = Id()
97	dns.Question = make([]Question, 1)
98	dns.Question[0] = Question{z, TypeAXFR, ClassINET}
99	return dns
100}
101
102// SetTsig appends a TSIG RR to the message.
103// This is only a skeleton TSIG RR that is added as the last RR in the
104// additional section. The Tsig is calculated when the message is being send.
105func (dns *Msg) SetTsig(z, algo string, fudge, timesigned int64) *Msg {
106	t := new(TSIG)
107	t.Hdr = RR_Header{z, TypeTSIG, ClassANY, 0, 0}
108	t.Algorithm = algo
109	t.Fudge = 300
110	t.TimeSigned = uint64(timesigned)
111	t.OrigId = dns.Id
112	dns.Extra = append(dns.Extra, t)
113	return dns
114}
115
116// SetEdns0 appends a EDNS0 OPT RR to the message.
117// TSIG should always the last RR in a message.
118func (dns *Msg) SetEdns0(udpsize uint16, do bool) *Msg {
119	e := new(OPT)
120	e.Hdr.Name = "."
121	e.Hdr.Rrtype = TypeOPT
122	e.SetUDPSize(udpsize)
123	if do {
124		e.SetDo()
125	}
126	dns.Extra = append(dns.Extra, e)
127	return dns
128}
129
130// IsTsig checks if the message has a TSIG record as the last record
131// in the additional section. It returns the TSIG record found or nil.
132func (dns *Msg) IsTsig() *TSIG {
133	if len(dns.Extra) > 0 {
134		if dns.Extra[len(dns.Extra)-1].Header().Rrtype == TypeTSIG {
135			return dns.Extra[len(dns.Extra)-1].(*TSIG)
136		}
137	}
138	return nil
139}
140
141// IsEdns0 checks if the message has a EDNS0 (OPT) record, any EDNS0
142// record in the additional section will do. It returns the OPT record
143// found or nil.
144func (dns *Msg) IsEdns0() *OPT {
145	// EDNS0 is at the end of the additional section, start there.
146	// We might want to change this to *only* look at the last two
147	// records. So we see TSIG and/or OPT - this a slightly bigger
148	// change though.
149	for i := len(dns.Extra) - 1; i >= 0; i-- {
150		if dns.Extra[i].Header().Rrtype == TypeOPT {
151			return dns.Extra[i].(*OPT)
152		}
153	}
154	return nil
155}
156
157// IsDomainName checks if s is a valid domain name, it returns the number of
158// labels and true, when a domain name is valid.  Note that non fully qualified
159// domain name is considered valid, in this case the last label is counted in
160// the number of labels.  When false is returned the number of labels is not
161// defined.  Also note that this function is extremely liberal; almost any
162// string is a valid domain name as the DNS is 8 bit protocol. It checks if each
163// label fits in 63 characters, but there is no length check for the entire
164// string s. I.e.  a domain name longer than 255 characters is considered valid.
165func IsDomainName(s string) (labels int, ok bool) {
166	_, labels, err := packDomainName(s, nil, 0, nil, false)
167	return labels, err == nil
168}
169
170// IsSubDomain checks if child is indeed a child of the parent. If child and parent
171// are the same domain true is returned as well.
172func IsSubDomain(parent, child string) bool {
173	// Entire child is contained in parent
174	return CompareDomainName(parent, child) == CountLabel(parent)
175}
176
177// IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
178// The checking is performed on the binary payload.
179func IsMsg(buf []byte) error {
180	// Header
181	if len(buf) < 12 {
182		return errors.New("dns: bad message header")
183	}
184	// Header: Opcode
185	// TODO(miek): more checks here, e.g. check all header bits.
186	return nil
187}
188
189// IsFqdn checks if a domain name is fully qualified.
190func IsFqdn(s string) bool {
191	l := len(s)
192	if l == 0 {
193		return false
194	}
195	return s[l-1] == '.'
196}
197
198// IsRRset checks if a set of RRs is a valid RRset as defined by RFC 2181.
199// This means the RRs need to have the same type, name, and class. Returns true
200// if the RR set is valid, otherwise false.
201func IsRRset(rrset []RR) bool {
202	if len(rrset) == 0 {
203		return false
204	}
205	if len(rrset) == 1 {
206		return true
207	}
208	rrHeader := rrset[0].Header()
209	rrType := rrHeader.Rrtype
210	rrClass := rrHeader.Class
211	rrName := rrHeader.Name
212
213	for _, rr := range rrset[1:] {
214		curRRHeader := rr.Header()
215		if curRRHeader.Rrtype != rrType || curRRHeader.Class != rrClass || curRRHeader.Name != rrName {
216			// Mismatch between the records, so this is not a valid rrset for
217			//signing/verifying
218			return false
219		}
220	}
221
222	return true
223}
224
225// Fqdn return the fully qualified domain name from s.
226// If s is already fully qualified, it behaves as the identity function.
227func Fqdn(s string) string {
228	if IsFqdn(s) {
229		return s
230	}
231	return s + "."
232}
233
234// Copied from the official Go code.
235
236// ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
237// address suitable for reverse DNS (PTR) record lookups or an error if it fails
238// to parse the IP address.
239func ReverseAddr(addr string) (arpa string, err error) {
240	ip := net.ParseIP(addr)
241	if ip == nil {
242		return "", &Error{err: "unrecognized address: " + addr}
243	}
244	if ip.To4() != nil {
245		return strconv.Itoa(int(ip[15])) + "." + strconv.Itoa(int(ip[14])) + "." + strconv.Itoa(int(ip[13])) + "." +
246			strconv.Itoa(int(ip[12])) + ".in-addr.arpa.", nil
247	}
248	// Must be IPv6
249	buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
250	// Add it, in reverse, to the buffer
251	for i := len(ip) - 1; i >= 0; i-- {
252		v := ip[i]
253		buf = append(buf, hexDigit[v&0xF])
254		buf = append(buf, '.')
255		buf = append(buf, hexDigit[v>>4])
256		buf = append(buf, '.')
257	}
258	// Append "ip6.arpa." and return (buf already has the final .)
259	buf = append(buf, "ip6.arpa."...)
260	return string(buf), nil
261}
262
263// String returns the string representation for the type t.
264func (t Type) String() string {
265	if t1, ok := TypeToString[uint16(t)]; ok {
266		return t1
267	}
268	return "TYPE" + strconv.Itoa(int(t))
269}
270
271// String returns the string representation for the class c.
272func (c Class) String() string {
273	if c1, ok := ClassToString[uint16(c)]; ok {
274		return c1
275	}
276	return "CLASS" + strconv.Itoa(int(c))
277}
278
279// String returns the string representation for the name n.
280func (n Name) String() string {
281	return sprintName(string(n))
282}
283