peer_error.go 2.96 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

zelig's avatar
zelig committed
17 18 19
package p2p

import (
20
	"errors"
zelig's avatar
zelig committed
21 22 23 24
	"fmt"
)

const (
25
	errInvalidMsgCode = iota
26
	errInvalidMsg
zelig's avatar
zelig committed
27 28
)

29
var errorToString = map[int]string{
30 31
	errInvalidMsgCode: "invalid message code",
	errInvalidMsg:     "invalid message",
zelig's avatar
zelig committed
32 33
}

34
type peerError struct {
35
	code    int
zelig's avatar
zelig committed
36 37 38
	message string
}

39
func newPeerError(code int, format string, v ...interface{}) *peerError {
zelig's avatar
zelig committed
40 41 42 43
	desc, ok := errorToString[code]
	if !ok {
		panic("invalid error code")
	}
44 45 46 47 48
	err := &peerError{code, desc}
	if format != "" {
		err.message += ": " + fmt.Sprintf(format, v...)
	}
	return err
zelig's avatar
zelig committed
49 50
}

51
func (self *peerError) Error() string {
zelig's avatar
zelig committed
52 53 54
	return self.message
}

55 56
var errProtocolReturned = errors.New("protocol returned")

57
type DiscReason uint
58 59

const (
60 61 62 63 64 65 66 67 68 69 70 71
	DiscRequested DiscReason = iota
	DiscNetworkError
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscUnexpectedIdentity
	DiscSelf
	DiscReadTimeout
72
	DiscSubprotocolError = 0x10
73 74
)

75
var discReasonToString = [...]string{
76 77 78 79 80 81 82 83 84 85 86 87 88
	DiscRequested:           "disconnect requested",
	DiscNetworkError:        "network error",
	DiscProtocolError:       "breach of protocol",
	DiscUselessPeer:         "useless peer",
	DiscTooManyPeers:        "too many peers",
	DiscAlreadyConnected:    "already connected",
	DiscIncompatibleVersion: "incompatible p2p protocol version",
	DiscInvalidIdentity:     "invalid node identity",
	DiscQuitting:            "client quitting",
	DiscUnexpectedIdentity:  "unexpected identity",
	DiscSelf:                "connected to self",
	DiscReadTimeout:         "read timeout",
	DiscSubprotocolError:    "subprotocol error",
89 90 91 92
}

func (d DiscReason) String() string {
	if len(discReasonToString) < int(d) {
93
		return fmt.Sprintf("unknown disconnect reason %d", d)
94 95 96 97
	}
	return discReasonToString[d]
}

98 99
func (d DiscReason) Error() string {
	return d.String()
100 101
}

102
func discReasonForError(err error) DiscReason {
103 104
	if reason, ok := err.(DiscReason); ok {
		return reason
105
	}
106 107 108
	if err == errProtocolReturned {
		return DiscQuitting
	}
109
	peerError, ok := err.(*peerError)
110 111 112 113 114 115 116
	if ok {
		switch peerError.code {
		case errInvalidMsgCode, errInvalidMsg:
			return DiscProtocolError
		default:
			return DiscSubprotocolError
		}
117
	}
118
	return DiscSubprotocolError
zelig's avatar
zelig committed
119
}