peer_error.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package p2p
  2. import (
  3. "fmt"
  4. )
  5. const (
  6. errMagicTokenMismatch = iota
  7. errRead
  8. errWrite
  9. errMisc
  10. errInvalidMsgCode
  11. errInvalidMsg
  12. errP2PVersionMismatch
  13. errPubkeyMissing
  14. errPubkeyInvalid
  15. errPubkeyForbidden
  16. errProtocolBreach
  17. errPingTimeout
  18. errInvalidNetworkId
  19. errInvalidProtocolVersion
  20. )
  21. var errorToString = map[int]string{
  22. errMagicTokenMismatch: "Magic token mismatch",
  23. errRead: "Read error",
  24. errWrite: "Write error",
  25. errMisc: "Misc error",
  26. errInvalidMsgCode: "Invalid message code",
  27. errInvalidMsg: "Invalid message",
  28. errP2PVersionMismatch: "P2P Version Mismatch",
  29. errPubkeyMissing: "Public key missing",
  30. errPubkeyInvalid: "Public key invalid",
  31. errPubkeyForbidden: "Public key forbidden",
  32. errProtocolBreach: "Protocol Breach",
  33. errPingTimeout: "Ping timeout",
  34. errInvalidNetworkId: "Invalid network id",
  35. errInvalidProtocolVersion: "Invalid protocol version",
  36. }
  37. type peerError struct {
  38. Code int
  39. message string
  40. }
  41. func newPeerError(code int, format string, v ...interface{}) *peerError {
  42. desc, ok := errorToString[code]
  43. if !ok {
  44. panic("invalid error code")
  45. }
  46. err := &peerError{code, desc}
  47. if format != "" {
  48. err.message += ": " + fmt.Sprintf(format, v...)
  49. }
  50. return err
  51. }
  52. func (self *peerError) Error() string {
  53. return self.message
  54. }
  55. type DiscReason byte
  56. const (
  57. DiscRequested DiscReason = 0x00
  58. DiscNetworkError = 0x01
  59. DiscProtocolError = 0x02
  60. DiscUselessPeer = 0x03
  61. DiscTooManyPeers = 0x04
  62. DiscAlreadyConnected = 0x05
  63. DiscIncompatibleVersion = 0x06
  64. DiscInvalidIdentity = 0x07
  65. DiscQuitting = 0x08
  66. DiscUnexpectedIdentity = 0x09
  67. DiscSelf = 0x0a
  68. DiscReadTimeout = 0x0b
  69. DiscSubprotocolError = 0x10
  70. )
  71. var discReasonToString = [DiscSubprotocolError + 1]string{
  72. DiscRequested: "Disconnect requested",
  73. DiscNetworkError: "Network error",
  74. DiscProtocolError: "Breach of protocol",
  75. DiscUselessPeer: "Useless peer",
  76. DiscTooManyPeers: "Too many peers",
  77. DiscAlreadyConnected: "Already connected",
  78. DiscIncompatibleVersion: "Incompatible P2P protocol version",
  79. DiscInvalidIdentity: "Invalid node identity",
  80. DiscQuitting: "Client quitting",
  81. DiscUnexpectedIdentity: "Unexpected identity",
  82. DiscSelf: "Connected to self",
  83. DiscReadTimeout: "Read timeout",
  84. DiscSubprotocolError: "Subprotocol error",
  85. }
  86. func (d DiscReason) String() string {
  87. if len(discReasonToString) < int(d) {
  88. return fmt.Sprintf("Unknown Reason(%d)", d)
  89. }
  90. return discReasonToString[d]
  91. }
  92. type discRequestedError DiscReason
  93. func (err discRequestedError) Error() string {
  94. return fmt.Sprintf("disconnect requested: %v", DiscReason(err))
  95. }
  96. func discReasonForError(err error) DiscReason {
  97. if reason, ok := err.(discRequestedError); ok {
  98. return DiscReason(reason)
  99. }
  100. peerError, ok := err.(*peerError)
  101. if !ok {
  102. return DiscSubprotocolError
  103. }
  104. switch peerError.Code {
  105. case errP2PVersionMismatch:
  106. return DiscIncompatibleVersion
  107. case errPubkeyMissing, errPubkeyInvalid:
  108. return DiscInvalidIdentity
  109. case errPubkeyForbidden:
  110. return DiscUselessPeer
  111. case errInvalidMsgCode, errMagicTokenMismatch, errProtocolBreach:
  112. return DiscProtocolError
  113. case errPingTimeout:
  114. return DiscReadTimeout
  115. case errRead, errWrite, errMisc:
  116. return DiscNetworkError
  117. default:
  118. return DiscSubprotocolError
  119. }
  120. }