dnssd

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2018 License: MIT Imports: 15 Imported by: 64

README

DNS-SD

Build Status

This library implements Multicast DNS and DNS-Based Service Discovery to provide zero-configuration operations. It lets you announce and find services in a specific link-local domain.

Usage

Create a mDNS responder

The following code creates a service with name "My Website._http._tcp.local." for the host "My Computer" which has the IP "192.168.0.123" on port "12345". The service is added to a responder.

service := dnssd.NewService("My Website", "_http._tcp.", "local.", "My Computer", []net.IP{net.ParseIP("192.168.0.123")}, 12345)
resp, _ := dnssd.NewResponder()
handle, _ := resp.Add(service)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resp.Respond(ctx)

If the service should be published on all available network interfaces, you can provide an empty host name and no IP addresses. The service will then get the local host name and IP addresses assigned to it.

service := dnssd.NewService("My Website", "_http._tcp.", "local.", "", nil, 12345)

When calling Respond the responder probes for the service instance name and host name to be unqiue in the network. Once probing is finished, the service will be announced.

Update TXT records

Once a service is added to a responder, you have to use the handle object to update properties.

handle.UpdateText(map[string]string{"key1": "value1", "key2": "value2"}, resp)

Examples

There are examples in the _cmd directory to register services, resolve service instances and to browse for service types.

Conformance

This library passes the multicast DNS tests of Apple's Bonjour Conformance Test.

TODO

  • Support negative responses (RFC6762 6.1)
  • Handle txt records case insensitive
  • Remove outdated services from cache regularly
  • Make sure that hostnames are FQDNs

Contact

Matthias Hochgatterer

Github: https://github.com/brutella

Twitter: https://twitter.com/brutella

License

dnssd is available under the MIT license. See the LICENSE file for more info.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	IPv4LinkLocalMulticast = net.ParseIP("224.0.0.251")
	IPv6LinkLocalMulticast = net.ParseIP("ff02::fb")

	AddrIPv4LinkLocalMulticast = &net.UDPAddr{
		IP:   IPv4LinkLocalMulticast,
		Port: 5353,
	}

	AddrIPv6LinkLocalMulticast = &net.UDPAddr{
		IP:   IPv6LinkLocalMulticast,
		Port: 5353,
	}

	TtlDefault  uint32 = 75 * 60 // Default ttl for mDNS resource records
	TtlHostname uint32 = 120     // TTL for mDNS resource records containing the host name
)
View Source
var DefaultResponder, _ = NewResponder()

Functions

func A

func A(srv Service, iface *net.Interface) []*dns.A

func AAAA

func AAAA(srv Service, iface *net.Interface) []*dns.AAAA

func DNSSDServicesPTR

func DNSSDServicesPTR(srv Service) *dns.PTR

func LookupType

func LookupType(ctx context.Context, service string, add AddServiceFunc, rmv RmvServiceFunc) (err error)

func NSEC

func NSEC(rr dns.RR, srv Service, iface *net.Interface) *dns.NSEC

func PTR

func PTR(srv Service) *dns.PTR

func SRV

func SRV(srv Service) *dns.SRV

func TXT

func TXT(srv Service) *dns.TXT

Types

type AddServiceFunc

type AddServiceFunc func(Service)

type Cache

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

func NewCache

func NewCache() *Cache

func (*Cache) UpdateFrom

func (c *Cache) UpdateFrom(msg *dns.Msg) (adds []*Service, rmvs []*Service)

UpdateFrom updates the cache from resource records in msg. TODO consider the cache-flush bit to make records as to be deleted in one second

type MDNSConn

type MDNSConn interface {
	// SendQuery sends a mDNS query.
	SendQuery(q *Query) error

	// SendResponse sends a mDNS response
	SendResponse(resp *Response) error

	// Read returns a channel which receives mDNS messages
	Read(ctx context.Context) <-chan *Request

	// Close closes the connection
	Close()
}

MDNSConn represents a mDNS connection. It encapsulates an IPv4 and IPv6 UDP connection.

func NewMDNSConn

func NewMDNSConn() (MDNSConn, error)

type Query

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

Query is a mDNS query

type ReadFunc

type ReadFunc func(*Request)

type Request

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

Request represents an incoming mDNS message

type Responder

type Responder interface {
	// Add adds a service to the responder.
	// Use the returned service handle to update service properties.
	Add(srv Service) (ServiceHandle, error)
	// Remove removes the service associated with the service handle from the responder.
	Remove(srv ServiceHandle)
	// Respond makes the receiver announcing and managing services.
	Respond(ctx context.Context) error
}

Responder represents a mDNS responder.

func NewResponder

func NewResponder() (Responder, error)

type Response

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

Response is a mDNS response

type RmvServiceFunc

type RmvServiceFunc func(Service)

type Service

type Service struct {
	Name     string // e.g. Accessory (no trailing dot)
	Type     string // e.g. _hap._tcp.
	Domain   string // e.g. local.
	Host     string // e.g. MacBook (no trailing dot)
	Text     map[string]string
	Ttl      time.Duration // Original time to live
	Port     int
	IPs      []net.IP
	IfaceIPs map[string][]net.IP
	// contains filtered or unexported fields
}

Service represents a DNS-SD service instance

func LookupInstance

func LookupInstance(ctx context.Context, instance string) (srv Service, err error)

LookupInstance resolves a service by its service instance name.

func NewService

func NewService(name, typ, domain, host string, ips []net.IP, port int) Service

func ProbeService

func ProbeService(ctx context.Context, srv Service) (Service, error)

ProbeService probes for the hostname and service instance name of srv. If err == nil, the returned service is verified to be unique on the local network.

func ReprobeService

func ReprobeService(ctx context.Context, srv Service) (Service, error)

func (Service) Copy

func (srv Service) Copy() *Service

func (Service) Equal

func (srv Service) Equal(other Service) bool

func (Service) Hostname

func (srv Service) Hostname() string

func (Service) ServiceInstanceName

func (srv Service) ServiceInstanceName() string

func (Service) ServiceName

func (srv Service) ServiceName() string

func (Service) ServicesMetaQueryName

func (srv Service) ServicesMetaQueryName() string

func (*Service) SetHostname

func (srv *Service) SetHostname(hostname string)

type ServiceHandle

type ServiceHandle interface {
	UpdateText(text map[string]string, r Responder)
	Service() Service
}

ServiceHandle serves a middleman between a service and a responder.

Directories

Path Synopsis
bct command

Jump to

Keyboard shortcuts

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