Parse reverse address lookup IPs in the Zone

.. rather than making clients of the zone do it. At the minute there's
only one call site, but soon there will be more.
This commit is contained in:
Michael Bridgen committed 2015-01-08 13:39:33 +00:00
1 parent 71d96eed7d
commit 3866bd5e63
3 files changed
+33 -26

No files matched your search

+8 -17
View File
@@ -9,12 +9,8 @@ import (
const (
LOCAL_DOMAIN = "weave.local."
RDNS_DOMAIN = "in-addr.arpa."
)
// +1 to also exclude a dot
var rdnsDomainLen = len(RDNS_DOMAIN) + 1
func checkFatal(e error) {
if e != nil {
Error.Fatal(e)
@@ -77,19 +73,14 @@ func rdnsHandler(zone Zone, mdnsClient *MDNSClient) dns.HandlerFunc {
q := r.Question[0]
Debug.Printf("Local rdns query: %+v", q)
if q.Qtype == dns.TypePTR {
if ip := net.ParseIP(q.Name[:len(q.Name)-rdnsDomainLen]); ip != nil {
ip4 := ip.To4()
revIP := []byte{ip4[3], ip4[2], ip4[1], ip4[0]}
Debug.Printf("Looking for address: %+v", revIP)
if name, err := zone.ReverseLookupLocal(revIP); err == nil {
Debug.Printf("Found name: %s", name)
m := makePTRReply(r, &q, []string{name})
w.WriteMsg(m)
} else {
Debug.Printf("Failed lookup for %s; sending mDNS query", q.Name)
// We don't know the answer; see if someone else does
// TODO
}
if name, err := zone.ReverseLookupLocal(q.Name); err == nil {
Debug.Printf("Found name: %s", name)
m := makePTRReply(r, &q, []string{name})
w.WriteMsg(m)
} else {
Debug.Printf("Failed lookup for %s; sending mDNS query", q.Name)
// We don't know the answer; see if someone else does
// TODO
}
}
}
+24 -8
View File
@@ -2,16 +2,24 @@ package nameserver
import (
"github.com/miekg/dns"
. "github.com/zettio/weave/common"
"net"
"sync"
)
const (
RDNS_DOMAIN = "in-addr.arpa."
)
// +1 to also exclude a dot
var rdnsDomainLen = len(RDNS_DOMAIN) + 1
type Zone interface {
AddRecord(ident string, name string, ip net.IP) error
DeleteRecord(ident string, ip net.IP) error
DeleteRecordsFor(ident string) error
LookupLocal(name string) (net.IP, error)
ReverseLookupLocal(ip net.IP) (string, error)
ReverseLookupLocal(inaddr string) (string, error)
}
type Record struct {
@@ -60,15 +68,23 @@ func (zone *ZoneDb) LookupLocal(name string) (net.IP, error) {
return nil, LookupError(name)
}
func (zone *ZoneDb) ReverseLookupLocal(ip net.IP) (string, error) {
zone.mx.RLock()
defer zone.mx.RUnlock()
for _, r := range zone.recs {
if r.IP.Equal(ip) {
return r.Name, nil
func (zone *ZoneDb) ReverseLookupLocal(inaddr string) (string, error) {
if revIP := net.ParseIP(inaddr[:len(inaddr)-rdnsDomainLen]); revIP != nil {
revIP4 := revIP.To4()
ip := []byte{revIP4[3], revIP4[2], revIP4[1], revIP4[0]}
Debug.Printf("Looking for address: %+v", ip)
zone.mx.RLock()
defer zone.mx.RUnlock()
for _, r := range zone.recs {
if r.IP.Equal(ip) {
return r.Name, nil
}
}
return "", LookupError(inaddr)
} else {
Warning.Printf("Asked to reverse lookup %s", inaddr)
return "", LookupError(inaddr)
}
return "", LookupError(ip.String())
}
func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) error {
+1 -1
View File
@@ -35,7 +35,7 @@ func TestZone(t *testing.T) {
}
// See if we can find the address by IP.
foundName, err := zone.ReverseLookupLocal(ip)
foundName, err := zone.ReverseLookupLocal("1.2.0.10.in-addr.arpa.")
wt.AssertNoErr(t, err)
if foundName != successTestName {