From 3137d9efa6261fbabeae2837cc2ce15cf575a709 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Mon, 31 Jul 2017 16:36:08 -0500 Subject: [PATCH] Now a successful secio connection adds the peer to the peerstore --- include/libp2p/peer/peer.h | 8 ++++++-- include/libp2p/secio/secio.h | 3 ++- peer/peer.c | 7 +++++-- secio/Makefile | 2 +- secio/secio.c | 20 +++++++++++++++++++- test/test_secio.h | 2 +- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/include/libp2p/peer/peer.h b/include/libp2p/peer/peer.h index 9373355..10023da 100644 --- a/include/libp2p/peer/peer.h +++ b/include/libp2p/peer/peer.h @@ -4,6 +4,8 @@ #include "libp2p/net/stream.h" #include "libp2p/crypto/rsa.h" +struct Peerstore; + enum ConnectionType { // sender does not have a connection to the peer, and no extra information (default) CONNECTION_TYPE_NOT_CONNECTED = 0, @@ -46,11 +48,13 @@ void libp2p_peer_free(struct Libp2pPeer* in); /** * Attempt to connect to the peer, setting connection_type correctly * NOTE: If successful, this will set peer->connection to the stream - * @param privateKey our private key + * + * @param privateKey the local private key to use * @param peer the peer to connect to + * @param peerstore if connection is successfull, will add peer to peerstore * @returns true(1) on success, false(0) if we could not connect */ -int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* peer, int timeout); +int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* peer, struct Peerstore* peerstore, int timeout); /** * Make a copy of a peer diff --git a/include/libp2p/secio/secio.h b/include/libp2p/secio/secio.h index ac582e5..be1b701 100644 --- a/include/libp2p/secio/secio.h +++ b/include/libp2p/secio/secio.h @@ -3,6 +3,7 @@ #include "libp2p/crypto/key.h" #include "libp2p/crypto/rsa.h" #include "libp2p/conn/session.h" +#include "libp2p/peer/peerstore.h" /** * Handling of a secure connection @@ -17,4 +18,4 @@ * @param remote_requested the other side is who asked for the upgrade * @returns true(1) on success, false(0) otherwise */ -int libp2p_secio_handshake(struct SessionContext* session, struct RsaPrivateKey* private_key, int remote_requested); +int libp2p_secio_handshake(struct SessionContext* session, struct RsaPrivateKey* private_key, struct Peerstore* peerstore, int remote_requested); diff --git a/peer/peer.c b/peer/peer.c index f545d22..4e41228 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -78,10 +78,13 @@ void libp2p_peer_free(struct Libp2pPeer* in) { /** * Attempt to connect to the peer, setting connection_type correctly * NOTE: If successful, this will set peer->connection to the stream + * + * @param privateKey the local private key to use * @param peer the peer to connect to + * @param peerstore if connection is successfull, will add peer to peerstore * @returns true(1) on success, false(0) if we could not connect */ -int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* peer, int timeout) { +int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* peer, struct Peerstore* peerstore, int timeout) { time_t now, prev = time(NULL); // find an appropriate address struct Libp2pLinkedList* current_address = peer->addr_head; @@ -102,7 +105,7 @@ 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, 0); + libp2p_secio_handshake(peer->sessionContext, privateKey, peerstore, 0); free(ip); } // is IP now = time(NULL); diff --git a/secio/Makefile b/secio/Makefile index 31bb4fc..8542bbd 100644 --- a/secio/Makefile +++ b/secio/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -O0 -Wall -I../include -I../../c-protobuf -std=c99 +CFLAGS = -O0 -Wall -I../include -I../../c-protobuf -I../../c-multiaddr/include -std=c99 ifdef DEBUG CFLAGS += -g3 diff --git a/secio/secio.c b/secio/secio.c index a6ea9ab..75e6818 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -761,7 +761,7 @@ int libp2p_secio_encrypted_read(void* stream_context, unsigned char** bytes, siz * @param remote_requested it is the other side that requested the upgrade to secio * @returns true(1) on success, false(0) otherwise */ -int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPrivateKey* private_key, int remote_requested) { +int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPrivateKey* private_key, struct Peerstore* peerstore, int remote_requested) { int retVal = 0; size_t results_size = 0, bytes_written = 0; unsigned char* propose_in_bytes = NULL; // the remote protobuf @@ -782,6 +782,10 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva struct StretchedKey* k1 = NULL, *k2 = NULL; struct PrivateKey* priv = NULL; struct PublicKey pub_key = {0}; + struct Libp2pPeer* remote_peer = libp2p_peer_new(); + + remote_peer->sessionContext = local_session; + remote_peer->connection_type = CONNECTION_TYPE_CONNECTED; //TODO: make sure we're not talking to ourself @@ -880,6 +884,16 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva // generate their peer id libp2p_crypto_public_key_to_peer_id(public_key, &local_session->remote_peer_id); + // put peer information in Libp2pPeer struct + remote_peer->id_size = strlen(local_session->remote_peer_id); + if (remote_peer->id_size > 0) { + remote_peer->id = malloc(remote_peer->id_size + 1); + if (remote_peer->id != NULL) { + memcpy(remote_peer->id, local_session->remote_peer_id, remote_peer->id_size); + remote_peer->id[remote_peer->id_size] = 0; + } + } + // negotiate encryption parameters NOTE: SelectBest must match, otherwise this won't work // first determine order order = libp2p_secio_determine_order(propose_in, propose_out); @@ -1072,8 +1086,12 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva if (retVal == 1) { libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake success!\n"); + // add this to the peerstore + if (peerstore != NULL) + libp2p_peerstore_add_peer(peerstore, remote_peer); } else { libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake returning false\n"); + libp2p_peer_free(remote_peer); } return retVal; } diff --git a/test/test_secio.h b/test/test_secio.h index fb56232..af7bb02 100644 --- a/test/test_secio.h +++ b/test/test_secio.h @@ -80,7 +80,7 @@ int test_secio_handshake() { goto exit; } - if (!libp2p_secio_handshake(&secure_session, rsa_private_key, 0)) { + if (!libp2p_secio_handshake(&secure_session, rsa_private_key, NULL, 0)) { fprintf(stderr, "test_secio_handshake: Unable to do handshake\n"); fprintf(stdout, "Shared key: "); for(int i = 0; i < secure_session.shared_key_size; i++)