funkensturm compiles, but is slow for some reason

This commit is contained in:
Miek Gieben 2011-07-06 19:21:19 +02:00
parent 7033f6abbe
commit 6e7a5dd725
5 changed files with 71 additions and 39 deletions

View File

@ -27,7 +27,8 @@ func match(m *dns.Msg, d int) (*dns.Msg, bool) {
func send(m *dns.Msg, ok bool) (o *dns.Msg) {
for _, c := range qr {
o = c.Exchange(m, c.Addr)
println("Sending to ", c.Addr)
o = c.Client.Exchange(m, c.Addr)
}
return
}
@ -35,9 +36,7 @@ func send(m *dns.Msg, ok bool) (o *dns.Msg) {
// Return the configration
func funkensturm() *Funkensturm {
f := new(Funkensturm)
// Nothing to set up
f.Setup = func() bool { return true }
f.Setup = func() bool { return true } // no setup
// 1 match function, use AND as op (doesn't matter in this case)
f.Matches = make([]Match, 1)

View File

@ -8,24 +8,26 @@ package main
import (
"os"
"log"
"flag"
"fmt"
"dns"
"os/signal"
"strings"
"runtime/pprof"
)
// Define a slice of conn for sending queries
var qr []*dns.Client
var in chan dns.Query
var out chan dns.Query
// The configuration of Funkensturm
var qr []*Funk
var f *Funkensturm
// Verbose flag
var verbose *bool
// A small wrapper to keep the address together
// with a client.
type Funk struct {
Client *dns.Client
Addr string
}
// Where does the packet come from?
// IN: initial packet received by the Responder
// any modifications here will reflect what kind of
@ -71,15 +73,16 @@ type Funkensturm struct {
Actions []Action // What to do with the packets
}
func verboseprint(i *dns.Msg, msg string) {
fmt.Printf(">>>>>> %s\n", msg)
fmt.Printf("%v", i)
fmt.Printf("<<<<<< %s\n\n", msg)
func verboseprint(i *dns.Msg, indent string) {
for _, line := range strings.Split(i.String(), "\n", -1) {
fmt.Printf("%s%s\n", indent, line)
}
fmt.Println()
}
func doFunkensturm(pkt *dns.Msg) ([]byte, os.Error) {
if *verbose {
verboseprint(pkt, "ORIGINAL INCOMING")
verboseprint(pkt, "> ")
}
// No matter what, we refuse to answer requests with the response bit set.
if pkt.MsgHdr.Response == true {
@ -92,6 +95,7 @@ func doFunkensturm(pkt *dns.Msg) ([]byte, os.Error) {
ok, ok1 := true, true
pkt1 := pkt
for _, m := range f.Matches {
println("*match functions")
pkt1, ok1 = m.Func(pkt1, IN)
switch m.Op {
case AND:
@ -100,8 +104,8 @@ func doFunkensturm(pkt *dns.Msg) ([]byte, os.Error) {
ok = ok || ok1
}
}
if *verbose {
verboseprint(pkt1, "MODIFIED INCOMING")
if *verbose { //modified
verboseprint(pkt1, ">> ")
}
// Loop through the Actions.Func* and do something with the
@ -112,8 +116,8 @@ func doFunkensturm(pkt *dns.Msg) ([]byte, os.Error) {
resultpkt = a.Func(pkt1, ok)
}
if *verbose {
verboseprint(resultpkt, "ORIGINAL OUTGOING")
if *verbose { //orignal out
verboseprint(resultpkt, "< ")
}
// loop again for matching, but now with OUT, this is done
@ -130,9 +134,8 @@ func doFunkensturm(pkt *dns.Msg) ([]byte, os.Error) {
return nil, nil
}
if *verbose {
verboseprint(pkt1, "MODIFIED OUTGOING")
fmt.Printf("-----------------------------------\n\n")
if *verbose { // modified out
verboseprint(pkt1, "<< ")
}
out, ok1 := pkt1.Pack()
@ -144,6 +147,7 @@ func doFunkensturm(pkt *dns.Msg) ([]byte, os.Error) {
}
func serve(w dns.ResponseWriter, req *dns.Msg) {
println("In serve")
out, err := doFunkensturm(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.String())
@ -162,21 +166,31 @@ func listenAndServe(add, net string) {
}
func main() {
var sserver *string = flag.String("sserver", "127.0.0.1:8053", "Set the listener address")
var rserver *string = flag.String("rserver", "127.0.0.1:53", "Remote server address(es), seperate with commas")
verbose = flag.Bool("verbose", false, "Print packet as it flows through") // verbose needs to be global
sserver := flag.String("sserver", "127.0.0.1:8053", "set the listener address")
rserver := flag.String("rserver", "127.0.0.1:53", "remote server address(es), seperate with commas")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
verbose = flag.Bool("verbose", false, "Print packet as it flows through")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *cpuprofile != "" {
println("* profiling *")
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
clients := strings.Split(*rserver, ",", -1)
qr = make([]*dns.Client, len(clients))
qr = make([]*Funk, len(clients))
for i, ra := range clients {
c := dns.NewClient()
c.Addr = ra
qr[i] = c
qr[i] = new(Funk)
qr[i].Client = dns.NewClient()
qr[i].Addr = ra
}
f = funkensturm()

View File

@ -16,14 +16,14 @@ func q(w dns.RequestWriter, m *dns.Msg) {
}
func main() {
var dnssec *bool = flag.Bool("dnssec", false, "Request DNSSEC records")
var short *bool = flag.Bool("short", false, "Abbriate long DNSKEY and RRSIG RRs")
var aa *bool = flag.Bool("aa", false, "Set AA flag in query")
var ad *bool = flag.Bool("ad", false, "Set AD flag in query")
var cd *bool = flag.Bool("cd", false, "Set CD flag in query")
var rd *bool = flag.Bool("rd", true, "Unset RD flag in query")
var dnssec *bool = flag.Bool("dnssec", false, "request DNSSEC records")
var short *bool = flag.Bool("short", false, "abbriate long DNSKEY and RRSIG RRs")
var aa *bool = flag.Bool("aa", false, "set AA flag in query")
var ad *bool = flag.Bool("ad", false, "set AD flag in query")
var cd *bool = flag.Bool("cd", false, "set CD flag in query")
var rd *bool = flag.Bool("rd", true, "unset RD flag in query")
var tcp *bool = flag.Bool("tcp", false, "TCP mode")
var nsid *bool = flag.Bool("nsid", false, "Ask for the NSID")
var nsid *bool = flag.Bool("nsid", false, "ask for NSID")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [@server(:port)] [qtype] [qclass] [name ...]\n", os.Args[0])
flag.PrintDefaults()

View File

@ -113,7 +113,6 @@ func (mux *QueryMux) QueryDNS(w RequestWriter, r *Msg) {
h.QueryDNS(w, r)
}
// TODO add: LocalAddr
type Client struct {
Net string // if "tcp" a TCP query will be initiated, otherwise an UDP one
Attempts int // number of attempts
@ -123,6 +122,7 @@ type Client struct {
ReadTimeout int64 // the net.Conn.SetReadTimeout value for new connections
WriteTimeout int64 // the net.Conn.SetWriteTimeout value for new connections
TsigSecret map[string]string // secret(s) for Tsig map[<zonename>]<base64 secret>
// LocalAddr string // Local address to use
}
// Create a new client, with some default values filled in.

View File

@ -26,3 +26,22 @@ func TestServing(t *testing.T) {
}()
time.Sleep(1e9)
}
func BenchmarkServing(b *testing.B) {
b.StopTimer()
// Again start a server
HandleFunc("miek.nl.", HelloServer)
go func() {
ListenAndServe("127.0.0.1:8053", "udp", nil)
}()
c := NewClient()
m := new(Msg)
m.SetQuestion("miek.nl", TypeSOA)
b.StartTimer()
for i := 0; i < b.N; i++ {
c.Exchange(m, "127.0.0.1:8053")
print(i, " ")
}
}