From 12c62b92ff2f63cb34dcb9c0555b96983e6aad94 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 13 Oct 2021 21:52:55 +0200 Subject: Make networking code reusable To prevent code duplication when creating new network backends move reusable code into a separate internal package. This allows all network backends to use the same code as long as they implement the new NetUtil interface. Signed-off-by: Paul Holzinger --- libpod/network/internal/util/interfaces.go | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 libpod/network/internal/util/interfaces.go (limited to 'libpod/network/internal/util/interfaces.go') diff --git a/libpod/network/internal/util/interfaces.go b/libpod/network/internal/util/interfaces.go new file mode 100644 index 000000000..20819f756 --- /dev/null +++ b/libpod/network/internal/util/interfaces.go @@ -0,0 +1,34 @@ +package util + +import "net" + +// getLiveNetworkSubnets returns a slice of subnets representing what the system +// has defined as network interfaces +func getLiveNetworkSubnets() ([]*net.IPNet, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return nil, err + } + nets := make([]*net.IPNet, 0, len(addrs)) + for _, address := range addrs { + _, n, err := net.ParseCIDR(address.String()) + if err != nil { + return nil, err + } + nets = append(nets, n) + } + return nets, nil +} + +// GetLiveNetworkNames returns a list of network interface names on the system +func GetLiveNetworkNames() ([]string, error) { + liveInterfaces, err := net.Interfaces() + if err != nil { + return nil, err + } + interfaceNames := make([]string, 0, len(liveInterfaces)) + for _, i := range liveInterfaces { + interfaceNames = append(interfaceNames, i.Name) + } + return interfaceNames, nil +} -- cgit v1.2.3-54-g00ecf