mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-03 23:22:23 -08:00
Add Network.ListenPacket, making Network a nettype.PacketListener with in-memory UDP sockets. Packets go by exact destination address and port. Sending never blocks; packets to an address without a socket, or to a full receive queue, are dropped. An unspecified or empty listen IP binds the family's loopback address, and port zero picks the next ephemeral port in rotation, so a just-closed socket's port isn't handed right back out, as with an OS. Reads block only on channels and timers, including for read deadlines, so the sockets work in testing/synctest bubbles. That's for running magicsock (via its TestOnlyPacketListener) and STUN (via stuntest.ServeWithPacketListener) on an in-memory network with fake time. Updates #22831 Change-Id: Id7043412ca036b075927c6352c663d73f2b8657f Signed-off-by: Brad Fitzpatrick <[email protected]>
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package memnet implements an in-memory network implementation.
|
|
// It is useful for dialing and listening on in-memory addresses
|
|
// in tests and other situations where you don't want to use the
|
|
// network.
|
|
package memnet
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
|
|
"tailscale.com/net/netx"
|
|
"tailscale.com/syncs"
|
|
)
|
|
|
|
var _ netx.Network = (*Network)(nil)
|
|
|
|
// Network implements [Network] using an in-memory network, usually
|
|
// used for testing.
|
|
//
|
|
// It supports TCP (Listen and Dial) and UDP (ListenPacket).
|
|
//
|
|
// Its zero value is a valid [netx.Network] implementation.
|
|
type Network struct {
|
|
mu syncs.Mutex
|
|
lns map[string]*Listener // address -> listener
|
|
pconns map[netip.AddrPort]*packetConn // UDP sockets by local address
|
|
|
|
nextUDPPort int // next ephemeral UDP port to try, minus firstEphemeralPort
|
|
}
|
|
|
|
func (m *Network) Listen(network, address string) (net.Listener, error) {
|
|
if network != "tcp" && network != "tcp4" && network != "tcp6" {
|
|
return nil, fmt.Errorf("memNetwork: Listen called with unsupported network %q", network)
|
|
}
|
|
ap, err := netip.ParseAddrPort(address)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("memNetwork: Listen called with invalid address %q: %w", address, err)
|
|
}
|
|
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if m.lns == nil {
|
|
m.lns = make(map[string]*Listener)
|
|
}
|
|
port := ap.Port()
|
|
for {
|
|
if port == 0 {
|
|
port = firstEphemeralPort
|
|
}
|
|
key := net.JoinHostPort(ap.Addr().String(), fmt.Sprint(port))
|
|
_, ok := m.lns[key]
|
|
if ok {
|
|
if ap.Port() != 0 {
|
|
return nil, fmt.Errorf("memNetwork: Listen called with duplicate address %q", address)
|
|
}
|
|
port++
|
|
continue
|
|
}
|
|
ln := Listen(key)
|
|
m.lns[key] = ln
|
|
ln.onClose = func() {
|
|
m.mu.Lock()
|
|
delete(m.lns, key)
|
|
m.mu.Unlock()
|
|
}
|
|
return ln, nil
|
|
}
|
|
}
|
|
|
|
func (m *Network) NewLocalTCPListener() net.Listener {
|
|
ln, err := m.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("memNetwork: failed to create local TCP listener: %v", err))
|
|
}
|
|
return ln
|
|
}
|
|
|
|
func (m *Network) Dial(ctx context.Context, network, address string) (net.Conn, error) {
|
|
if network != "tcp" && network != "tcp4" && network != "tcp6" {
|
|
return nil, fmt.Errorf("memNetwork: Dial called with unsupported network %q", network)
|
|
}
|
|
m.mu.Lock()
|
|
ln, ok := m.lns[address]
|
|
m.mu.Unlock()
|
|
if !ok {
|
|
return nil, fmt.Errorf("memNetwork: Dial called on unknown address %q", address)
|
|
}
|
|
return ln.Dial(ctx, network, address)
|
|
}
|