peer_error.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package p2p
  17. import (
  18. "errors"
  19. "fmt"
  20. )
  21. const (
  22. errInvalidMsgCode = iota
  23. errInvalidMsg
  24. )
  25. // Quorum
  26. //
  27. // Constants for peer connection errors
  28. const (
  29. // When permissioning is enabled, and node is not permissioned in the network
  30. errPermissionDenied = iota + 100
  31. // Unauthorized node joining existing raft cluster
  32. errNotInRaftCluster
  33. )
  34. var errorToString = map[int]string{
  35. errInvalidMsgCode: "invalid message code",
  36. errInvalidMsg: "invalid message",
  37. // Quorum
  38. errPermissionDenied: "permission denied",
  39. errNotInRaftCluster: "not in raft cluster",
  40. }
  41. type peerError struct {
  42. code int
  43. message string
  44. }
  45. func newPeerError(code int, format string, v ...interface{}) *peerError {
  46. desc, ok := errorToString[code]
  47. if !ok {
  48. panic("invalid error code")
  49. }
  50. err := &peerError{code, desc}
  51. if format != "" {
  52. err.message += ": " + fmt.Sprintf(format, v...)
  53. }
  54. return err
  55. }
  56. func (pe *peerError) Error() string {
  57. return pe.message
  58. }
  59. var errProtocolReturned = errors.New("protocol returned")
  60. type DiscReason uint
  61. const (
  62. DiscRequested DiscReason = iota
  63. DiscNetworkError
  64. DiscProtocolError
  65. DiscUselessPeer
  66. DiscTooManyPeers
  67. DiscAlreadyConnected
  68. DiscIncompatibleVersion
  69. DiscInvalidIdentity
  70. DiscQuitting
  71. DiscUnexpectedIdentity
  72. DiscSelf
  73. DiscReadTimeout
  74. DiscSubprotocolError = 0x10
  75. DiscAuthError DiscReason = 0x20
  76. )
  77. var discReasonToString = [...]string{
  78. DiscRequested: "disconnect requested",
  79. DiscNetworkError: "network error",
  80. DiscProtocolError: "breach of protocol",
  81. DiscUselessPeer: "useless peer",
  82. DiscTooManyPeers: "too many peers",
  83. DiscAlreadyConnected: "already connected",
  84. DiscIncompatibleVersion: "incompatible p2p protocol version",
  85. DiscInvalidIdentity: "invalid node identity",
  86. DiscQuitting: "client quitting",
  87. DiscUnexpectedIdentity: "unexpected identity",
  88. DiscSelf: "connected to self",
  89. DiscReadTimeout: "read timeout",
  90. DiscSubprotocolError: "subprotocol error",
  91. DiscAuthError: "invalid auth error",
  92. }
  93. func (d DiscReason) String() string {
  94. if len(discReasonToString) < int(d) {
  95. return fmt.Sprintf("unknown disconnect reason %d", d)
  96. }
  97. return discReasonToString[d]
  98. }
  99. func (d DiscReason) Error() string {
  100. return d.String()
  101. }
  102. func discReasonForError(err error) DiscReason {
  103. if reason, ok := err.(DiscReason); ok {
  104. return reason
  105. }
  106. if err == errProtocolReturned {
  107. return DiscQuitting
  108. }
  109. peerError, ok := err.(*peerError)
  110. if ok {
  111. switch peerError.code {
  112. case errInvalidMsgCode, errInvalidMsg:
  113. return DiscProtocolError
  114. default:
  115. return DiscSubprotocolError
  116. }
  117. }
  118. return DiscSubprotocolError
  119. }