refactor lneto2->lneto package

This commit is contained in:
soypat
2025-03-06 23:11:42 -03:00
parent d04d287ab6
commit d5d729b961
17 changed files with 57 additions and 53 deletions
+2 -2
View File
@@ -7,8 +7,8 @@ import (
"net"
"net/netip"
"github.com/soypat/lneto"
"github.com/soypat/lneto/ethernet"
"github.com/soypat/lneto/lneto2"
)
// NewARPFrame returns a ARPFrame with data set to buf.
@@ -135,7 +135,7 @@ func (afrm Frame) SwapTargetSender() {
//
// ValidateSize checks the frame's size fields and compares with the actual buffer
// the frame. It returns a non-nil error on finding an inconsistency.
func (afrm Frame) ValidateSize(v *lneto2.Validator) {
func (afrm Frame) ValidateSize(v *lneto.Validator) {
_, hlen := afrm.Hardware()
_, ilen := afrm.Protocol()
minLen := 8 + 2*(hlen+ilen)
+2 -2
View File
@@ -4,8 +4,8 @@ import (
"bytes"
"errors"
"github.com/soypat/lneto"
"github.com/soypat/lneto/ethernet"
"github.com/soypat/lneto/lneto2"
)
type Handler struct {
@@ -133,7 +133,7 @@ func (c *Handler) Recv(b []byte) error {
if err != nil {
return err
}
var vld lneto2.Validator
var vld lneto.Validator
afrm.ValidateSize(&vld)
if vld.HasError() {
return vld.Err()
+1 -1
View File
@@ -1,4 +1,4 @@
package lneto2
package lneto
import (
"encoding/binary"
+1 -1
View File
@@ -1,4 +1,4 @@
package lneto2
package lneto
//go:generate stringer -type=IPProto -linecomment -output stringers.go .
+2 -2
View File
@@ -4,7 +4,7 @@ import (
"encoding/binary"
"errors"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto"
)
// NewFrame returns a EthFrame with data set to buf.
@@ -120,7 +120,7 @@ var (
// ValidateSize checks the frame's size fields and compares with the actual buffer
// the frame. It returns a non-nil error on finding an inconsistency.
func (efrm Frame) ValidateSize(v *lneto2.Validator) {
func (efrm Frame) ValidateSize(v *lneto.Validator) {
sz := efrm.EtherTypeOrSize()
if sz.IsSize() && len(efrm.buf) < int(sz) {
v.AddError(errShort)
+9 -9
View File
@@ -15,12 +15,12 @@ import (
"os"
"time"
"github.com/soypat/lneto"
"github.com/soypat/lneto/arp"
"github.com/soypat/lneto/ethernet"
"github.com/soypat/lneto/internal"
"github.com/soypat/lneto/ipv4"
"github.com/soypat/lneto/ipv6"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto/tcp"
)
@@ -203,7 +203,7 @@ func (ls *LinkStack) RecvEth(ethFrame []byte) (err error) {
if !efrm.IsBroadcast() && ls.mac != *dstaddr {
return fmt.Errorf("incoming %s mismatch hwaddr %s", etype.String(), net.HardwareAddr(dstaddr[:]).String())
}
var vld lneto2.Validator
var vld lneto.Validator
efrm.ValidateSize(&vld)
if err := vld.Err(); err != nil {
return err
@@ -245,7 +245,7 @@ func (ls *LinkStack) HandleEth(dst []byte) (n int, err error) {
type IPv4Stack struct {
ip [4]byte
validator lneto2.Validator
validator lneto.Validator
handlers []handler
logger
}
@@ -315,7 +315,7 @@ func (is *IPv4Stack) Handle(ethFrame []byte, ipOff int) (int, error) {
ifrm.SetToS(0)
for i := range is.handlers {
h := &is.handlers[i]
proto := lneto2.IPProto(h.proto)
proto := lneto.IPProto(h.proto)
ifrm.SetProtocol(proto)
if len(h.raddr) == 4 {
copy(ifrm.DestinationAddr()[:], h.raddr)
@@ -336,7 +336,7 @@ func (is *IPv4Stack) Handle(ethFrame []byte, ipOff int) (int, error) {
ifrm.SetFlags(dontFrag)
ifrm.SetTTL(64)
ifrm.SetCRC(ifrm.CalculateHeaderCRC())
if ifrm.Protocol() == lneto2.IPProtoTCP {
if ifrm.Protocol() == lneto.IPProtoTCP {
tfrm, _ := tcp.NewFrame(ifrm.Payload())
is.info("IPv4Stack:send", slog.String("ip", ifrm.String()), slog.String("tcp", tfrm.String()))
}
@@ -347,12 +347,12 @@ func (is *IPv4Stack) Handle(ethFrame []byte, ipOff int) (int, error) {
}
type TCPStack struct {
validator lneto2.Validator
validator lneto.Validator
handlers []handler
logger
}
func (ts *TCPStack) Protocol() uint32 { return uint32(lneto2.IPProtoTCP) }
func (ts *TCPStack) Protocol() uint32 { return uint32(lneto.IPProtoTCP) }
func (ts *TCPStack) Register(h Handler, lport uint16) error {
if lport == 0 {
@@ -468,7 +468,7 @@ type TCPPort struct {
handler tcp.Handler
}
func (tp *TCPPort) Protocol() uint32 { return uint32(lneto2.IPProtoTCP) }
func (tp *TCPPort) Protocol() uint32 { return uint32(lneto.IPProtoTCP) }
func (tp *TCPPort) Recv(tcpFrame []byte, off int) error {
if off != 0 {
@@ -582,7 +582,7 @@ func (h *HTTPTap) Close() error { return nil }
func tcpChecksum(ipFrame []byte, tcpPayload int) uint16 {
version := ipFrame[0] >> 4
var tfrm tcp.Frame
var crc lneto2.CRC791
var crc lneto.CRC791
switch version {
case 4:
ifrm, _ := ipv4.NewFrame(ipFrame)
+4 -4
View File
@@ -5,9 +5,9 @@ import (
"math"
"math/rand"
"github.com/soypat/lneto"
"github.com/soypat/lneto/ethernet"
"github.com/soypat/lneto/ipv4"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto/tcp"
)
@@ -91,7 +91,7 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
ifrm.SetID(uint16(rng.Uint32()))
ifrm.SetFlags(0x4001) // Don't fragment.
ifrm.SetTTL(64)
ifrm.SetProtocol(lneto2.IPProtoTCP)
ifrm.SetProtocol(lneto.IPProtoTCP)
*ifrm.SourceAddr() = gen.SrcIPv4
*ifrm.DestinationAddr() = gen.DstIPv4
ifrm.SetCRC(ifrm.CalculateHeaderCRC())
@@ -122,7 +122,7 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
// Set Variable section of data.
copy(ifrm.Options(), ipOpts)
copy(tfrm.Options(), tcpOpts)
var crc lneto2.CRC791
var crc lneto.CRC791
ifrm.CRCWriteTCPPseudo(&crc)
tfrm.CRCWrite(&crc)
@@ -141,7 +141,7 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
case len(tcpPayload) > 0 && firstPayloadByte != tcpPayload[0]:
panic("TCP options overwrite payload")
}
var vld lneto2.Validator
var vld lneto.Validator
efrm.ValidateSize(&vld)
if err = vld.Err(); err != nil {
panic(err)
+4
View File
@@ -0,0 +1,4 @@
package internet
type StackBasic struct {
}
+10 -10
View File
@@ -6,7 +6,7 @@ import (
"fmt"
"net/netip"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto"
)
// NewIPv4Frame returns a new IPv4Frame with data set to buf.
@@ -113,10 +113,10 @@ func (ifrm Frame) SetTTL(ttl uint8) { ifrm.buf[8] = ttl }
// Protocol field defines the protocol used in the data portion of the IP datagram. TCP is 6, UDP is 17.
// See [IPProto].
func (ifrm Frame) Protocol() lneto2.IPProto { return lneto2.IPProto(ifrm.buf[9]) }
func (ifrm Frame) Protocol() lneto.IPProto { return lneto.IPProto(ifrm.buf[9]) }
// SetProtocol sets protocol field. See [Frame.Protocol] and [lneto2.IPProto].
func (ifrm Frame) SetProtocol(proto lneto2.IPProto) { ifrm.buf[9] = uint8(proto) }
// SetProtocol sets protocol field. See [Frame.Protocol] and [lneto.IPProto].
func (ifrm Frame) SetProtocol(proto lneto.IPProto) { ifrm.buf[9] = uint8(proto) }
// CRC returns the cyclic-redundancy-check (checksum) field of the IPv4 header.
func (ifrm Frame) CRC() uint16 {
@@ -130,20 +130,20 @@ func (ifrm Frame) SetCRC(cs uint16) {
// CalculateHeaderCRC calculates the CRC for this IPv4 frame.
func (ifrm Frame) CalculateHeaderCRC() uint16 {
var crc lneto2.CRC791
var crc lneto.CRC791
crc.Write(ifrm.buf[0:10])
crc.Write(ifrm.buf[12:20])
return crc.Sum16()
}
func (ifrm Frame) CRCWriteTCPPseudo(crc *lneto2.CRC791) {
func (ifrm Frame) CRCWriteTCPPseudo(crc *lneto.CRC791) {
crc.Write(ifrm.SourceAddr()[:])
crc.Write(ifrm.DestinationAddr()[:])
crc.AddUint16(ifrm.TotalLength() - 4*uint16(ifrm.ihl()))
crc.AddUint16(uint16(ifrm.Protocol()))
}
func (ifrm Frame) CRCWriteUDPPseudo(crc *lneto2.CRC791) {
func (ifrm Frame) CRCWriteUDPPseudo(crc *lneto.CRC791) {
crc.Write(ifrm.SourceAddr()[:])
crc.Write(ifrm.DestinationAddr()[:])
crc.AddUint16(uint16(ifrm.Protocol()))
@@ -195,7 +195,7 @@ var (
// ValidateSize checks the frame's size fields and compares with the actual buffer
// the frame. It returns a non-nil error on finding an inconsistency.
func (ifrm Frame) ValidateSize(v *lneto2.Validator) {
func (ifrm Frame) ValidateSize(v *lneto.Validator) {
ihl := ifrm.ihl()
tl := ifrm.TotalLength()
if tl < sizeHeader {
@@ -210,13 +210,13 @@ func (ifrm Frame) ValidateSize(v *lneto2.Validator) {
}
// ValidateExceptCRC checks for invalid frame values but does not check CRC.
func (ifrm Frame) ValidateExceptCRC(v *lneto2.Validator) {
func (ifrm Frame) ValidateExceptCRC(v *lneto.Validator) {
ifrm.ValidateSize(v)
flags := ifrm.Flags()
if ifrm.version() != 4 {
v.AddError(errBadVersion)
}
if v.Flags()&lneto2.ValidateEvilBit != 0 && flags.IsEvil() {
if v.Flags()&lneto.ValidateEvilBit != 0 && flags.IsEvil() {
v.AddError(errEvil)
}
}
+6 -6
View File
@@ -4,7 +4,7 @@ import (
"encoding/binary"
"errors"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto"
)
// NewIPv6Frame returns a new IPv6Frame with data set to buf.
@@ -67,12 +67,12 @@ func (i6frm Frame) SetPayloadLength(pl uint16) {
// NextHeader returns the Next Header field of the IPv6 header which usually specifies the transport layer
// protocol used by packet's payload.
func (i6frm Frame) NextHeader() lneto2.IPProto {
return lneto2.IPProto(i6frm.buf[6])
func (i6frm Frame) NextHeader() lneto.IPProto {
return lneto.IPProto(i6frm.buf[6])
}
// SetNextHeader sets the Next Header (protocol) field of the IPv6 header. See [Frame.NextHeader].
func (i6frm Frame) SetNextHeader(proto lneto2.IPProto) {
func (i6frm Frame) SetNextHeader(proto lneto.IPProto) {
i6frm.buf[6] = uint8(proto)
}
@@ -98,7 +98,7 @@ func (i6frm Frame) DestinationAddr() *[16]byte {
return (*[16]byte)(i6frm.buf[24:40])
}
func (i6frm Frame) CRCWritePseudo(crc *lneto2.CRC791) {
func (i6frm Frame) CRCWritePseudo(crc *lneto.CRC791) {
crc.Write(i6frm.SourceAddr()[:])
crc.Write(i6frm.DestinationAddr()[:])
crc.AddUint32(uint32(i6frm.PayloadLength()))
@@ -123,7 +123,7 @@ var (
// ValidateSize checks the frame's size fields and compares with the actual buffer
// the frame. It returns a non-nil error on finding an inconsistency.
func (i6frm Frame) ValidateSize(v *lneto2.Validator) {
func (i6frm Frame) ValidateSize(v *lneto.Validator) {
tl := i6frm.PayloadLength()
if int(tl)+sizeHeader > len(i6frm.RawData()) {
v.AddError(errShortFrame)
+1 -1
View File
@@ -1,4 +1,4 @@
package lneto2_test
package lneto_test
import (
"bytes"
+1 -1
View File
@@ -1,6 +1,6 @@
// Code generated by "stringer -type=IPProto -linecomment -output stringers.go ."; DO NOT EDIT.
package lneto2
package lneto
import "strconv"
+5 -5
View File
@@ -6,7 +6,7 @@ import (
"fmt"
"math"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto"
)
const (
@@ -140,7 +140,7 @@ func (tfrm Frame) Segment(payloadSize int) Segment {
}
}
func (tfrm Frame) CRCWrite(crc *lneto2.CRC791) {
func (tfrm Frame) CRCWrite(crc *lneto.CRC791) {
// Write excluding CRC
crc.Write(tfrm.buf[:16])
crc.Write(tfrm.buf[18:])
@@ -192,14 +192,14 @@ var (
errZeroSrcPort = errors.New("TCP zero source port")
)
// func (tfrm Frame) Validate(v *lneto2.Validator) {
// func (tfrm Frame) Validate(v *lneto.Validator) {
// tfrm.ValidateSize(v)
// tfrm.ValidateExceptCRC(v)
// }
// ValidateSize checks the frame's size fields and compares with the actual buffer
// the frame. It returns a non-nil error on finding an inconsistency.
func (tfrm Frame) ValidateSize(v *lneto2.Validator) {
func (tfrm Frame) ValidateSize(v *lneto.Validator) {
off := tfrm.HeaderLength()
if off < sizeHeaderTCP {
v.AddBitPosErr(12*8, 4, errBadTCPOff)
@@ -209,7 +209,7 @@ func (tfrm Frame) ValidateSize(v *lneto2.Validator) {
}
}
func (tfrm Frame) ValidateExceptCRC(v *lneto2.Validator) {
func (tfrm Frame) ValidateExceptCRC(v *lneto.Validator) {
tfrm.ValidateSize(v)
if tfrm.DestinationPort() == 0 {
v.AddBitPosErr(2*8, 16, errZeroDstPort)
+2 -2
View File
@@ -6,8 +6,8 @@ import (
"log/slog"
"github.com/soypat/lneto"
"github.com/soypat/lneto/internal"
"github.com/soypat/lneto/lneto2"
)
var (
@@ -24,7 +24,7 @@ type Handler struct {
bufTx ringTx
bufRx internal.Ring
logger
validator lneto2.Validator
validator lneto.Validator
localPort uint16
remotePort uint16
// connid is a conenction counter that is incremented each time a new
+2 -2
View File
@@ -5,9 +5,9 @@ import (
"strconv"
"testing"
"github.com/soypat/lneto"
"github.com/soypat/lneto/ethernet"
"github.com/soypat/lneto/ipv4"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto/tcp"
)
@@ -586,7 +586,7 @@ func TestExchange_helloworld_client(t *testing.T) {
}
func parseSegment(t *testing.T, b []byte) (tcp.Segment, []byte) {
var vld lneto2.Validator
var vld lneto.Validator
t.Helper()
efrm, err := ethernet.NewFrame(b)
if err != nil {
+4 -4
View File
@@ -4,7 +4,7 @@ import (
"encoding/binary"
"errors"
"github.com/soypat/lneto/lneto2"
"github.com/soypat/lneto"
)
// NewUDPFrame returns a new UDPFrame with data set to buf.
@@ -80,7 +80,7 @@ func (ufrm Frame) Payload() []byte {
}
// func (ufrm Frame) CalculateIPv4Checksum(ifrm IPv4Frame) uint16 {
// var crc lneto2.CRC791
// var crc lneto.CRC791
// ifrm.crcWriteUDPPseudo(&crc)
// crc.AddUint16(ufrm.Length())
// crc.AddUint16(ufrm.SourcePort())
@@ -91,7 +91,7 @@ func (ufrm Frame) Payload() []byte {
// }
// func (ufrm Frame) CalculateIPv6Checksum(ifrm IPv6Frame) uint16 {
// var crc lneto2.CRC791
// var crc lneto.CRC791
// ifrm.crcWritePseudo(&crc)
// crc.AddUint16(ufrm.SourcePort())
// crc.AddUint16(ufrm.DestinationPort())
@@ -118,7 +118,7 @@ var (
// ValidateSize checks the frame's size fields and compares with the actual buffer
// the frame. It returns a non-nil error on finding an inconsistency.
func (ufrm Frame) ValidateSize(v *lneto2.Validator) {
func (ufrm Frame) ValidateSize(v *lneto.Validator) {
ul := ufrm.Length()
if ul < sizeHeader {
v.AddError(errBadLen)
+1 -1
View File
@@ -1,4 +1,4 @@
package lneto2
package lneto
import (
"errors"