tun

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 31 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

Index

Constants

View Source
const (
	MaxMessageSize = math.MaxUint16
)

Variables

View Source
var (
	ErrTun        = errors.New("tun device error")
	ErrInvalidNet = errors.New("invalid net IP")
)
View Source
var ErrNoRoute = errors.New("tun: no route")

ErrNoRoute is what deliver reports when a destination names no peer, or a peer with no stream behind it. It is a routing miss and nothing worse: the packet is dropped, the hub keeps reading the device.

View Source
var ErrRegistrationRefused = errors.New("tun: registration refused")

ErrRegistrationRefused reports a spoke registration the hub's policy rejected — the authorizer, the passphrase, or the self-loop guard. It is returned — never echoed — so the stream is torn down: the spoke redials and re-registers (a fixed hub recovers on its own) instead of sitting "running" with zero traffic, and the failure surfaces in its counters. Nothing is answered, so a refused peer learns nothing except that the stream died. Malformed frames and bare echoes are not this: they never reach policy, and killing a stream over transport noise would turn one stray datagram into a reconnect storm.

Functions

func NewHandler

func NewHandler(opts ...handler.Option) handler.Handler

func NewP2PHandler added in v0.28.1

func NewP2PHandler(device net.Conn, authorizer PeerAuthorizer, opts ...handler.Option) handler.Handler

NewP2PHandler builds a hub over device — the tun conn the listener produced — for the peers that reach it over p2p. authorizer decides which addresses a peer's registration may claim, and may be nil when a peer is to be able to claim whatever it asks for.

There is no auther here, because a p2p peer has nothing to authenticate: the allowlist that routed its stream here already said who it is, and onKeepalive consults an auther with the *claimed address* as the user name — so a credential check could only ever ask "is this peer allowed the address it just claimed", which is the question authorizer answers, and which an operator could otherwise satisfy only by typing each spoke's own IP into the tunnel's username field.

The hub's device reader starts here and runs until Close, so a packet off the device is being routed before the first stream arrives.

Types

type PeerAuthorizer added in v0.28.1

type PeerAuthorizer interface {
	Authorize(ctx context.Context, peer string, ips []net.IP) bool
}

PeerAuthorizer decides whether the peer that sent a registration may claim the addresses in it.

It exists because an auth.Authenticator cannot answer the question: onKeepalive passes the claimed address as the user name and the frame's key as the password, so an auther never learns which peer is claiming and cannot bind a claim to one. The peer key is already authenticated by the transport — a p2p hub's allowlist is what routed the stream here — so this is authorization, not authentication: it decides which addresses a known peer may hold.

peer is the transport's name for the sender, verbatim: a "host:port" for a socket peer, a p2p peer key for a p2p one. ips is every address the frame claimed, and an implementation should require the whole set rather than a subset: peerTable.set is last-writer-wins per address, so a peer that also claims a neighbour's address takes that route over.

type PrefixRoute added in v0.28.1

type PrefixRoute struct {
	Peer  string
	Allow []string
}

PrefixRoute is a LAN route the hub holds: the member that reaches the prefix, and the members allowed to use it (empty = everyone).

It is exported because the hub that installs these routes lives in another module: the tun p2p handler is handed to it as a handler.Handler, so it names the map it passes by this type. Nothing else in x or outside it constructs one.

Jump to

Keyboard shortcuts

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