Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #39 from libp2p/close-conn-on-error
Browse files Browse the repository at this point in the history
close the underlying connection when the handshake fails
  • Loading branch information
marten-seemann authored Nov 26, 2019
2 parents 8afeaef + caaacc1 commit 702fd53
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ var _ sec.SecureTransport = &Transport{}
// SecureInbound runs the TLS handshake as a server.
func (t *Transport) SecureInbound(ctx context.Context, insecure net.Conn) (sec.SecureConn, error) {
config, keyCh := t.identity.ConfigForAny()
return t.handshake(ctx, tls.Server(insecure, config), keyCh)
cs, err := t.handshake(ctx, tls.Server(insecure, config), keyCh)
if err != nil {
insecure.Close()
}
return cs, err
}

// SecureOutbound runs the TLS handshake as a client.
Expand All @@ -66,7 +70,11 @@ func (t *Transport) SecureInbound(ctx context.Context, insecure net.Conn) (sec.S
// notice this after 1 RTT when calling Read.
func (t *Transport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) {
config, keyCh := t.identity.ConfigForPeer(p)
return t.handshake(ctx, tls.Client(insecure, config), keyCh)
cs, err := t.handshake(ctx, tls.Client(insecure, config), keyCh)
if err != nil {
insecure.Close()
}
return cs, err
}

func (t *Transport) handshake(
Expand Down

0 comments on commit 702fd53

Please sign in to comment.