Skip to content

Commit

Permalink
decoders: do not stop reading sets (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
lspgn authored Dec 9, 2023
1 parent 1a6d8c9 commit 4f053d3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions decoders/netflow/ipfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,9 @@ func (p IPFIXPacket) String() string {
case DataFlowSet:
str += fmt.Sprintf(" - DataFlowSet %v:\n", i)
str += flowSet.String(IPFIXTypeToString)
case RawFlowSet:
str += fmt.Sprintf(" - RawFlowSet %v:\n", i)
str += flowSet.String()
case OptionsDataFlowSet:
str += fmt.Sprintf(" - OptionsDataFlowSet %v:\n", i)
str += flowSet.String(IPFIXTypeToString, IPFIXTypeToString)
Expand Down
15 changes: 12 additions & 3 deletions decoders/netflow/netflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package netflow
import (
"bytes"
"encoding/binary"
"errors"
"fmt"

"github.com/netsampler/goflow2/v2/decoders/utils"
Expand Down Expand Up @@ -286,10 +287,13 @@ func DecodeMessageCommon(payload *bytes.Buffer, templates NetFlowTemplateSystem,
var read int
startSize := payload.Len()
for i := 0; ((i < int(size) && version == 9) || (uint16(read) < size && version == 10)) && payload.Len() > 0; i++ {
if flowSet, err := DecodeMessageCommonFlowSet(payload, templates, obsDomainId, version); err != nil {
return flowSets, err
if flowSet, lerr := DecodeMessageCommonFlowSet(payload, templates, obsDomainId, version); lerr != nil && !errors.Is(lerr, ErrorTemplateNotFound) {
return flowSets, lerr
} else {
flowSets = append(flowSets, flowSet)
if lerr != nil {
err = errors.Join(err, lerr)
}
}
read = startSize - payload.Len()
}
Expand Down Expand Up @@ -392,7 +396,12 @@ func DecodeMessageCommonFlowSet(payload *bytes.Buffer, templates NetFlowTemplate
}

} else if fsheader.Id >= 256 {
dataReader := bytes.NewBuffer(payload.Next(nextrelpos))
rawfs := RawFlowSet{
FlowSetHeader: fsheader,
Records: payload.Next(nextrelpos),
}
flowSet = rawfs
dataReader := bytes.NewBuffer(rawfs.Records)

if templates == nil {
return flowSet, &FlowError{version, "Templates", obsDomainId, fsheader.Id, fmt.Errorf("No templates")}
Expand Down
3 changes: 3 additions & 0 deletions decoders/netflow/nfv9.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ func (p NFv9Packet) String() string {
case DataFlowSet:
str += fmt.Sprintf(" - DataFlowSet %v:\n", i)
str += flowSet.String(NFv9TypeToString)
case RawFlowSet:
str += fmt.Sprintf(" - RawFlowSet %v:\n", i)
str += flowSet.String()
case OptionsDataFlowSet:
str += fmt.Sprintf(" - OptionsDataFlowSet %v:\n", i)
str += flowSet.String(NFv9TypeToString, NFv9ScopeToString)
Expand Down
15 changes: 15 additions & 0 deletions decoders/netflow/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ type DataFlowSet struct {
Records []DataRecord `json:"records"`
}

// RawFlowSet is a a set that could not be decoded due to the absence of a template
type RawFlowSet struct {
FlowSetHeader

Records []byte `json:"records"`
}

type OptionsDataFlowSet struct {
FlowSetHeader

Expand Down Expand Up @@ -98,6 +105,14 @@ type DataField struct {
//Value []byte
}

func (flowSet RawFlowSet) String() string {
str := fmt.Sprintf(" Id %v\n", flowSet.Id)
str += fmt.Sprintf(" Length: %v\n", len(flowSet.Records))
str += fmt.Sprintf(" Records: %v\n", flowSet.Records)

return str
}

func (flowSet OptionsDataFlowSet) String(TypeToString func(uint16) string, ScopeToString func(uint16) string) string {
str := fmt.Sprintf(" Id %v\n", flowSet.Id)
str += fmt.Sprintf(" Length: %v\n", flowSet.Length)
Expand Down

0 comments on commit 4f053d3

Please sign in to comment.