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"
|
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-05-07 00:19:14 +03:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
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 (
|
2015-04-27 14:42:30 +03:00
|
|
|
baseProtocolVersion = 4
|
2015-02-05 04:07:58 +02:00
|
|
|
baseProtocolLength = uint64(16)
|
|
|
|
baseProtocolMaxMsgSize = 10 * 1024 * 1024
|
2015-02-13 15:44:00 +02:00
|
|
|
|
2015-04-13 18:34:08 +03:00
|
|
|
pingInterval = 15 * time.Second
|
2015-02-05 04:07:58 +02:00
|
|
|
)
|
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
|
|
|
// Peer represents a connected remote node.
|
2014-10-23 18:57:54 +03:00
|
|
|
type Peer struct {
|
2015-02-27 05:06:55 +02:00
|
|
|
conn net.Conn
|
2015-02-19 02:52:03 +02:00
|
|
|
rw *conn
|
|
|
|
running map[string]*protoRW
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-04-08 18:37:11 +03:00
|
|
|
wg sync.WaitGroup
|
2014-11-21 22:48:49 +02:00
|
|
|
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 {
|
2015-02-19 02:52:03 +02:00
|
|
|
pipe, _ := net.Pipe()
|
2015-02-27 05:06:55 +02:00
|
|
|
msgpipe, _ := MsgPipe()
|
|
|
|
conn := &conn{msgpipe, &protoHandshake{ID: id, Name: name, Caps: caps}}
|
|
|
|
peer := newPeer(pipe, conn, nil)
|
2015-02-05 04:07:58 +02:00
|
|
|
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 {
|
2015-02-19 02:52:03 +02:00
|
|
|
return p.rw.ID
|
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 {
|
2015-02-19 02:52:03 +02:00
|
|
|
return p.rw.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-19 02:52:03 +02:00
|
|
|
// TODO: maybe return copy
|
|
|
|
return p.rw.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-27 05:06:55 +02:00
|
|
|
return p.conn.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-27 05:06:55 +02:00
|
|
|
return p.conn.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-19 02:52:03 +02:00
|
|
|
return fmt.Sprintf("Peer %.8x %v", p.rw.ID[:], p.RemoteAddr())
|
2015-02-05 04:07:58 +02:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:06:55 +02:00
|
|
|
func newPeer(fd net.Conn, conn *conn, protocols []Protocol) *Peer {
|
2015-04-08 18:37:11 +03:00
|
|
|
protomap := matchProtocols(protocols, conn.Caps, conn)
|
2015-02-19 02:52:03 +02:00
|
|
|
p := &Peer{
|
2015-02-27 05:06:55 +02:00
|
|
|
conn: fd,
|
2015-02-19 02:52:03 +02:00
|
|
|
rw: conn,
|
2015-04-08 18:37:11 +03:00
|
|
|
running: protomap,
|
2015-02-19 02:52:03 +02:00
|
|
|
disc: make(chan DiscReason),
|
2015-04-08 18:37:11 +03:00
|
|
|
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
|
2015-02-19 02:52:03 +02:00
|
|
|
closed: make(chan struct{}),
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-02-19 02:52:03 +02:00
|
|
|
return p
|
2015-02-05 04:07:58 +02:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) run() DiscReason {
|
2015-04-08 18:37:11 +03:00
|
|
|
readErr := make(chan error, 1)
|
|
|
|
p.wg.Add(2)
|
|
|
|
go p.readLoop(readErr)
|
|
|
|
go p.pingLoop()
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
p.startProtocols()
|
2015-02-19 17:53:52 +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
|
2015-04-08 18:37:11 +03:00
|
|
|
select {
|
|
|
|
case err := <-readErr:
|
|
|
|
if r, ok := err.(DiscReason); ok {
|
|
|
|
reason = r
|
2015-04-13 18:34:08 +03:00
|
|
|
} else {
|
|
|
|
// Note: We rely on protocols to abort if there is a write
|
|
|
|
// error. It might be more robust to handle them here as well.
|
2015-05-07 00:19:14 +03:00
|
|
|
glog.V(logger.Detail).Infof("%v: Read error: %v\n", p, err)
|
2015-04-13 18:34:08 +03:00
|
|
|
reason = DiscNetworkError
|
2015-02-19 17:53:52 +02:00
|
|
|
}
|
2015-04-08 18:37:11 +03: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
|
|
|
|
2015-04-08 18:37:11 +03:00
|
|
|
close(p.closed)
|
2015-04-13 18:34:08 +03:00
|
|
|
p.politeDisconnect(reason)
|
2015-04-08 18:37:11 +03:00
|
|
|
p.wg.Wait()
|
2015-05-07 00:19:14 +03:00
|
|
|
glog.V(logger.Debug).Infof("%v: Disconnected: %v\n", p, reason)
|
2015-02-05 04:07:58 +02:00
|
|
|
return reason
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (p *Peer) politeDisconnect(reason DiscReason) {
|
2015-04-13 18:34:08 +03:00
|
|
|
if reason != DiscNetworkError {
|
2015-03-19 16:11:02 +02:00
|
|
|
SendItems(p.rw, discMsg, uint(reason))
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-27 05:06:55 +02:00
|
|
|
p.conn.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:37:11 +03:00
|
|
|
func (p *Peer) pingLoop() {
|
|
|
|
ping := time.NewTicker(pingInterval)
|
|
|
|
defer p.wg.Done()
|
|
|
|
defer ping.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ping.C:
|
|
|
|
if err := SendItems(p.rw, pingMsg); err != nil {
|
|
|
|
p.protoErr <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-p.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) readLoop(errc chan<- error) {
|
|
|
|
defer p.wg.Done()
|
2015-02-05 04:07:58 +02:00
|
|
|
for {
|
|
|
|
msg, err := p.rw.ReadMsg()
|
|
|
|
if err != nil {
|
2015-04-08 18:37:11 +03:00
|
|
|
errc <- err
|
|
|
|
return
|
2015-02-05 04:07:58 +02:00
|
|
|
}
|
2015-04-29 23:49:58 +03:00
|
|
|
msg.ReceivedAt = time.Now()
|
2015-02-05 04:07:58 +02:00
|
|
|
if err = p.handle(msg); err != nil {
|
2015-04-08 18:37:11 +03:00
|
|
|
errc <- err
|
|
|
|
return
|
2015-02-05 04:07:58 +02:00
|
|
|
}
|
|
|
|
}
|
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()
|
2015-03-19 16:11:02 +02:00
|
|
|
go SendItems(p.rw, pongMsg)
|
2015-02-05 04:07:58 +02:00
|
|
|
case msg.Code == discMsg:
|
2015-03-04 13:03:43 +02:00
|
|
|
var reason [1]DiscReason
|
2015-04-08 18:37:11 +03:00
|
|
|
// This is the last message. We don't need to discard or
|
|
|
|
// check errors because, the connection will be closed after it.
|
2015-02-05 04:07:58 +02:00
|
|
|
rlp.Decode(msg.Payload, &reason)
|
2015-05-07 00:19:14 +03:00
|
|
|
glog.V(logger.Debug).Infof("%v: Disconnect Requested: %v\n", p, reason[0])
|
2015-04-08 18:37:11 +03:00
|
|
|
return DiscRequested
|
2015-02-05 04:07:58 +02:00
|
|
|
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
|
|
|
}
|
2015-04-08 18:37:11 +03:00
|
|
|
select {
|
|
|
|
case proto.in <- msg:
|
|
|
|
return nil
|
|
|
|
case <-p.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
return nil
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
// matchProtocols creates structures for matching named subprotocols.
|
|
|
|
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
2014-11-21 22:48:49 +02:00
|
|
|
sort.Sort(capsByName(caps))
|
|
|
|
offset := baseProtocolLength
|
2015-02-19 02:52:03 +02:00
|
|
|
result := make(map[string]*protoRW)
|
2014-11-21 22:48:49 +02:00
|
|
|
outer:
|
|
|
|
for _, cap := range caps {
|
2015-02-19 02:52:03 +02:00
|
|
|
for _, proto := range protocols {
|
|
|
|
if proto.Name == cap.Name && proto.Version == cap.Version && result[cap.Name] == nil {
|
|
|
|
result[cap.Name] = &protoRW{Protocol: proto, offset: offset, in: make(chan Msg), w: rw}
|
2014-11-21 22:48:49 +02:00
|
|
|
offset += proto.Length
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 02:52:03 +02:00
|
|
|
return result
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
func (p *Peer) startProtocols() {
|
2015-04-08 18:37:11 +03:00
|
|
|
p.wg.Add(len(p.running))
|
2015-02-19 02:52:03 +02:00
|
|
|
for _, proto := range p.running {
|
|
|
|
proto := proto
|
2015-04-08 18:37:11 +03:00
|
|
|
proto.closed = p.closed
|
2015-05-07 00:19:14 +03:00
|
|
|
glog.V(logger.Detail).Infof("%v: Starting protocol %s/%d\n", p, proto.Name, proto.Version)
|
2015-02-19 02:52:03 +02:00
|
|
|
go func() {
|
|
|
|
err := proto.Run(p, proto)
|
|
|
|
if err == nil {
|
2015-05-07 00:19:14 +03:00
|
|
|
glog.V(logger.Detail).Infof("%v: Protocol %s/%d returned\n", p, proto.Name, proto.Version)
|
2015-02-19 02:52:03 +02:00
|
|
|
err = errors.New("protocol returned")
|
2015-05-07 00:19:14 +03:00
|
|
|
} else if err != io.EOF {
|
|
|
|
glog.V(logger.Detail).Infof("%v: Protocol %s/%d error: \n", p, proto.Name, proto.Version, err)
|
2015-02-19 02:52:03 +02:00
|
|
|
}
|
2015-04-08 18:37:11 +03:00
|
|
|
p.protoErr <- err
|
|
|
|
p.wg.Done()
|
2015-02-19 02:52:03 +02:00
|
|
|
}()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProto finds the protocol responsible for handling
|
|
|
|
// the given message code.
|
2015-02-19 02:52:03 +02:00
|
|
|
func (p *Peer) getProto(code uint64) (*protoRW, error) {
|
2014-11-21 22:48:49 +02:00
|
|
|
for _, proto := range p.running {
|
2015-02-19 02:52:03 +02:00
|
|
|
if code >= proto.offset && code < proto.offset+proto.Length {
|
2014-11-21 22:48:49 +02:00
|
|
|
return proto, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, newPeerError(errInvalidMsgCode, "%d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
proto, ok := p.running[protoName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("protocol %s not handled by peer", protoName)
|
|
|
|
}
|
2015-02-19 02:52:03 +02:00
|
|
|
if msg.Code >= proto.Length {
|
2014-11-21 22:48:49 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
type protoRW struct {
|
|
|
|
Protocol
|
|
|
|
in chan Msg
|
2015-04-08 18:37:11 +03:00
|
|
|
closed <-chan struct{}
|
2015-02-19 02:52:03 +02:00
|
|
|
offset uint64
|
|
|
|
w MsgWriter
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
func (rw *protoRW) WriteMsg(msg Msg) error {
|
|
|
|
if msg.Code >= rw.Length {
|
2014-11-21 22:48:49 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
func (rw *protoRW) ReadMsg() (Msg, error) {
|
2015-04-08 18:37:11 +03:00
|
|
|
select {
|
|
|
|
case msg := <-rw.in:
|
|
|
|
msg.Code -= rw.offset
|
|
|
|
return msg, nil
|
|
|
|
case <-rw.closed:
|
|
|
|
return Msg{}, io.EOF
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|