2017-02-13 18:26:41 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-02-15 17:04:10 +00:00
|
|
|
#include "libp2p/crypto/key.h"
|
2017-02-13 18:26:41 +00:00
|
|
|
#include "libp2p/conn/transport_dialer.h"
|
|
|
|
|
2017-02-15 17:04:10 +00:00
|
|
|
struct TransportDialer* libp2p_conn_transport_dialer_new(char* peer_id, struct PrivateKey* private_key) {
|
2017-02-13 18:26:41 +00:00
|
|
|
struct TransportDialer* out = (struct TransportDialer*)malloc(sizeof(struct TransportDialer));
|
|
|
|
if (out != NULL) {
|
2017-02-15 17:04:10 +00:00
|
|
|
out->peer_id = malloc(strlen(peer_id) + 1);
|
|
|
|
strcpy(out->peer_id, peer_id);
|
|
|
|
out->private_key = (struct PrivateKey*)malloc(sizeof(struct PrivateKey));
|
|
|
|
libp2p_crypto_private_key_copy(private_key, out->private_key);
|
2017-02-13 18:26:41 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free resources from a TransportDialer struct
|
|
|
|
* @param in the struct to be freed
|
|
|
|
*/
|
|
|
|
void libp2p_conn_transport_dialer_free(struct TransportDialer* in) {
|
2017-02-15 17:04:10 +00:00
|
|
|
if (in != NULL) {
|
|
|
|
if (in->peer_id != NULL)
|
|
|
|
free(in->peer_id);
|
|
|
|
libp2p_crypto_private_key_free(in->private_key);
|
|
|
|
free(in);
|
|
|
|
}
|
2017-02-13 18:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of dialers, find the appropriate dialer for this multiaddress
|
|
|
|
* @param transport_dialers a list of dialers
|
|
|
|
* @param multiaddr the address
|
|
|
|
* @returns a connection, or NULL if no appropriate dialer was found
|
|
|
|
*/
|
2017-02-13 22:41:31 +00:00
|
|
|
struct Connection* libp2p_conn_transport_dialer_get(struct Libp2pLinkedList* transport_dialers, struct MultiAddress* multiaddr) {
|
2017-02-15 17:04:10 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|