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
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) CreateBind(uint16) (conn.Bind, uint16, error)
- func (c *Conn) CreateEndpoint(pubKey [32]byte, addrs string) (conn.Endpoint, error)
- func (c *Conn) DERPs() int
- func (c *Conn) DiscoPublicKey() tailcfg.DiscoKey
- func (c *Conn) LastMark() uint32
- func (c *Conn) LastRecvActivityOfDisco(dk tailcfg.DiscoKey) time.Time
- func (c *Conn) LocalPort() uint16
- func (c *Conn) PeerForIP(ip netaddr.IP) (n *tailcfg.Node, ok bool)
- func (c *Conn) PeerHasDiscoKey(k tailcfg.NodeKey) bool
- func (c *Conn) Ping(ip netaddr.IP, cb func(*ipnstate.PingResult))
- func (c *Conn) ReSTUN(why string)
- func (c *Conn) Rebind()
- func (c *Conn) ReceiveIPv4(b []byte) (n int, ep conn.Endpoint, err error)
- func (c *Conn) ReceiveIPv6(b []byte) (int, conn.Endpoint, error)
- func (c *Conn) Send(b []byte, ep conn.Endpoint) error
- func (c *Conn) SetDERPMap(dm *tailcfg.DERPMap)
- func (c *Conn) SetMark(value uint32) error
- func (c *Conn) SetNetInfoCallback(fn func(*tailcfg.NetInfo))
- func (c *Conn) SetNetworkMap(nm *controlclient.NetworkMap)
- func (c *Conn) SetNetworkUp(up bool)
- func (c *Conn) SetPrivateKey(privateKey wgkey.Private) error
- func (c *Conn) Start()
- func (c *Conn) UpdatePeers(newPeers map[key.Public]struct{})
- func (c *Conn) UpdateStatus(sb *ipnstate.StatusBuilder)
- func (c *Conn) WhoIs(ip netaddr.IP) (n *tailcfg.Node, u tailcfg.UserProfile, ok bool)
- type Options
- type RebindingUDPConn
- func (c *RebindingUDPConn) Close() error
- func (c *RebindingUDPConn) LocalAddr() *net.UDPAddr
- func (c *RebindingUDPConn) ReadFrom(b []byte) (int, net.Addr, error)
- func (c *RebindingUDPConn) Reset(pconn net.PacketConn)
- func (c *RebindingUDPConn) SetReadDeadline(t time.Time)
- func (c *RebindingUDPConn) WriteTo(b []byte, addr net.Addr) (int, error)
- func (c *RebindingUDPConn) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
Constants ¶
const DefaultPort = 0
DefaultPort is the default port to listen on. The current default (zero) means to auto-select a random free port.
const DerpMagicIP = "127.3.3.40"
DerpMagicIP is a fake WireGuard endpoint IP address that means to use DERP. When used, the port number of the WireGuard endpoint is the DERP server number to use.
Mnemonic: 3.3.40 are numbers above the keys D, E, R, P.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
A Conn routes UDP packets and actively manages a list of its endpoints. It implements wireguard/conn.Bind.
func NewConn ¶ added in v0.98.1
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.
It doesn't start doing anything until Start is called.
func (*Conn) Close ¶
Close closes the connection.
Only the first close does anything. Any later closes return nil.
func (*Conn) CreateBind ¶
CreateBind is called by WireGuard to create a UDP binding.
func (*Conn) CreateEndpoint ¶
func (*Conn) DiscoPublicKey ¶ added in v1.0.0
DiscoPublicKey returns the discovery public key.
func (*Conn) LastRecvActivityOfDisco ¶ added in v1.4.0
LastRecvActivityOfDisco returns the time we last got traffic from this endpoint (updated every ~10 seconds).
func (*Conn) PeerHasDiscoKey ¶ added in v1.0.0
PeerHasDiscoKey reports whether peer k supports discovery keys (client version 0.100.0+).
func (*Conn) Ping ¶ added in v1.2.0
func (c *Conn) Ping(ip netaddr.IP, cb func(*ipnstate.PingResult))
Ping handles a "tailscale ping" CLI query.
func (*Conn) ReSTUN ¶
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. It should be followed by a call to ReSTUN.
func (*Conn) ReceiveIPv4 ¶
ReceiveIPv4 is called by wireguard-go to receive an IPv4 packet. In Tailscale's case, that packet might also arrive via DERP. A DERP packet arrival aborts the pconn4 read deadline to make it fail.
func (*Conn) SetDERPMap ¶ added in v0.98.1
SetDERPMap controls which (if any) DERP servers are used. A nil value means to disable DERP; it's disabled by default.
func (*Conn) SetNetInfoCallback ¶
func (*Conn) SetNetworkMap ¶ added in v0.100.0
func (c *Conn) SetNetworkMap(nm *controlclient.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 ¶ added in v1.2.0
func (*Conn) SetPrivateKey ¶
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) UpdatePeers ¶ added in v0.98.0
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 ¶ added in v0.98.0
func (c *Conn) UpdateStatus(sb *ipnstate.StatusBuilder)
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(endpoint []string) // 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 // PacketListener optionally specifies how to create PacketConns. // It's meant for testing. PacketListener nettype.PacketListener // NoteRecvActivity, if provided, is a func for magicsock to // call whenever it receives a packet from a a // discovery-capable 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 called // while holding userspaceEngine.wgLock and likely calls // Conn.CreateEndpoint, which acquires Conn.mu. As such, you // should not hold Conn.mu while calling it. NoteRecvActivity func(tailcfg.DiscoKey) // SimulatedNetwork can be set true in tests to signal that // the network is simulated and thus it's okay to bind on the // unspecified address (which we'd normally avoid to avoid // triggering macOS and Windows firwall dialog boxes during // "go test"). SimulatedNetwork bool // DisableLegacyNetworking disables legacy peer handling. When // enabled, only active discovery-aware nodes will be able to // communicate with Conn. DisableLegacyNetworking bool }
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) Reset ¶
func (c *RebindingUDPConn) Reset(pconn net.PacketConn)
func (*RebindingUDPConn) SetReadDeadline ¶
func (c *RebindingUDPConn) SetReadDeadline(t time.Time)