stacks

package
v0.0.0-...-8c2ff3d Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause Imports: 20 Imported by: 13

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDroppedPacket = errors.New("dropped packet")

	ErrChecksumTCPorUDP = errors.New("invalid TCP/UDP checksum")
)

Common errors.

View Source
var ErrFlagPending = errors.New("seqs: pending data")

Functions

This section is empty.

Types

type DHCPClient

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

func NewDHCPClient

func NewDHCPClient(stack *PortStack, lport uint16) *DHCPClient

func (*DHCPClient) Abort

func (d *DHCPClient) Abort()

func (*DHCPClient) BeginRequest

func (d *DHCPClient) BeginRequest(cfg DHCPRequestConfig) error

func (*DHCPClient) BroadcastAddr

func (d *DHCPClient) BroadcastAddr() netip.Addr

func (*DHCPClient) CIDRBits

func (d *DHCPClient) CIDRBits() uint8

func (*DHCPClient) DHCPServer

func (d *DHCPClient) DHCPServer() netip.Addr

DHCPServer IP address field of the DHCP packet. Is the siaddr field of the DHCP packet, which can be overriden with the Server IP option.

func (*DHCPClient) DNSServers

func (d *DHCPClient) DNSServers() []netip.Addr

func (*DHCPClient) Gateway

func (d *DHCPClient) Gateway() netip.Addr

Gateway IP address field of the DHCP packet. The giaddr provides the DHCP server with information about the IP address subnet in which the client resides.

func (*DHCPClient) Hostname

func (d *DHCPClient) Hostname() []byte

func (*DHCPClient) IPLeaseTime

func (d *DHCPClient) IPLeaseTime() time.Duration

func (*DHCPClient) IsDone deprecated

func (d *DHCPClient) IsDone() bool

IsDone

Deprecated: Use d.State()==dhcp.StateBound instead.

func (*DHCPClient) LocalPort

func (d *DHCPClient) LocalPort() uint16

LocalPort returns the local port number used by the DHCP client. If zero the client is not initialized.

func (*DHCPClient) Offer

func (d *DHCPClient) Offer() netip.Addr

func (*DHCPClient) RebindingTime

func (d *DHCPClient) RebindingTime() time.Duration

func (*DHCPClient) RenewalTime

func (d *DHCPClient) RenewalTime() time.Duration

func (*DHCPClient) RequestSentAt

func (d *DHCPClient) RequestSentAt() time.Time

RequestSentAt returns the time at which the last DHCP request was sent.

func (*DHCPClient) Router

func (d *DHCPClient) Router() netip.Addr

func (*DHCPClient) State

func (d *DHCPClient) State() dhcp.ClientState

State returns the current state of the DHCP client.

type DHCPRequestConfig

type DHCPRequestConfig struct {
	RequestedAddr netip.Addr
	Xid           uint32
	// Optional hostname to request.
	Hostname string
	ServerIP netip.Addr
}

type DHCPServer

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

func NewDHCPServer

func NewDHCPServer(ps *PortStack, siaddr netip.Addr, lport uint16) *DHCPServer

func (*DHCPServer) HandleUDP

func (d *DHCPServer) HandleUDP(resp []byte, packet *UDPPacket) (_ int, err error)

func (*DHCPServer) Start

func (d *DHCPServer) Start() error

type DNSClient

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

func NewDNSClient

func NewDNSClient(stack *PortStack, localPort uint16) *DNSClient

func (*DNSClient) Abort

func (dnsc *DNSClient) Abort()

func (*DNSClient) Answers

func (dnsc *DNSClient) Answers() []dns.Resource

func (*DNSClient) IsDone

func (dnsc *DNSClient) IsDone() (bool, dns.RCode)

func (*DNSClient) StartResolve

func (dnsc *DNSClient) StartResolve(cfg DNSResolveConfig) error

type DNSResolveConfig

type DNSResolveConfig struct {
	Questions       []dns.Question
	DNSAddr         netip.Addr
	DNSHWAddr       [6]byte
	EnableRecursion bool
}

type NTPClient

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

func NewNTPClient

func NewNTPClient(stack *PortStack, lport uint16) *NTPClient

func (*NTPClient) Abort

func (d *NTPClient) Abort()

func (*NTPClient) BeginDefaultRequest

func (nc *NTPClient) BeginDefaultRequest(hwaddr [6]byte, raddr netip.Addr) error

func (*NTPClient) IsDone

func (d *NTPClient) IsDone() bool

func (*NTPClient) Offset

func (d *NTPClient) Offset() time.Duration

Offset returns the estimated time offset between the local clock and the server's clock.

type PortStack

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

PortStack implements partial TCP/UDP packet muxing to respective sockets with [PortStack.RcvEth]. This implementation limits itself basic header validation and port matching. Users of PortStack are expected to implement connection state, packet buffering and retransmission logic.

  • In the case of TCP this means implementing the TCP state machine.
  • In the case of UDP PortStack should be enough to build most applications.

Notes on PortStack handlers

  • While PortStack.HandleEth has yet to find a outgoing packet it will look for a port that has a pending packet or has been flagged as pending and call its handler.

  • A call to a handler may or may not have an incoming packet ready to process. When pkt.HasPacket() returns true then pkt contains an incoming packet to the port.

  • When pkt.HasPacket() returns false the contents are undefined.

  • Users can safely use pkt even if pkt.HasPacket() returns false.

  • If the handler returns an error that is not ErrFlagPending then the port is immediately closed.

  • io.EOF and ErrFlagPending: When returned by handler data written is not discarded. This means that the handler can write data and close port in same operation returning non-zero `n` and EOF.

  • ErrFlagPending: When returned by the handler then the port is flagged as pending and the written data is handled normally if there is any. If no data is written the call to HandleEth proceeds looking for another port to handle.

  • ErrFlagPending: When returned by the handler then for UDP/TCP implementations the incoming packet argument `pkt` is flagged as not present in future calls to the handler in pkt.HasPacket calls. The handler however can be aware of this fact and still use the pkt argument since the header+payload contents are not modified by the stack.

func NewPortStack

func NewPortStack(cfg PortStackConfig) *PortStack

NewPortStack creates a ready to use TCP/UDP Stack instance.

func (*PortStack) ARP

func (ps *PortStack) ARP() *arpClient

ARP returns the ARP client for this stack. The type is not exported since it's implementation is experimental.

func (*PortStack) Addr

func (ps *PortStack) Addr() netip.Addr

func (*PortStack) CloseTCP

func (ps *PortStack) CloseTCP(portNum uint16) error

CloseTCP closes the TCP port, effectively aborting the connection. See PortStack.

func (*PortStack) CloseUDP

func (ps *PortStack) CloseUDP(portNum uint16) error

CloseUDP closes a UDP port. See PortStack.

func (*PortStack) FlagPendingTCP

func (ps *PortStack) FlagPendingTCP(portNum uint16) error

FlagPendingTCP flags a given TCP port as having a pending packet. This is useful to force a response even if no packet has been received.

See PortStack for more information on how packets are processed.

func (*PortStack) FlagPendingUDP

func (ps *PortStack) FlagPendingUDP(portNum uint16) error

FlagPendingUDP flags a given UDP port as having a pending packet. This is useful to force a response even if no packet has been received.

See PortStack for more information on how packets are processed.

func (*PortStack) HandleEth

func (ps *PortStack) HandleEth(dst []byte) (n int, err error)

func (*PortStack) HardwareAddr6

func (ps *PortStack) HardwareAddr6() [6]byte

HardwareAddr6 returns the Stack's 6 byte MAC address (or EUI-48).

func (*PortStack) IsPendingHandling

func (ps *PortStack) IsPendingHandling() bool

IsPendingHandling checks if a call to HandleEth could possibly result in a packet being generated by the PortStack.

func (*PortStack) MTU

func (ps *PortStack) MTU() uint16

func (*PortStack) OpenTCP

func (ps *PortStack) OpenTCP(portNum uint16, handler itcphandler) error

OpenTCP opens a TCP port and sets the handler. OpenTCP returns an error if the port is already open or if there is no socket available it returns an error.

See PortStack for information on handler argument.

func (*PortStack) OpenUDP

func (ps *PortStack) OpenUDP(portNum uint16, handler iudphandler) error

OpenUDP opens a UDP port and sets the handler. OpenUDP returns an error if the port is already open or if there is no socket available it returns an error.

See PortStack for information on handler argument.

func (*PortStack) RecvEth

func (ps *PortStack) RecvEth(ethernetFrame []byte) (err error)

RecvEth validates an ethernet+ipv4 frame in payload. If it is OK then it defers response handling of the packets during a call to [Stack.HandleEth].

If [Stack.HandleEth] is not called often enough prevent packet queue from filling up on a socket RecvEth will start to return ErrDroppedPacket.

func (*PortStack) SetAddr

func (ps *PortStack) SetAddr(addr netip.Addr)

func (*PortStack) SetLogger

func (ps *PortStack) SetLogger(log *slog.Logger)

type PortStackConfig

type PortStackConfig struct {
	MaxOpenPortsUDP int
	MaxOpenPortsTCP int
	// GlobalHandler processes all incoming ethernet frames before they reach the port handlers.
	// If GlobalHandler returns an error the frame is discarded and PortStack.HandleEth returns the error.
	// GlobalHandler ethernethandler
	Logger *slog.Logger
	MAC    [6]byte
	// MTU is the maximum transmission unit of the ethernet interface.
	MTU uint16
}

type TCPConn

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

TCPConn is a userspace implementation of a TCP connection intended for use with PortStack though is purposefully loosely coupled. It implements net.Conn.

func NewTCPConn

func NewTCPConn(stack *PortStack, cfg TCPConnConfig) (*TCPConn, error)

func (*TCPConn) BufferedInput

func (sock *TCPConn) BufferedInput() int

BufferedInput returns the number of bytes in the socket's input buffer.

func (*TCPConn) Close

func (sock *TCPConn) Close() error

func (*TCPConn) FlushOutputBuffer

func (sock *TCPConn) FlushOutputBuffer() error

FlushOutputBuffer waits until the output buffer is empty or the socket is closed.

func (*TCPConn) LocalAddr

func (sock *TCPConn) LocalAddr() net.Addr

LocalAddr implements net.Conn interface.

func (*TCPConn) LocalPort

func (sock *TCPConn) LocalPort() uint16

LocalPort returns the local port on which the socket is listening or connected to.

func (*TCPConn) OpenDialTCP

func (sock *TCPConn) OpenDialTCP(localPort uint16, remoteMAC [6]byte, remote netip.AddrPort, iss seqs.Value) error

OpenDialTCP opens an active TCP connection to the given remote address.

func (*TCPConn) OpenListenTCP

func (sock *TCPConn) OpenListenTCP(localPortNum uint16, iss seqs.Value) error

OpenListenTCP opens a passive TCP connection that listens on the given port. OpenListenTCP only handles one connection at a time, so API may change in future to accomodate multiple connections.

func (*TCPConn) PortStack

func (sock *TCPConn) PortStack() *PortStack

PortStack returns the PortStack that this socket is attached to.

func (*TCPConn) Read

func (sock *TCPConn) Read(b []byte) (int, error)

Read reads data from the socket's input buffer. If the buffer is empty, Read will block until data is available.

func (*TCPConn) RemoteAddr

func (sock *TCPConn) RemoteAddr() net.Addr

RemoteAddr implements net.Conn interface.

func (*TCPConn) SetDeadline

func (sock *TCPConn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline. Implements net.Conn.

func (*TCPConn) SetReadDeadline

func (sock *TCPConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call. A zero value for t means Read will not time out.

func (*TCPConn) SetWriteDeadline

func (sock *TCPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls and any currently-blocked Write call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.

func (*TCPConn) State

func (sock *TCPConn) State() seqs.State

State returns the TCP state of the socket.

func (*TCPConn) Write

func (sock *TCPConn) Write(b []byte) (n int, _ error)

Write writes argument data to the socket's output buffer which is queued to be sent.

type TCPConnConfig

type TCPConnConfig struct {
	TxBufSize uint16
	RxBufSize uint16
}

type TCPListener

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

func NewTCPListener

func NewTCPListener(stack *PortStack, cfg TCPListenerConfig) (*TCPListener, error)

func (*TCPListener) Accept

func (l *TCPListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener. It implements the net.Listener interface.

func (*TCPListener) Addr

func (l *TCPListener) Addr() net.Addr

Addr returns the listener's network address. Implements net.Listener.

func (*TCPListener) Close

func (l *TCPListener) Close() error

func (*TCPListener) PortStack

func (l *TCPListener) PortStack() *PortStack

func (*TCPListener) StartListening

func (l *TCPListener) StartListening(port uint16) error

type TCPListenerConfig

type TCPListenerConfig struct {
	MaxConnections uint16
	ConnTxBufSize  uint16
	ConnRxBufSize  uint16
}

type TCPPacket

type TCPPacket struct {
	Rx  time.Time
	Eth eth.EthernetHeader
	IP  eth.IPv4Header
	TCP eth.TCPHeader
	// contains filtered or unexported fields
}

func ParseTCPPacket deprecated

func ParseTCPPacket(b []byte) (pkt TCPPacket, err error)

ParseTCPPacket is a convenience function for generating a pkt TCP packet

Deprecated: This function is guaranteed to disappear in the future. Used only in tests.

func (*TCPPacket) CalculateHeaders

func (pkt *TCPPacket) CalculateHeaders(seg seqs.Segment, payload []byte)

func (*TCPPacket) IPOptions

func (pkt *TCPPacket) IPOptions() []byte

Options returns the TCP options in the packet.

func (*TCPPacket) InvertSrcDest

func (pkt *TCPPacket) InvertSrcDest()

func (*TCPPacket) Payload

func (pkt *TCPPacket) Payload() []byte

Payload returns the TCP payload. If TCP or IPv4 header data is incorrect/bad it returns nil. If the response is "forced" then payload will be nil.

func (*TCPPacket) PutHeaders

func (pkt *TCPPacket) PutHeaders(b []byte)

PutHeaders puts 54 bytes including the Ethernet, IPv4 and TCP headers into b. b must be at least 54 bytes in length or else PutHeaders panics. No options are marshalled.

func (*TCPPacket) PutHeadersWithOptions

func (pkt *TCPPacket) PutHeadersWithOptions(b []byte) error

func (*TCPPacket) String

func (pkt *TCPPacket) String() string

func (*TCPPacket) TCPOptions

func (pkt *TCPPacket) TCPOptions() []byte

Options returns the TCP options in the packet.

type UDPPacket

type UDPPacket struct {
	Rx  time.Time
	Eth eth.EthernetHeader
	IP  eth.IPv4Header
	UDP eth.UDPHeader
	// contains filtered or unexported fields
}

func (*UDPPacket) Payload

func (pkt *UDPPacket) Payload() []byte

Payload returns the UDP payload. If UDP or IPv4 header data is incorrect/bad it returns nil. If the response is "forced" then payload will be nil.

func (*UDPPacket) PutHeaders

func (pkt *UDPPacket) PutHeaders(b []byte)

Jump to

Keyboard shortcuts

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