diff --git a/include/libp2p/secio/secio.h b/include/libp2p/secio/secio.h index 5c3f3e6..73b0c7b 100644 --- a/include/libp2p/secio/secio.h +++ b/include/libp2p/secio/secio.h @@ -22,3 +22,13 @@ struct Libp2pProtocolHandler* libp2p_secio_build_protocol_handler(struct RsaPriv * @returns true(1) on success, false(0) otherwise */ int libp2p_secio_handshake(struct SessionContext* session, struct RsaPrivateKey* private_key, struct Peerstore* peerstore); + +/*** + * Initiates a secio handshake. Use this method when you want to initiate a secio + * session. This should not be used to respond to incoming secio requests + * @param session_context the session context + * @param private_key the RSA private key to use + * @param peer_store the peer store + * @returns true(1) on success, false(0) otherwise + */ +int libp2p_secio_initiate_handshake(struct SessionContext* session_context, struct RsaPrivateKey* private_key, struct Peerstore* peer_store); diff --git a/peer/peer.c b/peer/peer.c index 09cc4b9..b950a0a 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -117,7 +117,10 @@ int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* pee peer->sessionContext->default_stream = peer->sessionContext->insecure_stream; peer->connection_type = CONNECTION_TYPE_CONNECTED; } - libp2p_secio_handshake(peer->sessionContext, privateKey, peerstore); + if (libp2p_secio_initiate_handshake(peer->sessionContext, privateKey, peerstore) <= 0) { + free(ip); + return 0; + } free(ip); } // is IP now = time(NULL); diff --git a/secio/secio.c b/secio/secio.c index c095337..adf4942 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -58,6 +58,24 @@ int libp2p_secio_shutdown(void* context) { return 1; } +/*** + * Initiates a secio handshake. Use this method when you want to initiate a secio + * session. This should not be used to respond to incoming secio requests + * @param session_context the session context + * @param private_key the RSA private key to use + * @param peer_store the peer store + * @returns true(1) on success, false(0) otherwise + */ +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"; + int protocol_len = strlen((char*)protocol); + if (!session_context->default_stream->write(session_context, protocol, protocol_len)) + return 0; + return libp2p_secio_handshake(session_context, private_key, peer_store); + +} + struct Libp2pProtocolHandler* libp2p_secio_build_protocol_handler(struct RsaPrivateKey* private_key, struct Peerstore* peer_store) { struct Libp2pProtocolHandler* handler = (struct Libp2pProtocolHandler*) malloc(sizeof(struct Libp2pProtocolHandler)); if (handler != NULL) { @@ -851,12 +869,6 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva if (libp2p_secio_propose_protobuf_encode(propose_out, propose_out_bytes, propose_out_size, &propose_out_size) == 0) goto exit; - // send the protocol id first - const unsigned char* protocol = (unsigned char*)"/secio/1.0.0\n"; - int protocol_len = strlen((char*)protocol); - if (!local_session->default_stream->write(local_session, protocol, protocol_len)) - goto exit; - // now send the Propose struct bytes_written = libp2p_secio_unencrypted_write(local_session, propose_out_bytes, propose_out_size); if (bytes_written != propose_out_size) {