|
| 1 | +// Copyright 2019-2024 The Liqo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package utils |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net" |
| 20 | + "net/netip" |
| 21 | + |
| 22 | + "go4.org/netipx" |
| 23 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 24 | + |
| 25 | + "github.com/liqotech/liqo/pkg/consts" |
| 26 | +) |
| 27 | + |
| 28 | +// MapIPToNetwork creates a new IP address obtained by means of the old IP address and the new network. |
| 29 | +func MapIPToNetwork(newNetwork, oldIP string) (newIP string, err error) { |
| 30 | + if newNetwork == consts.DefaultCIDRValue { |
| 31 | + return oldIP, nil |
| 32 | + } |
| 33 | + // Parse newNetwork |
| 34 | + ip, network, err := net.ParseCIDR(newNetwork) |
| 35 | + if err != nil { |
| 36 | + return "", err |
| 37 | + } |
| 38 | + // Get mask |
| 39 | + mask := network.Mask |
| 40 | + // Get slice of bytes for newNetwork |
| 41 | + // Type net.IP has underlying type []byte |
| 42 | + parsedNewIP := ip.To4() |
| 43 | + // Get oldIP as slice of bytes |
| 44 | + parsedOldIP := net.ParseIP(oldIP) |
| 45 | + if parsedOldIP == nil { |
| 46 | + return "", fmt.Errorf("cannot parse oldIP") |
| 47 | + } |
| 48 | + parsedOldIP = parsedOldIP.To4() |
| 49 | + // Substitute the last 32-mask bits of newNetwork with bits taken by the old ip |
| 50 | + for i := 0; i < len(mask); i++ { |
| 51 | + // Step 1: NOT(mask[i]) = mask[i] ^ 0xff. They are the 'host' bits |
| 52 | + // Step 2: BITWISE AND between the host bits and parsedOldIP[i] zeroes the network bits in parsedOldIP[i] |
| 53 | + // Step 3: BITWISE OR copies the result of step 2 in newIP[i] |
| 54 | + parsedNewIP[i] |= (mask[i] ^ 0xff) & parsedOldIP[i] |
| 55 | + } |
| 56 | + newIP = parsedNewIP.String() |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +// GetMask retrieves the mask from a CIDR. |
| 61 | +func GetMask(network string) uint8 { |
| 62 | + _, subnet, err := net.ParseCIDR(network) |
| 63 | + utilruntime.Must(err) |
| 64 | + ones, _ := subnet.Mask.Size() |
| 65 | + return uint8(ones) |
| 66 | +} |
| 67 | + |
| 68 | +// SetMask forges a new cidr from a network cidr and a mask. |
| 69 | +func SetMask(network string, mask uint8) string { |
| 70 | + _, n, err := net.ParseCIDR(network) |
| 71 | + utilruntime.Must(err) |
| 72 | + newMask := net.CIDRMask(int(mask), 32) |
| 73 | + n.Mask = newMask |
| 74 | + return n.String() |
| 75 | +} |
| 76 | + |
| 77 | +// Next used to get the second half of a given network. |
| 78 | +func Next(network string) string { |
| 79 | + prefix, err := netip.ParsePrefix(network) |
| 80 | + utilruntime.Must(err) |
| 81 | + // Step 1: Get last IP address of network |
| 82 | + // Step 2: Get next IP address |
| 83 | + firstIP := netipx.RangeOfPrefix(prefix).To().Next() |
| 84 | + prefix = netip.PrefixFrom(firstIP, prefix.Bits()) |
| 85 | + return prefix.String() |
| 86 | +} |
| 87 | + |
| 88 | +// IsValidCIDR returns an error if the received CIDR is invalid. |
| 89 | +func IsValidCIDR(cidr string) error { |
| 90 | + _, _, err := net.ParseCIDR(cidr) |
| 91 | + return err |
| 92 | +} |
| 93 | + |
| 94 | +// SplitNetwork returns the two halves that make up a given network. |
| 95 | +func SplitNetwork(network string) []string { |
| 96 | + halves := make([]string, 2) |
| 97 | + |
| 98 | + // Get halves mask length. |
| 99 | + mask := GetMask(network) |
| 100 | + mask++ |
| 101 | + |
| 102 | + // Get first half CIDR. |
| 103 | + halves[0] = SetMask(network, mask) |
| 104 | + |
| 105 | + // Get second half CIDR. |
| 106 | + halves[1] = Next(halves[0]) |
| 107 | + |
| 108 | + return halves |
| 109 | +} |
0 commit comments