vnet

package
v0.14.6 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package vnet provides a virtual (as opposed to mocked) implementation of the abstracted UDP networking stack. Multiple virtual hosts can be created with network linkages between them to simulate packet flows.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseInterface

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

BaseInterface handles the common elements of both physical and tunnel Interfaces

func (*BaseInterface) AddAddr

func (i *BaseInterface) AddAddr(a net.IPNet)

AddAddr adds an IP address to the interface on which it can receive packets and from which it can send them

func (*BaseInterface) AddSocket

func (i *BaseInterface) AddSocket(a *net.UDPAddr) *Socket

AddSocket creates a new socket on the interface

func (*BaseInterface) Addrs

func (i *BaseInterface) Addrs() []net.IPNet

Addrs fetches a list of the currently assigned addresses on the interface

func (*BaseInterface) DelSocket

func (i *BaseInterface) DelSocket(s *Socket)

DelSocket unregisters a socket from the interface

func (*BaseInterface) InboundPacket

func (i *BaseInterface) InboundPacket(p *Packet) bool

InboundPacket inspects the packet to see if its destination matches any address on the interface and any listening socket, and if so enqueues it for that listener

func (*BaseInterface) Name

func (i *BaseInterface) Name() string

Name gets the host-local name of the interface

type Host

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

A Host represents a system in a World with some set (possibly empty) of Interfaces, which may be connected to Networks to send and receive packets.

func (*Host) AddPhy

func (h *Host) AddPhy(name string) *PhysicalInterface

AddPhy adds a new interface to the Host with the given name, assigning it an id combining the host id with the name to ensure uniqueness

func (*Host) AddSocket

func (h *Host) AddSocket(a *net.UDPAddr) *Socket

AddSocket creates a new socket on the interface

func (*Host) AddTun

func (h *Host) AddTun(name string) *Tunnel

AddTun creates a new tunnel interface on the host, but does not connect it to any peers, nor open a listen port for it to receive packets

func (*Host) Close

func (h *Host) Close()

Close disconnects all Interfaces from the network, but does not Close Sockets

func (*Host) DelInterface

func (h *Host) DelInterface(name string) Interface

DelInterface unregisters an interface from the host and detaches it from any network

func (*Host) DelSocket

func (h *Host) DelSocket(s *Socket)

DelSocket unregisters a socket from the interface

func (*Host) InboundPacket

func (h *Host) InboundPacket(p *Packet) bool

InboundPacket inspects the packet to see if its destination matches any host-level listening socket, and if so enqueues it for that listener

func (*Host) Interface

func (h *Host) Interface(name string) Interface

Interface fetches the given interface by name

func (*Host) Name

func (h *Host) Name() string

Name gets the Host's Name, AKA id

func (*Host) OutboundPacket

func (h *Host) OutboundPacket(p *Packet) bool

OutboundPacket tries to send a packet on each interface registered on the host

func (*Host) Wrap

func (h *Host) Wrap() networking.Environment

Wrap provides an Environment view of a Host

type Interface

type Interface interface {
	SocketOwner
	Name() string
	DetachFromNetwork()
	Wrap() networking.Interface
	Addrs() []net.IPNet
}

An Interface is any network interface, whether physical or virtual, attached to a Host, which can be used to send and receive Packets.

type Network

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

A Network represents a connected region within which packets can pass among Interfaces

func (*Network) EnqueuePacket

func (n *Network) EnqueuePacket(p *Packet) bool

EnqueuePacket enqueues a packet to deliver to the network

type Packet

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

A Packet represents a UDP packet traveling on the virtual network

type PhysicalInterface

type PhysicalInterface struct {
	BaseInterface
	// contains filtered or unexported fields
}

An PhysicalInterface represents a network interface on a Host that is part of some World. The interface may be attached and detached from various Networks in the World.

func (*PhysicalInterface) AttachToNetwork

func (i *PhysicalInterface) AttachToNetwork(n *Network)

AttachToNetwork connects this interface to a given network, allowing it to send packets to other hosts on the network

func (*PhysicalInterface) DetachFromNetwork

func (i *PhysicalInterface) DetachFromNetwork()

DetachFromNetwork disconnects the interface from its network, if any.

func (*PhysicalInterface) OutboundPacket

func (i *PhysicalInterface) OutboundPacket(p *Packet) bool

OutboundPacket enqueues the packet to be sent out the interface into the network, if possible

func (*PhysicalInterface) Wrap

Wrap implements Interface

type Socket

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

A Socket represents a listening UDP socket which can send and receive Packets

func (*Socket) Close

func (s *Socket) Close()

Close shuts down a socket

func (*Socket) Connect

func (s *Socket) Connect() networking.UDPConn

Connect creates a SocketUDPConn wrapper for the Socket to treat it as a networking.UDPConn.

func (*Socket) InboundPacket

func (s *Socket) InboundPacket(p *Packet) bool

InboundPacket enqueues a packet for the receive listener to process

func (*Socket) OutboundPacket

func (s *Socket) OutboundPacket(p *Packet) bool

OutboundPacket sends a packet out the socket's interface

type SocketOwner

type SocketOwner interface {
	OutboundPacket(*Packet) bool
	AddSocket(a *net.UDPAddr) *Socket
	DelSocket(*Socket)
}

A SocketOwner can send packets

type TunPeer

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

TunPeer represents a simplified version of a wireguard peer, with a remote endpoint and a list of IPNets within the tunnel

func (*TunPeer) Addrs

func (p *TunPeer) Addrs() []net.IPNet

Addrs gets a copy of the currently configured addrs

func (*TunPeer) Endpoint

func (p *TunPeer) Endpoint() *net.UDPAddr

Endpoint returns the current endpoint of the peer

func (*TunPeer) LastReceive

func (p *TunPeer) LastReceive() time.Time

LastReceive gets the time of the last received packet, or zero if never

type Tunnel

type Tunnel struct {
	BaseInterface
	// contains filtered or unexported fields
}

A Tunnel represents a Wireguard interface, which encapsulates packets and sends them via some other interface

func (*Tunnel) AddPeer

func (t *Tunnel) AddPeer(name string, publicKey wgtypes.Key, endpoint *net.UDPAddr, addrs []net.IPNet) *TunPeer

AddPeer defines a new valid peer for communicating over the tunnel

func (*Tunnel) AsWgDevice

func (t *Tunnel) AsWgDevice() *wgtypes.Device

AsWgDevice creates a view of the current tunnel state as a wireguard wgtypes.Device

func (*Tunnel) DelPeer

func (t *Tunnel) DelPeer(id string)

DelPeer deletes the peer with the given id (String() of its PublicKey) from the tunnel

func (*Tunnel) DetachFromNetwork

func (t *Tunnel) DetachFromNetwork()

DetachFromNetwork implements Interface

func (*Tunnel) GenerateKeys

func (t *Tunnel) GenerateKeys() (privateKey, publicKey wgtypes.Key)

GenerateKeys makes a new key pair for the tunnel and uses it, panicing if generation fails

func (*Tunnel) Keys

func (t *Tunnel) Keys() (privateKey, publicKey wgtypes.Key)

Keys returns the local private and public keys for the tunnel

func (*Tunnel) Listen

func (t *Tunnel) Listen(port int)

Listen tells the tunnel to open a listening socket on the host on which it can receive encapsulated packets

func (*Tunnel) OutboundPacket

func (t *Tunnel) OutboundPacket(p *Packet) bool

OutboundPacket implements Interface

func (*Tunnel) Peers

func (t *Tunnel) Peers() map[string]*TunPeer

Peers gets a view of the peers map

func (*Tunnel) PublicKey

func (t *Tunnel) PublicKey() wgtypes.Key

PublicKey returns the tunnel's public key

func (*Tunnel) UseKey

func (t *Tunnel) UseKey(privateKey wgtypes.Key)

UseKey loads the given private key into the tunnel along with its computed public key

func (*Tunnel) Wrap

func (t *Tunnel) Wrap() networking.Interface

Wrap implements Interface

type World

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

A World represents a global set of networks, hosts, and their interfaces.

func NewWorld

func NewWorld() *World

NewWorld initializes a new empty world to which hosts and networks can be added.

func (*World) CreateEmptyHost

func (w *World) CreateEmptyHost(id string) *Host

CreateEmptyHost creates a new Host within the world, with no interfaces

func (*World) CreateHost

func (w *World) CreateHost(id string) *Host

CreateHost creates a simple host within the world, with its 'lo' (localhost) interface pre-configured

func (*World) CreateNetwork

func (w *World) CreateNetwork(id string) *Network

CreateNetwork creates and attaches a new Network with the given id to the world

Jump to

Keyboard shortcuts

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