Interfaces for the mDNS client and server
This commit is contained in:
1 parent
6197f98c26
commit
c1d124df4c
4 files changed
+39
-4
No files matched your search
@@ -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)
|
||||||
|
}
|
||||||
@@ -11,13 +11,14 @@ type MDNSServer struct {
|
|||||||
sendconn *net.UDPConn
|
sendconn *net.UDPConn
|
||||||
srv *dns.Server
|
srv *dns.Server
|
||||||
zone Zone
|
zone Zone
|
||||||
|
allowLocal bool
|
||||||
running bool
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new mDNS server
|
// Create a new mDNS server
|
||||||
// Nothing will be done (including port bindings) until you `Start()` the server
|
// Nothing will be done (including port bindings) until you `Start()` the server
|
||||||
func NewMDNSServer(zone Zone) (*MDNSServer, error) {
|
func NewMDNSServer(zone Zone, local bool) (*MDNSServer, error) {
|
||||||
return &MDNSServer{zone: zone}, nil
|
return &MDNSServer{zone: zone, allowLocal: local}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true if testaddr is a UDP address with IP matching my local i/f
|
// 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
|
// 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
|
// the answer, and if it does ask, it'll be happy to get an
|
||||||
// answer.
|
// 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...
|
// TODO: use any Answer in the query for adding records in the Zone database...
|
||||||
if len(r.Answer) == 0 && len(r.Question) > 0 {
|
if len(r.Answer) == 0 && len(r.Question) > 0 {
|
||||||
q := &r.Question[0]
|
q := &r.Question[0]
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func TestServerSimpleQuery(t *testing.T) {
|
|||||||
Info.Println("TestServerSimpleQuery starting")
|
Info.Println("TestServerSimpleQuery starting")
|
||||||
|
|
||||||
mzone := newMockedZoneWithRecords([]ZoneRecord{testRecord1, testRecord2})
|
mzone := newMockedZoneWithRecords([]ZoneRecord{testRecord1, testRecord2})
|
||||||
mdnsServer, err := NewMDNSServer(mzone)
|
mdnsServer, err := NewMDNSServer(mzone, true)
|
||||||
wt.AssertNoErr(t, err)
|
wt.AssertNoErr(t, err)
|
||||||
err = mdnsServer.Start(nil)
|
err = mdnsServer.Start(nil)
|
||||||
wt.AssertNoErr(t, err)
|
wt.AssertNoErr(t, err)
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.mdnsSrv, err = NewMDNSServer(s.Zone)
|
s.mdnsSrv, err = NewMDNSServer(s.Zone, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user