discovery

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package discovery finds Planck hosts on the LAN and the tailnet and picks the best path to one.

Hosts answer probes and broadcast beacons on one UDP port (DefaultPort). Every datagram is a small JSON object tagged with the protocol version.

Index

Constants

View Source
const (
	DefaultPort = 41000

	// SafeShardPayload is the media payload that fits a Tailscale 1280-byte
	// MTU after IPv6, UDP, and Planck's AEAD and media headers. LAN paths use
	// it too: spike 3 measured no gain from larger shards on wired LAN.
	SafeShardPayload = 1152
)
View Source
const DefaultHeadStart = 50 * time.Millisecond

DefaultHeadStart is how long each path kind probes alone before the next, worse kind starts.

View Source
const LANMaxBitrateKbps = 150_000

LANMaxBitrateKbps is the highest rate spike 3 sustained on wired LAN with 100% delivery (150 Mbps at 120 and 240 fps).

Variables

This section is empty.

Functions

func Announce

func Announce(ctx context.Context, conn PacketWriter, b Beacon, dst []netip.AddrPort, interval time.Duration) error

Announce sends the beacon to every address in dst now and then once per interval until ctx ends. Send errors on one address do not stop the others; interfaces come and go.

func AnnounceFunc

func AnnounceFunc(ctx context.Context, conn PacketWriter, beacon func() Beacon, dst []netip.AddrPort, interval time.Duration) error

AnnounceFunc is Announce with the beacon read before each round.

func BroadcastAddrs

func BroadcastAddrs(port uint16) []netip.AddrPort

BroadcastAddrs returns the directed broadcast address of every up IPv4 interface that supports broadcast. The limited broadcast 255.255.255.255 leaves through only one interface on macOS, which misses clients on the other NIC of a multi-homed host.

func Listen

func Listen(ctx context.Context, conn net.PacketConn, found func(Announcement)) error

Listen reports LAN beacons received on conn until ctx ends. Bind conn to DefaultPort on all interfaces to hear broadcasts.

func ProbeAll

func ProbeAll(ctx context.Context, dst []netip.AddrPort, found func(Announcement)) error

ProbeAll probes every address in dst at once, such as the LAN's BroadcastAddrs, and reports each reply until ctx ends: every host that answers, from the address it answered from. Each probe goes out three times, as broadcasts are easily lost on Wi-Fi.

func Serve

func Serve(ctx context.Context, conn net.PacketConn, b Beacon) error

Serve answers probes on conn with the host's beacon until ctx ends. Replies go to the probe's source address, so conn must not be connected.

func ServeFunc

func ServeFunc(ctx context.Context, conn net.PacketConn, beacon func() Beacon) error

ServeFunc is Serve with the beacon read at each reply, so a renamed host answers with its new name at once.

Types

type Announcement

type Announcement struct {
	Beacon Beacon
	Addr   netip.AddrPort
	Kind   PathKind
	RTT    time.Duration
}

Announcement is a host found by a beacon or a probe. Addr is the host's discovery address; the control address is Addr's IP with Beacon.ControlPort.

func ProbeTailscale

func ProbeTailscale(ctx context.Context, ts StatusSource, port uint16) ([]Announcement, error)

ProbeTailscale probes every online tailnet peer on port and returns the Planck hosts that answered before ctx ended, fastest first. Peers that do not answer are not errors; most tailnet devices are not Planck hosts, so ctx must carry a deadline.

func (Announcement) Candidate

func (a Announcement) Candidate() Candidate

type Beacon

type Beacon struct {
	HostID          string   `json:"hostID"`
	Name            string   `json:"name"`
	ControlPort     uint16   `json:"controlPort"`
	CertFingerprint string   `json:"certFingerprint"`
	OS              string   `json:"os"`
	GPUs            []string `json:"gpus,omitempty"`
	TailscaleIP     string   `json:"tailscaleIP,omitempty"`
}

Beacon is what a host says about itself.

func Probe

func Probe(ctx context.Context, addr netip.AddrPort) (Beacon, time.Duration, error)

Probe asks the host at addr for its beacon and measures the round trip. It resends every 100 ms until a reply arrives or ctx ends. Replies are matched by nonce, not source address: a multi-homed host may answer from another interface. RTT uses transport.Mono because Go's monotonic clock on Windows steps in about 0.5 ms, which reads a LAN round trip as zero.

type Candidate

type Candidate struct {
	HostID string
	Kind   PathKind
	Addr   netip.AddrPort
}

Candidate is one way to reach a host. HostID, when set, must match the beacon in the probe reply, so a stale address that now belongs to another host cannot win.

type PacketWriter

type PacketWriter interface {
	WriteTo(p []byte, addr net.Addr) (int, error)
}

PacketWriter is the sending half of a net.PacketConn, which a Port is too.

type PathKind

type PathKind uint8
const (
	PathLANDirect PathKind = iota
	PathTailscaleDirect
	PathTailscaleDERP
)

func (PathKind) RecommendedMaxBitrateKbps

func (k PathKind) RecommendedMaxBitrateKbps() int

RecommendedMaxBitrateKbps is the bitrate ceiling for a path kind.

func (PathKind) String

func (k PathKind) String() string

type Port

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

Port is a discovery socket shared by the hosts and the browsers of one process: OpenPort gives every caller of an address the same socket, so that an app hosting and browsing at once does not bind its port twice. One reader answers probes with each host's beacon (Serve) and hands beacons to each browser (Listen).

The socket lets other processes bind the port as well. macOS and Windows deliver a broadcast to every socket on the port but a unicast datagram to the oldest one alone, so a port with no host on it that hears a probe binds its socket anew, at most once per yieldEvery, leaving unicast probes to a host in another process.

func OpenPort

func OpenPort(addr string) (*Port, error)

OpenPort binds addr, such as ":41000", or joins the socket this process already has on it. Each OpenPort needs its own Close. An address on port 0 binds a socket of its own each time.

func (*Port) Close

func (p *Port) Close() error

Close lets go of the port; the last Close closes the socket and waits for its reader to end.

func (*Port) Listen

func (p *Port) Listen(ctx context.Context, found func(Announcement)) error

Listen reports the beacons the port hears until ctx ends.

func (*Port) LocalAddr

func (p *Port) LocalAddr() net.Addr

LocalAddr is the socket's address.

func (*Port) Serve

func (p *Port) Serve(ctx context.Context, beacon func() Beacon) error

Serve answers probes with beacon, read at each reply, until ctx ends.

func (*Port) WriteTo

func (p *Port) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo sends from the port's socket, as Announce does.

type Racer

type Racer struct {
	// HeadStart defaults to DefaultHeadStart.
	HeadStart time.Duration
	// Probe defaults to the package Probe function.
	Probe func(ctx context.Context, addr netip.AddrPort) (Beacon, time.Duration, error)
}

func (Racer) Race

func (r Racer) Race(ctx context.Context, candidates []Candidate) (Result, error)

Race probes candidates in tiers by kind: LAN direct first, Tailscale direct after one head start, Tailscale DERP after another. A tier starts early when every candidate started before it has failed. The first valid reply wins and the other probes are cancelled.

type Result

type Result struct {
	Candidate
	Beacon                    Beacon
	ControlAddr               netip.AddrPort
	RTT                       time.Duration
	SafeShardPayload          int
	RecommendedMaxBitrateKbps int
}

type StatusSource

type StatusSource interface {
	Status(ctx context.Context) (*tailscale.Status, error)
}

StatusSource is satisfied by *tailscale.Client.

Jump to

Keyboard shortcuts

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