From 09207867e40a85bafb3a93d659d7c428293bb6a7 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 7 Apr 2016 08:03:57 +0100 Subject: [PATCH] Add missing test file and fix notify We should not check the port of the request, we *should* actually normalize it to port 53 - as that will probably be the address of the server. Still need to double check if this will work if the axfr should actually be done from a different port. That will come later, this is good enough for now. --- middleware/file/notify.go | 2 +- middleware/file/secondary_test.go | 4 +-- middleware/testing/server.go | 53 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 middleware/testing/server.go diff --git a/middleware/file/notify.go b/middleware/file/notify.go index 1a4e43d31..b369f6ad1 100644 --- a/middleware/file/notify.go +++ b/middleware/file/notify.go @@ -19,7 +19,7 @@ func (z *Zone) isNotify(state middleware.State) bool { if len(z.TransferFrom) == 0 { return false } - remote := state.RemoteAddr() + remote := middleware.Addr(state.IP()).Normalize() for _, from := range z.TransferFrom { if from == remote { return true diff --git a/middleware/file/secondary_test.go b/middleware/file/secondary_test.go index 35866335a..111770320 100644 --- a/middleware/file/secondary_test.go +++ b/middleware/file/secondary_test.go @@ -138,11 +138,11 @@ func TestIsNotify(t *testing.T) { // need to set opcode state.Req.Opcode = dns.OpcodeNotify - z.TransferFrom = []string{"10.240.0.1:40212"} // IP from from testing/responseWriter + z.TransferFrom = []string{"10.240.0.1:53"} // IP from from testing/responseWriter if !z.isNotify(state) { t.Fatal("should have been valid notify") } - z.TransferFrom = []string{"10.240.0.2:40212"} + z.TransferFrom = []string{"10.240.0.2:53"} if z.isNotify(state) { t.Fatal("should have been invalid notify") } diff --git a/middleware/testing/server.go b/middleware/testing/server.go new file mode 100644 index 000000000..b32b8b965 --- /dev/null +++ b/middleware/testing/server.go @@ -0,0 +1,53 @@ +package testing + +import ( + "net" + "sync" + "time" + + "github.com/miekg/dns" +) + +func TCPServer(laddr string) (*dns.Server, string, error) { + l, err := net.Listen("tcp", laddr) + if err != nil { + return nil, "", err + } + + server := &dns.Server{Listener: l, ReadTimeout: time.Hour, WriteTimeout: time.Hour} + + waitLock := sync.Mutex{} + waitLock.Lock() + server.NotifyStartedFunc = waitLock.Unlock + + go func() { + server.ActivateAndServe() + l.Close() + }() + + waitLock.Lock() + return server, l.Addr().String(), nil +} + +func UDPServer(laddr string) (*dns.Server, string, chan bool, error) { + pc, err := net.ListenPacket("udp", laddr) + if err != nil { + return nil, "", nil, err + } + server := &dns.Server{PacketConn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour} + + waitLock := sync.Mutex{} + waitLock.Lock() + server.NotifyStartedFunc = waitLock.Unlock + + stop := make(chan bool) + + go func() { + server.ActivateAndServe() + close(stop) + pc.Close() + }() + + waitLock.Lock() + return server, pc.LocalAddr().String(), stop, nil +}