Skip to content

Commit

Permalink
Merge branch 'Edgio:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shift authored May 30, 2023
2 parents 0f034b4 + ab973ef commit 413e5b0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sflow/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var (
errNoneEnterpriseStandard = errors.New("the enterprise is not standard sflow data")
errDataLengthUnknown = errors.New("the sflow data length is unknown")
errSFVersionNotSupport = errors.New("the sflow version doesn't support")
errSampleLengthInvalid = errors.New("the sflow sample length is invalid")
)

// NewSFDecoder constructs new sflow decoder
Expand Down Expand Up @@ -112,6 +113,8 @@ func (d *SFDecoder) SFDecode() (*SFDatagram, error) {
continue
}

var offsetBefore, _ = d.reader.Seek(0, 1)

switch sfTypeFormat {
case DataFlowSample:
d, err := decodeFlowSample(d.reader)
Expand All @@ -129,11 +132,32 @@ func (d *SFDecoder) SFDecode() (*SFDatagram, error) {
d.reader.Seek(int64(sfDataLength), 1)
}

err = d.sfMoveToEndOfSample(sfDataLength, offsetBefore)
if err != nil {
return datagram, err
}

}

return datagram, nil
}

func (d *SFDecoder) sfMoveToEndOfSample(sfDataLength uint32, offsetBefore int64) error {
var offsetAfter, _ = d.reader.Seek(0, 1)
var bytesRead = offsetAfter - offsetBefore
var byteUntilEndOfSample = int64(sfDataLength) - bytesRead
if byteUntilEndOfSample < 0 {
return errSampleLengthInvalid
}
if byteUntilEndOfSample > 0 {
var _, err = d.reader.Seek(byteUntilEndOfSample, 1)
if err != nil {
return err
}
}
return nil
}

func (d *SFDecoder) sfHeaderDecode() (*SFDatagram, error) {
var (
datagram = &SFDatagram{}
Expand Down
103 changes: 103 additions & 0 deletions sflow/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,92 @@ var TestsFlowRawPacket = []byte{0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xea, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0xce, 0x48,
0xd3, 0x16, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x15}

var TestsFlowRawPacketWithPaddingAfterSample = []byte{
0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 0x02, 0xf6, 0x00, 0x00, 0x00, 0x01,
0x36, 0x2d, 0xc7, 0x73, 0x37, 0xc5, 0x77, 0xb4, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0xd0, 0x38, 0x78, 0x45, 0x37, 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x04, 0x00,
0xb0, 0x47, 0x04, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x05, 0x56, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x5e, 0x00,
0x67, 0x20, 0x90, 0xe2, 0xba, 0x39, 0x8e, 0x27, 0x08, 0x00, 0x45, 0x00, 0x05, 0x40, 0x00, 0x00,
0x40, 0x00, 0x1e, 0x11, 0x23, 0xd5, 0xc0, 0xa8, 0x2c, 0x0f, 0xe0, 0x00, 0x67, 0x20, 0xd3, 0xdb,
0x07, 0xd1, 0x05, 0x2c, 0x84, 0x51, 0x47, 0x01, 0x9a, 0x17, 0xc1, 0xbd, 0x62, 0xd6, 0xaf, 0xb2,
0x5c, 0x85, 0xf5, 0x03, 0x6b, 0x5b, 0x74, 0x66, 0x80, 0xe2, 0xa9, 0xb3, 0x21, 0x29, 0x64, 0x7c,
0xe7, 0xb4, 0xe3, 0x6c, 0x82, 0xc1, 0xb3, 0xf7, 0x19, 0x32, 0xd1, 0xb2, 0x52, 0xb0, 0xc3, 0x92,
0x90, 0x07, 0x21, 0xc6, 0x3d, 0x2c, 0x74, 0xe2, 0x26, 0x3a, 0x0e, 0x58, 0xc0, 0xaa, 0x16, 0xb4,
0xf1, 0xc4, 0x3b, 0xbb, 0x4f, 0xfb, 0x9e, 0x09, 0x45, 0x64, 0x85, 0xdd, 0x1e, 0xfe, 0x42, 0x6a,
0x57, 0x5e, 0xeb, 0x8b, 0x79, 0x92, 0xb6, 0x94, 0xbc, 0x05, 0x54, 0x83, 0x00, 0x00, 0x03, 0xe9,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xbc,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x1c, 0x90, 0x4d, 0xe8, 0x35,
0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x04, 0x00, 0x76, 0xb3, 0xaa, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x83, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0xc0, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x80, 0x60, 0x9c, 0x9f, 0x43, 0xf0, 0x62, 0xe0, 0xac, 0xf1, 0x1c, 0xcf, 0xed,
0x08, 0x00, 0x45, 0x28, 0x05, 0xaa, 0x28, 0x4f, 0x40, 0x00, 0x58, 0x06, 0xd2, 0xed, 0x1f, 0x0d,
0x42, 0x0e, 0x48, 0x09, 0x78, 0xc5, 0x01, 0xbb, 0xdd, 0x4a, 0xda, 0x0e, 0xe5, 0xe6, 0xea, 0x42,
0x35, 0x2e, 0x80, 0x10, 0x00, 0xbd, 0xef, 0x4e, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xe7, 0x30,
0x3a, 0xf9, 0x20, 0x01, 0xb3, 0x30, 0xa6, 0xc6, 0xe7, 0x9a, 0x61, 0x09, 0x3d, 0x2e, 0xb2, 0x8f,
0x99, 0x0a, 0xeb, 0x4c, 0x61, 0x9a, 0xd0, 0xb9, 0x24, 0x8c, 0x29, 0x46, 0x4f, 0xcd, 0xc3, 0x9e,
0x09, 0x6a, 0x33, 0x85, 0xba, 0xcb, 0x26, 0xdf, 0x14, 0x89, 0x97, 0x15, 0xc6, 0x82, 0xa7, 0x92,
0x2c, 0x99, 0xdb, 0xfb, 0x17, 0xb3, 0x25, 0xa7, 0xdc, 0xf5, 0x8e, 0xfd, 0x39, 0x30, 0x3f, 0x7e,
0x55, 0x36, 0x03, 0x4e, 0x00, 0x00, 0x03, 0xe9, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x69,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xd2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xea,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xeb, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7d, 0xa0, 0x00, 0x00, 0x00, 0xae, 0x00, 0x00, 0x00, 0xae,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xd0,
0x1a, 0xf2, 0x91, 0x87, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x1a, 0x8c, 0xd9, 0xd0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0x56,
0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x5e, 0x01, 0x73, 0x24, 0x00, 0x21,
0x9b, 0x97, 0xd7, 0xd3, 0x08, 0x00, 0x45, 0x00, 0x05, 0x40, 0xe6, 0xf2, 0x40, 0x00, 0x40, 0x11,
0xee, 0x3f, 0x0a, 0x00, 0x02, 0x55, 0xe1, 0x01, 0x73, 0x24, 0xd8, 0x04, 0x07, 0xd1, 0x05, 0x2c,
0xe7, 0xf8, 0x47, 0x00, 0x6e, 0x16, 0x4d, 0xa6, 0x49, 0x3e, 0x43, 0xff, 0xde, 0x92, 0x27, 0x88,
0x98, 0x79, 0xe9, 0xfb, 0x9f, 0x6e, 0xc3, 0x84, 0x12, 0xd0, 0x4e, 0x18, 0x4b, 0x4e, 0xf2, 0xc5,
0x0b, 0xfb, 0x85, 0xf4, 0x99, 0x15, 0x66, 0xe7, 0x36, 0x23, 0x6a, 0x7c, 0xe2, 0x3a, 0x13, 0x20,
0x00, 0x2b, 0xc1, 0xde, 0x3b, 0xe0, 0xa4, 0x45, 0xdd, 0x4d, 0xc1, 0x87, 0x4f, 0x60, 0xae, 0x2f,
0x87, 0x97, 0x35, 0x2c, 0x64, 0xa0, 0xe2, 0xd7, 0xb0, 0x1b, 0x30, 0xce, 0xaa, 0x37, 0xd4, 0x46,
0xaa, 0x69, 0x27, 0x27, 0xb9, 0x3f, 0xb3, 0x82, 0x00, 0x00, 0x03, 0xe9, 0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x03, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x90, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x1c, 0x38, 0x78, 0x45, 0x38, 0x00, 0x00, 0x00, 0x82,
0x00, 0x00, 0x04, 0x00, 0xb0, 0x47, 0x08, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82,
0x00, 0x00, 0x00, 0x83, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x90,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0xf2, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80,
0x60, 0x9c, 0x9f, 0x43, 0xf0, 0x62, 0x34, 0xdb, 0xfd, 0x04, 0x06, 0xc9, 0x08, 0x00, 0x45, 0x00,
0x05, 0xdc, 0x00, 0x02, 0x40, 0x00, 0x3f, 0x06, 0xe1, 0x74, 0x6c, 0xaf, 0x27, 0xa2, 0x48, 0x09,
0x78, 0x4b, 0x00, 0x50, 0xbf, 0x79, 0xb5, 0x97, 0x10, 0xc0, 0x25, 0x60, 0x3d, 0xfc, 0x80, 0x10,
0x08, 0x02, 0x38, 0x51, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x48, 0xd0, 0xe9, 0x69, 0x00, 0x24,
0x98, 0xe6, 0x6b, 0x08, 0x7f, 0xc6, 0x95, 0x2a, 0x22, 0x6a, 0xb6, 0x63, 0x81, 0x4c, 0x86, 0x5b,
0xa3, 0xb8, 0xc0, 0x06, 0x4d, 0x17, 0x10, 0x7b, 0x92, 0x84, 0x58, 0x1b, 0x1f, 0xba, 0x67, 0x1f,
0xe6, 0x5c, 0xce, 0xa7, 0x6f, 0xb0, 0x06, 0xc8, 0x33, 0xbb, 0x65, 0x6f, 0x2f, 0x4a, 0x24, 0x36,
0xa2, 0xfe, 0xf8, 0xa8, 0x85, 0xc5, 0xf3, 0xfa, 0x78, 0xff, 0xe7, 0x04, 0x83, 0xf7, 0xe1, 0xf5,
0x00, 0x00, 0x03, 0xe9, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0xd2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xea, 0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xeb, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7d, 0xa0, 0x00, 0x00, 0x00, 0xae, 0x00, 0x00, 0x00, 0xae, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x1c, 0x90, 0x4d, 0xe8, 0x36,
0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x04, 0x00, 0x76, 0xb3, 0xae, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0xf2, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x80, 0x60, 0x9c, 0x9f, 0x43, 0xf0, 0x62, 0xe0, 0xac, 0xf1, 0x1c, 0xcf, 0xed,
0x08, 0x00, 0x45, 0x48, 0x05, 0xdc, 0x97, 0xe0, 0x40, 0x00, 0x3c, 0x06, 0x53, 0x2d, 0x36, 0xc0,
0x5b, 0x4d, 0x48, 0x09, 0x73, 0xb0, 0x01, 0xbb, 0xcb, 0xa1, 0xa4, 0x5b, 0x9c, 0x18, 0x23, 0x60,
0x11, 0x4f, 0x80, 0x10, 0x00, 0x41, 0x2b, 0x11, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x3c, 0xdd,
0x89, 0x41, 0x12, 0x81, 0xa7, 0xb8, 0xbe, 0xd7, 0x0d, 0x7b, 0xc6, 0x75, 0xe1, 0xf2, 0x03, 0x93,
0xe3, 0x08, 0x17, 0xed, 0x4c, 0xda, 0x22, 0xef, 0xc1, 0xc3, 0x3d, 0x4e, 0x82, 0x11, 0xa0, 0xb5,
0x93, 0x0d, 0x61, 0x06, 0xef, 0xd0, 0x83, 0x0c, 0xaa, 0x70, 0x79, 0x0a, 0x8f, 0x52, 0x5b, 0x7c,
0x90, 0x0d, 0x19, 0x8b, 0x5f, 0xb5, 0x99, 0x73, 0x77, 0x60, 0x2c, 0x80, 0x28, 0x86, 0x64, 0xab,
0xd6, 0xcd, 0xf5, 0xe6, 0x00, 0x00, 0x03, 0xe9, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x69,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xea,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xeb, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01,
0x0a, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x7d, 0xa0, 0x00, 0x00, 0x00, 0xae, 0x00, 0x00, 0x00, 0xae,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

func TestSFHeaderDecode(t *testing.T) {
filter := []uint32{DataCounterSample}
reader := bytes.NewReader(TestsFlowRawPacket)
Expand Down Expand Up @@ -180,6 +266,23 @@ func TestSFDecode(t *testing.T) {
}
}

func TestSFDecodeWithPaddingAfterSample(t *testing.T) {
filter := []uint32{DataCounterSample}
reader := bytes.NewReader(TestsFlowRawPacketWithPaddingAfterSample)
d := NewSFDecoder(reader, filter)
datagram, err := d.SFDecode()
if err != nil {
t.Error("unexpected error", err)
}
if len(datagram.Samples) != 5 {
t.Error("expected samples## 5, got", len(datagram.Samples))
}
sample := datagram.Samples[4].(*FlowSample)
if sample.RecordsNo != 4 {
t.Error("expected RecordsNo 4, got ", sample.RecordsNo)
}
}

func TestDecodeSampleHeader(t *testing.T) {
filter := []uint32{DataCounterSample}
reader := bytes.NewReader(TestsFlowRawPacket)
Expand Down

0 comments on commit 413e5b0

Please sign in to comment.