Skip to content

Commit

Permalink
factor out IPsFromInterface function since we have it duplicated in t…
Browse files Browse the repository at this point in the history
…wo places now
  • Loading branch information
masonj5n committed Oct 3, 2023
1 parent 5a2772a commit b90e9df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 44 deletions.
23 changes: 1 addition & 22 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net"
"net/netip"
"time"

"github.com/cilium/ebpf/ringbuf"
Expand Down Expand Up @@ -185,27 +184,7 @@ func flowsAgent(cfg *Config,

case len(cfg.InterfaceIPs) > 0:
// configure ip interface filter
f, err := initIPInterfaceFilter(cfg.InterfaceIPs, func(ifaceName string) ([]netip.Addr, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return []netip.Addr{}, fmt.Errorf("error retrieving interface by name: %w", err)
}
addrs, err := iface.Addrs()
if err != nil {
return []netip.Addr{}, fmt.Errorf("error retrieving addresses from interface: %w", err)
}

interfaceAddrs := []netip.Addr{}
for _, addr := range addrs {
prefix, err := netip.ParsePrefix(addr.String())
if err != nil {
return []netip.Addr{}, fmt.Errorf("parsing given ip to netip.Addr: %w", err)
}
interfaceAddrs = append(interfaceAddrs, prefix.Addr())
}
return interfaceAddrs, nil

})
f, err := initIPInterfaceFilter(cfg.InterfaceIPs, IPsFromInterface)
if err != nil {
return nil, fmt.Errorf("configuring interface ip filter: %w", err)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/agent/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"fmt"
"net"
"net/netip"
"regexp"
"strings"
Expand All @@ -19,6 +20,29 @@ type ipInterfaceFilter struct {
ipsFromIface func(ifaceName string) ([]netip.Addr, error)
}

// Default function for getting the list of IPs configured
// for a specific network interface
func IPsFromInterface(ifaceName string) ([]netip.Addr, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return []netip.Addr{}, fmt.Errorf("error retrieving interface by name: %w", err)
}
addrs, err := iface.Addrs()
if err != nil {
return []netip.Addr{}, fmt.Errorf("error retrieving addresses from interface: %w", err)
}

interfaceAddrs := []netip.Addr{}
for _, addr := range addrs {
prefix, err := netip.ParsePrefix(addr.String())
if err != nil {
return []netip.Addr{}, fmt.Errorf("parsing given ip to netip.Addr: %w", err)
}
interfaceAddrs = append(interfaceAddrs, prefix.Addr())
}
return interfaceAddrs, nil
}

// initIPInterfaceFilter allows filtering network interfaces that are accepted/excluded by the user,
// according to the provided INTERFACE_IPS from the configuration. It allows interfaces where at least
// one of the provided CIDRs are associated with it.
Expand Down
23 changes: 1 addition & 22 deletions pkg/agent/packets_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net"
"net/netip"

"github.com/cilium/ebpf/perf"
"github.com/netobserv/gopipes/pkg/node"
Expand Down Expand Up @@ -88,27 +87,7 @@ func packetsAgent(cfg *Config,

case len(cfg.InterfaceIPs) > 0:
// configure ip interface filter
f, err := initIPInterfaceFilter(cfg.InterfaceIPs, func(ifaceName string) ([]netip.Addr, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return []netip.Addr{}, fmt.Errorf("error retrieving interface by name: %w", err)
}
addrs, err := iface.Addrs()
if err != nil {
return []netip.Addr{}, fmt.Errorf("error retrieving addresses from interface: %w", err)
}

interfaceAddrs := []netip.Addr{}
for _, addr := range addrs {
prefix, err := netip.ParsePrefix(addr.String())
if err != nil {
return []netip.Addr{}, fmt.Errorf("parsing given ip to netip.Addr: %w", err)
}
interfaceAddrs = append(interfaceAddrs, prefix.Addr())
}
return interfaceAddrs, nil

})
f, err := initIPInterfaceFilter(cfg.InterfaceIPs, IPsFromInterface)
if err != nil {
return nil, fmt.Errorf("configuring interface ip filter: %w", err)
}
Expand Down

0 comments on commit b90e9df

Please sign in to comment.