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