From c3b9e95d580b4c19d05f3632dd885b31e221a66c Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 27 Aug 2012 20:58:58 +0200 Subject: [PATCH] designed finalized --- ex/axfr/axfr.go | 8 ++++-- xfr.go | 74 ++++++++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/ex/axfr/axfr.go b/ex/axfr/axfr.go index 6fa0a0af..bb599bcd 100644 --- a/ex/axfr/axfr.go +++ b/ex/axfr/axfr.go @@ -1,9 +1,9 @@ package main import ( - "github.com/miekg/dns" "flag" "fmt" + "github.com/miekg/dns" "strings" "time" ) @@ -33,7 +33,11 @@ func main() { if t, e := client.XfrReceive(m, *nameserver); e == nil { for r := range t { 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 { diff --git a/xfr.go b/xfr.go index d58fa5f1..3e44ccac 100644 --- a/xfr.go +++ b/xfr.go @@ -1,16 +1,17 @@ package dns import ( - "net" "time" ) +// TODO: axfrreceive fixen, first can go +// a tsigTimersonly to responsewriter + // XfrMsg is used when doing [IA]xfr with a remote server. type XfrMsg struct { - Reply []RR // the set of RRs in the answer section form the message of the server - Rtt time.Duration // round trip time - RemoteAddr net.Addr // address of the server - Error error // if something went wrong, this contains the error + RR []RR // the set of RRs in the answer section form the message of the server + Rtt time.Duration // round trip time + Error error // if something went wrong, this contains the error } // 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: // -// // m contains the [AI]xfr request +// // m contains the AXFR request // t, e := client.XfrReceive(m, "127.0.0.1:53") // 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) { w := new(reply) @@ -59,16 +60,16 @@ func (w *reply) axfrReceive(c chan *XfrMsg) { for { in, err := w.receive() if err != nil { - c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: err} + c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: err} return } 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 } if first { 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 } first = !first @@ -77,10 +78,10 @@ func (w *reply) axfrReceive(c chan *XfrMsg) { if !first { w.tsigTimersOnly = true // Subsequent envelopes use this. 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 } - c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: nil} + c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: nil} } } panic("not reached") @@ -94,23 +95,23 @@ func (w *reply) ixfrReceive(c chan *XfrMsg) { for { in, err := w.receive() if err != nil { - c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt, Error: err} + c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt, Error: err} return } 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 } if first { // A single SOA RR signals "no changes" 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 } // Check if the returned answer is ok 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 } // 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 v, ok := in.Answer[len(in.Answer)-1].(*RR_SOA); ok { 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 } } - c <- &XfrMsg{Reply: in.Answer, Rtt: w.rtt} + c <- &XfrMsg{RR: in.Answer, Rtt: w.rtt} } } panic("not reached") } -// XfrSend performs an outgoing Ixfr or Axfr. The function is [AI]xfr agnostic, it is -// up to the caller to correctly send the sequence of messages. -func XfrSend(w ResponseWriter, q *Msg, a string) error { - switch q.Question[0].Qtype { +// XfrSend performs an outgoing [IX]xfr depending on the request message. As +// long as the channel c is open ... TODO(mg): docs +// tsig is done, enveloping is done, voor de rest niks... TODO +func XfrSend(w ResponseWriter, req *Msg, c chan *XfrMsg) error { + switch req.Question[0].Qtype { case TypeAXFR, TypeIXFR: - // go d.xfrWrite(q, m, e) + go axfrSend(w, req, c) default: return ErrXfrType } 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 i := 0 for r := range m {