it works very nicely

This commit is contained in:
Miek Gieben 2011-03-21 16:28:13 +01:00
parent d67c427953
commit e47ebb2e4c
4 changed files with 89 additions and 245 deletions

View File

@ -7,7 +7,6 @@ include $(GOROOT)/src/Make.inc
TARG=dns TARG=dns
GOFILES=\ GOFILES=\
xfr.go\
config.go\ config.go\
defaults.go\ defaults.go\
dns.go\ dns.go\
@ -21,6 +20,7 @@ GOFILES=\
string.go\ string.go\
tsig.go\ tsig.go\
types.go\ types.go\
xfr.go\
# y.go\ # y.go\
include $(GOROOT)/src/Make.pkg include $(GOROOT)/src/Make.pkg

2
TODO
View File

@ -5,8 +5,6 @@ o clean, small API
o fast data structures (rb-tree, when they come available) o fast data structures (rb-tree, when they come available)
o api-use should lead to self documenting code o api-use should lead to self documenting code
o compression (only ownernames?) o compression (only ownernames?)
o Xfr, part of (d *Conn).Xfr(m, channel)
Todo: Todo:

View File

@ -97,133 +97,10 @@ type Xfr struct {
} }
func (res *Resolver) Xfr(q *Msg, t *Tsig, m chan Xfr) { func (res *Resolver) Xfr(q *Msg, t *Tsig, m chan Xfr) {
switch q.Question[0].Qtype {
case TypeAXFR:
res.axfr(q, t, m)
case TypeIXFR:
res.ixfr(q, t, m)
default:
// wrong request
return
}
}
// Start an IXFR, q should contain a *Msg with the question
// for an IXFR: "miek.nl" ANY IXFR. RRs that should be added
// have Xfr.Add set to true otherwise it is false.
// Channel m is closed when the IXFR ends.
func (res *Resolver) ixfr(q *Msg, t *Tsig, m chan Xfr) {
var (
x Xfr
inb []byte
)
in := new(Msg)
port, err := check(res, q) port, err := check(res, q)
if err != nil { if err != nil {
return return
} }
defer close(m)
sending, ok := q.Pack()
if !ok {
return
}
Server:
for i := 0; i < len(res.Servers); i++ {
server := res.Servers[i] + ":" + port
c, err := net.Dial("tcp", "", server)
if err != nil {
continue Server
}
var serial uint32 // The first serial seen is the current server serial
d := new(Conn)
d.TCP = c.(*net.TCPConn)
d.Addr = d.TCP.RemoteAddr()
first := true
defer c.Close()
for {
if first {
inb, err = d.Exchange(sending, false)
} else {
inb, err = d.Exchange(sending, true)
}
if err != nil {
c.Close()
continue Server
}
in.Unpack(inb)
if in.Id != q.Id {
return
}
if first {
// A single SOA RR signals "no changes"
if len(in.Answer) == 1 && checkXfrSOA(in, true) {
return
}
// But still check if the returned answer is ok
if !checkXfrSOA(in, true) {
c.Close()
continue Server
}
// This serial is important
serial = in.Answer[0].(*RR_SOA).Serial
first = !first
}
// Now we need to check each message for SOA records, to see what we need to do
x.Add = true
if !first {
for k, r := range in.Answer {
// If the last record in the IXFR contains the servers' SOA, we should quit
if r.Header().Rrtype == TypeSOA {
switch {
case r.(*RR_SOA).Serial == serial:
if k == len(in.Answer)-1 {
// last rr is SOA with correct serial
//m <- r dont' send it
return
}
x.Add = true
if k != 0 {
// Intermediate SOA
continue
}
case r.(*RR_SOA).Serial != serial:
x.Add = false
continue // Don't need to see this SOA
}
}
x.RR = r
m <- x
}
}
return
}
panic("not reached")
return
}
return
}
// Start an AXFR, q should contain a message with the question
// for an AXFR: "miek.nl" ANY AXFR. The closing SOA isn't
// returned over the channel, so the caller will receive
// the zone as-is. Xfr.Add is always true.
// The channel is closed to signal the end of the AXFR.
func (res *Resolver) axfr(q *Msg, t *Tsig, m chan Xfr) {
var inb []byte
in := new(Msg)
port, err := check(res, q)
if err != nil {
return
}
defer close(m)
sending, ok := q.Pack() sending, ok := q.Pack()
if !ok { if !ok {
return return
@ -241,77 +118,15 @@ Server:
d.Addr = d.TCP.RemoteAddr() d.Addr = d.TCP.RemoteAddr()
d.Tsig = t d.Tsig = t
first := true _, err = d.Write(sending)
defer c.Close() // TODO(mg): if not open?
for {
if first {
inb, err = d.Exchange(sending, false)
} else {
inb, err = d.Exchange(sending, true)
}
if err != nil { if err != nil {
c.Close() println(err.String())
continue Server
} }
if !in.Unpack(inb) { d.XfrRead(q, m) // check
return
}
if in.Id != q.Id {
return
}
if first {
if !checkXfrSOA(in, true) {
c.Close()
continue Server
}
first = !first
}
if !first {
d.Tsig.TimersOnly = true
if !checkXfrSOA(in, false) {
// Soa record not the last one
sendMsg(in, m, false)
continue
} else {
sendMsg(in, m, true)
return
}
}
}
panic("not reached")
return
} }
return return
} }
// Check if he SOA record exists in the Answer section of
// the packet. If first is true the first RR must be a soa
// if false, the last one should be a SOA
func checkXfrSOA(in *Msg, first bool) bool {
if len(in.Answer) > 0 {
if first {
return in.Answer[0].Header().Rrtype == TypeSOA
} else {
return in.Answer[len(in.Answer)-1].Header().Rrtype == TypeSOA
}
}
return false
}
// Send the answer section to the channel
func sendMsg(in *Msg, c chan Xfr, nosoa bool) {
x := Xfr{Add: true}
for k, r := range in.Answer {
if nosoa && k == len(in.Answer)-1 {
continue
}
x.RR = r
c <- x
}
}
// Some assorted checks on the resolver // Some assorted checks on the resolver
func check(res *Resolver, q *Msg) (port string, err os.Error) { func check(res *Resolver, q *Msg) (port string, err os.Error) {
if res.Port == "" { if res.Port == "" {

37
xfr.go
View File

@ -18,7 +18,7 @@ func (d *Conn) XfrWrite(q *Msg, m chan Xfr) {
case TypeAXFR: case TypeAXFR:
d.axfrWrite(q, m) d.axfrWrite(q, m)
case TypeIXFR: case TypeIXFR:
// d.ixfrWrite(q, m) // d.ixfrWrite(q, m)
} }
} }
@ -49,6 +49,9 @@ func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
} }
if !first { if !first {
if d.Tsig != nil {
d.Tsig.TimersOnly = true // Subsequent envelopes use this
}
if !checkXfrSOA(in, false) { if !checkXfrSOA(in, false) {
// Soa record not the last one // Soa record not the last one
sendMsg(in, m, false) sendMsg(in, m, false)
@ -57,7 +60,6 @@ func (d *Conn) axfrRead(q *Msg, m chan Xfr) {
sendMsg(in, m, true) sendMsg(in, m, true)
return return
} }
d.Tsig.TimersOnly = true // Subsequent envelopes use this
} }
} }
panic("not reached") panic("not reached")
@ -70,7 +72,7 @@ func (d *Conn) axfrWrite(q *Msg, m chan Xfr) {
out.Id = q.Id out.Id = q.Id
out.Question = q.Question out.Question = q.Question
out.Answer = make([]RR, 1000) out.Answer = make([]RR, 1000)
var soa *RR_SOA; var soa *RR_SOA
i := 0 i := 0
for r := range m { for r := range m {
out.Answer[i] = r.RR out.Answer[i] = r.RR
@ -92,6 +94,7 @@ func (d *Conn) axfrWrite(q *Msg, m chan Xfr) {
i = 0 i = 0
out.Answer = out.Answer[:0] out.Answer = out.Answer[:0]
} }
// TimersOnly foo
} }
// Everything is send, only the closing soa is left. // Everything is send, only the closing soa is left.
out.Answer[i] = soa out.Answer[i] = soa
@ -146,7 +149,9 @@ func (d *Conn) ixfrRead(q *Msg, m chan Xfr) {
// 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
x.Add = true x.Add = true
if !first { if !first {
if d.Tsig != nil {
d.Tsig.TimersOnly = true d.Tsig.TimersOnly = true
}
for k, r := range in.Answer { for k, r := range in.Answer {
// If the last record in the IXFR contains the servers' SOA, we should quit // If the last record in the IXFR contains the servers' SOA, we should quit
if r.Header().Rrtype == TypeSOA { if r.Header().Rrtype == TypeSOA {
@ -175,3 +180,29 @@ func (d *Conn) ixfrRead(q *Msg, m chan Xfr) {
panic("not reached") panic("not reached")
return return
} }
// Check if he SOA record exists in the Answer section of
// the packet. If first is true the first RR must be a soa
// if false, the last one should be a SOA
func checkXfrSOA(in *Msg, first bool) bool {
if len(in.Answer) > 0 {
if first {
return in.Answer[0].Header().Rrtype == TypeSOA
} else {
return in.Answer[len(in.Answer)-1].Header().Rrtype == TypeSOA
}
}
return false
}
// Send the answer section to the channel
func sendMsg(in *Msg, c chan Xfr, nosoa bool) {
x := Xfr{Add: true}
for k, r := range in.Answer {
if nosoa && k == len(in.Answer)-1 {
continue
}
x.RR = r
c <- x
}
}