Look the expected checksum up instead of carrying it in the frame

This commit is contained in:
flamingcow
2026-08-04 12:45:04 -07:00
parent 03b7ff4954
commit 1f9b5ea62e
4 changed files with 18 additions and 12 deletions
+13 -6
View File
@@ -9,7 +9,7 @@ import (
const (
etherBase = 0x88b5
ethHdrLen = 14
hdrLen = 24
hdrLen = 20
hdrMagic = 0x43424c54
minFrame = ethHdrLen + hdrLen
maxFrame = 9216
@@ -88,7 +88,7 @@ func newFrameSpec(dst, src [6]byte, etherType uint16, sizes []int) *frameSpec {
fill(ref)
crcFor := make(map[int]uint32, len(sizes))
for _, s := range sizes {
crcFor[s] = crc32.Checksum(ref[:s-minFrame], crcTable)
crcFor[s-minFrame] = crc32.Checksum(ref[:s-minFrame], crcTable)
}
f.refs = append(f.refs, ref)
f.crcFor = append(f.crcFor, crcFor)
@@ -103,7 +103,17 @@ func (f *frameSpec) prefill(buf []byte, patIdx int) {
copy(buf[minFrame:], f.refs[patIdx])
}
func putHeader(buf []byte, patIdx int, stream uint16, seq uint64, payLen int, crc uint32) {
// Looked up rather than read from the frame: a checksum travelling beside the
// bytes it covers is only ever compared against itself.
func (f *frameSpec) expectedCRC(patIdx, payLen int) (uint32, bool) {
if patIdx < 0 || patIdx >= len(f.crcFor) {
return 0, false
}
crc, ok := f.crcFor[patIdx][payLen]
return crc, ok
}
func putHeader(buf []byte, patIdx int, stream uint16, seq uint64, payLen int) {
h := buf[ethHdrLen:]
binary.BigEndian.PutUint32(h[0:4], hdrMagic)
h[4] = 1
@@ -112,7 +122,6 @@ func putHeader(buf []byte, patIdx int, stream uint16, seq uint64, payLen int, cr
binary.BigEndian.PutUint64(h[8:16], seq)
binary.BigEndian.PutUint16(h[16:18], uint16(payLen))
binary.BigEndian.PutUint16(h[18:20], 0)
binary.BigEndian.PutUint32(h[20:24], crc)
}
type parsed struct {
@@ -120,7 +129,6 @@ type parsed struct {
stream uint16
seq uint64
payLen int
crc uint32
}
func parseHeader(buf []byte) (parsed, bool) {
@@ -136,7 +144,6 @@ func parseHeader(buf []byte) (parsed, bool) {
p.stream = binary.BigEndian.Uint16(h[6:8])
p.seq = binary.BigEndian.Uint64(h[8:16])
p.payLen = int(binary.BigEndian.Uint16(h[16:18]))
p.crc = binary.BigEndian.Uint32(h[20:24])
if minFrame+p.payLen > len(buf) {
return p, false
}