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.

155 lines
5.5 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. func NewNetAddressIPPort(ip net.IP, port UInt16) *NetAddress {
  17. na := NetAddress{
  18. IP: ip,
  19. Port: port,
  20. }
  21. return &na
  22. }
  23. func NewNetAddress(addr net.Addr) *NetAddress {
  24. tcpAddr, ok := addr.(*net.TCPAddr)
  25. if !ok { panic("addr is not a net.TCPAddr") }
  26. na := NewNetAddressIPPort(tcpAddr.IP, UInt16(tcpAddr.Port))
  27. return na
  28. }
  29. func ReadNetAddress(r io.Reader) *NetAddress {
  30. return &NetAddress{
  31. IP: net.IP(ReadByteSlice(r)),
  32. Port: ReadUInt16(r),
  33. }
  34. }
  35. func (na *NetAddress) WriteTo(w io.Writer) (n int64, err error) {
  36. n, err = WriteOnto(ByteSlice(na.IP.To16()), w, n, err)
  37. n, err = WriteOnto(na.Port, w, n, err)
  38. return
  39. }
  40. func (na *NetAddress) String() string {
  41. port := strconv.FormatUint(uint64(na.Port), 10)
  42. addr := net.JoinHostPort(na.IP.String(), port)
  43. return addr
  44. }
  45. func (na *NetAddress) Routable() bool {
  46. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  47. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  48. na.RFC4193() || na.RFC4843() || na.Local())
  49. }
  50. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  51. // address or one that matches the RFC3849 documentation address format.
  52. func (na *NetAddress) Valid() bool {
  53. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  54. na.IP.Equal(net.IPv4bcast))
  55. }
  56. func (na *NetAddress) Local() bool {
  57. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  58. }
  59. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  60. const (
  61. Unreachable = 0
  62. Default = iota
  63. Teredo
  64. Ipv6_weak
  65. Ipv4
  66. Ipv6_strong
  67. Private
  68. )
  69. if !na.Routable() {
  70. return Unreachable
  71. } else if na.RFC4380() {
  72. if !o.Routable() {
  73. return Default
  74. } else if o.RFC4380() {
  75. return Teredo
  76. } else if o.IP.To4() != nil {
  77. return Ipv4
  78. } else { // ipv6
  79. return Ipv6_weak
  80. }
  81. } else if na.IP.To4() != nil {
  82. if o.Routable() && o.IP.To4() != nil {
  83. return Ipv4
  84. }
  85. return Default
  86. } else /* ipv6 */ {
  87. var tunnelled bool
  88. // Is our v6 is tunnelled?
  89. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  90. tunnelled = true
  91. }
  92. if !o.Routable() {
  93. return Default
  94. } else if o.RFC4380() {
  95. return Teredo
  96. } else if o.IP.To4() != nil {
  97. return Ipv4
  98. } else if tunnelled {
  99. // only prioritise ipv6 if we aren't tunnelling it.
  100. return Ipv6_weak
  101. }
  102. return Ipv6_strong
  103. }
  104. }
  105. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  106. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  107. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  108. // RFC3964: IPv6 6to4 (2002::/16)
  109. // RFC4193: IPv6 unique local (FC00::/7)
  110. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  111. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  112. // RFC4862: IPv6 Autoconfig (FE80::/64)
  113. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  114. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  115. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  116. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  117. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  118. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  119. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  120. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  121. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  122. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  123. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  124. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  125. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  126. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  127. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  128. func (na *NetAddress) RFC1918() bool { return rfc1918_10.Contains(na.IP) ||
  129. rfc1918_192.Contains(na.IP) ||
  130. rfc1918_172.Contains(na.IP) }
  131. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  132. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  133. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  134. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  135. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  136. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  137. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  138. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  139. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }