Skip to content
This repository was archived by the owner on Feb 15, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions reducer/reducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
bwmessage "github.com/bwNetFlow/protobuf/go"
)

var (
// Masks the last byte.
IPv4Mask = net.IPv4Mask(0, 0, 0, 255)
// Masks the last 64 bits.
IPv6Mask = net.CIDRMask(64, 128)
)

// Reducer stores the reduction specific configuration.
type Reducer struct {
// Fields which will be kept
Expand Down Expand Up @@ -45,13 +52,13 @@ func (r *Reducer) Process(msg *bwmessage.FlowMessage) *bwmessage.FlowMessage {
if reduced_field.Type() == reflect.TypeOf([]uint8{}) {
raw := reduced_field.Interface().([]uint8)
address := net.IP(raw)
raw[len(raw)-1] = 0
if address.To4() == nil {
for i := 2; i <= 8; i++ {
raw[len(raw)-i] = 0
}
var maskedAddress net.IP
if v4Addr := address.To4(); v4Addr != nil {
maskedAddress = v4Addr.Mask(IPv4Mask)
} else {
maskedAddress = address.Mask(IPv6Mask)
}
reduced_field.Set(reflect.ValueOf(raw))
reduced_field.Set(reflect.ValueOf(maskedAddress))
} else {
log.Printf("Field '%s' has type '%s'. Anonymization is only supported for IP types.", fieldname, reduced_field.Type())
}
Expand Down