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