From e4ba343d48967af24bd56e7f71721cae2847b86f Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 11 May 2017 13:53:23 -0500 Subject: [PATCH] Fixes for various memory leaks --- net/multistream.c | 11 ++++++++--- net/socket.c | 3 ++- peer/peerstore.c | 3 +++ record/message.c | 8 ++++++++ routing/dht_protocol.c | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/net/multistream.c b/net/multistream.c index 540a3a8..217b53e 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -12,6 +12,9 @@ #include "libp2p/net/multistream.h" #include "multiaddr/multiaddr.h" +// NOTE: this is normally set to 5 seconds, but you may want to increase this during debugging +int multistream_default_timeout = 5; + /*** * An implementation of the libp2p multistream */ @@ -147,7 +150,7 @@ struct Stream* libp2p_net_multistream_connect(const char* hostname, int port) { session.default_stream = stream; // try to receive the protocol id - return_result = libp2p_net_multistream_read(&session, &results, &results_size, 5); + return_result = libp2p_net_multistream_read(&session, &results, &results_size, multistream_default_timeout); if (return_result == 0 || results_size < 1) goto exit; @@ -183,7 +186,7 @@ int libp2p_net_multistream_negotiate(struct Stream* stream) { if (!libp2p_net_multistream_write(&secure_session, (unsigned char*)protocolID, strlen(protocolID))) goto exit; // expect the same back - libp2p_net_multistream_read(&secure_session, &results, &results_length, 5); + libp2p_net_multistream_read(&secure_session, &results, &results_length, multistream_default_timeout); if (results_length == 0) goto exit; if (strncmp((char*)results, protocolID, strlen(protocolID)) != 0) @@ -226,8 +229,10 @@ struct Libp2pMessage* libp2p_net_multistream_get_message(struct Stream* stream) void libp2p_net_multistream_stream_free(struct Stream* stream) { if (stream != NULL) { - if (stream->socket_descriptor != NULL) + if (stream->socket_descriptor != NULL) { + close( *((int*)stream->socket_descriptor)); free(stream->socket_descriptor); + } if (stream->address != NULL) multiaddress_free(stream->address); free(stream); diff --git a/net/socket.c b/net/socket.c index 05ce15d..44a5bc9 100644 --- a/net/socket.c +++ b/net/socket.c @@ -37,7 +37,8 @@ int socket_bind4(int s, uint32_t ip, uint16_t port) int socket_bind4_reuse(int s, uint32_t ip, uint16_t port) { int opt = 1; - setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt); + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); return socket_bind4(s, ip, port); } diff --git a/peer/peerstore.c b/peer/peerstore.c index f32e4e9..6625f09 100644 --- a/peer/peerstore.c +++ b/peer/peerstore.c @@ -149,7 +149,10 @@ int libp2p_peerstore_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* pe */ struct PeerEntry* libp2p_peerstore_get_peer_entry(struct Peerstore* peerstore, const unsigned char* peer_id, size_t peer_id_size) { struct Libp2pLinkedList* current = peerstore->head_entry; + // JMJ Debugging + int count = 0; while(current != NULL) { + count++; struct Libp2pPeer* peer = ((struct PeerEntry*)current->item)->peer; if (peer->id_size == peer_id_size) { if (memcmp(peer_id, peer->id, peer->id_size) == 0) { diff --git a/record/message.c b/record/message.c index 0c516ef..fd97cbe 100644 --- a/record/message.c +++ b/record/message.c @@ -12,6 +12,10 @@ * protobuf and other methods for Message */ +/** + * Allocate memory for a message + * @returns a new, allocated Libp2pMessage struct + */ struct Libp2pMessage* libp2p_message_new() { struct Libp2pMessage* out = (struct Libp2pMessage*)malloc(sizeof(struct Libp2pMessage)); if (out != NULL) { @@ -26,6 +30,10 @@ struct Libp2pMessage* libp2p_message_new() { return out; } +/** + * Frees all resources related to a Libp2pMessage + * @param in the incoming message + */ void libp2p_message_free(struct Libp2pMessage* in) { if (in != NULL) { // a linked list of peer structs diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index b5d90c3..72f0177 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -311,7 +311,7 @@ int libp2p_routing_dht_handle_find_node(struct SessionContext* session, struct L struct Libp2pPeer* peer = libp2p_peerstore_get_peer(peerstore, message->key, message->key_size); if (peer != NULL) { message->provider_peer_head = libp2p_utils_linked_list_new(); - message->provider_peer_head->item = peer; + message->provider_peer_head->item = libp2p_peer_copy(peer); if (!libp2p_routing_dht_protobuf_message(message, result_buffer, result_buffer_size)) { return 0; }