您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

flatip.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // FromNetIPnet converts a net.IPNet into an IPNet.
  136. func FromNetIPNet(network net.IPNet) (result IPNet) {
  137. ones, _ := network.Mask.Size()
  138. if len(network.IP) == 16 {
  139. copy(result.IP[:], network.IP[:])
  140. } else {
  141. result.IP[10] = 0xff
  142. result.IP[11] = 0xff
  143. copy(result.IP[12:], network.IP[:])
  144. ones += 96
  145. }
  146. // perform masking so that equal CIDRs are ==
  147. result.IP = result.IP.Mask(ones, 128)
  148. result.PrefixLen = uint8(ones)
  149. return
  150. }
  151. // String returns a string representation of an IPNet.
  152. func (cidr IPNet) String() string {
  153. ip := make(net.IP, 16)
  154. copy(ip[:], cidr.IP[:])
  155. ipnet := net.IPNet{
  156. IP: ip,
  157. Mask: net.CIDRMask(int(cidr.PrefixLen), 128),
  158. }
  159. return ipnet.String()
  160. }
  161. // IsZero tests whether ipnet is the zero value of an IPNet, 0::0/0.
  162. // Although this is a valid subnet, it can still be used as a sentinel
  163. // value in some contexts.
  164. func (ipnet IPNet) IsZero() bool {
  165. return ipnet == IPNet{}
  166. }
  167. // ParseCIDR parses a string representation of an IP network in CIDR notation,
  168. // then returns it as an IPNet (along with the original, unmasked address).
  169. func ParseCIDR(netstr string) (ip IP, ipnet IPNet, err error) {
  170. // TODO reimplement this without net.ParseCIDR
  171. nip, nipnet, err := net.ParseCIDR(netstr)
  172. if err != nil {
  173. return
  174. }
  175. return FromNetIP(nip), FromNetIPNet(*nipnet), nil
  176. }