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.

175 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // ErrFilterTimeout indicates that a filter operation timed out.
  7. type ErrFilterTimeout struct{}
  8. func (e ErrFilterTimeout) Error() string {
  9. return "filter timed out"
  10. }
  11. // ErrRejected indicates that a Peer was rejected carrying additional
  12. // information as to the reason.
  13. type ErrRejected struct {
  14. addr NetAddress
  15. conn net.Conn
  16. err error
  17. id ID
  18. isAuthFailure bool
  19. isDuplicate bool
  20. isFiltered bool
  21. isIncompatible bool
  22. isNodeInfoInvalid bool
  23. isSelf bool
  24. }
  25. // Addr returns the NetAddress for the rejected Peer.
  26. func (e ErrRejected) Addr() NetAddress {
  27. return e.addr
  28. }
  29. func (e ErrRejected) Error() string {
  30. if e.isAuthFailure {
  31. return fmt.Sprintf("auth failure: %s", e.err)
  32. }
  33. if e.isDuplicate {
  34. if e.conn != nil {
  35. return fmt.Sprintf(
  36. "duplicate CONN<%s>: %s",
  37. e.conn.RemoteAddr().String(),
  38. e.err,
  39. )
  40. }
  41. if e.id != "" {
  42. return fmt.Sprintf("duplicate ID<%v>: %s", e.id, e.err)
  43. }
  44. }
  45. if e.isFiltered {
  46. if e.conn != nil {
  47. return fmt.Sprintf(
  48. "filtered CONN<%s>: %s",
  49. e.conn.RemoteAddr().String(),
  50. e.err,
  51. )
  52. }
  53. if e.id != "" {
  54. return fmt.Sprintf("filtered ID<%v>: %s", e.id, e.err)
  55. }
  56. }
  57. if e.isIncompatible {
  58. return fmt.Sprintf("incompatible: %s", e.err)
  59. }
  60. if e.isNodeInfoInvalid {
  61. return fmt.Sprintf("invalid NodeInfo: %s", e.err)
  62. }
  63. if e.isSelf {
  64. return fmt.Sprintf("self ID<%v>", e.id)
  65. }
  66. return fmt.Sprintf("%s", e.err)
  67. }
  68. // IsAuthFailure when Peer authentication was unsuccessful.
  69. func (e ErrRejected) IsAuthFailure() bool { return e.isAuthFailure }
  70. // IsDuplicate when Peer ID or IP are present already.
  71. func (e ErrRejected) IsDuplicate() bool { return e.isDuplicate }
  72. // IsFiltered when Peer ID or IP was filtered.
  73. func (e ErrRejected) IsFiltered() bool { return e.isFiltered }
  74. // IsIncompatible when Peer NodeInfo is not compatible with our own.
  75. func (e ErrRejected) IsIncompatible() bool { return e.isIncompatible }
  76. // IsNodeInfoInvalid when the sent NodeInfo is not valid.
  77. func (e ErrRejected) IsNodeInfoInvalid() bool { return e.isNodeInfoInvalid }
  78. // IsSelf when Peer is our own node.
  79. func (e ErrRejected) IsSelf() bool { return e.isSelf }
  80. // ErrSwitchDuplicatePeerID to be raised when a peer is connecting with a known
  81. // ID.
  82. type ErrSwitchDuplicatePeerID struct {
  83. ID ID
  84. }
  85. func (e ErrSwitchDuplicatePeerID) Error() string {
  86. return fmt.Sprintf("Duplicate peer ID %v", e.ID)
  87. }
  88. // ErrSwitchDuplicatePeerIP to be raised whena a peer is connecting with a known
  89. // IP.
  90. type ErrSwitchDuplicatePeerIP struct {
  91. IP net.IP
  92. }
  93. func (e ErrSwitchDuplicatePeerIP) Error() string {
  94. return fmt.Sprintf("Duplicate peer IP %v", e.IP.String())
  95. }
  96. // ErrSwitchConnectToSelf to be raised when trying to connect to itself.
  97. type ErrSwitchConnectToSelf struct {
  98. Addr *NetAddress
  99. }
  100. func (e ErrSwitchConnectToSelf) Error() string {
  101. return fmt.Sprintf("Connect to self: %v", e.Addr)
  102. }
  103. type ErrSwitchAuthenticationFailure struct {
  104. Dialed *NetAddress
  105. Got ID
  106. }
  107. func (e ErrSwitchAuthenticationFailure) Error() string {
  108. return fmt.Sprintf(
  109. "Failed to authenticate peer. Dialed %v, but got peer with ID %s",
  110. e.Dialed,
  111. e.Got,
  112. )
  113. }
  114. // ErrTransportClosed is raised when the Transport has been closed.
  115. type ErrTransportClosed struct{}
  116. func (e ErrTransportClosed) Error() string {
  117. return "transport has been closed"
  118. }
  119. //-------------------------------------------------------------------
  120. type ErrNetAddressNoID struct {
  121. Addr string
  122. }
  123. func (e ErrNetAddressNoID) Error() string {
  124. return fmt.Sprintf("Address (%s) does not contain ID", e.Addr)
  125. }
  126. type ErrNetAddressInvalid struct {
  127. Addr string
  128. Err error
  129. }
  130. func (e ErrNetAddressInvalid) Error() string {
  131. return fmt.Sprintf("Invalid address (%s): %v", e.Addr, e.Err)
  132. }
  133. type ErrNetAddressLookup struct {
  134. Addr string
  135. Err error
  136. }
  137. func (e ErrNetAddressLookup) Error() string {
  138. return fmt.Sprintf("Error looking up host (%s): %v", e.Addr, e.Err)
  139. }