Ixfr works, but needs to be refined

This commit is contained in:
Miek Gieben 2011-02-25 23:29:08 +01:00
parent 22f34ec885
commit 157062f6b0
3 changed files with 65 additions and 68 deletions

View File

@ -10,26 +10,28 @@ import (
func main() { func main() {
var serial *int = flag.Int("serial", 0, "Perform an IXFR with the given serial") var serial *int = flag.Int("serial", 0, "Perform an IXFR with the given serial")
var onesoa *bool = flag.Bool("1soa", false, "Don't print the last SOA") var onesoa *bool = flag.Bool("1soa", false, "Don't print the last SOA")
zone := "tjeb.nl."
flag.Parse() flag.Parse()
res := new(dns.Resolver) res := new(dns.Resolver)
res.FromFile("/etc/resolv.conf") res.FromFile("/etc/resolv.conf")
res.Servers[0] = "open.nlnetlabs.nl"
ch := make(chan dns.RR) ch := make(chan dns.RR)
m := new(dns.Msg) m := new(dns.Msg)
m.Question = make([]dns.Question, 1) m.Question = make([]dns.Question, 1)
if *serial > 0 { if *serial > 0 {
m.Question[0] = dns.Question{"miek.nl.", dns.TypeIXFR, dns.ClassINET} m.Question[0] = dns.Question{zone, dns.TypeIXFR, dns.ClassINET}
soa := new(dns.RR_SOA) soa := new(dns.RR_SOA)
soa.Hdr = dns.RR_Header{"miek.nl.", dns.TypeSOA, dns.ClassINET, 14400, 0} soa.Hdr = dns.RR_Header{zone, dns.TypeSOA, dns.ClassINET, 14400, 0}
soa.Serial = uint32(*serial) soa.Serial = uint32(*serial)
m.Ns = make([]dns.RR, 1) m.Ns = make([]dns.RR, 1)
m.Ns[0] = soa m.Ns[0] = soa
go res.Ixfr(m, ch) go res.Ixfr(m, ch)
} else { } else {
m.Question[0] = dns.Question{"miek.nl.", dns.TypeAXFR, dns.ClassINET} m.Question[0] = dns.Question{zone, dns.TypeAXFR, dns.ClassINET}
go res.Axfr(m, ch) go res.Axfr(m, ch)
} }

View File

@ -15,9 +15,6 @@ func main() {
fmt.Printf("%s DOMAIN\n", os.Args[0]) fmt.Printf("%s DOMAIN\n", os.Args[0])
os.Exit(1) os.Exit(1)
} }
dns.Test()
m := new(dns.Msg) m := new(dns.Msg)
m.MsgHdr.RecursionDesired = true //only set this bit m.MsgHdr.RecursionDesired = true //only set this bit
m.Question = make([]dns.Question, 1) m.Question = make([]dns.Question, 1)

View File

@ -120,13 +120,22 @@ func (res *Resolver) Ixfr(q *Msg, m chan RR) {
return return
} }
const ( const (
FIRST = iota FIRST = iota
SECOND SECOND
LAST LAST
) )
defer close(m)
tAdd := new(RR_TXT)
tAdd.Hdr = RR_Header{Name: "miek.nl", Rrtype: TypeTXT, Class: ClassINET, Ttl: 3600}
tAdd.Txt = "Add"
tRem := new(RR_TXT)
tRem.Hdr = RR_Header{Name: "miek.nl", Rrtype: TypeTXT, Class: ClassINET, Ttl: 3600}
tRem.Txt = "Rem"
defer close(m)
Server: Server:
for i := 0; i < len(res.Servers); i++ { for i := 0; i < len(res.Servers); i++ {
server := res.Servers[i] + ":" + port server := res.Servers[i] + ":" + port
@ -136,10 +145,10 @@ Server:
continue Server continue Server
} }
state := FIRST state := FIRST
var serial uint32 // The first serial seen is the current server serial var serial uint32 // The first serial seen is the current server serial
var _ = serial var _ = serial
defer c.Close() defer c.Close()
for { for {
if state == FIRST { if state == FIRST {
in, cerr = exchangeTCP(c, sending, res, true) in, cerr = exchangeTCP(c, sending, res, true)
@ -154,47 +163,51 @@ Server:
continue Server continue Server
} }
if in.Id != q.Id { if in.Id != q.Id {
// Query ID mismatch // Query ID mismatch
c.Close() c.Close()
return return
} }
if state == FIRST { if state == FIRST {
// A single SOA RR signals "no changes" // A single SOA RR signals "no changes"
if len(in.Answer) == 1 && checkAxfrSOA(in, true) { if len(in.Answer) == 1 && checkAxfrSOA(in, true) {
c.Close() //sendFromMsg(in, m) // Do you need to send 1 reply?
return c.Close()
} return
}
// But still check if the returned answer is ok // But still check if the returned answer is ok
if !checkAxfrSOA(in, true) { if !checkAxfrSOA(in, true) {
c.Close() c.Close()
continue Server continue Server
} }
// This serial is important // This serial is important
serial = in.Answer[0].(*RR_SOA).Serial serial = in.Answer[0].(*RR_SOA).Serial
sendFromMsg(in, m) //sendFromMsg(in, m)
state = SECOND state = SECOND
} }
// Now we need to check each message for SOA records, to see what we need to do // Now we need to check each message for SOA records, to see what we need to do
if state != FIRST { if state != FIRST {
// If the last record in the IXFR contains the servers' SOA // If the last record in the IXFR contains the servers' SOA
// we should quit // we should quit
for k, r := range in.Answer {
// for _, r := range in.Answer { if r.Header().Rrtype == TypeSOA {
se := r.(*RR_SOA).Serial
switch {
// } case se == serial:
if k == len(in.Answer)-1 {
if !checkAxfrSOA(in, false) { // last rr is SOA with correct serial
// Soa record not the last one m <- r
sendFromMsg(in, m) return
continue }
} else{ m <- tAdd
sendFromMsg(in, m) case se != serial:
return m <- tRem
} }
}
m <- r
}
} }
} }
panic("not reached") panic("not reached")
@ -229,7 +242,7 @@ func (res *Resolver) Axfr(q *Msg, m chan RR) {
return return
} }
defer close(m) defer close(m)
Server: Server:
for i := 0; i < len(res.Servers); i++ { for i := 0; i < len(res.Servers); i++ {
server := res.Servers[i] + ":" + port server := res.Servers[i] + ":" + port
@ -239,7 +252,7 @@ Server:
continue Server continue Server
} }
first := true first := true
defer c.Close() // TODO(mg): if not open? defer c.Close() // TODO(mg): if not open?
for { for {
if first { if first {
in, cerr = exchangeTCP(c, sending, res, true) in, cerr = exchangeTCP(c, sending, res, true)
@ -250,17 +263,17 @@ Server:
if cerr != nil { if cerr != nil {
// Failed to send, try the next // Failed to send, try the next
err = cerr err = cerr
c.Close() c.Close()
continue Server continue Server
} }
if in.Id != q.Id { if in.Id != q.Id {
c.Close() c.Close()
return return
} }
if first { if first {
if !checkAxfrSOA(in, true) { if !checkAxfrSOA(in, true) {
c.Close() c.Close()
continue Server continue Server
} }
sendFromMsg(in, m) sendFromMsg(in, m)
@ -270,10 +283,10 @@ Server:
if !first { if !first {
if !checkAxfrSOA(in, false) { if !checkAxfrSOA(in, false) {
// Soa record not the last one // Soa record not the last one
sendFromMsg(in, m) sendFromMsg(in, m)
continue continue
} else { } else {
sendFromMsg(in, m) sendFromMsg(in, m)
return return
} }
} }
@ -456,24 +469,9 @@ func checkAxfrSOA(in *Msg, first bool) bool {
return false return false
} }
// Same as Axfr one, but now also check the serial
func checkIxfrSOA(in *Msg, first bool, serial uint32) bool {
if len(in.Answer) > 0 {
if first {
return in.Answer[0].Header().Rrtype == TypeSOA &&
in.Answer[0].(*RR_SOA).Serial == serial
} else {
return in.Answer[len(in.Answer)-1].Header().Rrtype == TypeSOA &&
in.Answer[len(in.Answer)-1].(*RR_SOA).Serial == serial
}
}
return false
}
// Send the answer section to the channel // Send the answer section to the channel
func sendFromMsg(in *Msg, c chan RR) { func sendFromMsg(in *Msg, c chan RR) {
for _, r := range in.Answer { for _, r := range in.Answer {
c <- r c <- r
} }
} }