github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/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 "strings" 26 "sync" 27 "time" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/common/mclock" 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:"-"` // private key 저장하는 변수 서버쪽 63 PublicKey *ecdsa.PublicKey //yichoi added for sending public key to store Qmanager 64 // 개인키 생성후 공개키를 임시로 저장하는 변수 65 66 // MaxPeers is the maximum number of peers that can be 67 // connected. It must be greater than zero. 68 MaxPeers int 69 70 // MaxPendingPeers is the maximum number of peers that can be pending in the 71 // handshake phase, counted separately for inbound and outbound connections. 72 // Zero defaults to preset values. 73 MaxPendingPeers int `toml:",omitempty"` 74 75 // NoDiscovery can be used to disable the peer discovery mechanism. 76 // Disabling is useful for protocol debugging (manual topology). 77 NoDiscovery bool 78 79 // DiscoveryV5 specifies whether the the new topic-discovery based V5 discovery 80 // protocol should be started or not. 81 DiscoveryV5 bool `toml:",omitempty"` 82 83 // Listener address for the V5 discovery protocol UDP traffic. 84 DiscoveryV5Addr string `toml:",omitempty"` 85 86 // Name sets the node name of this server. 87 // Use common.MakeName to create a name that follows existing conventions. 88 Name string `toml:"-"` 89 90 // BootstrapNodes are used to establish connectivity 91 // with the rest of the network. 92 BootstrapNodes []*discover.Node 93 94 // BootstrapNodesV5 are used to establish connectivity 95 // with the rest of the network using the V5 discovery 96 // protocol. 97 BootstrapNodesV5 []*discv5.Node `toml:",omitempty"` 98 99 // Static nodes are used as pre-configured connections which are always 100 // maintained and re-connected on disconnects. 101 StaticNodes []*discover.Node 102 103 // Qmanager nodes are used as pre-configured connections which are always 104 // maintained and re-connected on disconnects. 105 //QmanagerNodes []*discover.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 []*discover.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 //ListenIP string 132 ListenAddr string 133 //ListenLocalAddr string //local ip for reapchain 134 135 Bootnodeport int 136 // If set to a non-nil value, the given NAT port mapper 137 // is used to make the listening port available to the 138 // Internet. 139 NAT nat.Interface `toml:",omitempty"` 140 141 // If Dialer is set to a non-nil value, the given Dialer 142 // is used to dial outbound peer connections. 143 Dialer *net.Dialer `toml:"-"` 144 145 // If NoDial is true, the server will not dial any peers. 146 NoDial bool `toml:",omitempty"` 147 } 148 149 // Server manages all peer connections. 150 type Server struct { 151 // Config fields may not be modified while the server is running. 152 Config 153 154 // Hooks for testing. These are useful because we can inhibit 155 // the whole protocol stack. 156 newTransport func(net.Conn) transport 157 newPeerHook func(*Peer) 158 159 lock sync.Mutex // protects running 160 running bool 161 162 ntab discoverTable 163 listener net.Listener 164 ourHandshake *protoHandshake 165 lastLookup time.Time 166 DiscV5 *discv5.Network 167 168 // These are for Peers, PeerCount (and nothing else). 169 peerOp chan peerOpFunc 170 peerOpDone chan struct{} 171 172 quit chan struct{} 173 addstatic chan *discover.Node 174 removestatic chan *discover.Node 175 posthandshake chan *conn 176 addpeer chan *conn 177 delpeer chan peerDrop 178 loopWG sync.WaitGroup // loop, listenLoop 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 // Self returns the local node's endpoint information. 305 func (srv *Server) Self() *discover.Node { 306 srv.lock.Lock() 307 defer srv.lock.Unlock() 308 309 if !srv.running { 310 return &discover.Node{IP: net.ParseIP("0.0.0.0")} 311 } 312 return srv.makeSelf(srv.listener, srv.ntab) 313 } 314 315 func (srv *Server) makeSelf(listener net.Listener, ntab discoverTable) *discover.Node { 316 // If the server's not running, return an empty node. 317 // If the node is running but discovery is off, manually assemble the node infos. 318 if ntab == nil { 319 // Inbound connections disabled, use zero address. 320 if listener == nil { 321 return &discover.Node{IP: net.ParseIP("0.0.0.0"), ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)} 322 } 323 /* else { 324 325 return &discover.Node{IP: net.ParseIP("192.168.0.2"), ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)} 326 } */ 327 // Otherwise inject the listener address too 328 addr := listener.Addr().(*net.TCPAddr) 329 return &discover.Node{ 330 ID: discover.PubkeyID(&srv.PrivateKey.PublicKey), 331 IP: addr.IP, 332 TCP: uint16(addr.Port), 333 } 334 } 335 // Otherwise return the discovery node. 336 return ntab.Self() 337 } 338 339 // Stop terminates the server and all active peer connections. 340 // It blocks until all active connections have been closed. 341 func (srv *Server) Stop() { 342 srv.lock.Lock() 343 defer srv.lock.Unlock() 344 if !srv.running { 345 return 346 } 347 srv.running = false 348 if srv.listener != nil { 349 // this unblocks listener Accept 350 srv.listener.Close() 351 } 352 close(srv.quit) 353 srv.loopWG.Wait() 354 } 355 356 // Start starts running the server. 357 // Servers can not be re-used after stopping. 358 func (srv *Server) Start() (err error) { 359 srv.lock.Lock() 360 defer srv.lock.Unlock() 361 if srv.running { 362 return errors.New("server already running") 363 } 364 srv.running = true 365 log.Info("Starting P2P networking") //yichoi // 3단계쯤. 여기서 완료단계.. 366 367 // static fields 368 if srv.PrivateKey == nil { 369 return fmt.Errorf("Server.PrivateKey must be set to a non-nil key") 370 } 371 if srv.newTransport == nil { 372 srv.newTransport = newRLPX 373 } 374 if srv.Dialer == nil { 375 srv.Dialer = &net.Dialer{Timeout: defaultDialTimeout} 376 } 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.addqmanager ? 384 srv.removestatic = make(chan *discover.Node) 385 srv.peerOp = make(chan peerOpFunc) 386 srv.peerOpDone = make(chan struct{}) 387 //yichoi begin for private ip set 388 //VerA := strings.Trim(runtime.Version(),"go") 389 //VerB := "1.7" 390 //if ( compareVerLessthan( VerA, VerB) == 1 ) && !strings.HasPrefix(runtime.Version(), "devel") 391 392 if strings.HasPrefix(srv.Config.ListenAddr, ":") { //run error 393 394 srv.ListenAddr = strings.Trim(srv.ListenAddr, "\n") 395 srv.ListenAddr = fmt.Sprintf(srv.GetLocalIP() + srv.ListenAddr) //yichoi 396 //fmt.Printf("ListenAddr= %s\n", srv.ListenAddr ) 397 log.Debug("p2p server", "ListenAddr", srv.ListenAddr) 398 //srv.ListenLocalAddr = srv.ListenAddr 399 //srv.ListenLocalAddr = fmt.Sprintf(srv.GetLocalIP() + srv.ListenLocalAddr )//yichoi 400 //fmt.Printf("ListenLocalAddr= %s\n", srv.ListenLocalAddr ) 401 } 402 //end 403 // node table 404 if !srv.NoDiscovery { // geth init 시 -nodiscovery option 없으면 405 ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.NodeDatabase, srv.NetRestrict) //Now using in development, important!! 406 if err != nil { 407 return err 408 } 409 if err := ntab.SetFallbackNodes(srv.BootstrapNodes); err != nil { 410 return err 411 } 412 srv.ntab = ntab 413 } 414 415 if srv.DiscoveryV5 { 416 ntab, err := discv5.ListenUDP(srv.PrivateKey, srv.DiscoveryV5Addr, srv.NAT, "", srv.NetRestrict) //srv.NodeDatabase) 417 if err != nil { 418 return err 419 } 420 if err := ntab.SetFallbackNodes(srv.BootstrapNodesV5); err != nil { 421 return err 422 } 423 srv.DiscV5 = ntab 424 } 425 426 dynPeers := (srv.MaxPeers + 1) / 2 427 if srv.NoDiscovery { 428 dynPeers = 0 429 } 430 dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, dynPeers, srv.NetRestrict) //bootnode dial 431 432 // handshake 433 srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)} 434 for _, p := range srv.Protocols { 435 srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap()) 436 } 437 // listen/dial 438 if srv.ListenAddr != "" { 439 if err := srv.startListening(); err != nil { // 440 return err //에러 .. 441 } 442 } 443 if srv.NoDial && srv.ListenAddr == "" { 444 log.Warn("P2P server will be useless, neither dialing nor listening") 445 } 446 447 srv.loopWG.Add(1) 448 go srv.run(dialer) 449 srv.running = true 450 return nil 451 } 452 453 func (srv *Server) startListening() error { 454 // Launch the TCP listener. 455 //set local ip:192.168.0.x in reapchain office private network 456 //log.Info("startListening():","srv.Listen", srv.ListenAddr ) 457 listener, err := net.Listen("tcp", srv.ListenAddr) //listener == nil.. error 458 log.Info("startListening(tcp)", "addr", srv.Config.ListenAddr, "listener(tcp)=", listener) 459 if err != nil { 460 return err //왜 에러 ? 461 } 462 laddr := listener.Addr().(*net.TCPAddr) //192.168.0.2:5003 this type ㄱㅏ져와야함. 463 // fmt.Printf("\n laddr(TCPAddr) : %v\n", laddr) //error , important 464 log.Debug("start listening", "laddr(TCPAddr)", laddr) //error , important 465 srv.ListenAddr = laddr.String() 466 srv.listener = listener 467 srv.loopWG.Add(1) 468 469 //fmt.Printf("srv.ListenAddr = %s, srv.listener =%s\n", srv.ListenAddr, srv.listener ) 470 //fmt.Printf("srv.GetLocalIP=%s\n", srv.GetLocalIP() ) 471 472 go srv.listenLoop() // 루프를 여기서 돈다. 473 474 // Map the TCP listening port if NAT is configured. 475 if !laddr.IP.IsLoopback() && srv.NAT != nil { 476 srv.loopWG.Add(1) 477 go func() { 478 nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p") 479 srv.loopWG.Done() 480 }() 481 } 482 return nil 483 } 484 485 type dialer interface { 486 newTasks(running int, peers map[discover.NodeID]*Peer, now time.Time) []task 487 taskDone(task, time.Time) 488 addStatic(*discover.Node) 489 removeStatic(*discover.Node) 490 } 491 492 func (srv *Server) run(dialstate dialer) { 493 defer srv.loopWG.Done() 494 var ( 495 peers = make(map[discover.NodeID]*Peer) 496 trusted = make(map[discover.NodeID]bool, len(srv.TrustedNodes)) 497 taskdone = make(chan task, maxActiveDialTasks) 498 runningTasks []task 499 queuedTasks []task // tasks that can't run yet 500 ) 501 // Put trusted nodes into a map to speed up checks. 502 // Trusted peers are loaded on startup and cannot be 503 // modified while the server is running. 504 for _, n := range srv.TrustedNodes { 505 trusted[n.ID] = true 506 } 507 508 // removes t from runningTasks 509 delTask := func(t task) { 510 for i := range runningTasks { 511 if runningTasks[i] == t { 512 runningTasks = append(runningTasks[:i], runningTasks[i+1:]...) 513 break 514 } 515 } 516 } 517 // starts until max number of active tasks is satisfied 518 startTasks := func(ts []task) (rest []task) { 519 i := 0 520 for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ { 521 t := ts[i] 522 log.Trace("New dial task", "task", t) 523 go func() { t.Do(srv); taskdone <- t }() 524 runningTasks = append(runningTasks, t) 525 } 526 return ts[i:] 527 } 528 scheduleTasks := func() { 529 // Start from queue first. 530 queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...) 531 // Query dialer for new tasks and start as many as possible now. 532 if len(runningTasks) < maxActiveDialTasks { 533 nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now()) 534 queuedTasks = append(queuedTasks, startTasks(nt)...) 535 } 536 } 537 538 running: 539 for { 540 scheduleTasks() 541 542 select { 543 case <-srv.quit: 544 // The server was stopped. Run the cleanup logic. 545 break running 546 case n := <-srv.addstatic: //static node 추가 채널 547 // This channel is used by AddPeer to add to the 548 // ephemeral static peer list. Add it to the dialer, 549 // it will keep the node connected. 550 log.Debug("Adding static node", "node", n) 551 dialstate.addStatic(n) 552 case n := <-srv.removestatic: 553 // This channel is used by RemovePeer to send a 554 // disconnect request to a peer and begin the 555 // stop keeping the node connected 556 log.Debug("Removing static node", "node", n) 557 dialstate.removeStatic(n) 558 if p, ok := peers[n.ID]; ok { 559 p.Disconnect(DiscRequested) 560 } 561 case op := <-srv.peerOp: 562 // This channel is used by Peers and PeerCount. 563 op(peers) 564 srv.peerOpDone <- struct{}{} 565 case t := <-taskdone: 566 // A task got done. Tell dialstate about it so it 567 // can update its state and remove it from the active 568 // tasks list. 569 log.Trace("Dial task done", "task", t) 570 dialstate.taskDone(t, time.Now()) 571 delTask(t) 572 case c := <-srv.posthandshake: 573 // A connection has passed the encryption handshake so 574 // the remote identity is known (but hasn't been verified yet). 575 if trusted[c.id] { 576 // Ensure that the trusted flag is set before checking against MaxPeers. 577 c.flags |= trustedConn 578 } 579 // TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them. 580 c.cont <- srv.encHandshakeChecks(peers, c) 581 case c := <-srv.addpeer: 582 // At this point the connection is past the protocol handshake. 583 // Its capabilities are known and the remote identity is verified. 584 err := srv.protoHandshakeChecks(peers, c) 585 if err == nil { 586 // The handshakes are done and it passed all checks. 587 p := newPeer(c, srv.Protocols) 588 name := truncateName(c.name) 589 log.Debug("Adding p2p peer", "id", c.id, "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1) 590 peers[c.id] = p 591 go srv.runPeer(p) 592 } 593 // The dialer logic relies on the assumption that 594 // dial tasks complete after the peer has been added or 595 // discarded. Unblock the task last. 596 c.cont <- err 597 case pd := <-srv.delpeer: 598 // A peer disconnected. 599 d := common.PrettyDuration(mclock.Now() - pd.created) 600 pd.log.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err) 601 delete(peers, pd.ID()) 602 } 603 } 604 605 log.Trace("P2P networking is spinning down") 606 607 // Terminate discovery. If there is a running lookup it will terminate soon. 608 if srv.ntab != nil { 609 srv.ntab.Close() 610 } 611 if srv.DiscV5 != nil { 612 srv.DiscV5.Close() 613 } 614 // Disconnect all peers. 615 for _, p := range peers { 616 p.Disconnect(DiscQuitting) 617 } 618 // Wait for peers to shut down. Pending connections and tasks are 619 // not handled here and will terminate soon-ish because srv.quit 620 // is closed. 621 for len(peers) > 0 { 622 p := <-srv.delpeer 623 p.log.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks)) 624 delete(peers, p.ID()) 625 } 626 } 627 628 func (srv *Server) protoHandshakeChecks(peers map[discover.NodeID]*Peer, c *conn) error { 629 // Drop connections with no matching protocols. 630 if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, c.caps) == 0 { 631 return DiscUselessPeer 632 } 633 // Repeat the encryption handshake checks because the 634 // peer set might have changed between the handshakes. 635 return srv.encHandshakeChecks(peers, c) 636 } 637 638 func (srv *Server) encHandshakeChecks(peers map[discover.NodeID]*Peer, c *conn) error { 639 switch { 640 case !c.is(trustedConn|staticDialedConn) && len(peers) >= srv.MaxPeers: 641 return DiscTooManyPeers 642 case peers[c.id] != nil: 643 return DiscAlreadyConnected 644 case c.id == srv.Self().ID: 645 return DiscSelf 646 default: 647 return nil 648 } 649 } 650 651 type tempError interface { 652 Temporary() bool 653 } 654 655 // GetLocalIP returns the non loopback local IP of the host 656 func (srv *Server) GetLocalIP() string { 657 addrs, err := net.InterfaceAddrs() 658 if err != nil { 659 return "" 660 } 661 for _, address := range addrs { 662 // check the address type and if it is not a loopback the display it 663 if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 664 if ipnet.IP.To4() != nil { 665 return ipnet.IP.String() 666 } 667 } 668 } 669 return "" 670 } 671 672 // listenLoop runs in its own goroutine and accepts 673 // inbound connections. 674 func (srv *Server) listenLoop() { 675 676 defer srv.loopWG.Done() 677 log.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab)) 678 // RLPx Listener는 실행되면, 호스트의 IP를 0.0.0.0 로 셋팅하고, 기다린다. 원격지로부터 접속을 679 // 그래서 외부의 연결 허용을 위해서 공유기의 외부 공인IP를 가져와서,, 셋팅된다. 680 681 // This channel acts as a semaphore limiting 682 // active inbound connections that are lingering pre-handshake. 683 // If all slots are taken, no further connections are accepted. 684 tokens := maxAcceptConns 685 if srv.MaxPendingPeers > 0 { 686 tokens = srv.MaxPendingPeers 687 } 688 slots := make(chan struct{}, tokens) 689 for i := 0; i < tokens; i++ { 690 slots <- struct{}{} 691 } 692 693 for { 694 // Wait for a handshake slot before accepting. 695 <-slots 696 697 var ( 698 fd net.Conn 699 err error 700 ) 701 for { 702 fd, err = srv.listener.Accept() 703 if tempErr, ok := err.(tempError); ok && tempErr.Temporary() { 704 log.Debug("Temporary read error", "err", err) 705 continue 706 } else if err != nil { 707 log.Debug("Read error", "err", err) 708 return 709 } 710 break 711 } 712 713 // Reject connections that do not match NetRestrict. 714 if srv.NetRestrict != nil { 715 if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) { 716 log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr()) 717 fd.Close() 718 slots <- struct{}{} 719 continue 720 } 721 } 722 723 fd = newMeteredConn(fd, true) 724 log.Trace("Accepted connection", "addr", fd.RemoteAddr()) 725 726 // Spawn the handler. It will give the slot back when the connection 727 // has been established. 728 go func() { 729 srv.setupConn(fd, inboundConn, nil) 730 slots <- struct{}{} 731 }() 732 } 733 } 734 735 // setupConn runs the handshakes and attempts to add the connection 736 // as a peer. It returns when the connection has been added as a peer 737 // or the handshakes have failed. 738 func (srv *Server) setupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) { 739 // Prevent leftover pending conns from entering the handshake. 740 srv.lock.Lock() 741 running := srv.running 742 srv.lock.Unlock() 743 c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)} 744 if !running { 745 c.close(errServerStopped) 746 return 747 } 748 // Run the encryption handshake. 749 var err error 750 if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil { 751 log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) //reviewed by yichoi 752 c.close(err) 753 return 754 } 755 clog := log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags) // 원격 주소 리턴, 756 // For dialed connections, check that the remote public key matches. 757 if dialDest != nil && c.id != dialDest.ID { 758 c.close(DiscUnexpectedIdentity) 759 clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID) 760 return 761 } 762 if err := srv.checkpoint(c, srv.posthandshake); err != nil { 763 clog.Trace("Rejected peer before protocol handshake", "err", err) 764 c.close(err) 765 return 766 } 767 // Run the protocol handshake 768 phs, err := c.doProtoHandshake(srv.ourHandshake) 769 if err != nil { 770 clog.Trace("Failed proto handshake", "err", err) 771 c.close(err) 772 return 773 } 774 if phs.ID != c.id { 775 clog.Trace("Wrong devp2p handshake identity", "err", phs.ID) 776 c.close(DiscUnexpectedIdentity) 777 return 778 } 779 c.caps, c.name = phs.Caps, phs.Name 780 if err := srv.checkpoint(c, srv.addpeer); err != nil { 781 clog.Trace("Rejected peer", "err", err) 782 c.close(err) 783 return 784 } 785 // If the checks completed successfully, runPeer has now been 786 // launched by run. 787 } 788 789 func truncateName(s string) string { 790 if len(s) > 20 { 791 return s[:20] + "..." 792 } 793 return s 794 } 795 796 // checkpoint sends the conn to run, which performs the 797 // post-handshake checks for the stage (posthandshake, addpeer). 798 func (srv *Server) checkpoint(c *conn, stage chan<- *conn) error { 799 select { 800 case stage <- c: 801 case <-srv.quit: 802 return errServerStopped 803 } 804 select { 805 case err := <-c.cont: 806 return err 807 case <-srv.quit: 808 return errServerStopped 809 } 810 } 811 812 // runPeer runs in its own goroutine for each peer. 813 // it waits until the Peer logic returns and removes 814 // the peer. 815 func (srv *Server) runPeer(p *Peer) { 816 if srv.newPeerHook != nil { 817 srv.newPeerHook(p) 818 } 819 remoteRequested, err := p.run() 820 // Note: run waits for existing peers to be sent on srv.delpeer 821 // before returning, so this send should not select on srv.quit. 822 srv.delpeer <- peerDrop{p, err, remoteRequested} 823 } 824 825 // NodeInfo represents a short summary of the information known about the host. 826 type NodeInfo struct { 827 ID string `json:"id"` // Unique node identifier (also the encryption key) 828 Name string `json:"name"` // Name of the node, including client type, version, OS, custom data 829 Enode string `json:"enode"` // Enode URL for adding this peer from remote peers 830 IP string `json:"ip"` // IP address of the node 831 Ports struct { 832 Discovery int `json:"discovery"` // UDP listening port for discovery protocol 833 Listener int `json:"listener"` // TCP listening port for RLPx 834 } `json:"ports"` 835 ListenAddr string `json:"listenAddr"` 836 Protocols map[string]interface{} `json:"protocols"` 837 } 838 839 // NodeInfo gathers and returns a collection of metadata known about the host. 840 func (srv *Server) NodeInfo() *NodeInfo { 841 node := srv.Self() 842 843 // Gather and assemble the generic node infos 844 info := &NodeInfo{ 845 Name: srv.Name, 846 Enode: node.String(), 847 ID: node.ID.String(), 848 IP: node.IP.String(), 849 ListenAddr: srv.ListenAddr, 850 Protocols: make(map[string]interface{}), 851 } 852 info.Ports.Discovery = int(node.UDP) 853 info.Ports.Listener = int(node.TCP) 854 855 // Gather all the running protocol infos (only once per protocol type) 856 for _, proto := range srv.Protocols { 857 if _, ok := info.Protocols[proto.Name]; !ok { 858 nodeInfo := interface{}("unknown") 859 if query := proto.NodeInfo; query != nil { 860 nodeInfo = proto.NodeInfo() 861 } 862 info.Protocols[proto.Name] = nodeInfo 863 } 864 } 865 return info 866 } 867 868 // PeersInfo returns an array of metadata objects describing connected peers. 869 func (srv *Server) PeersInfo() []*PeerInfo { 870 // Gather all the generic and sub-protocol specific infos 871 infos := make([]*PeerInfo, 0, srv.PeerCount()) 872 for _, peer := range srv.Peers() { 873 if peer != nil { 874 infos = append(infos, peer.Info()) 875 } 876 } 877 // Sort the result array alphabetically by node identifier 878 for i := 0; i < len(infos); i++ { 879 for j := i + 1; j < len(infos); j++ { 880 if infos[i].ID > infos[j].ID { 881 infos[i], infos[j] = infos[j], infos[i] 882 } 883 } 884 } 885 return infos 886 }