From caf02463c6bde4020fb379f9945f0e09523645de Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 17 Apr 2017 11:57:37 -0500 Subject: [PATCH] Memory cleanup --- include/libp2p/net/p2pnet.h | 1 + include/libp2p/utils/logger.h | 2 ++ net/socket.c | 13 +++++++++++++ peer/peer.c | 5 +++++ record/message.c | 6 ++++-- routing/dht_protocol.c | 9 ++++++++- utils/logger.c | 8 ++++++++ utils/vector.c | 1 + 8 files changed, 42 insertions(+), 3 deletions(-) diff --git a/include/libp2p/net/p2pnet.h b/include/libp2p/net/p2pnet.h index a6b3446..9c80b45 100644 --- a/include/libp2p/net/p2pnet.h +++ b/include/libp2p/net/p2pnet.h @@ -7,6 +7,7 @@ int socket_open4(); int socket_bind4(int s, uint32_t ip, uint16_t port); int socket_bind4_reuse(int s, uint32_t ip, uint16_t port); + int socket_read_select4(int socket_fd, int num_seconds); int socket_accept4(int s, uint32_t *ip, uint16_t *port); int socket_local4(int s, uint32_t *ip, uint16_t *port); int socket_connect4(int s, uint32_t ip, uint16_t port); diff --git a/include/libp2p/utils/logger.h b/include/libp2p/utils/logger.h index d145444..6a35237 100644 --- a/include/libp2p/utils/logger.h +++ b/include/libp2p/utils/logger.h @@ -26,6 +26,8 @@ void libp2p_logger_init(); */ int libp2p_logger_initialized(); +int libp2p_logger_free(); + /** * Log a message to the console * @param area the class it is coming from diff --git a/net/socket.c b/net/socket.c index 79ceab1..8c99973 100644 --- a/net/socket.c +++ b/net/socket.c @@ -41,6 +41,19 @@ int socket_bind4_reuse(int s, uint32_t ip, uint16_t port) return socket_bind4(s, ip, port); } +int socket_read_select4(int socket_fd, int num_seconds) { + fd_set rfds; + struct timeval tv; + + FD_ZERO(&rfds); + FD_SET(socket_fd, &rfds); + + tv.tv_sec = num_seconds; + tv.tv_usec = 0; + + return select(socket_fd +1, &rfds, NULL, NULL, &tv); +} + /* Accept a connection in a socket and return ip and port of * remote connection at pointers passed as parameters. */ diff --git a/peer/peer.c b/peer/peer.c index da140ee..72a179e 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -34,6 +34,7 @@ struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress* out->id_size = strlen(id) + 1; out->id = malloc(out->id_size); strcpy(out->id, id); + free(id); } out->addr_head = libp2p_utils_linked_list_new(); out->addr_head->item = multiaddress_copy(in); @@ -100,6 +101,10 @@ void libp2p_peer_free(struct Libp2pPeer* in) { if (in != NULL) { if (in->id != NULL) free(in->id); + if (in->connection != NULL) { + libp2p_net_multistream_stream_free(in->connection); + in->connection = NULL; + } // free the memory in the linked list struct Libp2pLinkedList* current = in->addr_head; while (current != NULL) { diff --git a/record/message.c b/record/message.c index 0240ebc..0c516ef 100644 --- a/record/message.c +++ b/record/message.c @@ -33,7 +33,8 @@ void libp2p_message_free(struct Libp2pMessage* in) { struct Libp2pLinkedList* next = NULL; while (current != NULL) { next = current->next; - libp2p_peer_free(current->item); + struct Libp2pPeer* peer = (struct Libp2pPeer*)current->item; + libp2p_peer_free(peer); current->item = NULL; libp2p_utils_linked_list_free(current); current = next; @@ -43,7 +44,8 @@ void libp2p_message_free(struct Libp2pMessage* in) { current = in->provider_peer_head; while (current != NULL) { next = current->next; - libp2p_peer_free(current->item); + struct Libp2pPeer* peer = (struct Libp2pPeer*)current->item; + libp2p_peer_free(peer); current->item = NULL; libp2p_utils_linked_list_free(current); current = next; diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index 93ec4b2..f4f8311 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -184,7 +184,10 @@ int libp2p_routing_dht_handle_add_provider(struct SessionContext* session, struc char new_string[255]; multiaddress_get_ip_address(session->default_stream->address, &ip); int port = multiaddress_get_ip_port(peer_ma); - sprintf(new_string, "/ip4/%s/tcp/%d/ipfs/%s", ip, port, peer->id); + char* peer_id = multiaddress_get_peer_id(peer_ma); + sprintf(new_string, "/ip4/%s/tcp/%d/ipfs/%s", ip, port, peer_id); + free(ip); + free(peer_id); struct MultiAddress* new_ma = multiaddress_new_from_string(new_string); if (new_ma == NULL) goto exit; @@ -359,5 +362,9 @@ int libp2p_routing_dht_handle_message(struct SessionContext* session, struct Pee free(buffer); if (result_buffer != NULL) free(result_buffer); + /* JMJ Debugging + if (message != NULL) + libp2p_message_free(message); + */ return retVal; } diff --git a/utils/logger.c b/utils/logger.c index 3ccee5c..fb41592 100644 --- a/utils/logger.c +++ b/utils/logger.c @@ -31,6 +31,14 @@ int libp2p_logger_initialized() { return 1; } +int libp2p_logger_free() { + for(int i = 0; i < logger_classes->total; i++) { + free(libp2p_utils_vector_get(logger_classes, i)); + } + libp2p_utils_vector_free(logger_classes); + return 1; +} + /*** * Add a class to watch for logging messages * @param str the class name to watch diff --git a/utils/vector.c b/utils/vector.c index 20bf86d..4097237 100644 --- a/utils/vector.c +++ b/utils/vector.c @@ -76,4 +76,5 @@ void libp2p_utils_vector_delete(struct Libp2pVector *v, int index) void libp2p_utils_vector_free(struct Libp2pVector *v) { free(v->items); + free(v); }