github.com/bcskill/bcschain/v3@v3.4.9-beta2/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 "crypto/ecdsa" 22 "errors" 23 "fmt" 24 "net" 25 "sync" 26 "time" 27 28 "github.com/bcskill/bcschain/v3/common" 29 "github.com/bcskill/bcschain/v3/common/mclock" 30 "github.com/bcskill/bcschain/v3/log" 31 "github.com/bcskill/bcschain/v3/p2p/discover" 32 "github.com/bcskill/bcschain/v3/p2p/discv5" 33 "github.com/bcskill/bcschain/v3/p2p/nat" 34 "github.com/bcskill/bcschain/v3/p2p/netutil" 35 ) 36 37 const ( 38 defaultDialTimeout = 15 * time.Second 39 40 // Connectivity defaults. 41 maxActiveDialTasks = 16 42 defaultMaxPendingPeers = 50 43 defaultDialRatio = 3 44 45 // Maximum time allowed for reading a complete message. 46 // This is effectively the amount of time a connection can be idle. 47 frameReadTimeout = 30 * time.Second 48 49 // Maximum amount of time allowed for writing a complete message. 50 frameWriteTimeout = 20 * time.Second 51 ) 52 53 var errServerStopped = errors.New("server stopped") 54 55 // Config holds Server options. 56 type Config struct { 57 // This field must be set to a valid secp256k1 private key. 58 PrivateKey *ecdsa.PrivateKey `toml:"-"` 59 60 // MaxPeers is the maximum number of peers that can be 61 // connected. It must be greater than zero. 62 MaxPeers int 63 64 // MaxPendingPeers is the maximum number of peers that can be pending in the 65 // handshake phase, counted separately for inbound and outbound connections. 66 // Zero defaults to preset values. 67 MaxPendingPeers int `toml:",omitempty"` 68 69 // DialRatio controls the ratio of inbound to dialed connections. 70 // Example: a DialRatio of 2 allows 1/2 of connections to be dialed. 71 // Setting DialRatio to zero defaults it to 3. 72 DialRatio int `toml:",omitempty"` 73 74 // NoDiscovery can be used to disable the peer discovery mechanism. 75 // Disabling is useful for protocol debugging (manual topology). 76 NoDiscovery bool `toml:",omitempty"` 77 78 // DiscoveryV5 specifies whether the the new topic-discovery based V5 discovery 79 // protocol should be started or not. 80 DiscoveryV5 bool `toml:",omitempty"` 81 82 // Name sets the node name of this server. 83 // Use common.MakeName to create a name that follows existing conventions. 84 Name string `toml:"-"` 85 86 // BootstrapNodes are used to establish connectivity 87 // with the rest of the network. 88 BootstrapNodes []*discover.Node `toml:",omitempty"` 89 90 // BootstrapNodesV5 are used to establish connectivity 91 // with the rest of the network using the V5 discovery 92 // protocol. 93 BootstrapNodesV5 []*discv5.Node `toml:",omitempty"` 94 95 // Static nodes are used as pre-configured connections which are always 96 // maintained and re-connected on disconnects. 97 StaticNodes []*discover.Node `toml:",omitempty"` 98 99 // Trusted nodes are used as pre-configured connections which are always 100 // allowed to connect, even above the peer limit. 101 TrustedNodes []*discover.Node `toml:",omitempty"` 102 103 // Connectivity can be restricted to certain IP networks. 104 // If this option is set to a non-nil value, only hosts which match one of the 105 // IP networks contained in the list are considered. 106 NetRestrict *netutil.Netlist `toml:",omitempty"` 107 108 // NodeDatabase is the path to the database containing the previously seen 109 // live nodes in the network. 110 NodeDatabase string `toml:",omitempty"` 111 112 // Protocols should contain the protocols supported 113 // by the server. Matching protocols are launched for 114 // each peer. 115 Protocols []Protocol `toml:"-"` 116 117 // If ListenAddr is set to a non-nil address, the server 118 // will listen for incoming connections. 119 // 120 // If the port is zero, the operating system will pick a port. The 121 // ListenAddr field will be updated with the actual address when 122 // the server is started. 123 ListenAddr string 124 125 // If set to a non-nil value, the given NAT port mapper 126 // is used to make the listening port available to the 127 // Internet. 128 NAT nat.Interface `toml:",omitempty"` 129 130 // If Dialer is set to a non-nil value, the given Dialer 131 // is used to dial outbound peer connections. 132 Dialer NodeDialer `toml:"-"` 133 134 // If NoDial is true, the server will not dial any peers. 135 NoDial bool `toml:",omitempty"` 136 137 // If EnableMsgEvents is set then the server will emit PeerEvents 138 // whenever a message is sent to or received from a peer 139 EnableMsgEvents bool 140 141 // Logger is a custom logger to use with the p2p.Server. 142 Logger log.Logger `toml:",omitempty"` 143 } 144 145 // Server manages all peer connections. 146 type Server struct { 147 // Config fields may not be modified while the server is running. 148 Config 149 150 // Hooks for testing. These are useful because we can inhibit 151 // the whole protocol stack. 152 newTransport func(net.Conn) transport 153 newPeerHook func(*Peer) 154 155 lock sync.Mutex // protects running 156 running bool 157 158 ntab discoverTable 159 listener net.Listener 160 ourHandshake *protoHandshake 161 lastLookup time.Time 162 DiscV5 *discv5.Network 163 164 // These are for Peers, PeerCount (and nothing else). 165 peerOp chan peerOpFunc 166 peerOpDone chan struct{} 167 168 quit chan struct{} 169 addstatic chan *discover.Node 170 removestatic chan *discover.Node 171 posthandshake chan *conn 172 addpeer chan *conn 173 delpeer chan peerDrop 174 loopWG sync.WaitGroup // loop, listenLoop 175 peerFeed PeerFeed 176 log log.Logger 177 } 178 179 type peerOpFunc func(map[discover.NodeID]*Peer) 180 181 type peerDrop struct { 182 *Peer 183 err error 184 requested bool // true if signaled by the peer 185 } 186 187 type connFlag int 188 189 const ( 190 dynDialedConn connFlag = 1 << iota 191 staticDialedConn 192 inboundConn 193 trustedConn 194 ) 195 196 // conn wraps a network connection with information gathered 197 // during the two handshakes. 198 type conn struct { 199 fd net.Conn 200 transport 201 flags connFlag 202 cont chan error // The run loop uses cont to signal errors to SetupConn. 203 id discover.NodeID // valid after the encryption handshake 204 caps []Cap // valid after the protocol handshake 205 name string // valid after the protocol handshake 206 } 207 208 type transport interface { 209 // The two handshakes. 210 doEncHandshake(prv *ecdsa.PrivateKey, dialDest *discover.Node) (discover.NodeID, error) 211 doProtoHandshake(our *protoHandshake) (*protoHandshake, error) 212 // The MsgReadWriter can only be used after the encryption 213 // handshake has completed. The code uses conn.id to track this 214 // by setting it to a non-nil value after the encryption handshake. 215 MsgReadWriter 216 // transports must provide Close because we use MsgPipe in some of 217 // the tests. Closing the actual network connection doesn't do 218 // anything in those tests because NsgPipe doesn't use it. 219 close(err error) 220 } 221 222 func (c *conn) String() string { 223 s := c.flags.String() 224 if (c.id != discover.NodeID{}) { 225 s += " " + c.id.String() 226 } 227 s += " " + c.fd.RemoteAddr().String() 228 return s 229 } 230 231 func (f connFlag) String() string { 232 s := "" 233 if f&trustedConn != 0 { 234 s += "-trusted" 235 } 236 if f&dynDialedConn != 0 { 237 s += "-dyndial" 238 } 239 if f&staticDialedConn != 0 { 240 s += "-staticdial" 241 } 242 if f&inboundConn != 0 { 243 s += "-inbound" 244 } 245 if s != "" { 246 s = s[1:] 247 } 248 return s 249 } 250 251 func (c *conn) is(f connFlag) bool { 252 return c.flags&f != 0 253 } 254 255 // Peers returns all connected peers. 256 func (srv *Server) Peers() []*Peer { 257 var ps []*Peer 258 select { 259 // Note: We'd love to put this function into a variable but 260 // that seems to cause a weird compiler error in some 261 // environments. 262 case srv.peerOp <- func(peers map[discover.NodeID]*Peer) { 263 for _, p := range peers { 264 ps = append(ps, p) 265 } 266 }: 267 <-srv.peerOpDone 268 case <-srv.quit: 269 } 270 return ps 271 } 272 273 // PeerCount returns the number of connected peers. 274 func (srv *Server) PeerCount() int { 275 var count int 276 select { 277 case srv.peerOp <- func(ps map[discover.NodeID]*Peer) { count = len(ps) }: 278 <-srv.peerOpDone 279 case <-srv.quit: 280 } 281 return count 282 } 283 284 // AddPeer connects to the given node and maintains the connection until the 285 // server is shut down. If the connection fails for any reason, the server will 286 // attempt to reconnect the peer. 287 func (srv *Server) AddPeer(node *discover.Node) { 288 select { 289 case srv.addstatic <- node: 290 case <-srv.quit: 291 } 292 } 293 294 // RemovePeer disconnects from the given node 295 func (srv *Server) RemovePeer(node *discover.Node) { 296 select { 297 case srv.removestatic <- node: 298 case <-srv.quit: 299 } 300 } 301 302 // SubscribePeers subscribes the given channel to peer events 303 func (srv *Server) SubscribeEvents(ch chan *PeerEvent, name string) { 304 srv.peerFeed.Subscribe(ch, name) 305 } 306 307 func (srv *Server) UnsubscribeEvents(ch chan *PeerEvent) { 308 srv.peerFeed.Unsubscribe(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 self := srv.Self() 805 if self == nil { 806 return errors.New("shutdown") 807 } 808 c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)} 809 err := srv.setupConn(c, flags, dialDest) 810 if err != nil { 811 c.close(err) 812 srv.log.Trace("Setting up connection failed", "id", c.id, "err", err) 813 } 814 return err 815 } 816 817 func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *discover.Node) error { 818 // Prevent leftover pending conns from entering the handshake. 819 srv.lock.Lock() 820 running := srv.running 821 srv.lock.Unlock() 822 if !running { 823 return errServerStopped 824 } 825 // Run the encryption handshake. 826 var err error 827 if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil { 828 srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) 829 return err 830 } 831 clog := srv.log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags) 832 // For dialed connections, check that the remote public key matches. 833 if dialDest != nil && c.id != dialDest.ID { 834 clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID) 835 return DiscUnexpectedIdentity 836 } 837 err = srv.checkpoint(c, srv.posthandshake) 838 if err != nil { 839 clog.Trace("Rejected peer before protocol handshake", "err", err) 840 return err 841 } 842 // Run the protocol handshake 843 phs, err := c.doProtoHandshake(srv.ourHandshake) 844 if err != nil { 845 clog.Trace("Failed proto handshake", "err", err) 846 return err 847 } 848 if phs.ID != c.id { 849 clog.Trace("Wrong devp2p handshake identity", "err", phs.ID) 850 return DiscUnexpectedIdentity 851 } 852 c.caps, c.name = phs.Caps, phs.Name 853 err = srv.checkpoint(c, srv.addpeer) 854 if err != nil { 855 clog.Trace("Rejected peer", "err", err) 856 return err 857 } 858 // If the checks completed successfully, runPeer has now been 859 // launched by run. 860 clog.Trace("connection set up", "inbound", dialDest == nil) 861 return nil 862 } 863 864 func truncateName(s string) string { 865 if len(s) > 20 { 866 return s[:20] + "..." 867 } 868 return s 869 } 870 871 // checkpoint sends the conn to run, which performs the 872 // post-handshake checks for the stage (posthandshake, addpeer). 873 func (srv *Server) checkpoint(c *conn, stage chan<- *conn) error { 874 select { 875 case stage <- c: 876 case <-srv.quit: 877 return errServerStopped 878 } 879 select { 880 case err := <-c.cont: 881 return err 882 case <-srv.quit: 883 return errServerStopped 884 } 885 } 886 887 // runPeer runs in its own goroutine for each peer. 888 // it waits until the Peer logic returns and removes 889 // the peer. 890 func (srv *Server) runPeer(p *Peer) { 891 if srv.newPeerHook != nil { 892 srv.newPeerHook(p) 893 } 894 895 // broadcast peer add 896 srv.peerFeed.Send(&PeerEvent{ 897 Type: PeerEventTypeAdd, 898 Peer: p.ID(), 899 }) 900 901 // run the protocol 902 remoteRequested, err := p.run() 903 904 // broadcast peer drop 905 srv.peerFeed.Send(&PeerEvent{ 906 Type: PeerEventTypeDrop, 907 Peer: p.ID(), 908 Error: err.Error(), 909 }) 910 911 // Note: run waits for existing peers to be sent on srv.delpeer 912 // before returning, so this send should not select on srv.quit. 913 srv.delpeer <- peerDrop{p, err, remoteRequested} 914 } 915 916 // NodeInfo represents a short summary of the information known about the host. 917 type NodeInfo struct { 918 ID string `json:"id"` // Unique node identifier (also the encryption key) 919 Name string `json:"name"` // Name of the node, including client type, version, OS, custom data 920 Enode string `json:"enode"` // Enode URL for adding this peer from remote peers 921 IP string `json:"ip"` // IP address of the node 922 Ports struct { 923 Discovery int `json:"discovery"` // UDP listening port for discovery protocol 924 Listener int `json:"listener"` // TCP listening port for RLPx 925 } `json:"ports"` 926 ListenAddr string `json:"listenAddr"` 927 Protocols map[string]interface{} `json:"protocols"` 928 } 929 930 // NodeInfo gathers and returns a collection of metadata known about the host. 931 func (srv *Server) NodeInfo() *NodeInfo { 932 node := srv.Self() 933 934 // Gather and assemble the generic node infos 935 info := &NodeInfo{ 936 Name: srv.Name, 937 Enode: node.String(), 938 ID: node.ID.String(), 939 IP: node.IP.String(), 940 ListenAddr: srv.ListenAddr, 941 Protocols: make(map[string]interface{}), 942 } 943 info.Ports.Discovery = int(node.UDP) 944 info.Ports.Listener = int(node.TCP) 945 946 // Gather all the running protocol infos (only once per protocol type) 947 for _, proto := range srv.Protocols { 948 if _, ok := info.Protocols[proto.Name]; !ok { 949 nodeInfo := interface{}("unknown") 950 if query := proto.NodeInfo; query != nil { 951 nodeInfo = proto.NodeInfo() 952 } 953 info.Protocols[proto.Name] = nodeInfo 954 } 955 } 956 return info 957 } 958 959 // PeersInfo returns an array of metadata objects describing connected peers. 960 func (srv *Server) PeersInfo() []*PeerInfo { 961 // Gather all the generic and sub-protocol specific infos 962 infos := make([]*PeerInfo, 0, srv.PeerCount()) 963 for _, peer := range srv.Peers() { 964 if peer != nil { 965 infos = append(infos, peer.Info()) 966 } 967 } 968 // Sort the result array alphabetically by node identifier 969 for i := 0; i < len(infos); i++ { 970 for j := i + 1; j < len(infos); j++ { 971 if infos[i].ID > infos[j].ID { 972 infos[i], infos[j] = infos[j], infos[i] 973 } 974 } 975 } 976 return infos 977 }