diff --git a/ex/as212/as212.go b/ex/as212/as212.go index 39fabf60..9fd1a00e 100644 --- a/ex/as212/as212.go +++ b/ex/as212/as212.go @@ -12,6 +12,7 @@ import ( "log" "os" "os/signal" + "runtime" "syscall" ) @@ -45,6 +46,17 @@ func NewRR(s string) dns.RR { } func main() { + runtime.GOMAXPROCS(runtime.NumCPU()*2 + 1) + for z, rr := range zones { + rrx := rr.(*dns.SOA) // Some foo needed to created actual RR, on the not a reference + dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) { + m := new(dns.Msg) + m.SetReply(r) + m.Authoritative = true + m.Ns = []dns.RR{rrx} + w.WriteMsg(m) + }) + } go func() { err := dns.ListenAndServe(":8053", "tcp", nil) if err != nil { @@ -57,18 +69,8 @@ func main() { log.Fatal("Failed to set tcp listener %s\n", err.Error()) } }() - for z, rr := range zones { - dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) { - m := new(dns.Msg) - m.SetReply(r) - m.Authoritative = true - m.Ns = []dns.RR{rr} - w.WriteMsg(m) - }) - } sig := make(chan os.Signal) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) - for { select { case s := <-sig: diff --git a/ex/reflect/reflect.go b/ex/reflect/reflect.go index 437b070c..798ec830 100644 --- a/ex/reflect/reflect.go +++ b/ex/reflect/reflect.go @@ -3,12 +3,12 @@ // license that can be found in the LICENSE file. // Reflect is a small name server which sends back the IP address of its client, the -// recursive resolver. +// recursive resolver. // When queried for type A (resp. AAAA), it sends back the IPv4 (resp. v6) address. // In the additional section the port number and transport are shown. -// +// // Basic use pattern: -// +// // dig @localhost -p 8053 whoami.miek.nl A // // ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2157 @@ -25,9 +25,9 @@ // Similar services: whoami.ultradns.net, whoami.akamai.net. Also (but it // is not their normal goal): rs.dns-oarc.net, porttest.dns-oarc.net, // amiopen.openresolvers.org. -// +// // Original version is from: Stephane Bortzmeyer . -// +// // Adapted to Go (i.e. completely rewritten) by Miek Gieben . package main @@ -39,6 +39,7 @@ import ( "net" "os" "os/signal" + "runtime" "runtime/pprof" "strconv" "strings" @@ -77,14 +78,14 @@ func handleReflect(w dns.ResponseWriter, r *dns.Msg) { } /* - if o := r.IsEdns0(); o != nil { - for _, s := range o.Option { - switch e := s.(type) { - case *dns.EDNS0_SUBNET: - log.Printf("Edns0 subnet %s", e.Address) + if o := r.IsEdns0(); o != nil { + for _, s := range o.Option { + switch e := s.(type) { + case *dns.EDNS0_SUBNET: + log.Printf("Edns0 subnet %s", e.Address) + } } } - } */ if v4 { @@ -160,6 +161,7 @@ func serve(net, name, secret string) { } func main() { + runtime.GOMAXPROCS(runtime.NumCPU()*2 + 1) cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file") printf = flag.Bool("print", false, "print replies") compress = flag.Bool("compress", false, "compress replies") diff --git a/server.go b/server.go index 6cb9e7dc..27096e9c 100644 --- a/server.go +++ b/server.go @@ -167,31 +167,22 @@ func ListenAndServe(addr string, network string, handler Handler) error { func (mux *ServeMux) match(q string, t uint16) Handler { mux.m.RLock() defer mux.m.RUnlock() - var ( - handler Handler - lastdot int = -1 - lastbyte byte - seendot bool = true - ) - for i := 0; i < len(q); i++ { - if seendot { - if h, ok := mux.z[q[lastdot+1:]]; ok { - if t != TypeDS { - return h - } else { - // Continue for DS to see if we have a parent too, if so delegeate to the parent - handler = h - } + var handler Handler + off := 0 + end := false + for { + if h, ok := mux.z[q[off:]]; ok { + if t != TypeDS { + return h + } else { + // Continue for DS to see if we have a parent too, if so delegeate to the parent + handler = h } } - - if q[i] == '.' && lastbyte != '\\' { - lastdot = i - seendot = true - } else { - seendot = false + off, end = NextLabel(q, off) + if end { + break } - lastbyte = q[i] } // Wildcard match, if we have found nothing try the root zone as a last resort. if h, ok := mux.z["."]; ok {