diff options
-rw-r--r-- | toxcore/DHT.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 5e356b0a..4076826a 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1025,10 +1025,12 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) | |||
1025 | return -1; | 1025 | return -1; |
1026 | } | 1026 | } |
1027 | 1027 | ||
1028 | static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8_t *client_id, | 1028 | /* returns number of nodes not in kill-timeout */ |
1029 | static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8_t *client_id, | ||
1029 | Client_data *list, uint32_t list_count) | 1030 | Client_data *list, uint32_t list_count) |
1030 | { | 1031 | { |
1031 | uint32_t i; | 1032 | uint32_t i; |
1033 | uint8_t not_kill = 0; | ||
1032 | uint64_t temp_time = unix_time(); | 1034 | uint64_t temp_time = unix_time(); |
1033 | 1035 | ||
1034 | uint32_t num_nodes = 0; | 1036 | uint32_t num_nodes = 0; |
@@ -1043,6 +1045,8 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8 | |||
1043 | 1045 | ||
1044 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) | 1046 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) |
1045 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { | 1047 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { |
1048 | not_kill++; | ||
1049 | |||
1046 | if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { | 1050 | if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { |
1047 | send_ping_request(dht->ping, assoc->ip_port, client->client_id ); | 1051 | send_ping_request(dht->ping, assoc->ip_port, client->client_id ); |
1048 | assoc->last_pinged = temp_time; | 1052 | assoc->last_pinged = temp_time; |
@@ -1063,6 +1067,8 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8 | |||
1063 | client_id); | 1067 | client_id); |
1064 | *lastgetnode = temp_time; | 1068 | *lastgetnode = temp_time; |
1065 | } | 1069 | } |
1070 | |||
1071 | return not_kill; | ||
1066 | } | 1072 | } |
1067 | 1073 | ||
1068 | /* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request | 1074 | /* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request |
@@ -1082,8 +1088,28 @@ static void do_DHT_friends(DHT *dht) | |||
1082 | */ | 1088 | */ |
1083 | static void do_Close(DHT *dht) | 1089 | static void do_Close(DHT *dht) |
1084 | { | 1090 | { |
1085 | do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->c->self_public_key, | 1091 | uint8_t not_killed = do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->c->self_public_key, |
1086 | dht->close_clientlist, LCLIENT_LIST); | 1092 | dht->close_clientlist, LCLIENT_LIST); |
1093 | |||
1094 | if (!not_killed) { | ||
1095 | /* all existing nodes are at least KILL_NODE_TIMEOUT, | ||
1096 | * which means we are mute, as we only send packets to | ||
1097 | * nodes NOT in KILL_NODE_TIMEOUT | ||
1098 | * | ||
1099 | * so: reset all nodes to be BAD_NODE_TIMEOUT, but not | ||
1100 | * KILL_NODE_TIMEOUT, so we at least keep trying pings */ | ||
1101 | uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; | ||
1102 | size_t i, a; | ||
1103 | |||
1104 | for (i = 0; i < LCLIENT_LIST; i++) { | ||
1105 | Client_data *client = &dht->close_clientlist[i]; | ||
1106 | IPPTsPng *assoc; | ||
1107 | |||
1108 | for (a = 0, assoc = &client->assoc4; a < 2; a++, assoc = &client->assoc6) | ||
1109 | if (assoc->timestamp) | ||
1110 | assoc->timestamp = badonly; | ||
1111 | } | ||
1112 | } | ||
1087 | } | 1113 | } |
1088 | 1114 | ||
1089 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) | 1115 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) |