diff --git a/conn/session.c b/conn/session.c index a50cc01..65d31ab 100644 --- a/conn/session.c +++ b/conn/session.c @@ -44,7 +44,7 @@ int libp2p_session_context_free(struct SessionContext* context) { if (context != NULL) { if (context->default_stream != NULL) context->default_stream->close(context); - //TODO: Be more intelligent + context->default_stream = NULL; free(context); } return 1; diff --git a/net/multistream.c b/net/multistream.c index b061e60..2900893 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -29,13 +29,20 @@ int multistream_default_timeout = 5; */ int libp2p_net_multistream_close(void* stream_context) { struct SessionContext* secure_context = (struct SessionContext*)stream_context; - struct Stream* stream = secure_context->insecure_stream; + struct Stream* stream = secure_context->default_stream; + if (stream == NULL || stream->socket_descriptor == NULL) + return 1; int socket_descriptor = *((int*)stream->socket_descriptor); close(socket_descriptor); free(stream->socket_descriptor); + stream->socket_descriptor = NULL; if (stream->address != NULL) multiaddress_free(stream->address); + stream->address = NULL; free(stream); + secure_context->default_stream = NULL; + secure_context->insecure_stream = NULL; + secure_context->secure_stream = NULL; return 1; } diff --git a/secio/secio.c b/secio/secio.c index de69993..d32ddec 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -974,8 +974,10 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva // receive Exchange packet libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Reading exchange packet\n"); bytes_written = libp2p_secio_unencrypted_read(local_session, &results, &results_size, 10); - if (bytes_written == 0) + if (bytes_written == 0) { + libp2p_peer_handle_connection_error(remote_peer); goto exit; + } libp2p_secio_exchange_protobuf_decode(results, results_size, &exchange_in); free(results); results = NULL; @@ -1103,7 +1105,6 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake success!\n"); } else { libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake returning false\n"); - libp2p_peer_free(remote_peer); } return retVal; }