From eecfea4f7850c7398464abe8fe2759fba401bc69 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Thu, 31 Aug 2017 06:41:06 -0500 Subject: [PATCH] journalio working, needs tuning --- conn/session.c | 1 + include/libp2p/conn/session.h | 1 + include/libp2p/crypto/encoding/base58.h | 1 + net/multistream.c | 5 +++++ net/protocol.c | 14 +++++++++++--- routing/dht_protocol.c | 10 +++++++++- secio/secio.c | 2 +- 7 files changed, 29 insertions(+), 5 deletions(-) diff --git a/conn/session.c b/conn/session.c index 3434672..afbfaa8 100644 --- a/conn/session.c +++ b/conn/session.c @@ -37,6 +37,7 @@ struct SessionContext* libp2p_session_context_new() { context->shared_key = NULL; context->shared_key_size = 0; context->traffic_type = TCP; + context->last_comm_epoch = 0; } return context; } diff --git a/include/libp2p/conn/session.h b/include/libp2p/conn/session.h index 208deff..6fdc56b 100644 --- a/include/libp2p/conn/session.h +++ b/include/libp2p/conn/session.h @@ -61,6 +61,7 @@ struct SessionContext { struct StretchedKey* remote_stretched_key; unsigned char* remote_ephemeral_public_key; size_t remote_ephemeral_public_key_size; + unsigned long long last_comm_epoch; }; /*** diff --git a/include/libp2p/crypto/encoding/base58.h b/include/libp2p/crypto/encoding/base58.h index 228b42b..800dabc 100644 --- a/include/libp2p/crypto/encoding/base58.h +++ b/include/libp2p/crypto/encoding/base58.h @@ -1,6 +1,7 @@ #ifndef base58_h #define base58_h +#include /** * convert a base58 encoded string into a binary array * @param base58 the base58 encoded string diff --git a/net/multistream.c b/net/multistream.c index 823f763..9cee294 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -6,6 +6,8 @@ #include #include #include + +#include "libp2p/os/utils.h" #include "libp2p/net/p2pnet.h" #include "libp2p/record/message.h" #include "libp2p/secio/secio.h" @@ -107,6 +109,7 @@ int libp2p_net_multistream_write(void* stream_context, const unsigned char* data } // then send the actual data num_bytes += socket_write(sd, (char*)data, data_length, 0); + session_context->last_comm_epoch = os_utils_gmtime(); } else { // write using secio num_bytes = stream->write(stream_context, buffer, data_length + varint_size); @@ -186,6 +189,7 @@ int libp2p_net_multistream_read(void* stream_context, unsigned char** results, s return 0; memcpy(*results, buffer, num_bytes_requested); *results_size = num_bytes_requested; + session_context->last_comm_epoch = os_utils_gmtime(); } else { // use secio instead of raw read/writes unsigned char* read_from_stream; size_t size_read_from_stream; @@ -213,6 +217,7 @@ int libp2p_net_multistream_read(void* stream_context, unsigned char** results, s return 0; } memcpy(*results, &buffer[left], num_bytes_requested); + session_context->last_comm_epoch = os_utils_gmtime(); } return num_bytes_requested; diff --git a/net/protocol.c b/net/protocol.c index b83f59f..f7b0846 100644 --- a/net/protocol.c +++ b/net/protocol.c @@ -30,12 +30,20 @@ const struct Libp2pProtocolHandler* protocol_compare(const unsigned char* incomi */ int libp2p_protocol_marshal(const unsigned char* incoming, size_t incoming_size, struct SessionContext* session, struct Libp2pVector* handlers) { const struct Libp2pProtocolHandler* handler = protocol_compare(incoming, incoming_size, handlers); + char str[incoming_size + 1]; + memcpy(str, incoming, incoming_size); + str[incoming_size] = 0; + for(int i = 0; i < incoming_size; i++) { + if (str[i] == '\n') { + str[i] = 0; + break; + } + } if (handler == NULL) { - char str[incoming_size + 1]; - memcpy(str, incoming, incoming_size); - str[incoming_size] = 0; libp2p_logger_error("protocol", "Unable to find handler for %s.\n", str); return -1; + } else { + libp2p_logger_debug("protocol", "Found handler for %s.\n", str); } //TODO: strip off the protocol? return handler->HandleMessage(incoming, incoming_size, session, handler->context); diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index 5551d18..dbaea27 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -1,6 +1,7 @@ #include #include +#include "libp2p/crypto/encoding/base58.h" #include "libp2p/net/stream.h" #include "libp2p/routing/dht_protocol.h" #include "libp2p/record/message.h" @@ -175,7 +176,14 @@ int libp2p_routing_dht_handle_get_providers(struct SessionContext* session, stru } } } else { - libp2p_logger_debug("dht_protocol", "I cannot provide a provider for this key.\n"); + size_t b58_size = 100; + uint8_t *b58key = (uint8_t *) malloc(b58_size); + if (!libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**)&b58key, &b58_size)) { + libp2p_logger_debug("dht_protocol", "I cannot provide a provider for this key.\n"); + } else { + libp2p_logger_debug("dht_protocol", "I cannot provide a provider for the key %s.\n", b58key); + } + free(b58key); } if (peer_id != NULL) free(peer_id); diff --git a/secio/secio.c b/secio/secio.c index 1c34f07..8e05165 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -54,7 +54,7 @@ int libp2p_secio_can_handle(const uint8_t* incoming, size_t incoming_size) { * @param incoming_size the size of the incoming buffer * @param session_context who is attempting to connect * @param protocol_context a SecioContext that contains the needed information - * @returns <0 on error, 0 if okay + * @returns <0 on error, 0 if okay (does not allow daemon to continue looping) */ int libp2p_secio_handle_message(const uint8_t* incoming, size_t incoming_size, struct SessionContext* session_context, void* protocol_context) { libp2p_logger_debug("secio", "Handling incoming secio message.\n");