network

package
v0.0.0-...-1f64b99 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Version is the version of the IP protocol
	Version = 4

	// IHL is the IPv4 header length in 32-bit words.
	IHL = HeaderLength / 4

	// HeaderLength is the IPv4 header length.
	HeaderLength = 20

	// MTU (maximum transmission unit) is the maximum number of bytes that are
	// allowed on the payload of a datagram (the network layer name for a packet).
	MTU = link.MTU - HeaderLength
)

Variables

This section is empty.

Functions

func DeserializeDatagram

func DeserializeDatagram(buf []byte) (*gplayers.IPv4, error)

func Internet

func Internet() *net.IPNet

Internet is the the CIDR block 0.0.0.0/0.

func LoopbackIPAddress

func LoopbackIPAddress() net.IP

LoopbackIPAddress is the IP address used for loopback in a host.

func LoopbackIPEndpoint

func LoopbackIPEndpoint() gopacket.Endpoint

LoopbackIPEndpoint is the IP address used for loopback in a host.

func SerializeDatagram

func SerializeDatagram(datagram *gplayers.IPv4) ([]byte, error)

func SerializeDatagramWithTransportSegment

func SerializeDatagramWithTransportSegment(datagramHeader *gplayers.IPv4, segment gopacket.TransportLayer) ([]byte, error)

Types

type ARPTable

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

ARPTable represents an ARP table, which maps IP addresses to MAC addresses in the local network. All the public methods are thread-safe.

func (*ARPTable) FindRoute

func (a *ARPTable) FindRoute(ipAddress gopacket.Endpoint) (gopacket.Endpoint, bool)

func (*ARPTable) StoreRoute

func (a *ARPTable) StoreRoute(ipAddress net.IP, macAddress net.HardwareAddr)

type ForwardingTable

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

ForwardingTable is a trie which is able to find a route for an IP datagram (an interface name to go out from) with an O(32) scan in the worst case. The dst IP address bits are used to index the trie, starting from the most significant bit. The scan only stops when the next trie node doesn't have the next bit, hence returning the most specific known route. All the public methods are thread-safe.

func NewForwardingTable

func NewForwardingTable(routes []RouteConfig) (*ForwardingTable, error)

NewForwardingTable is a convenience constructor for a ForwardingTable. Directly instantiating the public struct is also valid.

func (*ForwardingTable) Clear

func (f *ForwardingTable) Clear()

func (*ForwardingTable) DeleteRoute

func (f *ForwardingTable) DeleteRoute(network *net.IPNet)

func (*ForwardingTable) FindRoute

func (f *ForwardingTable) FindRoute(ipAddress net.IP) (intf string, ok bool)

func (*ForwardingTable) StoreRoute

func (f *ForwardingTable) StoreRoute(network *net.IPNet, intf string)

func (*ForwardingTable) StoreRoutesFromConfig

func (f *ForwardingTable) StoreRoutesFromConfig(routes []RouteConfig) error

StoreRoutesFromConfig atomically stores a list of routes if all of them are valid.

type IPProtocol

type IPProtocol interface {
	GetID() gplayers.IPProtocol
	// Recv is the method for a registered IPProtocol to consume datagrams
	// that were received by the network layer. For performance reasons,
	// it will be invoked from multiple threads, hence the implementation
	// must be thread-safe.
	Recv(datagram *gplayers.IPv4)
}

IPProtocol represents a protocol that uses the Internet Protocol for its transport, e.g. TCP and UDP.

type Interface

type Interface interface {
	Send(ctx context.Context, datagram *gplayers.IPv4) error
	// SendTransportSegment is a hack for the TCP/UDP checksums to be
	// computed by gopacket during serialization.
	SendTransportSegment(
		ctx context.Context,
		datagramHeader *gplayers.IPv4,
		segment gopacket.TransportLayer,
	) error
	Recv() <-chan *gplayers.IPv4
	Close() error
	ForwardingMode() bool
	Name() string
	IPAddress() gopacket.Endpoint
	Gateway() gopacket.Endpoint
	Network() *net.IPNet
	BroadcastIPAddress() gopacket.Endpoint
	Card() link.EthernetPort
}

Interface represents a hypothetical network interface, composed by an ethernet card, an IP address, a gateway IP address and a network CIDR block.

When sending a datagram out the interface sets the src IP address of the datagram to its own IP address, unless if running on "forwarding mode".

Inbound datagrams with dst IP address not matching the interface's IP address will be discarded, unless if running on "forwarding mode" or if the dst IP address is the broadcast IP address of the network.

The interface uses ARP queries to resolve dst IP addresses inside the network. When the dst IP address is outside the network, an ARP query is sent to resolve the MAC address of the gateway instead.

func NewInterface

func NewInterface(ctx context.Context, conf InterfaceConfig) (Interface, error)

NewInterface creates an Interface from config.

type InterfaceConfig

type InterfaceConfig struct {
	// ForwardingMode keeps inbound datagrams with wrong dst IP address.
	ForwardingMode bool   `yaml:"forwardingMode"`
	Name           string `yaml:"name"`
	IPAddress      string `yaml:"ipAddress"`
	Gateway        string `yaml:"gateway"`
	NetworkCIDR    string `yaml:"networkCIDR"`
	MetricLabels   struct {
		StackName string `yaml:"stackName"`
	} `yaml:"metricLabels"`

	Card link.EthernetPortConfig `yaml:"ethernetPort"`
}

InterfaceConfig contains the configs for the concrete implementation of Interface.

type Layer

type Layer interface {
	Send(ctx context.Context, datagram *gplayers.IPv4) error
	FindInterfaceForHeader(datagramHeader *gplayers.IPv4) (Interface, error)
	ForwardingMode() bool
	ForwardingTable() *ForwardingTable
	Interfaces() []Interface
	Interface(name string) Interface
	RegisterIPProtocol(protocol IPProtocol)
	DeregisterIPProtocol(protocolID gplayers.IPProtocol) bool
	GetRegisteredIPProtocol(protocolID gplayers.IPProtocol) (IPProtocol, bool)
	Close() error
	StackName() string
}

Layer represents the network layer of a device, composed by a forwarding table and a set of network interfaces. It provides the means to send and receive IP datagrams to and from the IP networks attached to the underlying interfaces.

When sending a datagram out, the network layer indexes the forwarding table with the dst IP address to choose the interface. The datagram goes out with the IP address of the interface as its src IP address, unless if running on "forwarding mode" and the datagram came in from one of the other interfaces. The src IP address of the datagram may be specified in order to choose the network interface to go out from. When doing so, the IP address must match the IP address of one of the interfaces, otherwise an error will be returned.

A consumer of IP datagrams (like the transport layer) may consume by registering protocol handlers (see the IPProtocol interface).

func NewLayer

func NewLayer(ctx context.Context, conf LayerConfig) (Layer, error)

NewLayer creates Layer from config.

func NewLayerFromConfigFile

func NewLayerFromConfigFile(ctx context.Context, file string) (Layer, error)

type LayerConfig

type LayerConfig struct {
	// ForwardingMode keeps inbound datagrams with wrong dst IP address.
	ForwardingMode bool `yaml:"forwardingMode"`
	MetricLabels   struct {
		StackName string `yaml:"stackName"`
	} `yaml:"metricLabels"`

	Interfaces            []InterfaceConfig `yaml:"interfaces"`
	DefaultRouteInterface string            `yaml:"defaultRouteInterface"`
}

LayerConfig contains the configs for the concrete implementation of Layer.

type RouteConfig

type RouteConfig struct {
	NetworkCIDR string `yaml:"networkCIDR"`
	Interface   string `yaml:"interface"`
}

RouteConfig represents a route: a network CIDR mapping to an interface name.

type TCPIPSegment

type TCPIPSegment interface {
	gopacket.TransportLayer
	gopacket.SerializableLayer
	SetNetworkLayerForChecksum(l gopacket.NetworkLayer) error
}

Jump to

Keyboard shortcuts

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