diff --git a/exchange/bitswap/bitswap.c b/exchange/bitswap/bitswap.c index d424512..d7320cd 100644 --- a/exchange/bitswap/bitswap.c +++ b/exchange/bitswap/bitswap.c @@ -59,6 +59,14 @@ int ipfs_bitswap_free(struct Exchange* exchange) { struct BitswapContext* bitswapContext = (struct BitswapContext*) exchange->exchangeContext; if (bitswapContext != NULL) ipfs_bitswap_engine_stop(bitswapContext); + if (bitswapContext->localWantlist != NULL) { + ipfs_bitswap_wantlist_queue_free(bitswapContext->localWantlist); + bitswapContext->localWantlist = NULL; + } + if (bitswapContext->peerRequestQueue != NULL) { + ipfs_bitswap_peer_request_queue_free(bitswapContext->peerRequestQueue); + bitswapContext->peerRequestQueue = NULL; + } free(exchange->exchangeContext); } free(exchange); diff --git a/exchange/bitswap/engine.c b/exchange/bitswap/engine.c index ea4f05c..7ae39f8 100644 --- a/exchange/bitswap/engine.c +++ b/exchange/bitswap/engine.c @@ -97,6 +97,7 @@ void* ipfs_bitswap_engine_peer_request_processor_start(void* ctx) { if (current_peer_entry->sessionContext->default_stream->read(current_peer_entry->sessionContext, &buffer, &buffer_len, 1)) { // handle it int retVal = ipfs_multistream_marshal(buffer, buffer_len, current_peer_entry->sessionContext, context->ipfsNode); + free(buffer); did_some_processing = 1; if (retVal == -1) { // there was a problem. Clean up diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index 326f525..61dcbcd 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -139,10 +139,12 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc if (!ipfs_cid_protobuf_decode(entry->block, entry->block_size, &cid) || cid->hash_length == 0) { libp2p_logger_error("bitswap_network", "Message had invalid CID\n"); ipfs_cid_free(cid); + ipfs_bitswap_message_free(message); return 0; } ipfs_bitswap_network_adjust_cid_queue(queueEntry->current->cids_they_want, cid, entry->cancel); } } + ipfs_bitswap_message_free(message); return 1; } diff --git a/exchange/bitswap/peer_request_queue.c b/exchange/bitswap/peer_request_queue.c index e71a54f..ddc4a2d 100644 --- a/exchange/bitswap/peer_request_queue.c +++ b/exchange/bitswap/peer_request_queue.c @@ -57,13 +57,45 @@ struct PeerRequest* ipfs_bitswap_peer_request_new() { return request; } +int ipfs_bitswap_cid_entry_free(struct CidEntry* entry) { + if (entry != NULL) { + if (entry->cid != NULL) { + ipfs_cid_free(entry->cid); + entry->cid = NULL; + } + free(entry); + } + return 1; +} + /** * Free resources from a PeerRequest * @param request the request to free * @returns true(1) */ int ipfs_bitswap_peer_request_free(struct PeerRequest* request) { - free(request); + if (request != NULL) { + for(int i = 0; i < request->cids_we_want->total; i++) { + struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(request->cids_we_want, i); + ipfs_bitswap_cid_entry_free(entry); + } + libp2p_utils_vector_free(request->cids_we_want); + request->cids_we_want = NULL; + for(int i = 0; i < request->cids_they_want->total; i++) { + struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(request->cids_they_want, i); + ipfs_bitswap_cid_entry_free(entry); + } + libp2p_utils_vector_free(request->cids_they_want); + request->cids_they_want = NULL; + for(int i = 0; i < request->blocks_we_want_to_send->total; i++) { + struct Block* block = (struct Block*)libp2p_utils_vector_get(request->blocks_we_want_to_send, i); + ipfs_block_free(block); + } + libp2p_utils_vector_free(request->blocks_we_want_to_send); + request->blocks_we_want_to_send = NULL; + free(request); + + } return 1; } @@ -341,8 +373,11 @@ int ipfs_bitswap_peer_request_process_entry(const struct BitswapContext* context // add requests that we would like ipfs_bitswap_message_add_wantlist_items(msg, request->cids_we_want); // send message - if (ipfs_bitswap_network_send_message(context, request->peer, msg)) + if (ipfs_bitswap_network_send_message(context, request->peer, msg)) { + ipfs_bitswap_message_free(msg); return 1; + } + ipfs_bitswap_message_free(msg); } } return 0; diff --git a/exchange/bitswap/wantlist_queue.c b/exchange/bitswap/wantlist_queue.c index 3e5d511..b1a74c0 100644 --- a/exchange/bitswap/wantlist_queue.c +++ b/exchange/bitswap/wantlist_queue.c @@ -47,6 +47,10 @@ struct WantListQueue* ipfs_bitswap_wantlist_queue_new() { int ipfs_bitswap_wantlist_queue_free(struct WantListQueue* wantlist) { if (wantlist != NULL) { if (wantlist->queue != NULL) { + for(int i = 0; i < wantlist->queue->total; i++) { + struct WantListQueueEntry* entry = (struct WantListQueueEntry*)libp2p_utils_vector_get(wantlist->queue, i); + ipfs_bitswap_wantlist_queue_entry_free(entry); + } libp2p_utils_vector_free(wantlist->queue); wantlist->queue = NULL; } @@ -262,7 +266,9 @@ int ipfs_bitswap_wantlist_get_block_remote(struct BitswapContext* context, struc libp2p_utils_vector_add(queueEntry->cids_we_want, entry); // process this queue via bitswap protocol ipfs_bitswap_peer_request_process_entry(context, queueEntry); + //libp2p_peer_free(current); } + libp2p_utils_vector_free(providers); return 1; } return 0; diff --git a/routing/online.c b/routing/online.c index 47ca6aa..8cabad2 100644 --- a/routing/online.c +++ b/routing/online.c @@ -36,16 +36,24 @@ struct Libp2pMessage* ipfs_routing_online_send_receive_message(struct SessionCon } // send the message, and expect the same back - sessionContext->default_stream->write(sessionContext, protobuf, protobuf_size); - sessionContext->default_stream->read(sessionContext, &results, &results_size, 5); - - if (results_size == 0) + if (!sessionContext->default_stream->write(sessionContext, protobuf, protobuf_size)) { + libp2p_logger_error("online", "Attempted to write to Kademlia stream, but could not.\n"); goto exit; + } + if (!sessionContext->default_stream->read(sessionContext, &results, &results_size, 5)) { + libp2p_logger_error("online", "Attempted to read from Kademlia stream, but could not.\n"); + } + + if (results_size == 0) { + libp2p_logger_error("online", "reading kademlia response returned nothing.\n"); + goto exit; + } // see if we can unprotobuf - if (!libp2p_message_protobuf_decode(results, results_size, &return_message)) + if (!libp2p_message_protobuf_decode(results, results_size, &return_message)) { + libp2p_logger_error("online", "Received kademlia response, but cannot decode it.\n"); goto exit; - + } exit: if (protobuf != NULL) @@ -464,13 +472,14 @@ int ipfs_routing_online_bootstrap(struct IpfsRouting* routing) { peer_id_size = strlen(peer_id); peer = libp2p_peer_new(); peer->id_size = peer_id_size; - peer->id = malloc(peer->id_size); + peer->id = malloc(peer->id_size + 1); if (peer->id == NULL) { // out of memory? libp2p_peer_free(peer); free(peer_id); return -1; } memcpy(peer->id, peer_id, peer->id_size); + peer->id[peer->id_size] = 0; peer->addr_head = libp2p_utils_linked_list_new(); if (peer->addr_head == NULL) { // out of memory? libp2p_peer_free(peer); diff --git a/test/exchange/test_bitswap.h b/test/exchange/test_bitswap.h index d3693cf..e0d8c84 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -1,4 +1,5 @@ #include +#include #include "../test_helper.h" #include "../routing/test_routing.h" // for test_routing_daemon_start #include "libp2p/utils/vector.h" @@ -262,16 +263,17 @@ int test_bitswap_retrieve_file_remote() { */ int test_bitswap_retrieve_file_known_remote() { int retVal = 0; + signal(SIGPIPE, SIG_IGN); /*** * This assumes a remote server with the hello_world.txt file already in its database */ int remote_port = 4001; // mac - // char* remote_peer_id = "QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4"; - // char* remote_ip = "10.211.55.2"; + char* remote_peer_id = "QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4"; + char* remote_ip = "10.211.55.2"; // linux - char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY"; - char* remote_ip = "10.211.55.4"; + //char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY"; + //char* remote_ip = "10.211.55.4"; char* hello_world_hash = "QmTUFTVgkHT3Qdd9ospVjSLi2upd6VdkeNXZQH66cVmzja"; /* @@ -317,6 +319,16 @@ int test_bitswap_retrieve_file_known_remote() { goto exit; } + if (result == NULL) { + libp2p_logger_error("test_bitswap", "GetBlock returned NULL"); + goto exit; + } + + if (result->cid == NULL) { + libp2p_logger_error("test_bitswap", "GetBlock returned an object with no CID"); + goto exit; + } + if (cid->hash_length != result->cid->hash_length) { libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", strlen(hello_world_hash), result->cid->hash_length); goto exit; @@ -335,6 +347,7 @@ int test_bitswap_retrieve_file_known_remote() { ipfs_block_free(result); if (cid != NULL) ipfs_cid_free(cid); + ipfs_node_free(ipfs_node2); return retVal; }