mirror of
https://github.com/soypat/lneto.git
synced 2026-08-21 15:09:05 +00:00
use lneto.ErrDropPacket where appropiate; clean up examples/xnet output
This commit is contained in:
@@ -21,6 +21,7 @@ type errGeneric uint8
|
|||||||
const (
|
const (
|
||||||
_ errGeneric = iota // non-initialized err
|
_ errGeneric = iota // non-initialized err
|
||||||
ErrPacketDrop // packet dropped
|
ErrPacketDrop // packet dropped
|
||||||
|
ErrBadCRC // incorrect checksum
|
||||||
)
|
)
|
||||||
|
|
||||||
func (err errGeneric) Error() string {
|
func (err errGeneric) Error() string {
|
||||||
|
|||||||
@@ -1,9 +1,28 @@
|
|||||||
package ethernet
|
package ethernet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sizeHeaderNoVLAN = 14
|
sizeHeaderNoVLAN = 14
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AppendAddr appends the text representation of the hardware address to the destination buffer.
|
||||||
|
func AppendAddr(dst []byte, hwAddr [6]byte) []byte {
|
||||||
|
for i, b := range hwAddr {
|
||||||
|
if i != 0 {
|
||||||
|
dst = append(dst, ':')
|
||||||
|
}
|
||||||
|
if b < 16 {
|
||||||
|
dst = append(dst, '0')
|
||||||
|
}
|
||||||
|
dst = strconv.AppendUint(dst, uint64(b), 16)
|
||||||
|
}
|
||||||
|
return dst
|
||||||
|
}
|
||||||
|
|
||||||
|
// BroadcastAddr returns the all 0xff's broadcast hardware/MAC/EUI/OUI address.
|
||||||
func BroadcastAddr() [6]byte {
|
func BroadcastAddr() [6]byte {
|
||||||
return [6]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
return [6]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
@@ -17,6 +18,7 @@ import (
|
|||||||
|
|
||||||
"github.com/soypat/lneto"
|
"github.com/soypat/lneto"
|
||||||
"github.com/soypat/lneto/dns"
|
"github.com/soypat/lneto/dns"
|
||||||
|
"github.com/soypat/lneto/ethernet"
|
||||||
"github.com/soypat/lneto/http/httpraw"
|
"github.com/soypat/lneto/http/httpraw"
|
||||||
"github.com/soypat/lneto/internal"
|
"github.com/soypat/lneto/internal"
|
||||||
"github.com/soypat/lneto/internal/ltesto"
|
"github.com/soypat/lneto/internal/ltesto"
|
||||||
@@ -129,6 +131,8 @@ func run() (err error) {
|
|||||||
pfbuf = fmt.Appendf(pfbuf[:0], "%-3s %3d", context, len(pkt))
|
pfbuf = fmt.Appendf(pfbuf[:0], "%-3s %3d", context, len(pkt))
|
||||||
pfbuf = append(pfbuf, ' ', '[')
|
pfbuf = append(pfbuf, ' ', '[')
|
||||||
pfbuf, err = pf.FormatFrames(pfbuf, frames, pkt)
|
pfbuf, err = pf.FormatFrames(pfbuf, frames, pkt)
|
||||||
|
pfbuf = bytes.ReplaceAll(pfbuf, stack.Addr().AppendTo(nil), []byte("us"))
|
||||||
|
pfbuf = bytes.ReplaceAll(pfbuf, ethernet.AppendAddr(nil, stack.HardwareAddress()), []byte("us"))
|
||||||
pfbuf = append(pfbuf, ']', '\n')
|
pfbuf = append(pfbuf, ']', '\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/soypat/lneto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StackNode is an abstraction of a packet exchanging protocol controller. This is the building block for all protocols,
|
// StackNode is an abstraction of a packet exchanging protocol controller. This is the building block for all protocols,
|
||||||
@@ -148,19 +150,20 @@ func (h *handlers) nodeByPortProto(port uint16, protocol uint16) *node {
|
|||||||
func (h *handlers) demuxByProto(buf []byte, offset int, proto uint16) (*node, error) {
|
func (h *handlers) demuxByProto(buf []byte, offset int, proto uint16) (*node, error) {
|
||||||
node := h.nodeByProto(proto)
|
node := h.nodeByProto(proto)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return nil, nil
|
return nil, lneto.ErrPacketDrop
|
||||||
}
|
}
|
||||||
err := node.demux(buf, offset)
|
err := node.demux(buf, offset)
|
||||||
if h.tryHandleError(node, err) {
|
if h.tryHandleError(node, err) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handlers) demuxByPort(buf []byte, offset int, port uint16) (*node, error) {
|
func (h *handlers) demuxByPort(buf []byte, offset int, port uint16) (*node, error) {
|
||||||
node := h.nodeByPort(port)
|
node := h.nodeByPort(port)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return nil, nil
|
return nil, lneto.ErrPacketDrop
|
||||||
}
|
}
|
||||||
err := node.demux(buf, offset)
|
err := node.demux(buf, offset)
|
||||||
if h.tryHandleError(node, err) {
|
if h.tryHandleError(node, err) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/soypat/lneto"
|
"github.com/soypat/lneto"
|
||||||
|
"github.com/soypat/lneto/ethernet"
|
||||||
"github.com/soypat/lneto/tcp"
|
"github.com/soypat/lneto/tcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -122,15 +123,7 @@ func (f *Formatter) formatField(dst []byte, pktStartOff int, field FrameField, p
|
|||||||
} else if field.BitLength == 4*8 {
|
} else if field.BitLength == 4*8 {
|
||||||
dst = netip.AddrFrom4([4]byte(f.buf)).AppendTo(dst)
|
dst = netip.AddrFrom4([4]byte(f.buf)).AppendTo(dst)
|
||||||
} else if field.BitLength == 6*8 {
|
} else if field.BitLength == 6*8 {
|
||||||
for i := range f.buf {
|
dst = ethernet.AppendAddr(dst, [6]byte(f.buf))
|
||||||
if i != 0 {
|
|
||||||
dst = append(dst, ':')
|
|
||||||
}
|
|
||||||
if f.buf[i] < 16 {
|
|
||||||
dst = append(dst, '0')
|
|
||||||
}
|
|
||||||
dst = strconv.AppendUint(dst, uint64(f.buf[i]), 16)
|
|
||||||
}
|
|
||||||
} else if field.BitLength == 16*8 {
|
} else if field.BitLength == 16*8 {
|
||||||
dst = netip.AddrFrom16([16]byte(f.buf)).AppendTo(dst)
|
dst = netip.AddrFrom16([16]byte(f.buf)).AppendTo(dst)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+10
-6
@@ -80,19 +80,21 @@ func (sb *StackIP) Demux(carrierData []byte, offset int) error {
|
|||||||
}
|
}
|
||||||
dst := ifrm.DestinationAddr()
|
dst := ifrm.DestinationAddr()
|
||||||
if sb.ip != ([4]byte{}) && *dst != sb.ip {
|
if sb.ip != ([4]byte{}) && *dst != sb.ip {
|
||||||
return errors.New("not meant for us") // Not meant for us.
|
sb.handlers.debug("ip:not-for-us")
|
||||||
|
return lneto.ErrPacketDrop // Not meant for us.
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.validator.ResetErr()
|
sb.validator.ResetErr()
|
||||||
ifrm.ValidateExceptCRC(&sb.validator)
|
ifrm.ValidateExceptCRC(&sb.validator)
|
||||||
if err = sb.validator.ErrPop(); err != nil {
|
if err = sb.validator.ErrPop(); err != nil {
|
||||||
|
sb.handlers.error("ip:Demux.validate")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gotCRC := ifrm.CRC()
|
gotCRC := ifrm.CRC()
|
||||||
wantCRC := ifrm.CalculateHeaderCRC()
|
wantCRC := ifrm.CalculateHeaderCRC()
|
||||||
if gotCRC != wantCRC {
|
if gotCRC != wantCRC {
|
||||||
sb.handlers.error("StackIP:Demux:crc-mismatch", slog.Uint64("want", uint64(wantCRC)), slog.Uint64("got", uint64(gotCRC)))
|
sb.handlers.error("StackIP:Demux:crc-mismatch", slog.Uint64("want", uint64(wantCRC)), slog.Uint64("got", uint64(gotCRC)))
|
||||||
return errors.New("IPv4 CRC mismatch")
|
return lneto.ErrBadCRC
|
||||||
}
|
}
|
||||||
off := ifrm.HeaderLength()
|
off := ifrm.HeaderLength()
|
||||||
totalLen := ifrm.TotalLength()
|
totalLen := ifrm.TotalLength()
|
||||||
@@ -104,8 +106,8 @@ func (sb *StackIP) Demux(carrierData []byte, offset int) error {
|
|||||||
// nodeIdx := getNodeByProto(sb.handlers, uint16(proto))
|
// nodeIdx := getNodeByProto(sb.handlers, uint16(proto))
|
||||||
if node == nil {
|
if node == nil {
|
||||||
// Drop packet.
|
// Drop packet.
|
||||||
sb.handlers.info("iprecv:drop", slog.String("dstaddr", netip.AddrFrom4(*ifrm.DestinationAddr()).String()), slog.String("proto", ifrm.Protocol().String()))
|
sb.handlers.info("ip:demux.drop", slog.String("dstaddr", netip.AddrFrom4(*ifrm.DestinationAddr()).String()), slog.String("proto", ifrm.Protocol().String()))
|
||||||
return nil
|
return lneto.ErrPacketDrop
|
||||||
}
|
}
|
||||||
// Incoming CRC Validation of common IP Protocols.
|
// Incoming CRC Validation of common IP Protocols.
|
||||||
var crc lneto.CRC791
|
var crc lneto.CRC791
|
||||||
@@ -118,7 +120,8 @@ func (sb *StackIP) Demux(carrierData []byte, offset int) error {
|
|||||||
}
|
}
|
||||||
tfrm.CRCWrite(&crc)
|
tfrm.CRCWrite(&crc)
|
||||||
if crc.Sum16() != tfrm.CRC() {
|
if crc.Sum16() != tfrm.CRC() {
|
||||||
return errors.New("TCP CRC mismatch")
|
sb.handlers.error("ip:demux.tcpcrc")
|
||||||
|
return lneto.ErrBadCRC
|
||||||
}
|
}
|
||||||
case lneto.IPProtoUDP:
|
case lneto.IPProtoUDP:
|
||||||
ifrm.CRCWriteUDPPseudo(&crc)
|
ifrm.CRCWriteUDPPseudo(&crc)
|
||||||
@@ -128,7 +131,8 @@ func (sb *StackIP) Demux(carrierData []byte, offset int) error {
|
|||||||
}
|
}
|
||||||
ufrm.CRCWriteIPv4(&crc)
|
ufrm.CRCWriteIPv4(&crc)
|
||||||
if crc.Sum16() != ufrm.CRC() {
|
if crc.Sum16() != ufrm.CRC() {
|
||||||
return errors.New("UDP CRC mismatch")
|
sb.handlers.error("ip:demux.udpcrc")
|
||||||
|
return lneto.ErrBadCRC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.handlers.info("ipDemux", slog.String("ipproto", proto.String()), slog.Int("plen", int(totalLen)))
|
sb.handlers.info("ipDemux", slog.String("ipproto", proto.String()), slog.Int("plen", int(totalLen)))
|
||||||
|
|||||||
@@ -43,13 +43,13 @@ func (sudp *StackUDPPort) Demux(carrierData []byte, frameOffset int) error {
|
|||||||
}
|
}
|
||||||
dst := ufrm.DestinationPort()
|
dst := ufrm.DestinationPort()
|
||||||
if dst != sudp.h.port {
|
if dst != sudp.h.port {
|
||||||
return nil // Not meant for us.
|
return lneto.ErrPacketDrop // Not meant for us.
|
||||||
}
|
}
|
||||||
// TODO remote ip address handling.
|
// TODO remote ip address handling.
|
||||||
|
|
||||||
src := ufrm.SourcePort()
|
src := ufrm.SourcePort()
|
||||||
if sudp.rmport != 0 && src != sudp.rmport {
|
if sudp.rmport != 0 && src != sudp.rmport {
|
||||||
return nil // Not from our target remote port.
|
return lneto.ErrPacketDrop // Not from our target remote port.
|
||||||
}
|
}
|
||||||
err = sudp.h.demux(carrierData, frameOffset+8)
|
err = sudp.h.demux(carrierData, frameOffset+8)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user