2014-10-23 18:57:54 +03:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-02-07 01:13:22 +02:00
|
|
|
"errors"
|
2014-10-23 18:57:54 +03:00
|
|
|
"fmt"
|
2014-11-21 22:48:49 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-10-23 18:57:54 +03:00
|
|
|
"net"
|
2014-11-21 22:48:49 +02:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2015-02-05 04:07:58 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-10-23 18:57:54 +03:00
|
|
|
)
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
const (
|
|
|
|
// maximum amount of time allowed for reading a message
|
|
|
|
msgReadTimeout = 5 * time.Second
|
|
|
|
// maximum amount of time allowed for writing a message
|
|
|
|
msgWriteTimeout = 5 * time.Second
|
|
|
|
// messages smaller than this many bytes will be read at
|
|
|
|
// once before passing them to a protocol.
|
|
|
|
wholePayloadSize = 64 * 1024
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
disconnectGracePeriod = 2 * time.Second
|
|
|
|
)
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
const (
|
|
|
|
baseProtocolVersion = 2
|
|
|
|
baseProtocolLength = uint64(16)
|
|
|
|
baseProtocolMaxMsgSize = 10 * 1024 * 1024
|
|
|
|
)
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
const (
|
|
|
|
// devp2p message codes
|
|
|
|
handshakeMsg = 0x00
|
|
|
|
discMsg = 0x01
|
|
|
|
pingMsg = 0x02
|
|
|
|
pongMsg = 0x03
|
|
|
|
getPeersMsg = 0x04
|
|
|
|
peersMsg = 0x05
|
|
|
|
)
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// handshake is the RLP structure of the protocol handshake.
|
|
|
|
type handshake struct {
|
|
|
|
Version uint64
|
|
|
|
Name string
|
|
|
|
Caps []Cap
|
|
|
|
ListenPort uint64
|
|
|
|
NodeID discover.NodeID
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// Peer represents a connected remote node.
|
2014-10-23 18:57:54 +03:00
|
|
|
type Peer struct {
|
2014-11-21 22:48:49 +02:00
|
|
|
// Peers have all the log methods.
|
|
|
|
// Use them to display messages related to the peer.
|
|
|
|
*logger.Logger
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
infoMu sync.Mutex
|
|
|
|
name string
|
|
|
|
caps []Cap
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
ourID, remoteID *discover.NodeID
|
|
|
|
ourName string
|
|
|
|
|
|
|
|
rw *frameRW
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
// These fields maintain the running protocols.
|
2015-02-05 04:07:58 +02:00
|
|
|
protocols []Protocol
|
|
|
|
runlock sync.RWMutex // protects running
|
|
|
|
running map[string]*proto
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-07 01:13:22 +02:00
|
|
|
// disables protocol handshake, for testing
|
|
|
|
noHandshake bool
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
protoWG sync.WaitGroup
|
|
|
|
protoErr chan error
|
|
|
|
closed chan struct{}
|
|
|
|
disc chan DiscReason
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeer returns a peer for testing purposes.
|
2015-02-05 04:07:58 +02:00
|
|
|
func NewPeer(id discover.NodeID, name string, caps []Cap) *Peer {
|
2014-11-21 22:48:49 +02:00
|
|
|
conn, _ := net.Pipe()
|
2015-02-05 04:07:58 +02:00
|
|
|
peer := newPeer(conn, nil, "", nil, &id)
|
|
|
|
peer.setHandshakeInfo(name, caps)
|
|
|
|
close(peer.closed) // ensures Disconnect doesn't block
|
2014-10-23 18:57:54 +03:00
|
|
|
return peer
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// ID returns the node's public key.
|
|
|
|
func (p *Peer) ID() discover.NodeID {
|
|
|
|
return *p.remoteID
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// Name returns the node name that the remote node advertised.
|
|
|
|
func (p *Peer) Name() string {
|
|
|
|
// this needs a lock because the information is part of the
|
|
|
|
// protocol handshake.
|
|
|
|
p.infoMu.Lock()
|
|
|
|
name := p.name
|
|
|
|
p.infoMu.Unlock()
|
|
|
|
return name
|
2015-01-18 09:59:54 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Caps returns the capabilities (supported subprotocols) of the remote peer.
|
|
|
|
func (p *Peer) Caps() []Cap {
|
2015-02-05 04:07:58 +02:00
|
|
|
// this needs a lock because the information is part of the
|
|
|
|
// protocol handshake.
|
|
|
|
p.infoMu.Lock()
|
|
|
|
caps := p.caps
|
|
|
|
p.infoMu.Unlock()
|
|
|
|
return caps
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote address of the network connection.
|
|
|
|
func (p *Peer) RemoteAddr() net.Addr {
|
2015-02-05 04:07:58 +02:00
|
|
|
return p.rw.RemoteAddr()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local address of the network connection.
|
|
|
|
func (p *Peer) LocalAddr() net.Addr {
|
2015-02-05 04:07:58 +02:00
|
|
|
return p.rw.LocalAddr()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect terminates the peer connection with the given reason.
|
|
|
|
// It returns immediately and does not wait until the connection is closed.
|
|
|
|
func (p *Peer) Disconnect(reason DiscReason) {
|
|
|
|
select {
|
|
|
|
case p.disc <- reason:
|
|
|
|
case <-p.closed:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer.
|
|
|
|
func (p *Peer) String() string {
|
2015-02-07 01:13:22 +02:00
|
|
|
return fmt.Sprintf("Peer %.8x %v", p.remoteID[:], p.RemoteAddr())
|
2015-02-05 04:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPeer(conn net.Conn, protocols []Protocol, ourName string, ourID, remoteID *discover.NodeID) *Peer {
|
2015-02-07 01:13:22 +02:00
|
|
|
logtag := fmt.Sprintf("Peer %.8x %v", remoteID[:], conn.RemoteAddr())
|
2015-02-05 04:07:58 +02:00
|
|
|
return &Peer{
|
|
|
|
Logger: logger.NewLogger(logtag),
|
|
|
|
rw: newFrameRW(conn, msgWriteTimeout),
|
|
|
|
ourID: ourID,
|
|
|
|
ourName: ourName,
|
|
|
|
remoteID: remoteID,
|
|
|
|
protocols: protocols,
|
|
|
|
running: make(map[string]*proto),
|
|
|
|
disc: make(chan DiscReason),
|
|
|
|
protoErr: make(chan error),
|
|
|
|
closed: make(chan struct{}),
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) setHandshakeInfo(name string, caps []Cap) {
|
|
|
|
p.infoMu.Lock()
|
|
|
|
p.name = name
|
|
|
|
p.caps = caps
|
|
|
|
p.infoMu.Unlock()
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) run() DiscReason {
|
|
|
|
var readErr = make(chan error, 1)
|
2014-11-21 22:48:49 +02:00
|
|
|
defer p.closeProtocols()
|
|
|
|
defer close(p.closed)
|
2015-02-05 04:07:58 +02:00
|
|
|
|
|
|
|
go func() { readErr <- p.readLoop() }()
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-07 01:13:22 +02:00
|
|
|
if !p.noHandshake {
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := writeProtocolHandshake(p.rw, p.ourName, *p.ourID, p.protocols); err != nil {
|
|
|
|
p.DebugDetailf("Protocol handshake error: %v\n", err)
|
2015-02-07 01:13:22 +02:00
|
|
|
p.rw.Close()
|
2015-02-05 04:07:58 +02:00
|
|
|
return DiscProtocolError
|
2015-01-18 09:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 01:13:22 +02:00
|
|
|
// Wait for an error or disconnect.
|
2015-02-05 04:07:58 +02:00
|
|
|
var reason DiscReason
|
|
|
|
select {
|
|
|
|
case err := <-readErr:
|
|
|
|
// We rely on protocols to abort if there is a write error. It
|
|
|
|
// might be more robust to handle them here as well.
|
|
|
|
p.DebugDetailf("Read error: %v\n", err)
|
2015-02-07 01:13:22 +02:00
|
|
|
p.rw.Close()
|
|
|
|
return DiscNetworkError
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
case err := <-p.protoErr:
|
|
|
|
reason = discReasonForError(err)
|
|
|
|
case reason = <-p.disc:
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-07 01:13:22 +02:00
|
|
|
p.politeDisconnect(reason)
|
|
|
|
|
|
|
|
// Wait for readLoop. It will end because conn is now closed.
|
|
|
|
<-readErr
|
2015-02-05 04:07:58 +02:00
|
|
|
p.Debugf("Disconnected: %v\n", reason)
|
|
|
|
return reason
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) politeDisconnect(reason DiscReason) {
|
2014-11-21 22:48:49 +02:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2015-02-05 04:07:58 +02:00
|
|
|
EncodeMsg(p.rw, discMsg, uint(reason))
|
2015-02-07 01:13:22 +02:00
|
|
|
// Wait for the other side to close the connection.
|
|
|
|
// Discard any data that they send until then.
|
2015-02-05 04:07:58 +02:00
|
|
|
io.Copy(ioutil.Discard, p.rw)
|
2014-11-21 22:48:49 +02:00
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(disconnectGracePeriod):
|
|
|
|
}
|
2015-02-07 01:13:22 +02:00
|
|
|
p.rw.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) readLoop() error {
|
2015-02-07 01:13:22 +02:00
|
|
|
if !p.noHandshake {
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := readProtocolHandshake(p, p.rw); err != nil {
|
|
|
|
return err
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
for {
|
|
|
|
msg, err := p.rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = p.handle(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) handle(msg Msg) error {
|
|
|
|
switch {
|
|
|
|
case msg.Code == pingMsg:
|
|
|
|
msg.Discard()
|
|
|
|
go EncodeMsg(p.rw, pongMsg)
|
|
|
|
case msg.Code == discMsg:
|
|
|
|
var reason DiscReason
|
|
|
|
// no need to discard or for error checking, we'll close the
|
|
|
|
// connection after this.
|
|
|
|
rlp.Decode(msg.Payload, &reason)
|
|
|
|
p.Disconnect(DiscRequested)
|
|
|
|
return discRequestedError(reason)
|
|
|
|
case msg.Code < baseProtocolLength:
|
|
|
|
// ignore other base protocol messages
|
|
|
|
return msg.Discard()
|
|
|
|
default:
|
|
|
|
// it's a subprotocol message
|
|
|
|
proto, err := p.getProto(msg.Code)
|
2014-11-21 22:48:49 +02:00
|
|
|
if err != nil {
|
2015-02-05 04:07:58 +02:00
|
|
|
return fmt.Errorf("msg code out of range: %v", msg.Code)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
proto.in <- msg
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
return nil
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func readProtocolHandshake(p *Peer, rw MsgReadWriter) error {
|
|
|
|
// read and handle remote handshake
|
|
|
|
msg, err := rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-26 18:16:23 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if msg.Code != handshakeMsg {
|
|
|
|
return newPeerError(errProtocolBreach, "expected handshake, got %x", msg.Code)
|
2015-01-21 18:53:13 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if msg.Size > baseProtocolMaxMsgSize {
|
2015-02-07 01:13:22 +02:00
|
|
|
return newPeerError(errInvalidMsg, "message too big")
|
2015-01-19 13:21:13 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
var hs handshake
|
|
|
|
if err := msg.Decode(&hs); err != nil {
|
|
|
|
return err
|
2015-01-26 18:16:23 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
// validate handshake info
|
|
|
|
if hs.Version != baseProtocolVersion {
|
|
|
|
return newPeerError(errP2PVersionMismatch, "required version %d, received %d\n",
|
|
|
|
baseProtocolVersion, hs.Version)
|
2015-01-19 13:21:13 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if hs.NodeID == *p.remoteID {
|
|
|
|
return newPeerError(errPubkeyForbidden, "node ID mismatch")
|
2015-01-20 01:42:13 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
// TODO: remove Caps with empty name
|
|
|
|
p.setHandshakeInfo(hs.Name, hs.Caps)
|
|
|
|
p.startSubprotocols(hs.Caps)
|
|
|
|
return nil
|
2015-01-18 09:59:54 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func writeProtocolHandshake(w MsgWriter, name string, id discover.NodeID, ps []Protocol) error {
|
|
|
|
var caps []interface{}
|
|
|
|
for _, proto := range ps {
|
|
|
|
caps = append(caps, proto.cap())
|
|
|
|
}
|
|
|
|
return EncodeMsg(w, handshakeMsg, baseProtocolVersion, name, caps, 0, id)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// startProtocols starts matching named subprotocols.
|
|
|
|
func (p *Peer) startSubprotocols(caps []Cap) {
|
|
|
|
sort.Sort(capsByName(caps))
|
|
|
|
p.runlock.Lock()
|
|
|
|
defer p.runlock.Unlock()
|
|
|
|
offset := baseProtocolLength
|
|
|
|
outer:
|
|
|
|
for _, cap := range caps {
|
|
|
|
for _, proto := range p.protocols {
|
|
|
|
if proto.Name == cap.Name &&
|
|
|
|
proto.Version == cap.Version &&
|
|
|
|
p.running[cap.Name] == nil {
|
|
|
|
p.running[cap.Name] = p.startProto(offset, proto)
|
|
|
|
offset += proto.Length
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) startProto(offset uint64, impl Protocol) *proto {
|
2015-02-05 04:07:58 +02:00
|
|
|
p.DebugDetailf("Starting protocol %s/%d\n", impl.Name, impl.Version)
|
2014-11-21 22:48:49 +02:00
|
|
|
rw := &proto{
|
2015-02-05 04:07:58 +02:00
|
|
|
name: impl.Name,
|
2014-11-21 22:48:49 +02:00
|
|
|
in: make(chan Msg),
|
|
|
|
offset: offset,
|
|
|
|
maxcode: impl.Length,
|
2015-02-05 04:07:58 +02:00
|
|
|
w: p.rw,
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
p.protoWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
err := impl.Run(p, rw)
|
|
|
|
if err == nil {
|
2015-02-05 04:07:58 +02:00
|
|
|
p.DebugDetailf("Protocol %s/%d returned\n", impl.Name, impl.Version)
|
2015-02-07 01:13:22 +02:00
|
|
|
err = errors.New("protocol returned")
|
2014-11-21 22:48:49 +02:00
|
|
|
} else {
|
2015-02-05 04:07:58 +02:00
|
|
|
p.DebugDetailf("Protocol %s/%d error: %v\n", impl.Name, impl.Version, err)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case p.protoErr <- err:
|
|
|
|
case <-p.closed:
|
|
|
|
}
|
|
|
|
p.protoWG.Done()
|
|
|
|
}()
|
|
|
|
return rw
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProto finds the protocol responsible for handling
|
|
|
|
// the given message code.
|
|
|
|
func (p *Peer) getProto(code uint64) (*proto, error) {
|
|
|
|
p.runlock.RLock()
|
|
|
|
defer p.runlock.RUnlock()
|
|
|
|
for _, proto := range p.running {
|
|
|
|
if code >= proto.offset && code < proto.offset+proto.maxcode {
|
|
|
|
return proto, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, newPeerError(errInvalidMsgCode, "%d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) closeProtocols() {
|
|
|
|
p.runlock.RLock()
|
|
|
|
for _, p := range p.running {
|
|
|
|
close(p.in)
|
|
|
|
}
|
|
|
|
p.runlock.RUnlock()
|
|
|
|
p.protoWG.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeProtoMsg sends the given message on behalf of the given named protocol.
|
2015-02-05 04:07:58 +02:00
|
|
|
// this exists because of Server.Broadcast.
|
2014-11-21 22:48:49 +02:00
|
|
|
func (p *Peer) writeProtoMsg(protoName string, msg Msg) error {
|
|
|
|
p.runlock.RLock()
|
|
|
|
proto, ok := p.running[protoName]
|
|
|
|
p.runlock.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("protocol %s not handled by peer", protoName)
|
|
|
|
}
|
|
|
|
if msg.Code >= proto.maxcode {
|
|
|
|
return newPeerError(errInvalidMsgCode, "code %x is out of range for protocol %q", msg.Code, protoName)
|
|
|
|
}
|
|
|
|
msg.Code += proto.offset
|
2015-02-05 04:07:58 +02:00
|
|
|
return p.rw.WriteMsg(msg)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type proto struct {
|
|
|
|
name string
|
|
|
|
in chan Msg
|
|
|
|
maxcode, offset uint64
|
2015-02-05 04:07:58 +02:00
|
|
|
w MsgWriter
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *proto) WriteMsg(msg Msg) error {
|
|
|
|
if msg.Code >= rw.maxcode {
|
|
|
|
return newPeerError(errInvalidMsgCode, "not handled")
|
|
|
|
}
|
|
|
|
msg.Code += rw.offset
|
2015-02-05 04:07:58 +02:00
|
|
|
return rw.w.WriteMsg(msg)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func (rw *proto) ReadMsg() (Msg, error) {
|
|
|
|
msg, ok := <-rw.in
|
|
|
|
if !ok {
|
|
|
|
return msg, io.EOF
|
|
|
|
}
|
|
|
|
msg.Code -= rw.offset
|
|
|
|
return msg, nil
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|