Error rewrites (#43)

* pcap: reuse Frame memory

* slog: reduce heap allocations of addresses; also prevent heap alloc of dhcp options in pcap

* dns: heapless improvement; add StackAsync buffer for more heapless operation; start thinking of errors

* errors: begin standardise errors in lneto

* errors: finish standardization of errors

* fix merge issues

* add more lneto errors to rest of package

* format errors.go
This commit is contained in:
Pat Whittingslow
2026-02-28 20:18:27 +01:00
committed by GitHub
parent 3d0bc93cbe
commit fa5ba918bb
38 changed files with 272 additions and 322 deletions
+13 -5
View File
@@ -1,6 +1,10 @@
package arp
import "errors"
import (
"errors"
"github.com/soypat/lneto"
)
//go:generate stringer -type=Operation -linecomment -output stringers.go .
@@ -11,10 +15,14 @@ const (
)
var (
errARPBufferFull = errors.New("ARP client need handling:too many ops pending")
errShortARP = errors.New("packet too short to be ARP")
errARPUnsupported = errors.New("ARP not supported")
errLargeSizes = errors.New("size of ARP protocol+hardware is unusually large")
errQueryPending = errors.New("arp: query pending")
errQueryNotFound = errors.New("arp: query not found")
// errGeneric aliases for common ARP errors.
errARPBufferFull = lneto.ErrBufferFull
errShortARP = lneto.ErrShortBuffer
errARPUnsupported = lneto.ErrUnsupported
errLargeSizes = lneto.ErrPacketDrop
)
// Operation represents the type of ARP packet, either request or reply/response.
+1 -2
View File
@@ -2,7 +2,6 @@ package arp
import (
"encoding/binary"
"errors"
"fmt"
"net"
"net/netip"
@@ -17,7 +16,7 @@ import (
// with payload/options of frames to avoid panics.
func NewFrame(buf []byte) (Frame, error) {
if len(buf) < sizeHeaderv4 {
return Frame{buf: nil}, errors.New("ARP packet too short")
return Frame{buf: nil}, lneto.ErrShortBuffer
}
return Frame{buf: buf}, nil
}
+13 -14
View File
@@ -1,7 +1,6 @@
package arp
import (
"errors"
"log/slog"
"github.com/soypat/lneto"
@@ -36,7 +35,7 @@ func (h *Handler) ConnectionID() *uint64 { return &h.connID }
func (h *Handler) UpdateProtoAddr(protoAddr []byte) error {
if len(protoAddr) != len(h.ourProtoAddr) {
return errors.New("mismatch ARP proto size")
return lneto.ErrMismatchLen
}
copy(h.ourProtoAddr, protoAddr)
return nil
@@ -45,9 +44,9 @@ func (h *Handler) UpdateProtoAddr(protoAddr []byte) error {
func (h *Handler) Reset(cfg HandlerConfig) error {
if len(cfg.HardwareAddr) == 0 || len(cfg.HardwareAddr) > 255 ||
len(cfg.ProtocolAddr) == 0 || len(cfg.ProtocolAddr) > 255 {
return errors.New("invalid Handler address config")
return lneto.ErrInvalidConfig
} else if cfg.MaxQueries <= 0 || cfg.MaxPending <= 0 {
return errors.New("invalid Handler query or pending config")
return lneto.ErrInvalidConfig
}
*h = Handler{
connID: h.connID + 1,
@@ -102,16 +101,16 @@ func (h *Handler) QueryResult(protoAddr []byte) (hwAddr []byte, err error) {
for i := range h.queries {
if internal.BytesEqual(protoAddr, h.queries[i].protoaddr) {
if !h.queries[i].querysent {
return nil, errors.New("query not yet sent")
return nil, errQueryPending
}
mac := h.queries[i].response()
if mac == nil {
return nil, errors.New("no response yet")
return nil, errQueryPending
}
return mac, nil
}
}
return nil, errors.New("query not exist or dropped")
return nil, errQueryNotFound
}
func (h *Handler) DiscardQuery(protoAddr []byte) error {
@@ -122,7 +121,7 @@ func (h *Handler) DiscardQuery(protoAddr []byte) error {
return nil
}
}
return errors.New("query not found")
return errQueryNotFound
}
func (h *Handler) compactQueries() {
@@ -150,15 +149,15 @@ func (h *Handler) StartQuery(dstHWAddr, proto []byte) error {
if len(h.queries) == cap(h.queries) {
h.compactQueries()
if len(h.queries) == cap(h.queries) {
return errors.New("too many ongoing queries")
return lneto.ErrBufferFull
}
}
if len(proto) != len(h.ourProtoAddr) {
return errors.New("bad protocol address length")
return lneto.ErrMismatchLen
} else if dstHWAddr != nil && len(dstHWAddr) != len(h.ourHWAddr) {
return errors.New("mismatch hardware size")
return lneto.ErrMismatchLen
} else if dstHWAddr != nil && !internal.IsZeroed(dstHWAddr...) {
return errors.New("write-to buffer must be zeroed out")
return lneto.ErrInvalidConfig
}
h.queries = h.queries[:len(h.queries)+1]
q := &h.queries[len(h.queries)-1]
@@ -230,11 +229,11 @@ func (h *Handler) Demux(ethFrame []byte, frameOffset int) error {
}
htype, hlen := afrm.Hardware()
if htype != h.htype || int(hlen) != len(h.ourHWAddr) {
return errors.New("bad ARP hardware")
return lneto.ErrMismatch
}
protoType, protoLen := afrm.Protocol()
if protoType != h.protoType || int(protoLen) != len(h.ourProtoAddr) {
return errors.New("bad ARP proto")
return lneto.ErrMismatch
}
switch afrm.Operation() {
case OpRequest: