netns

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package netns contains Linux network namespace-related functionality.

Package netns creates Linux network namespaces and runs code, opens sockets and dials inside them. Entering a namespace is a property of the OS thread, so everything here locks a thread for as long as it is in one and restores the caller's namespace before returning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowUnprivilegedPorts

func AllowUnprivilegedPorts() error

AllowUnprivilegedPorts lets this namespace bind ports below 1024 without CAP_NET_BIND_SERVICE, which is how atunnel answers a sandbox's DNS on 53. The sysctl is per-namespace and grants nothing outside it.

func Dialer

func Dialer(ns Handle) func(context.Context, string, string) (net.Conn, error)

Dialer dials TCP or UDP IP literals in ns, pinning a thread only until the socket is created.

func Do

func Do(ctx context.Context, targetNS Handle, do func(context.Context) error) error

Do runs do() with the OS thread switched into targetNS, then restores it.

func Listen

func Listen(ctx context.Context, ns Handle, ports []uint16) (_ []net.Listener, retErr error)

Listen opens wildcard TCP listeners inside ns. Sockets retain their namespace and can be served from another namespace.

func RemoveNamed

func RemoveNamed(name string) error

RemoveNamed unmounts and unlinks a name under /run/netns, without following it if it is a symlink. A name that is already gone is not an error, and the namespace itself survives for as long as something holds it open.

Types

type Handle

type Handle = vishnetns.NsHandle

Handle is a descriptor for a network namespace. Callers name it through this package so that nothing else has to import the one underneath.

func CreateNamed

func CreateNamed(name string) (Handle, error)

CreateNamed creates a named netns and returns its handle, restoring the caller's current netns before returning.

The caller owns the name exclusively, so a name still present when this runs was left behind by an earlier incarnation and is removed first. The kernel creates the name with O_EXCL, so without that removal a single failed teardown would wedge the name for good: nothing could ever create it again. Removal only unmounts and unlinks the name. Anything still holding the namespace keeps it alive, and existing handles stay usable.

func GetFromName

func GetFromName(name string) (Handle, error)

GetFromName opens the namespace of that name under /run/netns.

Jump to

Keyboard shortcuts

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