c-libp2p/conn/transport_dialer.c

58 lines
1.6 KiB
C
Raw Permalink Normal View History

2017-02-13 18:26:41 +00:00
#include <stdlib.h>
2017-10-25 17:43:27 +00:00
#include "libp2p/crypto/rsa.h"
2017-02-13 18:26:41 +00:00
#include "libp2p/conn/transport_dialer.h"
2017-10-25 17:43:27 +00:00
struct TransportDialer* libp2p_conn_transport_dialer_new(char* peer_id, struct RsaPrivateKey* private_key) {
2017-02-13 18:26:41 +00:00
struct TransportDialer* out = (struct TransportDialer*)malloc(sizeof(struct TransportDialer));
if (out != NULL) {
2017-11-20 00:29:40 +00:00
out->peer_id = NULL;
out->private_key = NULL;
if (peer_id != NULL) {
out->peer_id = malloc(strlen(peer_id) + 1);
strcpy(out->peer_id, peer_id);
}
2017-10-25 17:28:53 +00:00
if (private_key != NULL) {
2017-10-25 17:43:27 +00:00
out->private_key = private_key;
2017-10-25 17:28:53 +00:00
}
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);
2017-10-25 17:43:27 +00:00
//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-10-23 23:03:38 +00:00
struct Stream* libp2p_conn_transport_dialer_get(const struct Libp2pLinkedList* transport_dialers, const struct MultiAddress* multiaddr) {
2017-03-02 21:14:52 +00:00
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;
}