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