2014-10-23 18:57:54 +03:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-05 04:07:58 +02:00
|
|
|
"crypto/ecdsa"
|
2015-04-03 04:56:17 +03:00
|
|
|
"crypto/rand"
|
2014-11-21 22:48:49 +02:00
|
|
|
"errors"
|
2014-10-23 18:57:54 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2015-04-07 15:57:04 +03:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2015-02-05 04:07:58 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2015-02-11 18:19:31 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2015-03-19 16:11:02 +02:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-10-23 18:57:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-30 19:34:33 +03:00
|
|
|
defaultDialTimeout = 10 * time.Second
|
|
|
|
refreshPeersInterval = 30 * time.Second
|
|
|
|
staticPeerCheckInterval = 15 * time.Second
|
2015-03-04 17:27:37 +02:00
|
|
|
|
2015-04-10 18:24:41 +03:00
|
|
|
// This is the maximum number of inbound connection
|
|
|
|
// that are allowed to linger between 'accepted' and
|
|
|
|
// 'added as peer'.
|
|
|
|
maxAcceptConns = 50
|
|
|
|
|
2015-03-04 17:27:37 +02:00
|
|
|
// total timeout for encryption handshake and protocol
|
|
|
|
// handshake in both directions.
|
|
|
|
handshakeTimeout = 5 * time.Second
|
|
|
|
// maximum time allowed for reading a complete message.
|
|
|
|
// this is effectively the amount of time a connection can be idle.
|
|
|
|
frameReadTimeout = 1 * time.Minute
|
|
|
|
// maximum amount of time allowed for writing a complete message.
|
|
|
|
frameWriteTimeout = 5 * time.Second
|
2014-10-23 18:57:54 +03:00
|
|
|
)
|
|
|
|
|
2015-02-19 18:09:33 +02:00
|
|
|
var srvjslog = logger.NewJsonLogger()
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Server manages all peer connections.
|
|
|
|
//
|
|
|
|
// The fields of Server are used as configuration parameters.
|
|
|
|
// You should set them before starting the Server. Fields may not be
|
|
|
|
// modified while the server is running.
|
2014-10-23 18:57:54 +03:00
|
|
|
type Server struct {
|
2015-02-05 04:07:58 +02:00
|
|
|
// This field must be set to a valid secp256k1 private key.
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
// MaxPeers is the maximum number of peers that can be
|
|
|
|
// connected. It must be greater than zero.
|
|
|
|
MaxPeers int
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// Name sets the node name of this server.
|
2015-03-16 12:27:38 +02:00
|
|
|
// Use common.MakeName to create a name that follows existing conventions.
|
2015-02-05 04:07:58 +02:00
|
|
|
Name string
|
|
|
|
|
|
|
|
// Bootstrap nodes are used to establish connectivity
|
|
|
|
// with the rest of the network.
|
2015-02-07 01:38:36 +02:00
|
|
|
BootstrapNodes []*discover.Node
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-04-30 19:32:48 +03:00
|
|
|
// Static nodes are used as pre-configured connections which are always
|
|
|
|
// maintained and re-connected on disconnects.
|
|
|
|
StaticNodes []*discover.Node
|
2015-04-29 18:04:08 +03:00
|
|
|
|
2015-05-04 13:59:51 +03:00
|
|
|
// Trusted nodes are used as pre-configured connections which are always
|
|
|
|
// allowed to connect, even above the peer limit.
|
|
|
|
TrustedNodes []*discover.Node
|
|
|
|
|
2015-04-24 18:04:41 +03:00
|
|
|
// NodeDatabase is the path to the database containing the previously seen
|
|
|
|
// live nodes in the network.
|
|
|
|
NodeDatabase string
|
2015-04-23 18:47:24 +03:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Protocols should contain the protocols supported
|
|
|
|
// by the server. Matching protocols are launched for
|
|
|
|
// each peer.
|
|
|
|
Protocols []Protocol
|
|
|
|
|
|
|
|
// If ListenAddr is set to a non-nil address, the server
|
|
|
|
// will listen for incoming connections.
|
|
|
|
//
|
|
|
|
// If the port is zero, the operating system will pick a port. The
|
|
|
|
// ListenAddr field will be updated with the actual address when
|
|
|
|
// the server is started.
|
|
|
|
ListenAddr string
|
|
|
|
|
|
|
|
// If set to a non-nil value, the given NAT port mapper
|
|
|
|
// is used to make the listening port available to the
|
|
|
|
// Internet.
|
2015-02-11 18:19:31 +02:00
|
|
|
NAT nat.Interface
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
// If Dialer is set to a non-nil value, the given Dialer
|
|
|
|
// is used to dial outbound peer connections.
|
|
|
|
Dialer *net.Dialer
|
|
|
|
|
|
|
|
// If NoDial is true, the server will not dial any peers.
|
|
|
|
NoDial bool
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// Hooks for testing. These are useful because we can inhibit
|
2014-11-21 22:48:49 +02:00
|
|
|
// the whole protocol stack.
|
2015-02-19 02:52:03 +02:00
|
|
|
setupFunc
|
2015-02-05 04:07:58 +02:00
|
|
|
newPeerHook
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
ourHandshake *protoHandshake
|
|
|
|
|
2015-05-04 13:59:51 +03:00
|
|
|
lock sync.RWMutex // protects running, peers and the trust fields
|
|
|
|
running bool
|
|
|
|
peers map[discover.NodeID]*Peer
|
|
|
|
staticNodes map[discover.NodeID]*discover.Node // Map of currently maintained static remote nodes
|
|
|
|
staticDial chan *discover.Node // Dial request channel reserved for the static nodes
|
|
|
|
staticCycle time.Duration // Overrides staticPeerCheckInterval, used for testing
|
|
|
|
trustedNodes map[discover.NodeID]bool // Set of currently trusted remote nodes
|
2015-04-29 18:04:08 +03:00
|
|
|
|
2015-04-10 18:23:09 +03:00
|
|
|
ntab *discover.Table
|
|
|
|
listener net.Listener
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-04-29 18:04:08 +03:00
|
|
|
quit chan struct{}
|
|
|
|
loopWG sync.WaitGroup // {dial,listen,nat}Loop
|
|
|
|
peerWG sync.WaitGroup // active peer goroutines
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-05-04 13:59:51 +03:00
|
|
|
type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, bool, map[discover.NodeID]bool) (*conn, error)
|
2015-02-05 04:07:58 +02:00
|
|
|
type newPeerHook func(*Peer)
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Peers returns all connected peers.
|
|
|
|
func (srv *Server) Peers() (peers []*Peer) {
|
|
|
|
srv.lock.RLock()
|
|
|
|
defer srv.lock.RUnlock()
|
|
|
|
for _, peer := range srv.peers {
|
2014-10-23 18:57:54 +03:00
|
|
|
if peer != nil {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// PeerCount returns the number of connected peers.
|
|
|
|
func (srv *Server) PeerCount() int {
|
|
|
|
srv.lock.RLock()
|
2015-02-05 04:07:58 +02:00
|
|
|
n := len(srv.peers)
|
|
|
|
srv.lock.RUnlock()
|
|
|
|
return n
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 16:15:29 +03:00
|
|
|
// AddPeer connects to the given node and maintains the connection until the
|
|
|
|
// server is shut down. If the connection fails for any reason, the server will
|
|
|
|
// attempt to reconnect the peer.
|
|
|
|
func (srv *Server) AddPeer(node *discover.Node) {
|
2015-04-30 12:41:27 +03:00
|
|
|
srv.lock.Lock()
|
|
|
|
defer srv.lock.Unlock()
|
|
|
|
|
2015-05-04 13:08:42 +03:00
|
|
|
srv.staticNodes[node.ID] = node
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Broadcast sends an RLP-encoded message to all connected peers.
|
|
|
|
// This method is deprecated and will be removed later.
|
2015-03-19 16:11:02 +02:00
|
|
|
func (srv *Server) Broadcast(protocol string, code uint64, data interface{}) error {
|
2015-04-14 13:47:31 +03:00
|
|
|
return srv.BroadcastLimited(protocol, code, func(i float64) float64 { return i }, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastsRange an RLP-encoded message to a random set of peers using the limit function to limit the amount
|
|
|
|
// of peers.
|
|
|
|
func (srv *Server) BroadcastLimited(protocol string, code uint64, limit func(float64) float64, data interface{}) error {
|
2014-11-04 14:21:44 +02:00
|
|
|
var payload []byte
|
|
|
|
if data != nil {
|
2015-03-19 16:11:02 +02:00
|
|
|
var err error
|
|
|
|
payload, err = rlp.EncodeToBytes(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 14:21:44 +02:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.lock.RLock()
|
|
|
|
defer srv.lock.RUnlock()
|
2015-04-14 13:47:31 +03:00
|
|
|
|
|
|
|
i, max := 0, int(limit(float64(len(srv.peers))))
|
2014-11-21 22:48:49 +02:00
|
|
|
for _, peer := range srv.peers {
|
2015-04-14 13:47:31 +03:00
|
|
|
if i >= max {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-10-23 18:57:54 +03:00
|
|
|
if peer != nil {
|
2014-11-04 14:21:44 +02:00
|
|
|
var msg = Msg{Code: code}
|
|
|
|
if data != nil {
|
|
|
|
msg.Payload = bytes.NewReader(payload)
|
|
|
|
msg.Size = uint32(len(payload))
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
peer.writeProtoMsg(protocol, msg)
|
2015-04-14 13:47:31 +03:00
|
|
|
i++
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 16:11:02 +02:00
|
|
|
return nil
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Start starts running the server.
|
|
|
|
// Servers can be re-used and started again after stopping.
|
|
|
|
func (srv *Server) Start() (err error) {
|
|
|
|
srv.lock.Lock()
|
|
|
|
defer srv.lock.Unlock()
|
|
|
|
if srv.running {
|
|
|
|
return errors.New("server already running")
|
|
|
|
}
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Info).Infoln("Starting Server")
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// static fields
|
2015-02-05 04:07:58 +02:00
|
|
|
if srv.PrivateKey == nil {
|
|
|
|
return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
if srv.MaxPeers <= 0 {
|
|
|
|
return fmt.Errorf("Server.MaxPeers must be > 0")
|
|
|
|
}
|
|
|
|
srv.quit = make(chan struct{})
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.peers = make(map[discover.NodeID]*Peer)
|
2015-04-29 18:04:08 +03:00
|
|
|
|
2015-05-04 13:59:51 +03:00
|
|
|
// Create the current trust maps, and the associated dialing channel
|
|
|
|
srv.trustedNodes = make(map[discover.NodeID]bool)
|
|
|
|
for _, node := range srv.TrustedNodes {
|
|
|
|
srv.trustedNodes[node.ID] = true
|
|
|
|
}
|
2015-05-04 13:08:42 +03:00
|
|
|
srv.staticNodes = make(map[discover.NodeID]*discover.Node)
|
2015-04-30 19:32:48 +03:00
|
|
|
for _, node := range srv.StaticNodes {
|
2015-05-04 13:08:42 +03:00
|
|
|
srv.staticNodes[node.ID] = node
|
2015-04-29 18:04:08 +03:00
|
|
|
}
|
2015-04-30 19:32:48 +03:00
|
|
|
srv.staticDial = make(chan *discover.Node)
|
2015-04-29 18:04:08 +03:00
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
if srv.setupFunc == nil {
|
|
|
|
srv.setupFunc = setupConn
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// node table
|
2015-04-24 18:04:41 +03:00
|
|
|
ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.NodeDatabase)
|
2015-02-05 04:07:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-19 02:52:03 +02:00
|
|
|
srv.ntab = ntab
|
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// handshake
|
2015-03-15 08:38:41 +02:00
|
|
|
srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: ntab.Self().ID}
|
2015-02-19 02:52:03 +02:00
|
|
|
for _, p := range srv.Protocols {
|
|
|
|
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
|
|
|
|
}
|
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// listen/dial
|
|
|
|
if srv.ListenAddr != "" {
|
|
|
|
if err := srv.startListening(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if srv.Dialer == nil {
|
|
|
|
srv.Dialer = &net.Dialer{Timeout: defaultDialTimeout}
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
if !srv.NoDial {
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Add(1)
|
2014-11-21 22:48:49 +02:00
|
|
|
go srv.dialLoop()
|
|
|
|
}
|
|
|
|
if srv.NoDial && srv.ListenAddr == "" {
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Warn).Infoln("I will be kind-of useless, neither dialing nor listening.")
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-04-30 19:32:48 +03:00
|
|
|
// maintain the static peers
|
|
|
|
go srv.staticNodesLoop()
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
srv.running = true
|
|
|
|
return nil
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func (srv *Server) startListening() error {
|
|
|
|
listener, err := net.Listen("tcp", srv.ListenAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 18:19:31 +02:00
|
|
|
laddr := listener.Addr().(*net.TCPAddr)
|
|
|
|
srv.ListenAddr = laddr.String()
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.listener = listener
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Add(1)
|
2014-11-21 22:48:49 +02:00
|
|
|
go srv.listenLoop()
|
2015-02-11 18:19:31 +02:00
|
|
|
if !laddr.IP.IsLoopback() && srv.NAT != nil {
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Add(1)
|
2015-02-11 18:19:31 +02:00
|
|
|
go func() {
|
|
|
|
nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p")
|
|
|
|
srv.loopWG.Done()
|
|
|
|
}()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates the server and all active peer connections.
|
|
|
|
// It blocks until all active connections have been closed.
|
|
|
|
func (srv *Server) Stop() {
|
|
|
|
srv.lock.Lock()
|
|
|
|
if !srv.running {
|
|
|
|
srv.lock.Unlock()
|
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.running = false
|
|
|
|
srv.lock.Unlock()
|
|
|
|
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Info).Infoln("Stopping Server")
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.ntab.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
if srv.listener != nil {
|
|
|
|
// this unblocks listener Accept
|
|
|
|
srv.listener.Close()
|
|
|
|
}
|
|
|
|
close(srv.quit)
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Wait()
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// No new peers can be added at this point because dialLoop and
|
|
|
|
// listenLoop are down. It is safe to call peerWG.Wait because
|
|
|
|
// peerWG.Add is not called outside of those loops.
|
2015-04-13 18:37:32 +03:00
|
|
|
srv.lock.Lock()
|
2015-02-05 04:07:58 +02:00
|
|
|
for _, peer := range srv.peers {
|
|
|
|
peer.Disconnect(DiscQuitting)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-04-13 18:37:32 +03:00
|
|
|
srv.lock.Unlock()
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.peerWG.Wait()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2015-04-10 14:25:35 +03:00
|
|
|
// Self returns the local node's endpoint information.
|
|
|
|
func (srv *Server) Self() *discover.Node {
|
2015-04-22 11:59:15 +03:00
|
|
|
srv.lock.RLock()
|
|
|
|
defer srv.lock.RUnlock()
|
|
|
|
if !srv.running {
|
|
|
|
return &discover.Node{IP: net.ParseIP("0.0.0.0")}
|
|
|
|
}
|
2015-04-10 14:25:35 +03:00
|
|
|
return srv.ntab.Self()
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// main loop for adding connections via listening
|
|
|
|
func (srv *Server) listenLoop() {
|
2015-02-05 04:07:58 +02:00
|
|
|
defer srv.loopWG.Done()
|
2015-04-10 18:24:41 +03:00
|
|
|
|
|
|
|
// This channel acts as a semaphore limiting
|
|
|
|
// active inbound connections that are lingering pre-handshake.
|
|
|
|
// If all slots are taken, no further connections are accepted.
|
|
|
|
slots := make(chan struct{}, maxAcceptConns)
|
|
|
|
for i := 0; i < maxAcceptConns; i++ {
|
|
|
|
slots <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Info).Infoln("Listening on", srv.listener.Addr())
|
2014-10-23 18:57:54 +03:00
|
|
|
for {
|
2015-04-10 18:24:41 +03:00
|
|
|
<-slots
|
2015-02-05 04:07:58 +02:00
|
|
|
conn, err := srv.listener.Accept()
|
|
|
|
if err != nil {
|
2014-11-21 22:48:49 +02:00
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Debug).Infof("Accepted conn %v\n", conn.RemoteAddr())
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.peerWG.Add(1)
|
2015-04-10 18:24:41 +03:00
|
|
|
go func() {
|
|
|
|
srv.startPeer(conn, nil)
|
|
|
|
slots <- struct{}{}
|
|
|
|
}()
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:32:48 +03:00
|
|
|
// staticNodesLoop is responsible for periodically checking that static
|
2015-04-30 16:15:29 +03:00
|
|
|
// connections are actually live, and requests dialing if not.
|
2015-04-30 19:32:48 +03:00
|
|
|
func (srv *Server) staticNodesLoop() {
|
2015-05-04 13:08:42 +03:00
|
|
|
// Create a default maintenance ticker, but override it requested
|
|
|
|
cycle := staticPeerCheckInterval
|
|
|
|
if srv.staticCycle != 0 {
|
|
|
|
cycle = srv.staticCycle
|
|
|
|
}
|
|
|
|
tick := time.NewTicker(cycle)
|
|
|
|
|
2015-04-29 18:04:08 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-srv.quit:
|
|
|
|
return
|
|
|
|
|
2015-05-04 13:08:42 +03:00
|
|
|
case <-tick.C:
|
2015-04-30 19:32:48 +03:00
|
|
|
// Collect all the non-connected static nodes
|
2015-04-29 18:04:08 +03:00
|
|
|
needed := []*discover.Node{}
|
|
|
|
srv.lock.RLock()
|
2015-05-04 13:08:42 +03:00
|
|
|
for id, node := range srv.staticNodes {
|
2015-04-29 18:04:08 +03:00
|
|
|
if _, ok := srv.peers[id]; !ok {
|
|
|
|
needed = append(needed, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
srv.lock.RUnlock()
|
|
|
|
|
|
|
|
// Try to dial each of them (don't hang if server terminates)
|
|
|
|
for _, node := range needed {
|
2015-04-30 19:32:48 +03:00
|
|
|
glog.V(logger.Debug).Infof("Dialing static peer %v", node)
|
2015-04-29 18:04:08 +03:00
|
|
|
select {
|
2015-04-30 19:32:48 +03:00
|
|
|
case srv.staticDial <- node:
|
2015-04-29 18:04:08 +03:00
|
|
|
case <-srv.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func (srv *Server) dialLoop() {
|
2015-04-03 04:56:17 +03:00
|
|
|
var (
|
|
|
|
dialed = make(chan *discover.Node)
|
|
|
|
dialing = make(map[discover.NodeID]bool)
|
|
|
|
findresults = make(chan []*discover.Node)
|
|
|
|
refresh = time.NewTimer(0)
|
|
|
|
)
|
2015-02-05 04:07:58 +02:00
|
|
|
defer srv.loopWG.Done()
|
|
|
|
defer refresh.Stop()
|
|
|
|
|
2015-04-03 04:56:17 +03:00
|
|
|
// TODO: maybe limit number of active dials
|
|
|
|
dial := func(dest *discover.Node) {
|
|
|
|
// Don't dial nodes that would fail the checks in addPeer.
|
|
|
|
// This is important because the connection handshake is a lot
|
|
|
|
// of work and we'd rather avoid doing that work for peers
|
|
|
|
// that can't be added.
|
2015-04-10 18:23:09 +03:00
|
|
|
srv.lock.RLock()
|
|
|
|
ok, _ := srv.checkPeer(dest.ID)
|
|
|
|
srv.lock.RUnlock()
|
2015-04-03 04:56:17 +03:00
|
|
|
if !ok || dialing[dest.ID] {
|
|
|
|
return
|
|
|
|
}
|
2015-04-10 18:23:09 +03:00
|
|
|
|
2015-04-03 04:56:17 +03:00
|
|
|
dialing[dest.ID] = true
|
|
|
|
srv.peerWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
srv.dialNode(dest)
|
|
|
|
dialed <- dest
|
|
|
|
}()
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-04-03 04:56:17 +03:00
|
|
|
srv.ntab.Bootstrap(srv.BootstrapNodes)
|
2014-10-23 18:57:54 +03:00
|
|
|
for {
|
|
|
|
select {
|
2015-02-05 04:07:58 +02:00
|
|
|
case <-refresh.C:
|
2015-04-10 18:23:09 +03:00
|
|
|
// Grab some nodes to connect to if we're not at capacity.
|
|
|
|
srv.lock.RLock()
|
2015-04-03 04:56:17 +03:00
|
|
|
needpeers := len(srv.peers) < srv.MaxPeers
|
2015-04-10 18:23:09 +03:00
|
|
|
srv.lock.RUnlock()
|
2015-04-03 04:56:17 +03:00
|
|
|
if needpeers {
|
|
|
|
go func() {
|
|
|
|
var target discover.NodeID
|
|
|
|
rand.Read(target[:])
|
|
|
|
findresults <- srv.ntab.Lookup(target)
|
|
|
|
}()
|
2015-04-17 04:52:54 +03:00
|
|
|
} else {
|
|
|
|
// Make sure we check again if the peer count falls
|
|
|
|
// below MaxPeers.
|
|
|
|
refresh.Reset(refreshPeersInterval)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-04-30 19:32:48 +03:00
|
|
|
case dest := <-srv.staticDial:
|
2015-04-03 04:56:17 +03:00
|
|
|
dial(dest)
|
|
|
|
case dests := <-findresults:
|
|
|
|
for _, dest := range dests {
|
|
|
|
dial(dest)
|
|
|
|
}
|
|
|
|
refresh.Reset(refreshPeersInterval)
|
2015-02-05 04:07:58 +02:00
|
|
|
case dest := <-dialed:
|
|
|
|
delete(dialing, dest.ID)
|
2015-04-17 04:52:54 +03:00
|
|
|
if len(dialing) == 0 {
|
|
|
|
// Check again immediately after dialing all current candidates.
|
|
|
|
refresh.Reset(0)
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
case <-srv.quit:
|
|
|
|
// TODO: maybe wait for active dials
|
2014-10-23 18:57:54 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) dialNode(dest *discover.Node) {
|
2015-02-06 15:40:53 +02:00
|
|
|
addr := &net.TCPAddr{IP: dest.IP, Port: dest.TCPPort}
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Debug).Infof("Dialing %v\n", dest)
|
2015-02-06 15:40:53 +02:00
|
|
|
conn, err := srv.Dialer.Dial("tcp", addr.String())
|
2014-10-23 18:57:54 +03:00
|
|
|
if err != nil {
|
2015-04-10 14:20:26 +03:00
|
|
|
// dialLoop adds to the wait group counter when launching
|
|
|
|
// dialNode, so we need to count it down again. startPeer also
|
|
|
|
// does that when an error occurs.
|
|
|
|
srv.peerWG.Done()
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Detail).Infof("dial error: %v", err)
|
2014-11-04 14:21:44 +02:00
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.startPeer(conn, dest)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
|
2015-02-13 15:49:49 +02:00
|
|
|
// TODO: handle/store session token
|
2015-04-10 14:20:26 +03:00
|
|
|
|
|
|
|
// Run setupFunc, which should create an authenticated connection
|
|
|
|
// and run the capability exchange. Note that any early error
|
|
|
|
// returns during that exchange need to call peerWG.Done because
|
|
|
|
// the callers of startPeer added the peer to the wait group already.
|
2015-03-04 17:27:37 +02:00
|
|
|
fd.SetDeadline(time.Now().Add(handshakeTimeout))
|
2015-04-30 12:41:27 +03:00
|
|
|
|
2015-04-30 19:32:48 +03:00
|
|
|
// Check capacity, but override for static nodes
|
2015-04-10 14:25:35 +03:00
|
|
|
srv.lock.RLock()
|
|
|
|
atcap := len(srv.peers) == srv.MaxPeers
|
2015-04-30 19:32:48 +03:00
|
|
|
if dest != nil {
|
2015-05-04 13:08:42 +03:00
|
|
|
if _, ok := srv.staticNodes[dest.ID]; ok {
|
2015-04-30 19:32:48 +03:00
|
|
|
atcap = false
|
|
|
|
}
|
2015-04-30 12:41:27 +03:00
|
|
|
}
|
2015-04-10 14:25:35 +03:00
|
|
|
srv.lock.RUnlock()
|
2015-04-30 12:41:27 +03:00
|
|
|
|
2015-05-04 13:59:51 +03:00
|
|
|
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, atcap, srv.trustedNodes)
|
2015-02-05 04:07:58 +02:00
|
|
|
if err != nil {
|
2015-02-19 02:52:03 +02:00
|
|
|
fd.Close()
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Debug).Infof("Handshake with %v failed: %v", fd.RemoteAddr(), err)
|
2015-04-10 14:20:26 +03:00
|
|
|
srv.peerWG.Done()
|
2015-02-05 04:07:58 +02:00
|
|
|
return
|
|
|
|
}
|
2015-03-04 17:27:37 +02:00
|
|
|
conn.MsgReadWriter = &netWrapper{
|
|
|
|
wrapped: conn.MsgReadWriter,
|
|
|
|
conn: fd, rtimeout: frameReadTimeout, wtimeout: frameWriteTimeout,
|
|
|
|
}
|
2015-02-27 05:06:55 +02:00
|
|
|
p := newPeer(fd, conn, srv.Protocols)
|
2015-02-19 02:52:03 +02:00
|
|
|
if ok, reason := srv.addPeer(conn.ID, p); !ok {
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Detail).Infof("Not adding %v (%v)\n", p, reason)
|
2015-02-13 15:47:05 +02:00
|
|
|
p.politeDisconnect(reason)
|
2015-04-10 14:20:26 +03:00
|
|
|
srv.peerWG.Done()
|
2015-02-05 04:07:58 +02:00
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-04-03 04:56:17 +03:00
|
|
|
// The handshakes are done and it passed all checks.
|
|
|
|
// Spawn the Peer loops.
|
|
|
|
go srv.runPeer(p)
|
|
|
|
}
|
2015-02-19 18:09:33 +02:00
|
|
|
|
2015-04-03 04:56:17 +03:00
|
|
|
func (srv *Server) runPeer(p *Peer) {
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Debug).Infof("Added %v\n", p)
|
2015-02-19 18:09:33 +02:00
|
|
|
srvjslog.LogJson(&logger.P2PConnected{
|
2015-04-03 04:56:17 +03:00
|
|
|
RemoteId: p.ID().String(),
|
|
|
|
RemoteAddress: p.RemoteAddr().String(),
|
|
|
|
RemoteVersionString: p.Name(),
|
2015-02-19 18:09:33 +02:00
|
|
|
NumConnections: srv.PeerCount(),
|
|
|
|
})
|
2015-02-07 01:13:22 +02:00
|
|
|
if srv.newPeerHook != nil {
|
|
|
|
srv.newPeerHook(p)
|
|
|
|
}
|
2015-02-13 15:47:05 +02:00
|
|
|
discreason := p.run()
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.removePeer(p)
|
2015-04-07 15:57:04 +03:00
|
|
|
glog.V(logger.Debug).Infof("Removed %v (%v)\n", p, discreason)
|
2015-02-19 18:09:33 +02:00
|
|
|
srvjslog.LogJson(&logger.P2PDisconnected{
|
2015-04-03 04:56:17 +03:00
|
|
|
RemoteId: p.ID().String(),
|
2015-02-19 18:09:33 +02:00
|
|
|
NumConnections: srv.PeerCount(),
|
|
|
|
})
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.lock.Lock()
|
|
|
|
defer srv.lock.Unlock()
|
2015-04-03 04:56:17 +03:00
|
|
|
if ok, reason := srv.checkPeer(id); !ok {
|
|
|
|
return false, reason
|
|
|
|
}
|
|
|
|
srv.peers[id] = p
|
|
|
|
return true, 0
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:04:08 +03:00
|
|
|
// checkPeer verifies whether a peer looks promising and should be allowed/kept
|
|
|
|
// in the pool, or if it's of no use.
|
2015-04-03 04:56:17 +03:00
|
|
|
func (srv *Server) checkPeer(id discover.NodeID) (bool, DiscReason) {
|
2015-05-04 13:59:51 +03:00
|
|
|
// First up, figure out if the peer is static or trusted
|
2015-05-04 13:08:42 +03:00
|
|
|
_, static := srv.staticNodes[id]
|
2015-05-04 13:59:51 +03:00
|
|
|
trusted := srv.trustedNodes[id]
|
2015-04-29 18:04:08 +03:00
|
|
|
|
|
|
|
// Make sure the peer passes all required checks
|
2015-02-05 04:07:58 +02:00
|
|
|
switch {
|
|
|
|
case !srv.running:
|
|
|
|
return false, DiscQuitting
|
2015-05-04 13:59:51 +03:00
|
|
|
case !static && !trusted && len(srv.peers) >= srv.MaxPeers:
|
2015-02-05 04:07:58 +02:00
|
|
|
return false, DiscTooManyPeers
|
|
|
|
case srv.peers[id] != nil:
|
|
|
|
return false, DiscAlreadyConnected
|
2015-04-22 11:59:15 +03:00
|
|
|
case id == srv.ntab.Self().ID:
|
2015-02-05 04:07:58 +02:00
|
|
|
return false, DiscSelf
|
2015-04-03 04:56:17 +03:00
|
|
|
default:
|
|
|
|
return true, 0
|
2015-02-05 04:07:58 +02:00
|
|
|
}
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) removePeer(p *Peer) {
|
|
|
|
srv.lock.Lock()
|
2015-02-19 02:52:03 +02:00
|
|
|
delete(srv.peers, p.ID())
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.lock.Unlock()
|
|
|
|
srv.peerWG.Done()
|
2014-11-04 14:21:44 +02:00
|
|
|
}
|