diff options
author | zugz <mbays+tox@sdf.org> | 2017-03-12 11:09:24 +0100 |
---|---|---|
committer | Robin Lindén <dev@robinlinden.eu> | 2017-04-29 18:28:28 +0200 |
commit | 2474f43087f67cd9ac993a7ef78bfd81e2b083d7 (patch) | |
tree | 513804970ac5cd391dc2e560aca2f865d61dd0d3 /toxcore/DHT.c | |
parent | f6db9333e2d1262e7ba3846563c30f63c98ffa38 (diff) |
check if already in close list in ping_node_from_getnodes_ok()
fix index bounds check in add_to_close() and is_pk_in_close_list()
add TODO to write test for bug which fixed by this commit
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index dd7080ff..5ef24564 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -954,11 +954,13 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo | |||
954 | 954 | ||
955 | unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); | 955 | unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); |
956 | 956 | ||
957 | if (index > LCLIENT_LENGTH) { | 957 | if (index >= LCLIENT_LENGTH) { |
958 | index = LCLIENT_LENGTH - 1; | 958 | index = LCLIENT_LENGTH - 1; |
959 | } | 959 | } |
960 | 960 | ||
961 | for (i = 0; i < LCLIENT_NODES; ++i) { | 961 | for (i = 0; i < LCLIENT_NODES; ++i) { |
962 | /* TODO(iphydf): write bounds checking test to catch the case that | ||
963 | * index is left as >= LCLIENT_LENGTH */ | ||
962 | Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; | 964 | Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; |
963 | 965 | ||
964 | if (is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { | 966 | if (is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { |
@@ -1021,6 +1023,17 @@ static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_len | |||
1021 | return 0; | 1023 | return 0; |
1022 | } | 1024 | } |
1023 | 1025 | ||
1026 | static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port) | ||
1027 | { | ||
1028 | unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); | ||
1029 | |||
1030 | if (index >= LCLIENT_LENGTH) { | ||
1031 | index = LCLIENT_LENGTH - 1; | ||
1032 | } | ||
1033 | |||
1034 | return is_pk_in_client_list(dht->close_clientlist + index * LCLIENT_NODES, LCLIENT_NODES, public_key, ip_port); | ||
1035 | } | ||
1036 | |||
1024 | /* Check if the node obtained with a get_nodes with public_key should be pinged. | 1037 | /* Check if the node obtained with a get_nodes with public_key should be pinged. |
1025 | * NOTE: for best results call it after addto_lists; | 1038 | * NOTE: for best results call it after addto_lists; |
1026 | * | 1039 | * |
@@ -1035,7 +1048,8 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_k | |||
1035 | ret = 1; | 1048 | ret = 1; |
1036 | } | 1049 | } |
1037 | 1050 | ||
1038 | if (ret && !client_in_nodelist(dht->to_bootstrap, dht->num_to_bootstrap, public_key)) { | 1051 | if (ret && !client_in_nodelist(dht->to_bootstrap, dht->num_to_bootstrap, public_key) |
1052 | && !is_pk_in_close_list(dht, public_key, ip_port)) { | ||
1039 | if (dht->num_to_bootstrap < MAX_CLOSE_TO_BOOTSTRAP_NODES) { | 1053 | if (dht->num_to_bootstrap < MAX_CLOSE_TO_BOOTSTRAP_NODES) { |
1040 | memcpy(dht->to_bootstrap[dht->num_to_bootstrap].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 1054 | memcpy(dht->to_bootstrap[dht->num_to_bootstrap].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
1041 | dht->to_bootstrap[dht->num_to_bootstrap].ip_port = ip_port; | 1055 | dht->to_bootstrap[dht->num_to_bootstrap].ip_port = ip_port; |