From 7de2080b9e1e7e24eb252fe375cef34ed9a5bbc3 Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Tue, 28 Apr 2015 11:42:31 +0200 Subject: [PATCH] Added a TTL to the zone records --- nameserver/addrs.go | 6 ++++++ nameserver/cache_test.go | 12 ++++++------ nameserver/mdns_client.go | 7 +++++-- nameserver/mdns_client_test.go | 2 +- nameserver/mdns_server_test.go | 2 +- nameserver/server_test.go | 2 +- nameserver/zone.go | 4 ++-- 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/nameserver/addrs.go b/nameserver/addrs.go index 22d586ab..b3ddfd2b 100644 --- a/nameserver/addrs.go +++ b/nameserver/addrs.go @@ -15,6 +15,7 @@ type ZoneRecord interface { IP() net.IP // The IP (v4) address Priority() int // The priority Weight() int // The weight + TTL() int // TTL } type ZoneLookup interface { @@ -41,12 +42,14 @@ type Record struct { ip net.IP priority int weight int + ttl int } func (r Record) Name() string { return r.name } func (r Record) IP() net.IP { return r.ip } func (r Record) Priority() int { return r.priority } func (r Record) Weight() int { return r.weight } +func (r Record) TTL() int { return r.ttl } func (i Record) String() string { var buf bytes.Buffer @@ -62,6 +65,9 @@ func (i Record) String() string { if i.Weight() > 0 { fmt.Fprintf(&buf, "/W:%d", i.Weight()) } + if i.TTL() > 0 { + fmt.Fprintf(&buf, "/TTL:%d", i.TTL()) + } return buf.String() } diff --git a/nameserver/cache_test.go b/nameserver/cache_test.go index 169881c3..3f785fb8 100644 --- a/nameserver/cache_test.go +++ b/nameserver/cache_test.go @@ -31,7 +31,7 @@ func TestCacheLength(t *testing.T) { question := &questionMsg.Question[0] ip := net.ParseIP(fmt.Sprintf("10.0.1.%d", i)) - records := []ZoneRecord{Record{name, ip, 0, 0}} + records := []ZoneRecord{Record{name, ip, 0, 0, 0}} reply := makeAddressReply(questionMsg, question, records) reply.Answer[0].Header().Ttl = uint32(i) @@ -83,7 +83,7 @@ func TestCacheEntries(t *testing.T) { } t.Logf("Inserting the reply") - records := []ZoneRecord{Record{"some.name", net.ParseIP("10.0.1.1"), 0, 0}} + records := []ZoneRecord{Record{"some.name", net.ParseIP("10.0.1.1"), 0, 0, 0}} reply1 := makeAddressReply(questionMsg, question, records) l.Put(questionMsg, reply1, nullTTL, 0, time.Now()) @@ -116,7 +116,7 @@ func TestCacheEntries(t *testing.T) { } t.Logf("Checking that an Remove() results in Get() returning nothing") - records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.9.9"), 0, 0}} + records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.9.9"), 0, 0, 0}} replyTemp := makeAddressReply(questionMsg, question, records) l.Put(questionMsg, replyTemp, nullTTL, 0, time.Now()) lenBefore := l.Len() @@ -131,11 +131,11 @@ func TestCacheEntries(t *testing.T) { t.Logf("Inserting a two replies for the same query") timePut2 := time.Now() - records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.1.2"), 0, 0}} + records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.1.2"), 0, 0, 0}} reply2 := makeAddressReply(questionMsg, question, records) l.Put(questionMsg, reply2, nullTTL, 0, timePut2) timePut3 := timePut2.Add(time.Duration(1) * time.Second) - records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.1.3"), 0, 0}} + records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.1.3"), 0, 0, 0}} reply3 := makeAddressReply(questionMsg, question, records) l.Put(questionMsg, reply3, nullTTL, 0, timePut3) @@ -177,7 +177,7 @@ func TestCacheEntries(t *testing.T) { wt.AssertNil(t, resp, "reponse from Get() yet") t.Logf("Checking that an Remove() between Get() and Put() does not break things") - records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.9.9"), 0, 0}} + records = []ZoneRecord{Record{"some.name", net.ParseIP("10.0.9.9"), 0, 0, 0}} replyTemp2 := makeAddressReply(questionMsg2, question2, records) l.Remove(question2) l.Put(questionMsg2, replyTemp2, nullTTL, 0, time.Now()) diff --git a/nameserver/mdns_client.go b/nameserver/mdns_client.go index cffb2769..878ef98c 100644 --- a/nameserver/mdns_client.go +++ b/nameserver/mdns_client.go @@ -29,6 +29,7 @@ var ( type Response struct { name string addr net.IP + ttl int err error } @@ -36,6 +37,7 @@ func (r Response) Name() string { return r.name } func (r Response) IP() net.IP { return r.addr } func (r Response) Priority() int { return 0 } func (r Response) Weight() int { return 0 } +func (r Response) TTL() int { return r.ttl } func (r Response) Equal(r2 *Response) bool { if r.name != r2.name { @@ -182,10 +184,11 @@ func (c *MDNSClient) ResponseCallback(r *dns.Msg) { switch rr := answer.(type) { case *dns.A: name = rr.Hdr.Name - res = &Response{addr: rr.A} + res = &Response{name: name, addr: rr.A, ttl: int(rr.Hdr.Ttl)} case *dns.PTR: name = rr.Hdr.Name - res = &Response{name: rr.Ptr} + raddr, _ := raddrToIP(name) + res = &Response{name: rr.Ptr, addr: raddr, ttl: int(rr.Hdr.Ttl)} default: return } diff --git a/nameserver/mdns_client_test.go b/nameserver/mdns_client_test.go index 48587a68..bc7a8621 100644 --- a/nameserver/mdns_client_test.go +++ b/nameserver/mdns_client_test.go @@ -109,7 +109,7 @@ func (c *testContext) checkResponse(t *testing.T, channelOk bool, resp *Response return } wt.AssertNoErr(t, resp.err) - log.Printf("Got address response %s addr %s", resp.Name, resp.IP()) + log.Printf("Got address response %s addr %s", resp.Name(), resp.IP()) c.receivedAddr = resp.IP() c.receivedCount++ } diff --git a/nameserver/mdns_server_test.go b/nameserver/mdns_server_test.go index a29be20f..ea7e0c92 100644 --- a/nameserver/mdns_server_test.go +++ b/nameserver/mdns_server_test.go @@ -13,7 +13,7 @@ func TestServerSimpleQuery(t *testing.T) { InitDefaultLogging(testing.Verbose()) var ( - testRecord1 = Record{"test.weave.local.", net.ParseIP("10.20.20.10"), 0, 0} + testRecord1 = Record{"test.weave.local.", net.ParseIP("10.20.20.10"), 0, 0, 0} testInAddr1 = "10.20.20.10.in-addr.arpa." ) diff --git a/nameserver/server_test.go b/nameserver/server_test.go index 47576d04..37b483c9 100644 --- a/nameserver/server_test.go +++ b/nameserver/server_test.go @@ -133,7 +133,7 @@ func TestTCPDNSServer(t *testing.T) { bs := make([]byte, 4) for i := 0; i < numAnswers; i++ { binary.LittleEndian.PutUint32(bs, uint32(i)) - addrs = append(addrs, Record{"", net.IPv4(bs[0], bs[1], bs[2], bs[3]), 0, 0}) + addrs = append(addrs, Record{"", net.IPv4(bs[0], bs[1], bs[2], bs[3]), 0, 0, 0}) } // handler for the fallback server: it will just return a very long response diff --git a/nameserver/zone.go b/nameserver/zone.go index 09bd2a54..ca23dab1 100644 --- a/nameserver/zone.go +++ b/nameserver/zone.go @@ -85,7 +85,7 @@ func (zone *ZoneDb) LookupName(name string) ([]ZoneRecord, error) { defer zone.mx.RUnlock() for _, r := range zone.recs { if r.Name == name { - return []ZoneRecord{Record{r.Name, r.IP, 0, 0}}, nil + return []ZoneRecord{Record{r.Name, r.IP, 0, 0, 0}}, nil } } return nil, LookupError(name) @@ -100,7 +100,7 @@ func (zone *ZoneDb) LookupInaddr(inaddr string) ([]ZoneRecord, error) { defer zone.mx.RUnlock() for _, r := range zone.recs { if r.IP.Equal(ip) { - return []ZoneRecord{Record{r.Name, r.IP, 0, 0}}, nil + return []ZoneRecord{Record{r.Name, r.IP, 0, 0, 0}}, nil } } return nil, LookupError(inaddr)