peer_error.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. var errorToString = map[int]string{
  26. errInvalidMsgCode: "invalid message code",
  27. errInvalidMsg: "invalid message",
  28. }
  29. type peerError struct {
  30. code int
  31. message string
  32. }
  33. func newPeerError(code int, format string, v ...interface{}) *peerError {
  34. desc, ok := errorToString[code]
  35. if !ok {
  36. panic("invalid error code")
  37. }
  38. err := &peerError{code, desc}
  39. if format != "" {
  40. err.message += ": " + fmt.Sprintf(format, v...)
  41. }
  42. return err
  43. }
  44. func (pe *peerError) Error() string {
  45. return pe.message
  46. }
  47. var errProtocolReturned = errors.New("protocol returned")
  48. type DiscReason uint
  49. const (
  50. DiscRequested DiscReason = iota
  51. DiscNetworkError
  52. DiscProtocolError
  53. DiscUselessPeer
  54. DiscTooManyPeers
  55. DiscAlreadyConnected
  56. DiscIncompatibleVersion
  57. DiscInvalidIdentity
  58. DiscQuitting
  59. DiscUnexpectedIdentity
  60. DiscSelf
  61. DiscReadTimeout
  62. DiscSubprotocolError = 0x10
  63. )
  64. var discReasonToString = [...]string{
  65. DiscRequested: "disconnect requested",
  66. DiscNetworkError: "network error",
  67. DiscProtocolError: "breach of protocol",
  68. DiscUselessPeer: "useless peer",
  69. DiscTooManyPeers: "too many peers",
  70. DiscAlreadyConnected: "already connected",
  71. DiscIncompatibleVersion: "incompatible p2p protocol version",
  72. DiscInvalidIdentity: "invalid node identity",
  73. DiscQuitting: "client quitting",
  74. DiscUnexpectedIdentity: "unexpected identity",
  75. DiscSelf: "connected to self",
  76. DiscReadTimeout: "read timeout",
  77. DiscSubprotocolError: "subprotocol error",
  78. }
  79. func (d DiscReason) String() string {
  80. if len(discReasonToString) < int(d) {
  81. return fmt.Sprintf("unknown disconnect reason %d", d)
  82. }
  83. return discReasonToString[d]
  84. }
  85. func (d DiscReason) Error() string {
  86. return d.String()
  87. }
  88. func discReasonForError(err error) DiscReason {
  89. if reason, ok := err.(DiscReason); ok {
  90. return reason
  91. }
  92. if err == errProtocolReturned {
  93. return DiscQuitting
  94. }
  95. peerError, ok := err.(*peerError)
  96. if ok {
  97. switch peerError.code {
  98. case errInvalidMsgCode, errInvalidMsg:
  99. return DiscProtocolError
  100. default:
  101. return DiscSubprotocolError
  102. }
  103. }
  104. return DiscSubprotocolError
  105. }