p2p: don't send DiscReason when using net.Pipe (#16004)

This commit is contained in:
Anton Evangelatov 2018-02-22 11:41:06 +01:00 committed by Felix Lange
parent bb5349b154
commit 1e457b6599
2 changed files with 43 additions and 5 deletions

View File

@ -108,10 +108,16 @@ func (t *rlpx) close(err error) {
// Tell the remote end why we're disconnecting if possible. // Tell the remote end why we're disconnecting if possible.
if t.rw != nil { if t.rw != nil {
if r, ok := err.(DiscReason); ok && r != DiscNetworkError { if r, ok := err.(DiscReason); ok && r != DiscNetworkError {
t.fd.SetWriteDeadline(time.Now().Add(discWriteTimeout)) // rlpx tries to send DiscReason to disconnected peer
// if the connection is net.Pipe (in-memory simulation)
// it hangs forever, since net.Pipe does not implement
// a write deadline. Because of this only try to send
// the disconnect reason message if there is no error.
if err := t.fd.SetWriteDeadline(time.Now().Add(discWriteTimeout)); err == nil {
SendItems(t.rw, discMsg, r) SendItems(t.rw, discMsg, r)
} }
} }
}
t.fd.Close() t.fd.Close()
} }

View File

@ -156,14 +156,18 @@ func TestProtocolHandshake(t *testing.T) {
node1 = &discover.Node{ID: discover.PubkeyID(&prv1.PublicKey), IP: net.IP{5, 6, 7, 8}, TCP: 44} node1 = &discover.Node{ID: discover.PubkeyID(&prv1.PublicKey), IP: net.IP{5, 6, 7, 8}, TCP: 44}
hs1 = &protoHandshake{Version: 3, ID: node1.ID, Caps: []Cap{{"c", 1}, {"d", 3}}} hs1 = &protoHandshake{Version: 3, ID: node1.ID, Caps: []Cap{{"c", 1}, {"d", 3}}}
fd0, fd1 = net.Pipe()
wg sync.WaitGroup wg sync.WaitGroup
) )
fd0, fd1, err := tcpPipe()
if err != nil {
t.Fatal(err)
}
wg.Add(2) wg.Add(2)
go func() { go func() {
defer wg.Done() defer wg.Done()
defer fd1.Close() defer fd0.Close()
rlpx := newRLPX(fd0) rlpx := newRLPX(fd0)
remid, err := rlpx.doEncHandshake(prv0, node1) remid, err := rlpx.doEncHandshake(prv0, node1)
if err != nil { if err != nil {
@ -597,3 +601,31 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
t.Errorf("ingress-mac('foo') mismatch:\ngot %x\nwant %x", fooIngressHash, wantFooIngressHash) t.Errorf("ingress-mac('foo') mismatch:\ngot %x\nwant %x", fooIngressHash, wantFooIngressHash)
} }
} }
// tcpPipe creates an in process full duplex pipe based on a localhost TCP socket
func tcpPipe() (net.Conn, net.Conn, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, nil, err
}
defer l.Close()
var aconn net.Conn
aerr := make(chan error, 1)
go func() {
var err error
aconn, err = l.Accept()
aerr <- err
}()
dconn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
<-aerr
return nil, nil, err
}
if err := <-aerr; err != nil {
dconn.Close()
return nil, nil, err
}
return aconn, dconn, nil
}