From 7b4a5eca06bcba0536cb05114227cb9aff888ec7 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 4 Jul 2011 20:57:19 +0200 Subject: [PATCH] fix key2ds.go --- _examples/key2ds/key2ds.go | 58 ++++++++++++++++++++++---------------- client_test.go | 7 ----- clientconfig.go | 1 + 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/_examples/key2ds/key2ds.go b/_examples/key2ds/key2ds.go index 5cd1bcf0..6ee1230f 100644 --- a/_examples/key2ds/key2ds.go +++ b/_examples/key2ds/key2ds.go @@ -1,6 +1,8 @@ package main // Print the DNSKEY records of a domain as DS records +// Twist with all the other tools that can do this. Do +// this directly from the internet. // (c) Miek Gieben - 2011 import ( "dns" @@ -9,37 +11,43 @@ import ( ) func main() { - c, err := dns.ClientConfigFromFile("/etc/resolv.conf") + conf, err := dns.ClientConfigFromFile("/etc/resolv.conf") if len(os.Args) != 2 || err != nil { fmt.Printf("%s DOMAIN\n", os.Args[0]) os.Exit(1) } m := new(dns.Msg) - m.MsgHdr.RecursionDesired = true //only set this bit - m.Question = make([]dns.Question, 1) - m.Question[0] = dns.Question{os.Args[1], dns.TypeDNSKEY, dns.ClassINET} + m.SetQuestion(os.Args[1], dns.TypeDNSKEY) - d := new(dns.Conn) - d.RemoteAddr = c.Servers[0] - in, err := dns.SimpleQuery("udp", d, m) - if in != nil { - if in.Rcode != dns.RcodeSuccess { - fmt.Printf(" *** invalid answer name %s after DNSKEY query for %s\n", os.Args[1], os.Args[1]) - os.Exit(1) + // Set EDNS0's Do bit + e := new(dns.RR_OPT) + e.Hdr.Name = "." + e.Hdr.Rrtype = dns.TypeOPT + e.SetUDPSize(2048) + e.SetDo() + m.Extra = append(m.Extra, e) + + c := dns.NewClient() + r := c.Exchange(m, conf.Servers[0]) + if r == nil { + fmt.Printf("*** no answer received for %s\n", os.Args[1]) + os.Exit(1) + } + + if r.Rcode != dns.RcodeSuccess { + fmt.Printf(" *** invalid answer name %s after DNSKEY query for %s\n", os.Args[1], os.Args[1]) + os.Exit(1) + } + // Stuff must be in the answer section, check len(r.Answer) + for _, k := range r.Answer { + // Foreach key would need to provide a DS records, both sha1 and sha256 + if key, ok := k.(*dns.RR_DNSKEY); ok { + ds := key.ToDS(dns.HashSHA1) + ds.Hdr.Ttl = 0 + fmt.Printf("%v\n", ds) + ds = key.ToDS(dns.HashSHA256) + ds.Hdr.Ttl = 0 + fmt.Printf("%v\n", ds) } - // Stuff must be in the answer section - for _, k := range in.Answer { - // Foreach key would need to provide a DS records, both sha1 and sha256 - if key, ok := k.(*dns.RR_DNSKEY); ok { - ds := key.ToDS(dns.HashSHA1) - ds.Hdr.Ttl = 0 - fmt.Printf("%v\n", ds) - ds = key.ToDS(dns.HashSHA256) - ds.Hdr.Ttl = 0 - fmt.Printf("%v\n", ds) - } - } - } else { - fmt.Printf("*** error: %s\n", err.String()) } } diff --git a/client_test.go b/client_test.go index b3d92764..3b2e8b93 100644 --- a/client_test.go +++ b/client_test.go @@ -24,14 +24,7 @@ func TestClientSync(t *testing.T) { /* func TestResolverEdns(t *testing.T) { - res := new(Resolver) - res.Servers = []string{"127.0.0.1"} - res.Timeout = 2 - res.Attempts = 1 - m := new(Msg) - m.MsgHdr.RecursionDesired = true //only set this bit - m.Question = make([]Question, 1) // Add EDNS rr edns := new(RR_OPT) diff --git a/clientconfig.go b/clientconfig.go index aa1938d5..fb905b76 100644 --- a/clientconfig.go +++ b/clientconfig.go @@ -38,6 +38,7 @@ func ClientConfigFromFile(conf string) (*ClientConfig, os.Error) { b := bufio.NewReader(file) c.Servers = make([]string, 3)[0:0] // small, but the standard limit c.Search = make([]string, 0) + c.Port = "53" c.Ndots = 1 c.Timeout = 5 c.Attempts = 2