github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/p2p/server.go (about)

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