| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package net |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"math" |
| 23 |
"math/big" |
| 24 |
"net" |
| 25 |
"strconv" |
| 26 |
) |
| 27 |
|
| 28 |
// ParseCIDRs parses a list of cidrs and return error if any is invalid. |
| 29 |
// order is maintained |
| 30 |
func ParseCIDRs(cidrsString []string) ([]*net.IPNet, error) { |
| 31 |
cidrs := make([]*net.IPNet, 0, len(cidrsString)) |
| 32 |
for i, cidrString := range cidrsString { |
| 33 |
_, cidr, err := ParseCIDRSloppy(cidrString) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("invalid CIDR[%d]: %v (%v)", i, cidr, err) |
| 36 |
} |
| 37 |
cidrs = append(cidrs, cidr) |
| 38 |
} |
| 39 |
return cidrs, nil |
| 40 |
} |
| 41 |
|
| 42 |
// ParsePort parses a string representing an IP port. If the string is not a |
| 43 |
// valid port number, this returns an error. |
| 44 |
func ParsePort(port string, allowZero bool) (int, error) { |
| 45 |
portInt, err := strconv.ParseUint(port, 10, 16) |
| 46 |
if err != nil { |
| 47 |
return 0, err |
| 48 |
} |
| 49 |
if portInt == 0 && !allowZero { |
| 50 |
return 0, errors.New("0 is not a valid port number") |
| 51 |
} |
| 52 |
return int(portInt), nil |
| 53 |
} |
| 54 |
|
| 55 |
// BigForIP creates a big.Int based on the provided net.IP |
| 56 |
func BigForIP(ip net.IP) *big.Int { |
| 57 |
// NOTE: Convert to 16-byte representation so we can |
| 58 |
// handle v4 and v6 values the same way. |
| 59 |
return big.NewInt(0).SetBytes(ip.To16()) |
| 60 |
} |
| 61 |
|
| 62 |
// AddIPOffset adds the provided integer offset to a base big.Int representing a net.IP |
| 63 |
// NOTE: If you started with a v4 address and overflow it, you get a v6 result. |
| 64 |
func AddIPOffset(base *big.Int, offset int) net.IP { |
| 65 |
r := big.NewInt(0).Add(base, big.NewInt(int64(offset))).Bytes() |
| 66 |
r = append(make([]byte, 16), r...) |
| 67 |
return net.IP(r[len(r)-16:]) |
| 68 |
} |
| 69 |
|
| 70 |
// RangeSize returns the size of a range in valid addresses. |
| 71 |
// returns the size of the subnet (or math.MaxInt64 if the range size would overflow int64) |
| 72 |
func RangeSize(subnet *net.IPNet) int64 { |
| 73 |
ones, bits := subnet.Mask.Size() |
| 74 |
if bits == 32 && (bits-ones) >= 31 || bits == 128 && (bits-ones) >= 127 { |
| 75 |
return 0 |
| 76 |
} |
| 77 |
// this checks that we are not overflowing an int64 |
| 78 |
if bits-ones >= 63 { |
| 79 |
return math.MaxInt64 |
| 80 |
} |
| 81 |
return int64(1) << uint(bits-ones) |
| 82 |
} |
| 83 |
|
| 84 |
// GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space. |
| 85 |
func GetIndexedIP(subnet *net.IPNet, index int) (net.IP, error) { |
| 86 |
ip := AddIPOffset(BigForIP(subnet.IP), index) |
| 87 |
if !subnet.Contains(ip) { |
| 88 |
return nil, fmt.Errorf("can't generate IP with index %d from subnet. subnet too small. subnet: %q", index, subnet) |
| 89 |
} |
| 90 |
return ip, nil |
| 91 |
} |