net.go 2.8 KB

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