Handle swarm connect

This commit is contained in:
jmjatlanta 2017-10-12 10:12:22 -05:00
parent 1565b78fcd
commit 56e301df8d
4 changed files with 31 additions and 0 deletions

View file

@ -6,3 +6,10 @@
* Build a handler that can handle the yamux protocol * Build a handler that can handle the yamux protocol
*/ */
struct Libp2pProtocolHandler* yamux_build_protocol_handler(); 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);

View file

@ -8,6 +8,7 @@
#include "libp2p/secio/secio.h" #include "libp2p/secio/secio.h"
#include "libp2p/utils/linked_list.h" #include "libp2p/utils/linked_list.h"
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
#include "libp2p/yamux/yamux.h"
/** /**
* create a new Peer struct * 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); libp2p_session_context_free(peer->sessionContext);
} }
peer->sessionContext = libp2p_session_context_new(); peer->sessionContext = libp2p_session_context_new();
peer->sessionContext->host = ip;
peer->sessionContext->port = port;
peer->sessionContext->datastore = datastore; peer->sessionContext->datastore = datastore;
peer->sessionContext->insecure_stream = libp2p_net_multistream_connect_with_timeout(ip, port, timeout); peer->sessionContext->insecure_stream = libp2p_net_multistream_connect_with_timeout(ip, port, timeout);
if (peer->sessionContext->insecure_stream == NULL) { 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->sessionContext->default_stream = peer->sessionContext->insecure_stream;
peer->connection_type = CONNECTION_TYPE_CONNECTED; peer->connection_type = CONNECTION_TYPE_CONNECTED;
} }
// switch to secio
if (libp2p_secio_initiate_handshake(peer->sessionContext, privateKey, peerstore) <= 0) { 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)); libp2p_logger_error("peer", "Attempted secio handshake, but failed for peer %s.\n", libp2p_peer_id_to_string(peer));
free(ip); free(ip);
return 0; 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); free(ip);
} // is IP } // is IP
now = time(NULL); now = time(NULL);

1
swarm/swarm.c Normal file
View file

@ -0,0 +1 @@

View file

@ -50,6 +50,19 @@ void yamux_read_stream(struct yamux_stream* stream, ssize_t incoming_size, uint8
return; 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 * Handles the message
* @param incoming the incoming data buffer * @param incoming the incoming data buffer