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.

310 lines
9.0 KiB

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