github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/p2p/server.go (about)

     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  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     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  // GNU Lesser General Public License for more details.
    13  //
    14  // 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  
    17  // Package p2p implements the Ethereum p2p network protocols.
    18  package p2p
    19  
    20  import (
    21  	"bytes"
    22  	"crypto/ecdsa"
    23  	"encoding/hex"
    24  	"errors"
    25  	"fmt"
    26  	"net"
    27  	"sort"
    28  	"sync"
    29  	"sync/atomic"
    30  	"time"
    31  
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/common/mclock"
    34  	"github.com/ethereum/go-ethereum/crypto"
    35  	"github.com/ethereum/go-ethereum/event"
    36  	"github.com/ethereum/go-ethereum/log"
    37  	"github.com/ethereum/go-ethereum/p2p/discover"
    38  	"github.com/ethereum/go-ethereum/p2p/discv5"
    39  	"github.com/ethereum/go-ethereum/p2p/enode"
    40  	"github.com/ethereum/go-ethereum/p2p/enr"
    41  	"github.com/ethereum/go-ethereum/p2p/nat"
    42  	"github.com/ethereum/go-ethereum/p2p/netutil"
    43  	"github.com/ethereum/go-ethereum/rlp"
    44  )
    45  
    46  const (
    47  	defaultDialTimeout = 15 * time.Second
    48  
    49  	// Connectivity defaults.
    50  	maxActiveDialTasks     = 16
    51  	defaultMaxPendingPeers = 50
    52  	defaultDialRatio       = 3
    53  
    54  	// Maximum time allowed for reading a complete message.
    55  	// This is effectively the amount of time a connection can be idle.
    56  	frameReadTimeout = 30 * time.Second
    57  
    58  	// Maximum amount of time allowed for writing a complete message.
    59  	frameWriteTimeout = 20 * time.Second
    60  )
    61  
    62  var errServerStopped = errors.New("server stopped")
    63  
    64  // Config holds Server options.
    65  type Config struct {
    66  	// This field must be set to a valid secp256k1 private key.
    67  	PrivateKey *ecdsa.PrivateKey `toml:"-"`
    68  
    69  	// MaxPeers is the maximum number of peers that can be
    70  	// connected. It must be greater than zero.
    71  	MaxPeers int
    72  
    73  	// MaxPendingPeers is the maximum number of peers that can be pending in the
    74  	// handshake phase, counted separately for inbound and outbound connections.
    75  	// Zero defaults to preset values.
    76  	MaxPendingPeers int `toml:",omitempty"`
    77  
    78  	// DialRatio controls the ratio of inbound to dialed connections.
    79  	// Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
    80  	// Setting DialRatio to zero defaults it to 3.
    81  	DialRatio int `toml:",omitempty"`
    82  
    83  	// NoDiscovery can be used to disable the peer discovery mechanism.
    84  	// Disabling is useful for protocol debugging (manual topology).
    85  	NoDiscovery bool
    86  
    87  	// DiscoveryV5 specifies whether the new topic-discovery based V5 discovery
    88  	// protocol should be started or not.
    89  	DiscoveryV5 bool `toml:",omitempty"`
    90  
    91  	// Name sets the node name of this server.
    92  	// Use common.MakeName to create a name that follows existing conventions.
    93  	Name string `toml:"-"`
    94  
    95  	// BootstrapNodes are used to establish connectivity
    96  	// with the rest of the network.
    97  	BootstrapNodes []*enode.Node
    98  
    99  	// BootstrapNodesV5 are used to establish connectivity
   100  	// with the rest of the network using the V5 discovery
   101  	// protocol.
   102  	BootstrapNodesV5 []*discv5.Node `toml:",omitempty"`
   103  
   104  	// Static nodes are used as pre-configured connections which are always
   105  	// maintained and re-connected on disconnects.
   106  	StaticNodes []*enode.Node
   107  
   108  	// Trusted nodes are used as pre-configured connections which are always
   109  	// allowed to connect, even above the peer limit.
   110  	TrustedNodes []*enode.Node
   111  
   112  	// Connectivity can be restricted to certain IP networks.
   113  	// If this option is set to a non-nil value, only hosts which match one of the
   114  	// IP networks contained in the list are considered.
   115  	NetRestrict *netutil.Netlist `toml:",omitempty"`
   116  
   117  	// NodeDatabase is the path to the database containing the previously seen
   118  	// live nodes in the network.
   119  	NodeDatabase string `toml:",omitempty"`
   120  
   121  	// Protocols should contain the protocols supported
   122  	// by the server. Matching protocols are launched for
   123  	// each peer.
   124  	Protocols []Protocol `toml:"-"`
   125  
   126  	// If ListenAddr is set to a non-nil address, the server
   127  	// will listen for incoming connections.
   128  	//
   129  	// If the port is zero, the operating system will pick a port. The
   130  	// ListenAddr field will be updated with the actual address when
   131  	// the server is started.
   132  	ListenAddr string
   133  
   134  	// If set to a non-nil value, the given NAT port mapper
   135  	// is used to make the listening port available to the
   136  	// Internet.
   137  	NAT nat.Interface `toml:",omitempty"`
   138  
   139  	// If Dialer is set to a non-nil value, the given Dialer
   140  	// is used to dial outbound peer connections.
   141  	Dialer NodeDialer `toml:"-"`
   142  
   143  	// If NoDial is true, the server will not dial any peers.
   144  	NoDial bool `toml:",omitempty"`
   145  
   146  	// If EnableMsgEvents is set then the server will emit PeerEvents
   147  	// whenever a message is sent to or received from a peer
   148  	EnableMsgEvents bool
   149  
   150  	// Logger is a custom logger to use with the p2p.Server.
   151  	Logger log.Logger `toml:",omitempty"`
   152  }
   153  
   154  // Server manages all peer connections.
   155  type Server struct {
   156  	// Config fields may not be modified while the server is running.
   157  	Config
   158  
   159  	// Hooks for testing. These are useful because we can inhibit
   160  	// the whole protocol stack.
   161  	newTransport func(net.Conn) transport
   162  	newPeerHook  func(*Peer)
   163  
   164  	lock    sync.Mutex // protects running
   165  	running bool
   166  
   167  	nodedb       *enode.DB
   168  	localnode    *enode.LocalNode
   169  	ntab         discoverTable
   170  	listener     net.Listener
   171  	ourHandshake *protoHandshake
   172  	lastLookup   time.Time
   173  	DiscV5       *discv5.Network
   174  
   175  	// These are for Peers, PeerCount (and nothing else).
   176  	peerOp     chan peerOpFunc
   177  	peerOpDone chan struct{}
   178  
   179  	quit          chan struct{}
   180  	addstatic     chan *enode.Node
   181  	removestatic  chan *enode.Node
   182  	addtrusted    chan *enode.Node
   183  	removetrusted chan *enode.Node
   184  	posthandshake chan *conn
   185  	addpeer       chan *conn
   186  	delpeer       chan peerDrop
   187  	loopWG        sync.WaitGroup // loop, listenLoop
   188  	peerFeed      event.Feed
   189  	log           log.Logger
   190  }
   191  
   192  type peerOpFunc func(map[enode.ID]*Peer)
   193  
   194  type peerDrop struct {
   195  	*Peer
   196  	err       error
   197  	requested bool // true if signaled by the peer
   198  }
   199  
   200  type connFlag int32
   201  
   202  const (
   203  	dynDialedConn connFlag = 1 << iota
   204  	staticDialedConn
   205  	inboundConn
   206  	trustedConn
   207  )
   208  
   209  // conn wraps a network connection with information gathered
   210  // during the two handshakes.
   211  type conn struct {
   212  	fd net.Conn
   213  	transport
   214  	node  *enode.Node
   215  	flags connFlag
   216  	cont  chan error // The run loop uses cont to signal errors to SetupConn.
   217  	caps  []Cap      // valid after the protocol handshake
   218  	name  string     // valid after the protocol handshake
   219  }
   220  
   221  type transport interface {
   222  	// The two handshakes.
   223  	doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error)
   224  	doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
   225  	// The MsgReadWriter can only be used after the encryption
   226  	// handshake has completed. The code uses conn.id to track this
   227  	// by setting it to a non-nil value after the encryption handshake.
   228  	MsgReadWriter
   229  	// transports must provide Close because we use MsgPipe in some of
   230  	// the tests. Closing the actual network connection doesn't do
   231  	// anything in those tests because NsgPipe doesn't use it.
   232  	close(err error)
   233  }
   234  
   235  func (c *conn) String() string {
   236  	s := c.flags.String()
   237  	if (c.node.ID() != enode.ID{}) {
   238  		s += " " + c.node.ID().String()
   239  	}
   240  	s += " " + c.fd.RemoteAddr().String()
   241  	return s
   242  }
   243  
   244  func (f connFlag) String() string {
   245  	s := ""
   246  	if f&trustedConn != 0 {
   247  		s += "-trusted"
   248  	}
   249  	if f&dynDialedConn != 0 {
   250  		s += "-dyndial"
   251  	}
   252  	if f&staticDialedConn != 0 {
   253  		s += "-staticdial"
   254  	}
   255  	if f&inboundConn != 0 {
   256  		s += "-inbound"
   257  	}
   258  	if s != "" {
   259  		s = s[1:]
   260  	}
   261  	return s
   262  }
   263  
   264  func (c *conn) is(f connFlag) bool {
   265  	flags := connFlag(atomic.LoadInt32((*int32)(&c.flags)))
   266  	return flags&f != 0
   267  }
   268  
   269  func (c *conn) set(f connFlag, val bool) {
   270  	for {
   271  		oldFlags := connFlag(atomic.LoadInt32((*int32)(&c.flags)))
   272  		flags := oldFlags
   273  		if val {
   274  			flags |= f
   275  		} else {
   276  			flags &= ^f
   277  		}
   278  		if atomic.CompareAndSwapInt32((*int32)(&c.flags), int32(oldFlags), int32(flags)) {
   279  			return
   280  		}
   281  	}
   282  }
   283  
   284  // Peers returns all connected peers.
   285  func (srv *Server) Peers() []*Peer {
   286  	var ps []*Peer
   287  	select {
   288  	// Note: We'd love to put this function into a variable but
   289  	// that seems to cause a weird compiler error in some
   290  	// environments.
   291  	case srv.peerOp <- func(peers map[enode.ID]*Peer) {
   292  		for _, p := range peers {
   293  			ps = append(ps, p)
   294  		}
   295  	}:
   296  		<-srv.peerOpDone
   297  	case <-srv.quit:
   298  	}
   299  	return ps
   300  }
   301  
   302  // PeerCount returns the number of connected peers.
   303  func (srv *Server) PeerCount() int {
   304  	var count int
   305  	select {
   306  	case srv.peerOp <- func(ps map[enode.ID]*Peer) { count = len(ps) }:
   307  		<-srv.peerOpDone
   308  	case <-srv.quit:
   309  	}
   310  	return count
   311  }
   312  
   313  // AddPeer connects to the given node and maintains the connection until the
   314  // server is shut down. If the connection fails for any reason, the server will
   315  // attempt to reconnect the peer.
   316  func (srv *Server) AddPeer(node *enode.Node) {
   317  	select {
   318  	case srv.addstatic <- node:
   319  	case <-srv.quit:
   320  	}
   321  }
   322  
   323  // RemovePeer disconnects from the given node
   324  func (srv *Server) RemovePeer(node *enode.Node) {
   325  	select {
   326  	case srv.removestatic <- node:
   327  	case <-srv.quit:
   328  	}
   329  }
   330  
   331  // AddTrustedPeer adds the given node to a reserved whitelist which allows the
   332  // node to always connect, even if the slot are full.
   333  func (srv *Server) AddTrustedPeer(node *enode.Node) {
   334  	select {
   335  	case srv.addtrusted <- node:
   336  	case <-srv.quit:
   337  	}
   338  }
   339  
   340  // RemoveTrustedPeer removes the given node from the trusted peer set.
   341  func (srv *Server) RemoveTrustedPeer(node *enode.Node) {
   342  	select {
   343  	case srv.removetrusted <- node:
   344  	case <-srv.quit:
   345  	}
   346  }
   347  
   348  // SubscribePeers subscribes the given channel to peer events
   349  func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription {
   350  	return srv.peerFeed.Subscribe(ch)
   351  }
   352  
   353  // Self returns the local node's endpoint information.
   354  func (srv *Server) Self() *enode.Node {
   355  	srv.lock.Lock()
   356  	ln := srv.localnode
   357  	srv.lock.Unlock()
   358  
   359  	if ln == nil {
   360  		return enode.NewV4(&srv.PrivateKey.PublicKey, net.ParseIP("0.0.0.0"), 0, 0)
   361  	}
   362  	return ln.Node()
   363  }
   364  
   365  // Stop terminates the server and all active peer connections.
   366  // It blocks until all active connections have been closed.
   367  func (srv *Server) Stop() {
   368  	srv.lock.Lock()
   369  	if !srv.running {
   370  		srv.lock.Unlock()
   371  		return
   372  	}
   373  	srv.running = false
   374  	if srv.listener != nil {
   375  		// this unblocks listener Accept
   376  		srv.listener.Close()
   377  	}
   378  	close(srv.quit)
   379  	srv.lock.Unlock()
   380  	srv.loopWG.Wait()
   381  }
   382  
   383  // sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
   384  // messages that were found unprocessable and sent to the unhandled channel by the primary listener.
   385  type sharedUDPConn struct {
   386  	*net.UDPConn
   387  	unhandled chan discover.ReadPacket
   388  }
   389  
   390  // ReadFromUDP implements discv5.conn
   391  func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
   392  	packet, ok := <-s.unhandled
   393  	if !ok {
   394  		return 0, nil, fmt.Errorf("Connection was closed")
   395  	}
   396  	l := len(packet.Data)
   397  	if l > len(b) {
   398  		l = len(b)
   399  	}
   400  	copy(b[:l], packet.Data[:l])
   401  	return l, packet.Addr, nil
   402  }
   403  
   404  // Close implements discv5.conn
   405  func (s *sharedUDPConn) Close() error {
   406  	return nil
   407  }
   408  
   409  // Start starts running the server.
   410  // Servers can not be re-used after stopping.
   411  func (srv *Server) Start() (err error) {
   412  	srv.lock.Lock()
   413  	defer srv.lock.Unlock()
   414  	if srv.running {
   415  		return errors.New("server already running")
   416  	}
   417  	srv.running = true
   418  	srv.log = srv.Config.Logger
   419  	if srv.log == nil {
   420  		srv.log = log.New()
   421  	}
   422  	if srv.NoDial && srv.ListenAddr == "" {
   423  		srv.log.Warn("P2P server will be useless, neither dialing nor listening")
   424  	}
   425  
   426  	// static fields
   427  	if srv.PrivateKey == nil {
   428  		return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
   429  	}
   430  	if srv.newTransport == nil {
   431  		srv.newTransport = newRLPX
   432  	}
   433  	if srv.Dialer == nil {
   434  		srv.Dialer = TCPDialer{&net.Dialer{Timeout: defaultDialTimeout}}
   435  	}
   436  	srv.quit = make(chan struct{})
   437  	srv.addpeer = make(chan *conn)
   438  	srv.delpeer = make(chan peerDrop)
   439  	srv.posthandshake = make(chan *conn)
   440  	srv.addstatic = make(chan *enode.Node)
   441  	srv.removestatic = make(chan *enode.Node)
   442  	srv.addtrusted = make(chan *enode.Node)
   443  	srv.removetrusted = make(chan *enode.Node)
   444  	srv.peerOp = make(chan peerOpFunc)
   445  	srv.peerOpDone = make(chan struct{})
   446  
   447  	if err := srv.setupLocalNode(); err != nil {
   448  		return err
   449  	}
   450  	if srv.ListenAddr != "" {
   451  		if err := srv.setupListening(); err != nil {
   452  			return err
   453  		}
   454  	}
   455  	if err := srv.setupDiscovery(); err != nil {
   456  		return err
   457  	}
   458  
   459  	dynPeers := srv.maxDialedConns()
   460  	dialer := newDialState(srv.localnode.ID(), srv.StaticNodes, srv.BootstrapNodes, srv.ntab, dynPeers, srv.NetRestrict)
   461  	srv.loopWG.Add(1)
   462  	go srv.run(dialer)
   463  	return nil
   464  }
   465  
   466  func (srv *Server) setupLocalNode() error {
   467  	// Create the devp2p handshake.
   468  	pubkey := crypto.FromECDSAPub(&srv.PrivateKey.PublicKey)
   469  	srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: pubkey[1:]}
   470  	for _, p := range srv.Protocols {
   471  		srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
   472  	}
   473  	sort.Sort(capsByNameAndVersion(srv.ourHandshake.Caps))
   474  
   475  	// Create the local node.
   476  	db, err := enode.OpenDB(srv.Config.NodeDatabase)
   477  	if err != nil {
   478  		return err
   479  	}
   480  	srv.nodedb = db
   481  	srv.localnode = enode.NewLocalNode(db, srv.PrivateKey)
   482  	srv.localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
   483  	srv.localnode.Set(capsByNameAndVersion(srv.ourHandshake.Caps))
   484  	// TODO: check conflicts
   485  	for _, p := range srv.Protocols {
   486  		for _, e := range p.Attributes {
   487  			srv.localnode.Set(e)
   488  		}
   489  	}
   490  	switch srv.NAT.(type) {
   491  	case nil:
   492  		// No NAT interface, do nothing.
   493  	case nat.ExtIP:
   494  		// ExtIP doesn't block, set the IP right away.
   495  		ip, _ := srv.NAT.ExternalIP()
   496  		srv.localnode.SetStaticIP(ip)
   497  	default:
   498  		// Ask the router about the IP. This takes a while and blocks startup,
   499  		// do it in the background.
   500  		srv.loopWG.Add(1)
   501  		go func() {
   502  			defer srv.loopWG.Done()
   503  			if ip, err := srv.NAT.ExternalIP(); err == nil {
   504  				srv.localnode.SetStaticIP(ip)
   505  			}
   506  		}()
   507  	}
   508  	return nil
   509  }
   510  
   511  func (srv *Server) setupDiscovery() error {
   512  	if srv.NoDiscovery && !srv.DiscoveryV5 {
   513  		return nil
   514  	}
   515  
   516  	addr, err := net.ResolveUDPAddr("udp", srv.ListenAddr)
   517  	if err != nil {
   518  		return err
   519  	}
   520  	conn, err := net.ListenUDP("udp", addr)
   521  	if err != nil {
   522  		return err
   523  	}
   524  	realaddr := conn.LocalAddr().(*net.UDPAddr)
   525  	srv.log.Debug("UDP listener up", "addr", realaddr)
   526  	if srv.NAT != nil {
   527  		if !realaddr.IP.IsLoopback() {
   528  			go nat.Map(srv.NAT, srv.quit, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
   529  		}
   530  	}
   531  	srv.localnode.SetFallbackUDP(realaddr.Port)
   532  
   533  	// Discovery V4
   534  	var unhandled chan discover.ReadPacket
   535  	var sconn *sharedUDPConn
   536  	if !srv.NoDiscovery {
   537  		if srv.DiscoveryV5 {
   538  			unhandled = make(chan discover.ReadPacket, 100)
   539  			sconn = &sharedUDPConn{conn, unhandled}
   540  		}
   541  		cfg := discover.Config{
   542  			PrivateKey:  srv.PrivateKey,
   543  			NetRestrict: srv.NetRestrict,
   544  			Bootnodes:   srv.BootstrapNodes,
   545  			Unhandled:   unhandled,
   546  		}
   547  		ntab, err := discover.ListenUDP(conn, srv.localnode, cfg)
   548  		if err != nil {
   549  			return err
   550  		}
   551  		srv.ntab = ntab
   552  	}
   553  	// Discovery V5
   554  	if srv.DiscoveryV5 {
   555  		var ntab *discv5.Network
   556  		var err error
   557  		if sconn != nil {
   558  			ntab, err = discv5.ListenUDP(srv.PrivateKey, sconn, "", srv.NetRestrict)
   559  		} else {
   560  			ntab, err = discv5.ListenUDP(srv.PrivateKey, conn, "", srv.NetRestrict)
   561  		}
   562  		if err != nil {
   563  			return err
   564  		}
   565  		if err := ntab.SetFallbackNodes(srv.BootstrapNodesV5); err != nil {
   566  			return err
   567  		}
   568  		srv.DiscV5 = ntab
   569  	}
   570  	return nil
   571  }
   572  
   573  func (srv *Server) setupListening() error {
   574  	// Launch the TCP listener.
   575  	listener, err := net.Listen("tcp", srv.ListenAddr)
   576  	if err != nil {
   577  		return err
   578  	}
   579  	laddr := listener.Addr().(*net.TCPAddr)
   580  	srv.ListenAddr = laddr.String()
   581  	srv.listener = listener
   582  	srv.localnode.Set(enr.TCP(laddr.Port))
   583  
   584  	srv.loopWG.Add(1)
   585  	go srv.listenLoop()
   586  
   587  	// Map the TCP listening port if NAT is configured.
   588  	if !laddr.IP.IsLoopback() && srv.NAT != nil {
   589  		srv.loopWG.Add(1)
   590  		go func() {
   591  			nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p")
   592  			srv.loopWG.Done()
   593  		}()
   594  	}
   595  	return nil
   596  }
   597  
   598  type dialer interface {
   599  	newTasks(running int, peers map[enode.ID]*Peer, now time.Time) []task
   600  	taskDone(task, time.Time)
   601  	addStatic(*enode.Node)
   602  	removeStatic(*enode.Node)
   603  }
   604  
   605  func (srv *Server) run(dialstate dialer) {
   606  	srv.log.Info("Started P2P networking", "self", srv.localnode.Node())
   607  	defer srv.loopWG.Done()
   608  	defer srv.nodedb.Close()
   609  
   610  	var (
   611  		peers        = make(map[enode.ID]*Peer)
   612  		inboundCount = 0
   613  		trusted      = make(map[enode.ID]bool, len(srv.TrustedNodes))
   614  		taskdone     = make(chan task, maxActiveDialTasks)
   615  		runningTasks []task
   616  		queuedTasks  []task // tasks that can't run yet
   617  	)
   618  	// Put trusted nodes into a map to speed up checks.
   619  	// Trusted peers are loaded on startup or added via AddTrustedPeer RPC.
   620  	for _, n := range srv.TrustedNodes {
   621  		trusted[n.ID()] = true
   622  	}
   623  
   624  	// removes t from runningTasks
   625  	delTask := func(t task) {
   626  		for i := range runningTasks {
   627  			if runningTasks[i] == t {
   628  				runningTasks = append(runningTasks[:i], runningTasks[i+1:]...)
   629  				break
   630  			}
   631  		}
   632  	}
   633  	// starts until max number of active tasks is satisfied
   634  	startTasks := func(ts []task) (rest []task) {
   635  		i := 0
   636  		for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ {
   637  			t := ts[i]
   638  			srv.log.Trace("New dial task", "task", t)
   639  			go func() { t.Do(srv); taskdone <- t }()
   640  			runningTasks = append(runningTasks, t)
   641  		}
   642  		return ts[i:]
   643  	}
   644  	scheduleTasks := func() {
   645  		// Start from queue first.
   646  		queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...)
   647  		// Query dialer for new tasks and start as many as possible now.
   648  		if len(runningTasks) < maxActiveDialTasks {
   649  			nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now())
   650  			queuedTasks = append(queuedTasks, startTasks(nt)...)
   651  		}
   652  	}
   653  
   654  running:
   655  	for {
   656  		scheduleTasks()
   657  
   658  		select {
   659  		case <-srv.quit:
   660  			// The server was stopped. Run the cleanup logic.
   661  			break running
   662  		case n := <-srv.addstatic:
   663  			// This channel is used by AddPeer to add to the
   664  			// ephemeral static peer list. Add it to the dialer,
   665  			// it will keep the node connected.
   666  			srv.log.Trace("Adding static node", "node", n)
   667  			dialstate.addStatic(n)
   668  		case n := <-srv.removestatic:
   669  			// This channel is used by RemovePeer to send a
   670  			// disconnect request to a peer and begin the
   671  			// stop keeping the node connected.
   672  			srv.log.Trace("Removing static node", "node", n)
   673  			dialstate.removeStatic(n)
   674  			if p, ok := peers[n.ID()]; ok {
   675  				p.Disconnect(DiscRequested)
   676  			}
   677  		case n := <-srv.addtrusted:
   678  			// This channel is used by AddTrustedPeer to add an enode
   679  			// to the trusted node set.
   680  			srv.log.Trace("Adding trusted node", "node", n)
   681  			trusted[n.ID()] = true
   682  			// Mark any already-connected peer as trusted
   683  			if p, ok := peers[n.ID()]; ok {
   684  				p.rw.set(trustedConn, true)
   685  			}
   686  		case n := <-srv.removetrusted:
   687  			// This channel is used by RemoveTrustedPeer to remove an enode
   688  			// from the trusted node set.
   689  			srv.log.Trace("Removing trusted node", "node", n)
   690  			if _, ok := trusted[n.ID()]; ok {
   691  				delete(trusted, n.ID())
   692  			}
   693  			// Unmark any already-connected peer as trusted
   694  			if p, ok := peers[n.ID()]; ok {
   695  				p.rw.set(trustedConn, false)
   696  			}
   697  		case op := <-srv.peerOp:
   698  			// This channel is used by Peers and PeerCount.
   699  			op(peers)
   700  			srv.peerOpDone <- struct{}{}
   701  		case t := <-taskdone:
   702  			// A task got done. Tell dialstate about it so it
   703  			// can update its state and remove it from the active
   704  			// tasks list.
   705  			srv.log.Trace("Dial task done", "task", t)
   706  			dialstate.taskDone(t, time.Now())
   707  			delTask(t)
   708  		case c := <-srv.posthandshake:
   709  			// A connection has passed the encryption handshake so
   710  			// the remote identity is known (but hasn't been verified yet).
   711  			if trusted[c.node.ID()] {
   712  				// Ensure that the trusted flag is set before checking against MaxPeers.
   713  				c.flags |= trustedConn
   714  			}
   715  			// TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them.
   716  			select {
   717  			case c.cont <- srv.encHandshakeChecks(peers, inboundCount, c):
   718  			case <-srv.quit:
   719  				break running
   720  			}
   721  		case c := <-srv.addpeer:
   722  			// At this point the connection is past the protocol handshake.
   723  			// Its capabilities are known and the remote identity is verified.
   724  			err := srv.protoHandshakeChecks(peers, inboundCount, c)
   725  			if err == nil {
   726  				// The handshakes are done and it passed all checks.
   727  				p := newPeer(c, srv.Protocols)
   728  				// If message events are enabled, pass the peerFeed
   729  				// to the peer
   730  				if srv.EnableMsgEvents {
   731  					p.events = &srv.peerFeed
   732  				}
   733  				name := truncateName(c.name)
   734  				srv.log.Debug("XXX New p2p peer", "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1)
   735  				go srv.runPeer(p)
   736  				peers[c.node.ID()] = p
   737  				if p.Inbound() {
   738  					inboundCount++
   739  				}
   740  			}
   741  			// The dialer logic relies on the assumption that
   742  			// dial tasks complete after the peer has been added or
   743  			// discarded. Unblock the task last.
   744  			select {
   745  			case c.cont <- err:
   746  			case <-srv.quit:
   747  				break running
   748  			}
   749  		case pd := <-srv.delpeer:
   750  			// A peer disconnected.
   751  			d := common.PrettyDuration(mclock.Now() - pd.created)
   752  			pd.log.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err)
   753  			delete(peers, pd.ID())
   754  			if pd.Inbound() {
   755  				inboundCount--
   756  			}
   757  		}
   758  	}
   759  
   760  	srv.log.Trace("P2P networking is spinning down")
   761  
   762  	// Terminate discovery. If there is a running lookup it will terminate soon.
   763  	if srv.ntab != nil {
   764  		srv.ntab.Close()
   765  	}
   766  	if srv.DiscV5 != nil {
   767  		srv.DiscV5.Close()
   768  	}
   769  	// Disconnect all peers.
   770  	for _, p := range peers {
   771  		p.Disconnect(DiscQuitting)
   772  	}
   773  	// Wait for peers to shut down. Pending connections and tasks are
   774  	// not handled here and will terminate soon-ish because srv.quit
   775  	// is closed.
   776  	for len(peers) > 0 {
   777  		p := <-srv.delpeer
   778  		p.log.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks))
   779  		delete(peers, p.ID())
   780  	}
   781  }
   782  
   783  func (srv *Server) protoHandshakeChecks(peers map[enode.ID]*Peer, inboundCount int, c *conn) error {
   784  	// Drop connections with no matching protocols.
   785  	if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, c.caps) == 0 {
   786  		return DiscUselessPeer
   787  	}
   788  	// Repeat the encryption handshake checks because the
   789  	// peer set might have changed between the handshakes.
   790  	return srv.encHandshakeChecks(peers, inboundCount, c)
   791  }
   792  
   793  func (srv *Server) encHandshakeChecks(peers map[enode.ID]*Peer, inboundCount int, c *conn) error {
   794  	switch {
   795  	case !c.is(trustedConn|staticDialedConn) && len(peers) >= srv.MaxPeers:
   796  		return DiscTooManyPeers
   797  	case !c.is(trustedConn) && c.is(inboundConn) && inboundCount >= srv.maxInboundConns():
   798  		return DiscTooManyPeers
   799  	case peers[c.node.ID()] != nil:
   800  		return DiscAlreadyConnected
   801  	case c.node.ID() == srv.localnode.ID():
   802  		return DiscSelf
   803  	default:
   804  		return nil
   805  	}
   806  }
   807  
   808  func (srv *Server) maxInboundConns() int {
   809  	return srv.MaxPeers - srv.maxDialedConns()
   810  }
   811  func (srv *Server) maxDialedConns() int {
   812  	if srv.NoDiscovery || srv.NoDial {
   813  		return 0
   814  	}
   815  	r := srv.DialRatio
   816  	if r == 0 {
   817  		r = defaultDialRatio
   818  	}
   819  	return srv.MaxPeers / r
   820  }
   821  
   822  // listenLoop runs in its own goroutine and accepts
   823  // inbound connections.
   824  func (srv *Server) listenLoop() {
   825  	defer srv.loopWG.Done()
   826  	srv.log.Debug("TCP listener up", "addr", srv.listener.Addr())
   827  
   828  	tokens := defaultMaxPendingPeers
   829  	if srv.MaxPendingPeers > 0 {
   830  		tokens = srv.MaxPendingPeers
   831  	}
   832  	slots := make(chan struct{}, tokens)
   833  	for i := 0; i < tokens; i++ {
   834  		slots <- struct{}{}
   835  	}
   836  
   837  	for {
   838  		// Wait for a handshake slot before accepting.
   839  		<-slots
   840  
   841  		var (
   842  			fd  net.Conn
   843  			err error
   844  		)
   845  		for {
   846  			fd, err = srv.listener.Accept()
   847  			if netutil.IsTemporaryError(err) {
   848  				srv.log.Debug("Temporary read error", "err", err)
   849  				continue
   850  			} else if err != nil {
   851  				srv.log.Debug("Read error", "err", err)
   852  				return
   853  			}
   854  			break
   855  		}
   856  
   857  		// Reject connections that do not match NetRestrict.
   858  		if srv.NetRestrict != nil {
   859  			if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) {
   860  				srv.log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr())
   861  				fd.Close()
   862  				slots <- struct{}{}
   863  				continue
   864  			}
   865  		}
   866  
   867  		var ip net.IP
   868  		if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok {
   869  			ip = tcp.IP
   870  		}
   871  		fd = newMeteredConn(fd, true, ip)
   872  		srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr())
   873  		go func() {
   874  			srv.SetupConn(fd, inboundConn, nil)
   875  			slots <- struct{}{}
   876  		}()
   877  	}
   878  }
   879  
   880  // SetupConn runs the handshakes and attempts to add the connection
   881  // as a peer. It returns when the connection has been added as a peer
   882  // or the handshakes have failed.
   883  func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error {
   884  	c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)}
   885  	err := srv.setupConn(c, flags, dialDest)
   886  	if err != nil {
   887  		c.close(err)
   888  		srv.log.Trace("Setting up connection failed", "addr", fd.RemoteAddr(), "err", err)
   889  	}
   890  	return err
   891  }
   892  
   893  func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) error {
   894  	// Prevent leftover pending conns from entering the handshake.
   895  	srv.lock.Lock()
   896  	running := srv.running
   897  	srv.lock.Unlock()
   898  	if !running {
   899  		return errServerStopped
   900  	}
   901  	// If dialing, figure out the remote public key.
   902  	var dialPubkey *ecdsa.PublicKey
   903  	if dialDest != nil {
   904  		dialPubkey = new(ecdsa.PublicKey)
   905  		if err := dialDest.Load((*enode.Secp256k1)(dialPubkey)); err != nil {
   906  			return fmt.Errorf("dial destination doesn't have a secp256k1 public key")
   907  		}
   908  	}
   909  	// Run the encryption handshake.
   910  	remotePubkey, err := c.doEncHandshake(srv.PrivateKey, dialPubkey)
   911  	if err != nil {
   912  		srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
   913  		return err
   914  	}
   915  	if dialDest != nil {
   916  		// For dialed connections, check that the remote public key matches.
   917  		if dialPubkey.X.Cmp(remotePubkey.X) != 0 || dialPubkey.Y.Cmp(remotePubkey.Y) != 0 {
   918  			return DiscUnexpectedIdentity
   919  		}
   920  		c.node = dialDest
   921  	} else {
   922  		c.node = nodeFromConn(remotePubkey, c.fd)
   923  	}
   924  	if conn, ok := c.fd.(*meteredConn); ok {
   925  		conn.handshakeDone(c.node.ID())
   926  	}
   927  	clog := srv.log.New("id", c.node.ID(), "addr", c.fd.RemoteAddr(), "conn", c.flags)
   928  	err = srv.checkpoint(c, srv.posthandshake)
   929  	if err != nil {
   930  		clog.Trace("Rejected peer before protocol handshake", "err", err)
   931  		return err
   932  	}
   933  	// Run the protocol handshake
   934  	phs, err := c.doProtoHandshake(srv.ourHandshake)
   935  	if err != nil {
   936  		clog.Trace("Failed proto handshake", "err", err)
   937  		return err
   938  	}
   939  	if id := c.node.ID(); !bytes.Equal(crypto.Keccak256(phs.ID), id[:]) {
   940  		clog.Trace("Wrong devp2p handshake identity", "phsid", fmt.Sprintf("%x", phs.ID))
   941  		return DiscUnexpectedIdentity
   942  	}
   943  	c.caps, c.name = phs.Caps, phs.Name
   944  	err = srv.checkpoint(c, srv.addpeer)
   945  	if err != nil {
   946  		clog.Trace("Rejected peer", "err", err)
   947  		return err
   948  	}
   949  	// If the checks completed successfully, runPeer has now been
   950  	// launched by run.
   951  	clog.Trace("connection set up", "inbound", dialDest == nil)
   952  	return nil
   953  }
   954  
   955  func nodeFromConn(pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node {
   956  	var ip net.IP
   957  	var port int
   958  	if tcp, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
   959  		ip = tcp.IP
   960  		port = tcp.Port
   961  	}
   962  	return enode.NewV4(pubkey, ip, port, port)
   963  }
   964  
   965  func truncateName(s string) string {
   966  	if len(s) > 20 {
   967  		return s[:20] + "..."
   968  	}
   969  	return s
   970  }
   971  
   972  // checkpoint sends the conn to run, which performs the
   973  // post-handshake checks for the stage (posthandshake, addpeer).
   974  func (srv *Server) checkpoint(c *conn, stage chan<- *conn) error {
   975  	select {
   976  	case stage <- c:
   977  	case <-srv.quit:
   978  		return errServerStopped
   979  	}
   980  	select {
   981  	case err := <-c.cont:
   982  		return err
   983  	case <-srv.quit:
   984  		return errServerStopped
   985  	}
   986  }
   987  
   988  // runPeer runs in its own goroutine for each peer.
   989  // it waits until the Peer logic returns and removes
   990  // the peer.
   991  func (srv *Server) runPeer(p *Peer) {
   992  	if srv.newPeerHook != nil {
   993  		srv.newPeerHook(p)
   994  	}
   995  
   996  	// broadcast peer add
   997  	srv.peerFeed.Send(&PeerEvent{
   998  		Type: PeerEventTypeAdd,
   999  		Peer: p.ID(),
  1000  	})
  1001  
  1002  	// run the protocol
  1003  	remoteRequested, err := p.run()
  1004  
  1005  	// broadcast peer drop
  1006  	srv.peerFeed.Send(&PeerEvent{
  1007  		Type:  PeerEventTypeDrop,
  1008  		Peer:  p.ID(),
  1009  		Error: err.Error(),
  1010  	})
  1011  
  1012  	// Note: run waits for existing peers to be sent on srv.delpeer
  1013  	// before returning, so this send should not select on srv.quit.
  1014  	srv.delpeer <- peerDrop{p, err, remoteRequested}
  1015  }
  1016  
  1017  // NodeInfo represents a short summary of the information known about the host.
  1018  type NodeInfo struct {
  1019  	ID    string `json:"id"`    // Unique node identifier (also the encryption key)
  1020  	Name  string `json:"name"`  // Name of the node, including client type, version, OS, custom data
  1021  	Enode string `json:"enode"` // Enode URL for adding this peer from remote peers
  1022  	ENR   string `json:"enr"`   // Ethereum Node Record
  1023  	IP    string `json:"ip"`    // IP address of the node
  1024  	Ports struct {
  1025  		Discovery int `json:"discovery"` // UDP listening port for discovery protocol
  1026  		Listener  int `json:"listener"`  // TCP listening port for RLPx
  1027  	} `json:"ports"`
  1028  	ListenAddr string                 `json:"listenAddr"`
  1029  	Protocols  map[string]interface{} `json:"protocols"`
  1030  }
  1031  
  1032  // NodeInfo gathers and returns a collection of metadata known about the host.
  1033  func (srv *Server) NodeInfo() *NodeInfo {
  1034  	// Gather and assemble the generic node infos
  1035  	node := srv.Self()
  1036  	info := &NodeInfo{
  1037  		Name:       srv.Name,
  1038  		Enode:      node.String(),
  1039  		ID:         node.ID().String(),
  1040  		IP:         node.IP().String(),
  1041  		ListenAddr: srv.ListenAddr,
  1042  		Protocols:  make(map[string]interface{}),
  1043  	}
  1044  	info.Ports.Discovery = node.UDP()
  1045  	info.Ports.Listener = node.TCP()
  1046  	if enc, err := rlp.EncodeToBytes(node.Record()); err == nil {
  1047  		info.ENR = "0x" + hex.EncodeToString(enc)
  1048  	}
  1049  
  1050  	// Gather all the running protocol infos (only once per protocol type)
  1051  	for _, proto := range srv.Protocols {
  1052  		if _, ok := info.Protocols[proto.Name]; !ok {
  1053  			nodeInfo := interface{}("unknown")
  1054  			if query := proto.NodeInfo; query != nil {
  1055  				nodeInfo = proto.NodeInfo()
  1056  			}
  1057  			info.Protocols[proto.Name] = nodeInfo
  1058  		}
  1059  	}
  1060  	return info
  1061  }
  1062  
  1063  // PeersInfo returns an array of metadata objects describing connected peers.
  1064  func (srv *Server) PeersInfo() []*PeerInfo {
  1065  	// Gather all the generic and sub-protocol specific infos
  1066  	infos := make([]*PeerInfo, 0, srv.PeerCount())
  1067  	for _, peer := range srv.Peers() {
  1068  		if peer != nil {
  1069  			infos = append(infos, peer.Info())
  1070  		}
  1071  	}
  1072  	// Sort the result array alphabetically by node identifier
  1073  	for i := 0; i < len(infos); i++ {
  1074  		for j := i + 1; j < len(infos); j++ {
  1075  			if infos[i].ID > infos[j].ID {
  1076  				infos[i], infos[j] = infos[j], infos[i]
  1077  			}
  1078  		}
  1079  	}
  1080  	return infos
  1081  }