Skip to content

Commit a61288e

Browse files
vincentbernatlspgn
andauthored
decoders: fix sflow parsing of IP and MAC addresses (#261)
Due to IP and MAC addresses being a non-standard type, utils.BinaryRead was not able to decode them. Move these two types inside utils.go and teach BinaryRead to use them. Co-authored-by: lspgn <lspgn@users.noreply.github.com>
1 parent ff4ddca commit a61288e

4 files changed

Lines changed: 65 additions & 56 deletions

File tree

decoders/sflow/datastructure.go

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package sflow
22

3-
import (
4-
"fmt"
5-
"net"
6-
)
3+
import "github.com/netsampler/goflow2/v2/decoders/utils"
74

85
type SampledHeader struct {
96
Protocol uint32 `json:"protocol"`
@@ -14,26 +11,20 @@ type SampledHeader struct {
1411
}
1512

1613
type SampledEthernet struct {
17-
Length uint32 `json:"length"`
18-
SrcMac MacAddress `json:"src-mac"`
19-
DstMac MacAddress `json:"dst-mac"`
20-
EthType uint32 `json:"eth-type"`
14+
Length uint32 `json:"length"`
15+
SrcMac utils.MacAddress `json:"src-mac"`
16+
DstMac utils.MacAddress `json:"dst-mac"`
17+
EthType uint32 `json:"eth-type"`
2118
}
2219

2320
type SampledIPBase struct {
24-
Length uint32 `json:"length"`
25-
Protocol uint32 `json:"protocol"`
26-
SrcIP IPAddress `json:"src-ip"`
27-
DstIP IPAddress `json:"dst-ip"`
28-
SrcPort uint32 `json:"src-port"`
29-
DstPort uint32 `json:"dst-port"`
30-
TcpFlags uint32 `json:"tcp-flags"`
31-
}
32-
33-
type MacAddress []byte // purely for the formatting purpose
34-
35-
func (s *MacAddress) MarshalJSON() ([]byte, error) {
36-
return []byte(fmt.Sprintf("\"%s\"", net.HardwareAddr([]byte(*s)).String())), nil
21+
Length uint32 `json:"length"`
22+
Protocol uint32 `json:"protocol"`
23+
SrcIP utils.IPAddress `json:"src-ip"`
24+
DstIP utils.IPAddress `json:"dst-ip"`
25+
SrcPort uint32 `json:"src-port"`
26+
DstPort uint32 `json:"dst-port"`
27+
TcpFlags uint32 `json:"tcp-flags"`
3728
}
3829

3930
type SampledIPv4 struct {
@@ -54,25 +45,25 @@ type ExtendedSwitch struct {
5445
}
5546

5647
type ExtendedRouter struct {
57-
NextHopIPVersion uint32 `json:"next-hop-ip-version"`
58-
NextHop IPAddress `json:"next-hop"`
59-
SrcMaskLen uint32 `json:"src-mask-len"`
60-
DstMaskLen uint32 `json:"dst-mask-len"`
48+
NextHopIPVersion uint32 `json:"next-hop-ip-version"`
49+
NextHop utils.IPAddress `json:"next-hop"`
50+
SrcMaskLen uint32 `json:"src-mask-len"`
51+
DstMaskLen uint32 `json:"dst-mask-len"`
6152
}
6253

6354
type ExtendedGateway struct {
64-
NextHopIPVersion uint32 `json:"next-hop-ip-version"`
65-
NextHop IPAddress `json:"next-hop"`
66-
AS uint32 `json:"as"`
67-
SrcAS uint32 `json:"src-as"`
68-
SrcPeerAS uint32 `json:"src-peer-as"`
69-
ASDestinations uint32 `json:"as-destinations"`
70-
ASPathType uint32 `json:"as-path-type"`
71-
ASPathLength uint32 `json:"as-path-length"`
72-
ASPath []uint32 `json:"as-path"`
73-
CommunitiesLength uint32 `json:"communities-length"`
74-
Communities []uint32 `json:"communities"`
75-
LocalPref uint32 `json:"local-pref"`
55+
NextHopIPVersion uint32 `json:"next-hop-ip-version"`
56+
NextHop utils.IPAddress `json:"next-hop"`
57+
AS uint32 `json:"as"`
58+
SrcAS uint32 `json:"src-as"`
59+
SrcPeerAS uint32 `json:"src-peer-as"`
60+
ASDestinations uint32 `json:"as-destinations"`
61+
ASPathType uint32 `json:"as-path-type"`
62+
ASPathLength uint32 `json:"as-path-length"`
63+
ASPath []uint32 `json:"as-path"`
64+
CommunitiesLength uint32 `json:"communities-length"`
65+
Communities []uint32 `json:"communities"`
66+
LocalPref uint32 `json:"local-pref"`
7667
}
7768

7869
type IfCounters struct {

decoders/sflow/packet.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
package sflow
22

3-
import (
4-
"fmt"
5-
"net/netip"
6-
)
3+
import "github.com/netsampler/goflow2/v2/decoders/utils"
74

85
type Packet struct {
9-
Version uint32 `json:"version"`
10-
IPVersion uint32 `json:"ip-version"`
11-
AgentIP IPAddress `json:"agent-ip"`
12-
SubAgentId uint32 `json:"sub-agent-id"`
13-
SequenceNumber uint32 `json:"sequence-number"`
14-
Uptime uint32 `json:"uptime"`
15-
SamplesCount uint32 `json:"samples-count"`
16-
Samples []interface{} `json:"samples"`
17-
}
18-
19-
type IPAddress []byte // purely for the formatting purpose
20-
21-
func (s IPAddress) MarshalJSON() ([]byte, error) {
22-
ip, _ := netip.AddrFromSlice([]byte(s))
23-
return []byte(fmt.Sprintf("\"%s\"", ip.String())), nil
6+
Version uint32 `json:"version"`
7+
IPVersion uint32 `json:"ip-version"`
8+
AgentIP utils.IPAddress `json:"agent-ip"`
9+
SubAgentId uint32 `json:"sub-agent-id"`
10+
SequenceNumber uint32 `json:"sequence-number"`
11+
Uptime uint32 `json:"uptime"`
12+
SamplesCount uint32 `json:"samples-count"`
13+
Samples []interface{} `json:"samples"`
2414
}
2515

2616
type SampleHeader struct {

decoders/utils/types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/netip"
7+
)
8+
9+
type MacAddress []byte // purely for the formatting purpose
10+
11+
func (s *MacAddress) MarshalJSON() ([]byte, error) {
12+
return []byte(fmt.Sprintf("\"%s\"", net.HardwareAddr([]byte(*s)).String())), nil
13+
}
14+
15+
type IPAddress []byte // purely for the formatting purpose
16+
17+
func (s IPAddress) MarshalJSON() ([]byte, error) {
18+
ip, _ := netip.AddrFromSlice([]byte(s))
19+
return []byte(fmt.Sprintf("\"%s\"", ip.String())), nil
20+
}

decoders/utils/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func BinaryRead(payload BytesBuffer, order binary.ByteOrder, data any) error {
5858
}
5959
case []uint8:
6060
copy(data, bs)
61+
case IPAddress:
62+
copy(data, bs)
63+
case MacAddress:
64+
copy(data, bs)
6165
case []int16:
6266
for i := range data {
6367
data[i] = int16(order.Uint16(bs[2*i:]))
@@ -105,6 +109,10 @@ func intDataSize(data any) int {
105109
return len(data)
106110
case []uint8:
107111
return len(data)
112+
case IPAddress:
113+
return len(data)
114+
case MacAddress:
115+
return len(data)
108116
case int16, uint16, *int16, *uint16:
109117
return 2
110118
case []int16:

0 commit comments

Comments
 (0)