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