github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/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/insight-chain/inb-go/common" 32 "github.com/insight-chain/inb-go/common/mclock" 33 "github.com/insight-chain/inb-go/crypto" 34 "github.com/insight-chain/inb-go/event" 35 "github.com/insight-chain/inb-go/log" 36 "github.com/insight-chain/inb-go/p2p/discover" 37 "github.com/insight-chain/inb-go/p2p/discv5" 38 "github.com/insight-chain/inb-go/p2p/enode" 39 "github.com/insight-chain/inb-go/p2p/enr" 40 "github.com/insight-chain/inb-go/p2p/nat" 41 "github.com/insight-chain/inb-go/p2p/netutil" 42 "github.com/insight-chain/inb-go/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 var ( 609 peers = make(map[enode.ID]*Peer) 610 inboundCount = 0 611 trusted = make(map[enode.ID]bool, len(srv.TrustedNodes)) 612 taskdone = make(chan task, maxActiveDialTasks) 613 runningTasks []task 614 queuedTasks []task // tasks that can't run yet 615 ) 616 // Put trusted nodes into a map to speed up checks. 617 // Trusted peers are loaded on startup or added via AddTrustedPeer RPC. 618 for _, n := range srv.TrustedNodes { 619 trusted[n.ID()] = true 620 } 621 622 // removes t from runningTasks 623 delTask := func(t task) { 624 for i := range runningTasks { 625 if runningTasks[i] == t { 626 runningTasks = append(runningTasks[:i], runningTasks[i+1:]...) 627 break 628 } 629 } 630 } 631 // starts until max number of active tasks is satisfied 632 startTasks := func(ts []task) (rest []task) { 633 i := 0 634 for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ { 635 t := ts[i] 636 srv.log.Trace("New dial task", "task", t) 637 go func() { t.Do(srv); taskdone <- t }() 638 runningTasks = append(runningTasks, t) 639 } 640 return ts[i:] 641 } 642 scheduleTasks := func() { 643 // Start from queue first. 644 queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...) 645 // Query dialer for new tasks and start as many as possible now. 646 if len(runningTasks) < maxActiveDialTasks { 647 nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now()) 648 queuedTasks = append(queuedTasks, startTasks(nt)...) 649 } 650 } 651 652 running: 653 for { 654 scheduleTasks() 655 656 select { 657 case <-srv.quit: 658 // The server was stopped. Run the cleanup logic. 659 break running 660 case n := <-srv.addstatic: 661 // This channel is used by AddPeer to add to the 662 // ephemeral static peer list. Add it to the dialer, 663 // it will keep the node connected. 664 srv.log.Trace("Adding static node", "node", n) 665 dialstate.addStatic(n) 666 case n := <-srv.removestatic: 667 // This channel is used by RemovePeer to send a 668 // disconnect request to a peer and begin the 669 // stop keeping the node connected. 670 srv.log.Trace("Removing static node", "node", n) 671 dialstate.removeStatic(n) 672 if p, ok := peers[n.ID()]; ok { 673 p.Disconnect(DiscRequested) 674 } 675 case n := <-srv.addtrusted: 676 // This channel is used by AddTrustedPeer to add an enode 677 // to the trusted node set. 678 srv.log.Trace("Adding trusted node", "node", n) 679 trusted[n.ID()] = true 680 // Mark any already-connected peer as trusted 681 if p, ok := peers[n.ID()]; ok { 682 p.rw.set(trustedConn, true) 683 } 684 case n := <-srv.removetrusted: 685 // This channel is used by RemoveTrustedPeer to remove an enode 686 // from the trusted node set. 687 srv.log.Trace("Removing trusted node", "node", n) 688 if _, ok := trusted[n.ID()]; ok { 689 delete(trusted, n.ID()) 690 } 691 // Unmark any already-connected peer as trusted 692 if p, ok := peers[n.ID()]; ok { 693 p.rw.set(trustedConn, false) 694 } 695 case op := <-srv.peerOp: 696 // This channel is used by Peers and PeerCount. 697 op(peers) 698 srv.peerOpDone <- struct{}{} 699 case t := <-taskdone: 700 // A task got done. Tell dialstate about it so it 701 // can update its state and remove it from the active 702 // tasks list. 703 srv.log.Trace("Dial task done", "task", t) 704 dialstate.taskDone(t, time.Now()) 705 delTask(t) 706 case c := <-srv.posthandshake: 707 // A connection has passed the encryption handshake so 708 // the remote identity is known (but hasn't been verified yet). 709 if trusted[c.node.ID()] { 710 // Ensure that the trusted flag is set before checking against MaxPeers. 711 c.flags |= trustedConn 712 } 713 // TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them. 714 select { 715 case c.cont <- srv.encHandshakeChecks(peers, inboundCount, c): 716 case <-srv.quit: 717 break running 718 } 719 case c := <-srv.addpeer: 720 // At this point the connection is past the protocol handshake. 721 // Its capabilities are known and the remote identity is verified. 722 err := srv.protoHandshakeChecks(peers, inboundCount, c) 723 if err == nil { 724 // The handshakes are done and it passed all checks. 725 p := newPeer(c, srv.Protocols) 726 // If message events are enabled, pass the peerFeed 727 // to the peer 728 if srv.EnableMsgEvents { 729 p.events = &srv.peerFeed 730 } 731 name := truncateName(c.name) 732 srv.log.Debug("Adding p2p peer", "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1) 733 go srv.runPeer(p) 734 peers[c.node.ID()] = p 735 if p.Inbound() { 736 inboundCount++ 737 } 738 } 739 // The dialer logic relies on the assumption that 740 // dial tasks complete after the peer has been added or 741 // discarded. Unblock the task last. 742 select { 743 case c.cont <- err: 744 case <-srv.quit: 745 break running 746 } 747 case pd := <-srv.delpeer: 748 // A peer disconnected. 749 d := common.PrettyDuration(mclock.Now() - pd.created) 750 pd.log.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err) 751 delete(peers, pd.ID()) 752 if pd.Inbound() { 753 inboundCount-- 754 } 755 } 756 } 757 758 srv.log.Trace("P2P networking is spinning down") 759 760 // Terminate discovery. If there is a running lookup it will terminate soon. 761 if srv.ntab != nil { 762 srv.ntab.Close() 763 } 764 if srv.DiscV5 != nil { 765 srv.DiscV5.Close() 766 } 767 // Disconnect all peers. 768 for _, p := range peers { 769 p.Disconnect(DiscQuitting) 770 } 771 // Wait for peers to shut down. Pending connections and tasks are 772 // not handled here and will terminate soon-ish because srv.quit 773 // is closed. 774 for len(peers) > 0 { 775 p := <-srv.delpeer 776 p.log.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks)) 777 delete(peers, p.ID()) 778 } 779 } 780 781 func (srv *Server) protoHandshakeChecks(peers map[enode.ID]*Peer, inboundCount int, c *conn) error { 782 // Drop connections with no matching protocols. 783 if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, c.caps) == 0 { 784 return DiscUselessPeer 785 } 786 // Repeat the encryption handshake checks because the 787 // peer set might have changed between the handshakes. 788 return srv.encHandshakeChecks(peers, inboundCount, c) 789 } 790 791 func (srv *Server) encHandshakeChecks(peers map[enode.ID]*Peer, inboundCount int, c *conn) error { 792 switch { 793 case !c.is(trustedConn|staticDialedConn) && len(peers) >= srv.MaxPeers: 794 return DiscTooManyPeers 795 case !c.is(trustedConn) && c.is(inboundConn) && inboundCount >= srv.maxInboundConns(): 796 return DiscTooManyPeers 797 case peers[c.node.ID()] != nil: 798 return DiscAlreadyConnected 799 case c.node.ID() == srv.localnode.ID(): 800 return DiscSelf 801 default: 802 return nil 803 } 804 } 805 806 func (srv *Server) maxInboundConns() int { 807 return srv.MaxPeers - srv.maxDialedConns() 808 } 809 func (srv *Server) maxDialedConns() int { 810 if srv.NoDiscovery || srv.NoDial { 811 return 0 812 } 813 r := srv.DialRatio 814 if r == 0 { 815 r = defaultDialRatio 816 } 817 return srv.MaxPeers / r 818 } 819 820 // listenLoop runs in its own goroutine and accepts 821 // inbound connections. 822 func (srv *Server) listenLoop() { 823 defer srv.loopWG.Done() 824 srv.log.Debug("TCP listener up", "addr", srv.listener.Addr()) 825 826 tokens := defaultMaxPendingPeers 827 if srv.MaxPendingPeers > 0 { 828 tokens = srv.MaxPendingPeers 829 } 830 slots := make(chan struct{}, tokens) 831 for i := 0; i < tokens; i++ { 832 slots <- struct{}{} 833 } 834 835 for { 836 // Wait for a handshake slot before accepting. 837 <-slots 838 839 var ( 840 fd net.Conn 841 err error 842 ) 843 for { 844 fd, err = srv.listener.Accept() 845 if netutil.IsTemporaryError(err) { 846 srv.log.Debug("Temporary read error", "err", err) 847 continue 848 } else if err != nil { 849 srv.log.Debug("Read error", "err", err) 850 return 851 } 852 break 853 } 854 855 // Reject connections that do not match NetRestrict. 856 if srv.NetRestrict != nil { 857 if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) { 858 srv.log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr()) 859 fd.Close() 860 slots <- struct{}{} 861 continue 862 } 863 } 864 865 var ip net.IP 866 if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok { 867 ip = tcp.IP 868 } 869 fd = newMeteredConn(fd, true, ip) 870 srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr()) 871 go func() { 872 srv.SetupConn(fd, inboundConn, nil) 873 slots <- struct{}{} 874 }() 875 } 876 } 877 878 // SetupConn runs the handshakes and attempts to add the connection 879 // as a peer. It returns when the connection has been added as a peer 880 // or the handshakes have failed. 881 func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error { 882 c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)} 883 err := srv.setupConn(c, flags, dialDest) 884 if err != nil { 885 c.close(err) 886 srv.log.Trace("Setting up connection failed", "addr", fd.RemoteAddr(), "err", err) 887 } 888 return err 889 } 890 891 func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) error { 892 // Prevent leftover pending conns from entering the handshake. 893 srv.lock.Lock() 894 running := srv.running 895 srv.lock.Unlock() 896 if !running { 897 return errServerStopped 898 } 899 // If dialing, figure out the remote public key. 900 var dialPubkey *ecdsa.PublicKey 901 if dialDest != nil { 902 dialPubkey = new(ecdsa.PublicKey) 903 if err := dialDest.Load((*enode.Secp256k1)(dialPubkey)); err != nil { 904 return errors.New("dial destination doesn't have a secp256k1 public key") 905 } 906 } 907 // Run the encryption handshake. 908 remotePubkey, err := c.doEncHandshake(srv.PrivateKey, dialPubkey) 909 if err != nil { 910 srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) 911 return err 912 } 913 if dialDest != nil { 914 // For dialed connections, check that the remote public key matches. 915 if dialPubkey.X.Cmp(remotePubkey.X) != 0 || dialPubkey.Y.Cmp(remotePubkey.Y) != 0 { 916 return DiscUnexpectedIdentity 917 } 918 c.node = dialDest 919 } else { 920 c.node = nodeFromConn(remotePubkey, c.fd) 921 } 922 if conn, ok := c.fd.(*meteredConn); ok { 923 conn.handshakeDone(c.node.ID()) 924 } 925 clog := srv.log.New("id", c.node.ID(), "addr", c.fd.RemoteAddr(), "conn", c.flags) 926 err = srv.checkpoint(c, srv.posthandshake) 927 if err != nil { 928 clog.Trace("Rejected peer before protocol handshake", "err", err) 929 return err 930 } 931 // Run the protocol handshake 932 phs, err := c.doProtoHandshake(srv.ourHandshake) 933 if err != nil { 934 clog.Trace("Failed proto handshake", "err", err) 935 return err 936 } 937 if id := c.node.ID(); !bytes.Equal(crypto.Keccak256(phs.ID), id[:]) { 938 clog.Trace("Wrong devp2p handshake identity", "phsid", hex.EncodeToString(phs.ID)) 939 return DiscUnexpectedIdentity 940 } 941 c.caps, c.name = phs.Caps, phs.Name 942 err = srv.checkpoint(c, srv.addpeer) 943 if err != nil { 944 clog.Trace("Rejected peer", "err", err) 945 return err 946 } 947 // If the checks completed successfully, runPeer has now been 948 // launched by run. 949 clog.Trace("connection set up", "inbound", dialDest == nil) 950 return nil 951 } 952 953 func nodeFromConn(pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node { 954 var ip net.IP 955 var port int 956 if tcp, ok := conn.RemoteAddr().(*net.TCPAddr); ok { 957 ip = tcp.IP 958 port = tcp.Port 959 } 960 return enode.NewV4(pubkey, ip, port, port) 961 } 962 963 func truncateName(s string) string { 964 if len(s) > 20 { 965 return s[:20] + "..." 966 } 967 return s 968 } 969 970 // checkpoint sends the conn to run, which performs the 971 // post-handshake checks for the stage (posthandshake, addpeer). 972 func (srv *Server) checkpoint(c *conn, stage chan<- *conn) error { 973 select { 974 case stage <- c: 975 case <-srv.quit: 976 return errServerStopped 977 } 978 select { 979 case err := <-c.cont: 980 return err 981 case <-srv.quit: 982 return errServerStopped 983 } 984 } 985 986 // runPeer runs in its own goroutine for each peer. 987 // it waits until the Peer logic returns and removes 988 // the peer. 989 func (srv *Server) runPeer(p *Peer) { 990 if srv.newPeerHook != nil { 991 srv.newPeerHook(p) 992 } 993 994 // broadcast peer add 995 srv.peerFeed.Send(&PeerEvent{ 996 Type: PeerEventTypeAdd, 997 Peer: p.ID(), 998 }) 999 1000 // run the protocol 1001 remoteRequested, err := p.run() 1002 1003 // broadcast peer drop 1004 srv.peerFeed.Send(&PeerEvent{ 1005 Type: PeerEventTypeDrop, 1006 Peer: p.ID(), 1007 Error: err.Error(), 1008 }) 1009 1010 // Note: run waits for existing peers to be sent on srv.delpeer 1011 // before returning, so this send should not select on srv.quit. 1012 srv.delpeer <- peerDrop{p, err, remoteRequested} 1013 } 1014 1015 // NodeInfo represents a short summary of the information known about the host. 1016 type NodeInfo struct { 1017 ID string `json:"id"` // Unique node identifier (also the encryption key) 1018 Name string `json:"name"` // Name of the node, including client type, version, OS, custom data 1019 Enode string `json:"enode"` // Enode URL for adding this peer from remote peers 1020 ENR string `json:"enr"` // Ethereum Node Record 1021 IP string `json:"ip"` // IP address of the node 1022 Ports struct { 1023 Discovery int `json:"discovery"` // UDP listening port for discovery protocol 1024 Listener int `json:"listener"` // TCP listening port for RLPx 1025 } `json:"ports"` 1026 ListenAddr string `json:"listenAddr"` 1027 Protocols map[string]interface{} `json:"protocols"` 1028 } 1029 1030 // NodeInfo gathers and returns a collection of metadata known about the host. 1031 func (srv *Server) NodeInfo() *NodeInfo { 1032 // Gather and assemble the generic node infos 1033 node := srv.Self() 1034 info := &NodeInfo{ 1035 Name: srv.Name, 1036 Enode: node.String(), 1037 ID: node.ID().String(), 1038 IP: node.IP().String(), 1039 ListenAddr: srv.ListenAddr, 1040 Protocols: make(map[string]interface{}), 1041 } 1042 info.Ports.Discovery = node.UDP() 1043 info.Ports.Listener = node.TCP() 1044 if enc, err := rlp.EncodeToBytes(node.Record()); err == nil { 1045 info.ENR = "0x" + hex.EncodeToString(enc) 1046 } 1047 1048 // Gather all the running protocol infos (only once per protocol type) 1049 for _, proto := range srv.Protocols { 1050 if _, ok := info.Protocols[proto.Name]; !ok { 1051 nodeInfo := interface{}("unknown") 1052 if query := proto.NodeInfo; query != nil { 1053 nodeInfo = proto.NodeInfo() 1054 } 1055 info.Protocols[proto.Name] = nodeInfo 1056 } 1057 } 1058 return info 1059 } 1060 1061 // PeersInfo returns an array of metadata objects describing connected peers. 1062 func (srv *Server) PeersInfo() []*PeerInfo { 1063 // Gather all the generic and sub-protocol specific infos 1064 infos := make([]*PeerInfo, 0, srv.PeerCount()) 1065 for _, peer := range srv.Peers() { 1066 if peer != nil { 1067 infos = append(infos, peer.Info()) 1068 } 1069 } 1070 // Sort the result array alphabetically by node identifier 1071 for i := 0; i < len(infos); i++ { 1072 for j := i + 1; j < len(infos); j++ { 1073 if infos[i].ID > infos[j].ID { 1074 infos[i], infos[j] = infos[j], infos[i] 1075 } 1076 } 1077 } 1078 return infos 1079 }