discov

package
v0.0.0-...-9adf2cc Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

This package deals with UDP broadcasting to automatically discover peers and connect to them. If you see references to the "Discovery" protocol, it's this package.

Background

The Pikonet discovery protocol is a simple protocol operating on port 28779.

All messages have a 4 byte "PIKO" header, a one byte command type, and then a payload of arbitrary length. Messages should be sent to the appropriate broadcast address.

The protocol was created to allow efficient automatic configuration on private networks without telling the outside world. It allows communication across the same network as without it, it would be impossible to communicate with another device on the same network ordinarily.

It was not created for security, and it could lead to denial of service; these problems *may* be solved in the future but not necessarily will be solved.

Index

Constants

View Source
const (

	// 0x01 - Hello
	// Broadcasts your existance on the network.
	//
	// Payload:
	// - uint16: Listening port for WireGuard
	// - [44]byte: Base64 WireGuard public key.
	Hello = 0x01

	// 0x02 - Hello Reply
	// Broadcasts your existance on the network.
	//
	// The payload of this is the same as 0x01 Hello; this type explicitly
	// marks this message as a reply to an earlier Hello message.
	// Send these as a reply to 0x01, and not in any other case.
	//
	// Payload:
	// - uint16: Listening port for WireGuard
	// - [44]byte: Base64 WireGuard public key.
	HelloReply = 0x02
)

Discovery message commands.

View Source
const (
	// The port that Discovery communicates on.
	Port = 28779
)

Variables

View Source
var (
	// Multicast IP for Discovery communication.
	//
	// Previously this was 239.112.110.100, but due to Windows being
	// Windows, it was changed to piggyback off of the SSDP address.
	//
	// To anyne who's reading this: please let me know if you have a fix
	// that I can do easily and programmatically that would let me
	// uncomment this line:
	//   IP = net.IPv4(239, 112, 110, 100)
	IP = net.IPv4(239, 255, 255, 250)

	// IP and Port combined into a UDPAddr.
	Address = &net.UDPAddr{
		IP:   IP,
		Port: Port,
	}
)

Functions

func NewHello

func NewHello(port uint16, key string, reply bool) []byte

Types

type Discovery

type Discovery struct {

	// Ready is a channel that is closed when messages are ready to be sent
	// or received.
	Ready chan struct{}

	// Notify is a function that is called upon receiving a Discovery
	// message.
	Notify func(addr *net.UDPAddr, m Message)
	// contains filtered or unexported fields
}

Discovery holds state for local peer discovery.

func (*Discovery) Listen

func (d *Discovery) Listen(ctx context.Context) error

Listen listens for discovery messages on the local network.

The error returned will always be non-nil.

func (*Discovery) Send

func (d *Discovery) Send(msg []byte)

Send sends a message to the multicast group.

type Message

type Message struct {
	Type byte

	Port uint16
	Key  string
}

Message holds a decoded message and all associated data with it.

func Parse

func Parse(buf []byte) Message

Parse parses a message.

The "PIKO" header is not checked for its existance or validity.

Jump to

Keyboard shortcuts

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