From f3db50c3ba2ae2887523525135de307bdb7d503e Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 27 Nov 2017 09:06:57 -0500 Subject: [PATCH] debugging yamux and go --- .cproject | 29 ++++++++++++++++++++++++++--- core/http_request.c | 10 +++++++++- core/null.c | 36 +++++++++++++++++++++++++++--------- exchange/bitswap/bitswap.c | 2 ++ journal/journal.c | 2 ++ test/Makefile | 2 ++ test/core/test_compat_go.h | 13 ++++++++++++- 7 files changed, 80 insertions(+), 14 deletions(-) diff --git a/.cproject b/.cproject index c5f1416..3281213 100644 --- a/.cproject +++ b/.cproject @@ -81,7 +81,6 @@ make - all true true @@ -89,7 +88,6 @@ make - clean true true @@ -97,7 +95,6 @@ make - rebuild true true @@ -105,13 +102,39 @@ make + clean true false true + + make + + all + true + true + true + + + make + + clean + true + true + true + + + make + + rebuild + true + true + true + make + clean true false diff --git a/core/http_request.c b/core/http_request.c index dd5cfd3..f75db55 100644 --- a/core/http_request.c +++ b/core/http_request.c @@ -312,7 +312,15 @@ int ipfs_core_http_process_swarm_connect(struct IpfsNode* local_node, struct Htt multiaddress_free(ma); return 0; } - // ok, we're good. Send stuff back to the user + // ok, we're good. Start a thread to listen to them, and send stuff back to the user + // JMJ Debug - just put a loop here for testing + while(1) { + int retVal = ipfs_null_listen_and_handle(new_peer->sessionContext->default_stream, local_node->protocol_handlers); + if (retVal < 0) + break; + else + sleep(1); + } *resp = ipfs_core_http_response_new(); struct HttpResponse* response = *resp; if (response == NULL) { diff --git a/core/null.c b/core/null.c index b748b88..2a7052e 100644 --- a/core/null.c +++ b/core/null.c @@ -34,6 +34,31 @@ static int null_shutting_down = 0; +/*** + * Listens on a particular stream, and marshals the request + * @param stream the stream to listen to + * @param protocol_handlers a vector of protocol handlers + * @returns <0 on error, 0 if we shouldn't handle this anymore, or 1 on success + */ +int ipfs_null_listen_and_handle(struct Stream* stream, struct Libp2pVector* protocol_handlers) { + struct StreamMessage* results = NULL; + int retVal = 0; + // Read from the network + if (!stream->read(stream->stream_context, &results, DEFAULT_NETWORK_TIMEOUT)) { + libp2p_logger_error("null", "Unable to read from network. Exiting.\n"); + return retVal; + } + if (results != NULL) { + libp2p_logger_debug("null", "Attempting to marshal %d bytes from network.\n", results->data_size); + retVal = libp2p_protocol_marshal(results, stream, protocol_handlers); + libp2p_logger_debug("null", "The return value from the attempt to marshal %d bytes was %d.\n", results->data_size, retVal); + libp2p_stream_message_free(results); + } else { + libp2p_logger_debug("null", "Attempted read, but results were null. This is normal.\n"); + } + return retVal; +} + /** * We've received a new connection. Find out what they want. * @@ -59,20 +84,13 @@ void ipfs_null_connection (void *ptr) { libp2p_logger_info("null", "Connection %d, count %d\n", connection_param->file_descriptor, *(connection_param->count)); // try to read from the network - struct StreamMessage *results = 0; // handle the call for(;;) { // Read from the network - if (!session->default_stream->read(session->default_stream->stream_context, &results, DEFAULT_NETWORK_TIMEOUT)) { - // problem reading - break; - } - if (results != NULL) { - retVal = libp2p_protocol_marshal(results, session->default_stream, connection_param->local_node->protocol_handlers); - libp2p_stream_message_free(results); - } + retVal = ipfs_null_listen_and_handle(session->default_stream, connection_param->local_node->protocol_handlers); if (retVal < 0) { // exit the loop on error + libp2p_logger_debug("null", "Exiting loop due to retVal being %d.\n", retVal); break; } } // end of loop diff --git a/exchange/bitswap/bitswap.c b/exchange/bitswap/bitswap.c index 01fb7ff..7ff5e6d 100644 --- a/exchange/bitswap/bitswap.c +++ b/exchange/bitswap/bitswap.c @@ -18,6 +18,8 @@ #include "ipfs/exchange/bitswap/want_manager.h" int ipfs_bitswap_can_handle(const struct StreamMessage* msg) { + if (msg == NULL || msg->data == NULL || msg->data_size == 0) + return 0; char* result = strnstr((char*)msg->data, "/ipfs/bitswap", msg->data_size); if(result == NULL || result != (char*)msg->data) return 0; diff --git a/journal/journal.c b/journal/journal.c index c1ec933..bc6e78f 100644 --- a/journal/journal.c +++ b/journal/journal.c @@ -19,6 +19,8 @@ * @returns true(1) if the protocol in incoming is something we can handle. False(0) otherwise. */ int ipfs_journal_can_handle(const struct StreamMessage* msg) { + if (msg == NULL || msg->data_size == 0 || msg->data == NULL) + return 0; const char* protocol = "/ipfs/journalio/1.0.0"; if (msg->data_size < 21) return 0; diff --git a/test/Makefile b/test/Makefile index 0f16944..7e2dd66 100644 --- a/test/Makefile +++ b/test/Makefile @@ -43,3 +43,5 @@ all: test_ipfs clean: rm -f *.o rm -f test_ipfs + +rebuild: clean all diff --git a/test/core/test_compat_go.h b/test/core/test_compat_go.h index 222e64f..2e12661 100644 --- a/test/core/test_compat_go.h +++ b/test/core/test_compat_go.h @@ -15,6 +15,11 @@ int test_compat_go_join_swarm() { pthread_t daemon_thread; struct FSRepo* fs_repo = NULL; + libp2p_logger_add_class("test_api"); + libp2p_logger_add_class("yamux"); + libp2p_logger_add_class("identify"); + libp2p_logger_add_class("null"); + // Here is the connection information for the GO version: char* remote_string = "/ip4/10.211.55.2/tcp/4001/ipfs/QmacSE6bCZiAu7nrYkhPATaSoL2q9BszkKzbX6fCiXuBGA"; @@ -32,7 +37,13 @@ int test_compat_go_join_swarm() { // try to connect to a remote swarm struct IpfsNode *local_node = NULL; ipfs_node_offline_new(ipfs_path1, &local_node); - ipfs_swarm_connect(local_node, remote_string); + if (!ipfs_swarm_connect(local_node, remote_string)) { + libp2p_logger_error("test_api", "Unable to do swarm connect.\n"); + goto exit; + } + + // see what the remote will do + sleep(20); retVal = 1; exit: