aboutsummaryrefslogtreecommitdiff
path: root/libpod/network/util
diff options
context:
space:
mode:
authorPaul Holzinger <pholzing@redhat.com>2021-12-17 14:46:15 +0100
committerPaul Holzinger <pholzing@redhat.com>2022-01-12 17:07:30 +0100
commit495884b3195de482dc610a2a002db7e053188a32 (patch)
tree2a6f23db066cd52aa366991b0b34d7b919368ddc /libpod/network/util
parent2cdab5d53923784e72020d70ee9375518f19f9b6 (diff)
downloadpodman-495884b3195de482dc610a2a002db7e053188a32.tar.gz
podman-495884b3195de482dc610a2a002db7e053188a32.tar.bz2
podman-495884b3195de482dc610a2a002db7e053188a32.zip
use libnetwork from c/common
The libpod/network packages were moved to c/common so that buildah can use it as well. To prevent duplication use it in podman as well and remove it from here. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'libpod/network/util')
-rw-r--r--libpod/network/util/filters.go79
-rw-r--r--libpod/network/util/ip.go56
-rw-r--r--libpod/network/util/ip_calc.go53
-rw-r--r--libpod/network/util/ip_test.go73
4 files changed, 0 insertions, 261 deletions
diff --git a/libpod/network/util/filters.go b/libpod/network/util/filters.go
deleted file mode 100644
index c3c80b352..000000000
--- a/libpod/network/util/filters.go
+++ /dev/null
@@ -1,79 +0,0 @@
-package util
-
-import (
- "strings"
-
- "github.com/containers/podman/v3/libpod/network/types"
- "github.com/containers/podman/v3/pkg/util"
- "github.com/pkg/errors"
-)
-
-func GenerateNetworkFilters(filters map[string][]string) ([]types.FilterFunc, error) {
- filterFuncs := make([]types.FilterFunc, 0, len(filters))
- for key, filterValues := range filters {
- filterFunc, err := createFilterFuncs(key, filterValues)
- if err != nil {
- return nil, err
- }
- filterFuncs = append(filterFuncs, filterFunc)
- }
- return filterFuncs, nil
-}
-
-func createFilterFuncs(key string, filterValues []string) (types.FilterFunc, error) {
- switch strings.ToLower(key) {
- case "name":
- // matches one name, regex allowed
- return func(net types.Network) bool {
- return util.StringMatchRegexSlice(net.Name, filterValues)
- }, nil
-
- case "driver":
- // matches network driver
- return func(net types.Network) bool {
- return util.StringInSlice(net.Driver, filterValues)
- }, nil
-
- case "id":
- // matches part of one id
- return func(net types.Network) bool {
- return util.StringMatchRegexSlice(net.ID, filterValues)
- }, nil
-
- // TODO: add dns enabled, internal filter
- }
- return createPruneFilterFuncs(key, filterValues)
-}
-
-func GenerateNetworkPruneFilters(filters map[string][]string) ([]types.FilterFunc, error) {
- filterFuncs := make([]types.FilterFunc, 0, len(filters))
- for key, filterValues := range filters {
- filterFunc, err := createPruneFilterFuncs(key, filterValues)
- if err != nil {
- return nil, err
- }
- filterFuncs = append(filterFuncs, filterFunc)
- }
- return filterFuncs, nil
-}
-
-func createPruneFilterFuncs(key string, filterValues []string) (types.FilterFunc, error) {
- switch strings.ToLower(key) {
- case "label":
- // matches all labels
- return func(net types.Network) bool {
- return util.MatchLabelFilters(filterValues, net.Labels)
- }, nil
-
- case "until":
- until, err := util.ComputeUntilTimestamp(filterValues)
- if err != nil {
- return nil, err
- }
- return func(net types.Network) bool {
- return net.Created.Before(until)
- }, nil
- default:
- return nil, errors.Errorf("invalid filter %q", key)
- }
-}
diff --git a/libpod/network/util/ip.go b/libpod/network/util/ip.go
deleted file mode 100644
index e82b4a781..000000000
--- a/libpod/network/util/ip.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package util
-
-import (
- "net"
-)
-
-// IsIPv6 returns true if netIP is IPv6.
-func IsIPv6(netIP net.IP) bool {
- return netIP != nil && netIP.To4() == nil
-}
-
-// IsIPv4 returns true if netIP is IPv4.
-func IsIPv4(netIP net.IP) bool {
- return netIP != nil && netIP.To4() != nil
-}
-
-// LastIPInSubnet gets the last IP in a subnet
-func LastIPInSubnet(addr *net.IPNet) (net.IP, error) { //nolint:interfacer
- // re-parse to ensure clean network address
- _, cidr, err := net.ParseCIDR(addr.String())
- if err != nil {
- return nil, err
- }
-
- ones, bits := cidr.Mask.Size()
- if ones == bits {
- return cidr.IP, nil
- }
- for i := range cidr.IP {
- cidr.IP[i] = cidr.IP[i] | ^cidr.Mask[i]
- }
- return cidr.IP, nil
-}
-
-// FirstIPInSubnet gets the first IP in a subnet
-func FirstIPInSubnet(addr *net.IPNet) (net.IP, error) { //nolint:interfacer
- // re-parse to ensure clean network address
- _, cidr, err := net.ParseCIDR(addr.String())
- if err != nil {
- return nil, err
- }
- ones, bits := cidr.Mask.Size()
- if ones == bits {
- return cidr.IP, nil
- }
- cidr.IP[len(cidr.IP)-1]++
- return cidr.IP, nil
-}
-
-// NormalizeIP will transform the given ip to the 4 byte len ipv4 if possible
-func NormalizeIP(ip *net.IP) {
- ipv4 := ip.To4()
- if ipv4 != nil {
- *ip = ipv4
- }
-}
diff --git a/libpod/network/util/ip_calc.go b/libpod/network/util/ip_calc.go
deleted file mode 100644
index a27ddf78b..000000000
--- a/libpod/network/util/ip_calc.go
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright 2015 CNI authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package util
-
-import (
- "math/big"
- "net"
-)
-
-// NextIP returns IP incremented by 1
-func NextIP(ip net.IP) net.IP {
- i := ipToInt(ip)
- return intToIP(i.Add(i, big.NewInt(1)))
-}
-
-// PrevIP returns IP decremented by 1
-func PrevIP(ip net.IP) net.IP {
- i := ipToInt(ip)
- return intToIP(i.Sub(i, big.NewInt(1)))
-}
-
-// Cmp compares two IPs, returning the usual ordering:
-// a < b : -1
-// a == b : 0
-// a > b : 1
-func Cmp(a, b net.IP) int {
- aa := ipToInt(a)
- bb := ipToInt(b)
- return aa.Cmp(bb)
-}
-
-func ipToInt(ip net.IP) *big.Int {
- if v := ip.To4(); v != nil {
- return big.NewInt(0).SetBytes(v)
- }
- return big.NewInt(0).SetBytes(ip.To16())
-}
-
-func intToIP(i *big.Int) net.IP {
- return net.IP(i.Bytes())
-}
diff --git a/libpod/network/util/ip_test.go b/libpod/network/util/ip_test.go
deleted file mode 100644
index 63ac555f0..000000000
--- a/libpod/network/util/ip_test.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package util
-
-import (
- "net"
- "testing"
-)
-
-func parseCIDR(n string) *net.IPNet {
- _, parsedNet, _ := net.ParseCIDR(n)
- return parsedNet
-}
-
-func TestFirstIPInSubnet(t *testing.T) {
- tests := []struct {
- name string
- args *net.IPNet
- want net.IP
- wantErr bool
- }{
- {"class b", parseCIDR("192.168.0.0/16"), net.ParseIP("192.168.0.1"), false},
- {"class c", parseCIDR("192.168.1.0/24"), net.ParseIP("192.168.1.1"), false},
- {"cidr /23", parseCIDR("192.168.0.0/23"), net.ParseIP("192.168.0.1"), false},
- {"cidr /25", parseCIDR("192.168.1.0/25"), net.ParseIP("192.168.1.1"), false},
- {"cidr /26", parseCIDR("172.16.1.128/26"), net.ParseIP("172.16.1.129"), false},
- {"class a", parseCIDR("10.0.0.0/8"), net.ParseIP("10.0.0.1"), false},
- {"cidr /32", parseCIDR("192.168.255.4/32"), net.ParseIP("192.168.255.4"), false},
- {"cidr /31", parseCIDR("192.168.255.4/31"), net.ParseIP("192.168.255.5"), false},
- }
- for _, tt := range tests {
- test := tt
- t.Run(test.name, func(t *testing.T) {
- got, err := FirstIPInSubnet(test.args)
- if (err != nil) != test.wantErr {
- t.Errorf("FirstIPInSubnet() error = %v, wantErr %v", err, test.wantErr)
- return
- }
- if !got.Equal(test.want) {
- t.Errorf("FirstIPInSubnet() got = %v, want %v", got, test.want)
- }
- })
- }
-}
-
-func TestLastIPInSubnet(t *testing.T) {
- tests := []struct {
- name string
- args *net.IPNet
- want net.IP
- wantErr bool
- }{
- {"class b", parseCIDR("192.168.0.0/16"), net.ParseIP("192.168.255.255"), false},
- {"class c", parseCIDR("192.168.1.0/24"), net.ParseIP("192.168.1.255"), false},
- {"cidr /23", parseCIDR("192.168.0.0/23"), net.ParseIP("192.168.1.255"), false},
- {"cidr /25", parseCIDR("192.168.1.0/25"), net.ParseIP("192.168.1.127"), false},
- {"cidr /26", parseCIDR("172.16.1.128/26"), net.ParseIP("172.16.1.191"), false},
- {"class a", parseCIDR("10.0.0.0/8"), net.ParseIP("10.255.255.255"), false},
- {"cidr /32", parseCIDR("192.168.255.4/32"), net.ParseIP("192.168.255.4"), false},
- {"cidr /31", parseCIDR("192.168.255.4/31"), net.ParseIP("192.168.255.5"), false},
- }
- for _, tt := range tests {
- test := tt
- t.Run(test.name, func(t *testing.T) {
- got, err := LastIPInSubnet(test.args)
- if (err != nil) != test.wantErr {
- t.Errorf("LastIPInSubnet() error = %v, wantErr %v", err, test.wantErr)
- return
- }
- if !got.Equal(test.want) {
- t.Errorf("LastIPInSubnet() got = %v, want %v", got, test.want)
- }
- })
- }
-}