From f4860d3ed47c4434b8c471e39c2dee0581e11c61 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 6 Nov 2017 17:31:30 -0500 Subject: [PATCH] setting up when the remote requests the yamux protocol --- identify/identify.c | 7 ++++-- include/libp2p/net/protocol.h | 9 +++++++- net/protocol.c | 4 +--- test/test_yamux.h | 42 ++++++++++++++++++++++++++++++++++- test/testit.c | 1 + 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/identify/identify.c b/identify/identify.c index cb69d4b..079a31d 100644 --- a/identify/identify.c +++ b/identify/identify.c @@ -83,8 +83,11 @@ int libp2p_identify_receive_protocol(struct IdentifyContext* context) { int libp2p_identify_handle_message(const struct StreamMessage* msg, struct SessionContext* context, void* protocol_context) { if (protocol_context == NULL) return -1; - //struct IdentifyContext* ctx = (struct IdentifyContext*) protocol_context; - // TODO: Do something with the incoming msg + // attempt to create a new Identify connection with them. + // send the protocol id back, and set up the channel + struct IdentifyContext* ctx = (struct IdentifyContext*)protocol_context; + libp2p_identify_send_protocol(ctx); + //TODO: now add this "channel" return 1; } diff --git a/include/libp2p/net/protocol.h b/include/libp2p/net/protocol.h index 440d366..a9992fd 100644 --- a/include/libp2p/net/protocol.h +++ b/include/libp2p/net/protocol.h @@ -42,4 +42,11 @@ struct Libp2pProtocolHandler { */ struct Libp2pProtocolHandler* libp2p_protocol_handler_new(); -int libp2p_protocol_marshal(struct StreamMessage* msg, struct SessionContext* context, struct Libp2pVector* protocol_handlers); +/*** + * Handle an incoming message + * @param message the incoming message + * @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(struct StreamMessage* message, struct SessionContext* context, struct Libp2pVector* protocol_handlers); diff --git a/net/protocol.c b/net/protocol.c index 9369a4d..148d090 100644 --- a/net/protocol.c +++ b/net/protocol.c @@ -41,8 +41,7 @@ struct Libp2pProtocolHandler* libp2p_protocol_handler_new() { /*** * Handle an incoming message - * @param incoming the incoming data - * @param incoming_size the size of the incoming data buffer + * @param message the incoming message * @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 @@ -65,6 +64,5 @@ int libp2p_protocol_marshal(struct StreamMessage* msg, struct SessionContext* se return -1; } - //TODO: strip off the protocol? return handler->HandleMessage(msg, session, handler->context); } diff --git a/test/test_yamux.h b/test/test_yamux.h index 5d5a90c..e0f0c9a 100644 --- a/test/test_yamux.h +++ b/test/test_yamux.h @@ -2,6 +2,7 @@ #include "libp2p/yamux/yamux.h" #include "libp2p/identify/identify.h" #include "mock_stream.h" +#include "libp2p/utils/logger.h" /*** * Helpers @@ -68,7 +69,7 @@ int test_yamux_identify() { struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream); if (yamux_stream == NULL) goto exit; - // TODO: Now add in another protocol + // Now add in another protocol mock_stream->read = mock_identify_read_protocol; if (!libp2p_yamux_stream_add(yamux_stream->stream_context, libp2p_identify_stream_new(libp2p_yamux_channel_new(yamux_stream)))) { goto exit; @@ -81,3 +82,42 @@ int test_yamux_identify() { mock_stream->close(mock_stream->stream_context); return retVal; } + +int test_yamux_incoming_protocol_request() { + int retVal = 0; + // setup + struct Stream* mock_stream = mock_stream_new(); + mock_stream->read = mock_yamux_read_protocol; + struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream); + if (yamux_stream == NULL) + goto exit; + // build the protocol handler that can handle identify protocol + struct Libp2pVector* protocol_handlers = libp2p_utils_vector_new(1); + struct Libp2pProtocolHandler* handler = libp2p_identify_build_protocol_handler(protocol_handlers); + libp2p_utils_vector_add(protocol_handlers, handler); + struct SessionContext session_context; + session_context.default_stream = yamux_stream; + // Someone is requesting the identity protocol + mock_stream->read = mock_identify_read_protocol; + struct StreamMessage* result_message; + if (!yamux_stream->read(yamux_stream->stream_context, &result_message, 10)) { + libp2p_logger_error("test_yamux", "Unable to read identify protocol.\n"); + goto exit; + } + // handle the marshaling of the protocol + libp2p_protocol_marshal(result_message, &session_context, protocol_handlers); + // now verify the results + struct YamuxContext* yamux_context = (struct YamuxContext*)yamux_stream->stream_context; + if (yamux_context->channels->total != 1) { + libp2p_logger_error("test_yamux", "Identify protocol was not found.\n"); + goto exit; + } + + // tear down + retVal = 1; + exit: + if (yamux_stream != NULL) + yamux_stream->close(yamux_stream->stream_context); + mock_stream->close(mock_stream->stream_context); + return retVal; +} diff --git a/test/testit.c b/test/testit.c index 6e4a0df..bcf995f 100644 --- a/test/testit.c +++ b/test/testit.c @@ -116,6 +116,7 @@ int build_test_collection() { add_test("test_aes", test_aes, 1); add_test("test_yamux_stream_new", test_yamux_stream_new, 1); add_test("test_yamux_identify", test_yamux_identify, 1); + add_test("test_yamux_incoming_protocol_request", test_yamux_incoming_protocol_request, 1); return 1; };