This repository was archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
Fix: Decoding of IPFIX templates with Enterprise Number field #68
Open
shyam334
wants to merge
1
commit into
cloudflare:master
Choose a base branch
from
shyam334:fix/ipfix/template-skip-EN
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"math" | ||
"sync" | ||
|
||
"github.com/cloudflare/goflow/v3/decoders/utils" | ||
|
@@ -90,7 +91,7 @@ func DecodeIPFIXOptionsTemplateSet(payload *bytes.Buffer) ([]IPFIXOptionsTemplat | |
return records, nil | ||
} | ||
|
||
func DecodeTemplateSet(payload *bytes.Buffer) ([]TemplateRecord, error) { | ||
func DecodeNFv9TemplateSet(payload *bytes.Buffer) ([]TemplateRecord, error) { | ||
records := make([]TemplateRecord, 0) | ||
var err error | ||
for payload.Len() >= 4 { | ||
|
@@ -117,6 +118,38 @@ func DecodeTemplateSet(payload *bytes.Buffer) ([]TemplateRecord, error) { | |
return records, nil | ||
} | ||
|
||
func DecodeIPFIXTemplateSet(payload *bytes.Buffer) ([]TemplateRecord, error) { | ||
records := make([]TemplateRecord, 0) | ||
var err error | ||
for payload.Len() >= 4 { | ||
templateRecord := TemplateRecord{} | ||
err = utils.BinaryDecoder(payload, &templateRecord.TemplateId, &templateRecord.FieldCount) | ||
if err != nil { | ||
break | ||
} | ||
|
||
if int(templateRecord.FieldCount) < 0 { | ||
return records, NewErrorDecodingNetFlow("Error decoding TemplateSet: zero count.") | ||
} | ||
|
||
fields := make([]Field, int(templateRecord.FieldCount)) | ||
for i := 0; i < int(templateRecord.FieldCount); i++ { | ||
field := Field{} | ||
err = utils.BinaryDecoder(payload, &field) | ||
if field.Type > math.MaxInt16 { | ||
field.Type = field.Type - math.MaxInt16 | ||
// Skip Enterprise Number field - rfc7011#section-3.2 | ||
_ = payload.Next(4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Skipping the 4 bytes fixes the parsing, but I think it needs to track that this was a PEN field inside the |
||
} | ||
fields[i] = field | ||
} | ||
templateRecord.Fields = fields | ||
records = append(records, templateRecord) | ||
} | ||
|
||
return records, nil | ||
} | ||
|
||
func GetTemplateSize(template []Field) int { | ||
sum := 0 | ||
for _, templateField := range template { | ||
|
@@ -347,7 +380,7 @@ func DecodeMessage(payload *bytes.Buffer, templates NetFlowTemplateSystem) (inte | |
|
||
if fsheader.Id == 0 && version == 9 { | ||
templateReader := bytes.NewBuffer(payload.Next(nextrelpos)) | ||
records, err := DecodeTemplateSet(templateReader) | ||
records, err := DecodeNFv9TemplateSet(templateReader) | ||
if err != nil { | ||
return returnItem, err | ||
} | ||
|
@@ -384,7 +417,7 @@ func DecodeMessage(payload *bytes.Buffer, templates NetFlowTemplateSystem) (inte | |
|
||
} else if fsheader.Id == 2 && version == 10 { | ||
templateReader := bytes.NewBuffer(payload.Next(nextrelpos)) | ||
records, err := DecodeTemplateSet(templateReader) | ||
records, err := DecodeIPFIXTemplateSet(templateReader) | ||
if err != nil { | ||
return returnItem, err | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is off by one, you need
Because:
needs to turn into just '1', so you need to subtract
not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, will update. (could've sworn it was in my fork)