mirror of
https://github.com/soypat/lneto.git
synced 2026-08-23 16:09:07 +00:00
Add ICMPv4 and lneto.StackNode and deprecate internet.StackNode (#65)
* begin adding icmp client * rely on anon structs * add tests * tests passing * icmp fleshed out * rework icmp to include ip addr * rename StackAsync.Demux/Encapsulate to RecvEthernet and SendEthernet * remove legacy unreachable TCP tests * remove uses of deprecated internet.StackNode in preference of lneto.StackNode * documentation * rename methods to signal no I/O happening
This commit is contained in:
+4
-34
@@ -1,7 +1,6 @@
|
||||
package internet
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"math"
|
||||
"net"
|
||||
@@ -10,34 +9,6 @@ import (
|
||||
"github.com/soypat/lneto"
|
||||
)
|
||||
|
||||
// StackNode is an abstraction of a packet exchanging protocol controller. This is the building block for all protocols,
|
||||
// from Ethernet to IP to TCP, practically any protocol can be expressed as a StackNode and function completely.
|
||||
type StackNode interface {
|
||||
// Encapsulate writes the stack node's frame into carrierData[offsetToFrame:]
|
||||
// along with any other frame or payload the stack node encapsulates.
|
||||
// The returned integer is amount of bytes written such that carrierData[offsetToFrame:offsetToFrame+n]
|
||||
// contains written data. Data inside carrierData[:offsetToFrame] usually contains data necessary for
|
||||
// a StackNode to correctly emit valid frame data: such is the case for TCP packets which require IP
|
||||
// frame data for checksum calculation. Thus StackNodes must provide fields in their own frame
|
||||
// required by sub-stacknodes for correct encapsulation; in the case of IPv4/6 this means including fields
|
||||
// used in pseudo-header checksum like local IP (see [ipv4.CRCWriteUDPPseudo]).
|
||||
//
|
||||
// offsetToIP is the offset to the IP frame, if present, else its value should be -1.
|
||||
// The relation offsetToIP<=offsetToFrame should always hold.
|
||||
//
|
||||
// When [net.ErrClosed] is returned the StackNode should be discarded and any written data passed up normally.
|
||||
// Errors returned by Encapsulate are "extraordinary" and should not be returned unless the StackNode is receiving invalid carrierData/frameOffset.
|
||||
Encapsulate(carrierData []byte, offsetToIP, offsetToFrame int) (int, error)
|
||||
// Demux reads from the argument buffer where frameOffset is the offset of this StackNode's frame first byte.
|
||||
// The stack node then dispatches(demuxes) the encapsulated frames to its corresponding sub-stack-node(s).
|
||||
Demux(carrierData []byte, frameOffset int) error
|
||||
LocalPort() uint16
|
||||
Protocol() uint64
|
||||
// Connect
|
||||
ConnectionID() *uint64
|
||||
// SetFlagPending(flagPending func(numPendingEncapsulations int))
|
||||
}
|
||||
|
||||
// node is a concrete StackNode as stored in Stacks. Methods are devirtualized for performance benefits, especially on TinyGo.
|
||||
type node struct {
|
||||
currConnID uint64
|
||||
@@ -71,7 +42,7 @@ func (h *handlers) registerByProto(n node) error {
|
||||
return err
|
||||
}
|
||||
if h.nodeByProto(n.proto) != nil {
|
||||
return errProtoRegistered
|
||||
return lneto.ErrAlreadyRegistered
|
||||
}
|
||||
h.nodes = append(h.nodes, n)
|
||||
return nil
|
||||
@@ -83,7 +54,7 @@ func (h *handlers) registerByPortProto(n node) error {
|
||||
return err
|
||||
}
|
||||
if h.nodeByPortProto(n.port, n.proto) != nil {
|
||||
return errProtoRegistered
|
||||
return lneto.ErrAlreadyRegistered
|
||||
}
|
||||
h.nodes = append(h.nodes, n)
|
||||
return nil
|
||||
@@ -200,8 +171,7 @@ func (h *handlers) encapsulateAny(buf []byte, offsetIP, offsetThisFrame int) (_
|
||||
}
|
||||
|
||||
var (
|
||||
errProtoRegistered = errors.New("protocol already registered")
|
||||
_ = net.ErrClosed
|
||||
_ = net.ErrClosed
|
||||
)
|
||||
|
||||
func (node *node) IsInvalid() bool {
|
||||
@@ -212,7 +182,7 @@ func checkNodeErr(node *node, err error) (discard bool) {
|
||||
return node.IsInvalid() || (err != nil && err == net.ErrClosed)
|
||||
}
|
||||
|
||||
func nodeFromStackNode(s StackNode, port uint16, protocol uint64, remoteAddr []byte) node {
|
||||
func nodeFromStackNode(s lneto.StackNode, port uint16, protocol uint64, remoteAddr []byte) node {
|
||||
if protocol > math.MaxUint16 {
|
||||
panic(">16bit protocol number unsupported")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
package internet
|
||||
|
||||
func makecbnode(s StackNode) cbnode {
|
||||
import "github.com/soypat/lneto"
|
||||
|
||||
func makecbnode(s lneto.StackNode) cbnode {
|
||||
return cbnode{
|
||||
_s: s,
|
||||
}
|
||||
@@ -10,7 +12,7 @@ func makecbnode(s StackNode) cbnode {
|
||||
|
||||
type cbnode struct {
|
||||
// Do not access outside of handlers/node logic.
|
||||
_s StackNode
|
||||
_s lneto.StackNode
|
||||
}
|
||||
|
||||
func (s cbnode) Encapsulate(carrierData []byte, offsetToIP, offsetToFrame int) (int, error) {
|
||||
|
||||
@@ -101,6 +101,19 @@ func (ls *StackEthernet) Configure(cfg StackEthernetConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxFrameLength returns the maximum ethernet frame length in bytes, which is the MTU plus the Ethernet header (14 bytes) and CRC (4 bytes if enabled).
|
||||
// This is the maximum size of an Ethernet frame that can be sent from the stack.
|
||||
func (ls *StackEthernet) MaxFrameLength() int {
|
||||
base := int(ls.mtu) + 14
|
||||
if ls.crcupdate != nil {
|
||||
base += 4
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
// MTU is the Maximum Transmission Unit of the stack corresponding
|
||||
// to the maximum payload size of an ethernet frame that can be sent through the stack.
|
||||
// Important to note that the actual ethernet frame size is MTU + Ethernet header (14) + CRC (4 if enabled), this is known as the Maximum Frame Length.
|
||||
func (ls *StackEthernet) MTU() int { return int(ls.mtu) }
|
||||
|
||||
func (ls *StackEthernet) ConnectionID() *uint64 { return &ls.connID }
|
||||
@@ -109,7 +122,7 @@ func (ls *StackEthernet) LocalPort() uint16 { return 0 }
|
||||
|
||||
func (ls *StackEthernet) Protocol() uint64 { return 1 }
|
||||
|
||||
func (ls *StackEthernet) Register(h StackNode) error {
|
||||
func (ls *StackEthernet) Register(h lneto.StackNode) error {
|
||||
proto := h.Protocol()
|
||||
if proto > math.MaxUint16 || proto <= 1500 {
|
||||
return lneto.ErrInvalidConfig
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/soypat/lneto/udp"
|
||||
)
|
||||
|
||||
var _ StackNode = (*StackIP)(nil)
|
||||
var _ lneto.StackNode = (*StackIP)(nil)
|
||||
|
||||
type StackIP struct {
|
||||
connID uint64
|
||||
@@ -205,7 +205,7 @@ func (sb *StackIP) Encapsulate(carrierData []byte, offsetToIP, offsetToFrame int
|
||||
return totalLen, err
|
||||
}
|
||||
|
||||
func (sb *StackIP) Register(h StackNode) error {
|
||||
func (sb *StackIP) Register(h lneto.StackNode) error {
|
||||
proto := h.Protocol()
|
||||
if proto > 255 {
|
||||
return lneto.ErrInvalidConfig
|
||||
@@ -213,6 +213,10 @@ func (sb *StackIP) Register(h StackNode) error {
|
||||
return sb.handlers.registerByPortProto(nodeFromStackNode(h, h.LocalPort(), proto, nil))
|
||||
}
|
||||
|
||||
func (sb *StackIP) IsRegistered(proto lneto.IPProto) bool {
|
||||
return sb.handlers.nodeByProto(uint16(proto)) != nil
|
||||
}
|
||||
|
||||
func (sb *StackIP) recvicmp(icmpData []byte) error {
|
||||
var crc lneto.CRC791
|
||||
if crc.PayloadSum16(icmpData) != 0 {
|
||||
|
||||
@@ -87,7 +87,7 @@ func (ps *StackPorts) Demux(b []byte, offset int) (err error) {
|
||||
|
||||
// Register registers a port StackNode on StackPorts.
|
||||
// If dstMAC is set to non-nil, length six buffer then
|
||||
func (ps *StackPorts) Register(h StackNode) error {
|
||||
func (ps *StackPorts) Register(h lneto.StackNode) error {
|
||||
port := h.LocalPort()
|
||||
proto := h.Protocol()
|
||||
if port <= 0 {
|
||||
@@ -105,7 +105,7 @@ type StackPortsMACFiltered struct {
|
||||
sp StackPorts
|
||||
}
|
||||
|
||||
func (mfsp *StackPortsMACFiltered) Register(h StackNode, addr []byte) error {
|
||||
func (mfsp *StackPortsMACFiltered) Register(h lneto.StackNode, addr []byte) error {
|
||||
port := h.LocalPort()
|
||||
proto := h.Protocol()
|
||||
if port <= 0 {
|
||||
|
||||
@@ -16,7 +16,7 @@ type StackUDPPort struct {
|
||||
raddr []byte
|
||||
}
|
||||
|
||||
func (sudp *StackUDPPort) SetStackNode(node StackNode, raddr []byte, rmport uint16) {
|
||||
func (sudp *StackUDPPort) SetStackNode(node lneto.StackNode, raddr []byte, rmport uint16) {
|
||||
sudp.h = nodeFromStackNode(node, node.LocalPort(), node.Protocol(), raddr)
|
||||
sudp.rmport = rmport
|
||||
sudp.raddr = append(sudp.raddr[:0], raddr...)
|
||||
|
||||
Reference in New Issue
Block a user