Checksum simplification proposal (#29)

* Simplified checksum calculation and verification

* Fixed tests

* UDP: using NewBoundedFrame to create a Frame instance limited to the actual frame size.

* Minor: renamed some functions

* Commented and made more readable consecutive SetCRC calls.

* Fixed icmp checksum calculation after rebase

* Replaced NewBoundedFrame with explicit validation
This commit is contained in:
ddirect
2026-02-05 22:35:42 +02:00
committed by GitHub
parent cd51bd6c3f
commit fba97c990a
13 changed files with 139 additions and 228 deletions
+10 -8
View File
@@ -136,23 +136,25 @@ func (ifrm Frame) CalculateHeaderCRC() uint16 {
}
func (ifrm Frame) CRCWriteHeader(crc *lneto.CRC791) {
crc.Write(ifrm.buf[0:10])
crc.Write(ifrm.buf[12:20])
crc.WriteEven(ifrm.buf[:20])
}
func (ifrm Frame) CRCWriteTCPPseudo(crc *lneto.CRC791) {
crc.Write(ifrm.SourceAddr()[:])
crc.Write(ifrm.DestinationAddr()[:])
crc.AddUint16(ifrm.TotalLength() - 4*uint16(ifrm.ihl()))
crc.WriteEven(ifrm.sourceAndDestinationAddr())
crc.AddUint16(ifrm.TotalLength() - uint16(ifrm.HeaderLength()))
crc.AddUint16(uint16(ifrm.Protocol()))
}
func (ifrm Frame) CRCWriteUDPPseudo(crc *lneto.CRC791) {
crc.Write(ifrm.SourceAddr()[:])
crc.Write(ifrm.DestinationAddr()[:])
func (ifrm Frame) CRCWriteUDPPseudo(crc *lneto.CRC791, udpLength uint16) {
crc.WriteEven(ifrm.sourceAndDestinationAddr())
crc.AddUint16(udpLength)
crc.AddUint16(uint16(ifrm.Protocol()))
}
func (ifrm Frame) sourceAndDestinationAddr() []byte {
return ifrm.buf[12:20]
}
// SourceAddr returns pointer to the source IPv4 address in the IP header.
func (ifrm Frame) SourceAddr() *[4]byte {
return (*[4]byte)(ifrm.buf[12:16])
-8
View File
@@ -3,8 +3,6 @@ package icmpv4
import (
"encoding/binary"
"errors"
"github.com/soypat/lneto"
)
type Type uint8
@@ -87,12 +85,6 @@ func (frm Frame) SetCRC(crc uint16) {
binary.BigEndian.PutUint16(frm.buf[2:4], crc)
}
// CRCWrite calculates the checksum of the ICMP packet. Treats the checksum field as zero as per RFC 792.
func (frm Frame) CRCWrite(crc *lneto.CRC791) {
crc.AddUint16(binary.BigEndian.Uint16(frm.buf[0:2]))
crc.Write(frm.buf[4:])
}
func (frm Frame) payload() []byte {
return frm.buf[4:]
}