More changes for dialer

This commit is contained in:
jmjatlanta 2017-10-23 16:21:03 -05:00
parent 1dcac6ecb5
commit 05f2620054
5 changed files with 56 additions and 27 deletions

View file

@ -16,21 +16,19 @@ struct TransportDialer* libp2p_conn_tcp_transport_dialer_new();
/** /**
* Create a Dialer with the specified local information * Create a Dialer with the specified local information
*/ */
struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_key) { struct Dialer* libp2p_conn_dialer_new(struct Libp2pPeer* peer, struct RsaPrivateKey* private_key) {
int success = 0; int success = 0;
struct Dialer* dialer = (struct Dialer*)malloc(sizeof(struct Dialer)); struct Dialer* dialer = (struct Dialer*)malloc(sizeof(struct Dialer));
if (dialer != NULL) { if (dialer != NULL) {
dialer->peer_id = malloc(strlen(peer_id) + 1); dialer->peer_id = malloc(peer->id_size + 1);
memset(dialer->peer_id, 0, peer->id_size + 1);
if (dialer->peer_id != NULL) { if (dialer->peer_id != NULL) {
strcpy(dialer->peer_id, peer_id); strncpy(dialer->peer_id, peer->id, peer->id_size);
dialer->private_key = (struct PrivateKey*)malloc(sizeof(struct PrivateKey)); dialer->private_key = private_key;
if (dialer->private_key != NULL) { //TODO: build transport dialers
libp2p_crypto_private_key_copy(private_key, dialer->private_key); dialer->transport_dialers = NULL;
//TODO: build transport dialers dialer->fallback_dialer = libp2p_conn_tcp_transport_dialer_new(dialer->peer_id, private_key);
dialer->transport_dialers = NULL; return dialer;
dialer->fallback_dialer = libp2p_conn_tcp_transport_dialer_new(peer_id, private_key);
return dialer;
}
} }
} }
libp2p_conn_dialer_free(dialer); libp2p_conn_dialer_free(dialer);
@ -45,7 +43,7 @@ struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_
void libp2p_conn_dialer_free(struct Dialer* in) { void libp2p_conn_dialer_free(struct Dialer* in) {
if (in != NULL) { if (in != NULL) {
free(in->peer_id); free(in->peer_id);
libp2p_crypto_private_key_free(in->private_key); //libp2p_crypto_private_key_free(in->private_key);
if (in->transport_dialers != NULL) { if (in->transport_dialers != NULL) {
struct Libp2pLinkedList* current = in->transport_dialers; struct Libp2pLinkedList* current = in->transport_dialers;
while(current != NULL) { while(current != NULL) {
@ -76,6 +74,15 @@ struct Connection* libp2p_conn_dialer_get_connection(const struct Dialer* dialer
return conn; return conn;
} }
/***
* Attempt to connect to a particular peer
* @param dialer the dialer
* @param peer the peer to join
*/
int libp2p_conn_dialer_join_swarm(const struct Dialer* dialer, struct Libp2pPeer* peer, int timeout_secs) {
return 0;
}
/** /**
* return a Stream that is already set up to use the passed in protocol * return a Stream that is already set up to use the passed in protocol
* @param dialer the dialer to use * @param dialer the dialer to use

View file

@ -15,13 +15,14 @@
#include "multiaddr/multiaddr.h" #include "multiaddr/multiaddr.h"
#include "libp2p/conn/connection.h" #include "libp2p/conn/connection.h"
#include "libp2p/conn/transport_dialer.h" #include "libp2p/conn/transport_dialer.h"
#include "libp2p/peer/peer.h"
struct Dialer { struct Dialer {
/** /**
* These two are used to create connections * These two are used to create connections
*/ */
char* peer_id; // the local peer ID as null terminated string char* peer_id; // the local peer ID as null terminated string
struct PrivateKey* private_key; // used to initiate secure connections, can be NULL, and connections will not be secured struct RsaPrivateKey* private_key; // used to initiate secure connections, can be NULL, and connections will not be secured
/** /**
* A linked list of transport dialers. A transport dialer can be selected * A linked list of transport dialers. A transport dialer can be selected
@ -41,7 +42,7 @@ struct Dialer {
* @param private_key the local private key * @param private_key the local private key
* @returns a new Dialer struct * @returns a new Dialer struct
*/ */
struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_key); struct Dialer* libp2p_conn_dialer_new(struct Libp2pPeer* peer, struct RsaPrivateKey* private_key);
/** /**
* free resources from the Dialer struct * free resources from the Dialer struct
@ -49,6 +50,15 @@ struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_
*/ */
void libp2p_conn_dialer_free(struct Dialer* in); void libp2p_conn_dialer_free(struct Dialer* in);
/***
* Attempt to connect to a particular peer
* @param dialer the dialer
* @param peer the peer to join
* @param timeout_secs network timeout in seconds
* @returns true(1) on success, false(0) otherwise
*/
int libp2p_conn_dialer_join_swarm(const struct Dialer* dialer, struct Libp2pPeer* peer, int timeout_secs);
/** /**
* Retrieve a Connection struct from the dialer * Retrieve a Connection struct from the dialer
* @param dialer the dialer to use * @param dialer the dialer to use

View file

@ -4,9 +4,10 @@
#include "libp2p/net/stream.h" #include "libp2p/net/stream.h"
#include "libp2p/crypto/rsa.h" #include "libp2p/crypto/rsa.h"
#include "libp2p/conn/session.h" #include "libp2p/conn/session.h"
#include "libp2p/conn/dialer.h" //#include "libp2p/conn/dialer.h"
struct Peerstore; struct Peerstore;
struct Dialer;
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)

View file

@ -9,6 +9,7 @@
#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" #include "libp2p/yamux/yamux.h"
#include "libp2p/conn/dialer.h"
/** /**
* create a new Peer struct * create a new Peer struct
@ -102,6 +103,8 @@ int libp2p_peer_connect(const struct Dialer* dialer, struct Libp2pPeer* peer, st
if (peer->connection_type == CONNECTION_TYPE_CONNECTED && peer->sessionContext == NULL) if (peer->connection_type == CONNECTION_TYPE_CONNECTED && peer->sessionContext == NULL)
peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED; peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
libp2p_logger_debug("peer", "Attemping to connect to %s.\n", libp2p_peer_id_to_string(peer)); libp2p_logger_debug("peer", "Attemping to connect to %s.\n", libp2p_peer_id_to_string(peer));
return libp2p_conn_dialer_join_swarm(dialer, peer, 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;
@ -111,17 +114,20 @@ int libp2p_peer_connect(const struct Dialer* dialer, struct Libp2pPeer* peer, st
struct Stream* yamux_stream = libp2p_conn_dialer_get_stream(dialer, ma, "yamux"); struct Stream* yamux_stream = libp2p_conn_dialer_get_stream(dialer, ma, "yamux");
if (yamux_stream != NULL) { if (yamux_stream != NULL) {
// we're okay, get out // we're okay, get out
peer->connection_type = CONNECTION_TYPE_CONNECTED;
break; break;
} }
now = time(NULL); now = time(NULL);
if (now >= (prev + timeout)) if (now >= (prev + timeout))
break; break;
current_address = current_address->next;
} // trying to connect } // trying to connect
int retVal = peer->connection_type == CONNECTION_TYPE_CONNECTED; int retVal = (peer->connection_type == CONNECTION_TYPE_CONNECTED);
if (!retVal) { if (!retVal) {
libp2p_logger_debug("peer", "Attempted connect to %s but failed.\n", libp2p_peer_id_to_string(peer)); libp2p_logger_debug("peer", "Attempted connect to %s but failed.\n", libp2p_peer_id_to_string(peer));
} }
return retVal; return retVal;
*/
} }
/** /**

View file

@ -7,16 +7,17 @@
int test_dialer_new() { int test_dialer_new() {
int retVal = 0; int retVal = 0;
char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3"; char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3";
struct PrivateKey* private_key = libp2p_crypto_private_key_new(); struct RsaPrivateKey* private_key = NULL;
struct Dialer* dialer = libp2p_conn_dialer_new(peer_id, private_key); struct Libp2pPeer* peer = libp2p_peer_new();
peer->id = peer_id;
peer->id_size = strlen(peer_id);
struct Dialer* dialer = libp2p_conn_dialer_new(peer, private_key);
if (dialer == NULL) if (dialer == NULL)
goto exit; goto exit;
retVal = 1; retVal = 1;
exit: exit:
if (dialer != NULL) libp2p_peer_free(peer);
libp2p_conn_dialer_free(dialer); libp2p_conn_dialer_free(dialer);
if (private_key != NULL)
libp2p_crypto_private_key_free(private_key);
return retVal; return retVal;
} }
@ -31,12 +32,14 @@ int test_dialer_dial() {
struct Connection* conn = NULL; struct Connection* conn = NULL;
char* result = NULL; char* result = NULL;
size_t result_size = 0; size_t result_size = 0;
struct Libp2pPeer* peer = libp2p_peer_new();
test_helper_get_id_from_config(config_dir, &private_key, &peer_id); test_helper_get_id_from_config(config_dir, &private_key, &peer->id);
if (private_key == NULL) if (private_key == NULL)
goto exit; goto exit;
peer->id_size = strlen((char*)peer->id);
dialer = libp2p_conn_dialer_new(peer_id, private_key); dialer = libp2p_conn_dialer_new(peer, NULL);
if (dialer == NULL) if (dialer == NULL)
goto exit; goto exit;
@ -57,7 +60,6 @@ int test_dialer_dial() {
free(peer_id); free(peer_id);
multiaddress_free(destination_address); multiaddress_free(destination_address);
libp2p_conn_dialer_free(dialer); libp2p_conn_dialer_free(dialer);
libp2p_crypto_private_key_free(private_key);
libp2p_conn_connection_free(conn); libp2p_conn_connection_free(conn);
return retVal; return retVal;
} }
@ -73,12 +75,15 @@ int test_dialer_dial_multistream() {
struct Stream* stream = NULL; struct Stream* stream = NULL;
char* result = NULL; char* result = NULL;
size_t result_size = 0; size_t result_size = 0;
struct Libp2pPeer* peer = libp2p_peer_new();
test_helper_get_id_from_config(config_dir, &private_key, &peer_id); test_helper_get_id_from_config(config_dir, &private_key, &peer->id);
if (private_key == NULL) if (private_key == NULL)
goto exit; goto exit;
dialer = libp2p_conn_dialer_new(peer_id, private_key); peer->id_size = strlen((char*)peer->id);
dialer = libp2p_conn_dialer_new(peer, NULL);
if (dialer == NULL) if (dialer == NULL)
goto exit; goto exit;