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