More dialer changes, starting to implement default dialer

yamux
John Jones 2017-02-15 12:04:10 -05:00
parent 5c08094548
commit ccc7ca3e8b
9 changed files with 113 additions and 20 deletions

View File

@ -2,7 +2,7 @@ CC = gcc
CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -I../../c-multiaddr/include -g3 CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -I../../c-multiaddr/include -g3
LFLAGS = LFLAGS =
DEPS = DEPS =
OBJS = dialer.o transport_dialer.o connection.o OBJS = dialer.o transport_dialer.o connection.o tcp_transport_dialer.o
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)

View File

@ -16,7 +16,7 @@ struct Connection* libp2p_conn_connection_new(struct TransportDialer* transport_
void libp2p_conn_connection_free(struct Connection* connection) { void libp2p_conn_connection_free(struct Connection* connection) {
if (connection != NULL) { if (connection != NULL) {
fclose(connection->socket_handle); //close(connection->socket_handle);
free(connection); free(connection);
} }
} }

View File

@ -9,10 +9,13 @@
#include "libp2p/crypto/key.h" #include "libp2p/crypto/key.h"
#include "libp2p/utils/linked_list.h" #include "libp2p/utils/linked_list.h"
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(char* peer_id, struct PrivateKey* private_key) {
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(strlen(peer_id) + 1);
@ -21,8 +24,9 @@ struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_
dialer->private_key = (struct PrivateKey*)malloc(sizeof(struct PrivateKey)); dialer->private_key = (struct PrivateKey*)malloc(sizeof(struct PrivateKey));
if (dialer->private_key != NULL) { if (dialer->private_key != NULL) {
libp2p_crypto_private_key_copy(private_key, dialer->private_key); libp2p_crypto_private_key_copy(private_key, dialer->private_key);
dialer->fallback_dialer = NULL; //TODO: build transport dialers
dialer->transport_dialers = NULL; dialer->transport_dialers = NULL;
dialer->fallback_dialer = libp2p_conn_tcp_transport_dialer_new(peer_id, private_key);
return dialer; return dialer;
} }
} }
@ -32,7 +36,9 @@ struct Dialer* libp2p_conn_dialer_new(char* peer_id, struct PrivateKey* private_
} }
/** /**
* free resources from the Dialer * Free resources from the Dialer
* NOTE: this frees the fallback dialer too (should we be doing this?
* @param in the Dialer struct to free
*/ */
void libp2p_conn_dialer_free(struct Dialer* in) { void libp2p_conn_dialer_free(struct Dialer* in) {
if (in != NULL) { if (in != NULL) {
@ -47,6 +53,7 @@ void libp2p_conn_dialer_free(struct Dialer* in) {
} }
if (in->fallback_dialer != NULL) if (in->fallback_dialer != NULL)
libp2p_conn_transport_dialer_free((struct TransportDialer*)in->fallback_dialer); libp2p_conn_transport_dialer_free((struct TransportDialer*)in->fallback_dialer);
free(in);
} }
return; return;
} }

View File

@ -0,0 +1,66 @@
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "multiaddr/multiaddr.h"
#include "libp2p/net/p2pnet.h"
#include "libp2p/conn/connection.h"
#include "libp2p/conn/transport_dialer.h"
/**
* An implementation of a tcp transport dialer
*/
struct TcpIp {
char* ip;
int port;
};
struct TcpIp* libp2p_conn_parse_ip_multiaddress(struct MultiAddress* addr) {
struct TcpIp* out = (struct TcpIp*)malloc(sizeof(struct TcpIp));
char* address = malloc(strlen(addr->string) + 1);
strcpy(address, addr->string);
char* tok = strtok(address, "/");
int pos = 0;
while (tok != NULL) {
switch (pos) {
case 2: {
out->ip = malloc(strlen(tok) + 1);
strcpy(out->ip, tok);
break;
}
case 4: {
out->port = strtol(tok, NULL, 10);
break;
}
}
tok = strtok(NULL, "/");
pos++;
}
//TODO: do a better job of parsing the results
return out;
}
int libp2p_conn_tcp_can_handle(struct MultiAddress* addr) {
return 1;
}
struct Connection* libp2p_conn_tcp_dial(struct TransportDialer* transport_dialer, struct MultiAddress* addr) {
struct Connection* conn = (struct Connection*) malloc(sizeof(struct Connection*));
conn->socket_handle = socket_open4();
struct TcpIp* results = libp2p_conn_parse_ip_multiaddress(addr);
struct hostent* host = gethostbyname(results->ip);
struct in_addr** addr_list = (struct in_addr**)host->h_addr_list;
socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, results->port);
return conn;
}
struct TransportDialer* libp2p_conn_tcp_transport_dialer_new(char* peer_id, struct PrivateKey* private_key) {
struct TransportDialer* out = libp2p_conn_transport_dialer_new(peer_id, private_key);
out->can_handle = libp2p_conn_tcp_can_handle;
out->dial = libp2p_conn_tcp_dial;
return out;
}

View File

@ -1,19 +1,15 @@
#include <stdlib.h> #include <stdlib.h>
#include "libp2p/crypto/key.h"
#include "libp2p/conn/transport_dialer.h" #include "libp2p/conn/transport_dialer.h"
struct TransportDialer* libp2p_conn_transport_dialer_new(struct MultiAddress* multiaddr) { struct TransportDialer* libp2p_conn_transport_dialer_new(char* peer_id, struct PrivateKey* private_key) {
struct TransportDialer* out = (struct TransportDialer*)malloc(sizeof(struct TransportDialer)); struct TransportDialer* out = (struct TransportDialer*)malloc(sizeof(struct TransportDialer));
if (out != NULL) { if (out != NULL) {
out->multiaddr = (struct MultiAddress*)malloc(sizeof(struct MultiAddress)); out->peer_id = malloc(strlen(peer_id) + 1);
if (out->multiaddr == NULL) { strcpy(out->peer_id, peer_id);
libp2p_conn_transport_dialer_free(out); out->private_key = (struct PrivateKey*)malloc(sizeof(struct PrivateKey));
return NULL; libp2p_crypto_private_key_copy(private_key, out->private_key);
}
if (multiaddress_copy(multiaddr, out->multiaddr) == 0) {
libp2p_conn_transport_dialer_free(out);
return NULL;
}
} }
return out; return out;
} }
@ -23,7 +19,12 @@ struct TransportDialer* libp2p_conn_transport_dialer_new(struct MultiAddress* mu
* @param in the struct to be freed * @param in the struct to be freed
*/ */
void libp2p_conn_transport_dialer_free(struct TransportDialer* in) { void libp2p_conn_transport_dialer_free(struct TransportDialer* in) {
free(in); if (in != NULL) {
if (in->peer_id != NULL)
free(in->peer_id);
libp2p_crypto_private_key_free(in->private_key);
free(in);
}
} }
/** /**
@ -33,6 +34,19 @@ void libp2p_conn_transport_dialer_free(struct TransportDialer* in) {
* @returns a connection, or NULL if no appropriate dialer was found * @returns a connection, or NULL if no appropriate dialer was found
*/ */
struct Connection* libp2p_conn_transport_dialer_get(struct Libp2pLinkedList* transport_dialers, struct MultiAddress* multiaddr) { struct Connection* libp2p_conn_transport_dialer_get(struct Libp2pLinkedList* transport_dialers, struct MultiAddress* multiaddr) {
//TODO: implement this method struct Libp2pLinkedList* current = transport_dialers;
struct TransportDialer* t_dialer = NULL;
while (current != NULL) {
t_dialer = (struct TransportDialer*)current->item;
if (t_dialer->can_handle(multiaddr))
break;
current = current->next;
t_dialer = NULL;
}
if (t_dialer != NULL) {
return t_dialer->dial(t_dialer, multiaddr);
}
return NULL; return NULL;
} }

View File

@ -8,7 +8,7 @@
#include "multiaddr/multiaddr.h" #include "multiaddr/multiaddr.h"
struct Connection { struct Connection {
FILE* socket_handle; int socket_handle;
/** /**
* Read from the stream * Read from the stream
* @param socket_handle the socket to read from * @param socket_handle the socket to read from

View File

@ -4,10 +4,13 @@
#include "libp2p/utils/linked_list.h" #include "libp2p/utils/linked_list.h"
struct TransportDialer { struct TransportDialer {
struct MultiAddress* multiaddr; char* peer_id;
struct PrivateKey* private_key;
int (*can_handle)(struct MultiAddress* multiaddr);
struct Connection* (*dial)(struct TransportDialer* transport_dialer, struct MultiAddress* multiaddr);
}; };
struct TransportDialer* libp2p_conn_transport_dialer_new(struct MultiAddress* multiaddr); struct TransportDialer* libp2p_conn_transport_dialer_new(char* peer_id, struct PrivateKey* private_key);
void libp2p_conn_transport_dialer_free(struct TransportDialer* in); void libp2p_conn_transport_dialer_free(struct TransportDialer* in);
struct Connection* libp2p_conn_transport_dialer_get(struct Libp2pLinkedList* transport_dialers, struct MultiAddress* multiaddr); struct Connection* libp2p_conn_transport_dialer_get(struct Libp2pLinkedList* transport_dialers, struct MultiAddress* multiaddr);

View File

@ -16,7 +16,7 @@ int test_dialer_new() {
int test_dialer_dial() { int test_dialer_dial() {
int retVal = 0; int retVal = 0;
char* config_dir = "/home/parallels/.ipfs/config"; char* config_dir = "/home/parallels/.ipfs/config";
char* destination_string = "/ip/192.210.179.217/tcp/4001"; char* destination_string = "/ip4/192.210.179.217/tcp/4001/";
char* peer_id = NULL; char* peer_id = NULL;
struct PrivateKey* private_key = NULL; struct PrivateKey* private_key = NULL;
struct Dialer* dialer = NULL; struct Dialer* dialer = NULL;
@ -56,6 +56,7 @@ int test_dialer_dial() {
exit: exit:
if (result != NULL) if (result != NULL)
free(result); free(result);
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_crypto_private_key_free(private_key);

View File

@ -39,6 +39,8 @@ struct PrivateKey* base64ToPrivateKey(char* base64) {
libp2p_crypto_private_key_free(out); libp2p_crypto_private_key_free(out);
out = NULL; out = NULL;
} }
if (decode_base64 != NULL)
free(decode_base64);
return out; return out;
} }