magicsock

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

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

Go to latest
Published: Sep 4, 2023 License: BSD-3-Clause Imports: 75 Imported by: 0

Documentation

Overview

Package magicsock implements a socket that can change its communication path while in use, actively searching for the best way to communicate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanPMTUD

func CanPMTUD() bool

Types

type Conn

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

A Conn routes UDP packets and actively manages a list of its endpoints.

func NewConn

func NewConn(opts Options) (*Conn, error)

NewConn creates a magic Conn listening on opts.Port. As the set of possible endpoints for a Conn changes, the callback opts.EndpointsFunc is called.

func (*Conn) Bind

func (c *Conn) Bind() conn.Bind

Bind returns the wireguard-go conn.Bind for c.

See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

Only the first close does anything. Any later closes return nil.

func (*Conn) DERPs

func (c *Conn) DERPs() int

DERPs reports the number of active DERP connections.

func (*Conn) DebugBreakDERPConns

func (c *Conn) DebugBreakDERPConns() error

DebugBreakDERPConns breaks all DERP connections for debug/testing reasons.

func (*Conn) DiscoPublicKey

func (c *Conn) DiscoPublicKey() key.DiscoPublic

DiscoPublicKey returns the discovery public key.

func (*Conn) GetEndpointChanges

func (c *Conn) GetEndpointChanges(peer tailcfg.NodeView) ([]EndpointChange, error)

GetEndpointChanges returns the most recent changes for a particular endpoint. The returned EndpointChange structs are for debug use only and there are no guarantees about order, size, or content.

func (*Conn) InstallCaptureHook

func (c *Conn) InstallCaptureHook(cb capture.Callback)

InstallCaptureHook installs a callback which is called to log debug information into the pcap stream. This function can be called with a nil argument to uninstall the capture hook.

func (*Conn) LastRecvActivityOfNodeKey

func (c *Conn) LastRecvActivityOfNodeKey(nk key.NodePublic) string

LastRecvActivityOfNodeKey describes the time we last got traffic from this endpoint (updated every ~10 seconds).

func (*Conn) LocalPort

func (c *Conn) LocalPort() uint16

LocalPort returns the current IPv4 listener's port number.

func (*Conn) ParseEndpoint

func (c *Conn) ParseEndpoint(nodeKeyStr string) (conn.Endpoint, error)

ParseEndpoint implements conn.Bind; it's called by WireGuard to connect to an endpoint.

See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.ParseEndpoint

func (*Conn) Ping

func (c *Conn) Ping(peer tailcfg.NodeView, res *ipnstate.PingResult, size int, cb func(*ipnstate.PingResult))

Ping handles a "tailscale ping" CLI query.

func (*Conn) ReSTUN

func (c *Conn) ReSTUN(why string)

ReSTUN triggers an address discovery. The provided why string is for debug logging only.

func (*Conn) Rebind

func (c *Conn) Rebind()

Rebind closes and re-binds the UDP sockets and resets the DERP connection. It should be followed by a call to ReSTUN.

func (*Conn) Send

func (c *Conn) Send(buffs [][]byte, ep conn.Endpoint) error

Send implements conn.Bind.

See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.Send

func (*Conn) ServeHTTPDebug

func (c *Conn) ServeHTTPDebug(w http.ResponseWriter, r *http.Request)

ServeHTTPDebug serves an HTML representation of the innards of c for debugging.

It's accessible either from tailscaled's debug port (at /debug/magicsock) or via peerapi to a peer that's owned by the same user (so they can e.g. inspect their phones).

func (*Conn) SetDERPMap

func (c *Conn) SetDERPMap(dm *tailcfg.DERPMap)

SetDERPMap controls which (if any) DERP servers are used. A nil value means to disable DERP; it's disabled by default.

func (*Conn) SetDebugLoggingEnabled

func (c *Conn) SetDebugLoggingEnabled(v bool)

SetDebugLoggingEnabled controls whether spammy debug logging is enabled.

Note that this is currently independent from the log levels, even though they're pretty correlated: debugging logs should be [v1] (or higher), but some non-debug logs may also still have a [vN] annotation. The [vN] level controls which gets shown in stderr. The dlogf method, on the other hand, controls which gets even printed or uploaded at any level.

func (*Conn) SetNetInfoCallback

func (c *Conn) SetNetInfoCallback(fn func(*tailcfg.NetInfo))

func (*Conn) SetNetworkMap

func (c *Conn) SetNetworkMap(nm *netmap.NetworkMap)

SetNetworkMap is called when the control client gets a new network map from the control server. It must always be non-nil.

It should not use the DERPMap field of NetworkMap; that's conditionally sent to SetDERPMap instead.

func (*Conn) SetNetworkUp

func (c *Conn) SetNetworkUp(up bool)

func (*Conn) SetPreferredPort

func (c *Conn) SetPreferredPort(port uint16)

SetPreferredPort sets the connection's preferred local port.

func (*Conn) SetPrivateKey

func (c *Conn) SetPrivateKey(privateKey key.NodePrivate) error

SetPrivateKey sets the connection's private key.

This is only used to be able prove our identity when connecting to DERP servers.

If the private key changes, any DERP connections are torn down & recreated when needed.

func (*Conn) SetStatistics

func (c *Conn) SetStatistics(stats *connstats.Statistics)

SetStatistics specifies a per-connection statistics aggregator. Nil may be specified to disable statistics gathering.

func (*Conn) UpdatePeers

func (c *Conn) UpdatePeers(newPeers map[key.NodePublic]struct{})

UpdatePeers is called when the set of WireGuard peers changes. It then removes any state for old peers.

The caller passes ownership of newPeers map to UpdatePeers.

func (*Conn) UpdateStatus

func (c *Conn) UpdateStatus(sb *ipnstate.StatusBuilder)

UpdateStatus implements the interface nede by ipnstate.StatusBuilder.

This method adds in the magicsock-specific information only. Most of the status is otherwise populated by LocalBackend.

type EndpointChange

type EndpointChange struct {
	When time.Time // when the change occurred
	What string    // what this change is
	From any       `json:",omitempty"` // information about the previous state
	To   any       `json:",omitempty"` // information about the new state
}

EndpointChange is a structure containing information about changes made to a particular endpoint. This is not a stable interface and could change at any time.

type Options

type Options struct {
	// Logf optionally provides a log function to use.
	// Must not be nil.
	Logf logger.Logf

	// Port is the port to listen on.
	// Zero means to pick one automatically.
	Port uint16

	// EndpointsFunc optionally provides a func to be called when
	// endpoints change. The called func does not own the slice.
	EndpointsFunc func([]tailcfg.Endpoint)

	// DERPActiveFunc optionally provides a func to be called when
	// a connection is made to a DERP server.
	DERPActiveFunc func()

	// IdleFunc optionally provides a func to return how long
	// it's been since a TUN packet was sent or received.
	IdleFunc func() time.Duration

	// TestOnlyPacketListener optionally specifies how to create PacketConns.
	// Only used by tests.
	TestOnlyPacketListener nettype.PacketListener

	// NoteRecvActivity, if provided, is a func for magicsock to call
	// whenever it receives a packet from a a peer if it's been more
	// than ~10 seconds since the last one. (10 seconds is somewhat
	// arbitrary; the sole user just doesn't need or want it called on
	// every packet, just every minute or two for WireGuard timeouts,
	// and 10 seconds seems like a good trade-off between often enough
	// and not too often.)
	// The provided func is likely to call back into
	// Conn.ParseEndpoint, which acquires Conn.mu. As such, you should
	// not hold Conn.mu while calling it.
	NoteRecvActivity func(key.NodePublic)

	// NetMon is the network monitor to use.
	// With one, the portmapper won't be used.
	NetMon *netmon.Monitor
}

Options contains options for Listen.

type RebindingUDPConn

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

RebindingUDPConn is a UDP socket that can be re-bound. Unix has no notion of re-binding a socket, so we swap it out for a new one.

func (*RebindingUDPConn) Close

func (c *RebindingUDPConn) Close() error

func (*RebindingUDPConn) LocalAddr

func (c *RebindingUDPConn) LocalAddr() *net.UDPAddr

func (*RebindingUDPConn) Port

func (c *RebindingUDPConn) Port() uint16

func (*RebindingUDPConn) ReadBatch

func (c *RebindingUDPConn) ReadBatch(msgs []ipv6.Message, flags int) (int, error)

ReadBatch reads messages from c into msgs. It returns the number of messages the caller should evaluate for nonzero len, as a zero len message may fall on either side of a nonzero.

func (*RebindingUDPConn) ReadFromUDPAddrPort

func (c *RebindingUDPConn) ReadFromUDPAddrPort(b []byte) (int, netip.AddrPort, error)

ReadFromUDPAddrPort reads a packet from c into b. It returns the number of bytes copied and the source address.

func (*RebindingUDPConn) WriteBatchTo

func (c *RebindingUDPConn) WriteBatchTo(buffs [][]byte, addr netip.AddrPort) error

WriteBatchTo writes buffs to addr.

func (*RebindingUDPConn) WriteToUDPAddrPort

func (c *RebindingUDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error)

Jump to

Keyboard shortcuts

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