git » dnss » commit 52049c3

Explicit support for unqualified requests (no tests yet)

author Alberto Bertogli
2015-09-13 19:34:56 UTC
committer Alberto Bertogli
2015-09-13 19:34:56 UTC
parent bffb1973d7167e34153336a9ab5c4dc7839a607d

Explicit support for unqualified requests (no tests yet)

dnss.go +4 -1
dnss_test.go +1 -1
dnstogrpc/dnstogrpc.go +21 -2

diff --git a/dnss.go b/dnss.go
index d3d33e3..516e588 100644
--- a/dnss.go
+++ b/dnss.go
@@ -28,6 +28,8 @@ var (
 		"address of the upstream GRPC server")
 	grpcClientCAFile = flag.String("grpc_client_cafile", "",
 		"CA file to use for the GRPC client")
+	dnsUnqualifiedUpstream = flag.String("dns_unqualified_upstream", "",
+		"DNS server to forward unqualified requests to")
 
 	enableGRPCtoDNS = flag.Bool("enable_grpc_to_dns", false,
 		"enable GRPC-to-DNS server")
@@ -78,7 +80,8 @@ func main() {
 
 	// DNS to GRPC.
 	if *enableDNStoGRPC {
-		dtg := dnstogrpc.New(*dnsListenAddr, *grpcUpstream, *grpcClientCAFile)
+		dtg := dnstogrpc.New(*dnsListenAddr, *grpcUpstream, *grpcClientCAFile,
+			*dnsUnqualifiedUpstream)
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
diff --git a/dnss_test.go b/dnss_test.go
index 6fc9d86..47448ef 100644
--- a/dnss_test.go
+++ b/dnss_test.go
@@ -230,7 +230,7 @@ func realMain(m *testing.M) int {
 	}
 
 	// DNS to GRPC server.
-	dtg := dnstogrpc.New(dnsToGrpcAddr, grpcToDnsAddr, tmpDir+"/cert.pem")
+	dtg := dnstogrpc.New(dnsToGrpcAddr, grpcToDnsAddr, tmpDir+"/cert.pem", "")
 	go dtg.ListenAndServe()
 
 	// GRPC to DNS server.
diff --git a/dnstogrpc/dnstogrpc.go b/dnstogrpc/dnstogrpc.go
index 084b069..557044b 100644
--- a/dnstogrpc/dnstogrpc.go
+++ b/dnstogrpc/dnstogrpc.go
@@ -3,6 +3,7 @@
 package dnstogrpc
 
 import (
+	"strings"
 	"sync"
 	"time"
 
@@ -65,18 +66,20 @@ func (c *grpcclient) Query(r *dns.Msg) (*dns.Msg, error) {
 }
 
 type Server struct {
-	Addr string
+	Addr        string
+	unqUpstream string
 
 	client *grpcclient
 }
 
-func New(addr, upstream, caFile string) *Server {
+func New(addr, upstream, caFile, unqUpstream string) *Server {
 	return &Server{
 		Addr: addr,
 		client: &grpcclient{
 			Upstream: upstream,
 			CAFile:   caFile,
 		},
+		unqUpstream: unqUpstream,
 	}
 }
 
@@ -90,6 +93,22 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 		tr.LazyPrintf(util.QuestionsToString(r.Question))
 	}
 
+	if s.unqUpstream != "" &&
+		len(r.Question) == 1 &&
+		strings.Count(r.Question[0].Name, ".") <= 1 {
+		u, err := dns.Exchange(r, s.unqUpstream)
+		if err == nil {
+			tr.LazyPrintf("used unqualified upstream")
+			if glog.V(3) {
+				util.TraceAnswer(tr, u)
+			}
+			w.WriteMsg(u)
+			return
+		} else {
+			tr.LazyPrintf("unqualified upstream error: %v", err)
+		}
+	}
+
 	// TODO: we should create our own IDs, in case different users pick the
 	// same id and we pass that upstream.