Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Fix handling of mock snaplen field in pktHdr for packets smaller than snaplen #86

Merged

Conversation

fako1024
Copy link
Owner

@fako1024 fako1024 commented May 6, 2024

Second attempt - took the wrong default value and found a few other unwanted side-effect / inconsistencies that only occur for the edge case of payloads smaller than the snaplen and situations where it's important that the data length isn't spare.

Closes #84

@fako1024 fako1024 added the bug Something isn't working label May 6, 2024
@fako1024 fako1024 requested a review from els0r May 6, 2024 15:12
@fako1024 fako1024 self-assigned this May 6, 2024
@fako1024 fako1024 linked an issue May 6, 2024 that may be closed by this pull request
@@ -121,7 +121,7 @@ func NewSourceFromLink(link *link.Link, options ...Option) (*Source, error) {
// NextIPPacket() methods (the latter two by calling .Payload() / .IPLayer() on the created buffer). It ensures
// that a valid packet of appropriate structure / length is created
func (s *Source) NewPacket() capture.Packet {
p := make(capture.Packet, 6+s.snapLen)
p := make(capture.Packet, capture.PacketHdrOffset+s.snapLen)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by code cleanup (uses newly introduced const).

@@ -141,7 +141,7 @@ func (m *MockSource) addPacket(payload []byte, totalLen uint32, pktType, ipLayer
block := m.ringBuffer.ring[thisBlock*m.blockSize : thisBlock*m.blockSize+m.blockSize]

*(*tPacketHeaderV3Mock)(unsafe.Pointer(&block[m.curBlockPos+12])) = tPacketHeaderV3Mock{
snaplen: min(uint32(m.snapLen), totalLen), // The snaplen is set to the totalLen if a packet is shorter
snaplen: min(uint32(m.snapLen), uint32(len(payload))), // The snaplen is set to the totalLen if a packet is shorter
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrect. The totalLen of course is the wrong value to take here, what matters is the actual length of the captured fraction of the packet.

@fako1024 fako1024 changed the title [bugfix] Fix handling of snaplen field in pktHdr for packets smaller than snaplen [bugfix] Fix handling of mock snaplen field in pktHdr for packets smaller than snaplen May 6, 2024
@fako1024 fako1024 merged commit 3f4b169 into main May 7, 2024
4 checks passed
@fako1024 fako1024 deleted the 84-align-mock-snaplen-handling-for-pkthdr-with-reality-2 branch May 7, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Align mock snaplen handling for pktHdr with reality
2 participants