natlab

package
v0.100.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package natlab lets us simulate different types of networks all in-memory without running VMs or requiring root, etc. Despite the name, it does more than just NATs. But NATs are the most interesting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

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

func (*Interface) Contains

func (f *Interface) Contains(ip netaddr.IP) bool

Contains reports whether f contains ip as an IP.

func (*Interface) String

func (f *Interface) String() string

func (*Interface) V4

func (f *Interface) V4() netaddr.IP

V4 returns the machine's first IPv4 address, or the zero value if none.

func (*Interface) V6

func (f *Interface) V6() netaddr.IP

V6 returns the machine's first IPv6 address, or the zero value if none.

type Machine

type Machine struct {
	// Name is a pretty name for debugging and packet tracing. It need
	// not be globally unique.
	Name string

	// HandlePacket, if not nil, is a function that gets invoked for
	// every packet this Machine receives. Returns a verdict for how
	// the packet should continue to be handled (or not).
	//
	// This can be used to implement things like stateful firewalls
	// and NAT boxes.
	HandlePacket PacketHandler
	// contains filtered or unexported fields
}

A Machine is a representation of an operating system's network stack. It has a network routing table and can have multiple attached networks. The zero value is valid, but lacks any networking capability until Attach is called.

func (*Machine) AddNetwork

func (m *Machine) AddNetwork(n *Network)

func (*Machine) Attach

func (m *Machine) Attach(interfaceName string, n *Network) *Interface

Attach adds an interface to a machine.

The first interface added to a Machine becomes that machine's default route.

func (*Machine) Inject

func (m *Machine) Inject(p []byte, dst, src netaddr.IPPort) error

Inject transmits p from src to dst, without the need for a local socket. It's useful for implementing e.g. NAT boxes that need to mangle IPs.

func (*Machine) ListenPacket

func (m *Machine) ListenPacket(network, address string) (net.PacketConn, error)

type Network

type Network struct {
	Name    string
	Prefix4 netaddr.IPPrefix
	Prefix6 netaddr.IPPrefix
	// contains filtered or unexported fields
}

func NewInternet

func NewInternet() *Network

NewInternet returns a network that simulates the internet.

func (*Network) SetDefaultGateway

func (n *Network) SetDefaultGateway(gw *Machine)

type PacketHandler

type PacketHandler func(p []byte, dst, src netaddr.IPPort) PacketVerdict

A PacketHandler is a function that can process packets.

type PacketVerdict

type PacketVerdict int

A PacketVerdict is a decision of what to do with a packet.

const (
	// Continue means the packet should be processed by the "local
	// sockets" logic of the Machine.
	Continue PacketVerdict = iota
	// Drop means the packet should not be handled further.
	Drop
)

func (PacketVerdict) String

func (v PacketVerdict) String() string

Jump to

Keyboard shortcuts

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