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.

174 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>",
  37. e.conn.RemoteAddr().String(),
  38. )
  39. }
  40. if e.id != "" {
  41. return fmt.Sprintf("duplicate ID<%v>", e.id)
  42. }
  43. }
  44. if e.isFiltered {
  45. if e.conn != nil {
  46. return fmt.Sprintf(
  47. "filtered CONN<%s>: %s",
  48. e.conn.RemoteAddr().String(),
  49. e.err,
  50. )
  51. }
  52. if e.id != "" {
  53. return fmt.Sprintf("filtered ID<%v>: %s", e.id, e.err)
  54. }
  55. }
  56. if e.isIncompatible {
  57. return fmt.Sprintf("incompatible: %s", e.err)
  58. }
  59. if e.isNodeInfoInvalid {
  60. return fmt.Sprintf("invalid NodeInfo: %s", e.err)
  61. }
  62. if e.isSelf {
  63. return fmt.Sprintf("self ID<%v>", e.id)
  64. }
  65. return fmt.Sprintf("%s", e.err)
  66. }
  67. // IsAuthFailure when Peer authentication was unsuccessful.
  68. func (e ErrRejected) IsAuthFailure() bool { return e.isAuthFailure }
  69. // IsDuplicate when Peer ID or IP are present already.
  70. func (e ErrRejected) IsDuplicate() bool { return e.isDuplicate }
  71. // IsFiltered when Peer ID or IP was filtered.
  72. func (e ErrRejected) IsFiltered() bool { return e.isFiltered }
  73. // IsIncompatible when Peer NodeInfo is not compatible with our own.
  74. func (e ErrRejected) IsIncompatible() bool { return e.isIncompatible }
  75. // IsNodeInfoInvalid when the sent NodeInfo is not valid.
  76. func (e ErrRejected) IsNodeInfoInvalid() bool { return e.isNodeInfoInvalid }
  77. // IsSelf when Peer is our own node.
  78. func (e ErrRejected) IsSelf() bool { return e.isSelf }
  79. // ErrSwitchDuplicatePeerID to be raised when a peer is connecting with a known
  80. // ID.
  81. type ErrSwitchDuplicatePeerID struct {
  82. ID ID
  83. }
  84. func (e ErrSwitchDuplicatePeerID) Error() string {
  85. return fmt.Sprintf("Duplicate peer ID %v", e.ID)
  86. }
  87. // ErrSwitchDuplicatePeerIP to be raised whena a peer is connecting with a known
  88. // IP.
  89. type ErrSwitchDuplicatePeerIP struct {
  90. IP net.IP
  91. }
  92. func (e ErrSwitchDuplicatePeerIP) Error() string {
  93. return fmt.Sprintf("Duplicate peer IP %v", e.IP.String())
  94. }
  95. // ErrSwitchConnectToSelf to be raised when trying to connect to itself.
  96. type ErrSwitchConnectToSelf struct {
  97. Addr *NetAddress
  98. }
  99. func (e ErrSwitchConnectToSelf) Error() string {
  100. return fmt.Sprintf("Connect to self: %v", e.Addr)
  101. }
  102. type ErrSwitchAuthenticationFailure struct {
  103. Dialed *NetAddress
  104. Got ID
  105. }
  106. func (e ErrSwitchAuthenticationFailure) Error() string {
  107. return fmt.Sprintf(
  108. "Failed to authenticate peer. Dialed %v, but got peer with ID %s",
  109. e.Dialed,
  110. e.Got,
  111. )
  112. }
  113. // ErrTransportClosed is raised when the Transport has been closed.
  114. type ErrTransportClosed struct{}
  115. func (e ErrTransportClosed) Error() string {
  116. return "transport has been closed"
  117. }
  118. //-------------------------------------------------------------------
  119. type ErrNetAddressNoID struct {
  120. Addr string
  121. }
  122. func (e ErrNetAddressNoID) Error() string {
  123. return fmt.Sprintf("Address (%s) does not contain ID", e.Addr)
  124. }
  125. type ErrNetAddressInvalid struct {
  126. Addr string
  127. Err error
  128. }
  129. func (e ErrNetAddressInvalid) Error() string {
  130. return fmt.Sprintf("Invalid address (%s): %v", e.Addr, e.Err)
  131. }
  132. type ErrNetAddressLookup struct {
  133. Addr string
  134. Err error
  135. }
  136. func (e ErrNetAddressLookup) Error() string {
  137. return fmt.Sprintf("Error looking up host (%s): %v", e.Addr, e.Err)
  138. }