From 56e301df8d9c71859da671aea868b36c1cd9b521 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Thu, 12 Oct 2017 10:12:22 -0500 Subject: [PATCH] Handle swarm connect --- include/libp2p/yamux/yamux.h | 7 +++++++ peer/peer.c | 10 ++++++++++ swarm/swarm.c | 1 + yamux/yamux.c | 13 +++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 swarm/swarm.c diff --git a/include/libp2p/yamux/yamux.h b/include/libp2p/yamux/yamux.h index 0001a36..5a204fb 100644 --- a/include/libp2p/yamux/yamux.h +++ b/include/libp2p/yamux/yamux.h @@ -6,3 +6,10 @@ * Build a handler that can handle the yamux protocol */ struct Libp2pProtocolHandler* yamux_build_protocol_handler(); +/*** + * Send the yamux protocol out the default stream + * NOTE: if we initiate the connection, we should expect the same back + * @param context the SessionContext + * @returns true(1) on success, false(0) otherwise + */ +int yamux_send_protocol(struct SessionContext* context); diff --git a/peer/peer.c b/peer/peer.c index 80c63a4..2a7cdff 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -8,6 +8,7 @@ #include "libp2p/secio/secio.h" #include "libp2p/utils/linked_list.h" #include "libp2p/utils/logger.h" +#include "libp2p/yamux/yamux.h" /** * create a new Peer struct @@ -116,6 +117,8 @@ int libp2p_peer_connect(const struct RsaPrivateKey* privateKey, struct Libp2pPee libp2p_session_context_free(peer->sessionContext); } peer->sessionContext = libp2p_session_context_new(); + peer->sessionContext->host = ip; + peer->sessionContext->port = port; peer->sessionContext->datastore = datastore; peer->sessionContext->insecure_stream = libp2p_net_multistream_connect_with_timeout(ip, port, timeout); if (peer->sessionContext->insecure_stream == NULL) { @@ -127,11 +130,18 @@ int libp2p_peer_connect(const struct RsaPrivateKey* privateKey, struct Libp2pPee peer->sessionContext->default_stream = peer->sessionContext->insecure_stream; peer->connection_type = CONNECTION_TYPE_CONNECTED; } + // switch to secio if (libp2p_secio_initiate_handshake(peer->sessionContext, privateKey, peerstore) <= 0) { libp2p_logger_error("peer", "Attempted secio handshake, but failed for peer %s.\n", libp2p_peer_id_to_string(peer)); free(ip); return 0; } + // switch to yamux + if (!yamux_send_protocol(peer->sessionContext)) { + libp2p_logger_error("peer", "Attempted yamux handshake, but could not send protocol header for peer %s.\n", libp2p_peer_id_to_string(peer)); + free(ip); + return 0; + } free(ip); } // is IP now = time(NULL); diff --git a/swarm/swarm.c b/swarm/swarm.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/swarm/swarm.c @@ -0,0 +1 @@ + diff --git a/yamux/yamux.c b/yamux/yamux.c index dda9fb6..c2dddd5 100644 --- a/yamux/yamux.c +++ b/yamux/yamux.c @@ -50,6 +50,19 @@ void yamux_read_stream(struct yamux_stream* stream, ssize_t incoming_size, uint8 return; } +/*** + * Send the yamux protocol out the default stream + * NOTE: if we initiate the connection, we should expect the same back + * @param context the SessionContext + * @returns true(1) on success, false(0) otherwise + */ +int yamux_send_protocol(struct SessionContext* context) { + char* protocol = "/yamux/1.0.0\n"; + if (!context->default_stream->write(context, (uint8_t*)protocol, strlen(protocol))) + return 0; + return 1; +} + /*** * Handles the message * @param incoming the incoming data buffer