net

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 26 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 (
	NetworkNAT      = "nat"
	NetworkShared   = "shared"
	NetworkHost     = "host"
	NetworkIsolated = "isolated"
	NetworkNone     = "none"
	NetworkUser     = "user" // user-mode networking in the VM daemon (gvisor-tap-vsock)
)

Network mode constants.

View Source
const (
	UserSubnet    = "192.168.127.0/24"
	UserGatewayIP = "192.168.127.1"   // DHCP and DNS server
	UserGuestIP   = "192.168.127.2"   // static lease for the VM's MAC
	UserHostIP    = "192.168.127.254" // NATed to the Mac's 127.0.0.1

	// UserHostName and UserGatewayName resolve to UserHostIP through the
	// gateway's DNS server, in the guest and in its containers and pods, as
	// with Docker Desktop.
	UserHostName    = "host.docker.internal"
	UserGatewayName = "gateway.docker.internal"
)

User-mode network addressing. Every VM in user mode gets its own private copy of this network, so the addresses are the same for all of them.

Variables

ValidNetworkModes is the set of accepted --network-mode values.

Functions

func CleanNetworkPid

func CleanNetworkPid(name string)

CleanNetworkPid removes the PID file.

func IsNetworkDaemonAlive

func IsNetworkDaemonAlive(name string) bool

IsNetworkDaemonAlive checks if the daemon for the given network is running.

func LookupIPByMAC

func LookupIPByMAC(mac string) string

lookupIPByMAC reads the macOS DHCP server leases file to find the IP address assigned to the given MAC address. This is more reliable than the ARP table, which only contains entries for recent IP traffic.

The leases file at /var/db/dhcpd_leases has entries like:

{
	ip_address=192.168.64.14
	hw_address=1,ce:b7:d1:c3:89:1d
	identifier=1,ce:b7:d1:c3:89:1d
	lease=0x69db5aec
}

The hw_address is prefixed with "1," (ethernet type) and uses shortened octets (e.g. "e:3d:be" instead of "0e:3d:be"). We normalize both sides for comparison. LookupIPByMAC reads the macOS DHCP server leases file to find the IP address assigned to the given MAC address.

func LookupIPForMode

func LookupIPForMode(mode, mac string) string

LookupIPForMode picks the right IP discovery strategy based on network mode. - nat/shared: DHCP leases file (macOS DHCP server assigns IPs) - host: DHCP leases first (router assigns), ARP table fallback - isolated/none: no IP expected

func NetworkDir

func NetworkDir(name string) (string, error)

NetworkDir returns the path to the named network's directory.

func NetworkLogPath

func NetworkLogPath(name string) (string, error)

NetworkLogPath returns the path to the network daemon's log file.

func NetworkServiceName

func NetworkServiceName(name string) string

NetworkServiceName returns the launchd label and Mach service name used by the daemon for the named network.

func NetworksDir

func NetworksDir() (string, error)

NetworksDir returns the parent directory for all named networks.

func ReadNetworkPid

func ReadNetworkPid(name string) (int, error)

ReadNetworkPid reads the daemon PID for a named network. Returns 0 if the PID file doesn't exist.

func RunNetworkDaemon

func RunNetworkDaemon(name, mode string, verbose bool) error

RunNetworkDaemon is the main loop for the network daemon process. It is started by launchd (see StartNetworkDaemon), creates the vmnet network, publishes it on the network's Mach service with vmnet.ShareNetwork, and keeps it alive until it receives SIGTERM or SIGINT. VM daemons join the network with joinNamedNetwork.

func StartNetworkDaemon

func StartNetworkDaemon(name, mode string, verbose bool) error

StartNetworkDaemon loads the launchd job for the named network, which starts `monolith __netd <name> <mode>` from the current executable. Any stale job with the same label is unloaded first. It returns once launchd accepts the job; callers should wait for IsNetworkDaemonAlive.

func StopNetworkDaemon

func StopNetworkDaemon(name string) (bool, error)

StopNetworkDaemon unloads the launchd job for the named network, which sends the daemon SIGTERM, and waits for it to exit. It reports whether a running daemon was stopped.

func VmenetForMAC

func VmenetForMAC(mac string) string

VmenetForMAC finds the vmenet<N> interface associated with the given guest MAC address by parsing the bridge100 address cache from `ifconfig bridge100`.

The address cache section looks like:

Address cache:
    ce:b7:d1:c3:89:1d Vlan1 vmenet0 1116 flags=0<>

Returns "" if no matching interface is found.

func WriteNetworkConfig

func WriteNetworkConfig(name string, cfg NetworkConfig) error

WriteNetworkConfig writes a named network's configuration.

func WriteNetworkPid

func WriteNetworkPid(name string, pid int) error

WriteNetworkPid writes the daemon PID file.

Types

type NetStats

type NetStats struct {
	RxBytes int64
	TxBytes int64
	RxPkts  int64
	TxPkts  int64
}

NetStats holds per-VM network counters from the host's vmenet interface. RX/TX are from the guest's perspective (RX = bytes received by guest).

func NetworkStats

func NetworkStats(iface string) NetStats

NetworkStats returns per-VM network counters by querying the given vmenet interface via `netstat -I <iface> -b`.

Note: from the host's perspective, Ibytes = bytes received from guest (guest TX) and Obytes = bytes sent to guest (guest RX). We swap them so the returned values are from the guest's perspective.

netstat output:

Name  Mtu  Network  Address   Ipkts Ierrs  Ibytes   Opkts Oerrs  Obytes  Coll
vmenet0 1500 <Link#27> ba:23:...  9     0     1286    156     0     24693     0

type NetworkConfig

type NetworkConfig struct {
	Mode       string `json:"mode"`                  // "shared" or "isolated"
	Subnet     string `json:"subnet"`                // actual IPv4 subnet allocated by vmnet (e.g. "192.168.64.0/24"), filled after creation
	UserSubnet string `json:"user_subnet,omitempty"` // caller-requested IPv4 subnet (CIDR); empty means vmnet picks
}

NetworkConfig holds the persisted configuration for a named network.

func ReadNetworkConfig

func ReadNetworkConfig(name string) (NetworkConfig, error)

ReadNetworkConfig reads a named network's configuration.

type UserNetwork

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

UserNetwork is a user-mode virtual network for one VM, implemented by gvisor-tap-vsock inside the VM daemon. The guest's NIC is attached to one end of a datagram socket pair carrying raw Ethernet frames; a userspace TCP/IP stack on the other end terminates the guest's connections and re-opens them from the daemon process. Outbound traffic therefore looks like any other app's traffic to macOS, VPNs and endpoint filters, which is what lets it work where vmnet's NAT forwarding is blocked. The guest is not reachable from the Mac by IP; DialGuest connects to it instead.

func ConfigureNetwork

func ConfigureNetwork(vmConfig *vz.VirtualMachineConfiguration, mode, networkName, hostInterface, macAddr string, userHostAccess bool) (string, *UserNetwork, error)

ConfigureNetwork sets up the VM's network device based on the config. macAddr is the VM's persisted MAC; when empty a random locally administered one is generated. A stable MAC is required to restore a saved VM state and keeps the VM's DHCP address stable. It returns the MAC address string (even for "none" mode, for state tracking) and attaches the network device to the VM configuration when applicable. In "user" mode it also returns the running UserNetwork, which the caller must Close when the VM stops; userHostAccess is passed to NewUserNetwork.

func NewUserNetwork

func NewUserNetwork(mac string, hostAccess bool) (*UserNetwork, error)

NewUserNetwork creates the virtual network and starts serving it. mac is the VM's MAC address, which gets the static DHCP lease UserGuestIP. hostAccess maps UserHostIP (and host.docker.internal) to the Mac's 127.0.0.1; sandboxes leave it off so guests can't reach services that listen only on the Mac's loopback.

func (*UserNetwork) Close

func (u *UserNetwork) Close()

Close stops serving the network.

func (*UserNetwork) DialGuest

func (u *UserNetwork) DialGuest(ctx context.Context, port int) (net.Conn, error)

DialGuest opens a TCP connection to port on the guest through the virtual network, as if from the gateway.

func (*UserNetwork) ExposeUDP

func (u *UserNetwork) ExposeUDP(hostPort, guestPort int) error

ExposeUDP forwards UDP on 127.0.0.1:hostPort on the Mac to guestPort on the guest, using gvisor-tap-vsock's port forwarder (it tracks a session per client address). TCP goes through DialGuest instead.

func (*UserNetwork) UnexposeUDP

func (u *UserNetwork) UnexposeUDP(hostPort int) error

UnexposeUDP stops a forward started by ExposeUDP.

Jump to

Keyboard shortcuts

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