designed finalized

This commit is contained in:
Miek Gieben 2012-08-27 20:58:58 +02:00
parent 2c6eb05cf8
commit c3b9e95d58
2 changed files with 48 additions and 34 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"github.com/miekg/dns"
"flag" "flag"
"fmt" "fmt"
"github.com/miekg/dns"
"strings" "strings"
"time" "time"
) )
@ -33,7 +33,11 @@ func main() {
if t, e := client.XfrReceive(m, *nameserver); e == nil { if t, e := client.XfrReceive(m, *nameserver); e == nil {
for r := range t { for r := range t {
if r.Error == nil { if r.Error == nil {
fmt.Printf("%v\n", r.Reply) for _, rr := range r.Reply {
fmt.Printf("%v\n", rr)
}
} else {
fmt.Printf("error: %s\n", r.Error.Error())
} }
} }
} else { } else {

74
xfr.go
View File

@ -1,16 +1,17 @@
package dns package dns
import ( import (
"net"
"time" "time"
) )
// TODO: axfrreceive fixen, first can go
// a tsigTimersonly to responsewriter
// XfrMsg is used when doing [IA]xfr with a remote server. // XfrMsg is used when doing [IA]xfr with a remote server.
type XfrMsg struct { type XfrMsg struct {
Reply []RR // the set of RRs in the answer section form the message of the server RR []RR // the set of RRs in the answer section form the message of the server
Rtt time.Duration // round trip time Rtt time.Duration // round trip time
RemoteAddr net.Addr // address of the server Error error // if something went wrong, this contains the error
Error error // if something went wrong, this contains the error
} }
// XfrReceive performs a [AI]xfr request (depends on the message's Qtype). It returns // XfrReceive performs a [AI]xfr request (depends on the message's Qtype). It returns
@ -22,10 +23,10 @@ type XfrMsg struct {
// //
// Basic use pattern for receiving an AXFR: // Basic use pattern for receiving an AXFR:
// //
// // m contains the [AI]xfr request // // m contains the AXFR request
// t, e := client.XfrReceive(m, "127.0.0.1:53") // t, e := client.XfrReceive(m, "127.0.0.1:53")
// for r := range t { // for r := range t {
// // ... deal with r.Reply or r.Error // // ... deal with r.RR or r.Error
// } // }
func (c *Client) XfrReceive(q *Msg, a string) (chan *XfrMsg, error) { func (c *Client) XfrReceive(q *Msg, a string) (chan *XfrMsg, error) {
w := new(reply) w := new(reply)
@ -59,16 +60,16 @@ func (w *reply) axfrReceive(c chan *XfrMsg) {
for { for {
in, err := w.receive() in, err := w.receive()
if err != nil { if err != nil {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: err} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: err}
return return
} }
if w.req.Id != in.Id { if w.req.Id != in.Id {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: ErrId} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: ErrId}
return return
} }
if first { if first {
if !checkXfrSOA(in, true) { if !checkXfrSOA(in, true) {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: ErrXfrSoa} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: ErrXfrSoa}
return return
} }
first = !first first = !first
@ -77,10 +78,10 @@ func (w *reply) axfrReceive(c chan *XfrMsg) {
if !first { if !first {
w.tsigTimersOnly = true // Subsequent envelopes use this. w.tsigTimersOnly = true // Subsequent envelopes use this.
if checkXfrSOA(in, false) { if checkXfrSOA(in, false) {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: nil} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: nil}
return return
} }
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: nil} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: nil}
} }
} }
panic("not reached") panic("not reached")
@ -94,23 +95,23 @@ func (w *reply) ixfrReceive(c chan *XfrMsg) {
for { for {
in, err := w.receive() in, err := w.receive()
if err != nil { if err != nil {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: err} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: err}
return return
} }
if w.req.Id != in.Id { if w.req.Id != in.Id {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: ErrId} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: ErrId}
return return
} }
if first { if first {
// A single SOA RR signals "no changes" // A single SOA RR signals "no changes"
if len(in.Answer) == 1 && checkXfrSOA(in, true) { if len(in.Answer) == 1 && checkXfrSOA(in, true) {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: nil} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: nil}
return return
} }
// Check if the returned answer is ok // Check if the returned answer is ok
if !checkXfrSOA(in, true) { if !checkXfrSOA(in, true) {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: ErrXfrSoa} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: ErrXfrSoa}
return return
} }
// This serial is important // This serial is important
@ -124,38 +125,47 @@ func (w *reply) ixfrReceive(c chan *XfrMsg) {
// 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 v, ok := in.Answer[len(in.Answer)-1].(*RR_SOA); ok { if v, ok := in.Answer[len(in.Answer)-1].(*RR_SOA); ok {
if v.Serial == serial { if v.Serial == serial {
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: nil} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: nil}
return return
} }
} }
c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt} c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt}
} }
} }
panic("not reached") panic("not reached")
} }
// XfrSend performs an outgoing Ixfr or Axfr. The function is [AI]xfr agnostic, it is // XfrSend performs an outgoing [IX]xfr depending on the request message. As
// up to the caller to correctly send the sequence of messages. // long as the channel c is open ... TODO(mg): docs
func XfrSend(w ResponseWriter, q *Msg, a string) error { // tsig is done, enveloping is done, voor de rest niks... TODO
switch q.Question[0].Qtype { func XfrSend(w ResponseWriter, req *Msg, c chan *XfrMsg) error {
switch req.Question[0].Qtype {
case TypeAXFR, TypeIXFR: case TypeAXFR, TypeIXFR:
// go d.xfrWrite(q, m, e) go axfrSend(w, req, c)
default: default:
return ErrXfrType return ErrXfrType
} }
return nil return nil
} }
func axfrSend(w ResponseWriter, req *Msg, c chan *XfrMsg) {
rep := new(Msg)
rep.SetReply(req)
rep.MsgHdr.Authoritative = true
w.tsigTimersOnly = false
for x := range c {
// assume is fits
rep.Answer = append(rep.Answer, x.RR...)
w.Write(rep)
if !w.tsigTimersOnly {
w.tsigTimersOnly = !w.tsigTimersOnly
}
rep.Answer = nil
}
}
/* /*
// Just send the zone
func (d *Conn) axfrSend(q *Msg, m chan *Xfr, e chan os.Error) {
out := new(Msg)
out.Id = q.Id
out.Question = q.Question
out.Answer = make([]RR, 1001) // TODO(mg) look at this number
out.MsgHdr.Response = true
out.MsgHdr.Authoritative = true
first := true
var soa *RR_SOA var soa *RR_SOA
i := 0 i := 0
for r := range m { for r := range m {