edurouter

package module
v0.0.0-...-190bf64 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 16 Imported by: 0

README

edurouter

a demo of the edurouter rendered with vhs-terminal

edurouter is an educational implementation of a router. It works by simulating additional IP addresses on network interfaces managed by the Linux kernel.

The conceptual overview of the edurouter implementation

Documentation

Index

Constants

View Source
const (
	HardwareAddrLen             = 6
	InterfaceConfigFormatString = "interfaceName:IPv4/Mask"
)
View Source
const (
	DefaultIPv4Version = 4
	IPv4IHL            = 5
	DefaultIPv4TTL     = 64
	IPv4HeaderLength   = 20
)

Variables

View Source
var (
	HandledPdu                = errors.New("this pdu is processed. this is intended behaviour")
	ErrDropPdu                = errors.New("no action for given PDU found. dropping it")
	ErrNoRoute                = errors.New("no route found")
	ErrNoLinkLayerHandler     = errors.New("no link layer handler for given etherType found")
	ErrUnsupportedArpProtocol = errors.New("unsupported ARP Version. requires ethernet+IPv4")

	ErrNotAnMACHardwareAddress = errors.New("provided hardware address was no MAC address")

	ErrNotAnIPv4Address       = errors.New("ip address it not an IPv4 address")
	ErrNoInternetLayerHandler = errors.New("no internet layer handler for given IPProtocol found")
	ErrARPTimeout             = errors.New("ARP timeout. no MAC found for this IP Address")
	ErrARPPacketConn          = errors.New("outbound PacketConn was nil")

	ErrNotANetworkAddress                 = errors.New("not a correct network address")
	ErrNextHopNotOnLinkLocalNetwork       = errors.New("next hop is not on local network of the outbound interface")
	ErrLinkLocalRouteShouldNotHaveNextHop = errors.New("a link-local route should not have a next hop address defined")

	ErrInvalidInterfaceConfigString = errors.New("invalid interface config string. malformed input, should have following format: '" + InterfaceConfigFormatString + "'")
)
View Source
var EmptyHardwareAddr = []byte{
	0x0, 0x0, 0x0,
	0x0, 0x0, 0x0,
}

Functions

func Version

func Version() string

Types

type ARPOperation

type ARPOperation uint16
const (
	ARPOperationRequest  ARPOperation = 1
	ARPOperationResponse ARPOperation = 2
	HTYPEEthernet                     = 1
)

type ARPWriter

type ARPWriter interface {
	SendArpRequest(ip net.IP) error
}

type ARPv4LinkLayerHandler

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

func NewARPv4LinkLayerHandler

func NewARPv4LinkLayerHandler(publishCh chan<- *ethernet.Frame) *ARPv4LinkLayerHandler

func (*ARPv4LinkLayerHandler) RunHandler

func (llh *ARPv4LinkLayerHandler) RunHandler(ctx context.Context)

func (*ARPv4LinkLayerHandler) SupplierC

func (llh *ARPv4LinkLayerHandler) SupplierC() chan<- FrameIn

type ARPv4Pdu

type ARPv4Pdu struct {
	HTYPE           uint16
	PTYPE           ethernet.EtherType
	HLEN            uint8
	PLEN            uint8
	Operation       ARPOperation
	SrcHardwareAddr []byte
	SrcProtoAddr    []byte
	DstHardwareAddr []byte
	DstProtoAddr    []byte
}

func (*ARPv4Pdu) BuildARPResponseWithConfig

func (a *ARPv4Pdu) BuildARPResponseWithConfig(config *InterfaceConfig) *ARPv4Pdu

func (*ARPv4Pdu) IsArpRequestForConfig

func (a *ARPv4Pdu) IsArpRequestForConfig(config *InterfaceConfig) bool

func (*ARPv4Pdu) IsArpResponse

func (a *ARPv4Pdu) IsArpResponse() bool

func (*ARPv4Pdu) IsEthernetAndIPv4

func (a *ARPv4Pdu) IsEthernetAndIPv4() bool

func (*ARPv4Pdu) MarshalBinary

func (a *ARPv4Pdu) MarshalBinary() ([]byte, error)

func (*ARPv4Pdu) UnmarshalBinary

func (a *ARPv4Pdu) UnmarshalBinary(payload []byte) error

type ARPv4Table

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

func NewARPv4Table

func NewARPv4Table(ifconfig *InterfaceConfig, arpWriter ARPWriter) *ARPv4Table

func (*ARPv4Table) Resolve

func (a *ARPv4Table) Resolve(ipAddr net.IP) ([]byte, error)

func (*ARPv4Table) Store

func (a *ARPv4Table) Store(ipAddr, macAddr []byte) error

type ARPv4Writer

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

func NewARPv4Writer

func NewARPv4Writer(ifconfig *InterfaceConfig) *ARPv4Writer

func (*ARPv4Writer) Initialize

func (a *ARPv4Writer) Initialize(c net.PacketConn)

func (*ARPv4Writer) SendArpRequest

func (a *ARPv4Writer) SendArpRequest(ip net.IP) error

type FrameIn

type FrameIn struct {
	Frame     *ethernet.Frame
	Interface *InterfaceConfig
}

type FrameOut

type FrameOut struct {
	Frame     *IPv4Pdu
	RouteInfo *RouteInfo
}

type ICMPPacket

type ICMPPacket struct {
	IcmpType IcmpType
	IcmpCode uint8
	Checksum uint16
	Id       uint16
	Seq      uint16
	Data     []byte
}

func (*ICMPPacket) MakeResponse

func (icmp *ICMPPacket) MakeResponse()

func (*ICMPPacket) MarshalBinary

func (icmp *ICMPPacket) MarshalBinary() ([]byte, error)

func (*ICMPPacket) UnmarshalBinary

func (icmp *ICMPPacket) UnmarshalBinary(data []byte) error

type IPProtocol

type IPProtocol uint8
const (
	IPProtocolICMPv4 IPProtocol = 1
	IPProtocolIPv4   IPProtocol = 4
	IPProtocolTCP    IPProtocol = 6
	IPProtocolUDP    IPProtocol = 17
)

type IPv4LinkLayerInputHandler

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

func NewIPv4LinkLayerInputHandler

func NewIPv4LinkLayerInputHandler(publishCh chan<- *InternetV4PacketIn) *IPv4LinkLayerInputHandler

func (*IPv4LinkLayerInputHandler) RunHandler

func (llh *IPv4LinkLayerInputHandler) RunHandler(ctx context.Context)

func (*IPv4LinkLayerInputHandler) SupplierC

func (llh *IPv4LinkLayerInputHandler) SupplierC() chan<- FrameIn

type IPv4LinkLayerOutputHandler

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

func NewIPv4LinkLayerOutputHandler

func NewIPv4LinkLayerOutputHandler(publishCh chan<- *ethernet.Frame) *IPv4LinkLayerOutputHandler

func (*IPv4LinkLayerOutputHandler) RunHandler

func (h *IPv4LinkLayerOutputHandler) RunHandler(ctx context.Context)

func (*IPv4LinkLayerOutputHandler) SupplierC

func (h *IPv4LinkLayerOutputHandler) SupplierC() chan *InternetV4PacketOut

type IPv4Pdu

type IPv4Pdu struct {
	Version        uint8
	TOS            uint8
	TotalLength    uint16
	Id             uint16
	Flags          byte
	FragOffset     uint16
	TTL            uint8
	Protocol       IPProtocol
	HeaderChecksum uint16
	SrcIP          net.IP
	DstIP          net.IP
	Payload        []byte
}

func NewIPv4Pdu

func NewIPv4Pdu(srcIp, dstIp net.IP, ipProto IPProtocol, payload []byte) *IPv4Pdu

func (*IPv4Pdu) MarshalBinary

func (ip *IPv4Pdu) MarshalBinary() ([]byte, error)

func (*IPv4Pdu) UnmarshalBinary

func (ip *IPv4Pdu) UnmarshalBinary(payload []byte) error

type IcmpHandler

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

func NewIcmpHandler

func NewIcmpHandler(publishCh chan<- *IPv4Pdu) *IcmpHandler

func (*IcmpHandler) Ping

func (i *IcmpHandler) Ping(dstIP net.IP, numPings uint16)

func (*IcmpHandler) RunHandler

func (i *IcmpHandler) RunHandler(ctx context.Context)

func (*IcmpHandler) SupplierC

func (i *IcmpHandler) SupplierC() chan<- *IPv4Pdu

type IcmpType

type IcmpType uint8
const (
	IcmpTypeEchoRequest IcmpType = 8
	IcmpTypeEchoReply   IcmpType = 0
)

type InterfaceConfig

type InterfaceConfig struct {
	InterfaceName string
	HardwareAddr  *net.HardwareAddr
	Addr          *net.IPNet
	RealIPAddr    *net.IPNet
	ArpTable      *ARPv4Table
	// contains filtered or unexported fields
}

func NewInterfaceConfig

func NewInterfaceConfig(name string, addr *net.IPNet) (*InterfaceConfig, error)

func ParseInterfaceConfig

func ParseInterfaceConfig(config string) (*InterfaceConfig, error)

func (*InterfaceConfig) SetupAndListen

func (i *InterfaceConfig) SetupAndListen(ctx context.Context, supportedEtherTypes []ethernet.EtherType, frameChan chan<- FrameIn)

func (*InterfaceConfig) WriteFrame

func (i *InterfaceConfig) WriteFrame(f *ethernet.Frame) error

type InternetLayerHandler

type InternetLayerHandler interface {
	RunHandler(ctx context.Context)
	SupplierC() chan *InternetV4PacketIn
}

type InternetLayerStrategy

type InternetLayerStrategy interface {
	GetHandler(ipProto IPProtocol) (TransportLayerHandler, error)
}

type InternetLayerStrategyImpl

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

func NewInternetLayerStrategy

func NewInternetLayerStrategy(icmpHandler *IcmpHandler) *InternetLayerStrategyImpl

func (*InternetLayerStrategyImpl) GetHandler

type InternetV4PacketIn

type InternetV4PacketIn struct {
	Packet   *IPv4Pdu
	Ifconfig *InterfaceConfig
}

type InternetV4PacketOut

type InternetV4PacketOut struct {
	Packet    *IPv4Pdu
	RouteInfo *RouteInfo
}

type Internetv4LayerHandler

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

func NewInternetLayerHandler

func NewInternetLayerHandler(publishCh chan<- *InternetV4PacketOut, routeTable *RouteTable) *Internetv4LayerHandler

func (*Internetv4LayerHandler) RunHandler

func (h *Internetv4LayerHandler) RunHandler(ctx context.Context)

func (*Internetv4LayerHandler) SetStrategy

func (*Internetv4LayerHandler) SupplierC

func (h *Internetv4LayerHandler) SupplierC() chan *InternetV4PacketIn

func (*Internetv4LayerHandler) SupplierLocalC

func (h *Internetv4LayerHandler) SupplierLocalC() chan *IPv4Pdu

type LinkLayerHandler

type LinkLayerHandler interface {
	SupplierC() chan<- FrameIn
}

type LinkLayerListener

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

func NewLinkLayerListener

func NewLinkLayerListener(interfaces ...*InterfaceConfig) *LinkLayerListener

func (*LinkLayerListener) AddInterface

func (l *LinkLayerListener) AddInterface(iface *InterfaceConfig)

func (*LinkLayerListener) IcmpPing

func (l *LinkLayerListener) IcmpPing(ip net.IP, numPings uint16)

func (*LinkLayerListener) Interfaces

func (l *LinkLayerListener) Interfaces() []*InterfaceConfig

func (*LinkLayerListener) ListenAndServe

func (l *LinkLayerListener) ListenAndServe(ctx context.Context)

func (*LinkLayerListener) RouteTable

func (l *LinkLayerListener) RouteTable() *RouteTable

type LinkLayerStrategy

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

func NewLinkLayerStrategy

func NewLinkLayerStrategy(strategies map[ethernet.EtherType]LinkLayerHandler) *LinkLayerStrategy

func (*LinkLayerStrategy) GetHandler

func (l *LinkLayerStrategy) GetHandler(etherType ethernet.EtherType) (LinkLayerHandler, error)

func (*LinkLayerStrategy) GetSupportedEtherTypes

func (l *LinkLayerStrategy) GetSupportedEtherTypes() []ethernet.EtherType

type RouteInfo

type RouteInfo struct {
	RouteType    RouteType
	DstNet       net.IPNet
	OutInterface *InterfaceConfig
	NextHop      *net.IP
}

func (*RouteInfo) Validate

func (ri *RouteInfo) Validate() error

type RouteTable

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

func NewRouteTable

func NewRouteTable() *RouteTable

func (*RouteTable) AddRoute

func (table *RouteTable) AddRoute(config RouteInfo) error

func (*RouteTable) DeleteRouteAtIndex

func (table *RouteTable) DeleteRouteAtIndex(index uint)

func (*RouteTable) GetRoutes

func (table *RouteTable) GetRoutes() []RouteInfo

func (*RouteTable) MustAddRoute

func (table *RouteTable) MustAddRoute(config RouteInfo)

func (*RouteTable) RoutePacket

func (table *RouteTable) RoutePacket(ip IPv4Pdu) (*IPv4Pdu, *RouteInfo, error)

type RouteType

type RouteType uint8
const (
	LinkLocalRouteType RouteType = 0
	StaticRouteType    RouteType = 1
)

func (RouteType) String

func (r RouteType) String() string

type TransportLayerHandler

type TransportLayerHandler interface {
	SupplierC() chan<- *IPv4Pdu
}

Directories

Path Synopsis
cmd
edurouter command
internal
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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