This repository was archived by the owner on Feb 15, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,13 @@ import (
9
9
bwmessage "github.com/bwNetFlow/protobuf/go"
10
10
)
11
11
12
+ var (
13
+ // Masks the last byte.
14
+ IPv4Mask = net .IPv4Mask (0 , 0 , 0 , 255 )
15
+ // Masks the last 64 bits.
16
+ IPv6Mask = net .CIDRMask (64 , 128 )
17
+ )
18
+
12
19
// Reducer stores the reduction specific configuration.
13
20
type Reducer struct {
14
21
// Fields which will be kept
@@ -45,13 +52,13 @@ func (r *Reducer) Process(msg *bwmessage.FlowMessage) *bwmessage.FlowMessage {
45
52
if reduced_field .Type () == reflect .TypeOf ([]uint8 {}) {
46
53
raw := reduced_field .Interface ().([]uint8 )
47
54
address := net .IP (raw )
48
- raw [ len ( raw ) - 1 ] = 0
49
- if address .To4 () = = nil {
50
- for i := 2 ; i <= 8 ; i ++ {
51
- raw [ len ( raw ) - i ] = 0
52
- }
55
+ var maskedAddress net. IP
56
+ if v4Addr := address .To4 (); v4Addr ! = nil {
57
+ maskedAddress = v4Addr . Mask ( IPv4Mask )
58
+ } else {
59
+ maskedAddress = address . Mask ( IPv6Mask )
53
60
}
54
- reduced_field .Set (reflect .ValueOf (raw ))
61
+ reduced_field .Set (reflect .ValueOf (maskedAddress ))
55
62
} else {
56
63
log .Printf ("Field '%s' has type '%s'. Anonymization is only supported for IP types." , fieldname , reduced_field .Type ())
57
64
}
You can’t perform that action at this time.
0 commit comments