c-libp2p/conn/transport_dialer.c

53 lines
1.6 KiB
C
Raw Normal View History

2017-02-13 18:26:41 +00:00
#include <stdlib.h>
#include "libp2p/crypto/key.h"
2017-02-13 18:26:41 +00:00
#include "libp2p/conn/transport_dialer.h"
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) {
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) {
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-03-02 21:14:52 +00:00
struct Connection* libp2p_conn_transport_dialer_get(const struct Libp2pLinkedList* transport_dialers, const struct MultiAddress* multiaddr) {
const 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;
}