Interfaces for the mDNS client and server

This commit is contained in:
Alvaro Saurin committed 2015-05-26 21:11:34 +02:00
1 parent 6197f98c26
commit c1d124df4c
4 files changed
+39 -4

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
package nameserver
import "net"
// A basic mDNS service
type ZoneMDNS interface {
// Start the service
Start(*net.Interface) error
// Stop the service
Stop() error
}
// A mDNS server
type ZoneMDNSServer interface {
ZoneMDNS
// Return the Zone database used by the server
Zone() Zone
}
// A mDNS client interface
type ZoneMDNSClient interface {
ZoneMDNS
ZoneLookup
// Perform an insistent lookup for a name
InsistentLookupName(name string) ([]ZoneRecord, error)
// Perform an insistent lookup for a reverse address
InsistentLookupInaddr(inaddr string) ([]ZoneRecord, error)
}
+9 -2
View File
@@ -11,13 +11,14 @@ type MDNSServer struct {
sendconn *net.UDPConn
srv *dns.Server
zone Zone
allowLocal bool
running bool
}
// Create a new mDNS server
// Nothing will be done (including port bindings) until you `Start()` the server
func NewMDNSServer(zone Zone) (*MDNSServer, error) {
return &MDNSServer{zone: zone}, nil
func NewMDNSServer(zone Zone, local bool) (*MDNSServer, error) {
return &MDNSServer{zone: zone, allowLocal: local}, nil
}
// Return true if testaddr is a UDP address with IP matching my local i/f
@@ -112,6 +113,12 @@ func (s *MDNSServer) makeHandler(qtype uint16, lookup LookupFunc) dns.HandlerFun
// assumption that the client wouldn't ask if it already knew
// the answer, and if it does ask, it'll be happy to get an
// answer.
remoteAddr := rw.RemoteAddr()
if s.addrIsLocal(remoteAddr) && !s.allowLocal {
Debug.Printf("[mdns] srv: mDNS query from local host ('%s'): ignored", remoteAddr)
return
}
// TODO: use any Answer in the query for adding records in the Zone database...
if len(r.Answer) == 0 && len(r.Question) > 0 {
q := &r.Question[0]
+1 -1
View File
@@ -20,7 +20,7 @@ func TestServerSimpleQuery(t *testing.T) {
Info.Println("TestServerSimpleQuery starting")
mzone := newMockedZoneWithRecords([]ZoneRecord{testRecord1, testRecord2})
mdnsServer, err := NewMDNSServer(mzone)
mdnsServer, err := NewMDNSServer(mzone, true)
wt.AssertNoErr(t, err)
err = mdnsServer.Start(nil)
wt.AssertNoErr(t, err)
+1 -1
View File
@@ -120,7 +120,7 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
if err != nil {
return
}
s.mdnsSrv, err = NewMDNSServer(s.Zone)
s.mdnsSrv, err = NewMDNSServer(s.Zone, false)
if err != nil {
return
}