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