remove legacy remnants and move ip,udp,ethernet logic into subpackages

This commit is contained in:
soypat
2025-02-20 16:00:55 -03:00
parent 0606a04f45
commit f86bb6f01a
22 changed files with 590 additions and 1885 deletions
+31 -1
View File
@@ -2,10 +2,22 @@ package ipv6
import (
"encoding/binary"
"errors"
"github.com/soypat/lneto/lneto2"
)
// NewIPv6Frame returns a new IPv6Frame with data set to buf.
// An error is returned if the buffer size is smaller than 40.
// Users should still call [IPv6Frame.ValidateSize] before working
// with payload/options of frames to avoid panics.
func NewFrame(buf []byte) (Frame, error) {
if len(buf) < sizeHeader {
return Frame{buf: nil}, errShortBuf
}
return Frame{buf: buf}, nil
}
// Frame encapsulates the raw data of an IPv6 packet
// and provides methods for manipulating, validating and
// retrieving fields and payload data. See [RFC8200].
@@ -86,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 *lneto2.CRC791) {
crc.Write(i6frm.SourceAddr()[:])
crc.Write(i6frm.DestinationAddr()[:])
crc.AddUint32(uint32(i6frm.PayloadLength()))
@@ -99,3 +111,21 @@ func (i6frm Frame) ClearHeader() {
i6frm.buf[i] = 0
}
}
//
// Validate API.
//
var (
errShortFrame = errors.New("ipv6: short frame")
errShortBuf = errors.New("ipv6: short buffer for frame")
)
// 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) {
tl := i6frm.PayloadLength()
if int(tl)+sizeHeader > len(i6frm.RawData()) {
v.AddError(errShortFrame)
}
}