mdns

package
v0.0.0-...-82b0641 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2021 License: MIT, MIT Imports: 12 Imported by: 0

README

MDNS GoDoc

MDNS is a simple mdns client/server library by Hashicorp.

We maintain a fork with updates for PRs and issues they have not merged or addressed.

Overview

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.

Usage

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

package main

import (
	"github.com/micro/mdns"
	"os"
)

func main() {

	// Setup our service export
	host, _ := os.Hostname()
	info := []string{"My awesome service"}
	service, _ := mdns.NewMDNSService(host, "_foobar._tcp", "", "", 8000, nil, info)

	// 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:

package main

import (
	"fmt"
	"github.com/micro/mdns"
)

func main() {

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

	// Start the lookup
	err := mdns.Lookup("_foobar._tcp", entriesCh)
	if err != nil {
		fmt.Println(err)
	}

	close(entriesCh)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(entries chan<- *ServiceEntry, exit chan struct{}) error

Listen listens indefinitely for multicast updates

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

	// Port If it is not 0, replace the port 5353 with this port number.
	Port int
}

Config is used to configure the mDNS server

type DNSSDService

type DNSSDService struct {
	MDNSService *MDNSService
}

DNSSDService is a service that complies with the DNS-SD (RFC 6762) and MDNS (RFC 6762) specs for local, multicast-DNS-based discovery.

DNSSDService implements the Zone interface and wraps an MDNSService instance. To deploy an mDNS service that is compliant with DNS-SD, it's recommended to register only the wrapped instance with the server.

Example usage:

    service := &mdns.DNSSDService{
      MDNSService: &mdns.MDNSService{
	       Instance: "My Foobar Service",
	       Service: "_foobar._tcp",
	       Port:    8000,
       }
     }
     server, err := mdns.NewServer(&mdns.Config{Zone: service})
     if err != nil {
       log.Fatalf("Error creating server: %v", err)
     }
     defer server.Shutdown()

func (*DNSSDService) Records

func (s *DNSSDService) Records(q dns.Question) []dns.RR

Records returns DNS records in response to a DNS question.

This function returns the DNS response of the underlying MDNSService instance. It also returns a PTR record for a request for " _services._dns-sd._udp.<Domain>", as described in section 9 of RFC 6763 ("Service Type Enumeration"), to allow browsing of the underlying MDNSService instance.

type MDNSService

type MDNSService struct {
	Instance string   // Instance name (e.g. "hostService name")
	Service  string   // Service name (e.g. "_http._tcp.")
	Domain   string   // If blank, assumes "local"
	HostName string   // Host machine DNS name (e.g. "mymachine.net.")
	Port     int      // Service Port
	IPs      []net.IP // IP addresses for the service's host
	TXT      []string // Service TXT records
	TTL      uint32
	// contains filtered or unexported fields
}

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

func NewMDNSService

func NewMDNSService(instance, service, domain, hostName string, port int, ips []net.IP, txt []string) (*MDNSService, error)

NewMDNSService returns a new instance of MDNSService.

If domain, hostName, or ips is set to the zero value, then a default value will be inferred from the operating system.

TODO(reddaly): This interface may need to change to account for "unique record" conflict rules of the mDNS protocol. Upon startup, the server should check to ensure that the instance name does not conflict with other instance names, and, if required, select a new name. There may also be conflicting hostName A/AAAA records.

func (*MDNSService) Records

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

Records returns DNS records in response to a DNS question.

type QueryParam

type QueryParam struct {
	Service             string               // Service to lookup
	Domain              string               // Lookup domain, default "local"
	Context             context.Context      // Context
	Timeout             time.Duration        // Lookup timeout, default 1 second. Ignored if Context is provided
	Interface           *net.Interface       // Multicast interface to use
	Entries             chan<- *ServiceEntry // Entries Channel
	WantUnicastResponse bool                 // Unicast response desired, as per 5.4 in RFC
}

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) SendMulticast

func (s *Server) SendMulticast(msg *dns.Msg) error

multicastResponse us used to send a multicast response packet

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
	InfoFields []string
	TTL        int

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

ServiceEntry is returned after we query for a service

type Zone

type Zone interface {
	// Records returns DNS records in response to a DNS question.
	Records(q dns.Question) []dns.RR
}

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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