Skip to content

Commit

Permalink
Finish @Diadlo's network Family abstraction.
Browse files Browse the repository at this point in the history
The Family stuff in toxcore is a big mess. I'm sure I saw a bunch of bugs
on the way, but I'm not verifying that code now, so the bugs stay.
  • Loading branch information
iphydf committed Feb 27, 2018
1 parent 1ed247f commit 498532c
Show file tree
Hide file tree
Showing 20 changed files with 329 additions and 229 deletions.
11 changes: 5 additions & 6 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@
#endif

#if !USE_IPV6
#undef TOX_AF_INET6
#define TOX_AF_INET6 TOX_AF_INET
#define net_family_ipv6 net_family_ipv4
#endif

static inline IP get_loopback()
{
IP ip;
#if USE_IPV6
ip.family = TOX_AF_INET6;
ip.family = net_family_ipv6;
ip.ip.v6 = get_ip6_loopback();
#else
ip.family = TOX_AF_INET;
ip.family = net_family_ipv4;
ip.ip.v4 = get_ip4_loopback();
#endif
return ip;
Expand All @@ -56,7 +55,7 @@ START_TEST(test_basic)
ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server");
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports");

Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
IP_Port ip_port_loopback;
ip_port_loopback.ip = get_loopback();
ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]);
Expand Down Expand Up @@ -156,7 +155,7 @@ struct sec_TCP_con {
static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
{
struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con));
Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);

IP_Port ip_port_loopback;
ip_port_loopback.ip = get_loopback();
Expand Down
18 changes: 9 additions & 9 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ static inline IP get_loopback()
{
IP ip;
#if USE_IPV6
ip.family = TOX_AF_INET6;
ip.family = net_family_ipv6;
ip.ip.v6 = get_ip6_loopback();
#else
ip.family = TOX_AF_INET;
ip.family = net_family_ipv4;
ip.ip.v4 = get_ip4_loopback();
#endif
return ip;
Expand Down Expand Up @@ -112,7 +112,7 @@ static void test_addto_lists_update(DHT *dht,
int used, test, test1, test2, found;
IP_Port test_ipp;
uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0;
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;

// check id update for existing ip_port
test = rand() % length;
Expand Down Expand Up @@ -187,7 +187,7 @@ static void test_addto_lists_bad(DHT *dht,
int used, test1, test2, test3;
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE],
test_id3[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0;
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;

random_bytes(public_key, sizeof(public_key));
mark_all_good(list, length, ipv6);
Expand Down Expand Up @@ -231,7 +231,7 @@ static void test_addto_lists_possible_bad(DHT *dht,
int used, test1, test2, test3;
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE],
test_id3[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0;
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;

random_bytes(public_key, sizeof(public_key));
mark_all_good(list, length, ipv6);
Expand Down Expand Up @@ -293,7 +293,7 @@ static void test_addto_lists_good(DHT *dht,
const uint8_t *comp_client_id)
{
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0;
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;

mark_all_good(list, length, ipv6);

Expand Down Expand Up @@ -536,7 +536,7 @@ static void test_list_main(void)
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);

Node_format ln[MAX_SENT_NODES];
int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0);
int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1, 0);
ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j);

count = 0;
Expand Down Expand Up @@ -728,8 +728,8 @@ static void random_ip(IP_Port *ipp, int family)
uint8_t *port = (uint8_t *)&ipp->port;
random_bytes(port, sizeof(ipp->port));
random_bytes(ip, size);
ipp->ip.family = family;

// TODO(iphydf): Pass the net_family variant to random_ip.
ipp->ip.family.value = family;
}

#define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE)
Expand Down
21 changes: 12 additions & 9 deletions auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ START_TEST(test_addr_resolv_localhost)
ck_assert_msg(res > 0, "Resolver failed: %u, %s", errno, strerror(errno));

char ip_str[IP_NTOA_LEN];
ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family);
ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET, got %u.", ip.family.value);
const uint32_t loopback = get_ip4_loopback().uint32;
ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
ip_ntoa(&ip, ip_str, sizeof(ip_str)));
Expand All @@ -56,7 +56,8 @@ START_TEST(test_addr_resolv_localhost)

ck_assert_msg(res > 0, "Resolver failed: %u, %s", errno, strerror(errno));

ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family);
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6,
ip.family.value);
IP6 ip6_loopback = get_ip6_loopback();
ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
ip_ntoa(&ip, ip_str, sizeof(ip_str)));
Expand All @@ -67,22 +68,24 @@ START_TEST(test_addr_resolv_localhost)
}

ip_init(&ip, 1); // ipv6enabled = 1
ip.family = TOX_AF_UNSPEC;
ip.family = net_family_unspec;
IP extra;
ip_reset(&extra);
res = addr_resolve(localhost, &ip, &extra);
ck_assert_msg(res > 0, "Resolver failed: %u, %s", errno, strerror(errno));

#if USE_IPV6
ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family);
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6,
ip.family.value);
ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
ip_ntoa(&ip, ip_str, sizeof(ip_str)));

ck_assert_msg(extra.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, extra.family);
ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET,
extra.family.value);
ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
ip_ntoa(&ip, ip_str, sizeof(ip_str)));
#else
ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, ip.family);
ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, ip.family.value);
ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
ip_ntoa(&ip, ip_str, sizeof(ip_str)));
#endif
Expand All @@ -105,14 +108,14 @@ START_TEST(test_ip_equal)
res = ip_equal(nullptr, &ip1);
ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res);

ip1.family = TOX_AF_INET;
ip1.family = net_family_ipv4;
ip1.ip.v4.uint32 = net_htonl(0x7F000001);

res = ip_equal(&ip1, &ip2);
ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_UNSPEC, 0} ): "
"expected result 0, got %u.", res);

ip2.family = TOX_AF_INET;
ip2.family = net_family_ipv4;
ip2.ip.v4.uint32 = net_htonl(0x7F000001);

res = ip_equal(&ip1, &ip2);
Expand All @@ -125,7 +128,7 @@ START_TEST(test_ip_equal)
ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): "
"expected result 0, got %u.", res);

ip2.family = TOX_AF_INET6;
ip2.family = net_family_ipv6;
ip2.ip.v6.uint32[0] = 0;
ip2.ip.v6.uint32[1] = 0;
ip2.ip.v6.uint32[2] = net_htonl(0xFFFF);
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ static inline IP get_loopback()
{
IP ip;
#if USE_IPV6
ip.family = TOX_AF_INET6;
ip.family = net_family_ipv6;
ip.ip.v6 = get_ip6_loopback();
#else
ip.family = TOX_AF_INET;
ip.family = net_family_ipv4;
ip.ip.v4 = get_ip4_loopback();
#endif
return ip;
Expand Down Expand Up @@ -349,7 +349,7 @@ static Onions *new_onions(uint16_t port)
return nullptr;
}

TCP_Proxy_Info inf = {{{0}}};
TCP_Proxy_Info inf = {{{{0}}}};
on->onion_c = new_onion_client(new_net_crypto(nullptr, dht, &inf));

if (!on->onion_c) {
Expand Down
Loading

0 comments on commit 498532c

Please sign in to comment.