Now a successful secio connection adds the peer to the peerstore
This commit is contained in:
parent
8c1c813643
commit
3137d9efa6
6 changed files with 34 additions and 8 deletions
|
@ -4,6 +4,8 @@
|
||||||
#include "libp2p/net/stream.h"
|
#include "libp2p/net/stream.h"
|
||||||
#include "libp2p/crypto/rsa.h"
|
#include "libp2p/crypto/rsa.h"
|
||||||
|
|
||||||
|
struct Peerstore;
|
||||||
|
|
||||||
enum ConnectionType {
|
enum ConnectionType {
|
||||||
// sender does not have a connection to the peer, and no extra information (default)
|
// sender does not have a connection to the peer, and no extra information (default)
|
||||||
CONNECTION_TYPE_NOT_CONNECTED = 0,
|
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
|
* Attempt to connect to the peer, setting connection_type correctly
|
||||||
* NOTE: If successful, this will set peer->connection to the stream
|
* 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 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
|
* @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
|
* Make a copy of a peer
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "libp2p/crypto/key.h"
|
#include "libp2p/crypto/key.h"
|
||||||
#include "libp2p/crypto/rsa.h"
|
#include "libp2p/crypto/rsa.h"
|
||||||
#include "libp2p/conn/session.h"
|
#include "libp2p/conn/session.h"
|
||||||
|
#include "libp2p/peer/peerstore.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handling of a secure connection
|
* Handling of a secure connection
|
||||||
|
@ -17,4 +18,4 @@
|
||||||
* @param remote_requested the other side is who asked for the upgrade
|
* @param remote_requested the other side is who asked for the upgrade
|
||||||
* @returns true(1) on success, false(0) otherwise
|
* @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);
|
||||||
|
|
|
@ -78,10 +78,13 @@ void libp2p_peer_free(struct Libp2pPeer* in) {
|
||||||
/**
|
/**
|
||||||
* Attempt to connect to the peer, setting connection_type correctly
|
* Attempt to connect to the peer, setting connection_type correctly
|
||||||
* NOTE: If successful, this will set peer->connection to the stream
|
* 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 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
|
* @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);
|
time_t now, prev = time(NULL);
|
||||||
// find an appropriate address
|
// find an appropriate address
|
||||||
struct Libp2pLinkedList* current_address = peer->addr_head;
|
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->sessionContext->default_stream = peer->sessionContext->insecure_stream;
|
||||||
peer->connection_type = CONNECTION_TYPE_CONNECTED;
|
peer->connection_type = CONNECTION_TYPE_CONNECTED;
|
||||||
}
|
}
|
||||||
libp2p_secio_handshake(peer->sessionContext, privateKey, 0);
|
libp2p_secio_handshake(peer->sessionContext, privateKey, peerstore, 0);
|
||||||
free(ip);
|
free(ip);
|
||||||
} // is IP
|
} // is IP
|
||||||
now = time(NULL);
|
now = time(NULL);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
CC = gcc
|
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
|
ifdef DEBUG
|
||||||
CFLAGS += -g3
|
CFLAGS += -g3
|
||||||
|
|
|
@ -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
|
* @param remote_requested it is the other side that requested the upgrade to secio
|
||||||
* @returns true(1) on success, false(0) otherwise
|
* @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;
|
int retVal = 0;
|
||||||
size_t results_size = 0, bytes_written = 0;
|
size_t results_size = 0, bytes_written = 0;
|
||||||
unsigned char* propose_in_bytes = NULL; // the remote protobuf
|
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 StretchedKey* k1 = NULL, *k2 = NULL;
|
||||||
struct PrivateKey* priv = NULL;
|
struct PrivateKey* priv = NULL;
|
||||||
struct PublicKey pub_key = {0};
|
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
|
//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
|
// generate their peer id
|
||||||
libp2p_crypto_public_key_to_peer_id(public_key, &local_session->remote_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
|
// negotiate encryption parameters NOTE: SelectBest must match, otherwise this won't work
|
||||||
// first determine order
|
// first determine order
|
||||||
order = libp2p_secio_determine_order(propose_in, propose_out);
|
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) {
|
if (retVal == 1) {
|
||||||
libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake success!\n");
|
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 {
|
} else {
|
||||||
libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake returning false\n");
|
libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake returning false\n");
|
||||||
|
libp2p_peer_free(remote_peer);
|
||||||
}
|
}
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ int test_secio_handshake() {
|
||||||
goto exit;
|
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(stderr, "test_secio_handshake: Unable to do handshake\n");
|
||||||
fprintf(stdout, "Shared key: ");
|
fprintf(stdout, "Shared key: ");
|
||||||
for(int i = 0; i < secure_session.shared_key_size; i++)
|
for(int i = 0; i < secure_session.shared_key_size; i++)
|
||||||
|
|
Loading…
Reference in a new issue