net.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 _, cidrString := range cidrsString {
  27. _, cidr, err := net.ParseCIDR(cidrString)
  28. if err != nil {
  29. return nil, fmt.Errorf("failed to parse cidr value:%q with error:%v", cidrString, err)
  30. }
  31. cidrs = append(cidrs, cidr)
  32. }
  33. return cidrs, nil
  34. }
  35. // IsDualStackIPs returns if a slice of ips is:
  36. // - all are valid ips
  37. // - at least one ip from each family (v4 or v6)
  38. func IsDualStackIPs(ips []net.IP) (bool, error) {
  39. v4Found := false
  40. v6Found := false
  41. for _, ip := range ips {
  42. if ip == nil {
  43. return false, fmt.Errorf("ip %v is invalid", ip)
  44. }
  45. if v4Found && v6Found {
  46. continue
  47. }
  48. if IsIPv6(ip) {
  49. v6Found = true
  50. continue
  51. }
  52. v4Found = true
  53. }
  54. return (v4Found && v6Found), nil
  55. }
  56. // IsDualStackIPStrings returns if
  57. // - all are valid ips
  58. // - at least one ip from each family (v4 or v6)
  59. func IsDualStackIPStrings(ips []string) (bool, error) {
  60. parsedIPs := make([]net.IP, 0, len(ips))
  61. for _, ip := range ips {
  62. parsedIP := net.ParseIP(ip)
  63. parsedIPs = append(parsedIPs, parsedIP)
  64. }
  65. return IsDualStackIPs(parsedIPs)
  66. }
  67. // IsDualStackCIDRs returns if
  68. // - all are valid cidrs
  69. // - at least one cidr from each family (v4 or v6)
  70. func IsDualStackCIDRs(cidrs []*net.IPNet) (bool, error) {
  71. v4Found := false
  72. v6Found := false
  73. for _, cidr := range cidrs {
  74. if cidr == nil {
  75. return false, fmt.Errorf("cidr %v is invalid", cidr)
  76. }
  77. if v4Found && v6Found {
  78. continue
  79. }
  80. if IsIPv6(cidr.IP) {
  81. v6Found = true
  82. continue
  83. }
  84. v4Found = true
  85. }
  86. return v4Found && v6Found, nil
  87. }
  88. // IsDualStackCIDRStrings returns if
  89. // - all are valid cidrs
  90. // - at least one cidr from each family (v4 or v6)
  91. func IsDualStackCIDRStrings(cidrs []string) (bool, error) {
  92. parsedCIDRs, err := ParseCIDRs(cidrs)
  93. if err != nil {
  94. return false, err
  95. }
  96. return IsDualStackCIDRs(parsedCIDRs)
  97. }
  98. // IsIPv6 returns if netIP is IPv6.
  99. func IsIPv6(netIP net.IP) bool {
  100. return netIP != nil && netIP.To4() == nil
  101. }
  102. // IsIPv6String returns if ip is IPv6.
  103. func IsIPv6String(ip string) bool {
  104. netIP := net.ParseIP(ip)
  105. return IsIPv6(netIP)
  106. }
  107. // IsIPv6CIDRString returns if cidr is IPv6.
  108. // This assumes cidr is a valid CIDR.
  109. func IsIPv6CIDRString(cidr string) bool {
  110. ip, _, _ := net.ParseCIDR(cidr)
  111. return IsIPv6(ip)
  112. }
  113. // IsIPv6CIDR returns if a cidr is ipv6
  114. func IsIPv6CIDR(cidr *net.IPNet) bool {
  115. ip := cidr.IP
  116. return IsIPv6(ip)
  117. }
  118. // ParsePort parses a string representing an IP port. If the string is not a
  119. // valid port number, this returns an error.
  120. func ParsePort(port string, allowZero bool) (int, error) {
  121. portInt, err := strconv.ParseUint(port, 10, 16)
  122. if err != nil {
  123. return 0, err
  124. }
  125. if portInt == 0 && !allowZero {
  126. return 0, errors.New("0 is not a valid port number")
  127. }
  128. return int(portInt), nil
  129. }
  130. // BigForIP creates a big.Int based on the provided net.IP
  131. func BigForIP(ip net.IP) *big.Int {
  132. b := ip.To4()
  133. if b == nil {
  134. b = ip.To16()
  135. }
  136. return big.NewInt(0).SetBytes(b)
  137. }
  138. // AddIPOffset adds the provided integer offset to a base big.Int representing a
  139. // net.IP
  140. func AddIPOffset(base *big.Int, offset int) net.IP {
  141. return net.IP(big.NewInt(0).Add(base, big.NewInt(int64(offset))).Bytes())
  142. }
  143. // RangeSize returns the size of a range in valid addresses.
  144. // returns the size of the subnet (or math.MaxInt64 if the range size would overflow int64)
  145. func RangeSize(subnet *net.IPNet) int64 {
  146. ones, bits := subnet.Mask.Size()
  147. if bits == 32 && (bits-ones) >= 31 || bits == 128 && (bits-ones) >= 127 {
  148. return 0
  149. }
  150. // this checks that we are not overflowing an int64
  151. if bits-ones >= 63 {
  152. return math.MaxInt64
  153. }
  154. return int64(1) << uint(bits-ones)
  155. }
  156. // GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space.
  157. func GetIndexedIP(subnet *net.IPNet, index int) (net.IP, error) {
  158. ip := AddIPOffset(BigForIP(subnet.IP), index)
  159. if !subnet.Contains(ip) {
  160. return nil, fmt.Errorf("can't generate IP with index %d from subnet. subnet too small. subnet: %q", index, subnet)
  161. }
  162. return ip, nil
  163. }