From f4dd9b46bee21e513c779cdec71e164306b533e0 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 28 Sep 2017 17:14:37 -0500 Subject: [PATCH] No longer deleting session context when deleting message --- conn/session.c | 24 ++++++++++++++++++++++++ include/libp2p/conn/session.h | 2 ++ record/message.c | 2 ++ 3 files changed, 28 insertions(+) diff --git a/conn/session.c b/conn/session.c index afbfaa8..43f8742 100644 --- a/conn/session.c +++ b/conn/session.c @@ -90,6 +90,29 @@ int libp2p_session_context_free(struct SessionContext* context) { return 1; } +/**** + * Make a copy of a SessionContext + * @param original the original + * @returns a copy of the original, or NULL on error + */ +struct SessionContext* libp2p_session_context_copy(const struct SessionContext* original) { + struct SessionContext* new_ctx = libp2p_session_context_new(); + if (new_ctx != NULL) { + new_ctx->aes_decode_nonce_offset = original->aes_decode_nonce_offset; + memcpy(new_ctx->aes_decode_stream_block, original->aes_decode_stream_block, 16); + new_ctx->aes_encode_nonce_offset = original->aes_encode_nonce_offset; + memcpy(new_ctx->aes_encode_stream_block, original->aes_encode_stream_block, 16); + new_ctx->chosen_cipher = (char*) malloc(strlen(original->chosen_cipher) + 1); + strcpy(new_ctx->chosen_cipher, original->chosen_cipher); + new_ctx->chosen_curve = (char*) malloc(strlen(original->chosen_curve) + 1); + strcpy(new_ctx->chosen_curve, original->chosen_curve); + new_ctx->chosen_hash = (char*) malloc(strlen(original->chosen_hash) + 1); + strcpy(new_ctx->chosen_hash, original->chosen_hash); + // TODO: Copy everything else + } + return new_ctx; +} + int libp2p_session_context_compare_streams(const struct Stream* a, const struct Stream* b) { if (a == NULL && b == NULL) return 0; @@ -134,3 +157,4 @@ int libp2p_session_context_compare(const struct SessionContext* a, const struct total = libp2p_session_context_compare_remote_key(&a->remote_key, &b->remote_key); return total; } + diff --git a/include/libp2p/conn/session.h b/include/libp2p/conn/session.h index 6fdc56b..d6ffa59 100644 --- a/include/libp2p/conn/session.h +++ b/include/libp2p/conn/session.h @@ -83,3 +83,5 @@ int libp2p_session_context_free(struct SessionContext* session); * @returns 0 if equal, <0 if A wins, >0 if B wins */ int libp2p_session_context_compare(const struct SessionContext* a, const struct SessionContext* b); + +struct SessionContext* libp2p_session_context_copy(const struct SessionContext* original); diff --git a/record/message.c b/record/message.c index 0980872..f3aeac7 100644 --- a/record/message.c +++ b/record/message.c @@ -53,6 +53,8 @@ void libp2p_message_free(struct KademliaMessage* in) { while (current != NULL) { next = current->next; struct Libp2pPeer* peer = (struct Libp2pPeer*)current->item; + // don't delete the session context + peer->sessionContext = NULL; libp2p_peer_free(peer); current->item = NULL; libp2p_utils_linked_list_free(current);