diff options
author | Diadlo <polsha3@gmail.com> | 2017-06-05 01:06:35 +0300 |
---|---|---|
committer | Diadlo <polsha3@gmail.com> | 2017-06-05 02:02:45 +0300 |
commit | d9503763b353ca472a8cdc0eb6d8bf39edfeca2a (patch) | |
tree | 4348ce952576929863a2ae741d2900884ae07e89 /toxcore/DHT.c | |
parent | ffcee69db956ef368e08cf3ebcd3f8cbe21ffc13 (diff) |
Extract 'update_client_data' to reduce code duplication
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 73 |
1 files changed, 35 insertions, 38 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 287f14aa..08faa635 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1164,15 +1164,36 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) | |||
1164 | return used; | 1164 | return used; |
1165 | } | 1165 | } |
1166 | 1166 | ||
1167 | /* If public_key is a friend or us, update ret_ip_port | 1167 | static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, const uint8_t *pk) |
1168 | * nodepublic_key is the id of the node that sent us this info. | ||
1169 | */ | ||
1170 | static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) | ||
1171 | { | 1168 | { |
1172 | uint64_t temp_time = unix_time(); | 1169 | uint64_t temp_time = unix_time(); |
1170 | uint32_t index = index_of_client_pk(array, size, pk); | ||
1173 | 1171 | ||
1174 | uint32_t used = 0; | 1172 | if (index == UINT32_MAX) { |
1173 | return false; | ||
1174 | } | ||
1175 | 1175 | ||
1176 | Client_data *data = &array[index]; | ||
1177 | IPPTsPng *assoc; | ||
1178 | |||
1179 | if (ip_port.ip.family == TOX_AF_INET) { | ||
1180 | assoc = &data->assoc4; | ||
1181 | } else if (ip_port.ip.family == TOX_AF_INET6) { | ||
1182 | assoc = &data->assoc6; | ||
1183 | } else { | ||
1184 | return true; | ||
1185 | } | ||
1186 | |||
1187 | assoc->ret_ip_port = ip_port; | ||
1188 | assoc->ret_timestamp = temp_time; | ||
1189 | return true; | ||
1190 | } | ||
1191 | |||
1192 | /* If public_key is a friend or us, update ret_ip_port | ||
1193 | * nodepublic_key is the id of the node that sent us this info. | ||
1194 | */ | ||
1195 | static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) | ||
1196 | { | ||
1176 | /* convert IPv4-in-IPv6 to IPv4 */ | 1197 | /* convert IPv4-in-IPv6 to IPv4 */ |
1177 | if ((ip_port.ip.family == AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { | 1198 | if ((ip_port.ip.family == AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { |
1178 | ip_port.ip.family = AF_INET; | 1199 | ip_port.ip.family = AF_INET; |
@@ -1180,43 +1201,19 @@ static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key | |||
1180 | } | 1201 | } |
1181 | 1202 | ||
1182 | if (id_equal(public_key, dht->self_public_key)) { | 1203 | if (id_equal(public_key, dht->self_public_key)) { |
1183 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 1204 | update_client_data(dht->close_clientlist, LCLIENT_LIST, ip_port, nodepublic_key); |
1184 | if (id_equal(nodepublic_key, dht->close_clientlist[i].public_key)) { | 1205 | return; |
1185 | if (ip_port.ip.family == AF_INET) { | 1206 | } |
1186 | dht->close_clientlist[i].assoc4.ret_ip_port = ip_port; | ||
1187 | dht->close_clientlist[i].assoc4.ret_timestamp = temp_time; | ||
1188 | } else if (ip_port.ip.family == AF_INET6) { | ||
1189 | dht->close_clientlist[i].assoc6.ret_ip_port = ip_port; | ||
1190 | dht->close_clientlist[i].assoc6.ret_timestamp = temp_time; | ||
1191 | } | ||
1192 | 1207 | ||
1193 | ++used; | 1208 | for (uint32_t i = 0; i < dht->num_friends; ++i) { |
1194 | break; | 1209 | if (id_equal(public_key, dht->friends_list[i].public_key)) { |
1195 | } | 1210 | Client_data *client_list = dht->friends_list[i].client_list; |
1196 | } | 1211 | |
1197 | } else { | 1212 | if (update_client_data(client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key)) { |
1198 | for (uint32_t i = 0; i < dht->num_friends; ++i) { | 1213 | return; |
1199 | if (id_equal(public_key, dht->friends_list[i].public_key)) { | ||
1200 | for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | ||
1201 | if (id_equal(nodepublic_key, dht->friends_list[i].client_list[j].public_key)) { | ||
1202 | if (ip_port.ip.family == AF_INET) { | ||
1203 | dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port; | ||
1204 | dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time; | ||
1205 | } else if (ip_port.ip.family == AF_INET6) { | ||
1206 | dht->friends_list[i].client_list[j].assoc6.ret_ip_port = ip_port; | ||
1207 | dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time; | ||
1208 | } | ||
1209 | |||
1210 | ++used; | ||
1211 | goto end; | ||
1212 | } | ||
1213 | } | ||
1214 | } | 1214 | } |
1215 | } | 1215 | } |
1216 | } | 1216 | } |
1217 | |||
1218 | end: | ||
1219 | return 0; | ||
1220 | } | 1217 | } |
1221 | 1218 | ||
1222 | /* Send a getnodes request. | 1219 | /* Send a getnodes request. |