mdns

package module
v0.0.0-...-63d122e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2019 License: MIT Imports: 10 Imported by: 1

README

mdns

Simple mDNS client/server library in Golang. mDNS or Multicast DNS can be used to discover services on the local network without the use of an authoritative DNS server. This enables peer-to-peer discovery. It is important to note that many networks restrict the use of multicasting, which prevents mDNS from functioning. Notably, multicast cannot be used in any sort of cloud, or shared infrastructure environment. However it works well in most office, home, or private infrastructure environments.

Using the library is very simple, here is an example of publishing a service entry:

// Setup our service export
host, _ := os.Hostname()
service := &mdns.MDNSService{
    Instance: host,
    Service: "_foobar._tcp",
    Port:    8000,
    Info:    "My awesome service",
}
service.Init()

// Create the mDNS server, defer shutdown
server, _ := mdns.NewServer(&mdns.Config{Zone: service})
defer server.Shutdown()

Doing a lookup for service providers is also very simple:

// Make a channel for results and start listening
entriesCh := make(chan *mdns.ServiceEntry, 4)
go func() {
    for entry := range entriesCh {
        fmt.Printf("Got new entry: %v\n", entry)
    }
}()

// Start the lookup
mdns.Lookup("_foobar._tcp", entriesCh)
close(entriesCh)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lookup

func Lookup(service string, entries chan<- *ServiceEntry) error

Lookup is the same as Query, however it uses all the default parameters

func Query

func Query(params *QueryParam) error

Query looks up a given service, in a domain, waiting at most for a timeout before finishing the query. The results are streamed to a channel. Sends will not block, so clients should make sure to either read or buffer.

Types

type Config

type Config struct {
	// Zone must be provided to support responding to queries
	Zone Zone

	// Iface if provided binds the multicast listener to the given
	// interface. If not provided, the system default multicase interface
	// is used.
	Iface *net.Interface

	IPv4Addr *net.UDPAddr
	IPv6Addr *net.UDPAddr
}

Config is used to configure the mDNS server

type MDNSService

type MDNSService struct {
	Instance string // Instance name (e.g. host name)
	Service  string // Service name (e.g. _http._tcp.)
	HostName string // Host machine DNS name
	Port     int    // Service Port
	Info     string // Service info served as a TXT record
	Domain   string // If blank, assumes "local"

	Addr net.IP // @Deprecated. Service IP
	// contains filtered or unexported fields
}

MDNSService is used to export a named service by implementing a Zone

func (*MDNSService) Init

func (m *MDNSService) Init() error

Init should be called to setup the internal state

func (*MDNSService) Records

func (m *MDNSService) Records(q dns.Question) []dns.RR

type QueryParam

type QueryParam struct {
	Service   string               // Service to lookup
	Domain    string               // Lookup domain, default "local"
	Timeout   time.Duration        // Lookup timeout, default 1 second
	Interface *net.Interface       // Multicast interface to use
	Entries   chan<- *ServiceEntry // Entries Channel

	IPv4mdns *net.UDPAddr
	IPv6mdns *net.UDPAddr
}

QueryParam is used to customize how a Lookup is performed

func DefaultParams

func DefaultParams(service string) *QueryParam

DefaultParams is used to return a default set of QueryParam's

type Server

type Server struct {
	// contains filtered or unexported fields
}

mDNS server is used to listen for mDNS queries and respond if we have a matching local record

func NewServer

func NewServer(config *Config) (*Server, error)

NewServer is used to create a new mDNS server from a config

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown is used to shutdown the listener

type ServiceEntry

type ServiceEntry struct {
	Name   string
	Host   string
	AddrV4 net.IP
	AddrV6 net.IP
	Port   int
	Info   string

	Addr net.IP // @Deprecated
	// contains filtered or unexported fields
}

ServiceEntry is returned after we query for a service

type Zone

type Zone interface {
	Records(q dns.Question) []dns.RR
}

Zone is the interface used to integrate with the server and to serve records dynamically

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL