2011-03-16 22:19:15 +11:00
|
|
|
package dns
|
|
|
|
|
2011-03-30 20:14:36 +11:00
|
|
|
// Everything is assumed in the ClassINET class. If
|
|
|
|
// you need other classes you are on your own.
|
2011-03-23 06:12:36 +11:00
|
|
|
|
2011-03-25 00:29:23 +11:00
|
|
|
// Create a reply packet from a request message.
|
2011-03-23 19:50:38 +11:00
|
|
|
func (dns *Msg) SetReply(request *Msg) {
|
2011-03-24 19:24:24 +11:00
|
|
|
dns.MsgHdr.Id = request.MsgHdr.Id
|
|
|
|
dns.MsgHdr.Authoritative = true
|
|
|
|
dns.MsgHdr.Response = true
|
|
|
|
dns.MsgHdr.Opcode = OpcodeQuery
|
|
|
|
dns.MsgHdr.Rcode = RcodeSuccess
|
|
|
|
dns.Question = make([]Question, 1)
|
|
|
|
dns.Question[0] = request.Question[0]
|
2011-03-22 08:53:15 +11:00
|
|
|
}
|
|
|
|
|
2011-06-14 16:32:03 +10:00
|
|
|
// Create a question packet.
|
2011-04-15 06:22:24 +10:00
|
|
|
func (dns *Msg) SetQuestion(z string, t uint16) {
|
2011-07-06 04:52:35 +10:00
|
|
|
dns.MsgHdr.Id = Id()
|
|
|
|
dns.MsgHdr.RecursionDesired = true
|
2011-04-15 06:22:24 +10:00
|
|
|
dns.Question = make([]Question, 1)
|
|
|
|
dns.Question[0] = Question{z, t, ClassINET}
|
|
|
|
}
|
|
|
|
|
2011-03-17 03:28:35 +11:00
|
|
|
// Create a notify packet.
|
2011-03-31 02:50:07 +11:00
|
|
|
func (dns *Msg) SetNotify(z string) {
|
2011-03-16 22:19:15 +11:00
|
|
|
dns.MsgHdr.Opcode = OpcodeNotify
|
|
|
|
dns.MsgHdr.Authoritative = true
|
|
|
|
dns.MsgHdr.Id = Id()
|
|
|
|
dns.Question = make([]Question, 1)
|
2011-03-31 02:50:07 +11:00
|
|
|
dns.Question[0] = Question{z, TypeSOA, ClassINET}
|
2011-03-16 22:19:15 +11:00
|
|
|
}
|
|
|
|
|
2011-07-27 03:16:28 +10:00
|
|
|
// Create an error packet.
|
|
|
|
func (dns *Msg) SetRcode(request *Msg, rcode int) {
|
|
|
|
dns.MsgHdr.Rcode = rcode
|
|
|
|
dns.MsgHdr.Opcode = OpcodeQuery
|
|
|
|
dns.MsgHdr.Response = true
|
|
|
|
dns.MsgHdr.Authoritative = false
|
|
|
|
dns.MsgHdr.Id = request.MsgHdr.Id
|
|
|
|
dns.Question = make([]Question, 1)
|
|
|
|
dns.Question[0] = request.Question[0]
|
|
|
|
}
|
|
|
|
|
2011-07-27 05:41:24 +10:00
|
|
|
// Create a packet with FormError set.
|
2011-07-27 03:16:28 +10:00
|
|
|
func (dns *Msg) SetRcodeFormatError(request *Msg) {
|
|
|
|
dns.MsgHdr.Rcode = RcodeFormatError
|
|
|
|
dns.MsgHdr.Opcode = OpcodeQuery
|
|
|
|
dns.MsgHdr.Response = true
|
|
|
|
dns.MsgHdr.Authoritative = false
|
|
|
|
dns.MsgHdr.Id = request.MsgHdr.Id
|
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// SetUpdate makes the message a dynamic update packet. It
|
|
|
|
// sets the ZONE section to: z, TypeSOA, classINET.
|
|
|
|
func (dns *Msg) SetUpdate(z string) {
|
|
|
|
dns.MsgHdr.Id = Id()
|
|
|
|
dns.MsgHdr.Opcode = OpcodeUpdate
|
|
|
|
dns.Question = make([]Question, 1)
|
|
|
|
dns.Question[0] = Question{z, TypeSOA, ClassINET}
|
|
|
|
}
|
|
|
|
|
2011-09-11 00:50:27 +10:00
|
|
|
// SetIxfr creates dns msg suitable for requesting an ixfr.
|
2011-09-02 22:28:40 +10:00
|
|
|
func (dns *Msg) SetIxfr(z string, serial uint32) {
|
|
|
|
dns.MsgHdr.Id = Id()
|
|
|
|
dns.Question = make([]Question, 1)
|
|
|
|
dns.Ns = make([]RR, 1)
|
|
|
|
s := new(RR_SOA)
|
|
|
|
s.Hdr = RR_Header{z, TypeSOA, ClassINET, DefaultTTL, 0}
|
|
|
|
s.Serial = serial
|
|
|
|
|
|
|
|
dns.Question[0] = Question{z, TypeIXFR, ClassINET}
|
|
|
|
dns.Ns[0] = s
|
|
|
|
}
|
|
|
|
|
2011-09-11 00:50:27 +10:00
|
|
|
// SetAxfr creates dns msg suitable for requesting an ixfr.
|
2011-09-02 22:28:40 +10:00
|
|
|
func (dns *Msg) SetAxfr(z string) {
|
|
|
|
dns.MsgHdr.Id = Id()
|
|
|
|
dns.Question = make([]Question, 1)
|
|
|
|
dns.Question[0] = Question{z, TypeAXFR, ClassINET}
|
|
|
|
}
|
|
|
|
|
2011-09-11 00:50:27 +10:00
|
|
|
// SetTsig Calculates and appends a TSIG RR to the message.
|
2011-09-02 22:28:40 +10:00
|
|
|
func (dns *Msg) SetTsig(z, algo string, fudge uint16, timesigned uint64) {
|
|
|
|
t := new(RR_TSIG)
|
|
|
|
t.Hdr = RR_Header{z, TypeTSIG, ClassANY, 0, 0}
|
|
|
|
t.Algorithm = algo
|
|
|
|
t.Fudge = fudge
|
|
|
|
t.TimeSigned = timesigned
|
|
|
|
dns.Extra = append(dns.Extra, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRcode checks if the header of the packet has rcode set.
|
|
|
|
func (dns *Msg) IsRcode(rcode int) (ok bool) {
|
|
|
|
if len(dns.Question) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok = dns.MsgHdr.Rcode == rcode
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-08-22 21:06:34 +10:00
|
|
|
// IsQuestion returns true if the the packet is a question.
|
2011-08-22 20:52:13 +10:00
|
|
|
func (dns *Msg) IsQuestion() (ok bool) {
|
|
|
|
if len(dns.Question) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok = dns.MsgHdr.Response == false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// IsRcodeFormatError checks if the message has the FormErr set.
|
2011-07-27 05:41:24 +10:00
|
|
|
func (dns *Msg) IsRcodeFormatError() (ok bool) {
|
|
|
|
if len(dns.Question) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok = dns.MsgHdr.Rcode == RcodeFormatError
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// IsUpdate checks if the message is a dynamic update packet.
|
2011-03-25 20:16:55 +11:00
|
|
|
func (dns *Msg) IsUpdate() (ok bool) {
|
|
|
|
if len(dns.Question) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2011-07-06 04:52:35 +10:00
|
|
|
ok = dns.MsgHdr.Opcode == OpcodeUpdate
|
|
|
|
ok = ok && dns.Question[0].Qtype == TypeSOA
|
|
|
|
return
|
2011-03-25 20:16:55 +11:00
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// IsNotify checks if the message is a valid notify packet.
|
2011-03-23 19:50:38 +11:00
|
|
|
func (dns *Msg) IsNotify() (ok bool) {
|
2011-03-16 22:19:15 +11:00
|
|
|
if len(dns.Question) == 0 {
|
2011-03-23 19:50:38 +11:00
|
|
|
return false
|
2011-03-16 22:19:15 +11:00
|
|
|
}
|
2011-03-23 19:50:38 +11:00
|
|
|
ok = dns.MsgHdr.Opcode == OpcodeNotify
|
2011-03-16 22:19:15 +11:00
|
|
|
ok = ok && dns.Question[0].Qclass == ClassINET
|
|
|
|
ok = ok && dns.Question[0].Qtype == TypeSOA
|
2011-04-19 06:08:12 +10:00
|
|
|
return
|
2011-03-16 22:19:15 +11:00
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// IsAxfr checks if the message is a valid axfr request packet.
|
2011-03-23 20:48:21 +11:00
|
|
|
func (dns *Msg) IsAxfr() (ok bool) {
|
|
|
|
if len(dns.Question) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok = dns.MsgHdr.Opcode == OpcodeQuery
|
|
|
|
ok = ok && dns.Question[0].Qclass == ClassINET
|
|
|
|
ok = ok && dns.Question[0].Qtype == TypeAXFR
|
2011-04-19 06:08:12 +10:00
|
|
|
return
|
2011-03-23 20:48:21 +11:00
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// IsIXfr checks if the message is a valid ixfr request packet.
|
2011-03-23 20:48:21 +11:00
|
|
|
func (dns *Msg) IsIxfr() (ok bool) {
|
|
|
|
if len(dns.Question) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok = dns.MsgHdr.Opcode == OpcodeQuery
|
|
|
|
ok = ok && dns.Question[0].Qclass == ClassINET
|
|
|
|
ok = ok && dns.Question[0].Qtype == TypeIXFR
|
2011-04-19 06:08:12 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-02 22:28:40 +10:00
|
|
|
// IsTsig checks if the message has a TSIG record as the last record.
|
2011-04-19 06:08:12 +10:00
|
|
|
func (dns *Msg) IsTsig() (ok bool) {
|
2011-07-06 04:52:35 +10:00
|
|
|
if len(dns.Extra) > 0 {
|
2011-09-11 00:50:27 +10:00
|
|
|
return dns.Extra[len(dns.Extra)-1].Header().Rrtype == TypeTSIG
|
2011-07-06 04:52:35 +10:00
|
|
|
}
|
|
|
|
return
|
2011-04-19 06:08:12 +10:00
|
|
|
}
|
|
|
|
|
2011-07-25 01:08:33 +10:00
|
|
|
// IsDomainName checks if s is a valid domainname.
|
|
|
|
func IsDomainName(s string) bool { // copied from net package.
|
|
|
|
// See RFC 1035, RFC 3696.
|
|
|
|
if len(s) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(s) > 255 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s[len(s)-1] != '.' { // simplify checking loop: make name end in dot
|
|
|
|
s += "."
|
|
|
|
}
|
|
|
|
|
|
|
|
last := byte('.')
|
|
|
|
ok := false // ok once we've seen a letter
|
|
|
|
partlen := 0
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
c := s[i]
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return false
|
2011-07-25 06:21:48 +10:00
|
|
|
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_' || c == '*':
|
2011-07-25 01:08:33 +10:00
|
|
|
ok = true
|
|
|
|
partlen++
|
|
|
|
case '0' <= c && c <= '9':
|
|
|
|
// fine
|
|
|
|
partlen++
|
|
|
|
case c == '-':
|
|
|
|
// byte before dash cannot be dot
|
|
|
|
if last == '.' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
partlen++
|
|
|
|
case c == '.':
|
|
|
|
// byte before dot cannot be dot, dash
|
|
|
|
if last == '.' || last == '-' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if partlen > 63 || partlen == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
partlen = 0
|
|
|
|
}
|
|
|
|
last = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of labels in a domain name.
|
|
|
|
// Need to add these kind of function in a structured way. TODO(mg)
|
|
|
|
func Labels(a string) (c uint8) {
|
|
|
|
// walk the string and count the dots
|
|
|
|
// except when it is escaped
|
|
|
|
esc := false
|
|
|
|
for _, v := range a {
|
|
|
|
switch v {
|
|
|
|
case '.':
|
|
|
|
if esc {
|
|
|
|
esc = !esc
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c++
|
|
|
|
case '\\':
|
|
|
|
esc = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|