Added a TTL to the zone records
This commit is contained in:
1 parent
22a229c305
commit
7de2080b9e
7 files changed
+22
-13
No files matched your search
@@ -15,6 +15,7 @@ type ZoneRecord interface {
|
|||||||
IP() net.IP // The IP (v4) address
|
IP() net.IP // The IP (v4) address
|
||||||
Priority() int // The priority
|
Priority() int // The priority
|
||||||
Weight() int // The weight
|
Weight() int // The weight
|
||||||
|
TTL() int // TTL
|
||||||
}
|
}
|
||||||
|
|
||||||
type ZoneLookup interface {
|
type ZoneLookup interface {
|
||||||
@@ -41,12 +42,14 @@ type Record struct {
|
|||||||
ip net.IP
|
ip net.IP
|
||||||
priority int
|
priority int
|
||||||
weight int
|
weight int
|
||||||
|
ttl int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Record) Name() string { return r.name }
|
func (r Record) Name() string { return r.name }
|
||||||
func (r Record) IP() net.IP { return r.ip }
|
func (r Record) IP() net.IP { return r.ip }
|
||||||
func (r Record) Priority() int { return r.priority }
|
func (r Record) Priority() int { return r.priority }
|
||||||
func (r Record) Weight() int { return r.weight }
|
func (r Record) Weight() int { return r.weight }
|
||||||
|
func (r Record) TTL() int { return r.ttl }
|
||||||
|
|
||||||
func (i Record) String() string {
|
func (i Record) String() string {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
@@ -62,6 +65,9 @@ func (i Record) String() string {
|
|||||||
if i.Weight() > 0 {
|
if i.Weight() > 0 {
|
||||||
fmt.Fprintf(&buf, "/W:%d", i.Weight())
|
fmt.Fprintf(&buf, "/W:%d", i.Weight())
|
||||||
}
|
}
|
||||||
|
if i.TTL() > 0 {
|
||||||
|
fmt.Fprintf(&buf, "/TTL:%d", i.TTL())
|
||||||
|
}
|
||||||
|
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func TestCacheLength(t *testing.T) {
|
|||||||
question := &questionMsg.Question[0]
|
question := &questionMsg.Question[0]
|
||||||
|
|
||||||
ip := net.ParseIP(fmt.Sprintf("10.0.1.%d", i))
|
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 := makeAddressReply(questionMsg, question, records)
|
||||||
reply.Answer[0].Header().Ttl = uint32(i)
|
reply.Answer[0].Header().Ttl = uint32(i)
|
||||||
@@ -83,7 +83,7 @@ func TestCacheEntries(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("Inserting the reply")
|
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)
|
reply1 := makeAddressReply(questionMsg, question, records)
|
||||||
l.Put(questionMsg, reply1, nullTTL, 0, time.Now())
|
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")
|
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)
|
replyTemp := makeAddressReply(questionMsg, question, records)
|
||||||
l.Put(questionMsg, replyTemp, nullTTL, 0, time.Now())
|
l.Put(questionMsg, replyTemp, nullTTL, 0, time.Now())
|
||||||
lenBefore := l.Len()
|
lenBefore := l.Len()
|
||||||
@@ -131,11 +131,11 @@ func TestCacheEntries(t *testing.T) {
|
|||||||
|
|
||||||
t.Logf("Inserting a two replies for the same query")
|
t.Logf("Inserting a two replies for the same query")
|
||||||
timePut2 := time.Now()
|
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)
|
reply2 := makeAddressReply(questionMsg, question, records)
|
||||||
l.Put(questionMsg, reply2, nullTTL, 0, timePut2)
|
l.Put(questionMsg, reply2, nullTTL, 0, timePut2)
|
||||||
timePut3 := timePut2.Add(time.Duration(1) * time.Second)
|
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)
|
reply3 := makeAddressReply(questionMsg, question, records)
|
||||||
l.Put(questionMsg, reply3, nullTTL, 0, timePut3)
|
l.Put(questionMsg, reply3, nullTTL, 0, timePut3)
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ func TestCacheEntries(t *testing.T) {
|
|||||||
wt.AssertNil(t, resp, "reponse from Get() yet")
|
wt.AssertNil(t, resp, "reponse from Get() yet")
|
||||||
|
|
||||||
t.Logf("Checking that an Remove() between Get() and Put() does not break things")
|
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)
|
replyTemp2 := makeAddressReply(questionMsg2, question2, records)
|
||||||
l.Remove(question2)
|
l.Remove(question2)
|
||||||
l.Put(questionMsg2, replyTemp2, nullTTL, 0, time.Now())
|
l.Put(questionMsg2, replyTemp2, nullTTL, 0, time.Now())
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ var (
|
|||||||
type Response struct {
|
type Response struct {
|
||||||
name string
|
name string
|
||||||
addr net.IP
|
addr net.IP
|
||||||
|
ttl int
|
||||||
err error
|
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) IP() net.IP { return r.addr }
|
||||||
func (r Response) Priority() int { return 0 }
|
func (r Response) Priority() int { return 0 }
|
||||||
func (r Response) Weight() 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 {
|
func (r Response) Equal(r2 *Response) bool {
|
||||||
if r.name != r2.name {
|
if r.name != r2.name {
|
||||||
@@ -182,10 +184,11 @@ func (c *MDNSClient) ResponseCallback(r *dns.Msg) {
|
|||||||
switch rr := answer.(type) {
|
switch rr := answer.(type) {
|
||||||
case *dns.A:
|
case *dns.A:
|
||||||
name = rr.Hdr.Name
|
name = rr.Hdr.Name
|
||||||
res = &Response{addr: rr.A}
|
res = &Response{name: name, addr: rr.A, ttl: int(rr.Hdr.Ttl)}
|
||||||
case *dns.PTR:
|
case *dns.PTR:
|
||||||
name = rr.Hdr.Name
|
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:
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func (c *testContext) checkResponse(t *testing.T, channelOk bool, resp *Response
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
wt.AssertNoErr(t, resp.err)
|
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.receivedAddr = resp.IP()
|
||||||
c.receivedCount++
|
c.receivedCount++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func TestServerSimpleQuery(t *testing.T) {
|
|||||||
InitDefaultLogging(testing.Verbose())
|
InitDefaultLogging(testing.Verbose())
|
||||||
|
|
||||||
var (
|
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."
|
testInAddr1 = "10.20.20.10.in-addr.arpa."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ func TestTCPDNSServer(t *testing.T) {
|
|||||||
bs := make([]byte, 4)
|
bs := make([]byte, 4)
|
||||||
for i := 0; i < numAnswers; i++ {
|
for i := 0; i < numAnswers; i++ {
|
||||||
binary.LittleEndian.PutUint32(bs, uint32(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
|
// handler for the fallback server: it will just return a very long response
|
||||||
|
|||||||
+2
-2
@@ -85,7 +85,7 @@ func (zone *ZoneDb) LookupName(name string) ([]ZoneRecord, error) {
|
|||||||
defer zone.mx.RUnlock()
|
defer zone.mx.RUnlock()
|
||||||
for _, r := range zone.recs {
|
for _, r := range zone.recs {
|
||||||
if r.Name == name {
|
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)
|
return nil, LookupError(name)
|
||||||
@@ -100,7 +100,7 @@ func (zone *ZoneDb) LookupInaddr(inaddr string) ([]ZoneRecord, error) {
|
|||||||
defer zone.mx.RUnlock()
|
defer zone.mx.RUnlock()
|
||||||
for _, r := range zone.recs {
|
for _, r := range zone.recs {
|
||||||
if r.IP.Equal(ip) {
|
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)
|
return nil, LookupError(inaddr)
|
||||||
|
|||||||
Reference in new issue
Block a user