Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thank you for forking
google/gopacket
and keeping it alive!Originally submitted here: google/gopacket#1057
With a commit from: google/gopacket#883
This PR adds a LayerWithChecksum interface for layers that have a checksum with a function to verify it and implements this function for all layers that use the internet checksum from RFC1071 and where the checksum computation was implemented (for SerializeTo).
It also adds a way to verify the checksums of all layers of a packet and retrieve a list of all found mismatches.
To implement this, I refactored the internet checksum calculation code, deduplicated it and made it a little bit more modular. There are now two functions: ComputeChecksum and FoldChecksum.
These split-up functions make it possible to verify checksums without having to zero-out the checksum that's already set in the packet. Instead, the checksum of the whole packet (including the existing checksum) is computed and then the existing checksum is subtracted, before it is folded. This way we don't need to copy the packet buffer and zero-out the checksum field, just to verify it.
The refactoring and checksum-verification are split up into two commits to make this easier to review.
For the UDP layer, there is a special case, because the UDP checksum is optional. That's why this PR is based on google/gopacket#883 and extends it to make the checksum verification succeed, if the existing UDP checksum was unset.