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