Cleaning up bad connections

This commit is contained in:
jmjatlanta 2017-08-03 12:17:17 -05:00
parent 61a576eb93
commit 48c4b5ade0
3 changed files with 12 additions and 4 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
}