From c6e16cc0543c8e6315831a09f2a6a097acbc9a33 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 24 Dec 2010 12:43:53 +0100 Subject: [PATCH] Add examples Completely modules after the examples of ldns. Fist example is 'mx', print out the MX records of a domain. More are coming, each is used to fine-tune the dns api --- Changes | 12 ------------ Makefile | 13 ++++++------- ednstest.go | 36 ------------------------------------ examples/Makefile | 9 +++++++++ examples/mx.go | 42 ++++++++++++++++++++++++++++++++++++++++++ resolver.go | 3 +++ resolverEdns_test.go | 3 +-- resolver_test.go | 3 +-- restest.go | 38 -------------------------------------- 9 files changed, 62 insertions(+), 97 deletions(-) delete mode 100644 Changes delete mode 100644 ednstest.go create mode 100644 examples/Makefile create mode 100644 examples/mx.go delete mode 100644 restest.go diff --git a/Changes b/Changes deleted file mode 100644 index 872d7c54..00000000 --- a/Changes +++ /dev/null @@ -1,12 +0,0 @@ -Add resolver type and use the for querying -IPv6 support -DNSSEC support -uint8 support -base64 support (only for unpacking atm) -hex support -Split of the type definition into dnstypes.go - -make should build the package - -make restest -builds a small sample program diff --git a/Makefile b/Makefile index 0b69440e..5e6211cd 100644 --- a/Makefile +++ b/Makefile @@ -14,15 +14,14 @@ GOFILES=\ include $(GOROOT)/src/Make.pkg -p: restest manglertest ednstest dnssectest ednstest2 +.PHONY: examples + +examples: + (cd examples; make) + +progs: manglertest dnssectest # too lazy to lookup how this works again in Makefiles -restest: restest.go $(GOFILES) - 6g -I _obj restest.go && 6l -L _obj -o restest restest.6 - -ednstest: ednstest.go $(GOFILES) - 6g -I _obj ednstest.go && 6l -L _obj -o ednstest ednstest.6 - manglertest: manglertest.go $(GOFILES) 6g -I _obj manglertest.go && 6l -L _obj -o manglertest manglertest.6 diff --git a/ednstest.go b/ednstest.go deleted file mode 100644 index 4038d94a..00000000 --- a/ednstest.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -// Test EDNS RR records -import ( - "fmt" - "dns" -) - -func main() { - sig := new(dns.RR_RRSIG) - sig.Hdr.Name = "miek.nl." - sig.Hdr.Rrtype = dns.TypeRRSIG - sig.Hdr.Class = dns.ClassINET - sig.Hdr.Ttl = 3600 - sig.TypeCovered = dns.TypeDNSKEY - sig.Algorithm = dns.AlgRSASHA1 - sig.Labels = 2 - sig.OrigTtl = 4000 - sig.Expiration = 1000 - sig.Inception = 800 - sig.KeyTag = 34641 - sig.SignerName = "miek.nl." - sig.Sig = "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ" - - fmt.Printf("%v\n", sig) - - edns := new(dns.RR_OPT) - edns.Hdr.Name = "miek.nl." - edns.Hdr.Rrtype = dns.TypeOPT - edns.Hdr.Class = dns.ClassINET - edns.Hdr.Ttl = 3600 - edns.Option = make([]dns.Option, 1) - edns.Option[0].Code = dns.OptionCodeNSID - edns.Option[0].Data = "lalalala" - fmt.Printf("%v\n", edns) -} diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 00000000..ce7a425e --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,9 @@ +# Copyright 2009 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +all: mx + +# too lazy to lookup how this works again in Makefiles +mx: mx.go + 6g -I ../_obj mx.go && 6l -L ../_obj -o mx mx.6 diff --git a/examples/mx.go b/examples/mx.go new file mode 100644 index 00000000..008c3cd2 --- /dev/null +++ b/examples/mx.go @@ -0,0 +1,42 @@ +package main + +// Print the MX records of a domain +import ( + "dns" + "os" + "fmt" +) + +func main() { + r := new(dns.Resolver) + qr := dns.NewQuerier(r) + r.Servers = []string{"127.0.0.1"} + r.Timeout = 2 + r.Attempts = 1 + + if len(os.Args) != 2 { + fmt.Printf("%s DOMAIN\n", os.Args[0]) + os.Exit(1) + } + + m := new(dns.Msg) + m.MsgHdr.Recursion_desired = true //only set this bit + m.Question = make([]dns.Question, 1) + m.Question[0] = dns.Question{os.Args[1], dns.TypeMX, dns.ClassINET} + + qr <- dns.DnsMsg{m, nil} + in := <-qr + + if in.Dns.Rcode != dns.RcodeSuccess { + fmt.Printf(" *** invalid answer name %s after MX query for %s\n", os.Args[1], os.Args[1]) + os.Exit(1) + } + // Stuff must be in the answer section + for _, a := range in.Dns.Answer { + fmt.Printf("%v\n", a) + } + + // Stop the resolver, send it a null mesg + qr <- dns.DnsMsg{nil, nil} + <-qr +} diff --git a/resolver.go b/resolver.go index b3b765df..306debdd 100644 --- a/resolver.go +++ b/resolver.go @@ -6,6 +6,7 @@ // A dns resolver is to be run as a seperate goroutine. // For every reply the resolver answers by sending the // received packet back on the channel. +// TODO: resolverFromConf (/etc/resolv.conf) parsing?? package dns @@ -55,6 +56,8 @@ func query(res *Resolver, msg chan DnsMsg) { case out := <-msg: //msg received if out.Dns == nil { // nil message, quit the goroutine + msg <- DnsMsg{nil, nil} + close(msg) return } diff --git a/resolverEdns_test.go b/resolverEdns_test.go index a51e5231..57673f40 100644 --- a/resolverEdns_test.go +++ b/resolverEdns_test.go @@ -2,7 +2,6 @@ package dns import ( "testing" - "time" ) func TestResolverEdns(t *testing.T) { @@ -42,5 +41,5 @@ func TestResolverEdns(t *testing.T) { t.Fail() } ch <- DnsMsg{nil, nil} - time.Sleep(0.5e9) + <-ch } diff --git a/resolver_test.go b/resolver_test.go index b3566ab4..a75fdb9c 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -2,7 +2,6 @@ package dns import ( "testing" - "time" ) @@ -41,5 +40,5 @@ func TestResolver(t *testing.T) { } ch <- DnsMsg{nil, nil} - time.Sleep(0.5e9) + <-ch } diff --git a/restest.go b/restest.go deleted file mode 100644 index 4043a457..00000000 --- a/restest.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "dns" - "time" - "fmt" -) - -const ( - NLOOP = 5 -) - -func main() { - res := new(dns.Resolver) - ch := dns.NewQuerier(res) - - // configure the resolver - res.Servers = []string{"192.168.1.2"} - res.Timeout = 2 - res.Attempts = 1 - - // Setup done, now for some real work - // Create a new message - m := new(dns.Msg) - m.MsgHdr.Recursion_desired = true //only set this bit - m.Question = make([]dns.Question, 1) - - m.Question[0] = dns.Question{"pa1ton.nl", dns.TypeDS, dns.ClassINET} - ch <- dns.DnsMsg{m, nil} - - // wait for an reply - in := <-ch - fmt.Printf("%v\n", in.Dns) - - ch <- dns.DnsMsg{nil, nil} - - time.Sleep(2.0e9) // wait for Go routine to do something -}