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.

209 lines
6.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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. "io"
  7. "net"
  8. "strconv"
  9. "time"
  10. . "github.com/tendermint/tendermint/binary"
  11. . "github.com/tendermint/tendermint/common"
  12. )
  13. /* NetAddress */
  14. type NetAddress struct {
  15. IP net.IP
  16. Port UInt16
  17. }
  18. // TODO: socks proxies?
  19. func NewNetAddress(addr net.Addr) *NetAddress {
  20. tcpAddr, ok := addr.(*net.TCPAddr)
  21. if !ok {
  22. Panicf("Only TCPAddrs are supported. Got: %v", addr)
  23. }
  24. ip := tcpAddr.IP
  25. port := UInt16(tcpAddr.Port)
  26. return NewNetAddressIPPort(ip, port)
  27. }
  28. func NewNetAddressString(addr string) *NetAddress {
  29. host, portStr, err := net.SplitHostPort(addr)
  30. if err != nil {
  31. panic(err)
  32. }
  33. ip := net.ParseIP(host)
  34. port, err := strconv.ParseUint(portStr, 10, 16)
  35. if err != nil {
  36. panic(err)
  37. }
  38. na := NewNetAddressIPPort(ip, UInt16(port))
  39. return na
  40. }
  41. func NewNetAddressIPPort(ip net.IP, port UInt16) *NetAddress {
  42. na := NetAddress{
  43. IP: ip,
  44. Port: port,
  45. }
  46. return &na
  47. }
  48. func ReadNetAddress(r io.Reader) *NetAddress {
  49. return &NetAddress{
  50. IP: net.IP(ReadByteSlice(r)),
  51. Port: ReadUInt16(r),
  52. }
  53. }
  54. func (na *NetAddress) WriteTo(w io.Writer) (n int64, err error) {
  55. n, err = WriteOnto(ByteSlice(na.IP.To16()), w, n, err)
  56. n, err = WriteOnto(na.Port, w, n, err)
  57. return
  58. }
  59. func (na *NetAddress) Equals(other Binary) bool {
  60. if o, ok := other.(*NetAddress); ok {
  61. return na.String() == o.String()
  62. } else {
  63. return false
  64. }
  65. }
  66. func (na *NetAddress) Less(other Binary) bool {
  67. if o, ok := other.(*NetAddress); ok {
  68. return na.String() < o.String()
  69. } else {
  70. panic("Cannot compare unequal types")
  71. }
  72. }
  73. func (na *NetAddress) String() string {
  74. port := strconv.FormatUint(uint64(na.Port), 10)
  75. addr := net.JoinHostPort(na.IP.String(), port)
  76. return addr
  77. }
  78. func (na *NetAddress) Dial() (*Connection, error) {
  79. conn, err := net.Dial("tcp", na.String())
  80. if err != nil {
  81. return nil, err
  82. }
  83. return NewConnection(conn), nil
  84. }
  85. func (na *NetAddress) DialTimeout(timeout time.Duration) (*Connection, error) {
  86. conn, err := net.DialTimeout("tcp", na.String(), timeout)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return NewConnection(conn), nil
  91. }
  92. func (na *NetAddress) Routable() bool {
  93. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  94. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  95. na.RFC4193() || na.RFC4843() || na.Local())
  96. }
  97. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  98. // address or one that matches the RFC3849 documentation address format.
  99. func (na *NetAddress) Valid() bool {
  100. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  101. na.IP.Equal(net.IPv4bcast))
  102. }
  103. func (na *NetAddress) Local() bool {
  104. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  105. }
  106. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  107. const (
  108. Unreachable = 0
  109. Default = iota
  110. Teredo
  111. Ipv6_weak
  112. Ipv4
  113. Ipv6_strong
  114. Private
  115. )
  116. if !na.Routable() {
  117. return Unreachable
  118. } else if na.RFC4380() {
  119. if !o.Routable() {
  120. return Default
  121. } else if o.RFC4380() {
  122. return Teredo
  123. } else if o.IP.To4() != nil {
  124. return Ipv4
  125. } else { // ipv6
  126. return Ipv6_weak
  127. }
  128. } else if na.IP.To4() != nil {
  129. if o.Routable() && o.IP.To4() != nil {
  130. return Ipv4
  131. }
  132. return Default
  133. } else /* ipv6 */ {
  134. var tunnelled bool
  135. // Is our v6 is tunnelled?
  136. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  137. tunnelled = true
  138. }
  139. if !o.Routable() {
  140. return Default
  141. } else if o.RFC4380() {
  142. return Teredo
  143. } else if o.IP.To4() != nil {
  144. return Ipv4
  145. } else if tunnelled {
  146. // only prioritise ipv6 if we aren't tunnelling it.
  147. return Ipv6_weak
  148. }
  149. return Ipv6_strong
  150. }
  151. }
  152. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  153. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  154. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  155. // RFC3964: IPv6 6to4 (2002::/16)
  156. // RFC4193: IPv6 unique local (FC00::/7)
  157. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  158. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  159. // RFC4862: IPv6 Autoconfig (FE80::/64)
  160. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  161. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  162. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  163. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  164. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  165. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  166. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  167. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  168. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  169. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  170. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  171. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  172. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  173. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  174. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  175. func (na *NetAddress) RFC1918() bool {
  176. return rfc1918_10.Contains(na.IP) ||
  177. rfc1918_192.Contains(na.IP) ||
  178. rfc1918_172.Contains(na.IP)
  179. }
  180. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  181. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  182. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  183. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  184. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  185. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  186. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  187. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  188. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }