More changes for dialer

yamux
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
*/
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;
struct Dialer* dialer = (struct Dialer*)malloc(sizeof(struct Dialer));
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) {
strcpy(dialer->peer_id, peer_id);
dialer->private_key = (struct PrivateKey*)malloc(sizeof(struct PrivateKey));
if (dialer->private_key != NULL) {
libp2p_crypto_private_key_copy(private_key, dialer->private_key);
//TODO: build transport dialers
dialer->transport_dialers = NULL;
dialer->fallback_dialer = libp2p_conn_tcp_transport_dialer_new(peer_id, private_key);
return dialer;
}
strncpy(dialer->peer_id, peer->id, peer->id_size);
dialer->private_key = private_key;
//TODO: build transport dialers
dialer->transport_dialers = NULL;
dialer->fallback_dialer = libp2p_conn_tcp_transport_dialer_new(dialer->peer_id, private_key);
return 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) {
if (in != NULL) {
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) {
struct Libp2pLinkedList* current = in->transport_dialers;
while(current != NULL) {
@ -76,6 +74,15 @@ struct Connection* libp2p_conn_dialer_get_connection(const struct Dialer* dialer
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
* @param dialer the dialer to use

View File

@ -15,13 +15,14 @@
#include "multiaddr/multiaddr.h"
#include "libp2p/conn/connection.h"
#include "libp2p/conn/transport_dialer.h"
#include "libp2p/peer/peer.h"
struct Dialer {
/**
* These two are used to create connections
*/
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
@ -41,7 +42,7 @@ struct Dialer {
* @param private_key the local private key
* @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
@ -49,6 +50,15 @@ struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_
*/
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
* @param dialer the dialer to use

View File

@ -4,9 +4,10 @@
#include "libp2p/net/stream.h"
#include "libp2p/crypto/rsa.h"
#include "libp2p/conn/session.h"
#include "libp2p/conn/dialer.h"
//#include "libp2p/conn/dialer.h"
struct Peerstore;
struct Dialer;
enum ConnectionType {
// 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/logger.h"
#include "libp2p/yamux/yamux.h"
#include "libp2p/conn/dialer.h"
/**
* 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)
peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
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);
// find an appropriate address
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");
if (yamux_stream != NULL) {
// we're okay, get out
peer->connection_type = CONNECTION_TYPE_CONNECTED;
break;
}
now = time(NULL);
if (now >= (prev + timeout))
break;
current_address = current_address->next;
} // trying to connect
int retVal = peer->connection_type == CONNECTION_TYPE_CONNECTED;
int retVal = (peer->connection_type == CONNECTION_TYPE_CONNECTED);
if (!retVal) {
libp2p_logger_debug("peer", "Attempted connect to %s but failed.\n", libp2p_peer_id_to_string(peer));
}
return retVal;
*/
}
/**

View File

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