You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flatip.go 5.1KB

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