From 00100ea335fe2d65e6c42d2e73fe23842b92bd6a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 21 Jan 2014 11:14:16 -0500 Subject: [PATCH] random_nodes_path should return slightly better paths. Fixed test in network_test failing on some machines because of localhost ipv6 issues. --- auto_tests/network_test.c | 33 +++++++++++++++++++++------------ toxcore/DHT.c | 15 ++++++--------- 2 files changed, 27 insertions(+), 21 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) #endif const char localhost[] = "localhost"; + int localhost_split = 0; IP ip; ip_init(&ip, 0); @@ -39,6 +40,11 @@ START_TEST(test_addr_resolv_localhost) ip_init(&ip, 1); res = addr_resolve(localhost, &ip, NULL); + if (res < 1) { + res = addr_resolve("ip6-localhost", &ip, NULL); + localhost_split = 1; + } + ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); if (res > 0) { @@ -46,20 +52,23 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); } - ip_init(&ip, 1); - ip.family = AF_UNSPEC; - IP extra; - ip_reset(&extra); - res = addr_resolve(localhost, &ip, &extra); + if (!localhost_split) { + ip_init(&ip, 1); + ip.family = AF_UNSPEC; + IP extra; + ip_reset(&extra); + res = addr_resolve(localhost, &ip, &extra); + ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); + if (res > 0) { + ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); + ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); - if (res > 0) { - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); - ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); - - ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); - ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); + ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); + ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); + } + } else { + printf("Localhost seems to be split in two.\n"); } } END_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) if (max_num == 0) return 0; + if (dht->num_friends == 0) + return 0; + uint16_t count = 0; Client_data *list = NULL; uint16_t list_size = 0; uint32_t i; for (i = 0; i < max_num; ++i) { - uint16_t rand_num = rand() % (dht->num_friends + 1); - - if (rand_num == dht->num_friends) { - list = dht->close_clientlist; - list_size = LCLIENT_LIST; - } else { - list = dht->friends_list[rand_num].client_list; - list_size = MAX_FRIEND_CLIENTS; - } + uint16_t rand_num = rand() % (dht->num_friends); + list = dht->friends_list[rand_num].client_list; + list_size = MAX_FRIEND_CLIENTS; uint8_t LAN_ok = 1;