ipnet.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "net"
  20. )
  21. type IP4 uint32
  22. func FromBytes(ip []byte) IP4 {
  23. return IP4(uint32(ip[3]) |
  24. (uint32(ip[2]) << 8) |
  25. (uint32(ip[1]) << 16) |
  26. (uint32(ip[0]) << 24))
  27. }
  28. func FromIP(ip net.IP) IP4 {
  29. return FromBytes(ip.To4())
  30. }
  31. func ParseIP4(s string) (IP4, error) {
  32. ip := net.ParseIP(s)
  33. if ip == nil {
  34. return IP4(0), errors.New("Invalid IP address format")
  35. }
  36. return FromIP(ip), nil
  37. }
  38. func MustParseIP4(s string) IP4 {
  39. ip, err := ParseIP4(s)
  40. if err != nil {
  41. panic(err)
  42. }
  43. return ip
  44. }
  45. func (ip IP4) Octets() (a, b, c, d byte) {
  46. a, b, c, d = byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
  47. return
  48. }
  49. func (ip IP4) ToIP() net.IP {
  50. return net.IPv4(ip.Octets())
  51. }
  52. func (ip IP4) NetworkOrder() uint32 {
  53. if NativelyLittle() {
  54. a, b, c, d := byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
  55. return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
  56. } else {
  57. return uint32(ip)
  58. }
  59. }
  60. func (ip IP4) String() string {
  61. return ip.ToIP().String()
  62. }
  63. func (ip IP4) StringSep(sep string) string {
  64. a, b, c, d := ip.Octets()
  65. return fmt.Sprintf("%d%s%d%s%d%s%d", a, sep, b, sep, c, sep, d)
  66. }
  67. // json.Marshaler impl
  68. func (ip IP4) MarshalJSON() ([]byte, error) {
  69. return []byte(fmt.Sprintf(`"%s"`, ip)), nil
  70. }
  71. // json.Unmarshaler impl
  72. func (ip *IP4) UnmarshalJSON(j []byte) error {
  73. j = bytes.Trim(j, "\"")
  74. if val, err := ParseIP4(string(j)); err != nil {
  75. return err
  76. } else {
  77. *ip = val
  78. return nil
  79. }
  80. }
  81. // similar to net.IPNet but has uint based representation
  82. type IP4Net struct {
  83. IP IP4
  84. PrefixLen uint
  85. }
  86. func (n IP4Net) String() string {
  87. return fmt.Sprintf("%s/%d", n.IP.String(), n.PrefixLen)
  88. }
  89. func (n IP4Net) StringSep(octetSep, prefixSep string) string {
  90. return fmt.Sprintf("%s%s%d", n.IP.StringSep(octetSep), prefixSep, n.PrefixLen)
  91. }
  92. func (n IP4Net) Network() IP4Net {
  93. return IP4Net{
  94. n.IP & IP4(n.Mask()),
  95. n.PrefixLen,
  96. }
  97. }
  98. func (n IP4Net) Next() IP4Net {
  99. return IP4Net{
  100. n.IP + (1 << (32 - n.PrefixLen)),
  101. n.PrefixLen,
  102. }
  103. }
  104. func FromIPNet(n *net.IPNet) IP4Net {
  105. prefixLen, _ := n.Mask.Size()
  106. return IP4Net{
  107. FromIP(n.IP),
  108. uint(prefixLen),
  109. }
  110. }
  111. func (n IP4Net) ToIPNet() *net.IPNet {
  112. return &net.IPNet{
  113. IP: n.IP.ToIP(),
  114. Mask: net.CIDRMask(int(n.PrefixLen), 32),
  115. }
  116. }
  117. func (n IP4Net) Overlaps(other IP4Net) bool {
  118. var mask uint32
  119. if n.PrefixLen < other.PrefixLen {
  120. mask = n.Mask()
  121. } else {
  122. mask = other.Mask()
  123. }
  124. return (uint32(n.IP) & mask) == (uint32(other.IP) & mask)
  125. }
  126. func (n IP4Net) Equal(other IP4Net) bool {
  127. return n.IP == other.IP && n.PrefixLen == other.PrefixLen
  128. }
  129. func (n IP4Net) Mask() uint32 {
  130. var ones uint32 = 0xFFFFFFFF
  131. return ones << (32 - n.PrefixLen)
  132. }
  133. func (n IP4Net) Contains(ip IP4) bool {
  134. return (uint32(n.IP) & n.Mask()) == (uint32(ip) & n.Mask())
  135. }
  136. // json.Marshaler impl
  137. func (n IP4Net) MarshalJSON() ([]byte, error) {
  138. return []byte(fmt.Sprintf(`"%s"`, n)), nil
  139. }
  140. // json.Unmarshaler impl
  141. func (n *IP4Net) UnmarshalJSON(j []byte) error {
  142. j = bytes.Trim(j, "\"")
  143. if _, val, err := net.ParseCIDR(string(j)); err != nil {
  144. return err
  145. } else {
  146. *n = FromIPNet(val)
  147. return nil
  148. }
  149. }