mdns

package module
v0.0.0-...-5570bdb Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: MIT Imports: 9 Imported by: 0

README

example


package main

import (
	"fmt"
	"github.com/cc14514/mdns"
	"os"
	"time"
)

var tag = "_foobar._tcp"

func run() {
	// Setup our service export
	host, _ := os.Hostname()
	n := time.Now().Unix()
	selfid := fmt.Sprintf("myid:%d", n)
	fmt.Println("self.id", selfid)
	service, _ := mdns.NewMDNSService(host, tag, "", "", 8000, nil, []string{selfid})
	// Create the mDNS server, defer shutdown
	server, _ := mdns.NewServer(&mdns.Config{Zone: service})
	defer server.Shutdown()
	ticker := time.NewTicker(time.Second * 5)

	for {
		//execute mdns query right away at method call and then with every tick
		entriesCh := make(chan *mdns.ServiceEntry, 16)
		go func() {
			for entry := range entriesCh {
				if entry != nil && entry.Info != selfid {
					fmt.Println(" 🏠-- 👬 -->", entry.Info, entry.InfoFields)
				}
			}
		}()

		fmt.Println("starting mdns query")
		qp := &mdns.QueryParam{
			Domain:  "local",
			Entries: entriesCh,
			Service: tag,
			Timeout: time.Second * 5,
		}

		mdns.Query(qp)
		close(entriesCh)
		fmt.Println("mdns query complete")

		select {
		case <-ticker.C:
			continue
		}
	}
}

func main() {
	go run()
	c := make(chan int)
	<-c
}

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
}

Config is used to configure the mDNS server

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
	// 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"
	Timeout             time.Duration        // Lookup timeout, default 1 second
	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) 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

	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

Jump to

Keyboard shortcuts

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