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
This commit is contained in:
Miek Gieben 2010-12-24 12:43:53 +01:00
parent fd9afcb44d
commit c6e16cc054
9 changed files with 62 additions and 97 deletions

12
Changes
View File

@ -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

View File

@ -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

View File

@ -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)
}

9
examples/Makefile Normal file
View File

@ -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

42
examples/mx.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}