Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // Copyright 2009 The Go Authors
  3. // Released under the MIT license
  4. package flatip
  5. import (
  6. "bytes"
  7. "errors"
  8. "net"
  9. )
  10. var (
  11. v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
  12. IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  13. IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  14. IPv4zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0}
  15. ErrInvalidIPString = errors.New("String could not be interpreted as an IP address")
  16. )
  17. // packed versions of net.IP and net.IPNet; these are pure value types,
  18. // so they can be compared with == and used as map keys.
  19. // IP is a 128-bit representation of an IP address, using the 4-in-6 mapping
  20. // to represent IPv4 addresses.
  21. type IP [16]byte
  22. // IPNet is a IP network. In a valid value, all bits after PrefixLen are zeroes.
  23. type IPNet struct {
  24. IP
  25. PrefixLen uint8
  26. }
  27. // NetIP converts an IP into a net.IP.
  28. func (ip IP) NetIP() (result net.IP) {
  29. result = make(net.IP, 16)
  30. copy(result[:], ip[:])
  31. return
  32. }
  33. // FromNetIP converts a net.IP into an IP.
  34. func FromNetIP(ip net.IP) (result IP) {
  35. if len(ip) == 16 {
  36. copy(result[:], ip[:])
  37. } else {
  38. result[10] = 0xff
  39. result[11] = 0xff
  40. copy(result[12:], ip[:])
  41. }
  42. return
  43. }
  44. // IPv4 returns the IP address representation of a.b.c.d
  45. func IPv4(a, b, c, d byte) (result IP) {
  46. copy(result[:12], v4InV6Prefix)
  47. result[12] = a
  48. result[13] = b
  49. result[14] = c
  50. result[15] = d
  51. return
  52. }
  53. // ParseIP parses a string representation of an IP address into an IP.
  54. // Unlike net.ParseIP, it returns an error instead of a zero value on failure,
  55. // since the zero value of `IP` is a representation of a valid IP (::0, the
  56. // IPv6 "unspecified address").
  57. func ParseIP(ipstr string) (ip IP, err error) {
  58. // TODO reimplement this without net.ParseIP
  59. netip := net.ParseIP(ipstr)
  60. if netip == nil {
  61. err = ErrInvalidIPString
  62. return
  63. }
  64. netip = netip.To16()
  65. copy(ip[:], netip)
  66. return
  67. }
  68. // String returns the string representation of an IP
  69. func (ip IP) String() string {
  70. // TODO reimplement this without using (net.IP).String()
  71. return (net.IP)(ip[:]).String()
  72. }
  73. // IsIPv4 returns whether the IP is an IPv4 address.
  74. func (ip IP) IsIPv4() bool {
  75. return bytes.Equal(ip[:12], v4InV6Prefix)
  76. }
  77. // IsLoopback returns whether the IP is a loopback address.
  78. func (ip IP) IsLoopback() bool {
  79. if ip.IsIPv4() {
  80. return ip[12] == 127
  81. } else {
  82. return ip == IPv6loopback
  83. }
  84. }
  85. func (ip IP) IsUnspecified() bool {
  86. return ip == IPv4zero || ip == IPv6zero
  87. }
  88. func rawCidrMask(length int) (m IP) {
  89. n := uint(length)
  90. for i := 0; i < 16; i++ {
  91. if n >= 8 {
  92. m[i] = 0xff
  93. n -= 8
  94. continue
  95. }
  96. m[i] = ^byte(0xff >> n)
  97. return
  98. }
  99. return
  100. }
  101. func (ip IP) applyMask(mask IP) (result IP) {
  102. for i := 0; i < 16; i += 1 {
  103. result[i] = ip[i] & mask[i]
  104. }
  105. return
  106. }
  107. func cidrMask(ones, bits int) (result IP) {
  108. switch bits {
  109. case 32:
  110. return rawCidrMask(96 + ones)
  111. case 128:
  112. return rawCidrMask(ones)
  113. default:
  114. return
  115. }
  116. }
  117. // Mask returns the result of masking ip with the CIDR mask of
  118. // length 'ones', out of a total of 'bits' (which must be either
  119. // 32 for an IPv4 subnet or 128 for an IPv6 subnet).
  120. func (ip IP) Mask(ones, bits int) (result IP) {
  121. return ip.applyMask(cidrMask(ones, bits))
  122. }
  123. // ToNetIPNet converts an IPNet into a net.IPNet.
  124. func (cidr IPNet) ToNetIPNet() (result net.IPNet) {
  125. return net.IPNet{
  126. IP: cidr.IP.NetIP(),
  127. Mask: net.CIDRMask(int(cidr.PrefixLen), 128),
  128. }
  129. }
  130. // Contains retuns whether the network contains `ip`.
  131. func (cidr IPNet) Contains(ip IP) bool {
  132. maskedIP := ip.Mask(int(cidr.PrefixLen), 128)
  133. return cidr.IP == maskedIP
  134. }
  135. func (cidr IPNet) Size() (ones, bits int) {
  136. if cidr.IP.IsIPv4() {
  137. return int(cidr.PrefixLen) - 96, 32
  138. } else {
  139. return int(cidr.PrefixLen), 128
  140. }
  141. }
  142. // FromNetIPnet converts a net.IPNet into an IPNet.
  143. func FromNetIPNet(network net.IPNet) (result IPNet) {
  144. ones, _ := network.Mask.Size()
  145. if len(network.IP) == 16 {
  146. copy(result.IP[:], network.IP[:])
  147. } else {
  148. result.IP[10] = 0xff
  149. result.IP[11] = 0xff
  150. copy(result.IP[12:], network.IP[:])
  151. ones += 96
  152. }
  153. // perform masking so that equal CIDRs are ==
  154. result.IP = result.IP.Mask(ones, 128)
  155. result.PrefixLen = uint8(ones)
  156. return
  157. }
  158. // String returns a string representation of an IPNet.
  159. func (cidr IPNet) String() string {
  160. ip := make(net.IP, 16)
  161. copy(ip[:], cidr.IP[:])
  162. ipnet := net.IPNet{
  163. IP: ip,
  164. Mask: net.CIDRMask(int(cidr.PrefixLen), 128),
  165. }
  166. return ipnet.String()
  167. }
  168. // HumanReadableString returns a string representation of an IPNet;
  169. // if the network contains only a single IP address, it returns
  170. // a representation of that address.
  171. func (cidr IPNet) HumanReadableString() string {
  172. if cidr.PrefixLen == 128 {
  173. return cidr.IP.String()
  174. }
  175. return cidr.String()
  176. }
  177. // IsZero tests whether ipnet is the zero value of an IPNet, 0::0/0.
  178. // Although this is a valid subnet, it can still be used as a sentinel
  179. // value in some contexts.
  180. func (ipnet IPNet) IsZero() bool {
  181. return ipnet == IPNet{}
  182. }
  183. // ParseCIDR parses a string representation of an IP network in CIDR notation,
  184. // then returns it as an IPNet (along with the original, unmasked address).
  185. func ParseCIDR(netstr string) (ip IP, ipnet IPNet, err error) {
  186. // TODO reimplement this without net.ParseCIDR
  187. nip, nipnet, err := net.ParseCIDR(netstr)
  188. if err != nil {
  189. return
  190. }
  191. return FromNetIP(nip), FromNetIPNet(*nipnet), nil
  192. }