vnet

package module
v0.0.0-...-63ed7f6 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

README

vnet - network namespace conpatible vnet in go

The vnet package provides an ultra-simple interface for handling vnet in go. Useing vnet jail 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/oss-fun/vnet

Testing (requires root):

sudo -E go test github.com/oss-fun/vnet -run Test***

Example

package main

import (
        "fmt"
        "net"

        "github.com/oss-fun/vnet"
)

func main() {
        // Create a new network namespace
        newvj, _ := vnet.New()

        // Do something with the network namespace
        curvj, _ := vnet.Get()
        if newvj.Equal(curvj) {
                fmt.Printf("OK!\n")
        }

        ifaces, _ := net.Interfaces()
        fmt.Printf("Interfaces: %v\n", ifaces)
}

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(vj VjHandle) error

Types

type VjHandle

type VjHandle int

func Get

func Get() (VjHandle, error)

func GetFromName

func GetFromName(name string) (VjHandle, error)

func GetFromPath

func GetFromPath(path string) (VjHandle, error)

func GetFromPid

func GetFromPid(pid int) (VjHandle, error)

func GetFromThread

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

func New

func New() (VjHandle, error)

func NewNamed

func NewNamed(name string) (VjHandle, error)

func None

func None() VjHandle

None gets an empty (closed) NsHandle.

func (*VjHandle) Close

func (vj *VjHandle) Close() error

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

func (VjHandle) Equal

func (vj VjHandle) Equal(_ VjHandle) bool

func (VjHandle) IsOpen

func (vj VjHandle) 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 (VjHandle) String

func (vj VjHandle) 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 (VjHandle) UniqueId

func (vj VjHandle) 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