This separates Zone into the DB ops and the Lookup ops, and implements the latter for MDNSClient so it can be used just like the zonedb. A notable change is that Lookup methods only ever return one result, while MDNSClient.SendQuery could in theory return multiple results (though it wouldn't, because no server would supply more than one result in a response, and it stops waiting after getting one response ..). ReverseLookupLocal is implemented even though no MDNSServer will answer at present (hence no test yet). Lastly, the handler (creating) functions can now be given a list of Lookup implementations to try in order.
38 lines
857 B
Go
38 lines
857 B
Go
package nameserver
|
|
|
|
import (
|
|
"github.com/miekg/dns"
|
|
. "github.com/zettio/weave/common"
|
|
"net"
|
|
)
|
|
|
|
func mdnsLookup(client *MDNSClient, name string, qtype uint16) (*ResponseA, error) {
|
|
channel := make(chan *ResponseA)
|
|
client.SendQuery(name, qtype, channel)
|
|
for resp := range channel {
|
|
Debug.Printf("Got response name %s addr %s", resp.Name, resp.Addr)
|
|
if err := resp.Err; err != nil {
|
|
return nil, err
|
|
} else {
|
|
return resp, nil
|
|
}
|
|
}
|
|
return nil, LookupError(name)
|
|
}
|
|
|
|
func (client *MDNSClient) LookupLocal(name string) (net.IP, error) {
|
|
if r, e := mdnsLookup(client, name, dns.TypeA); r != nil {
|
|
return r.Addr, nil
|
|
} else {
|
|
return nil, e
|
|
}
|
|
}
|
|
|
|
func (client *MDNSClient) ReverseLookupLocal(inaddr string) (string, error) {
|
|
if r, e := mdnsLookup(client, inaddr, dns.TypePTR); r != nil {
|
|
return r.Name, nil
|
|
} else {
|
|
return "", e
|
|
}
|
|
}
|