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