summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/network_test.c35
-rw-r--r--toxcore/DHT.c15
2 files changed, 28 insertions, 22 deletions
diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c
index d65b8100..35209bea 100644
--- a/auto_tests/network_test.c
+++ b/auto_tests/network_test.c
@@ -23,6 +23,7 @@ START_TEST(test_addr_resolv_localhost)
23#endif 23#endif
24 24
25 const char localhost[] = "localhost"; 25 const char localhost[] = "localhost";
26 int localhost_split = 0;
26 27
27 IP ip; 28 IP ip;
28 ip_init(&ip, 0); 29 ip_init(&ip, 0);
@@ -39,27 +40,35 @@ START_TEST(test_addr_resolv_localhost)
39 ip_init(&ip, 1); 40 ip_init(&ip, 1);
40 res = addr_resolve(localhost, &ip, NULL); 41 res = addr_resolve(localhost, &ip, NULL);
41 42
42 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); 43 if (res < 1) {
43 44 res = addr_resolve("ip6-localhost", &ip, NULL);
44 if (res > 0) { 45 localhost_split = 1;
45 ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
46 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
47 } 46 }
48 47
49 ip_init(&ip, 1);
50 ip.family = AF_UNSPEC;
51 IP extra;
52 ip_reset(&extra);
53 res = addr_resolve(localhost, &ip, &extra);
54
55 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); 48 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
56 49
57 if (res > 0) { 50 if (res > 0) {
58 ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); 51 ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
59 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); 52 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
53 }
60 54
61 ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); 55 if (!localhost_split) {
62 ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); 56 ip_init(&ip, 1);
57 ip.family = AF_UNSPEC;
58 IP extra;
59 ip_reset(&extra);
60 res = addr_resolve(localhost, &ip, &extra);
61 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
62
63 if (res > 0) {
64 ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
65 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
66
67 ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family);
68 ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr));
69 }
70 } else {
71 printf("Localhost seems to be split in two.\n");
63 } 72 }
64} 73}
65END_TEST 74END_TEST
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index b736d55e..d5808313 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2135,21 +2135,18 @@ uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num)
2135 if (max_num == 0) 2135 if (max_num == 0)
2136 return 0; 2136 return 0;
2137 2137
2138 if (dht->num_friends == 0)
2139 return 0;
2140
2138 uint16_t count = 0; 2141 uint16_t count = 0;
2139 Client_data *list = NULL; 2142 Client_data *list = NULL;
2140 uint16_t list_size = 0; 2143 uint16_t list_size = 0;
2141 uint32_t i; 2144 uint32_t i;
2142 2145
2143 for (i = 0; i < max_num; ++i) { 2146 for (i = 0; i < max_num; ++i) {
2144 uint16_t rand_num = rand() % (dht->num_friends + 1); 2147 uint16_t rand_num = rand() % (dht->num_friends);
2145 2148 list = dht->friends_list[rand_num].client_list;
2146 if (rand_num == dht->num_friends) { 2149 list_size = MAX_FRIEND_CLIENTS;
2147 list = dht->close_clientlist;
2148 list_size = LCLIENT_LIST;
2149 } else {
2150 list = dht->friends_list[rand_num].client_list;
2151 list_size = MAX_FRIEND_CLIENTS;
2152 }
2153 2150
2154 uint8_t LAN_ok = 1; 2151 uint8_t LAN_ok = 1;
2155 2152