ipnet.go 3.9 KB

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