Skip to content

Commit

Permalink
fix segfaults in sflow plugin by checking if protocol headers are set (
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoessbauer authored Mar 17, 2021
1 parent 13a4657 commit 927612a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
26 changes: 22 additions & 4 deletions plugins/inputs/sflow/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,22 @@ type RawPacketHeaderFlowData struct {
}

func (h RawPacketHeaderFlowData) GetTags() map[string]string {
t := h.Header.GetTags()
var t map[string]string
if h.Header != nil {
t = h.Header.GetTags()
} else {
t = map[string]string{}
}
t["header_protocol"] = HeaderProtocolMap[h.HeaderProtocol]
return t
}
func (h RawPacketHeaderFlowData) GetFields() map[string]interface{} {
f := h.Header.GetFields()
var f map[string]interface{}
if h.Header != nil {
f = h.Header.GetFields()
} else {
f = map[string]interface{}{}
}
f["bytes"] = h.Bytes
f["frame_length"] = h.FrameLength
f["header_length"] = h.HeaderLength
Expand All @@ -143,14 +153,22 @@ type EthHeader struct {
}

func (h EthHeader) GetTags() map[string]string {
t := h.IPHeader.GetTags()
var t map[string]string
if h.IPHeader != nil {
t = h.IPHeader.GetTags()
} else {
t = map[string]string{}
}
t["src_mac"] = net.HardwareAddr(h.SourceMAC[:]).String()
t["dst_mac"] = net.HardwareAddr(h.DestinationMAC[:]).String()
t["ether_type"] = h.EtherType
return t
}
func (h EthHeader) GetFields() map[string]interface{} {
return h.IPHeader.GetFields()
if h.IPHeader != nil {
return h.IPHeader.GetFields()
}
return map[string]interface{}{}
}

type ProtocolHeader ContainsMetricData
Expand Down
43 changes: 43 additions & 0 deletions plugins/inputs/sflow/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sflow

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRawPacketHeaderFlowData(t *testing.T) {
h := RawPacketHeaderFlowData{
HeaderProtocol: HeaderProtocolTypeEthernetISO88023,
FrameLength: 64,
Bytes: 64,
StrippedOctets: 0,
HeaderLength: 0,
Header: nil,
}
tags := h.GetTags()
fields := h.GetFields()

require.NotNil(t, fields)
require.NotNil(t, tags)
require.Contains(t, tags, "header_protocol")
require.Equal(t, 1, len(tags))
}

// process a raw ethernet packet without any encapsulated protocol
func TestEthHeader(t *testing.T) {
h := EthHeader{
DestinationMAC: [6]byte{0xca, 0xff, 0xee, 0xff, 0xe, 0x0},
SourceMAC: [6]byte{0xde, 0xad, 0xbe, 0xef, 0x0, 0x0},
TagProtocolIdentifier: 0x88B5, // IEEE Std 802 - Local Experimental Ethertype
TagControlInformation: 0,
EtherTypeCode: 0,
EtherType: "",
IPHeader: nil,
}
tags := h.GetTags()
fields := h.GetFields()

require.NotNil(t, fields)
require.NotNil(t, tags)
}

0 comments on commit 927612a

Please sign in to comment.