c-libp2p/conn/tcp_transport_dialer.c

39 lines
1.1 KiB
C
Raw Permalink Normal View History

#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "multiaddr/multiaddr.h"
#include "libp2p/net/p2pnet.h"
2017-10-23 23:03:38 +00:00
#include "libp2p/net/connectionstream.h"
#include "libp2p/conn/connection.h"
#include "libp2p/conn/transport_dialer.h"
2017-03-09 23:48:28 +00:00
#include "multiaddr/multiaddr.h"
/**
* An implementation of a tcp transport dialer
*/
2017-03-02 21:14:52 +00:00
int libp2p_conn_tcp_can_handle(const struct MultiAddress* addr) {
2017-10-23 23:03:38 +00:00
return multiaddress_is_ip(addr);
}
2017-10-23 23:03:38 +00:00
struct Stream* libp2p_conn_tcp_dial(const struct TransportDialer* transport_dialer, const struct MultiAddress* addr) {
int socket_descriptor = socket_open4();
2017-03-02 21:14:52 +00:00
char* ip;
2017-03-09 23:48:28 +00:00
int port = multiaddress_get_ip_port(addr);
if (!multiaddress_get_ip_address(addr, &ip))
return NULL;
2017-10-23 23:03:38 +00:00
struct Stream* stream = libp2p_net_connection_new(socket_descriptor, ip, port, NULL);
2017-04-27 04:56:18 +00:00
free(ip);
2017-10-23 23:03:38 +00:00
return stream;
}
2017-10-25 17:43:27 +00:00
struct TransportDialer* libp2p_conn_tcp_transport_dialer_new(char* peer_id, struct RsaPrivateKey* 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;
}