dns/defaults.go

113 lines
2.9 KiB
Go

package dns
// Everything is assumed in the ClassINET class. If
// you need other classes you are on your own.
// Create a reply packet from a request message.
func (dns *Msg) SetReply(request *Msg) {
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]
}
// Create a question packet.
func (dns *Msg) SetQuestion(z string, t uint16) {
dns.MsgHdr.Id = Id()
dns.MsgHdr.RecursionDesired = true
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, t, ClassINET}
}
// Create a notify packet.
func (dns *Msg) SetNotify(z string) {
dns.MsgHdr.Opcode = OpcodeNotify
dns.MsgHdr.Authoritative = true
dns.MsgHdr.Id = Id()
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, TypeSOA, ClassINET}
}
// Is the message a dynamic update packet?
func (dns *Msg) IsUpdate() (ok bool) {
if len(dns.Question) == 0 {
return false
}
ok = dns.MsgHdr.Opcode == OpcodeUpdate
ok = ok && dns.Question[0].Qtype == TypeSOA
return
}
// Is the message a valid notify packet?
func (dns *Msg) IsNotify() (ok bool) {
if len(dns.Question) == 0 {
return false
}
ok = dns.MsgHdr.Opcode == OpcodeNotify
ok = ok && dns.Question[0].Qclass == ClassINET
ok = ok && dns.Question[0].Qtype == TypeSOA
return
}
// Create a dns msg suitable for requesting an ixfr.
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
}
// Create a dns msg suitable for requesting an axfr.
func (dns *Msg) SetAxfr(z string) {
dns.MsgHdr.Id = Id()
dns.Question = make([]Question, 1)
dns.Question[0] = Question{z, TypeAXFR, ClassINET}
}
// Is the message a valid axfr request packet?
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
return
}
// Is the message a valid ixfr request packet?
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
return
}
// Has a message a TSIG record as the last record?
func (dns *Msg) IsTsig() (ok bool) {
if len(dns.Extra) > 0 {
return dns.Extra[0].Header().Rrtype == TypeTSIG
}
return
}
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)
}