diff --git a/conn/dialer.c b/conn/dialer.c index 56899fa..fee893b 100644 --- a/conn/dialer.c +++ b/conn/dialer.c @@ -8,7 +8,7 @@ #include "libp2p/conn/transport_dialer.h" #include "libp2p/crypto/key.h" #include "libp2p/utils/linked_list.h" -#include "libp2p/utils/multiaddress.h" +#include "multiaddr/multiaddr.h" #include "libp2p/net/multistream.h" struct TransportDialer* libp2p_conn_tcp_transport_dialer_new(); @@ -88,8 +88,8 @@ struct Stream* libp2p_conn_dialer_get_stream(const struct Dialer* dialer, const if (strcmp(protocol, "multistream") != 0) return NULL; char* ip; - int port; - if (!libp2p_utils_multiaddress_parse_ip4_tcp(multiaddress, &ip, &port)) { + int port = multiaddress_get_ip_port(multiaddress); + if (!multiaddress_get_ip_address(multiaddress, &ip)) { free(ip); return NULL; } diff --git a/conn/tcp_transport_dialer.c b/conn/tcp_transport_dialer.c index 0ae1133..30d4bbb 100644 --- a/conn/tcp_transport_dialer.c +++ b/conn/tcp_transport_dialer.c @@ -6,7 +6,7 @@ #include "libp2p/net/p2pnet.h" #include "libp2p/conn/connection.h" #include "libp2p/conn/transport_dialer.h" -#include "libp2p/utils/multiaddress.h" +#include "multiaddr/multiaddr.h" /** * An implementation of a tcp transport dialer @@ -33,8 +33,9 @@ struct Connection* libp2p_conn_tcp_dial(const struct TransportDialer* transport_ struct Connection* conn = (struct Connection*) malloc(sizeof(struct Connection*)); conn->socket_handle = socket_open4(); char* ip; - int port; - libp2p_utils_multiaddress_parse_ip4_tcp(addr, &ip, &port); + int port = multiaddress_get_ip_port(addr); + if (!multiaddress_get_ip_address(addr, &ip)) + return NULL; struct hostent* host = gethostbyname(ip); struct in_addr** addr_list = (struct in_addr**)host->h_addr_list; socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, port); diff --git a/include/libp2p/utils/multiaddress.h b/include/libp2p/utils/multiaddress.h deleted file mode 100644 index 274d946..0000000 --- a/include/libp2p/utils/multiaddress.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "multiaddr/multiaddr.h" - -/** - * This is a hack to get ip4/tcp working - * TODO: this should be moved further down in the networking stack and generified for different multiaddresses - * This makes too many assumptions - * @param address the multiaddress to parse - * @param ip the first IP address in the multiaddress - * @param port the first port in the multiaddress - * @returns true(1) on success, false(0) on failure - */ -int libp2p_utils_multiaddress_parse_ip4_tcp(const struct MultiAddress* address, char** ip, int* port); diff --git a/utils/Makefile b/utils/Makefile index 6df187a..1921f2c 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -7,7 +7,7 @@ endif LFLAGS = DEPS = -OBJS = string_list.o vector.o linked_list.o multiaddress.o +OBJS = string_list.o vector.o linked_list.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/utils/multiaddress.c b/utils/multiaddress.c deleted file mode 100644 index 163603a..0000000 --- a/utils/multiaddress.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -/** - * A central repository for parsing Multiaddress structs - */ - -#include "libp2p/utils/multiaddress.h" - -/** - * This is a hack to get ip4/tcp working - * TODO: this should be moved further down in the networking stack and generified for different multiaddresses - * This makes too many assumptions - * @param address the multiaddress to parse - * @param ip the first IP address in the multiaddress - * @param port the first port in the multiaddress - * @returns true(1) on success, false(0) on failure - */ -int libp2p_utils_multiaddress_parse_ip4_tcp(const struct MultiAddress* address, char** ip, int* port) { - // the incoming address is not what was expected - if (strncmp(address->string, "/ip4/", 5) != 0) - return 0; - if (strstr(address->string, "/tcp/") == NULL) - return 0; - // ip - char* str = malloc(strlen(address->string)); - if (str == NULL) - return 0; - strcpy(str, &address->string[5]); // gets rid of /ip4/ - char* pos = strchr(str, '/'); - pos[0] = 0; - *ip = malloc(strlen(str) + 1); - strcpy(*ip, str); - free(str); - // port - str = strstr(address->string, "/tcp/"); - str += 5; - *port = atoi(str); - return 1; -}