mdns

package
v0.0.0-...-bbc9ce3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2021 License: BSD-2-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// A is the DNS Type for ipv4
	A = 1
	// AAAA is the DNS Type for ipv6
	AAAA = 28
	// PTR is the DNS Type for domain name pointers
	PTR = 12
	// SRV is the DNS Type for services
	SRV = 33
	// IN is the Internet DNS Class
	IN = 1
)
View Source
const DefaultPort int = 5353

DefaultPort is the mDNS port required of the spec, though this library is port-agnostic.

Variables

This section is empty.

Functions

func IpToDnsRecordType

func IpToDnsRecordType(ip net.IP) uint16

IpToDnsRecordType returns either A or AAAA based on the type of ip.

func NewMDNS

func NewMDNS() *mDNS

NewMDNS creates a new object implementing the MDNS interface. Do not forget to call EnableIPv4() or EnableIPv6() to enable listening on interfaces of the corresponding type, or nothing will work.

Types

type Header struct {
	ID      uint16
	Flags   uint16
	QDCount uint16
	ANCount uint16
	NSCount uint16
	ARCount uint16
}

type MDNS

type MDNS interface {
	// EnableIPv4 enables listening on IPv4 network interfaces.
	EnableIPv4()

	// EnableIPv6 enables listening on IPv6 network interfaces.
	EnableIPv6()

	// SetAddress sets a non-default listen address.
	SetAddress(address string) error

	// SetMCastTTL sets the multicast time to live. If this is set to less
	// than zero it stays at the default. If it is set to zero this will mean
	// no packets can escape the host.
	//
	// Must be no greater than 255.
	SetMCastTTL(ttl int) error

	// AddHandler calls f on every Packet received.
	AddHandler(f func(net.Interface, net.Addr, Packet))

	// AddWarningHandler calls f on every non-fatal error.
	AddWarningHandler(f func(net.Addr, error))

	// AddErrorHandler calls f on every fatal error. After
	// all active handlers are called, m will stop listening and
	// close it's connection so this function will not be called twice.
	AddErrorHandler(f func(error))

	// Start causes m to start listening for mDNS packets on all interfaces on
	// the specified port. Listening will stop if ctx is done.
	Start(ctx context.Context, port int) error

	// Send serializes and sends packet out as a multicast to all interfaces
	// using the port that m is listening on. Note that Start must be
	// called prior to making this call.
	Send(ctx context.Context, packet Packet) error

	// SendTo serializes and sends packet to dst. If dst is a multicast
	// address then packet is multicast to the corresponding group on
	// all interfaces. Note that start must be called prior to making this
	// call.
	SendTo(ctx context.Context, packet Packet, dst *net.UDPAddr) error

	// Close closes all connections.
	Close()
}

MDNS is the central interface through which requests are sent and received. This implementation is agnostic to use case and asynchronous. To handle various responses add Handlers. To send a packet you may use either SendTo (generally used for unicast) or Send (generally used for multicast).

type Packet

type Packet struct {
	Header     Header
	Questions  []Question
	Answers    []Record
	Authority  []Record
	Additional []Record
}

func AnswerPacket

func AnswerPacket(domain string, ip net.IP) Packet

AnswerPacket constructs and returns a packet that gives a response to the

func QuestionPacket

func QuestionPacket(domain string) Packet

QuestionPacket constructs and returns a packet that requests the ip address associated with domain.

type Question

type Question struct {
	Domain  string
	Type    uint16
	Class   uint16
	Unicast bool
}

type Record

type Record struct {
	Domain string
	Type   uint16
	Class  uint16
	Flush  bool
	TTL    uint32
	Data   []byte
}

Jump to

Keyboard shortcuts

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