ip6net.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2015 flannel 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. package ip
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "math/big"
  20. "net"
  21. )
  22. type IP6 big.Int
  23. func FromIP16Bytes(ip []byte) *IP6 {
  24. return (*IP6)(big.NewInt(0).SetBytes(ip))
  25. }
  26. func FromIP6(ip net.IP) *IP6 {
  27. ipv6 := ip.To16()
  28. if ipv6 == nil {
  29. panic("Address is not an IPv6 address")
  30. }
  31. return FromIP16Bytes(ipv6)
  32. }
  33. func ParseIP6(s string) (*IP6, error) {
  34. ip := net.ParseIP(s)
  35. if ip == nil {
  36. return (*IP6)(big.NewInt(0)), errors.New("Invalid IP address format")
  37. }
  38. return FromIP6(ip), nil
  39. }
  40. func Mask(prefixLen int) *big.Int {
  41. mask := net.CIDRMask(prefixLen, 128)
  42. return big.NewInt(0).SetBytes(mask)
  43. }
  44. func IsEmpty(subnet *IP6) bool {
  45. if subnet == nil || (*big.Int)(subnet).Cmp(big.NewInt(0)) == 0 {
  46. return true
  47. }
  48. return false
  49. }
  50. func GetIPv6SubnetMin(networkIP *IP6, subnetSize *big.Int) *IP6 {
  51. return (*IP6)(big.NewInt(0).Add((*big.Int)(networkIP), subnetSize))
  52. }
  53. func GetIPv6SubnetMax(networkIP *IP6, subnetSize *big.Int) *IP6 {
  54. return (*IP6)(big.NewInt(0).Sub((*big.Int)(networkIP), subnetSize))
  55. }
  56. func CheckIPv6Subnet(subnetIP *IP6, mask *big.Int) bool {
  57. if (*big.Int)(subnetIP).Cmp(big.NewInt(0).And((*big.Int)(subnetIP), mask)) != 0 {
  58. return false
  59. }
  60. return true
  61. }
  62. func MustParseIP6(s string) *IP6 {
  63. ip, err := ParseIP6(s)
  64. if err != nil {
  65. panic(err)
  66. }
  67. return ip
  68. }
  69. func (ip6 *IP6) ToIP() net.IP {
  70. ip := net.IP((*big.Int)(ip6).Bytes())
  71. if ip.To4() != nil {
  72. return ip
  73. }
  74. a := (*big.Int)(ip6).FillBytes(make([]byte, 16))
  75. return a
  76. }
  77. func (ip6 IP6) String() string {
  78. return ip6.ToIP().String()
  79. }
  80. // MarshalJSON: json.Marshaler impl
  81. func (ip6 IP6) MarshalJSON() ([]byte, error) {
  82. return []byte(fmt.Sprintf(`"%s"`, ip6)), nil
  83. }
  84. // UnmarshalJSON: json.Unmarshaler impl
  85. func (ip6 *IP6) UnmarshalJSON(j []byte) error {
  86. j = bytes.Trim(j, "\"")
  87. if val, err := ParseIP6(string(j)); err != nil {
  88. return err
  89. } else {
  90. *ip6 = *val
  91. return nil
  92. }
  93. }
  94. // similar to net.IPNet but has uint based representation
  95. type IP6Net struct {
  96. IP *IP6
  97. PrefixLen uint
  98. }
  99. func (n IP6Net) String() string {
  100. if n.IP == nil {
  101. n.IP = (*IP6)(big.NewInt(0))
  102. }
  103. return fmt.Sprintf("%s/%d", n.IP.String(), n.PrefixLen)
  104. }
  105. func (n IP6Net) StringSep(hexSep, prefixSep string) string {
  106. return fmt.Sprintf("%s%s%d", n.IP.String(), prefixSep, n.PrefixLen)
  107. }
  108. func (n IP6Net) Network() IP6Net {
  109. mask := net.CIDRMask(int(n.PrefixLen), 128)
  110. return IP6Net{
  111. FromIP6(n.IP.ToIP().Mask(mask)),
  112. n.PrefixLen,
  113. }
  114. }
  115. func (n IP6Net) Next() IP6Net {
  116. return IP6Net{
  117. (*IP6)(big.NewInt(0).Add((*big.Int)(n.IP), big.NewInt(0).Lsh(big.NewInt(1), 128-n.PrefixLen))),
  118. n.PrefixLen,
  119. }
  120. }
  121. // IncrementIP() increments the IP of IP6Net CIDR by 1
  122. func (n *IP6Net) IncrementIP() {
  123. n.IP = (*IP6)(big.NewInt(0).Add((*big.Int)(n.IP), big.NewInt(1)))
  124. }
  125. func FromIP6Net(n *net.IPNet) IP6Net {
  126. prefixLen, _ := n.Mask.Size()
  127. return IP6Net{
  128. FromIP6(n.IP),
  129. uint(prefixLen),
  130. }
  131. }
  132. func (n IP6Net) ToIPNet() *net.IPNet {
  133. return &net.IPNet{
  134. IP: n.IP.ToIP(),
  135. Mask: net.CIDRMask(int(n.PrefixLen), 128),
  136. }
  137. }
  138. func (n IP6Net) Overlaps(other IP6Net) bool {
  139. var mask *big.Int
  140. if n.PrefixLen < other.PrefixLen {
  141. mask = n.Mask()
  142. } else {
  143. mask = other.Mask()
  144. }
  145. return (IP6)(*big.NewInt(0).And((*big.Int)(n.IP), mask)).String() ==
  146. (IP6)(*big.NewInt(0).And((*big.Int)(other.IP), mask)).String()
  147. }
  148. func (n IP6Net) Equal(other IP6Net) bool {
  149. return ((*big.Int)(n.IP).Cmp((*big.Int)(other.IP)) == 0) &&
  150. n.PrefixLen == other.PrefixLen
  151. }
  152. func (n IP6Net) Mask() *big.Int {
  153. mask := net.CIDRMask(int(n.PrefixLen), 128)
  154. return big.NewInt(0).SetBytes(mask)
  155. }
  156. func (n IP6Net) Contains(ip *IP6) bool {
  157. network := big.NewInt(0).And((*big.Int)(n.IP), n.Mask())
  158. subnet := big.NewInt(0).And((*big.Int)(ip), n.Mask())
  159. return (IP6)(*network).String() == (IP6)(*subnet).String()
  160. }
  161. func (n IP6Net) Empty() bool {
  162. return n.IP == (*IP6)(big.NewInt(0)) && n.PrefixLen == uint(0)
  163. }
  164. // MarshalJSON: json.Marshaler impl
  165. func (n IP6Net) MarshalJSON() ([]byte, error) {
  166. return []byte(fmt.Sprintf(`"%s"`, n)), nil
  167. }
  168. // UnmarshalJSON: json.Unmarshaler impl
  169. func (n *IP6Net) UnmarshalJSON(j []byte) error {
  170. j = bytes.Trim(j, "\"")
  171. if _, val, err := net.ParseCIDR(string(j)); err != nil {
  172. return err
  173. } else {
  174. *n = FromIP6Net(val)
  175. return nil
  176. }
  177. }