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.

268 lines
7.6 KiB

9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. // Modified for Tendermint
  2. // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
  3. // https://github.com/conformal/btcd/blob/master/LICENSE
  4. package p2p
  5. import (
  6. "flag"
  7. "fmt"
  8. "net"
  9. "strconv"
  10. "strings"
  11. "time"
  12. cmn "github.com/tendermint/tmlibs/common"
  13. )
  14. // NetAddress defines information about a peer on the network
  15. // including its ID, IP address, and port.
  16. type NetAddress struct {
  17. ID ID
  18. IP net.IP
  19. Port uint16
  20. str string
  21. }
  22. // NewNetAddress returns a new NetAddress using the provided TCP
  23. // address. When testing, other net.Addr (except TCP) will result in
  24. // using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will
  25. // panic.
  26. // TODO: socks proxies?
  27. func NewNetAddress(addr net.Addr) *NetAddress {
  28. tcpAddr, ok := addr.(*net.TCPAddr)
  29. if !ok {
  30. if flag.Lookup("test.v") == nil { // normal run
  31. cmn.PanicSanity(cmn.Fmt("Only TCPAddrs are supported. Got: %v", addr))
  32. } else { // in testing
  33. return NewNetAddressIPPort(net.IP("0.0.0.0"), 0)
  34. }
  35. }
  36. ip := tcpAddr.IP
  37. port := uint16(tcpAddr.Port)
  38. return NewNetAddressIPPort(ip, port)
  39. }
  40. // NewNetAddressString returns a new NetAddress using the provided
  41. // address in the form of "IP:Port". Also resolves the host if host
  42. // is not an IP.
  43. func NewNetAddressString(addr string) (*NetAddress, error) {
  44. host, portStr, err := net.SplitHostPort(removeProtocolIfDefined(addr))
  45. if err != nil {
  46. return nil, err
  47. }
  48. ip := net.ParseIP(host)
  49. if ip == nil {
  50. if len(host) > 0 {
  51. ips, err := net.LookupIP(host)
  52. if err != nil {
  53. return nil, err
  54. }
  55. ip = ips[0]
  56. }
  57. }
  58. port, err := strconv.ParseUint(portStr, 10, 16)
  59. if err != nil {
  60. return nil, err
  61. }
  62. na := NewNetAddressIPPort(ip, uint16(port))
  63. return na, nil
  64. }
  65. // NewNetAddressStrings returns an array of NetAddress'es build using
  66. // the provided strings.
  67. func NewNetAddressStrings(addrs []string) ([]*NetAddress, []error) {
  68. netAddrs := make([]*NetAddress, 0)
  69. errs := make([]error, 0)
  70. for _, addr := range addrs {
  71. netAddr, err := NewNetAddressString(addr)
  72. if err != nil {
  73. errs = append(errs, fmt.Errorf("Error in address %s: %v", addr, err))
  74. } else {
  75. netAddrs = append(netAddrs, netAddr)
  76. }
  77. }
  78. return netAddrs, errs
  79. }
  80. // NewNetAddressIPPort returns a new NetAddress using the provided IP
  81. // and port number.
  82. func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
  83. na := &NetAddress{
  84. IP: ip,
  85. Port: port,
  86. str: net.JoinHostPort(
  87. ip.String(),
  88. strconv.FormatUint(uint64(port), 10),
  89. ),
  90. }
  91. return na
  92. }
  93. // Equals reports whether na and other are the same addresses.
  94. func (na *NetAddress) Equals(other interface{}) bool {
  95. if o, ok := other.(*NetAddress); ok {
  96. return na.String() == o.String()
  97. }
  98. return false
  99. }
  100. func (na *NetAddress) Less(other interface{}) bool {
  101. if o, ok := other.(*NetAddress); ok {
  102. return na.String() < o.String()
  103. }
  104. cmn.PanicSanity("Cannot compare unequal types")
  105. return false
  106. }
  107. // String representation.
  108. func (na *NetAddress) String() string {
  109. if na.str == "" {
  110. addrStr := net.JoinHostPort(
  111. na.IP.String(),
  112. strconv.FormatUint(uint64(na.Port), 10),
  113. )
  114. if na.ID != "" {
  115. addrStr = fmt.Sprintf("%s@%s", na.ID, addrStr)
  116. }
  117. na.str = addrStr
  118. }
  119. return na.str
  120. }
  121. // Dial calls net.Dial on the address.
  122. func (na *NetAddress) Dial() (net.Conn, error) {
  123. conn, err := net.Dial("tcp", na.String())
  124. if err != nil {
  125. return nil, err
  126. }
  127. return conn, nil
  128. }
  129. // DialTimeout calls net.DialTimeout on the address.
  130. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  131. conn, err := net.DialTimeout("tcp", na.String(), timeout)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return conn, nil
  136. }
  137. // Routable returns true if the address is routable.
  138. func (na *NetAddress) Routable() bool {
  139. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  140. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  141. na.RFC4193() || na.RFC4843() || na.Local())
  142. }
  143. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  144. // address or one that matches the RFC3849 documentation address format.
  145. func (na *NetAddress) Valid() bool {
  146. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  147. na.IP.Equal(net.IPv4bcast))
  148. }
  149. // Local returns true if it is a local address.
  150. func (na *NetAddress) Local() bool {
  151. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  152. }
  153. // ReachabilityTo checks whenever o can be reached from na.
  154. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  155. const (
  156. Unreachable = 0
  157. Default = iota
  158. Teredo
  159. Ipv6_weak
  160. Ipv4
  161. Ipv6_strong
  162. )
  163. if !na.Routable() {
  164. return Unreachable
  165. } else if na.RFC4380() {
  166. if !o.Routable() {
  167. return Default
  168. } else if o.RFC4380() {
  169. return Teredo
  170. } else if o.IP.To4() != nil {
  171. return Ipv4
  172. } else { // ipv6
  173. return Ipv6_weak
  174. }
  175. } else if na.IP.To4() != nil {
  176. if o.Routable() && o.IP.To4() != nil {
  177. return Ipv4
  178. }
  179. return Default
  180. } else /* ipv6 */ {
  181. var tunnelled bool
  182. // Is our v6 is tunnelled?
  183. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  184. tunnelled = true
  185. }
  186. if !o.Routable() {
  187. return Default
  188. } else if o.RFC4380() {
  189. return Teredo
  190. } else if o.IP.To4() != nil {
  191. return Ipv4
  192. } else if tunnelled {
  193. // only prioritise ipv6 if we aren't tunnelling it.
  194. return Ipv6_weak
  195. }
  196. return Ipv6_strong
  197. }
  198. }
  199. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  200. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  201. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  202. // RFC3964: IPv6 6to4 (2002::/16)
  203. // RFC4193: IPv6 unique local (FC00::/7)
  204. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  205. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  206. // RFC4862: IPv6 Autoconfig (FE80::/64)
  207. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  208. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  209. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  210. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  211. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  212. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  213. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  214. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  215. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  216. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  217. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  218. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  219. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  220. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  221. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  222. func (na *NetAddress) RFC1918() bool {
  223. return rfc1918_10.Contains(na.IP) ||
  224. rfc1918_192.Contains(na.IP) ||
  225. rfc1918_172.Contains(na.IP)
  226. }
  227. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  228. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  229. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  230. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  231. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  232. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  233. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  234. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  235. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }
  236. func removeProtocolIfDefined(addr string) string {
  237. if strings.Contains(addr, "://") {
  238. return strings.Split(addr, "://")[1]
  239. } else {
  240. return addr
  241. }
  242. }