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