mirror of
https://github.com/soypat/lneto.git
synced 2026-08-21 15:09:05 +00:00
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:
+8
-10
@@ -99,13 +99,13 @@ func (s *StackAsync) Reset(cfg StackConfig) error {
|
||||
addr := cfg.StaticAddress
|
||||
s.prng = uint32(cfg.RandSeed)
|
||||
if s.prng == 0 {
|
||||
return errors.New("zero random seed")
|
||||
return lneto.ErrInvalidConfig
|
||||
}
|
||||
s.hostname = cfg.Hostname
|
||||
if !addr.IsValid() {
|
||||
addr = netip.AddrFrom4([4]byte{}) // If static not set DHCP will be performed and address will be zero.
|
||||
} else if addr.Is6() {
|
||||
return errors.New("IPv6 unsupported as of yet")
|
||||
return lneto.ErrUnsupported
|
||||
}
|
||||
const linkNodes = 2 // ARP and IP nodes
|
||||
ecfg := internet.StackEthernetConfig{
|
||||
@@ -171,13 +171,11 @@ func (s *StackAsync) Reset(cfg StackConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var errInvalidIPAddr = errors.New("invaldi IP address")
|
||||
|
||||
func (s *StackAsync) resetARP() error {
|
||||
mac := s.link.HardwareAddr6()
|
||||
addr := s.ip.Addr()
|
||||
if !addr.IsValid() {
|
||||
return errInvalidIPAddr
|
||||
return lneto.ErrInvalidAddr
|
||||
}
|
||||
proto := ethernet.TypeIPv4
|
||||
if addr.Is6() {
|
||||
@@ -384,7 +382,7 @@ func (s *StackAsync) ResultLookupIP(host string) ([]netip.Addr, bool, error) {
|
||||
} else if len(data) == 16 {
|
||||
addrs = append(addrs, netip.AddrFrom16([16]byte(data)))
|
||||
} else {
|
||||
err = errors.New("bogus IP")
|
||||
err = lneto.ErrInvalidAddr
|
||||
}
|
||||
}
|
||||
if err == nil && len(addrs) == 0 {
|
||||
@@ -441,7 +439,7 @@ func (s *StackAsync) StartResolveHardwareAddress6(ip netip.Addr) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if !ip.Is4() {
|
||||
return errors.New("unsupported or invalid IP address")
|
||||
return lneto.ErrUnsupported
|
||||
}
|
||||
addr := ip.As4()
|
||||
return s.arp.StartQuery(nil, addr[:])
|
||||
@@ -452,7 +450,7 @@ func (s *StackAsync) ResultResolveHardwareAddress6(ip netip.Addr) (hw [6]byte, e
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if !ip.Is4() {
|
||||
return hw, errors.New("unsupported or invalid IP address")
|
||||
return hw, lneto.ErrUnsupported
|
||||
}
|
||||
addr := ip.As4()
|
||||
hwslice, err := s.arp.QueryResult(addr[:])
|
||||
@@ -469,7 +467,7 @@ func (s *StackAsync) DiscardResolveHardwareAddress6(ip netip.Addr) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if !ip.Is4() {
|
||||
return errors.New("unsupported or invalid IP address")
|
||||
return lneto.ErrUnsupported
|
||||
}
|
||||
addr := ip.As4()
|
||||
return s.arp.DiscardQuery(addr[:])
|
||||
@@ -526,7 +524,7 @@ func (stack *StackAsync) AssimilateDHCPResults(results *DHCPResults) error {
|
||||
}
|
||||
if len(results.DNSServers) > 0 {
|
||||
if !results.DNSServers[0].IsValid() || !results.DNSServers[0].Is4() {
|
||||
return errors.New("bad DNS server address, IPv6 or invalid")
|
||||
return lneto.ErrInvalidAddr
|
||||
}
|
||||
stack.dnssv = results.DNSServers[0]
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ package xnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/netip"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto"
|
||||
"github.com/soypat/lneto/tcp"
|
||||
)
|
||||
|
||||
@@ -36,7 +36,7 @@ func (s StackBerkeley) Socket(ctx context.Context, network string, family, sotyp
|
||||
switch family {
|
||||
case syscall.AF_INET:
|
||||
default:
|
||||
return nil, errors.New("unsupported address family")
|
||||
return nil, lneto.ErrUnsupported
|
||||
}
|
||||
var local, remote netip.AddrPort
|
||||
if laddr != nil {
|
||||
@@ -54,10 +54,10 @@ func (s StackBerkeley) Socket(ctx context.Context, network string, family, sotyp
|
||||
|
||||
switch network {
|
||||
case "udp", "udp4":
|
||||
return nil, errors.New("udp not yet supported")
|
||||
return nil, lneto.ErrUnsupported
|
||||
case "tcp", "tcp4":
|
||||
if sotype != sockSTREAM {
|
||||
return nil, errors.New("unsupported socket type")
|
||||
return nil, lneto.ErrUnsupported
|
||||
}
|
||||
|
||||
if raddr != nil {
|
||||
@@ -106,7 +106,7 @@ func (s StackBerkeley) Socket(ctx context.Context, network string, family, sotyp
|
||||
return &l, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unsupported network")
|
||||
return nil, lneto.ErrUnsupported
|
||||
}
|
||||
|
||||
type tcplistener struct {
|
||||
|
||||
+2
-2
@@ -2,11 +2,11 @@ package xnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto"
|
||||
"github.com/soypat/lneto/tcp"
|
||||
)
|
||||
|
||||
@@ -52,7 +52,7 @@ type TCPPoolConfig struct {
|
||||
|
||||
func NewTCPPool(cfg TCPPoolConfig) (*TCPPool, error) {
|
||||
if cfg.EstablishedTimeout <= 0 || cfg.ClosingTimeout <= 0 {
|
||||
return nil, errors.New("invalid timeout")
|
||||
return nil, lneto.ErrInvalidConfig
|
||||
}
|
||||
n := cfg.PoolSize
|
||||
pool := &TCPPool{
|
||||
|
||||
Reference in New Issue
Block a user