netns

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 1,075

README

netns - network namespaces in go

The netns package provides an ultra-simple interface for handling network namespaces in go. Changing namespaces requires elevated privileges, so in most cases this code needs to be run as root.

Local Build and Test

You can use go get command:

go get github.com/vishvananda/netns

Testing (requires root):

sudo -E go test github.com/vishvananda/netns

Example

package main

import (
    "fmt"
    "net"
    "runtime"

    "github.com/vishvananda/netns"
)

func main() {
    // Lock the OS Thread so we don't accidentally switch namespaces
    runtime.LockOSThread()
    defer runtime.UnlockOSThread()

    // Save the current network namespace
    origns, _ := netns.Get()
    defer origns.Close()

    // Create a new network namespace
    newns, _ := netns.New()
    defer newns.Close()

    // Do something with the network namespace
    ifaces, _ := net.Interfaces()
    fmt.Printf("Interfaces: %v\n", ifaces)

    // Switch back to the original namespace
    netns.Set(origns)
}

Documentation

Overview

Package netns allows ultra-simple network namespace handling. NsHandles can be retrieved and set. Note that the current namespace is thread local so actions that set and reset namespaces should use LockOSThread to make sure the namespace doesn't change due to a goroutine switch. It is best to close NsHandles when you are done with them. This can be accomplished via a `defer ns.Close()` on the handle. Changing namespaces requires elevated privileges, so in most cases this code needs to be run as root.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = errors.New("not implemented")

Functions

func DeleteNamed

func DeleteNamed(name string) error

func Set

func Set(ns NsHandle) error

func Setns deprecated

func Setns(ns NsHandle, nstype int) error

Setns sets namespace using golang.org/x/sys/unix.Setns on Linux. It is not implemented on other platforms.

Deprecated: Use golang.org/x/sys/unix.Setns instead.

Types

type NsHandle

type NsHandle int

NsHandle is a handle to a network namespace. It can only be used on Linux, but provides stub methods on other platforms.

func Get

func Get() (NsHandle, error)

func GetFromDocker

func GetFromDocker(id string) (NsHandle, error)

func GetFromName

func GetFromName(name string) (NsHandle, error)

func GetFromPath

func GetFromPath(path string) (NsHandle, error)

func GetFromPid

func GetFromPid(pid int) (NsHandle, error)

func GetFromThread

func GetFromThread(pid int, tid int) (NsHandle, error)

func New

func New() (NsHandle, error)

func NewNamed

func NewNamed(name string) (NsHandle, error)

func None

func None() NsHandle

None gets an empty (closed) NsHandle.

func (*NsHandle) Close

func (ns *NsHandle) Close() error

Close closes the NsHandle and resets its file descriptor to -1. It is only implemented on Linux.

func (NsHandle) Equal

func (ns NsHandle) Equal(_ NsHandle) bool

Equal determines if two network handles refer to the same network namespace. It is only implemented on Linux.

func (NsHandle) IsOpen

func (ns NsHandle) IsOpen() bool

IsOpen returns true if Close() has not been called. It is only implemented on Linux and always returns false on other platforms.

func (NsHandle) String

func (ns NsHandle) String() string

String shows the file descriptor number and its dev and inode. It is only implemented on Linux, and returns "NS(none)" on other platforms.

func (NsHandle) UniqueId

func (ns NsHandle) UniqueId() string

UniqueId returns a string which uniquely identifies the namespace associated with the network handle. It is only implemented on Linux, and returns "NS(none)" on other platforms.

Jump to

Keyboard shortcuts

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