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.

188 lines
6.4 KiB

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