mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
p2p: refactors SetupConn and dial
* SetupConn now returns error * dial checks the error and calls resolve on failed dial only
This commit is contained in:
parent
72535b00b1
commit
31bae2f349
2 changed files with 44 additions and 29 deletions
19
p2p/dial.go
19
p2p/dial.go
|
|
@ -291,13 +291,16 @@ func (t *dialTask) Do(srv *Server) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
success := t.dial(srv, t.dest)
|
err := t.dial(srv, t.dest)
|
||||||
|
if err != nil {
|
||||||
|
log.Trace("Dial error", "task", t, "err", err)
|
||||||
// Try resolving the ID of static nodes if dialing failed.
|
// Try resolving the ID of static nodes if dialing failed.
|
||||||
if !success && t.flags&staticDialedConn != 0 {
|
if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 {
|
||||||
if t.resolve(srv) {
|
if t.resolve(srv) {
|
||||||
t.dial(srv, t.dest)
|
t.dial(srv, t.dest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolve attempts to find the current endpoint for the destination
|
// resolve attempts to find the current endpoint for the destination
|
||||||
|
|
@ -334,16 +337,18 @@ func (t *dialTask) resolve(srv *Server) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dialError struct {
|
||||||
|
error
|
||||||
|
}
|
||||||
|
|
||||||
// dial performs the actual connection attempt.
|
// dial performs the actual connection attempt.
|
||||||
func (t *dialTask) dial(srv *Server, dest *discover.Node) bool {
|
func (t *dialTask) dial(srv *Server, dest *discover.Node) error {
|
||||||
fd, err := srv.Dialer.Dial(dest)
|
fd, err := srv.Dialer.Dial(dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Trace("Dial error", "task", t, "err", err)
|
return &dialError{err}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
mfd := newMeteredConn(fd, false)
|
mfd := newMeteredConn(fd, false)
|
||||||
srv.SetupConn(mfd, t.flags, dest)
|
return srv.SetupConn(mfd, t.flags, dest)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *dialTask) String() string {
|
func (t *dialTask) String() string {
|
||||||
|
|
|
||||||
|
|
@ -706,55 +706,65 @@ func (srv *Server) listenLoop() {
|
||||||
// SetupConn runs the handshakes and attempts to add the connection
|
// SetupConn runs the handshakes and attempts to add the connection
|
||||||
// as a peer. It returns when the connection has been added as a peer
|
// as a peer. It returns when the connection has been added as a peer
|
||||||
// or the handshakes have failed.
|
// or the handshakes have failed.
|
||||||
func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) {
|
func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error {
|
||||||
|
self := srv.Self()
|
||||||
|
if self == nil {
|
||||||
|
return errors.New("shutdown")
|
||||||
|
}
|
||||||
|
c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)}
|
||||||
|
err := srv.setupConn(c, flags, dialDest)
|
||||||
|
if err != nil {
|
||||||
|
c.close(err)
|
||||||
|
srv.log.Trace("Setting up connection failed", "id", c.id, "err", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *discover.Node) error {
|
||||||
// Prevent leftover pending conns from entering the handshake.
|
// Prevent leftover pending conns from entering the handshake.
|
||||||
srv.lock.Lock()
|
srv.lock.Lock()
|
||||||
running := srv.running
|
running := srv.running
|
||||||
srv.lock.Unlock()
|
srv.lock.Unlock()
|
||||||
c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)}
|
|
||||||
if !running {
|
if !running {
|
||||||
c.close(errServerStopped)
|
return errServerStopped
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// Run the encryption handshake.
|
// Run the encryption handshake.
|
||||||
var err error
|
var err error
|
||||||
if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil {
|
if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil {
|
||||||
srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
|
srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
|
||||||
c.close(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
clog := log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
|
clog := srv.log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
|
||||||
// For dialed connections, check that the remote public key matches.
|
// For dialed connections, check that the remote public key matches.
|
||||||
if dialDest != nil && c.id != dialDest.ID {
|
if dialDest != nil && c.id != dialDest.ID {
|
||||||
c.close(DiscUnexpectedIdentity)
|
|
||||||
clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID)
|
clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID)
|
||||||
return
|
return DiscUnexpectedIdentity
|
||||||
}
|
}
|
||||||
if err := srv.checkpoint(c, srv.posthandshake); err != nil {
|
err = srv.checkpoint(c, srv.posthandshake)
|
||||||
|
if err != nil {
|
||||||
clog.Trace("Rejected peer before protocol handshake", "err", err)
|
clog.Trace("Rejected peer before protocol handshake", "err", err)
|
||||||
c.close(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// Run the protocol handshake
|
// Run the protocol handshake
|
||||||
phs, err := c.doProtoHandshake(srv.ourHandshake)
|
phs, err := c.doProtoHandshake(srv.ourHandshake)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clog.Trace("Failed proto handshake", "err", err)
|
clog.Trace("Failed proto handshake", "err", err)
|
||||||
c.close(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if phs.ID != c.id {
|
if phs.ID != c.id {
|
||||||
clog.Trace("Wrong devp2p handshake identity", "err", phs.ID)
|
clog.Trace("Wrong devp2p handshake identity", "err", phs.ID)
|
||||||
c.close(DiscUnexpectedIdentity)
|
return DiscUnexpectedIdentity
|
||||||
return
|
|
||||||
}
|
}
|
||||||
c.caps, c.name = phs.Caps, phs.Name
|
c.caps, c.name = phs.Caps, phs.Name
|
||||||
if err := srv.checkpoint(c, srv.addpeer); err != nil {
|
err = srv.checkpoint(c, srv.addpeer)
|
||||||
|
if err != nil {
|
||||||
clog.Trace("Rejected peer", "err", err)
|
clog.Trace("Rejected peer", "err", err)
|
||||||
c.close(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// If the checks completed successfully, runPeer has now been
|
// If the checks completed successfully, runPeer has now been
|
||||||
// launched by run.
|
// launched by run.
|
||||||
|
clog.Trace("connection set up", "inbound", dialDest == nil)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func truncateName(s string) string {
|
func truncateName(s string) string {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue