Check we can use our mDNS client with our mDNS server

This commit is contained in:
Alvaro Saurin committed 2015-05-26 21:24:26 +02:00
1 parent 5fe0a7fbb2
commit b87b942363
7 files changed
+276 -131

No files matched your search

+61 -91
View File
@@ -6,6 +6,20 @@ import (
"time"
)
func (zone *zoneDb) lookup(target string, lfun func(ns *namesSet) []*recordEntry) (res []ZoneRecord, err error) {
for identName, nameset := range zone.idents {
if identName != defaultRemoteIdent {
for _, ze := range lfun(nameset) {
res = append(res, ze)
}
}
}
if len(res) == 0 {
err = LookupError(target)
}
return
}
// Perform a lookup for a name in the zone
// The name can be resolved locally with the local database
func (zone *zoneDb) LookupName(name string) (res []ZoneRecord, err error) {
@@ -15,17 +29,50 @@ func (zone *zoneDb) LookupName(name string) (res []ZoneRecord, err error) {
// note: LookupName() is usually called from the mDNS server, so we do not touch the name
name = dns.Fqdn(name)
Debug.Printf("[zonedb] Looking for name '%s' in local database", name)
for identName, nameset := range zone.idents {
if identName != defaultRemoteIdent {
for _, ze := range nameset.getEntriesForName(name) {
res = append(res, ze)
}
}
return zone.lookup(name, func(ns *namesSet) []*recordEntry { return ns.getEntriesForName(name) })
}
// Perform a lookup for a IP address in the zone
// The address can be resolved locally with the local database
func (zone *zoneDb) LookupInaddr(inaddr string) (res []ZoneRecord, err error) {
zone.mx.RLock()
defer zone.mx.RUnlock()
// note: LookupInaddr() is usually called from the mDNS server, so we do not touch the name
revIPv4, err := raddrToIPv4(inaddr)
if err != nil {
return nil, newParseError("lookup address", inaddr)
}
Debug.Printf("[zonedb] Looking for address in local database: '%s' (%s)", revIPv4, inaddr)
return zone.lookup(inaddr, func(ns *namesSet) []*recordEntry { return ns.getEntriesForIP(revIPv4) })
}
// Perform a domain lookup with mDNS
func (zone *zoneDb) domainLookup(target string, lfun ZoneLookupFunc) (res []ZoneRecord, err error) {
// no local results have been obtained in the local database: try with a mDNS query
Debug.Printf("[zonedb] '%s' not in local database... trying with mDNS", target)
lanswers, err := lfun(target)
if err != nil {
Debug.Printf("[zonedb] mDNS lookup error for '%s': %s", target, err)
return nil, err
}
if len(res) == 0 {
err = LookupError(name)
// if the request has been successful, save the IP in the local database and return the corresponding ZoneRecord
// (we do not get the remote ident in the mDNS reply, so we save it in a "remote" ident)
Debug.Printf("[zonedb] adding '%s' (obtained with mDNS) to '%s'", lanswers, target)
res = make([]ZoneRecord, len(lanswers))
zone.mx.Lock()
now := zone.clock.Now()
remoteIdent := zone.getNamesSet(defaultRemoteIdent)
for i, answer := range lanswers {
res[i], err = remoteIdent.addIPToName(answer, now)
if err != nil {
zone.mx.Unlock()
Warning.Printf("[zonedb] '%s' insertion for %s failed: %s", answer, target, err)
return nil, err
}
}
zone.mx.Unlock()
return
}
@@ -40,7 +87,7 @@ func (zone *zoneDb) DomainLookupName(name string) (res []ZoneRecord, err error)
for identName, nameset := range zone.idents {
for _, ze := range nameset.getEntriesForName(name) {
// filter the entries with expired TTL
// locally introduced entries are nver expired as the always have TTL=0
// locally introduced entries are never expired: they always have TTL=0
if ze.hasExpired(now) {
Debug.Printf("[zonedb] '%s': expired entry '%s' ignored: removing", name, ze)
nameset.deleteName(name)
@@ -57,69 +104,16 @@ func (zone *zoneDb) DomainLookupName(name string) (res []ZoneRecord, err error)
if len(res) > 0 {
Debug.Printf("[zonedb] '%s' resolved in local database", name)
} else {
// no local results have been obtained in the local database: try with a mDNS query
// (this request is not a background query, so we cannot delay the response)
Debug.Printf("[zonedb] name '%s' not in local database: trying with mDNS", name)
ips, err := zone.mdnsCli.LookupName(name)
if err != nil {
Debug.Printf("[zonedb] mDNS lookup error for '%s': %s", name, err)
return nil, err
}
// if the request has been successful, save the IP in the local database and return the corresponding ZoneRecord
// (we do not get the remote ident in the mDNS reply, so we save it in a "remote" ident)
Debug.Printf("[zonedb] adding '%s' (obtained with mDNS) to '%s'", ips, name)
res = make([]ZoneRecord, len(ips))
zone.mx.Lock()
now = zone.clock.Now()
for i, zr := range ips {
res[i], err = zone.getNamesSet(defaultRemoteIdent).addIPToName(zr, now)
if err != nil {
zone.mx.Unlock()
Warning.Printf("[zonedb] IP [%s] insertion for '%s' failed: %s", ips, name, err)
return nil, err
}
}
zone.mx.Unlock()
res, err = zone.domainLookup(name, zone.mdnsCli.LookupName)
}
if len(res) > 0 {
// start (if we need to) a background refresh for the name
// note: we do not spend time trying to update names that did not return an initial response...
zone.startUpdatingName(name)
return res, nil
}
return nil, LookupError(name)
}
// Perform a lookup for a IP address in the zone
// The address can be resolved locally with the local database
func (zone *zoneDb) LookupInaddr(inaddr string) (res []ZoneRecord, err error) {
zone.mx.RLock()
defer zone.mx.RUnlock()
// note: LookupInaddr() is usually called from the mDNS server, so we do not touch the name
revIPv4, err := raddrToIPv4(inaddr)
if err != nil {
return nil, newParseError("lookup address", inaddr)
}
Debug.Printf("[zonedb] Looking for address in local database: '%s' (%s)", revIPv4, inaddr)
for identName, nameset := range zone.idents {
if identName != defaultRemoteIdent {
for _, ze := range nameset.getEntriesForIP(revIPv4) {
res = append(res, ZoneRecord(ze))
}
}
}
if len(res) == 0 {
err = LookupError(inaddr)
}
return
}
// Perform a lookup for a IP address in the zone
// The address can be resolved either with the local database or
// with some other resolution method (eg, a mDNS query)
@@ -136,7 +130,7 @@ func (zone *zoneDb) DomainLookupInaddr(inaddr string) (res []ZoneRecord, err err
for identName, nameset := range zone.idents {
for _, ze := range nameset.getEntriesForIP(revIPv4) {
// filter the entries with expired TTL
// locally introduced entries are nver expired as the always have TTL=0
// locally introduced entries are never expired: they always have TTL=0
if ze.hasExpired(now) {
Debug.Printf("[zonedb] '%s': expired entry '%s' ignored: removing", revIPv4, ze)
nameset.deleteIP(revIPv4)
@@ -153,44 +147,20 @@ func (zone *zoneDb) DomainLookupInaddr(inaddr string) (res []ZoneRecord, err err
if len(res) > 0 {
Debug.Printf("[zonedb] '%s' resolved in local database", inaddr)
} else {
// no local results have been obtained in the local database: try with a mDNS query
Debug.Printf("[zonedb] '%s'(%+v) not in local database... trying with mDNS", inaddr, revIPv4)
names, err := zone.mdnsCli.LookupInaddr(inaddr)
if err != nil {
Debug.Printf("[zonedb] mDNS lookup error for '%s': %s", inaddr, err)
return nil, err
}
// if the request has been successful, save the IP in the local database and return the corresponding ZoneRecord
// (we do not get the remote ident in the mDNS reply, so we save it in a "remote" ident)
Debug.Printf("[zonedb] adding '%s' (obtained with mDNS) to '%s'", names, revIPv4)
res = make([]ZoneRecord, len(names))
zone.mx.Lock()
now = zone.clock.Now()
for i, name := range names {
res[i], err = zone.getNamesSet(defaultRemoteIdent).addIPToName(name, now)
if err != nil {
zone.mx.Unlock()
Warning.Printf("[zonedb] Name '%s' insertion for %s failed: %s", name.Name(), revIPv4, err)
return nil, err
}
}
zone.mx.Unlock()
res, err = zone.domainLookup(inaddr, zone.mdnsCli.LookupInaddr)
}
if len(res) > 0 {
// note: even for reverse addresses, we perform the background updates in the name, not in the IP
// this simplifies the process and produces basically the same results...
// note: we do not spend time trying to update names that did not return an initial response...
for _, name := range res {
zone.startUpdatingName(name.Name())
for _, r := range res {
zone.startUpdatingName(r.Name())
}
return res, nil
}
return nil, LookupError(inaddr)
}
//////////////////////////////////////////////////////////////////////////////