-
Notifications
You must be signed in to change notification settings - Fork 176
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
Fix: Decoding of IPFIX templates with Enterprise Number field #68
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} | ||
|
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)