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.

322 lines
9.3 KiB

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