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
+1 -2
View File
@@ -567,6 +567,5 @@ func addr4(addr [4]byte, ok bool) netip.Addr {
func hash(b []byte) uint16 {
var csum lneto.CRC791
csum.Write(b)
return csum.Sum16()
return csum.PayloadSum16(b)
}
+11 -5
View File
@@ -205,7 +205,10 @@ func buildDNSResponsePacket(t *testing.T, txid uint16, dstPort uint16, hostname
ifrm.SetProtocol(lneto.IPProtoUDP)
*ifrm.SourceAddr() = srcIP.As4()
*ifrm.DestinationAddr() = dstIP.As4()
ifrm.SetCRC(ifrm.CalculateHeaderCRC())
// Zero the CRC field so its value does not add to the final result.
ifrm.SetCRC(0)
crcValue := ifrm.CalculateHeaderCRC()
ifrm.SetCRC(crcValue)
// UDP header.
udpStart := ipStart + ipHdrLen
@@ -215,7 +218,8 @@ func buildDNSResponsePacket(t *testing.T, txid uint16, dstPort uint16, hostname
}
udpFrame.SetSourcePort(dns.ServerPort)
udpFrame.SetDestinationPort(dstPort)
udpFrame.SetLength(uint16(udpHdrLen + len(dnsPayload)))
udpLen := udpHdrLen + len(dnsPayload)
udpFrame.SetLength(uint16(udpLen))
// Copy DNS payload before calculating checksum.
dnsStart := udpStart + udpHdrLen
@@ -223,9 +227,11 @@ func buildDNSResponsePacket(t *testing.T, txid uint16, dstPort uint16, hostname
// Calculate UDP checksum using pseudo header.
var crc lneto.CRC791
ifrm.CRCWriteUDPPseudo(&crc)
udpFrame.CRCWriteIPv4(&crc)
udpFrame.SetCRC(crc.Sum16())
ifrm.CRCWriteUDPPseudo(&crc, uint16(udpLen))
// Zero the CRC field so its value does not add to the final result.
udpFrame.SetCRC(0)
crcValue = crc.PayloadSum16(udpFrame.RawData())
udpFrame.SetCRC(crcValue)
return pkt, nil
}