From 3750040174e5b1749a356382935bae2b113868fb Mon Sep 17 00:00:00 2001 From: nullswan Date: Sun, 22 Sep 2024 13:27:02 +0200 Subject: [PATCH] feat(internal/bpf): enhance NetworkEvent with enums for direction and protocol --- internal/bpf/event.go | 32 ++++++++++++++++++++++++++++++-- internal/bpf/event_processor.go | 4 +++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/internal/bpf/event.go b/internal/bpf/event.go index d50c20b..525552d 100644 --- a/internal/bpf/event.go +++ b/internal/bpf/event.go @@ -27,6 +27,34 @@ type NetworkEvent struct { Sport uint16 Dport uint16 - Direction uint8 - Protocol uint8 + Direction NetworkEventDirection + Protocol NetworkEventProtocol +} + +type NetworkEventDirection uint8 + +const ( + NetworkEventDirectionInbound NetworkEventDirection = 0 + NetworkEventDirectionOutbound NetworkEventDirection = 1 +) + +func (d NetworkEventDirection) String() string { + if d == NetworkEventDirectionInbound { + return "inbound" + } + return "outbound" +} + +type NetworkEventProtocol uint8 + +const ( + NetworkEventProtocolTCP NetworkEventProtocol = 6 + NetworkEventProtocolUDP NetworkEventProtocol = 17 +) + +func (p NetworkEventProtocol) String() string { + if p == NetworkEventProtocolTCP { + return "tcp" + } + return "udp" } diff --git a/internal/bpf/event_processor.go b/internal/bpf/event_processor.go index 7a5635b..8d67b50 100644 --- a/internal/bpf/event_processor.go +++ b/internal/bpf/event_processor.go @@ -35,7 +35,9 @@ func ProcessNetworkEvent( Debug("Received network event") } - if event.Protocol == 17 && event.Direction == 1 && event.Dport == 53 { + if event.Protocol == NetworkEventProtocolUDP && + event.Direction == NetworkEventDirectionOutbound && + event.Dport == 53 { metrics.DNSQueryCounter.WithLabelValues(container).Inc() }