gont

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: Apache-2.0 Imports: 38 Imported by: 3

Documentation

Index

Constants

View Source
const (
	WithQdiscNetem = (1 << iota)
	WithQdiscTbf   = (1 << iota)
)
View Source
const CGoPCAP = true

Variables

View Source
var (
	DefaultIPv4Mask = net.IPNet{
		IP:   net.IPv4zero,
		Mask: net.CIDRMask(0, net.IPv4len*8),
	}

	DefaultIPv6Mask = net.IPNet{
		IP:   net.IPv6zero,
		Mask: net.CIDRMask(0, net.IPv6len*8),
	}
)
View Source
var (
	IPv4loopback = net.IPv4(127, 0, 0, 1)
)
View Source
var (
	Names = []string{}/* 120 elements not displayed */

)

Functions

func CheckCaps

func CheckCaps() error

CheckCaps checks if the current process has the required privileges to run Gont

func Exec

func Exec(network, node string, args []string) error

func GenerateNetworkName

func GenerateNetworkName() string

func GetRandomName

func GetRandomName() string

func Identify

func Identify() (string, string, error)

Identify returns the network and node name if the current process is running in a network netspace created by Gont

func NetworkNames added in v0.2.0

func NetworkNames() []string

func NodeNames added in v0.2.0

func NodeNames(network string) []string

func TeardownAllNetworks added in v0.1.0

func TeardownAllNetworks() error

func TeardownNetwork added in v0.1.0

func TeardownNetwork(network string) error

func TeardownNode added in v1.4.0

func TeardownNode(network, node string) error

func TestConnectivity

func TestConnectivity(hosts ...*Host) error

TestConnectivity performs ICMP ping tests between all pairs of nodes in the network

Types

type BaseNode

type BaseNode struct {
	*Namespace

	BasePath string

	Interfaces []*Interface

	// Options
	ConfiguredInterfaces    []*Interface
	ExistingNamespace       string
	ExistingDockerContainer string
	LogToDebug              bool
	EmptyDirs               []string
	Env                     map[string]any
	// contains filtered or unexported fields
}

func (*BaseNode) AddDefaultRoute added in v0.2.0

func (n *BaseNode) AddDefaultRoute(gw net.IP) error

AddDefaultRoute adds a default route for this node by providing a default gateway.

func (*BaseNode) AddInterface added in v0.2.0

func (n *BaseNode) AddInterface(i *Interface)

AddInterface adds an interface to the list of configured interfaces

func (*BaseNode) AddRoute

func (n *BaseNode) AddRoute(r *nl.Route) error

AddRoute adds a route to the node.

func (*BaseNode) Close added in v1.2.0

func (n *BaseNode) Close() error

func (*BaseNode) Command

func (n *BaseNode) Command(name string, args ...string) *exec.Cmd

func (*BaseNode) CommandWith added in v1.5.0

func (n *BaseNode) CommandWith(name string, env []string, dir string, args ...string) *exec.Cmd

func (*BaseNode) ConfigureInterface added in v0.2.0

func (n *BaseNode) ConfigureInterface(i *Interface) error

func (*BaseNode) EnableForwarding

func (n *BaseNode) EnableForwarding() error

EnableForwarding enables forwarding for both IPv4 and IPv6 protocols in the kernel for all interfaces

func (*BaseNode) Interface added in v0.2.0

func (n *BaseNode) Interface(name string) *Interface

func (*BaseNode) Name

func (n *BaseNode) Name() string

func (*BaseNode) NetNSHandle added in v0.2.0

func (n *BaseNode) NetNSHandle() netns.NsHandle

func (*BaseNode) NetlinkHandle added in v0.2.0

func (n *BaseNode) NetlinkHandle() *nl.Handle

func (*BaseNode) Network

func (n *BaseNode) Network() *Network

Network returns the network to which this node belongs

func (*BaseNode) NftConn added in v0.3.0

func (n *BaseNode) NftConn() *nft.Conn

func (*BaseNode) Run

func (n *BaseNode) Run(cmd string, args ...any) ([]byte, *exec.Cmd, error)

func (*BaseNode) RunGo added in v0.2.0

func (n *BaseNode) RunGo(script string, args ...any) ([]byte, *exec.Cmd, error)

func (*BaseNode) RunWith added in v1.5.0

func (n *BaseNode) RunWith(cmd string, env []string, dir string, args ...any) ([]byte, *exec.Cmd, error)

func (*BaseNode) Start added in v0.1.0

func (n *BaseNode) Start(cmd string, args ...any) (io.Reader, io.Reader, *exec.Cmd, error)

func (*BaseNode) StartGo added in v0.2.0

func (n *BaseNode) StartGo(script string, args ...any) (io.Reader, io.Reader, *exec.Cmd, error)

func (*BaseNode) StartWith added in v1.5.0

func (n *BaseNode) StartWith(cmd string, env []string, dir string, args ...any) (io.Reader, io.Reader, *exec.Cmd, error)

func (*BaseNode) String added in v0.2.0

func (n *BaseNode) String() string

func (*BaseNode) Teardown

func (n *BaseNode) Teardown() error

func (*BaseNode) WriteProcFS

func (n *BaseNode) WriteProcFS(path, value string) error

WriteProcFS write a value to a path within the ProcFS by entering the namespace of this node.

type BaseNodeOption added in v1.5.0

type BaseNodeOption interface {
	Apply(b *BaseNode)
}

type BridgeOption added in v0.1.0

type BridgeOption interface {
	Apply(br *nl.Bridge)
}

type Callback

type Callback func() error

type Capture added in v1.2.0

type Capture struct {
	// Options
	CaptureLength int
	Promiscuous   bool
	Comment       string
	Timeout       time.Duration
	LogKeys       bool

	// Filter options
	FilterInterface    CaptureFilterInterfaceFunc
	FilterPackets      CaptureFilterPacketFunc
	FilterExpression   string
	FilterInstructions []bpf.Instruction

	// Output options
	File     *os.File
	Filename string
	Channel  chan CapturePacket
	Callback CaptureCallbackFunc
	// contains filtered or unexported fields
}

func NewCapture added in v1.2.0

func NewCapture() *Capture

func (*Capture) Apply added in v1.2.0

func (c *Capture) Apply(i *Interface)

func (*Capture) Close added in v1.2.0

func (c *Capture) Close() error

func (*Capture) Count added in v1.4.0

func (c *Capture) Count() uint64

Count returns the total number of captured packets

func (*Capture) Flush added in v1.2.0

func (c *Capture) Flush() error

func (*Capture) Reader added in v1.2.0

func (c *Capture) Reader() (*pcapgo.NgReader, error)

func (*Capture) Start added in v1.2.0

func (c *Capture) Start(i *Interface) error

func (*Capture) WriteDecryptionSecret added in v1.5.7

func (c *Capture) WriteDecryptionSecret(typ uint32, payload []byte) error

type CaptureCallbackFunc added in v1.4.0

type CaptureCallbackFunc func(pkt CapturePacket)

type CaptureFilterInterfaceFunc added in v1.2.0

type CaptureFilterInterfaceFunc func(i *Interface) bool

type CaptureFilterPacketFunc added in v1.4.0

type CaptureFilterPacketFunc func(p *CapturePacket) bool

type CaptureInterface added in v1.4.0

type CaptureInterface struct {
	*Interface

	PCAPInterfaceIndex int
	Handle             handle

	StartTime time.Time
	// contains filtered or unexported fields
}

type CaptureOption added in v1.2.0

type CaptureOption interface {
	Apply(n *Capture)
}

type CapturePacket added in v1.4.0

type CapturePacket struct {
	gopacket.Packet

	Interface *CaptureInterface
}

func (CapturePacket) Time added in v1.4.0

func (p CapturePacket) Time() time.Time

type CaptureStats added in v1.4.0

type CaptureStats struct {
	PacketsReceived int
	PacketsDropped  int
}

type DeviceGroup

type DeviceGroup int
const (
	DeviceGroupDefault    DeviceGroup = 0
	DeviceGroupSouthBound DeviceGroup = 1000 + iota
	DeviceGroupNorthBound
)

type Filter added in v0.3.0

type Filter struct {
	Family nft.TableFamily
	Table  *nft.Table

	Input   *nft.Chain
	Output  *nft.Chain
	Forward *nft.Chain
	// contains filtered or unexported fields
}

func NewFilter added in v0.3.0

func NewFilter(c *nft.Conn) (*Filter, error)

func (*Filter) AddRule added in v0.3.0

func (f *Filter) AddRule(h FilterHook, exprs ...expr.Any)

func (*Filter) Flush added in v0.3.0

func (f *Filter) Flush() error

type FilterHook added in v0.3.0

type FilterHook int
const (
	FilterInput FilterHook = iota
	FilterOutput
	FilterForward
)

type FilterRule added in v0.3.0

type FilterRule struct {
	Exprs []expr.Any

	Hook FilterHook
}

func (FilterRule) Apply added in v0.3.0

func (fr FilterRule) Apply(h *Host)

type Host

type Host struct {
	*BaseNode

	Filter *Filter

	// Options
	FilterRules []*FilterRule
	Routes      []*nl.Route
}

func HostNode

func HostNode(n *Network) *Host

func (*Host) Apply

func (h *Host) Apply(i *Interface)

Options

func (*Host) ConfigureInterface

func (h *Host) ConfigureInterface(i *Interface) error
func (h *Host) ConfigureLinks() error

ConfigureLinks adds links to other nodes which have been configured by functional options

func (*Host) LookupAddress added in v0.1.0

func (h *Host) LookupAddress(n string) *net.IPAddr

func (*Host) Ping

func (h *Host) Ping(o *Host) (*ping.Statistics, error)

func (*Host) PingWithNetwork added in v0.1.0

func (h *Host) PingWithNetwork(o *Host, net string) (*ping.Statistics, error)

func (*Host) PingWithOptions added in v0.1.0

func (h *Host) PingWithOptions(o *Host, net string, count int, timeout time.Duration, intv time.Duration, output bool) (*ping.Statistics, error)

func (*Host) Traceroute

func (h *Host) Traceroute(o *Host, opts ...any) error

type HostOption

type HostOption interface {
	Apply(h *Host)
}

type Interface

type Interface struct {
	Name string
	Node Node

	Link nl.Link

	Flags int

	// Options
	Netem     nl.NetemQdiscAttrs
	Tbf       nl.Tbf
	EnableDAD bool
	LinkAttrs nl.LinkAttrs
	Addresses []net.IPNet
	Captures  []*Capture
}

func (*Interface) AddAddress added in v1.6.2

func (i *Interface) AddAddress(a *net.IPNet) error

func (*Interface) Apply

func (i *Interface) Apply(n *BaseNode)

func (*Interface) DeleteAddress added in v1.6.2

func (i *Interface) DeleteAddress(a *net.IPNet) error

func (Interface) IsLoopback added in v0.2.0

func (i Interface) IsLoopback() bool

func (*Interface) SetDown added in v1.6.2

func (i *Interface) SetDown() error

func (*Interface) SetUp added in v1.6.2

func (i *Interface) SetUp() error

func (Interface) String

func (i Interface) String() string

type InterfaceOption

type InterfaceOption interface {
	Apply(n *Interface)
}

type LinkOption

type LinkOption interface {
	Apply(a *nl.LinkAttrs)
}

type NAT

type NAT struct {
	*Router

	Table       *nft.Table
	Input       *nft.Chain
	Forward     *nft.Chain
	PostRouting *nft.Chain

	// Options
	Persistent    bool
	Random        bool
	FullyRandom   bool
	SourcePortMin int
	SourcePortMax int
}

func (*NAT) Apply added in v0.2.0

func (n *NAT) Apply(i *Interface)

type NATOption

type NATOption interface {
	Apply(n *NAT)
}

type Namespace

type Namespace struct {
	netns.NsHandle

	Name string
	// contains filtered or unexported fields
}

func NewNamespace

func NewNamespace(name string) (*Namespace, error)

func (*Namespace) Close

func (ns *Namespace) Close() error

func (*Namespace) Enter added in v0.1.0

func (ns *Namespace) Enter() (func(), error)

func (*Namespace) RunFunc

func (ns *Namespace) RunFunc(cb Callback) error

type Network

type Network struct {
	Name string

	HostNode *Host
	VarPath  string
	TmpPath  string

	// Options
	Persistent bool
	NSPrefix   string
	Captures   []*Capture
	// contains filtered or unexported fields
}

func NewNetwork

func NewNetwork(name string, opts ...Option) (*Network, error)

func (*Network) AddHost

func (n *Network) AddHost(name string, opts ...Option) (*Host, error)

func (*Network) AddHostNAT

func (n *Network) AddHostNAT(name string, opts ...Option) (*NAT, error)
func (n *Network) AddLink(l, r *Interface, opts ...Option) error

func (*Network) AddNAT

func (n *Network) AddNAT(name string, opts ...Option) (*NAT, error)

func (*Network) AddNode

func (n *Network) AddNode(name string, opts ...Option) (*BaseNode, error)

func (*Network) AddRouter

func (n *Network) AddRouter(name string, opts ...Option) (*Router, error)

func (*Network) AddSwitch

func (n *Network) AddSwitch(name string, opts ...Option) (*Switch, error)

AddSwitch adds a new Linux virtual bridge in a dedicated namespace

func (*Network) Close

func (n *Network) Close() error

func (*Network) GenerateConfigFiles added in v0.2.0

func (n *Network) GenerateConfigFiles() error

func (*Network) GenerateHostsFile added in v0.2.0

func (n *Network) GenerateHostsFile() error

GenerateHostsFile writes the addresses and host names of all nodes into a file located at /run/gont/<network>/files/etc/hosts

Processes started via BaseNode.Run or BaseNode.Start, will see this file bind mounted at /etc/hosts

func (*Network) GenerateIProute2Files added in v0.2.0

func (n *Network) GenerateIProute2Files() error

func (*Network) Hosts added in v0.1.0

func (n *Network) Hosts() []*Host

func (*Network) KeyLogPipe added in v1.6.0

func (n *Network) KeyLogPipe(secretsType uint32) (*os.File, error)

func (*Network) Nodes

func (n *Network) Nodes() []Node

func (*Network) Register added in v0.2.0

func (n *Network) Register(m Node)

func (*Network) Routers added in v0.1.0

func (n *Network) Routers() []*Router

func (*Network) String added in v0.2.0

func (n *Network) String() string

func (*Network) Switches added in v0.1.0

func (n *Network) Switches() []*Switch

func (*Network) Teardown

func (n *Network) Teardown() error

func (*Network) WriteHostsFile added in v1.5.8

func (n *Network) WriteHostsFile(f io.Writer) error

type NetworkOption

type NetworkOption interface {
	Apply(n *Network)
}

type Node

type Node interface {
	Close() error
	Teardown() error

	// Getters
	Name() string
	String() string
	Network() *Network
	Interface(name string) *Interface
	NetNSHandle() netns.NsHandle
	NetlinkHandle() *nl.Handle

	RunFunc(cb Callback) error

	ConfigureInterface(i *Interface) error
}

type Option

type Option any

Option is the base type for all functional options.

type Router

type Router struct {
	*Host
}

func (*Router) Apply added in v0.2.0

func (h *Router) Apply(i *Interface)

type RouterOption added in v1.5.0

type RouterOption interface {
	Apply(r *Router)
}

type Switch

type Switch struct {
	*BaseNode
}

Switch is an abstraction for a Linux virtual bridge

func (*Switch) Apply

func (sw *Switch) Apply(i *Interface)

func (*Switch) ConfigureInterface added in v0.2.0

func (sw *Switch) ConfigureInterface(i *Interface) error

ConfigureInterface attaches an existing interface to a bridge interface

type SwitchOption

type SwitchOption interface {
	Apply(sw *Switch)
}

type VethOption

type VethOption interface {
	Apply(ve *nl.Veth)
}

Directories

Path Synopsis
Package options contains all the general options for configuring the general objects like hosts, switches, routers and interfaces
Package options contains all the general options for configuring the general objects like hosts, switches, routers and interfaces
capture
Package capture contains the options to configure to packet capturing
Package capture contains the options to configure to packet capturing
filters
Package filters contains the options for configuring NFTables filtering
Package filters contains the options for configuring NFTables filtering
tc
Package tc contains the options for configuring per-interface Traffic Control (TC) queuing disciplines
Package tc contains the options for configuring per-interface Traffic Control (TC) queuing disciplines

Jump to

Keyboard shortcuts

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