github.com/klaytn/klaytn@v1.12.1/networks/p2p/server.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from p2p/server.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package p2p
    22  
    23  import (
    24  	"crypto/ecdsa"
    25  	"errors"
    26  	"fmt"
    27  	"net"
    28  	"strconv"
    29  	"strings"
    30  	"sync"
    31  	"time"
    32  
    33  	"github.com/klaytn/klaytn/common"
    34  	"github.com/klaytn/klaytn/common/mclock"
    35  	"github.com/klaytn/klaytn/event"
    36  	"github.com/klaytn/klaytn/log"
    37  	"github.com/klaytn/klaytn/networks/p2p/discover"
    38  	"github.com/klaytn/klaytn/networks/p2p/nat"
    39  	"github.com/klaytn/klaytn/networks/p2p/netutil"
    40  )
    41  
    42  const (
    43  	defaultDialTimeout = 15 * time.Second
    44  
    45  	// Connectivity defaults.
    46  	maxActiveDialTasks     = 16
    47  	defaultMaxPendingPeers = 50
    48  	defaultDialRatio       = 3
    49  
    50  	// Maximum time allowed for reading a complete message.
    51  	// This is effectively the amount of time a connection can be idle.
    52  	frameReadTimeout = 30 * time.Second
    53  
    54  	// Maximum amount of time allowed for writing a complete message.
    55  	frameWriteTimeout = 20 * time.Second
    56  )
    57  
    58  var errServerStopped = errors.New("server stopped")
    59  
    60  // Config holds Server options.
    61  type Config struct {
    62  	// This field must be set to a valid secp256k1 private key.
    63  	PrivateKey *ecdsa.PrivateKey `toml:"-"`
    64  
    65  	// MaxPhysicalConnections is the maximum number of physical connections.
    66  	// A peer uses one connection if single channel peer and uses two connections if
    67  	// multi channel peer. It must be greater than zero.
    68  	MaxPhysicalConnections int
    69  
    70  	// ConnectionType is a type of connection like Consensus or Normal
    71  	// described at common.ConnType
    72  	// When the connection is established, each peer exchange each connection type
    73  	ConnectionType common.ConnType
    74  
    75  	// MaxPendingPeers is the maximum number of peers that can be pending in the
    76  	// handshake phase, counted separately for inbound and outbound connections.
    77  	// Zero defaults to preset values.
    78  	MaxPendingPeers int `toml:",omitempty"`
    79  
    80  	// DialRatio controls the ratio of inbound to dialed connections.
    81  	// Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
    82  	// Setting DialRatio to zero defaults it to 3.
    83  	DialRatio int `toml:",omitempty"`
    84  
    85  	// NoDiscovery can be used to disable the peer discovery mechanism.
    86  	// Disabling is useful for protocol debugging (manual topology).
    87  	NoDiscovery bool
    88  
    89  	// Name sets the node name of this server.
    90  	// Use common.MakeName to create a name that follows existing conventions.
    91  	Name string `toml:"-"`
    92  
    93  	// BootstrapNodes are used to establish connectivity
    94  	// with the rest of the network.
    95  	BootstrapNodes []*discover.Node
    96  
    97  	//// BootstrapNodesV5 are used to establish connectivity
    98  	//// with the rest of the network using the V5 discovery
    99  	//// protocol.
   100  	//BootstrapNodesV5 []*discv5.Node `toml:",omitempty"`
   101  
   102  	// Static nodes are used as pre-configured connections which are always
   103  	// maintained and re-connected on disconnects.
   104  	StaticNodes []*discover.Node
   105  
   106  	// Trusted nodes are used as pre-configured connections which are always
   107  	// allowed to connect, even above the peer limit.
   108  	TrustedNodes []*discover.Node
   109  
   110  	// Connectivity can be restricted to certain IP networks.
   111  	// If this option is set to a non-nil value, only hosts which match one of the
   112  	// IP networks contained in the list are considered.
   113  	NetRestrict *netutil.Netlist `toml:",omitempty"`
   114  
   115  	// NodeDatabase is the path to the database containing the previously seen
   116  	// live nodes in the network.
   117  	NodeDatabase string `toml:",omitempty"`
   118  
   119  	// Protocols should contain the protocols supported
   120  	// by the server. Matching protocols are launched for
   121  	// each peer.
   122  	Protocols []Protocol `toml:"-"`
   123  
   124  	// If ListenAddr is set to a non-nil address, the server
   125  	// will listen for incoming connections.
   126  	//
   127  	// If the port is zero, the operating system will pick a port. The
   128  	// ListenAddr field will be updated with the actual address when
   129  	// the server is started.
   130  	ListenAddr string
   131  
   132  	// NoListen can be used to disable the listening for incoming connections.
   133  	NoListen bool
   134  
   135  	// SubListenAddr is the list of the secondary listen address used for peer-to-peer connections.
   136  	SubListenAddr []string
   137  
   138  	// If EnableMultiChannelServer is true, multichannel can communicate with other nodes
   139  	EnableMultiChannelServer bool
   140  
   141  	// If set to a non-nil value, the given NAT port mapper
   142  	// is used to make the listening port available to the
   143  	// Internet.
   144  	NAT nat.Interface `toml:",omitempty"`
   145  
   146  	// If Dialer is set to a non-nil value, the given Dialer
   147  	// is used to dial outbound peer connections.
   148  	Dialer NodeDialer `toml:"-"`
   149  
   150  	// If NoDial is true, the server will not dial any peers.
   151  	NoDial bool `toml:",omitempty"`
   152  
   153  	// If EnableMsgEvents is set then the server will emit PeerEvents
   154  	// whenever a message is sent to or received from a peer
   155  	EnableMsgEvents bool
   156  
   157  	// Logger is a custom logger to use with the p2p.Server.
   158  	Logger log.Logger `toml:",omitempty"`
   159  
   160  	// RWTimerConfig is a configuration for interval based timer for rw.
   161  	// It checks if a rw successfully writes its task in given time.
   162  	RWTimerConfig RWTimerConfig
   163  
   164  	// NetworkID to use for selecting peers to connect to
   165  	NetworkID uint64
   166  }
   167  
   168  // NewServer returns a new Server interface.
   169  func NewServer(config Config) Server {
   170  	bServer := &BaseServer{
   171  		Config: config,
   172  	}
   173  
   174  	if config.EnableMultiChannelServer {
   175  		listeners := make([]net.Listener, 0, len(config.SubListenAddr)+1)
   176  		listenAddrs := make([]string, 0, len(config.SubListenAddr)+1)
   177  		listenAddrs = append(listenAddrs, config.ListenAddr)
   178  		listenAddrs = append(listenAddrs, config.SubListenAddr...)
   179  		return &MultiChannelServer{
   180  			BaseServer:     bServer,
   181  			listeners:      listeners,
   182  			ListenAddrs:    listenAddrs,
   183  			CandidateConns: make(map[discover.NodeID][]*conn),
   184  		}
   185  	} else {
   186  		return &SingleChannelServer{
   187  			BaseServer: bServer,
   188  		}
   189  	}
   190  }
   191  
   192  // Server manages all peer connections.
   193  type Server interface {
   194  	// GetProtocols returns a slice of protocols.
   195  	GetProtocols() []Protocol
   196  
   197  	// AddProtocols adds protocols to the server.
   198  	AddProtocols(p []Protocol)
   199  
   200  	// SetupConn runs the handshakes and attempts to add the connection
   201  	// as a peer. It returns when the connection has been added as a peer
   202  	// or the handshakes have failed.
   203  	SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error
   204  
   205  	// AddLastLookup adds lastLookup to duration.
   206  	AddLastLookup() time.Time
   207  
   208  	// SetLastLookupToNow sets LastLookup to the current time.
   209  	SetLastLookupToNow()
   210  
   211  	// CheckNilNetworkTable returns whether network table is nil.
   212  	CheckNilNetworkTable() bool
   213  
   214  	// GetNodes returns up to max alive nodes which a NodeType is nType
   215  	GetNodes(nType discover.NodeType, max int) []*discover.Node
   216  
   217  	// Lookup performs a network search for nodes close
   218  	// to the given target. It approaches the target by querying
   219  	// nodes that are closer to it on each iteration.
   220  	// The given target does not need to be an actual node
   221  	// identifier.
   222  	Lookup(target discover.NodeID, nType discover.NodeType) []*discover.Node
   223  
   224  	// Resolve searches for a specific node with the given ID and NodeType.
   225  	// It returns nil if the node could not be found.
   226  	Resolve(target discover.NodeID, nType discover.NodeType) *discover.Node
   227  
   228  	// Start starts running the server.
   229  	// Servers can not be re-used after stopping.
   230  	Start() (err error)
   231  
   232  	// Stop terminates the server and all active peer connections.
   233  	// It blocks until all active connections are closed.
   234  	Stop()
   235  
   236  	// AddPeer connects to the given node and maintains the connection until the
   237  	// server is shut down. If the connection fails for any reason, the server will
   238  	// attempt to reconnect the peer.
   239  	AddPeer(node *discover.Node)
   240  
   241  	// RemovePeer disconnects from the given node.
   242  	RemovePeer(node *discover.Node)
   243  
   244  	// SubscribePeers subscribes the given channel to peer events.
   245  	SubscribeEvents(ch chan *PeerEvent) event.Subscription
   246  
   247  	// PeersInfo returns an array of metadata objects describing connected peers.
   248  	PeersInfo() []*PeerInfo
   249  
   250  	// NodeInfo gathers and returns a collection of metadata known about the host.
   251  	NodeInfo() *NodeInfo
   252  
   253  	// Name returns name of server.
   254  	Name() string
   255  
   256  	// PeerCount returns the number of connected peers.
   257  	PeerCount() int
   258  
   259  	// PeerCountByType returns the number of connected specific tyeps of peers.
   260  	PeerCountByType() map[string]uint
   261  
   262  	// MaxPhysicalConnections returns maximum count of peers.
   263  	MaxPeers() int
   264  
   265  	// Disconnect tries to disconnect peer.
   266  	Disconnect(destID discover.NodeID)
   267  
   268  	// GetListenAddress returns the listen address list of the server.
   269  	GetListenAddress() []string
   270  
   271  	// Peers returns all connected peers.
   272  	Peers() []*Peer
   273  
   274  	// NodeDialer is used to connect to nodes in the network, typically by using
   275  	// an underlying net.Dialer but also using net.Pipe in tests.
   276  	NodeDialer
   277  }
   278  
   279  // MultiChannelServer is a server that uses a multi channel.
   280  type MultiChannelServer struct {
   281  	*BaseServer
   282  	listeners      []net.Listener
   283  	ListenAddrs    []string
   284  	CandidateConns map[discover.NodeID][]*conn
   285  }
   286  
   287  // Start starts running the MultiChannelServer.
   288  // MultiChannelServer can not be re-used after stopping.
   289  func (srv *MultiChannelServer) Start() (err error) {
   290  	srv.lock.Lock()
   291  	defer srv.lock.Unlock()
   292  	if srv.running {
   293  		return errors.New("server already running")
   294  	}
   295  	srv.running = true
   296  	srv.logger = srv.Config.Logger
   297  	if srv.logger == nil {
   298  		srv.logger = logger.NewWith()
   299  	}
   300  	srv.logger.Info("Starting P2P networking")
   301  
   302  	// static fields
   303  	if srv.PrivateKey == nil {
   304  		return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
   305  	}
   306  
   307  	if !srv.ConnectionType.Valid() {
   308  		return fmt.Errorf("Invalid connection type speficied")
   309  	}
   310  
   311  	if srv.newTransport == nil {
   312  		srv.newTransport = newRLPX
   313  	}
   314  	if srv.Dialer == nil {
   315  		srv.Dialer = TCPDialer{&net.Dialer{Timeout: defaultDialTimeout}}
   316  	}
   317  	srv.quit = make(chan struct{})
   318  	srv.addpeer = make(chan *conn)
   319  	srv.delpeer = make(chan peerDrop)
   320  	srv.posthandshake = make(chan *conn)
   321  	srv.addstatic = make(chan *discover.Node)
   322  	srv.removestatic = make(chan *discover.Node)
   323  	srv.peerOp = make(chan peerOpFunc)
   324  	srv.peerOpDone = make(chan struct{})
   325  	srv.discpeer = make(chan discover.NodeID)
   326  
   327  	var (
   328  		conn      *net.UDPConn
   329  		realaddr  *net.UDPAddr
   330  		unhandled chan discover.ReadPacket
   331  	)
   332  
   333  	if !srv.NoDiscovery {
   334  		addr, err := net.ResolveUDPAddr("udp", srv.ListenAddrs[ConnDefault])
   335  		if err != nil {
   336  			return err
   337  		}
   338  		conn, err = net.ListenUDP("udp", addr)
   339  		if err != nil {
   340  			return err
   341  		}
   342  		realaddr = conn.LocalAddr().(*net.UDPAddr)
   343  		if srv.NAT != nil {
   344  			if !realaddr.IP.IsLoopback() {
   345  				go nat.Map(srv.NAT, srv.quit, "udp", realaddr.Port, realaddr.Port, "klaytn discovery")
   346  			}
   347  			// TODO: react to external IP changes over time.
   348  			if ext, err := srv.NAT.ExternalIP(); err == nil {
   349  				realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
   350  			}
   351  		}
   352  	}
   353  
   354  	// node table
   355  	if !srv.NoDiscovery {
   356  		cfg := discover.Config{
   357  			PrivateKey:   srv.PrivateKey,
   358  			AnnounceAddr: realaddr,
   359  			NodeDBPath:   srv.NodeDatabase,
   360  			NetRestrict:  srv.NetRestrict,
   361  			Bootnodes:    srv.BootstrapNodes,
   362  			Unhandled:    unhandled,
   363  			Conn:         conn,
   364  			Addr:         realaddr,
   365  			Id:           discover.PubkeyID(&srv.PrivateKey.PublicKey),
   366  			NodeType:     ConvertNodeType(srv.ConnectionType),
   367  			NetworkID:    srv.NetworkID,
   368  		}
   369  
   370  		ntab, err := discover.ListenUDP(&cfg)
   371  		if err != nil {
   372  			return err
   373  		}
   374  		srv.ntab = ntab
   375  	}
   376  
   377  	dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, srv.maxDialedConns(), srv.NetRestrict, srv.PrivateKey, srv.getTypeStatics())
   378  
   379  	// handshake
   380  	srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name(), ID: discover.PubkeyID(&srv.PrivateKey.PublicKey), Multichannel: true}
   381  	for _, p := range srv.Protocols {
   382  		srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
   383  	}
   384  	for _, l := range srv.ListenAddrs {
   385  		s := strings.Split(l, ":")
   386  		if len(s) == 2 {
   387  			if port, err := strconv.Atoi(s[1]); err == nil {
   388  				srv.ourHandshake.ListenPort = append(srv.ourHandshake.ListenPort, uint64(port))
   389  			}
   390  		}
   391  	}
   392  
   393  	// listen/dial
   394  	if srv.NoDial && srv.NoListen {
   395  		srv.logger.Error("P2P server will be useless, neither dialing nor listening")
   396  	}
   397  	if !srv.NoListen {
   398  		if srv.ListenAddrs != nil && len(srv.ListenAddrs) != 0 && srv.ListenAddrs[ConnDefault] != "" {
   399  			if err := srv.startListening(); err != nil {
   400  				return err
   401  			}
   402  		} else {
   403  			srv.logger.Error("P2P server might be useless, listening address is missing")
   404  		}
   405  	}
   406  
   407  	srv.loopWG.Add(1)
   408  	go srv.run(dialer)
   409  	srv.running = true
   410  	srv.logger.Info("Started P2P server", "id", discover.PubkeyID(&srv.PrivateKey.PublicKey), "multichannel", true)
   411  	return nil
   412  }
   413  
   414  // startListening starts listening on the specified port on the server.
   415  func (srv *MultiChannelServer) startListening() error {
   416  	// Launch the TCP listener.
   417  	for i, listenAddr := range srv.ListenAddrs {
   418  		listener, err := net.Listen("tcp", listenAddr)
   419  		if err != nil {
   420  			return err
   421  		}
   422  		laddr := listener.Addr().(*net.TCPAddr)
   423  		srv.ListenAddrs[i] = laddr.String()
   424  		srv.listeners = append(srv.listeners, listener)
   425  		srv.loopWG.Add(1)
   426  		go srv.listenLoop(listener)
   427  		// Map the TCP listening port if NAT is configured.
   428  		if !laddr.IP.IsLoopback() && srv.NAT != nil {
   429  			srv.loopWG.Add(1)
   430  			go func() {
   431  				nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "klaytn p2p")
   432  				srv.loopWG.Done()
   433  			}()
   434  		}
   435  	}
   436  	return nil
   437  }
   438  
   439  // listenLoop waits for an external connection and connects it.
   440  func (srv *MultiChannelServer) listenLoop(listener net.Listener) {
   441  	defer srv.loopWG.Done()
   442  	srv.logger.Info("RLPx listener up", "self", srv.makeSelf(listener, srv.ntab))
   443  
   444  	tokens := defaultMaxPendingPeers
   445  	if srv.MaxPendingPeers > 0 {
   446  		tokens = srv.MaxPendingPeers
   447  	}
   448  	slots := make(chan struct{}, tokens)
   449  	for i := 0; i < tokens; i++ {
   450  		slots <- struct{}{}
   451  	}
   452  
   453  	for {
   454  		// Wait for a handshake slot before accepting.
   455  		<-slots
   456  
   457  		var (
   458  			fd  net.Conn
   459  			err error
   460  		)
   461  		for {
   462  			fd, err = listener.Accept()
   463  			if tempErr, ok := err.(tempError); ok && tempErr.Temporary() {
   464  				srv.logger.Debug("Temporary read error", "err", err)
   465  				continue
   466  			} else if err != nil {
   467  				srv.logger.Debug("Read error", "err", err)
   468  				return
   469  			}
   470  			break
   471  		}
   472  
   473  		// Reject connections that do not match NetRestrict.
   474  		if srv.NetRestrict != nil {
   475  			if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) {
   476  				srv.logger.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr())
   477  				fd.Close()
   478  				slots <- struct{}{}
   479  				continue
   480  			}
   481  		}
   482  
   483  		fd = newMeteredConn(fd, true)
   484  		srv.logger.Trace("Accepted connection", "addr", fd.RemoteAddr())
   485  		go func() {
   486  			srv.SetupConn(fd, inboundConn, nil)
   487  			slots <- struct{}{}
   488  		}()
   489  	}
   490  }
   491  
   492  // SetupConn runs the handshakes and attempts to add the connection
   493  // as a peer. It returns when the connection has been added as a peer
   494  // or the handshakes have failed.
   495  func (srv *MultiChannelServer) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error {
   496  	self := srv.Self()
   497  	if self == nil {
   498  		return errors.New("shutdown")
   499  	}
   500  
   501  	c := &conn{fd: fd, flags: flags, conntype: common.ConnTypeUndefined, cont: make(chan error), portOrder: PortOrderUndefined}
   502  
   503  	if dialDest != nil {
   504  		// retrieve pubkey. if err occurs, dialPubkey is automatically set as nil
   505  		dialPubkey, _ := dialDest.ID.Pubkey()
   506  		c.transport = srv.newTransport(fd, dialPubkey)
   507  		c.portOrder = PortOrder(dialDest.PortOrder)
   508  	} else {
   509  		c.transport = srv.newTransport(fd, nil)
   510  		for i, addr := range srv.ListenAddrs {
   511  			s1 := strings.Split(addr, ":")                    // string format example, [::]:30303 or 123.123.123.123:30303
   512  			s2 := strings.Split(fd.LocalAddr().String(), ":") // string format example, 123.123.123.123:30303
   513  			if s1[len(s1)-1] == s2[len(s2)-1] {
   514  				c.portOrder = PortOrder(i)
   515  				break
   516  			}
   517  		}
   518  	}
   519  
   520  	err := srv.setupConn(c, flags, dialDest)
   521  	if err != nil {
   522  		c.close(err)
   523  		srv.logger.Trace("close connection", "id", c.id, "err", err)
   524  	}
   525  	return err
   526  }
   527  
   528  // setupConn runs the handshakes and attempts to add the connection
   529  // as a peer. It returns when the connection has been added as a peer
   530  // or the handshakes have failed.
   531  func (srv *MultiChannelServer) setupConn(c *conn, flags connFlag, dialDest *discover.Node) error {
   532  	// Prevent leftover pending conns from entering the handshake.
   533  	srv.lock.Lock()
   534  	running := srv.running
   535  	srv.lock.Unlock()
   536  	if !running {
   537  		return errServerStopped
   538  	}
   539  
   540  	// Run the connection type handshake
   541  	var err error
   542  	if c.conntype, err = c.doConnTypeHandshake(srv.ConnectionType); err != nil {
   543  		srv.logger.Warn("Failed doConnTypeHandshake", "addr", c.fd.RemoteAddr(), "conn", c.flags,
   544  			"conntype", c.conntype, "err", err)
   545  		return err
   546  	}
   547  	srv.logger.Trace("Connection Type Trace", "addr", c.fd.RemoteAddr(), "conn", c.flags, "ConnType", c.conntype.String())
   548  
   549  	// Run the RLPx handshake.
   550  	remotePubkey, err := c.doEncHandshake(srv.PrivateKey)
   551  	if err != nil {
   552  		srv.logger.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
   553  		return err
   554  	}
   555  
   556  	c.id = discover.PubkeyID(remotePubkey)
   557  	clog := srv.logger.NewWith("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
   558  	// For dialed connections, check that the remote public key matches.
   559  	if dialDest != nil && c.id != dialDest.ID {
   560  		clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID)
   561  		return DiscUnexpectedIdentity
   562  	}
   563  	err = srv.checkpoint(c, srv.posthandshake)
   564  	if err != nil {
   565  		clog.Trace("Rejected peer before protocol handshake", "err", err)
   566  		return err
   567  	}
   568  	// Run the protocol handshake
   569  	phs, err := c.doProtoHandshake(srv.ourHandshake)
   570  	if err != nil {
   571  		clog.Trace("Failed protobuf handshake", "err", err)
   572  		return err
   573  	}
   574  	if phs.ID != c.id {
   575  		clog.Trace("Wrong devp2p handshake identity", "err", phs.ID)
   576  		return DiscUnexpectedIdentity
   577  	}
   578  	c.caps, c.name, c.multiChannel = phs.Caps, phs.Name, phs.Multichannel
   579  
   580  	if c.multiChannel && dialDest != nil && (dialDest.TCPs == nil || len(dialDest.TCPs) < 2) && len(dialDest.TCPs) < len(phs.ListenPort) {
   581  		logger.Debug("[Dial] update and retry the dial candidate as a multichannel",
   582  			"id", dialDest.ID, "addr", dialDest.IP, "previous", dialDest.TCPs, "new", phs.ListenPort)
   583  
   584  		dialDest.TCPs = make([]uint16, 0, len(phs.ListenPort))
   585  		for _, listenPort := range phs.ListenPort {
   586  			dialDest.TCPs = append(dialDest.TCPs, uint16(listenPort))
   587  		}
   588  		return errUpdateDial
   589  	}
   590  
   591  	err = srv.checkpoint(c, srv.addpeer)
   592  	if err != nil {
   593  		clog.Trace("Rejected peer", "err", err)
   594  		return err
   595  	}
   596  	// If the checks completed successfully, runPeer has now been
   597  	// launched by run.
   598  	clog.Trace("connection set up", "inbound", dialDest == nil)
   599  	return nil
   600  }
   601  
   602  // run is the main loop that the server runs.
   603  func (srv *MultiChannelServer) run(dialstate dialer) {
   604  	logger.Debug("[p2p.Server] start MultiChannel p2p server")
   605  	defer srv.loopWG.Done()
   606  	var (
   607  		peers         = make(map[discover.NodeID]*Peer)
   608  		inboundCount  = 0
   609  		outboundCount = 0
   610  		trusted       = make(map[discover.NodeID]bool, len(srv.TrustedNodes))
   611  		taskdone      = make(chan task, maxActiveDialTasks)
   612  		runningTasks  []task
   613  		queuedTasks   []task // tasks that can't run yet
   614  	)
   615  	// Put trusted nodes into a map to speed up checks.
   616  	// Trusted peers are loaded on startup and cannot be
   617  	// modified while the server is running.
   618  	for _, n := range srv.TrustedNodes {
   619  		trusted[n.ID] = true
   620  	}
   621  
   622  	// removes t from runningTasks
   623  	delTask := func(t task) {
   624  		for i := range runningTasks {
   625  			if runningTasks[i] == t {
   626  				runningTasks = append(runningTasks[:i], runningTasks[i+1:]...)
   627  				break
   628  			}
   629  		}
   630  	}
   631  	// starts until max number of active tasks is satisfied
   632  	startTasks := func(ts []task) (rest []task) {
   633  		i := 0
   634  		for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ {
   635  			t := ts[i]
   636  			srv.logger.Trace("New dial task", "task", t)
   637  			go func() { t.Do(srv); taskdone <- t }()
   638  			runningTasks = append(runningTasks, t)
   639  		}
   640  		return ts[i:]
   641  	}
   642  	scheduleTasks := func() {
   643  		// Start from queue first.
   644  		queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...)
   645  		// Query dialer for new tasks and start as many as possible now.
   646  		if len(runningTasks) < maxActiveDialTasks {
   647  			nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now())
   648  			queuedTasks = append(queuedTasks, startTasks(nt)...)
   649  		}
   650  	}
   651  
   652  running:
   653  	for {
   654  		scheduleTasks()
   655  
   656  		select {
   657  		case <-srv.quit:
   658  			// The server was stopped. Run the cleanup logic.
   659  			break running
   660  		case n := <-srv.addstatic:
   661  			// This channel is used by AddPeer to add to the
   662  			// ephemeral static peer list. Add it to the dialer,
   663  			// it will keep the node connected.
   664  			srv.logger.Debug("Adding static node", "node", n)
   665  			dialstate.addStatic(n)
   666  		case n := <-srv.removestatic:
   667  			// This channel is used by RemovePeer to send a
   668  			// disconnect request to a peer and begin the
   669  			// stop keeping the node connected
   670  			srv.logger.Debug("Removing static node", "node", n)
   671  			dialstate.removeStatic(n)
   672  			if p, ok := peers[n.ID]; ok {
   673  				p.Disconnect(DiscRequested)
   674  			}
   675  		case op := <-srv.peerOp:
   676  			// This channel is used by Peers and PeerCount.
   677  			op(peers)
   678  			srv.peerOpDone <- struct{}{}
   679  		case t := <-taskdone:
   680  			// A task got done. Tell dialstate about it so it
   681  			// can update its state and remove it from the active
   682  			// tasks list.
   683  			srv.logger.Trace("Dial task done", "task", t)
   684  			dialstate.taskDone(t, time.Now())
   685  			delTask(t)
   686  		case c := <-srv.posthandshake:
   687  			// A connection has passed the encryption handshake so
   688  			// the remote identity is known (but hasn't been verified yet).
   689  			if trusted[c.id] {
   690  				// Ensure that the trusted flag is set before checking against MaxPhysicalConnections.
   691  				c.flags |= trustedConn
   692  			}
   693  			// TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them.
   694  			select {
   695  			case c.cont <- srv.encHandshakeChecks(peers, inboundCount, c):
   696  			case <-srv.quit:
   697  				break running
   698  			}
   699  		case c := <-srv.addpeer:
   700  			var p *Peer
   701  			var e error
   702  			// At this point the connection is past the protocol handshake.
   703  			// Its capabilities are known and the remote identity is verified.
   704  			err := srv.protoHandshakeChecks(peers, inboundCount, c)
   705  			if err == nil {
   706  				if c.multiChannel {
   707  					connSet := srv.CandidateConns[c.id]
   708  					if connSet == nil {
   709  						connSet = make([]*conn, len(srv.ListenAddrs))
   710  						srv.CandidateConns[c.id] = connSet
   711  					}
   712  
   713  					if int(c.portOrder) < len(connSet) {
   714  						connSet[c.portOrder] = c
   715  					}
   716  
   717  					count := len(connSet)
   718  					for _, conn := range connSet {
   719  						if conn != nil {
   720  							count--
   721  						}
   722  					}
   723  
   724  					if count == 0 {
   725  						p, e = newPeer(connSet, srv.Protocols, srv.Config.RWTimerConfig)
   726  						srv.CandidateConns[c.id] = nil
   727  					}
   728  				} else {
   729  					// The handshakes are done and it passed all checks.
   730  					p, e = newPeer([]*conn{c}, srv.Protocols, srv.Config.RWTimerConfig)
   731  				}
   732  
   733  				if e != nil {
   734  					srv.logger.Error("Fail make a new peer", "err", e)
   735  				} else if p != nil {
   736  					// If message events are enabled, pass the peerFeed
   737  					// to the peer
   738  					if srv.EnableMsgEvents {
   739  						p.events = &srv.peerFeed
   740  					}
   741  					name := truncateName(c.name)
   742  					srv.logger.Debug("Adding p2p peer", "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1)
   743  					go srv.runPeer(p)
   744  					peers[c.id] = p
   745  
   746  					peerCountGauge.Update(int64(len(peers)))
   747  					inboundCount, outboundCount = increasesConnectionMetric(inboundCount, outboundCount, p)
   748  				}
   749  			}
   750  			// The dialer logic relies on the assumption that
   751  			// dial tasks complete after the peer has been added or
   752  			// discarded. Unblock the task last.
   753  			select {
   754  			case c.cont <- err:
   755  			case <-srv.quit:
   756  				break running
   757  			}
   758  		case pd := <-srv.delpeer:
   759  			// A peer disconnected.
   760  			d := common.PrettyDuration(mclock.Now() - pd.created)
   761  			pd.logger.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err)
   762  			delete(peers, pd.ID())
   763  
   764  			peerCountGauge.Update(int64(len(peers)))
   765  			inboundCount, outboundCount = decreasesConnectionMetric(inboundCount, outboundCount, pd.Peer)
   766  		case nid := <-srv.discpeer:
   767  			if p, ok := peers[nid]; ok {
   768  				p.Disconnect(DiscRequested)
   769  				p.logger.Debug(fmt.Sprintf("disconnect peer"))
   770  			}
   771  		}
   772  	}
   773  
   774  	srv.logger.Trace("P2P networking is spinning down")
   775  
   776  	// Terminate discovery. If there is a running lookup it will terminate soon.
   777  	if srv.ntab != nil {
   778  		srv.ntab.Close()
   779  	}
   780  	//if srv.DiscV5 != nil {
   781  	//	srv.DiscV5.Close()
   782  	//}
   783  	// Disconnect all peers.
   784  	for _, p := range peers {
   785  		p.Disconnect(DiscQuitting)
   786  	}
   787  	// Wait for peers to shut down. Pending connections and tasks are
   788  	// not handled here and will terminate soon-ish because srv.quit
   789  	// is closed.
   790  	for len(peers) > 0 {
   791  		p := <-srv.delpeer
   792  		p.logger.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks))
   793  		delete(peers, p.ID())
   794  	}
   795  }
   796  
   797  // Stop terminates the server and all active peer connections.
   798  // It blocks until all active connections are closed.
   799  func (srv *MultiChannelServer) Stop() {
   800  	srv.lock.Lock()
   801  	defer srv.lock.Unlock()
   802  	if !srv.running {
   803  		return
   804  	}
   805  	srv.running = false
   806  	if srv.listener != nil {
   807  		// this unblocks listener Accept
   808  		srv.listener.Close()
   809  	}
   810  	for _, listener := range srv.listeners {
   811  		listener.Close()
   812  	}
   813  	close(srv.quit)
   814  	srv.loopWG.Wait()
   815  }
   816  
   817  // GetListenAddress returns the listen addresses of the server.
   818  func (srv *MultiChannelServer) GetListenAddress() []string {
   819  	return srv.ListenAddrs
   820  }
   821  
   822  // decreasesConnectionMetric decreases the metric of the number of peer connections.
   823  func decreasesConnectionMetric(inboundCount int, outboundCount int, p *Peer) (int, int) {
   824  	pInbound, pOutbound := p.GetNumberInboundAndOutbound()
   825  	inboundCount -= pInbound
   826  	outboundCount -= pOutbound
   827  
   828  	updatesConnectionMetric(inboundCount, outboundCount)
   829  	return inboundCount, outboundCount
   830  }
   831  
   832  // increasesConnectionMetric increases the metric of the number of peer connections.
   833  func increasesConnectionMetric(inboundCount int, outboundCount int, p *Peer) (int, int) {
   834  	pInbound, pOutbound := p.GetNumberInboundAndOutbound()
   835  	inboundCount += pInbound
   836  	outboundCount += pOutbound
   837  
   838  	updatesConnectionMetric(inboundCount, outboundCount)
   839  	return inboundCount, outboundCount
   840  }
   841  
   842  // updatesConnectionMetric updates the metric of the number of peer connections.
   843  func updatesConnectionMetric(inboundCount int, outboundCount int) {
   844  	connectionCountGauge.Update(int64(outboundCount + inboundCount))
   845  	connectionInCountGauge.Update(int64(inboundCount))
   846  	connectionOutCountGauge.Update(int64(outboundCount))
   847  }
   848  
   849  // runPeer runs in its own goroutine for each peer.
   850  // it waits until the Peer logic returns and removes
   851  // the peer.
   852  func (srv *MultiChannelServer) runPeer(p *Peer) {
   853  	if srv.newPeerHook != nil {
   854  		srv.newPeerHook(p)
   855  	}
   856  
   857  	// broadcast peer add
   858  	srv.peerFeed.Send(&PeerEvent{
   859  		Type: PeerEventTypeAdd,
   860  		Peer: p.ID(),
   861  	})
   862  
   863  	// run the protocol
   864  	remoteRequested, err := p.runWithRWs()
   865  
   866  	// broadcast peer drop
   867  	srv.peerFeed.Send(&PeerEvent{
   868  		Type:  PeerEventTypeDrop,
   869  		Peer:  p.ID(),
   870  		Error: err.Error(),
   871  	})
   872  
   873  	// Note: run waits for existing peers to be sent on srv.delpeer
   874  	// before returning, so this send should not select on srv.quit.
   875  	srv.delpeer <- peerDrop{p, err, remoteRequested}
   876  }
   877  
   878  // SingleChannelServer is a server that uses a single channel.
   879  type SingleChannelServer struct {
   880  	*BaseServer
   881  }
   882  
   883  // AddLastLookup adds lastLookup to duration.
   884  func (srv *BaseServer) AddLastLookup() time.Time {
   885  	srv.lastLookupMu.Lock()
   886  	defer srv.lastLookupMu.Unlock()
   887  	return srv.lastLookup.Add(lookupInterval)
   888  }
   889  
   890  // SetLastLookupToNow sets LastLookup to the current time.
   891  func (srv *BaseServer) SetLastLookupToNow() {
   892  	srv.lastLookupMu.Lock()
   893  	defer srv.lastLookupMu.Unlock()
   894  	srv.lastLookup = time.Now()
   895  }
   896  
   897  // Dial creates a TCP connection to the node.
   898  func (srv *BaseServer) Dial(dest *discover.Node) (net.Conn, error) {
   899  	return srv.Dialer.Dial(dest)
   900  }
   901  
   902  // Dial creates a TCP connection to the node.
   903  func (srv *BaseServer) DialMulti(dest *discover.Node) ([]net.Conn, error) {
   904  	return srv.Dialer.DialMulti(dest)
   905  }
   906  
   907  // BaseServer is a common data structure used by implementation of Server.
   908  type BaseServer struct {
   909  	// Config fields may not be modified while the server is running.
   910  	Config
   911  
   912  	// Hooks for testing. These are useful because we can inhibit
   913  	// the whole protocol stack.
   914  	newTransport func(net.Conn, *ecdsa.PublicKey) transport
   915  	newPeerHook  func(*Peer)
   916  
   917  	lock    sync.Mutex // protects running
   918  	running bool
   919  
   920  	ntab         discover.Discovery
   921  	listener     net.Listener
   922  	ourHandshake *protoHandshake
   923  	lastLookup   time.Time
   924  	lastLookupMu sync.Mutex
   925  	// DiscV5       *discv5.Network
   926  
   927  	// These are for Peers, PeerCount (and nothing else).
   928  	peerOp     chan peerOpFunc
   929  	peerOpDone chan struct{}
   930  
   931  	quit          chan struct{}
   932  	addstatic     chan *discover.Node
   933  	removestatic  chan *discover.Node
   934  	posthandshake chan *conn
   935  	addpeer       chan *conn
   936  	delpeer       chan peerDrop
   937  	discpeer      chan discover.NodeID
   938  	loopWG        sync.WaitGroup // loop, listenLoop
   939  	peerFeed      event.Feed
   940  	logger        log.Logger
   941  }
   942  
   943  type peerOpFunc func(map[discover.NodeID]*Peer)
   944  
   945  type peerDrop struct {
   946  	*Peer
   947  	err       error
   948  	requested bool // true if signaled by the peer
   949  }
   950  
   951  type connFlag int
   952  
   953  const (
   954  	dynDialedConn connFlag = 1 << iota
   955  	staticDialedConn
   956  	inboundConn
   957  	trustedConn
   958  )
   959  
   960  type PortOrder int
   961  
   962  const (
   963  	PortOrderUndefined PortOrder = -1
   964  )
   965  
   966  // conn wraps a network connection with information gathered
   967  // during the two handshakes.
   968  type conn struct {
   969  	fd net.Conn
   970  	transport
   971  	flags        connFlag
   972  	conntype     common.ConnType // valid after the encryption handshake at the inbound connection case
   973  	cont         chan error      // The run loop uses cont to signal errors to SetupConn.
   974  	id           discover.NodeID // valid after the encryption handshake
   975  	caps         []Cap           // valid after the protocol handshake
   976  	name         string          // valid after the protocol handshake
   977  	portOrder    PortOrder       // portOrder is the order of the ports that should be connected in multi-channel.
   978  	multiChannel bool            // multiChannel is whether the peer is using multi-channel.
   979  }
   980  
   981  type transport interface {
   982  	doConnTypeHandshake(myConnType common.ConnType) (common.ConnType, error)
   983  	// The two handshakes.
   984  	doEncHandshake(prv *ecdsa.PrivateKey) (*ecdsa.PublicKey, error)
   985  	doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
   986  	// The MsgReadWriter can only be used after the encryption
   987  	// handshake has completed. The code uses conn.id to track this
   988  	// by setting it to a non-nil value after the encryption handshake.
   989  	MsgReadWriter
   990  	// transports must provide Close because we use MsgPipe in some of
   991  	// the tests. Closing the actual network connection doesn't do
   992  	// anything in those tests because NsgPipe doesn't use it.
   993  	close(err error)
   994  }
   995  
   996  func (c *conn) String() string {
   997  	s := c.flags.String()
   998  	s += " " + c.conntype.String()
   999  	if (c.id != discover.NodeID{}) {
  1000  		s += " " + c.id.String()
  1001  	}
  1002  	s += " " + c.fd.RemoteAddr().String()
  1003  	return s
  1004  }
  1005  
  1006  func (c *conn) Inbound() bool {
  1007  	return c.flags&inboundConn != 0
  1008  }
  1009  
  1010  func (f connFlag) String() string {
  1011  	s := ""
  1012  	if f&trustedConn != 0 {
  1013  		s += "-trusted"
  1014  	}
  1015  	if f&dynDialedConn != 0 {
  1016  		s += "-dyndial"
  1017  	}
  1018  	if f&staticDialedConn != 0 {
  1019  		s += "-staticdial"
  1020  	}
  1021  	if f&inboundConn != 0 {
  1022  		s += "-inbound"
  1023  	}
  1024  	if s != "" {
  1025  		s = s[1:]
  1026  	}
  1027  	return s
  1028  }
  1029  
  1030  func (c *conn) is(f connFlag) bool {
  1031  	return c.flags&f != 0
  1032  }
  1033  
  1034  // GetProtocols returns a slice of protocols.
  1035  func (srv *BaseServer) GetProtocols() []Protocol {
  1036  	return srv.Protocols
  1037  }
  1038  
  1039  // AddProtocols adds protocols to the server.
  1040  func (srv *BaseServer) AddProtocols(p []Protocol) {
  1041  	srv.Protocols = append(srv.Protocols, p...)
  1042  }
  1043  
  1044  // Peers returns all connected peers.
  1045  func (srv *BaseServer) Peers() []*Peer {
  1046  	var ps []*Peer
  1047  	select {
  1048  	// Note: We'd love to put this function into a variable but
  1049  	// that seems to cause a weird compiler error in some
  1050  	// environments.
  1051  	case srv.peerOp <- func(peers map[discover.NodeID]*Peer) {
  1052  		for _, p := range peers {
  1053  			ps = append(ps, p)
  1054  		}
  1055  	}:
  1056  		<-srv.peerOpDone
  1057  	case <-srv.quit:
  1058  	}
  1059  	return ps
  1060  }
  1061  
  1062  // PeerCount returns the number of connected peers.
  1063  func (srv *BaseServer) PeerCount() int {
  1064  	var count int
  1065  	select {
  1066  	case srv.peerOp <- func(ps map[discover.NodeID]*Peer) { count = len(ps) }:
  1067  		<-srv.peerOpDone
  1068  	case <-srv.quit:
  1069  	}
  1070  	return count
  1071  }
  1072  
  1073  func (srv *BaseServer) PeerCountByType() map[string]uint {
  1074  	pc := make(map[string]uint)
  1075  	pc["total"] = 0
  1076  	select {
  1077  	case srv.peerOp <- func(ps map[discover.NodeID]*Peer) {
  1078  		for _, peer := range ps {
  1079  			key := ConvertConnTypeToString(peer.ConnType())
  1080  			pc[key]++
  1081  			pc["total"]++
  1082  		}
  1083  	}:
  1084  		<-srv.peerOpDone
  1085  	case <-srv.quit:
  1086  	}
  1087  	return pc
  1088  }
  1089  
  1090  // AddPeer connects to the given node and maintains the connection until the
  1091  // server is shut down. If the connection fails for any reason, the server will
  1092  // attempt to reconnect the peer.
  1093  func (srv *BaseServer) AddPeer(node *discover.Node) {
  1094  	select {
  1095  	case srv.addstatic <- node:
  1096  	case <-srv.quit:
  1097  	}
  1098  }
  1099  
  1100  // RemovePeer disconnects from the given node.
  1101  func (srv *BaseServer) RemovePeer(node *discover.Node) {
  1102  	select {
  1103  	case srv.removestatic <- node:
  1104  	case <-srv.quit:
  1105  	}
  1106  }
  1107  
  1108  // SubscribePeers subscribes the given channel to peer events.
  1109  func (srv *BaseServer) SubscribeEvents(ch chan *PeerEvent) event.Subscription {
  1110  	return srv.peerFeed.Subscribe(ch)
  1111  }
  1112  
  1113  // Self returns the local node's endpoint information.
  1114  func (srv *BaseServer) Self() *discover.Node {
  1115  	srv.lock.Lock()
  1116  	defer srv.lock.Unlock()
  1117  
  1118  	if !srv.running {
  1119  		return &discover.Node{IP: net.ParseIP("0.0.0.0")}
  1120  	}
  1121  	return srv.makeSelf(srv.listener, srv.ntab)
  1122  }
  1123  
  1124  func (srv *BaseServer) makeSelf(listener net.Listener, discovery discover.Discovery) *discover.Node {
  1125  	// If the server's not running, return an empty node.
  1126  	// If the node is running but discovery is off, manually assemble the node infos.
  1127  	if discovery == nil {
  1128  		// Inbound connections disabled, use zero address.
  1129  		if listener == nil {
  1130  			return &discover.Node{IP: net.ParseIP("0.0.0.0"), ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)}
  1131  		}
  1132  		// Otherwise inject the listener address too
  1133  		addr := listener.Addr().(*net.TCPAddr)
  1134  		return &discover.Node{
  1135  			ID:  discover.PubkeyID(&srv.PrivateKey.PublicKey),
  1136  			IP:  addr.IP,
  1137  			TCP: uint16(addr.Port),
  1138  		}
  1139  	}
  1140  	// Otherwise return the discovery node.
  1141  	return discovery.Self()
  1142  }
  1143  
  1144  // Stop terminates the server and all active peer connections.
  1145  // It blocks until all active connections are closed.
  1146  func (srv *BaseServer) Stop() {
  1147  	srv.lock.Lock()
  1148  	defer srv.lock.Unlock()
  1149  	if !srv.running {
  1150  		return
  1151  	}
  1152  	srv.running = false
  1153  	if srv.listener != nil {
  1154  		// this unblocks listener Accept
  1155  		srv.listener.Close()
  1156  	}
  1157  	close(srv.quit)
  1158  	srv.loopWG.Wait()
  1159  }
  1160  
  1161  // GetListenAddress returns the listen address of the server.
  1162  func (srv *BaseServer) GetListenAddress() []string {
  1163  	return []string{srv.ListenAddr}
  1164  }
  1165  
  1166  // sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
  1167  // messages that were found unprocessable and sent to the unhandled channel by the primary listener.
  1168  type sharedUDPConn struct {
  1169  	*net.UDPConn
  1170  	unhandled chan discover.ReadPacket
  1171  }
  1172  
  1173  // ReadFromUDP implements discv5.conn
  1174  func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
  1175  	packet, ok := <-s.unhandled
  1176  	if !ok {
  1177  		return 0, nil, fmt.Errorf("Connection was closed")
  1178  	}
  1179  	l := len(packet.Data)
  1180  	if l > len(b) {
  1181  		l = len(b)
  1182  	}
  1183  	copy(b[:l], packet.Data[:l])
  1184  	return l, packet.Addr, nil
  1185  }
  1186  
  1187  // Close implements discv5.conn
  1188  func (s *sharedUDPConn) Close() error {
  1189  	return nil
  1190  }
  1191  
  1192  // Start starts running the server.
  1193  // Servers can not be re-used after stopping.
  1194  func (srv *BaseServer) Start() (err error) {
  1195  	srv.lock.Lock()
  1196  	defer srv.lock.Unlock()
  1197  	if srv.running {
  1198  		return errors.New("server already running")
  1199  	}
  1200  	srv.running = true
  1201  	srv.logger = srv.Config.Logger
  1202  	if srv.logger == nil {
  1203  		srv.logger = logger.NewWith()
  1204  	}
  1205  	srv.logger.Info("Starting P2P networking")
  1206  
  1207  	// static fields
  1208  	if srv.PrivateKey == nil {
  1209  		return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
  1210  	}
  1211  
  1212  	if !srv.ConnectionType.Valid() {
  1213  		return fmt.Errorf("Invalid connection type speficied")
  1214  	}
  1215  
  1216  	if srv.newTransport == nil {
  1217  		srv.newTransport = newRLPX
  1218  	}
  1219  	if srv.Dialer == nil {
  1220  		srv.Dialer = TCPDialer{&net.Dialer{Timeout: defaultDialTimeout}}
  1221  	}
  1222  	srv.quit = make(chan struct{})
  1223  	srv.addpeer = make(chan *conn)
  1224  	srv.delpeer = make(chan peerDrop)
  1225  	srv.posthandshake = make(chan *conn)
  1226  	srv.addstatic = make(chan *discover.Node)
  1227  	srv.removestatic = make(chan *discover.Node)
  1228  	srv.peerOp = make(chan peerOpFunc)
  1229  	srv.peerOpDone = make(chan struct{})
  1230  	srv.discpeer = make(chan discover.NodeID)
  1231  
  1232  	var (
  1233  		conn      *net.UDPConn
  1234  		realaddr  *net.UDPAddr
  1235  		unhandled chan discover.ReadPacket
  1236  	)
  1237  
  1238  	if !srv.NoDiscovery {
  1239  		addr, err := net.ResolveUDPAddr("udp", srv.ListenAddr)
  1240  		if err != nil {
  1241  			return err
  1242  		}
  1243  		conn, err = net.ListenUDP("udp", addr)
  1244  		if err != nil {
  1245  			return err
  1246  		}
  1247  		realaddr = conn.LocalAddr().(*net.UDPAddr)
  1248  		if srv.NAT != nil {
  1249  			if !realaddr.IP.IsLoopback() {
  1250  				go nat.Map(srv.NAT, srv.quit, "udp", realaddr.Port, realaddr.Port, "klaytn discovery")
  1251  			}
  1252  			// TODO: react to external IP changes over time.
  1253  			if ext, err := srv.NAT.ExternalIP(); err == nil {
  1254  				realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
  1255  			}
  1256  		}
  1257  	}
  1258  
  1259  	// node table
  1260  	if !srv.NoDiscovery {
  1261  		cfg := discover.Config{
  1262  			PrivateKey:   srv.PrivateKey,
  1263  			AnnounceAddr: realaddr,
  1264  			NodeDBPath:   srv.NodeDatabase,
  1265  			NetRestrict:  srv.NetRestrict,
  1266  			Bootnodes:    srv.BootstrapNodes,
  1267  			Unhandled:    unhandled,
  1268  			Conn:         conn,
  1269  			Addr:         realaddr,
  1270  			Id:           discover.PubkeyID(&srv.PrivateKey.PublicKey),
  1271  			NodeType:     ConvertNodeType(srv.ConnectionType),
  1272  			NetworkID:    srv.NetworkID,
  1273  		}
  1274  
  1275  		cfgForLog := cfg
  1276  		cfgForLog.PrivateKey = nil
  1277  
  1278  		logger.Info("Create udp", "config", cfgForLog)
  1279  
  1280  		ntab, err := discover.ListenUDP(&cfg)
  1281  		if err != nil {
  1282  			return err
  1283  		}
  1284  		srv.ntab = ntab
  1285  	}
  1286  
  1287  	dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, srv.maxDialedConns(), srv.NetRestrict, srv.PrivateKey, srv.getTypeStatics())
  1288  
  1289  	// handshake
  1290  	srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name(), ID: discover.PubkeyID(&srv.PrivateKey.PublicKey), Multichannel: false}
  1291  	for _, p := range srv.Protocols {
  1292  		srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
  1293  	}
  1294  	// listen/dial
  1295  	if srv.NoDial && srv.NoListen {
  1296  		srv.logger.Error("P2P server will be useless, neither dialing nor listening")
  1297  	}
  1298  	if !srv.NoListen {
  1299  		if srv.ListenAddr != "" {
  1300  			if err := srv.startListening(); err != nil {
  1301  				return err
  1302  			}
  1303  		} else {
  1304  			srv.logger.Error("P2P server might be useless, listening address is missing")
  1305  		}
  1306  	}
  1307  
  1308  	srv.loopWG.Add(1)
  1309  	go srv.run(dialer)
  1310  	srv.running = true
  1311  	srv.logger.Info("Started P2P server", "id", discover.PubkeyID(&srv.PrivateKey.PublicKey), "multichannel", false)
  1312  	return nil
  1313  }
  1314  
  1315  func (srv *BaseServer) startListening() error {
  1316  	// Launch the TCP listener.
  1317  	listener, err := net.Listen("tcp", srv.ListenAddr)
  1318  	if err != nil {
  1319  		return err
  1320  	}
  1321  	laddr := listener.Addr().(*net.TCPAddr)
  1322  	srv.ListenAddr = laddr.String()
  1323  	srv.listener = listener
  1324  	srv.loopWG.Add(1)
  1325  	go srv.listenLoop()
  1326  	// Map the TCP listening port if NAT is configured.
  1327  	if !laddr.IP.IsLoopback() && srv.NAT != nil {
  1328  		srv.loopWG.Add(1)
  1329  		go func() {
  1330  			nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "klaytn p2p")
  1331  			srv.loopWG.Done()
  1332  		}()
  1333  	}
  1334  	return nil
  1335  }
  1336  
  1337  type dialer interface {
  1338  	newTasks(running int, peers map[discover.NodeID]*Peer, now time.Time) []task
  1339  	taskDone(task, time.Time)
  1340  	addStatic(*discover.Node)
  1341  	removeStatic(*discover.Node)
  1342  }
  1343  
  1344  func (srv *BaseServer) run(dialstate dialer) {
  1345  	defer srv.loopWG.Done()
  1346  	var (
  1347  		peers        = make(map[discover.NodeID]*Peer)
  1348  		inboundCount = 0
  1349  		trusted      = make(map[discover.NodeID]bool, len(srv.TrustedNodes))
  1350  		taskdone     = make(chan task, maxActiveDialTasks)
  1351  		runningTasks []task
  1352  		queuedTasks  []task // tasks that can't run yet
  1353  	)
  1354  	// Put trusted nodes into a map to speed up checks.
  1355  	// Trusted peers are loaded on startup and cannot be
  1356  	// modified while the server is running.
  1357  	for _, n := range srv.TrustedNodes {
  1358  		trusted[n.ID] = true
  1359  	}
  1360  
  1361  	// removes t from runningTasks
  1362  	delTask := func(t task) {
  1363  		for i := range runningTasks {
  1364  			if runningTasks[i] == t {
  1365  				runningTasks = append(runningTasks[:i], runningTasks[i+1:]...)
  1366  				break
  1367  			}
  1368  		}
  1369  	}
  1370  	// starts until max number of active tasks is satisfied
  1371  	startTasks := func(ts []task) (rest []task) {
  1372  		i := 0
  1373  		for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ {
  1374  			t := ts[i]
  1375  			srv.logger.Trace("New dial task", "task", t)
  1376  			go func() { t.Do(srv); taskdone <- t }()
  1377  			runningTasks = append(runningTasks, t)
  1378  		}
  1379  		return ts[i:]
  1380  	}
  1381  	scheduleTasks := func() {
  1382  		// Start from queue first.
  1383  		queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...)
  1384  		// Query dialer for new tasks and start as many as possible now.
  1385  		if len(runningTasks) < maxActiveDialTasks {
  1386  			nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now())
  1387  			queuedTasks = append(queuedTasks, startTasks(nt)...)
  1388  		}
  1389  	}
  1390  
  1391  running:
  1392  	for {
  1393  		scheduleTasks()
  1394  
  1395  		select {
  1396  		case <-srv.quit:
  1397  			// The server was stopped. Run the cleanup logic.
  1398  			break running
  1399  		case n := <-srv.addstatic:
  1400  			// This channel is used by AddPeer to add to the
  1401  			// ephemeral static peer list. Add it to the dialer,
  1402  			// it will keep the node connected.
  1403  			srv.logger.Debug("Adding static node", "node", n)
  1404  			dialstate.addStatic(n)
  1405  		case n := <-srv.removestatic:
  1406  			// This channel is used by RemovePeer to send a
  1407  			// disconnect request to a peer and begin the
  1408  			// stop keeping the node connected
  1409  			srv.logger.Debug("Removing static node", "node", n)
  1410  			dialstate.removeStatic(n)
  1411  			if p, ok := peers[n.ID]; ok {
  1412  				p.Disconnect(DiscRequested)
  1413  			}
  1414  		case op := <-srv.peerOp:
  1415  			// This channel is used by Peers and PeerCount.
  1416  			op(peers)
  1417  			srv.peerOpDone <- struct{}{}
  1418  		case t := <-taskdone:
  1419  			// A task got done. Tell dialstate about it so it
  1420  			// can update its state and remove it from the active
  1421  			// tasks list.
  1422  			srv.logger.Trace("Dial task done", "task", t)
  1423  			dialstate.taskDone(t, time.Now())
  1424  			delTask(t)
  1425  		case c := <-srv.posthandshake:
  1426  			// A connection has passed the encryption handshake so
  1427  			// the remote identity is known (but hasn't been verified yet).
  1428  			if trusted[c.id] {
  1429  				// Ensure that the trusted flag is set before checking against MaxPhysicalConnections.
  1430  				c.flags |= trustedConn
  1431  			}
  1432  			// TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them.
  1433  			select {
  1434  			case c.cont <- srv.encHandshakeChecks(peers, inboundCount, c):
  1435  			case <-srv.quit:
  1436  				break running
  1437  			}
  1438  		case c := <-srv.addpeer:
  1439  			// At this point the connection is past the protocol handshake.
  1440  			// Its capabilities are known and the remote identity is verified.
  1441  			var err error
  1442  			err = srv.protoHandshakeChecks(peers, inboundCount, c)
  1443  			if err == nil {
  1444  				// The handshakes are done and it passed all checks.
  1445  				p, err := newPeer([]*conn{c}, srv.Protocols, srv.Config.RWTimerConfig)
  1446  				if err != nil {
  1447  					srv.logger.Error("Fail make a new peer", "err", err)
  1448  				} else {
  1449  					// If message events are enabled, pass the peerFeed
  1450  					// to the peer
  1451  					if srv.EnableMsgEvents {
  1452  						p.events = &srv.peerFeed
  1453  					}
  1454  					name := truncateName(c.name)
  1455  					srv.logger.Debug("Adding p2p peer", "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1)
  1456  					go srv.runPeer(p)
  1457  					peers[c.id] = p
  1458  
  1459  					if p.Inbound() {
  1460  						inboundCount++
  1461  					}
  1462  					peerCountGauge.Update(int64(len(peers)))
  1463  					peerInCountGauge.Update(int64(inboundCount))
  1464  					peerOutCountGauge.Update(int64(len(peers) - inboundCount))
  1465  				}
  1466  			}
  1467  			// The dialer logic relies on the assumption that
  1468  			// dial tasks complete after the peer has been added or
  1469  			// discarded. Unblock the task last.
  1470  			select {
  1471  			case c.cont <- err:
  1472  			case <-srv.quit:
  1473  				break running
  1474  			}
  1475  		case pd := <-srv.delpeer:
  1476  			// A peer disconnected.
  1477  			d := common.PrettyDuration(mclock.Now() - pd.created)
  1478  			pd.logger.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err)
  1479  			delete(peers, pd.ID())
  1480  
  1481  			if pd.Inbound() {
  1482  				inboundCount--
  1483  			}
  1484  
  1485  			peerCountGauge.Update(int64(len(peers)))
  1486  			peerInCountGauge.Update(int64(inboundCount))
  1487  			peerOutCountGauge.Update(int64(len(peers) - inboundCount))
  1488  		case nid := <-srv.discpeer:
  1489  			if p, ok := peers[nid]; ok {
  1490  				p.Disconnect(DiscRequested)
  1491  				p.logger.Debug(fmt.Sprintf("disconnect peer"))
  1492  			}
  1493  		}
  1494  	}
  1495  
  1496  	srv.logger.Trace("P2P networking is spinning down")
  1497  
  1498  	// Terminate discovery. If there is a running lookup it will terminate soon.
  1499  	if srv.ntab != nil {
  1500  		srv.ntab.Close()
  1501  	}
  1502  	//if srv.DiscV5 != nil {
  1503  	//	srv.DiscV5.Close()
  1504  	//}
  1505  	// Disconnect all peers.
  1506  	for _, p := range peers {
  1507  		p.Disconnect(DiscQuitting)
  1508  	}
  1509  	// Wait for peers to shut down. Pending connections and tasks are
  1510  	// not handled here and will terminate soon-ish because srv.quit
  1511  	// is closed.
  1512  	for len(peers) > 0 {
  1513  		p := <-srv.delpeer
  1514  		p.logger.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks))
  1515  		delete(peers, p.ID())
  1516  	}
  1517  }
  1518  
  1519  func (srv *BaseServer) protoHandshakeChecks(peers map[discover.NodeID]*Peer, inboundCount int, c *conn) error {
  1520  	// Drop connections with no matching protocols.
  1521  	if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, c.caps) == 0 {
  1522  		return DiscUselessPeer
  1523  	}
  1524  	// Repeat the encryption handshake checks because the
  1525  	// peer set might have changed between the handshakes.
  1526  	return srv.encHandshakeChecks(peers, inboundCount, c)
  1527  }
  1528  
  1529  func (srv *BaseServer) encHandshakeChecks(peers map[discover.NodeID]*Peer, inboundCount int, c *conn) error {
  1530  	switch {
  1531  	case !c.is(trustedConn|staticDialedConn) && len(peers) >= srv.Config.MaxPhysicalConnections:
  1532  		return DiscTooManyPeers
  1533  	case !c.is(trustedConn) && c.is(inboundConn) && inboundCount >= srv.maxInboundConns():
  1534  		return DiscTooManyPeers
  1535  	case peers[c.id] != nil:
  1536  		return DiscAlreadyConnected
  1537  	case c.id == srv.Self().ID:
  1538  		return DiscSelf
  1539  	default:
  1540  		return nil
  1541  	}
  1542  }
  1543  
  1544  func (srv *BaseServer) maxInboundConns() int {
  1545  	return srv.Config.MaxPhysicalConnections - srv.maxDialedConns()
  1546  }
  1547  
  1548  func (srv *BaseServer) maxDialedConns() int {
  1549  	switch srv.ConnectionType {
  1550  	case common.CONSENSUSNODE:
  1551  		return 0
  1552  	case common.PROXYNODE:
  1553  		return 0
  1554  	case common.ENDPOINTNODE:
  1555  		if srv.NoDiscovery || srv.NoDial {
  1556  			return 0
  1557  		}
  1558  		r := srv.DialRatio
  1559  		if r == 0 {
  1560  			r = defaultDialRatio
  1561  		}
  1562  		return srv.Config.MaxPhysicalConnections / r
  1563  	case common.BOOTNODE:
  1564  		return 0 // TODO check the bn for en
  1565  	default:
  1566  		logger.Crit("[p2p.Server] UnSupported Connection Type:", "ConnectionType", srv.ConnectionType)
  1567  		return 0
  1568  	}
  1569  }
  1570  
  1571  func (srv *BaseServer) getTypeStatics() map[dialType]typedStatic {
  1572  	switch srv.ConnectionType {
  1573  	case common.CONSENSUSNODE:
  1574  		tsMap := make(map[dialType]typedStatic)
  1575  		tsMap[DT_CN] = typedStatic{100, 3} // TODO-Klaytn-Node Change to literal to constant (maxNodeCount, MaxTry)
  1576  		return tsMap
  1577  	case common.PROXYNODE:
  1578  		tsMap := make(map[dialType]typedStatic)
  1579  		tsMap[DT_PN] = typedStatic{1, 3} // // TODO-Klaytn-Node Change to literal to constant (maxNodeCount, MaxTry)
  1580  		return tsMap
  1581  	case common.ENDPOINTNODE:
  1582  		tsMap := make(map[dialType]typedStatic)
  1583  		tsMap[DT_PN] = typedStatic{2, 3} // // TODO-Klaytn-Node Change to literal to constant (maxNodeCount, MaxTry)
  1584  		return tsMap
  1585  	case common.BOOTNODE:
  1586  		return nil
  1587  	default:
  1588  		logger.Crit("[p2p.Server] UnSupported Connection Type:", "ConnectionType", srv.ConnectionType)
  1589  		return nil
  1590  	}
  1591  }
  1592  
  1593  type tempError interface {
  1594  	Temporary() bool
  1595  }
  1596  
  1597  // listenLoop runs in its own goroutine and accepts
  1598  // inbound connections.
  1599  func (srv *BaseServer) listenLoop() {
  1600  	defer srv.loopWG.Done()
  1601  	srv.logger.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab))
  1602  
  1603  	tokens := defaultMaxPendingPeers
  1604  	if srv.MaxPendingPeers > 0 {
  1605  		tokens = srv.MaxPendingPeers
  1606  	}
  1607  	slots := make(chan struct{}, tokens)
  1608  	for i := 0; i < tokens; i++ {
  1609  		slots <- struct{}{}
  1610  	}
  1611  
  1612  	for {
  1613  		// Wait for a handshake slot before accepting.
  1614  		<-slots
  1615  
  1616  		var (
  1617  			fd  net.Conn
  1618  			err error
  1619  		)
  1620  		for {
  1621  			fd, err = srv.listener.Accept()
  1622  			if tempErr, ok := err.(tempError); ok && tempErr.Temporary() {
  1623  				srv.logger.Debug("Temporary read error", "err", err)
  1624  				continue
  1625  			} else if err != nil {
  1626  				srv.logger.Debug("Read error", "err", err)
  1627  				return
  1628  			}
  1629  			break
  1630  		}
  1631  
  1632  		// Reject connections that do not match NetRestrict.
  1633  		if srv.NetRestrict != nil {
  1634  			if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) {
  1635  				srv.logger.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr())
  1636  				fd.Close()
  1637  				slots <- struct{}{}
  1638  				continue
  1639  			}
  1640  		}
  1641  
  1642  		fd = newMeteredConn(fd, true)
  1643  		srv.logger.Trace("Accepted connection", "addr", fd.RemoteAddr())
  1644  		go func() {
  1645  			srv.SetupConn(fd, inboundConn, nil)
  1646  			slots <- struct{}{}
  1647  		}()
  1648  	}
  1649  }
  1650  
  1651  // SetupConn runs the handshakes and attempts to add the connection
  1652  // as a peer. It returns when the connection has been added as a peer
  1653  // or the handshakes have failed.
  1654  func (srv *BaseServer) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error {
  1655  	self := srv.Self()
  1656  	if self == nil {
  1657  		return errors.New("shutdown")
  1658  	}
  1659  
  1660  	c := &conn{fd: fd, flags: flags, conntype: common.ConnTypeUndefined, cont: make(chan error), portOrder: ConnDefault}
  1661  
  1662  	// if err occurs, dialPubkey is automatically set as nil
  1663  	if dialDest != nil {
  1664  		dialPubkey, _ := dialDest.ID.Pubkey()
  1665  		c.transport = srv.newTransport(fd, dialPubkey)
  1666  	} else {
  1667  		c.transport = srv.newTransport(fd, nil)
  1668  	}
  1669  
  1670  	err := srv.setupConn(c, flags, dialDest)
  1671  	if err != nil {
  1672  		c.close(err)
  1673  		srv.logger.Trace("Setting up connection failed", "id", c.id, "err", err)
  1674  	}
  1675  	return err
  1676  }
  1677  
  1678  func (srv *BaseServer) setupConn(c *conn, flags connFlag, dialDest *discover.Node) error {
  1679  	// Prevent leftover pending conns from entering the handshake.
  1680  	srv.lock.Lock()
  1681  	running := srv.running
  1682  	srv.lock.Unlock()
  1683  	if !running {
  1684  		return errServerStopped
  1685  	}
  1686  
  1687  	// Run the connection type handshake
  1688  	var err error
  1689  	if c.conntype, err = c.doConnTypeHandshake(srv.ConnectionType); err != nil {
  1690  		srv.logger.Warn("Failed doConnTypeHandshake", "addr", c.fd.RemoteAddr(), "conn", c.flags,
  1691  			"conntype", c.conntype, "err", err)
  1692  		return err
  1693  	}
  1694  	srv.logger.Trace("Connection Type Trace", "addr", c.fd.RemoteAddr(), "conn", c.flags, "ConnType", c.conntype.String())
  1695  
  1696  	// Run the encryption handshake.
  1697  	remotePubkey, err := c.doEncHandshake(srv.PrivateKey)
  1698  	if err != nil {
  1699  		srv.logger.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
  1700  		return err
  1701  	}
  1702  
  1703  	c.id = discover.PubkeyID(remotePubkey)
  1704  	clog := srv.logger.NewWith("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
  1705  	// For dialed connections, check that the remote public key matches.
  1706  	if dialDest != nil && c.id != dialDest.ID {
  1707  		clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID)
  1708  		return DiscUnexpectedIdentity
  1709  	}
  1710  	err = srv.checkpoint(c, srv.posthandshake)
  1711  	if err != nil {
  1712  		clog.Trace("Rejected peer before protocol handshake", "err", err)
  1713  		return err
  1714  	}
  1715  	// Run the protocol handshake
  1716  	phs, err := c.doProtoHandshake(srv.ourHandshake)
  1717  	if err != nil {
  1718  		clog.Trace("Failed protobuf handshake", "err", err)
  1719  		return err
  1720  	}
  1721  	if phs.ID != c.id {
  1722  		clog.Trace("Wrong devp2p handshake identity", "err", phs.ID)
  1723  		return DiscUnexpectedIdentity
  1724  	}
  1725  	c.caps, c.name, c.multiChannel = phs.Caps, phs.Name, phs.Multichannel
  1726  
  1727  	err = srv.checkpoint(c, srv.addpeer)
  1728  	if err != nil {
  1729  		clog.Trace("Rejected peer", "err", err)
  1730  		return err
  1731  	}
  1732  	// If the checks completed successfully, runPeer has now been
  1733  	// launched by run.
  1734  	clog.Trace("connection set up", "inbound", dialDest == nil)
  1735  	return nil
  1736  }
  1737  
  1738  func truncateName(s string) string {
  1739  	if len(s) > 20 {
  1740  		return s[:20] + "..."
  1741  	}
  1742  	return s
  1743  }
  1744  
  1745  // checkpoint sends the conn to run, which performs the
  1746  // post-handshake checks for the stage (posthandshake, addpeer).
  1747  func (srv *BaseServer) checkpoint(c *conn, stage chan<- *conn) error {
  1748  	select {
  1749  	case stage <- c:
  1750  	case <-srv.quit:
  1751  		return errServerStopped
  1752  	}
  1753  	select {
  1754  	case err := <-c.cont:
  1755  		return err
  1756  	case <-srv.quit:
  1757  		return errServerStopped
  1758  	}
  1759  }
  1760  
  1761  // runPeer runs in its own goroutine for each peer.
  1762  // it waits until the Peer logic returns and removes
  1763  // the peer.
  1764  func (srv *BaseServer) runPeer(p *Peer) {
  1765  	if srv.newPeerHook != nil {
  1766  		srv.newPeerHook(p)
  1767  	}
  1768  
  1769  	// broadcast peer add
  1770  	srv.peerFeed.Send(&PeerEvent{
  1771  		Type: PeerEventTypeAdd,
  1772  		Peer: p.ID(),
  1773  	})
  1774  
  1775  	// run the protocol
  1776  	remoteRequested, err := p.run()
  1777  
  1778  	// broadcast peer drop
  1779  	srv.peerFeed.Send(&PeerEvent{
  1780  		Type:  PeerEventTypeDrop,
  1781  		Peer:  p.ID(),
  1782  		Error: err.Error(),
  1783  	})
  1784  
  1785  	// Note: run waits for existing peers to be sent on srv.delpeer
  1786  	// before returning, so this send should not select on srv.quit.
  1787  	srv.delpeer <- peerDrop{p, err, remoteRequested}
  1788  }
  1789  
  1790  // NodeInfo represents a short summary of the information known about the host.
  1791  type NodeInfo struct {
  1792  	ID    string `json:"id"`   // Unique node identifier (also the encryption key)
  1793  	Name  string `json:"name"` // Name of the node, including client type, version, OS, custom data
  1794  	Enode string `json:"kni"`  // Enode URL for adding this peer from remote peers
  1795  	IP    string `json:"ip"`   // IP address of the node
  1796  	Ports struct {
  1797  		Discovery int `json:"discovery"` // UDP listening port for discovery protocol
  1798  		Listener  int `json:"listener"`  // TCP listening port for RLPx
  1799  	} `json:"ports"`
  1800  	ListenAddr string                 `json:"listenAddr"`
  1801  	Protocols  map[string]interface{} `json:"protocols"`
  1802  }
  1803  
  1804  // NodeInfo gathers and returns a collection of metadata known about the host.
  1805  func (srv *BaseServer) NodeInfo() *NodeInfo {
  1806  	node := srv.Self()
  1807  
  1808  	// Gather and assemble the generic node infos
  1809  	info := &NodeInfo{
  1810  		Name:       srv.Name(),
  1811  		Enode:      node.String(),
  1812  		ID:         node.ID.String(),
  1813  		IP:         node.IP.String(),
  1814  		ListenAddr: srv.ListenAddr,
  1815  		Protocols:  make(map[string]interface{}),
  1816  	}
  1817  	info.Ports.Discovery = int(node.UDP)
  1818  	info.Ports.Listener = int(node.TCP)
  1819  
  1820  	// Gather all the running protocol infos (only once per protocol type)
  1821  	for _, proto := range srv.Protocols {
  1822  		if _, ok := info.Protocols[proto.Name]; !ok {
  1823  			nodeInfo := interface{}("unknown")
  1824  			if query := proto.NodeInfo; query != nil {
  1825  				nodeInfo = proto.NodeInfo()
  1826  			}
  1827  			info.Protocols[proto.Name] = nodeInfo
  1828  		}
  1829  	}
  1830  	return info
  1831  }
  1832  
  1833  // PeersInfo returns an array of metadata objects describing connected peers.
  1834  func (srv *BaseServer) PeersInfo() []*PeerInfo {
  1835  	// Gather all the generic and sub-protocol specific infos
  1836  	infos := make([]*PeerInfo, 0, srv.PeerCount())
  1837  	for _, peer := range srv.Peers() {
  1838  		if peer != nil {
  1839  			infos = append(infos, peer.Info())
  1840  		}
  1841  	}
  1842  	// Sort the result array alphabetically by node identifier
  1843  	for i := 0; i < len(infos); i++ {
  1844  		for j := i + 1; j < len(infos); j++ {
  1845  			if infos[i].ID > infos[j].ID {
  1846  				infos[i], infos[j] = infos[j], infos[i]
  1847  			}
  1848  		}
  1849  	}
  1850  	return infos
  1851  }
  1852  
  1853  // Disconnect tries to disconnect peer.
  1854  func (srv *BaseServer) Disconnect(destID discover.NodeID) {
  1855  	srv.discpeer <- destID
  1856  }
  1857  
  1858  // CheckNilNetworkTable returns whether network table is nil.
  1859  func (srv *BaseServer) CheckNilNetworkTable() bool {
  1860  	return srv.ntab == nil
  1861  }
  1862  
  1863  // Lookup performs a network search for nodes close
  1864  // to the given target. It approaches the target by querying
  1865  // nodes that are closer to it on each iteration.
  1866  // The given target does not need to be an actual node
  1867  // identifier.
  1868  func (srv *BaseServer) Lookup(target discover.NodeID, nType discover.NodeType) []*discover.Node {
  1869  	return srv.ntab.Lookup(target, nType)
  1870  }
  1871  
  1872  // Resolve searches for a specific node with the given ID and NodeType.
  1873  // It returns nil if the node could not be found.
  1874  func (srv *BaseServer) Resolve(target discover.NodeID, nType discover.NodeType) *discover.Node {
  1875  	return srv.ntab.Resolve(target, nType)
  1876  }
  1877  
  1878  func (srv *BaseServer) GetNodes(nType discover.NodeType, max int) []*discover.Node {
  1879  	return srv.ntab.GetNodes(nType, max)
  1880  }
  1881  
  1882  // Name returns name of server.
  1883  func (srv *BaseServer) Name() string {
  1884  	return srv.Config.Name
  1885  }
  1886  
  1887  // MaxPhysicalConnections returns maximum count of peers.
  1888  func (srv *BaseServer) MaxPeers() int {
  1889  	return srv.Config.MaxPhysicalConnections
  1890  }
  1891  
  1892  func ConvertNodeType(ct common.ConnType) discover.NodeType {
  1893  	switch ct {
  1894  	case common.CONSENSUSNODE:
  1895  		return discover.NodeTypeCN
  1896  	case common.PROXYNODE:
  1897  		return discover.NodeTypePN
  1898  	case common.ENDPOINTNODE:
  1899  		return discover.NodeTypeEN
  1900  	case common.BOOTNODE:
  1901  		return discover.NodeTypeBN
  1902  	default:
  1903  		return discover.NodeTypeUnknown // TODO-Klaytn-Node Maybe, call panic() func or Crit()
  1904  	}
  1905  }
  1906  
  1907  func ConvertConnType(nt discover.NodeType) common.ConnType {
  1908  	switch nt {
  1909  	case discover.NodeTypeCN:
  1910  		return common.CONSENSUSNODE
  1911  	case discover.NodeTypePN:
  1912  		return common.PROXYNODE
  1913  	case discover.NodeTypeEN:
  1914  		return common.ENDPOINTNODE
  1915  	case discover.NodeTypeBN:
  1916  		return common.BOOTNODE
  1917  	default:
  1918  		return common.UNKNOWNNODE
  1919  	}
  1920  }
  1921  
  1922  func ConvertConnTypeToString(ct common.ConnType) string {
  1923  	switch ct {
  1924  	case common.CONSENSUSNODE:
  1925  		return "cn"
  1926  	case common.PROXYNODE:
  1927  		return "pn"
  1928  	case common.ENDPOINTNODE:
  1929  		return "en"
  1930  	case common.BOOTNODE:
  1931  		return "bn"
  1932  	default:
  1933  		return "unknown"
  1934  	}
  1935  }
  1936  
  1937  func ConvertStringToConnType(s string) common.ConnType {
  1938  	st := strings.ToLower(s)
  1939  	switch st {
  1940  	case "cn":
  1941  		return common.CONSENSUSNODE
  1942  	case "pn":
  1943  		return common.PROXYNODE
  1944  	case "en":
  1945  		return common.ENDPOINTNODE
  1946  	case "bn":
  1947  		return common.BOOTNODE
  1948  	default:
  1949  		return common.UNKNOWNNODE
  1950  	}
  1951  }