2014-10-23 15:57:54 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-02-06 23:13:22 +00:00
|
|
|
"errors"
|
2014-10-23 15:57:54 +00:00
|
|
|
"fmt"
|
2014-11-21 20:48:49 +00:00
|
|
|
"io"
|
2014-10-23 15:57:54 +00:00
|
|
|
"net"
|
2014-11-21 20:48:49 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2015-05-06 21:19:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2015-02-05 02:07:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-10-23 15:57:54 +00:00
|
|
|
)
|
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
const (
|
2015-04-27 11:42:30 +00:00
|
|
|
baseProtocolVersion = 4
|
2015-02-05 02:07:58 +00:00
|
|
|
baseProtocolLength = uint64(16)
|
2015-05-17 23:14:35 +00:00
|
|
|
baseProtocolMaxMsgSize = 2 * 1024
|
2015-02-13 13:44:00 +00:00
|
|
|
|
2015-04-13 15:34:08 +00:00
|
|
|
pingInterval = 15 * time.Second
|
2015-02-05 02:07:58 +00:00
|
|
|
)
|
2014-11-21 20:48:49 +00:00
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
const (
|
|
|
|
// devp2p message codes
|
|
|
|
handshakeMsg = 0x00
|
|
|
|
discMsg = 0x01
|
|
|
|
pingMsg = 0x02
|
|
|
|
pongMsg = 0x03
|
|
|
|
getPeersMsg = 0x04
|
|
|
|
peersMsg = 0x05
|
|
|
|
)
|
2014-11-21 20:48:49 +00:00
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
// Peer represents a connected remote node.
|
2014-10-23 15:57:54 +00:00
|
|
|
type Peer struct {
|
2015-02-27 03:06:55 +00:00
|
|
|
conn net.Conn
|
2015-02-19 00:52:03 +00:00
|
|
|
rw *conn
|
|
|
|
running map[string]*protoRW
|
2014-11-21 20:48:49 +00:00
|
|
|
|
2015-04-08 15:37:11 +00:00
|
|
|
wg sync.WaitGroup
|
2014-11-21 20:48:49 +00:00
|
|
|
protoErr chan error
|
|
|
|
closed chan struct{}
|
|
|
|
disc chan DiscReason
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeer returns a peer for testing purposes.
|
2015-02-05 02:07:58 +00:00
|
|
|
func NewPeer(id discover.NodeID, name string, caps []Cap) *Peer {
|
2015-02-19 00:52:03 +00:00
|
|
|
pipe, _ := net.Pipe()
|
2015-02-27 03:06:55 +00:00
|
|
|
msgpipe, _ := MsgPipe()
|
|
|
|
conn := &conn{msgpipe, &protoHandshake{ID: id, Name: name, Caps: caps}}
|
|
|
|
peer := newPeer(pipe, conn, nil)
|
2015-02-05 02:07:58 +00:00
|
|
|
close(peer.closed) // ensures Disconnect doesn't block
|
2014-10-23 15:57:54 +00:00
|
|
|
return peer
|
|
|
|
}
|
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
// ID returns the node's public key.
|
|
|
|
func (p *Peer) ID() discover.NodeID {
|
2015-02-19 00:52:03 +00:00
|
|
|
return p.rw.ID
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
// Name returns the node name that the remote node advertised.
|
|
|
|
func (p *Peer) Name() string {
|
2015-02-19 00:52:03 +00:00
|
|
|
return p.rw.Name
|
2015-01-18 07:59:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 20:48:49 +00:00
|
|
|
// Caps returns the capabilities (supported subprotocols) of the remote peer.
|
|
|
|
func (p *Peer) Caps() []Cap {
|
2015-02-19 00:52:03 +00:00
|
|
|
// TODO: maybe return copy
|
|
|
|
return p.rw.Caps
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote address of the network connection.
|
|
|
|
func (p *Peer) RemoteAddr() net.Addr {
|
2015-02-27 03:06:55 +00:00
|
|
|
return p.conn.RemoteAddr()
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local address of the network connection.
|
|
|
|
func (p *Peer) LocalAddr() net.Addr {
|
2015-02-27 03:06:55 +00:00
|
|
|
return p.conn.LocalAddr()
|
2014-11-21 20:48:49 +00: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 00:52:03 +00:00
|
|
|
return fmt.Sprintf("Peer %.8x %v", p.rw.ID[:], p.RemoteAddr())
|
2015-02-05 02:07:58 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 03:06:55 +00:00
|
|
|
func newPeer(fd net.Conn, conn *conn, protocols []Protocol) *Peer {
|
2015-04-08 15:37:11 +00:00
|
|
|
protomap := matchProtocols(protocols, conn.Caps, conn)
|
2015-02-19 00:52:03 +00:00
|
|
|
p := &Peer{
|
2015-02-27 03:06:55 +00:00
|
|
|
conn: fd,
|
2015-02-19 00:52:03 +00:00
|
|
|
rw: conn,
|
2015-04-08 15:37:11 +00:00
|
|
|
running: protomap,
|
2015-02-19 00:52:03 +00:00
|
|
|
disc: make(chan DiscReason),
|
2015-04-08 15:37:11 +00:00
|
|
|
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
|
2015-02-19 00:52:03 +00:00
|
|
|
closed: make(chan struct{}),
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|
2015-02-19 00:52:03 +00:00
|
|
|
return p
|
2015-02-05 02:07:58 +00:00
|
|
|
}
|
2014-11-21 20:48:49 +00:00
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
func (p *Peer) run() DiscReason {
|
2015-04-08 15:37:11 +00:00
|
|
|
readErr := make(chan error, 1)
|
|
|
|
p.wg.Add(2)
|
|
|
|
go p.readLoop(readErr)
|
|
|
|
go p.pingLoop()
|
2015-02-05 02:07:58 +00:00
|
|
|
|
2015-02-19 00:52:03 +00:00
|
|
|
p.startProtocols()
|
2015-02-19 15:53:52 +00:00
|
|
|
|
2015-02-06 23:13:22 +00:00
|
|
|
// Wait for an error or disconnect.
|
2015-02-05 02:07:58 +00:00
|
|
|
var reason DiscReason
|
2015-04-08 15:37:11 +00:00
|
|
|
select {
|
|
|
|
case err := <-readErr:
|
|
|
|
if r, ok := err.(DiscReason); ok {
|
|
|
|
reason = r
|
2015-04-13 15:34:08 +00: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-06 21:19:14 +00:00
|
|
|
glog.V(logger.Detail).Infof("%v: Read error: %v\n", p, err)
|
2015-04-13 15:34:08 +00:00
|
|
|
reason = DiscNetworkError
|
2015-02-19 15:53:52 +00:00
|
|
|
}
|
2015-04-08 15:37:11 +00:00
|
|
|
case err := <-p.protoErr:
|
|
|
|
reason = discReasonForError(err)
|
|
|
|
case reason = <-p.disc:
|
2015-05-14 01:04:04 +00:00
|
|
|
p.politeDisconnect(reason)
|
|
|
|
reason = DiscRequested
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
2015-02-06 23:13:22 +00:00
|
|
|
|
2015-04-08 15:37:11 +00:00
|
|
|
close(p.closed)
|
|
|
|
p.wg.Wait()
|
2015-05-06 21:19:14 +00:00
|
|
|
glog.V(logger.Debug).Infof("%v: Disconnected: %v\n", p, reason)
|
2015-02-05 02:07:58 +00:00
|
|
|
return reason
|
|
|
|
}
|
2014-11-21 20:48:49 +00:00
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
func (p *Peer) politeDisconnect(reason DiscReason) {
|
2015-04-13 15:34:08 +00:00
|
|
|
if reason != DiscNetworkError {
|
2015-03-19 14:11:02 +00:00
|
|
|
SendItems(p.rw, discMsg, uint(reason))
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
2015-02-27 03:06:55 +00:00
|
|
|
p.conn.Close()
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:37:11 +00: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 02:07:58 +00:00
|
|
|
for {
|
|
|
|
msg, err := p.rw.ReadMsg()
|
|
|
|
if err != nil {
|
2015-04-08 15:37:11 +00:00
|
|
|
errc <- err
|
|
|
|
return
|
2015-02-05 02:07:58 +00:00
|
|
|
}
|
2015-04-29 20:49:58 +00:00
|
|
|
msg.ReceivedAt = time.Now()
|
2015-02-05 02:07:58 +00:00
|
|
|
if err = p.handle(msg); err != nil {
|
2015-04-08 15:37:11 +00:00
|
|
|
errc <- err
|
|
|
|
return
|
2015-02-05 02:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
func (p *Peer) handle(msg Msg) error {
|
|
|
|
switch {
|
|
|
|
case msg.Code == pingMsg:
|
|
|
|
msg.Discard()
|
2015-03-19 14:11:02 +00:00
|
|
|
go SendItems(p.rw, pongMsg)
|
2015-02-05 02:07:58 +00:00
|
|
|
case msg.Code == discMsg:
|
2015-03-04 11:03:43 +00:00
|
|
|
var reason [1]DiscReason
|
2015-04-08 15:37:11 +00: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 02:07:58 +00:00
|
|
|
rlp.Decode(msg.Payload, &reason)
|
2015-05-06 21:19:14 +00:00
|
|
|
glog.V(logger.Debug).Infof("%v: Disconnect Requested: %v\n", p, reason[0])
|
2015-05-14 01:04:04 +00:00
|
|
|
return reason[0]
|
2015-02-05 02:07:58 +00: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 20:48:49 +00:00
|
|
|
if err != nil {
|
2015-02-05 02:07:58 +00:00
|
|
|
return fmt.Errorf("msg code out of range: %v", msg.Code)
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
2015-04-08 15:37:11 +00:00
|
|
|
select {
|
|
|
|
case proto.in <- msg:
|
|
|
|
return nil
|
|
|
|
case <-p.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
2015-02-05 02:07:58 +00:00
|
|
|
return nil
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 14:09:38 +00:00
|
|
|
func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
|
|
|
|
n := 0
|
|
|
|
for _, cap := range caps {
|
|
|
|
for _, proto := range protocols {
|
|
|
|
if proto.Name == cap.Name && proto.Version == cap.Version {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-02-19 00:52:03 +00:00
|
|
|
// matchProtocols creates structures for matching named subprotocols.
|
|
|
|
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
2014-11-21 20:48:49 +00:00
|
|
|
sort.Sort(capsByName(caps))
|
|
|
|
offset := baseProtocolLength
|
2015-02-19 00:52:03 +00:00
|
|
|
result := make(map[string]*protoRW)
|
2014-11-21 20:48:49 +00:00
|
|
|
outer:
|
|
|
|
for _, cap := range caps {
|
2015-02-19 00:52:03 +00: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 20:48:49 +00:00
|
|
|
offset += proto.Length
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 00:52:03 +00:00
|
|
|
return result
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 00:52:03 +00:00
|
|
|
func (p *Peer) startProtocols() {
|
2015-04-08 15:37:11 +00:00
|
|
|
p.wg.Add(len(p.running))
|
2015-02-19 00:52:03 +00:00
|
|
|
for _, proto := range p.running {
|
|
|
|
proto := proto
|
2015-04-08 15:37:11 +00:00
|
|
|
proto.closed = p.closed
|
2015-05-06 21:19:14 +00:00
|
|
|
glog.V(logger.Detail).Infof("%v: Starting protocol %s/%d\n", p, proto.Name, proto.Version)
|
2015-02-19 00:52:03 +00:00
|
|
|
go func() {
|
|
|
|
err := proto.Run(p, proto)
|
|
|
|
if err == nil {
|
2015-05-06 21:19:14 +00:00
|
|
|
glog.V(logger.Detail).Infof("%v: Protocol %s/%d returned\n", p, proto.Name, proto.Version)
|
2015-02-19 00:52:03 +00:00
|
|
|
err = errors.New("protocol returned")
|
2015-05-06 21:19:14 +00: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 00:52:03 +00:00
|
|
|
}
|
2015-04-08 15:37:11 +00:00
|
|
|
p.protoErr <- err
|
|
|
|
p.wg.Done()
|
2015-02-19 00:52:03 +00:00
|
|
|
}()
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProto finds the protocol responsible for handling
|
|
|
|
// the given message code.
|
2015-02-19 00:52:03 +00:00
|
|
|
func (p *Peer) getProto(code uint64) (*protoRW, error) {
|
2014-11-21 20:48:49 +00:00
|
|
|
for _, proto := range p.running {
|
2015-02-19 00:52:03 +00:00
|
|
|
if code >= proto.offset && code < proto.offset+proto.Length {
|
2014-11-21 20:48:49 +00:00
|
|
|
return proto, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, newPeerError(errInvalidMsgCode, "%d", code)
|
|
|
|
}
|
|
|
|
|
2015-02-19 00:52:03 +00:00
|
|
|
type protoRW struct {
|
|
|
|
Protocol
|
|
|
|
in chan Msg
|
2015-04-08 15:37:11 +00:00
|
|
|
closed <-chan struct{}
|
2015-02-19 00:52:03 +00:00
|
|
|
offset uint64
|
|
|
|
w MsgWriter
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 00:52:03 +00:00
|
|
|
func (rw *protoRW) WriteMsg(msg Msg) error {
|
|
|
|
if msg.Code >= rw.Length {
|
2014-11-21 20:48:49 +00:00
|
|
|
return newPeerError(errInvalidMsgCode, "not handled")
|
|
|
|
}
|
|
|
|
msg.Code += rw.offset
|
2015-02-05 02:07:58 +00:00
|
|
|
return rw.w.WriteMsg(msg)
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 00:52:03 +00:00
|
|
|
func (rw *protoRW) ReadMsg() (Msg, error) {
|
2015-04-08 15:37:11 +00:00
|
|
|
select {
|
|
|
|
case msg := <-rw.in:
|
|
|
|
msg.Code -= rw.offset
|
|
|
|
return msg, nil
|
|
|
|
case <-rw.closed:
|
|
|
|
return Msg{}, io.EOF
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|