ipnet.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. a, b, c, d := byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
  54. return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
  55. }
  56. func (ip IP4) String() string {
  57. return ip.ToIP().String()
  58. }
  59. func (ip IP4) StringSep(sep string) string {
  60. a, b, c, d := ip.Octets()
  61. return fmt.Sprintf("%d%s%d%s%d%s%d", a, sep, b, sep, c, sep, d)
  62. }
  63. // json.Marshaler impl
  64. func (ip IP4) MarshalJSON() ([]byte, error) {
  65. return []byte(fmt.Sprintf(`"%s"`, ip)), nil
  66. }
  67. // json.Unmarshaler impl
  68. func (ip *IP4) UnmarshalJSON(j []byte) error {
  69. j = bytes.Trim(j, "\"")
  70. if val, err := ParseIP4(string(j)); err != nil {
  71. return err
  72. } else {
  73. *ip = val
  74. return nil
  75. }
  76. }
  77. // similar to net.IPNet but has uint based representation
  78. type IP4Net struct {
  79. IP IP4
  80. PrefixLen uint
  81. }
  82. func (n IP4Net) String() string {
  83. return fmt.Sprintf("%s/%d", n.IP.String(), n.PrefixLen)
  84. }
  85. func (n IP4Net) StringSep(octetSep, prefixSep string) string {
  86. return fmt.Sprintf("%s%s%d", n.IP.StringSep(octetSep), prefixSep, n.PrefixLen)
  87. }
  88. func (n IP4Net) Network() IP4Net {
  89. return IP4Net{
  90. n.IP & IP4(n.Mask()),
  91. n.PrefixLen,
  92. }
  93. }
  94. func (n IP4Net) Next() IP4Net {
  95. return IP4Net{
  96. n.IP + (1 << (32 - n.PrefixLen)),
  97. n.PrefixLen,
  98. }
  99. }
  100. func FromIPNet(n *net.IPNet) IP4Net {
  101. prefixLen, _ := n.Mask.Size()
  102. return IP4Net{
  103. FromIP(n.IP),
  104. uint(prefixLen),
  105. }
  106. }
  107. func (n IP4Net) ToIPNet() *net.IPNet {
  108. return &net.IPNet{
  109. IP: n.IP.ToIP(),
  110. Mask: net.CIDRMask(int(n.PrefixLen), 32),
  111. }
  112. }
  113. func (n IP4Net) Overlaps(other IP4Net) bool {
  114. var mask uint32
  115. if n.PrefixLen < other.PrefixLen {
  116. mask = n.Mask()
  117. } else {
  118. mask = other.Mask()
  119. }
  120. return (uint32(n.IP) & mask) == (uint32(other.IP) & mask)
  121. }
  122. func (n IP4Net) Equal(other IP4Net) bool {
  123. return n.IP == other.IP && n.PrefixLen == other.PrefixLen
  124. }
  125. func (n IP4Net) Mask() uint32 {
  126. var ones uint32 = 0xFFFFFFFF
  127. return ones << (32 - n.PrefixLen)
  128. }
  129. func (n IP4Net) Contains(ip IP4) bool {
  130. return (uint32(n.IP) & n.Mask()) == (uint32(ip) & n.Mask())
  131. }
  132. // json.Marshaler impl
  133. func (n IP4Net) MarshalJSON() ([]byte, error) {
  134. return []byte(fmt.Sprintf(`"%s"`, n)), nil
  135. }
  136. // json.Unmarshaler impl
  137. func (n *IP4Net) UnmarshalJSON(j []byte) error {
  138. j = bytes.Trim(j, "\"")
  139. if _, val, err := net.ParseCIDR(string(j)); err != nil {
  140. return err
  141. } else {
  142. *n = FromIPNet(val)
  143. return nil
  144. }
  145. }