ipnet.go 4.0 KB

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