From a00967396936e3fa3fdf1cbdf2ba8dd1745eb18b Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 9 Aug 2017 12:08:57 -0500 Subject: [PATCH] Added some debugging information to protocols --- net/protocol.c | 10 +++++++++- routing/dht_protocol.c | 11 ++++++----- secio/secio.c | 3 ++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/net/protocol.c b/net/protocol.c index 1fb15ad..06da022 100644 --- a/net/protocol.c +++ b/net/protocol.c @@ -18,10 +18,18 @@ const struct Libp2pProtocolHandler* protocol_compare(const unsigned char* incomi return NULL; } +/*** + * Handle an incoming message + * @param incoming the incoming data + * @param incoming_size the size of the incoming data buffer + * @param session the SessionContext of the incoming connection + * @param handlers a Vector of protocol handlers + * @returns -1 on error, 0 if everything was okay, but the daemon should no longer handle this connection, 1 on success + */ 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); if (handler != NULL) { return handler->HandleMessage(incoming, incoming_size, session, handler->context); } - return 0; + return -1; } diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index c02e115..c83c12e 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -22,9 +22,9 @@ int libp2p_routing_dht_can_handle(const uint8_t* incoming, size_t incoming_size) if (incoming_size < 8) return 0; char* result = strstr((char*)incoming, "/ipfs/kad"); - if (result == NULL || result != (char*)incoming) - return 0; - return 1; + if (result != NULL && result == (char*)incoming) + return 1; + return 0; } int libp2p_routing_dht_shutdown(void* context) { @@ -33,10 +33,11 @@ int libp2p_routing_dht_shutdown(void* context) { } int libp2p_routing_dht_handle_msg(const uint8_t* incoming, size_t incoming_size, struct SessionContext* session_context, void* context) { + libp2p_logger_debug("dht_routing", "Handling incoming dht routing request.\n"); struct DhtContext* ctx = (struct DhtContext*)context; if (!libp2p_routing_dht_handshake(session_context)) - return 0; - return libp2p_routing_dht_handle_message(session_context, ctx->peer_store, ctx->provider_store); + return -1; + return (libp2p_routing_dht_handle_message(session_context, ctx->peer_store, ctx->provider_store) == 0) ? -1 : 1; } struct Libp2pProtocolHandler* libp2p_routing_dht_build_protocol_handler(struct Peerstore* peer_store, struct ProviderStore* provider_store) { diff --git a/secio/secio.c b/secio/secio.c index d88905a..1d5e9ad 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -57,6 +57,7 @@ int libp2p_secio_can_handle(const uint8_t* incoming, size_t incoming_size) { * @returns <0 on error, 0 if okay */ 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"); struct SecioContext* ctx = (struct SecioContext*)protocol_context; int retVal = libp2p_secio_handshake(session_context, ctx->private_key, ctx->peer_store); if (retVal) @@ -79,7 +80,7 @@ int libp2p_secio_shutdown(void* context) { */ int libp2p_secio_initiate_handshake(struct SessionContext* session_context, struct RsaPrivateKey* private_key, struct Peerstore* peer_store) { // send the protocol id first - const unsigned char* protocol = (unsigned char*)"/secio/1.0.0\n"; + const unsigned char* protocol = (unsigned char*)"/ipfs/secio/1.0.0\n"; int protocol_len = strlen((char*)protocol); if (!session_context->default_stream->write(session_context, protocol, protocol_len)) return 0;