Documentation ¶
Index ¶
- Variables
- type DHCPClient
- func (d *DHCPClient) Abort()
- func (d *DHCPClient) BeginRequest(cfg DHCPRequestConfig) error
- func (d *DHCPClient) BroadcastAddr() netip.Addr
- func (d *DHCPClient) CIDRBits() uint8
- func (d *DHCPClient) DHCPServer() netip.Addr
- func (d *DHCPClient) DNSServers() []netip.Addr
- func (d *DHCPClient) Gateway() netip.Addr
- func (d *DHCPClient) Hostname() []byte
- func (d *DHCPClient) IPLeaseTime() time.Duration
- func (d *DHCPClient) IsDone() booldeprecated
- func (d *DHCPClient) LocalPort() uint16
- func (d *DHCPClient) Offer() netip.Addr
- func (d *DHCPClient) RebindingTime() time.Duration
- func (d *DHCPClient) RenewalTime() time.Duration
- func (d *DHCPClient) RequestSentAt() time.Time
- func (d *DHCPClient) Router() netip.Addr
- func (d *DHCPClient) State() dhcp.ClientState
- type DHCPRequestConfig
- type DHCPServer
- type DNSClient
- type DNSResolveConfig
- type NTPClient
- type PortStack
- func (ps *PortStack) ARP() *arpClient
- func (ps *PortStack) Addr() netip.Addr
- func (ps *PortStack) CloseTCP(portNum uint16) error
- func (ps *PortStack) CloseUDP(portNum uint16) error
- func (ps *PortStack) FlagPendingTCP(portNum uint16) error
- func (ps *PortStack) FlagPendingUDP(portNum uint16) error
- func (ps *PortStack) HandleEth(dst []byte) (n int, err error)
- func (ps *PortStack) HardwareAddr6() [6]byte
- func (ps *PortStack) IsPendingHandling() bool
- func (ps *PortStack) MTU() uint16
- func (ps *PortStack) OpenTCP(portNum uint16, handler itcphandler) error
- func (ps *PortStack) OpenUDP(portNum uint16, handler iudphandler) error
- func (ps *PortStack) RecvEth(ethernetFrame []byte) (err error)
- func (ps *PortStack) SetAddr(addr netip.Addr)
- func (ps *PortStack) SetLogger(log *slog.Logger)
- type PortStackConfig
- type TCPConn
- func (sock *TCPConn) BufferedInput() int
- func (sock *TCPConn) Close() error
- func (sock *TCPConn) FlushOutputBuffer() error
- func (sock *TCPConn) LocalAddr() net.Addr
- func (sock *TCPConn) LocalPort() uint16
- func (sock *TCPConn) OpenDialTCP(localPort uint16, remoteMAC [6]byte, remote netip.AddrPort, iss seqs.Value) error
- func (sock *TCPConn) OpenListenTCP(localPortNum uint16, iss seqs.Value) error
- func (sock *TCPConn) PortStack() *PortStack
- func (sock *TCPConn) Read(b []byte) (int, error)
- func (sock *TCPConn) RemoteAddr() net.Addr
- func (sock *TCPConn) SetDeadline(t time.Time) error
- func (sock *TCPConn) SetReadDeadline(t time.Time) error
- func (sock *TCPConn) SetWriteDeadline(t time.Time) error
- func (sock *TCPConn) State() seqs.State
- func (sock *TCPConn) Write(b []byte) (n int, _ error)
- type TCPConnConfig
- type TCPListener
- type TCPListenerConfig
- type TCPPacket
- func (pkt *TCPPacket) CalculateHeaders(seg seqs.Segment, payload []byte)
- func (pkt *TCPPacket) IPOptions() []byte
- func (pkt *TCPPacket) InvertSrcDest()
- func (pkt *TCPPacket) Payload() []byte
- func (pkt *TCPPacket) PutHeaders(b []byte)
- func (pkt *TCPPacket) PutHeadersWithOptions(b []byte) error
- func (pkt *TCPPacket) String() string
- func (pkt *TCPPacket) TCPOptions() []byte
- type UDPPacket
Constants ¶
This section is empty.
Variables ¶
var ( ErrDroppedPacket = errors.New("dropped packet") ErrChecksumTCPorUDP = errors.New("invalid TCP/UDP checksum") )
Common errors.
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 (*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 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 (*DNSClient) StartResolve ¶
func (dnsc *DNSClient) StartResolve(cfg DNSResolveConfig) error
type DNSResolveConfig ¶
type NTPClient ¶
type NTPClient struct {
// contains filtered or unexported fields
}
func NewNTPClient ¶
func (*NTPClient) BeginDefaultRequest ¶
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) CloseTCP ¶
CloseTCP closes the TCP port, effectively aborting the connection. See PortStack.
func (*PortStack) FlagPendingTCP ¶
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 ¶
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) HardwareAddr6 ¶
HardwareAddr6 returns the Stack's 6 byte MAC address (or EUI-48).
func (*PortStack) IsPendingHandling ¶
IsPendingHandling checks if a call to HandleEth could possibly result in a packet being generated by the PortStack.
func (*PortStack) OpenTCP ¶
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 ¶
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 ¶
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.
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 ¶
BufferedInput returns the number of bytes in the socket's input buffer.
func (*TCPConn) FlushOutputBuffer ¶
FlushOutputBuffer waits until the output buffer is empty or the socket is closed.
func (*TCPConn) LocalPort ¶
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 ¶
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) Read ¶
Read reads data from the socket's input buffer. If the buffer is empty, Read will block until data is available.
func (*TCPConn) RemoteAddr ¶
RemoteAddr implements net.Conn interface.
func (*TCPConn) SetDeadline ¶
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 ¶
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 ¶
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.
type TCPConnConfig ¶
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 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 (*TCPPacket) CalculateHeaders ¶
func (*TCPPacket) InvertSrcDest ¶
func (pkt *TCPPacket) InvertSrcDest()
func (*TCPPacket) Payload ¶
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 ¶
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 (*TCPPacket) TCPOptions ¶
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 ¶
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.