From 60c66fa9adc84c134e6b30f55e81de6e53501eda Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 4 Jul 2011 23:33:06 +0200 Subject: [PATCH] fix chaos Perform the a and aaaa query in parallel --- _examples/Makefile | 12 +++---- _examples/chaos/chaos.go | 67 +++++++++++++++++++++++----------------- client_test.go | 2 +- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/_examples/Makefile b/_examples/Makefile index 007470cf..f54db1e8 100644 --- a/_examples/Makefile +++ b/_examples/Makefile @@ -1,12 +1,12 @@ EXAMPLES=mx \ -q \ chaos \ -axfr \ -reflect \ -xfrprx \ key2ds \ - -# funkensturm -- need fix +axfr +# these need to be fixed +#q \ +#reflect \ +#xfrprx \ +#funkensturm all: for i in $(EXAMPLES); do gomake -C $$i; done diff --git a/_examples/chaos/chaos.go b/_examples/chaos/chaos.go index 73219e40..ddc167bb 100644 --- a/_examples/chaos/chaos.go +++ b/_examples/chaos/chaos.go @@ -40,35 +40,46 @@ func main() { } } +func qhandler(w dns.RequestWriter, m *dns.Msg) { + w.Send(m) + r, _ := w.Receive() + w.Write(r) +} + func addresses(conf *dns.ClientConfig, c *dns.Client, name string) []string { - m := new(dns.Msg) - m.SetQuestion(os.Args[1], dns.TypeA) // Allocates space + dns.HandleQueryFunc(os.Args[1], qhandler) + dns.ListenAndQuery(nil, nil) + + m4 := new(dns.Msg) + m4.SetQuestion(os.Args[1], dns.TypeA) + m6 := new(dns.Msg) + m6.SetQuestion(os.Args[1], dns.TypeAAAA) + c.Do(m4, conf.Servers[0]) // Also 1 and 2 (and merge the results?? + c.Do(m6, conf.Servers[0]) + var ips []string - - r := c.Exchange(m, conf.Servers[0]) - if r == nil { - fmt.Printf("Nothing recevied for %s\n", name) - return nil - } - if r.Rcode != dns.RcodeSuccess { - return nil - } - for _, a := range r.Answer { - ips = append(ips, a.(*dns.RR_A).A.String()+":53") - } - - m.SetQuestion(os.Args[1], dns.TypeAAAA) - r = c.Exchange(m, conf.Servers[0]) - if r == nil { - fmt.Printf("Nothing recevied for %s\n", name) - return ips - } - if r.Rcode != dns.RcodeSuccess { - return ips - } - for _, a := range r.Answer { - ips = append(ips, "["+a.(*dns.RR_AAAA).AAAA.String()+"]:53") - } - + i := 2 // two outstanding queries +forever: + for { + select { + case r := <-dns.DefaultReplyChan: + if r[1] !=nil && r[1].Rcode == dns.RcodeSuccess { + for _, aa := range r[1].Answer { + switch aa.(type) { + case *dns.RR_A: + ips = append(ips, aa.(*dns.RR_A).A.String()+":53") + case *dns.RR_AAAA: + ips = append(ips, "[" + aa.(*dns.RR_AAAA).AAAA.String()+"]:53") + } + } + } else { + fmt.Printf("Nothing recevied for %s\n", name) + } + i-- + if i == 0 { + break forever + } + } + } return ips } diff --git a/client_test.go b/client_test.go index bf492623..a6373a72 100644 --- a/client_test.go +++ b/client_test.go @@ -27,7 +27,7 @@ func helloMiek(w RequestWriter, r *Msg) { func TestClientASync(t *testing.T) { HandleQueryFunc("miek.nl", helloMiek) // All queries for miek.nl will be handled by HelloMiek - ListenAndQuery(nil, nil) + ListenAndQuery(nil, nil) // Detect if this isn't running m := new(Msg) m.SetQuestion("miek.nl", TypeSOA)