Fixes for various memory leaks

yamux
John Jones 2017-05-11 13:53:23 -05:00
parent ff4be03782
commit e4ba343d48
5 changed files with 22 additions and 5 deletions

View File

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

View File

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

View File

@ -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) {

View File

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

View File

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