Multiaddress changes
This commit is contained in:
parent
74d95cb696
commit
72dfe2d786
5 changed files with 8 additions and 58 deletions
|
@ -8,7 +8,7 @@
|
||||||
#include "libp2p/conn/transport_dialer.h"
|
#include "libp2p/conn/transport_dialer.h"
|
||||||
#include "libp2p/crypto/key.h"
|
#include "libp2p/crypto/key.h"
|
||||||
#include "libp2p/utils/linked_list.h"
|
#include "libp2p/utils/linked_list.h"
|
||||||
#include "libp2p/utils/multiaddress.h"
|
#include "multiaddr/multiaddr.h"
|
||||||
#include "libp2p/net/multistream.h"
|
#include "libp2p/net/multistream.h"
|
||||||
|
|
||||||
struct TransportDialer* libp2p_conn_tcp_transport_dialer_new();
|
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)
|
if (strcmp(protocol, "multistream") != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
char* ip;
|
char* ip;
|
||||||
int port;
|
int port = multiaddress_get_ip_port(multiaddress);
|
||||||
if (!libp2p_utils_multiaddress_parse_ip4_tcp(multiaddress, &ip, &port)) {
|
if (!multiaddress_get_ip_address(multiaddress, &ip)) {
|
||||||
free(ip);
|
free(ip);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#include "libp2p/net/p2pnet.h"
|
#include "libp2p/net/p2pnet.h"
|
||||||
#include "libp2p/conn/connection.h"
|
#include "libp2p/conn/connection.h"
|
||||||
#include "libp2p/conn/transport_dialer.h"
|
#include "libp2p/conn/transport_dialer.h"
|
||||||
#include "libp2p/utils/multiaddress.h"
|
#include "multiaddr/multiaddr.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An implementation of a tcp transport dialer
|
* 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*));
|
struct Connection* conn = (struct Connection*) malloc(sizeof(struct Connection*));
|
||||||
conn->socket_handle = socket_open4();
|
conn->socket_handle = socket_open4();
|
||||||
char* ip;
|
char* ip;
|
||||||
int port;
|
int port = multiaddress_get_ip_port(addr);
|
||||||
libp2p_utils_multiaddress_parse_ip4_tcp(addr, &ip, &port);
|
if (!multiaddress_get_ip_address(addr, &ip))
|
||||||
|
return NULL;
|
||||||
struct hostent* host = gethostbyname(ip);
|
struct hostent* host = gethostbyname(ip);
|
||||||
struct in_addr** addr_list = (struct in_addr**)host->h_addr_list;
|
struct in_addr** addr_list = (struct in_addr**)host->h_addr_list;
|
||||||
socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, port);
|
socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, port);
|
||||||
|
|
|
@ -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);
|
|
|
@ -7,7 +7,7 @@ endif
|
||||||
|
|
||||||
LFLAGS =
|
LFLAGS =
|
||||||
DEPS =
|
DEPS =
|
||||||
OBJS = string_list.o vector.o linked_list.o multiaddress.o
|
OBJS = string_list.o vector.o linked_list.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
#include <stdlib.h>
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
Loading…
Reference in a new issue