From c03a914cec77271ba4a4f218f1f0bd5401b3639a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 3 Sep 2012 13:56:43 +0200 Subject: [PATCH] add version.server and version.bind --- ex/reflect/reflect.go | 2 ++ example_test.go | 2 ++ server.go | 44 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/ex/reflect/reflect.go b/ex/reflect/reflect.go index 75e3bb20..f43b8a3b 100644 --- a/ex/reflect/reflect.go +++ b/ex/reflect/reflect.go @@ -160,6 +160,8 @@ func main() { dns.HandleFunc(".", handleReflect) dns.HandleFunc("authors.bind.", dns.HandleAuthors) dns.HandleFunc("authors.server.", dns.HandleAuthors) + dns.HandleFunc("version.bind.", dns.HandleVersion) + dns.HandleFunc("version.server.", dns.HandleVersion) go serve("tcp", name, secret) go serve("udp", name, secret) sig := make(chan os.Signal) diff --git a/example_test.go b/example_test.go index a1a2e9c4..82bccaa2 100644 --- a/example_test.go +++ b/example_test.go @@ -56,6 +56,8 @@ func ExampleToDs(zone string) { // queries. func ExampleAuthors() { // ... server setup is out of scope ... + + // Register the handle funcs. HandleFunc("authors.bind.", HandleAuthors) HandleFunc("authors.server.", HandleAuthors) diff --git a/server.go b/server.go index 9c62b44d..41d21d03 100644 --- a/server.go +++ b/server.go @@ -73,6 +73,9 @@ var DefaultServeMux = NewServeMux() // Authors is a list of authors that helped create or make Go DNS better. var Authors = []string{ "Miek Gieben", "Ask Bjorn Hansen", "Dave Cheney", "Dusty Wilson", "Peter van Dijk"} +// Version holds the current version. +var Version = "Go DNS" + // The HandlerFunc type is an adapter to allow the use of // ordinary functions as DNS handlers. If f is a function // with the appropriate signature, HandlerFunc(f) is a @@ -95,7 +98,14 @@ func HandleFailed(w ResponseWriter, r *Msg) { // AuthorHandler returns a HandlerFunc that returns the authors // of Go DNS for 'authors.bind' or 'authors.server' queries in the -// CHAOS Class. +// CHAOS Class. Note with +// +// HandleFunc("authors.bind.", HandleAuthors) +// +// The handler is registered for all DNS classes, thereby potentially +// hijacking the authors.bind. zone in the IN class. If you need the +// authors.bind zone to exist in the IN class, you need to register +// some other handler, check the class in there and then call HandleAuthors. func HandleAuthors(w ResponseWriter, r *Msg) { if len(r.Question) != 1 { HandleFailed(w, r) @@ -109,7 +119,6 @@ func HandleAuthors(w ResponseWriter, r *Msg) { HandleFailed(w, r) return } - // primary author m := new(Msg) m.SetReply(r) for _, author := range Authors { @@ -119,8 +128,39 @@ func HandleAuthors(w ResponseWriter, r *Msg) { w.Write(m) } +// VersionHandler returns a HandlerFunc that returns the version +// of Go DNS for 'version.bind' or 'version.server' queries in the +// CHAOS Class. Note with +// +// HandleFunc("version.bind.", HandleVersion) +// +// The handler is registered for all DNS classes, thereby potentially +// hijacking the version.bind. zone in the IN class. If you need the +// version.bind zone to exist in the IN class, you need to register +// some other handler, check the class in there and then call HandleVersion. +func HandleVersion(w ResponseWriter, r *Msg) { + if len(r.Question) != 1 { + HandleFailed(w, r) + return + } + if r.Question[0].Qtype != ClassCHAOS && r.Question[0].Qtype != TypeTXT { + HandleFailed(w, r) + return + } + if r.Question[0].Name != "version.server." && r.Question[0].Name != "version.bind." { + HandleFailed(w, r) + return + } + m := new(Msg) + m.SetReply(r) + h := RR_Header{r.Question[0].Name, TypeTXT, ClassCHAOS, 0, 0} + m.Answer = append(m.Answer, &RR_TXT{h, []string{Version}}) + w.Write(m) +} + func authorHandler() Handler { return HandlerFunc(HandleAuthors) } func failedHandler() Handler { return HandlerFunc(HandleFailed) } +func versionHandler() Handler { return HandlerFunc(HandleVersion) } // Start a server on addresss and network speficied. Invoke handler // for any incoming queries.