mirror of
https://github.com/soypat/lneto.git
synced 2026-08-19 06:04:01 +00:00
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:
@@ -94,7 +94,10 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
|
||||
ifrm.SetProtocol(lneto.IPProtoTCP)
|
||||
*ifrm.SourceAddr() = gen.SrcIPv4
|
||||
*ifrm.DestinationAddr() = gen.DstIPv4
|
||||
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)
|
||||
|
||||
ipPayload := ifrm.Payload()
|
||||
tfrm, err := tcp.NewFrame(ipPayload)
|
||||
@@ -124,9 +127,10 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
|
||||
copy(tfrm.Options(), tcpOpts)
|
||||
var crc lneto.CRC791
|
||||
ifrm.CRCWriteTCPPseudo(&crc)
|
||||
tfrm.CRCWrite(&crc)
|
||||
|
||||
tfrm.SetCRC(crc.Sum16())
|
||||
// Zero the CRC field so its value does not add to the final result.
|
||||
tfrm.SetCRC(0)
|
||||
crcValue = crc.PayloadSum16(tfrm.RawData())
|
||||
tfrm.SetCRC(crcValue)
|
||||
switch {
|
||||
case gen.SrcTCP != tfrm.SourcePort():
|
||||
panic("IP options overwrite TCP header")
|
||||
|
||||
Reference in New Issue
Block a user