cosmetic: more linting

This commit is contained in:
Matthias Radestock committed 2015-03-27 01:12:16 +00:00
1 parent e3ef5433cf
commit 1ad950f177
11 files changed
+76 -85

No files matched your search

+4 -4
View File
@@ -150,14 +150,14 @@ func (e *cacheEntry) setReply(reply *dns.Msg, flags uint8, now time.Time) bool {
e.validUntil = now.Add(time.Second * time.Duration(negLocalTTL)) e.validUntil = now.Add(time.Second * time.Duration(negLocalTTL))
} else { } else {
// calculate the validUntil from the reply TTL // calculate the validUntil from the reply TTL
var minTtl uint32 = math.MaxUint32 var minTTL uint32 = math.MaxUint32
for _, rr := range reply.Answer { for _, rr := range reply.Answer {
ttl := rr.Header().Ttl ttl := rr.Header().Ttl
if ttl < minTtl { if ttl < minTTL {
minTtl = ttl // TODO: improve the minTTL calculation (maybe we should skip some RRs) minTTL = ttl // TODO: improve the minTTL calculation (maybe we should skip some RRs)
} }
} }
e.validUntil = now.Add(time.Second * time.Duration(minTtl)) e.validUntil = now.Add(time.Second * time.Duration(minTTL))
} }
if reply != nil { if reply != nil {
+18 -18
View File
@@ -66,14 +66,14 @@ func TestCacheEntries(t *testing.T) {
question := &questionMsg.Question[0] question := &questionMsg.Question[0]
t.Logf("Trying to get a name") t.Logf("Trying to get a name")
resp, err := l.Get(questionMsg, minUdpSize, time.Now()) resp, err := l.Get(questionMsg, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
if resp != nil { if resp != nil {
t.Logf("Got '%s'", resp) t.Logf("Got '%s'", resp)
t.Fatalf("ERROR: Did not expect a reponse from Get() yet") t.Fatalf("ERROR: Did not expect a reponse from Get() yet")
} }
t.Logf("Trying to get it again") t.Logf("Trying to get it again")
resp, err = l.Get(questionMsg, minUdpSize, time.Now()) resp, err = l.Get(questionMsg, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
if resp != nil { if resp != nil {
t.Logf("Got '%s'", resp) t.Logf("Got '%s'", resp)
@@ -86,7 +86,7 @@ func TestCacheEntries(t *testing.T) {
timeGet1 := time.Now() timeGet1 := time.Now()
t.Logf("Checking we can Get() the reply now") t.Logf("Checking we can Get() the reply now")
resp, err = l.Get(questionMsg, minUdpSize, timeGet1) resp, err = l.Get(questionMsg, minUDPSize, timeGet1)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertTrue(t, resp != nil, "reponse from Get()") wt.AssertTrue(t, resp != nil, "reponse from Get()")
t.Logf("Received '%s'", resp.Answer[0]) t.Logf("Received '%s'", resp.Answer[0])
@@ -94,7 +94,7 @@ func TestCacheEntries(t *testing.T) {
ttlGet1 := resp.Answer[0].Header().Ttl ttlGet1 := resp.Answer[0].Header().Ttl
t.Logf("Checking a Wait() with timeout=0 gets the same result") t.Logf("Checking a Wait() with timeout=0 gets the same result")
resp, err = l.Wait(questionMsg, time.Duration(0)*time.Second, minUdpSize, time.Now()) resp, err = l.Wait(questionMsg, time.Duration(0)*time.Second, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertTrue(t, resp != nil, "reponse from a Wait(timeout=0)") wt.AssertTrue(t, resp != nil, "reponse from a Wait(timeout=0)")
t.Logf("Received '%s'", resp.Answer[0]) t.Logf("Received '%s'", resp.Answer[0])
@@ -102,7 +102,7 @@ func TestCacheEntries(t *testing.T) {
timeGet2 := timeGet1.Add(time.Duration(1) * time.Second) timeGet2 := timeGet1.Add(time.Duration(1) * time.Second)
t.Logf("Checking that a second Get(), after 1 second, gets the same result, but with reduced TTL") t.Logf("Checking that a second Get(), after 1 second, gets the same result, but with reduced TTL")
resp, err = l.Get(questionMsg, minUdpSize, timeGet2) resp, err = l.Get(questionMsg, minUDPSize, timeGet2)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertTrue(t, resp != nil, "reponse from a second Get()") wt.AssertTrue(t, resp != nil, "reponse from a second Get()")
t.Logf("Received '%s'", resp.Answer[0]) t.Logf("Received '%s'", resp.Answer[0])
@@ -112,7 +112,7 @@ func TestCacheEntries(t *testing.T) {
timeGet3 := timeGet1.Add(time.Duration(localTTL) * time.Second) timeGet3 := timeGet1.Add(time.Duration(localTTL) * time.Second)
t.Logf("Checking that a third Get(), after %d second, gets no result", localTTL) t.Logf("Checking that a third Get(), after %d second, gets no result", localTTL)
resp, err = l.Get(questionMsg, minUdpSize, timeGet3) resp, err = l.Get(questionMsg, minUDPSize, timeGet3)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
if resp != nil { if resp != nil {
t.Logf("Got '%s'", resp) t.Logf("Got '%s'", resp)
@@ -128,7 +128,7 @@ func TestCacheEntries(t *testing.T) {
l.Remove(question) // do it again: should have no effect... l.Remove(question) // do it again: should have no effect...
wt.AssertEqualInt(t, l.Len(), lenBefore-1, "cache length") wt.AssertEqualInt(t, l.Len(), lenBefore-1, "cache length")
resp, err = l.Get(questionMsg, minUdpSize, timeGet1) resp, err = l.Get(questionMsg, minUDPSize, timeGet1)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertTrue(t, resp == nil, "reponse from the Get() after a Remove()") wt.AssertTrue(t, resp == nil, "reponse from the Get() after a Remove()")
@@ -141,7 +141,7 @@ func TestCacheEntries(t *testing.T) {
l.Put(questionMsg, reply3, 0, timePut3) l.Put(questionMsg, reply3, 0, timePut3)
t.Logf("Checking we get the last one...") t.Logf("Checking we get the last one...")
resp, err = l.Get(questionMsg, minUdpSize, timePut3) resp, err = l.Get(questionMsg, minUDPSize, timePut3)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertTrue(t, resp != nil, "reponse from the Get()") wt.AssertTrue(t, resp != nil, "reponse from the Get()")
t.Logf("Received '%s'", resp.Answer[0]) t.Logf("Received '%s'", resp.Answer[0])
@@ -149,7 +149,7 @@ func TestCacheEntries(t *testing.T) {
wt.AssertEqualString(t, resp.Answer[0].(*dns.A).A.String(), "10.0.1.3", "IP address") wt.AssertEqualString(t, resp.Answer[0].(*dns.A).A.String(), "10.0.1.3", "IP address")
wt.AssertEqualInt(t, int(resp.Answer[0].Header().Ttl), int(localTTL), "TTL") wt.AssertEqualInt(t, int(resp.Answer[0].Header().Ttl), int(localTTL), "TTL")
resp, err = l.Get(questionMsg, minUdpSize, timePut3.Add(time.Duration(localTTL-1)*time.Second)) resp, err = l.Get(questionMsg, minUDPSize, timePut3.Add(time.Duration(localTTL-1)*time.Second))
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertTrue(t, resp != nil, "reponse from the Get()") wt.AssertTrue(t, resp != nil, "reponse from the Get()")
t.Logf("Received '%s'", resp.Answer[0]) t.Logf("Received '%s'", resp.Answer[0])
@@ -159,7 +159,7 @@ func TestCacheEntries(t *testing.T) {
t.Logf("Checking we get empty replies when they are expired...") t.Logf("Checking we get empty replies when they are expired...")
lenBefore = l.Len() lenBefore = l.Len()
resp, err = l.Get(questionMsg, minUdpSize, timePut3.Add(time.Duration(localTTL)*time.Second)) resp, err = l.Get(questionMsg, minUDPSize, timePut3.Add(time.Duration(localTTL)*time.Second))
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
if resp != nil { if resp != nil {
t.Logf("Received '%s'", resp.Answer[0]) t.Logf("Received '%s'", resp.Answer[0])
@@ -173,7 +173,7 @@ func TestCacheEntries(t *testing.T) {
question2 := &questionMsg2.Question[0] question2 := &questionMsg2.Question[0]
t.Logf("Trying to Get() a name") t.Logf("Trying to Get() a name")
resp, err = l.Get(questionMsg2, minUdpSize, time.Now()) resp, err = l.Get(questionMsg2, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertNil(t, resp, "reponse from Get() yet") wt.AssertNil(t, resp, "reponse from Get() yet")
@@ -181,10 +181,10 @@ func TestCacheEntries(t *testing.T) {
replyTemp2 := makeAddressReply(questionMsg2, question2, []net.IP{net.ParseIP("10.0.9.9")}) replyTemp2 := makeAddressReply(questionMsg2, question2, []net.IP{net.ParseIP("10.0.9.9")})
l.Remove(question2) l.Remove(question2)
l.Put(questionMsg2, replyTemp2, 0, time.Now()) l.Put(questionMsg2, replyTemp2, 0, time.Now())
resp, err = l.Get(questionMsg2, minUdpSize, time.Now()) resp, err = l.Get(questionMsg2, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertNotNil(t, resp, "reponse from Get()") wt.AssertNotNil(t, resp, "reponse from Get()")
resp, err = l.Wait(questionMsg2, time.Duration(0)*time.Second, minUdpSize, time.Now()) resp, err = l.Wait(questionMsg2, time.Duration(0)*time.Second, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertNotNil(t, resp, "reponse from Get()") wt.AssertNotNil(t, resp, "reponse from Get()")
@@ -196,13 +196,13 @@ func TestCacheEntries(t *testing.T) {
t.Logf("Checking that a entry with CacheNoLocalReplies return an error") t.Logf("Checking that a entry with CacheNoLocalReplies return an error")
timePut3 = time.Now() timePut3 = time.Now()
l.Put(questionMsg3, nil, CacheNoLocalReplies, timePut3) l.Put(questionMsg3, nil, CacheNoLocalReplies, timePut3)
resp, err = l.Get(questionMsg3, minUdpSize, timePut3) resp, err = l.Get(questionMsg3, minUDPSize, timePut3)
wt.AssertNil(t, resp, "Get() response with CacheNoLocalReplies") wt.AssertNil(t, resp, "Get() response with CacheNoLocalReplies")
wt.AssertNotNil(t, err, "Get() error with CacheNoLocalReplies") wt.AssertNotNil(t, err, "Get() error with CacheNoLocalReplies")
timeExpiredGet3 := timePut3.Add(time.Second * time.Duration(negLocalTTL+1)) timeExpiredGet3 := timePut3.Add(time.Second * time.Duration(negLocalTTL+1))
t.Logf("Checking that we get an expired response after %f seconds", timeExpiredGet3.Sub(timePut3).Seconds()) t.Logf("Checking that we get an expired response after %f seconds", timeExpiredGet3.Sub(timePut3).Seconds())
resp, err = l.Get(questionMsg3, minUdpSize, timeExpiredGet3) resp, err = l.Get(questionMsg3, minUDPSize, timeExpiredGet3)
wt.AssertNil(t, resp, "expired Get() response with CacheNoLocalReplies") wt.AssertNil(t, resp, "expired Get() response with CacheNoLocalReplies")
wt.AssertNil(t, err, "expired Get() error with CacheNoLocalReplies") wt.AssertNil(t, err, "expired Get() error with CacheNoLocalReplies")
@@ -210,7 +210,7 @@ func TestCacheEntries(t *testing.T) {
t.Logf("Checking that Put&Get with CacheNoLocalReplies with a Remove in the middle returns nothing") t.Logf("Checking that Put&Get with CacheNoLocalReplies with a Remove in the middle returns nothing")
l.Put(questionMsg3, nil, CacheNoLocalReplies, time.Now()) l.Put(questionMsg3, nil, CacheNoLocalReplies, time.Now())
l.Remove(question3) l.Remove(question3)
resp, err = l.Get(questionMsg3, minUdpSize, time.Now()) resp, err = l.Get(questionMsg3, minUDPSize, time.Now())
wt.AssertNil(t, resp, "Get() reponse with CacheNoLocalReplies") wt.AssertNil(t, resp, "Get() reponse with CacheNoLocalReplies")
wt.AssertNil(t, err, "Get() error with CacheNoLocalReplies") wt.AssertNil(t, err, "Get() error with CacheNoLocalReplies")
} }
@@ -237,10 +237,10 @@ func TestCacheBlockingOps(t *testing.T) {
go func(request *dns.Msg) { go func(request *dns.Msg) {
t.Logf("Querying about %s...", request.Question[0].Name) t.Logf("Querying about %s...", request.Question[0].Name)
_, err := l.Get(request, minUdpSize, time.Now()) _, err := l.Get(request, minUDPSize, time.Now())
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
t.Logf("Waiting for %s...", request.Question[0].Name) t.Logf("Waiting for %s...", request.Question[0].Name)
r, err := l.Wait(request, 1*time.Second, minUdpSize, time.Now()) r, err := l.Wait(request, 1*time.Second, minUDPSize, time.Now())
t.Logf("Obtained response for %s:\n%s", request.Question[0].Name, r) t.Logf("Obtained response for %s:\n%s", request.Question[0].Name, r)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
}(questionMsg) }(questionMsg)
+5 -5
View File
@@ -8,8 +8,8 @@ import (
const ( const (
localTTL uint32 = 30 // somewhat arbitrary; we don't expect anyone downstream to cache results localTTL uint32 = 30 // somewhat arbitrary; we don't expect anyone downstream to cache results
negLocalTTL = 30 // TTL for negative local resolutions negLocalTTL = 30 // TTL for negative local resolutions
minUdpSize = 512 minUDPSize = 512
maxUdpSize = 65535 maxUDPSize = 65535
) )
func makeHeader(r *dns.Msg, q *dns.Question) *dns.RR_Header { func makeHeader(r *dns.Msg, q *dns.Question) *dns.RR_Header {
@@ -82,9 +82,9 @@ func makeDNSNotImplResponse(r *dns.Msg) *dns.Msg {
// get the maximum UDP-reply length // get the maximum UDP-reply length
func getMaxReplyLen(r *dns.Msg, proto dnsProtocol) int { func getMaxReplyLen(r *dns.Msg, proto dnsProtocol) int {
maxLen := minUdpSize maxLen := minUDPSize
if proto == protTcp { if proto == protTCP {
maxLen = maxUdpSize maxLen = maxUDPSize
} else if opt := r.IsEdns0(); opt != nil { } else if opt := r.IsEdns0(); opt != nil {
maxLen = int(opt.UDPSize()) maxLen = int(opt.UDPSize())
} }
+1 -1
View File
@@ -17,7 +17,7 @@ func httpErrorAndLog(level *log.Logger, w http.ResponseWriter, msg string,
level.Printf("[http] "+logmsg, logargs...) level.Printf("[http] "+logmsg, logargs...)
} }
func ListenHttp(version string, server *DNSServer, domain string, db Zone, port int) { func ListenHTTP(version string, server *DNSServer, domain string, db Zone, port int) {
muxRouter := mux.NewRouter() muxRouter := mux.NewRouter()
+8 -8
View File
@@ -30,14 +30,14 @@ func TestHttp(t *testing.T) {
var zone = new(ZoneDb) var zone = new(ZoneDb)
port := rand.Intn(10000) + 32768 port := rand.Intn(10000) + 32768
fmt.Println("Http test on port", port) fmt.Println("Http test on port", port)
go ListenHttp("", nil, testDomain, zone, port) go ListenHTTP("", nil, testDomain, zone, port)
time.Sleep(100 * time.Millisecond) // Allow for http server to get going time.Sleep(100 * time.Millisecond) // Allow for http server to get going
// Ask the http server to add our test address into the database // Ask the http server to add our test address into the database
addrParts := strings.Split(testAddr1, "/") addrParts := strings.Split(testAddr1, "/")
addrUrl := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, containerID, addrParts[0]) addrURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, containerID, addrParts[0])
resp, err := genForm("PUT", addrUrl, resp, err := genForm("PUT", addrURL,
url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}}) url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response") wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")
@@ -51,21 +51,21 @@ func TestHttp(t *testing.T) {
} }
// Adding exactly the same address should be OK // Adding exactly the same address should be OK
resp, err = genForm("PUT", addrUrl, resp, err = genForm("PUT", addrURL,
url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}}) url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http success response for duplicate add") wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http success response for duplicate add")
// Now try adding the same address again with a different ident -- // Now try adding the same address again with a different ident --
// again should be fine // again should be fine
otherUrl := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, "other", addrParts[0]) otherURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, "other", addrParts[0])
resp, err = genForm("PUT", otherUrl, resp, err = genForm("PUT", otherURL,
url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}}) url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response") wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")
// Delete the address // Delete the address
resp, err = genForm("DELETE", addrUrl, nil) resp, err = genForm("DELETE", addrURL, nil)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response") wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")
@@ -74,7 +74,7 @@ func TestHttp(t *testing.T) {
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
// Delete the address record mentioning the other container // Delete the address record mentioning the other container
resp, err = genForm("DELETE", otherUrl, nil) resp, err = genForm("DELETE", otherURL, nil)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response") wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")
+3 -4
View File
@@ -14,11 +14,10 @@ func mdnsLookup(client *MDNSClient, name string, qtype uint16) (*Response, error
Debug.Printf("[mdns] Error for query type %s name %s: %s", Debug.Printf("[mdns] Error for query type %s name %s: %s",
dns.TypeToString[qtype], name, err) dns.TypeToString[qtype], name, err)
return nil, err return nil, err
} else {
Debug.Printf("[mdns] Got response name for query type %s name %s: name %s, addr %s",
dns.TypeToString[qtype], name, resp.Name, resp.Addr)
return resp, nil
} }
Debug.Printf("[mdns] Got response name for query type %s name %s: name %s, addr %s",
dns.TypeToString[qtype], name, resp.Name, resp.Addr)
return resp, nil
} }
return nil, LookupError(name) return nil, LookupError(name)
} }
+2 -4
View File
@@ -55,18 +55,16 @@ func (s *MDNSServer) Start(ifi *net.Interface, localDomain string) error {
func(zone Lookup, r *dns.Msg, q *dns.Question) *dns.Msg { func(zone Lookup, r *dns.Msg, q *dns.Question) *dns.Msg {
if ip, err := zone.LookupName(q.Name); err == nil { if ip, err := zone.LookupName(q.Name); err == nil {
return makeAddressReply(r, q, []net.IP{ip}) return makeAddressReply(r, q, []net.IP{ip})
} else {
return nil
} }
return nil
}) })
handleReverse := s.makeHandler(dns.TypePTR, handleReverse := s.makeHandler(dns.TypePTR,
func(zone Lookup, r *dns.Msg, q *dns.Question) *dns.Msg { func(zone Lookup, r *dns.Msg, q *dns.Question) *dns.Msg {
if name, err := zone.LookupInaddr(q.Name); err == nil { if name, err := zone.LookupInaddr(q.Name); err == nil {
return makePTRReply(r, q, []string{name}) return makePTRReply(r, q, []string{name})
} else {
return nil
} }
return nil
}) })
mux := dns.NewServeMux() mux := dns.NewServeMux()
+11 -11
View File
@@ -36,21 +36,21 @@ type DNSServerConfig struct {
// (Optional) timeout for DNS queries // (Optional) timeout for DNS queries
Timeout int Timeout int
// (Optional) UDP buffer length // (Optional) UDP buffer length
UdpBufLen int UDPBufLen int
} }
type dnsProtocol uint8 type dnsProtocol uint8
const ( const (
protUdp dnsProtocol = iota // UDP protocol protUDP dnsProtocol = iota // UDP protocol
protTcp dnsProtocol = iota // TCP protocol protTCP dnsProtocol = iota // TCP protocol
) )
func (proto dnsProtocol) String() string { func (proto dnsProtocol) String() string {
switch proto { switch proto {
case protUdp: case protUDP:
return "UDP" return "UDP"
case protTcp: case protTCP:
return "TCP" return "TCP"
} }
return "unknown" return "unknown"
@@ -59,9 +59,9 @@ func (proto dnsProtocol) String() string {
// get a new dns.Client for a protocol // get a new dns.Client for a protocol
func (proto dnsProtocol) GetNewClient(bufsize int) *dns.Client { func (proto dnsProtocol) GetNewClient(bufsize int) *dns.Client {
switch proto { switch proto {
case protTcp: case protTCP:
return &dns.Client{Net: "tcp"} return &dns.Client{Net: "tcp"}
case protUdp: case protUDP:
return &dns.Client{Net: "udp", UDPSize: uint16(bufsize)} return &dns.Client{Net: "udp", UDPSize: uint16(bufsize)}
} }
return nil return nil
@@ -118,8 +118,8 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
if config.Timeout > 0 { if config.Timeout > 0 {
s.timeout = config.Timeout s.timeout = config.Timeout
} }
if config.UdpBufLen > 0 { if config.UDPBufLen > 0 {
s.udpBuf = config.UdpBufLen s.udpBuf = config.UDPBufLen
} }
s.mdnsCli, err = NewMDNSClient() s.mdnsCli, err = NewMDNSClient()
if err != nil { if err != nil {
@@ -152,8 +152,8 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
return m return m
} }
s.udpSrv = &dns.Server{Addr: s.ListenAddr, Net: "udp", Handler: mux(protUdp)} s.udpSrv = &dns.Server{Addr: s.ListenAddr, Net: "udp", Handler: mux(protUDP)}
s.tcpSrv = &dns.Server{Addr: s.ListenAddr, Net: "tcp", Handler: mux(protTcp)} s.tcpSrv = &dns.Server{Addr: s.ListenAddr, Net: "tcp", Handler: mux(protTCP)}
return return
} }
+14 -14
View File
@@ -15,7 +15,7 @@ const (
testRDNSsuccess = "1.2.2.10.in-addr.arpa." testRDNSsuccess = "1.2.2.10.in-addr.arpa."
testRDNSfail = "4.3.2.1.in-addr.arpa." testRDNSfail = "4.3.2.1.in-addr.arpa."
testRDNSnonlocal = "8.8.8.8.in-addr.arpa." testRDNSnonlocal = "8.8.8.8.in-addr.arpa."
testUdpBufSize = 16384 testUDPBufSize = 16384
) )
var ( var (
@@ -130,7 +130,7 @@ func TestTCPDNSServer(t *testing.T) {
// handler for the fallback server: it will just return a very long response // handler for the fallback server: it will just return a very long response
fallbackUDPHandler := func(w dns.ResponseWriter, req *dns.Msg) { fallbackUDPHandler := func(w dns.ResponseWriter, req *dns.Msg) {
maxLen := getMaxReplyLen(req, protUdp) maxLen := getMaxReplyLen(req, protUDP)
t.Logf("Fallback UDP server got asked: returning %d answers", numAnswers) t.Logf("Fallback UDP server got asked: returning %d answers", numAnswers)
q := req.Question[0] q := req.Question[0]
@@ -152,16 +152,16 @@ func TestTCPDNSServer(t *testing.T) {
} }
t.Logf("Running a DNS fallback server with UDP") t.Logf("Running a DNS fallback server with UDP")
us, fallbackUdpAddr, err := runLocalUDPServer(t, "127.0.0.1:0", fallbackUDPHandler) us, fallbackUDPAddr, err := runLocalUDPServer(t, "127.0.0.1:0", fallbackUDPHandler)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
defer us.Shutdown() defer us.Shutdown()
_, fallbackPort, err := net.SplitHostPort(fallbackUdpAddr) _, fallbackPort, err := net.SplitHostPort(fallbackUDPAddr)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
t.Logf("Starting another fallback server, with TCP, on the same port as the UDP server") t.Logf("Starting another fallback server, with TCP, on the same port as the UDP server")
fallbackTcpAddr := fmt.Sprintf("127.0.0.1:%s", fallbackPort) fallbackTCPAddr := fmt.Sprintf("127.0.0.1:%s", fallbackPort)
ts, fallbackTcpAddr, err := runLocalTCPServer(t, fallbackTcpAddr, fallbackTCPHandler) ts, fallbackTCPAddr, err := runLocalTCPServer(t, fallbackTCPAddr, fallbackTCPHandler)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
defer ts.Shutdown() defer ts.Shutdown()
@@ -175,7 +175,7 @@ func TestTCPDNSServer(t *testing.T) {
t.Logf("Creating a UDP and a TCP client") t.Logf("Creating a UDP and a TCP client")
uc := new(dns.Client) uc := new(dns.Client)
uc.UDPSize = minUdpSize uc.UDPSize = minUDPSize
tc := new(dns.Client) tc := new(dns.Client)
tc.Net = "tcp" tc.Net = "tcp"
@@ -184,8 +184,8 @@ func TestTCPDNSServer(t *testing.T) {
m.RecursionDesired = true m.RecursionDesired = true
m.SetQuestion(nonLocalName, dns.TypeA) m.SetQuestion(nonLocalName, dns.TypeA)
t.Logf("Checking the fallback server at %s returns a truncated response with UDP", fallbackUdpAddr) t.Logf("Checking the fallback server at %s returns a truncated response with UDP", fallbackUDPAddr)
r, _, err := uc.Exchange(m, fallbackUdpAddr) r, _, err := uc.Exchange(m, fallbackUDPAddr)
t.Logf("Got response from fallback server (UDP) with %d answers", len(r.Answer)) t.Logf("Got response from fallback server (UDP) with %d answers", len(r.Answer))
t.Logf("Response:\n%+v\n", r) t.Logf("Response:\n%+v\n", r)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
@@ -211,7 +211,7 @@ func TestTCPDNSServer(t *testing.T) {
wt.AssertEqualInt(t, len(r.Answer), numAnswers, "number of answers (TCP)") wt.AssertEqualInt(t, len(r.Answer), numAnswers, "number of answers (TCP)")
t.Logf("Checking the WeaveDNS server at %s does not return a truncated reponse with UDP with a bigger buffer", dnsAddr) t.Logf("Checking the WeaveDNS server at %s does not return a truncated reponse with UDP with a bigger buffer", dnsAddr)
m.SetEdns0(testUdpBufSize, false) m.SetEdns0(testUDPBufSize, false)
r, _, err = uc.Exchange(m, dnsAddr) r, _, err = uc.Exchange(m, dnsAddr)
t.Logf("UDP-large Response:\n%+v\n", r) t.Logf("UDP-large Response:\n%+v\n", r)
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
@@ -227,11 +227,11 @@ func TestTCPDNSServer(t *testing.T) {
// perform a DNS query and assert the reply code, number or answers, etc // perform a DNS query and assert the reply code, number or answers, etc
func assertExchange(t *testing.T, z string, ty uint16, minAnswers int, maxAnswers int, expErr int) *dns.Msg { func assertExchange(t *testing.T, z string, ty uint16, minAnswers int, maxAnswers int, expErr int) *dns.Msg {
c := new(dns.Client) c := new(dns.Client)
c.UDPSize = testUdpBufSize c.UDPSize = testUDPBufSize
m := new(dns.Msg) m := new(dns.Msg)
m.RecursionDesired = true m.RecursionDesired = true
m.SetQuestion(z, ty) m.SetQuestion(z, ty)
m.SetEdns0(testUdpBufSize, false) // we don't want to play with truncation here... m.SetEdns0(testUDPBufSize, false) // we don't want to play with truncation here...
r, _, err := c.Exchange(m, fmt.Sprintf("127.0.0.1:%d", testPort)) r, _, err := c.Exchange(m, fmt.Sprintf("127.0.0.1:%d", testPort))
t.Logf("Response:\n%+v\n", r) t.Logf("Response:\n%+v\n", r)
@@ -272,12 +272,12 @@ func runLocalUDPServer(t *testing.T, laddr string, handler dns.HandlerFunc) (*dn
// run a TCP fallback server // run a TCP fallback server
func runLocalTCPServer(t *testing.T, laddr string, handler dns.HandlerFunc) (*dns.Server, string, error) { func runLocalTCPServer(t *testing.T, laddr string, handler dns.HandlerFunc) (*dns.Server, string, error) {
t.Logf("Starting fallback TCP server at %s", laddr) t.Logf("Starting fallback TCP server at %s", laddr)
laddrTcp, err := net.ResolveTCPAddr("tcp", laddr) laddrTCP, err := net.ResolveTCPAddr("tcp", laddr)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
l, err := net.ListenTCP("tcp", laddrTcp) l, err := net.ListenTCP("tcp", laddrTCP)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
+8 -14
View File
@@ -97,20 +97,17 @@ func (zone *ZoneDb) LookupInaddr(inaddr string) (string, error) {
} }
} }
return "", LookupError(inaddr) return "", LookupError(inaddr)
} else {
Warning.Printf("[zonedb] Asked to reverse lookup %s", inaddr)
return "", LookupError(inaddr)
} }
Warning.Printf("[zonedb] Asked to reverse lookup %s", inaddr)
return "", LookupError(inaddr)
} }
func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) error { func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) error {
zone.mx.Lock() zone.mx.Lock()
defer zone.mx.Unlock() defer zone.mx.Unlock()
fqdn := dns.Fqdn(name) fqdn := dns.Fqdn(name)
if index := zone.indexOf( pred := func(r Record) bool { return r.Name == fqdn && r.IP.Equal(ip) && r.Ident == ident }
func(r Record) bool { if index := zone.indexOf(pred); index != -1 {
return r.Name == fqdn && r.IP.Equal(ip) && r.Ident == ident
}); index != -1 {
return DuplicateError{} return DuplicateError{}
} }
zone.recs = append(zone.recs, Record{ident, fqdn, ip}) zone.recs = append(zone.recs, Record{ident, fqdn, ip})
@@ -120,15 +117,12 @@ func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) error {
func (zone *ZoneDb) DeleteRecord(ident string, ip net.IP) error { func (zone *ZoneDb) DeleteRecord(ident string, ip net.IP) error {
zone.mx.Lock() zone.mx.Lock()
defer zone.mx.Unlock() defer zone.mx.Unlock()
if index := zone.indexOf( pred := func(r Record) bool { return r.Ident == ident && r.IP.Equal(ip) }
func(r Record) bool { if index := zone.indexOf(pred); index != -1 {
return r.Ident == ident && r.IP.Equal(ip)
}); index == -1 {
return LookupError(ident)
} else {
zone.recs = append(zone.recs[:index], zone.recs[index+1:]...) zone.recs = append(zone.recs[:index], zone.recs[index+1:]...)
return nil
} }
return nil return LookupError(ident)
} }
func (zone *ZoneDb) DeleteRecordsFor(ident string) error { func (zone *ZoneDb) DeleteRecordsFor(ident string) error {
+2 -2
View File
@@ -83,7 +83,7 @@ func main() {
CacheLen: cacheLen, CacheLen: cacheLen,
LocalDomain: localDomain, LocalDomain: localDomain,
Timeout: timeout, Timeout: timeout,
UdpBufLen: udpbuf, UDPBufLen: udpbuf,
} }
if len(fallback) > 0 { if len(fallback) > 0 {
@@ -103,7 +103,7 @@ func main() {
Debug.Printf("Starting the signals handler") Debug.Printf("Starting the signals handler")
go handleSignals(srv) go handleSignals(srv)
go weavedns.ListenHttp(version, srv, localDomain, zone, httpPort) go weavedns.ListenHTTP(version, srv, localDomain, zone, httpPort)
err = srv.Start() err = srv.Start()
if err != nil { if err != nil {
Error.Fatal("Failed to start the WeaveDNS server", err) Error.Fatal("Failed to start the WeaveDNS server", err)