Skip to content

Commit 49bfc11

Browse files
committed
new uint conversion func
1 parent 24f358e commit 49bfc11

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

pkg/pipeline/transform/transform_network.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,8 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo
143143
}
144144
case api.NetworkDecodeTCPFlags:
145145
if anyFlags, ok := outputEntry[rule.DecodeTCPFlags.Input]; ok && anyFlags != nil {
146-
if flags, ok := anyFlags.(uint16); ok {
147-
flags := util.DecodeTCPFlagsU16(flags)
148-
outputEntry[rule.DecodeTCPFlags.Output] = flags
149-
} else if flags, ok := anyFlags.(uint32); ok {
150-
flags := util.DecodeTCPFlagsU32(flags)
146+
if flags, err := util.ConvertToUint(anyFlags); err == nil {
147+
flags := util.DecodeTCPFlags(flags)
151148
outputEntry[rule.DecodeTCPFlags.Output] = flags
152149
}
153150
}

pkg/utils/convert.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ func ConvertToInt(unk interface{}) (int, error) {
217217
}
218218
}
219219

220+
func ConvertToUint(unk interface{}) (uint, error) {
221+
switch i := unk.(type) {
222+
case uint64:
223+
return uint(i), nil
224+
case uint32:
225+
return uint(i), nil
226+
case uint16:
227+
return uint(i), nil
228+
case uint:
229+
return uint(i), nil
230+
default:
231+
return 0, fmt.Errorf("can't convert %v to uint", i)
232+
}
233+
}
234+
220235
func ConvertToBool(unk interface{}) (bool, error) {
221236
switch i := unk.(type) {
222237
case string:

pkg/utils/tcp_flags.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
package utils
22

33
type tcpFlag struct {
4-
value16 uint16
5-
value32 uint32
6-
name string
4+
value uint
5+
name string
76
}
87

98
var tcpFlags = []tcpFlag{
10-
{value16: 1, value32: 1, name: "FIN"},
11-
{value16: 2, value32: 2, name: "SYN"},
12-
{value16: 4, value32: 4, name: "RST"},
13-
{value16: 8, value32: 8, name: "PSH"},
14-
{value16: 16, value32: 16, name: "ACK"},
15-
{value16: 32, value32: 32, name: "URG"},
16-
{value16: 64, value32: 64, name: "ECE"},
17-
{value16: 128, value32: 128, name: "CWR"},
18-
{value16: 256, value32: 256, name: "SYN_ACK"},
19-
{value16: 512, value32: 512, name: "FIN_ACK"},
20-
{value16: 1024, value32: 1024, name: "RST_ACK"},
9+
{value: 1, name: "FIN"},
10+
{value: 2, name: "SYN"},
11+
{value: 4, name: "RST"},
12+
{value: 8, name: "PSH"},
13+
{value: 16, name: "ACK"},
14+
{value: 32, name: "URG"},
15+
{value: 64, name: "ECE"},
16+
{value: 128, name: "CWR"},
17+
{value: 256, name: "SYN_ACK"},
18+
{value: 512, name: "FIN_ACK"},
19+
{value: 1024, name: "RST_ACK"},
2120
}
2221

23-
func DecodeTCPFlagsU16(bitfield uint16) []string {
22+
func DecodeTCPFlags(bitfield uint) []string {
2423
var values []string
2524
for _, flag := range tcpFlags {
26-
if bitfield&flag.value16 != 0 {
27-
values = append(values, flag.name)
28-
}
29-
}
30-
return values
31-
}
32-
33-
func DecodeTCPFlagsU32(bitfield uint32) []string {
34-
var values []string
35-
for _, flag := range tcpFlags {
36-
if bitfield&flag.value32 != 0 {
25+
if bitfield&flag.value != 0 {
3726
values = append(values, flag.name)
3827
}
3928
}

pkg/utils/tcp_flags_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
)
88

99
func TestDecodeFlags(t *testing.T) {
10-
flags528 := DecodeTCPFlagsU16(528)
10+
flags528 := DecodeTCPFlags(528)
1111
assert.Equal(t, []string{"ACK", "FIN_ACK"}, flags528)
1212

13-
flags256 := DecodeTCPFlagsU32(256)
13+
flags256 := DecodeTCPFlags(256)
1414
assert.Equal(t, []string{"SYN_ACK"}, flags256)
1515

16-
flags666 := DecodeTCPFlagsU16(666)
16+
flags666 := DecodeTCPFlags(666)
1717
assert.Equal(t, []string{"SYN", "PSH", "ACK", "CWR", "FIN_ACK"}, flags666)
1818
}

0 commit comments

Comments
 (0)