Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish @Diadlo's network Family abstraction. #823

Merged
merged 1 commit into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,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 @@ -57,7 +56,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[random_u32() % NUM_PORTS]);
Expand Down Expand Up @@ -157,7 +156,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 @@ -31,10 +31,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 @@ -113,7 +113,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 = random_u32() % length;
Expand Down Expand Up @@ -188,7 +188,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 @@ -232,7 +232,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 @@ -294,7 +294,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 @@ -547,7 +547,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 @@ -744,8 +744,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 @@ -44,7 +44,7 @@ START_TEST(test_addr_resolv_localhost)
net_kill_strerror(strerror);

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 @@ -62,7 +62,8 @@ START_TEST(test_addr_resolv_localhost)
ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror);
net_kill_strerror(strerror);

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 @@ -73,7 +74,7 @@ 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);
Expand All @@ -83,15 +84,17 @@ START_TEST(test_addr_resolv_localhost)
net_kill_strerror(strerror);

#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 (%d), 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 (%d), 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 @@ -114,14 +117,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 @@ -134,7 +137,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 @@ -375,7 +375,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
return nullptr;
}

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

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