summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-06-05 00:51:46 +0300
committerDiadlo <polsha3@gmail.com>2017-06-05 02:02:33 +0300
commitffcee69db956ef368e08cf3ebcd3f8cbe21ffc13 (patch)
treed67a40616adba8fdced1cc11aca0efda8d8fd792 /toxcore/DHT.c
parent0eaa44650fcea3864ff045a197dbf580b31390c8 (diff)
Extract 'update_client_with_reset' function to reduce code duplication
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c94
1 files changed, 42 insertions, 52 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 6e425939..287f14aa 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -899,6 +899,30 @@ static void sort_client_list(Client_data *list, unsigned int length, const uint8
899 } 899 }
900} 900}
901 901
902static void update_client_with_reset(Client_data *client, const IP_Port *ip_port)
903{
904 IPPTsPng *ipptp_write = NULL;
905 IPPTsPng *ipptp_clear = NULL;
906
907 if (ip_port->ip.family == AF_INET) {
908 ipptp_write = &client->assoc4;
909 ipptp_clear = &client->assoc6;
910 } else {
911 ipptp_write = &client->assoc6;
912 ipptp_clear = &client->assoc4;
913 }
914
915 ipptp_write->ip_port = *ip_port;
916 ipptp_write->timestamp = unix_time();
917
918 ip_reset(&ipptp_write->ret_ip_port.ip);
919 ipptp_write->ret_ip_port.port = 0;
920 ipptp_write->ret_timestamp = 0;
921
922 /* zero out other address */
923 memset(ipptp_clear, 0, sizeof(*ipptp_clear));
924}
925
902/* Replace a first bad (or empty) node with this one 926/* Replace a first bad (or empty) node with this one
903 * or replace a possibly bad node (tests failed or not done yet) 927 * or replace a possibly bad node (tests failed or not done yet)
904 * that is further than any other in the list 928 * that is further than any other in the list
@@ -922,37 +946,18 @@ static int replace_all(Client_data *list,
922 return 0; 946 return 0;
923 } 947 }
924 948
925 if (store_node_ok(&list[1], public_key, comp_public_key) || store_node_ok(&list[0], public_key, comp_public_key)) { 949 if (!store_node_ok(&list[1], public_key, comp_public_key) &&
926 sort_client_list(list, length, comp_public_key); 950 !store_node_ok(&list[0], public_key, comp_public_key)) {
927 951 return 0;
928 IPPTsPng *ipptp_write = NULL; 952 }
929 IPPTsPng *ipptp_clear = NULL;
930
931 Client_data *client = &list[0];
932
933 if (ip_port.ip.family == AF_INET) {
934 ipptp_write = &client->assoc4;
935 ipptp_clear = &client->assoc6;
936 } else {
937 ipptp_write = &client->assoc6;
938 ipptp_clear = &client->assoc4;
939 }
940
941 id_copy(client->public_key, public_key);
942 ipptp_write->ip_port = ip_port;
943 ipptp_write->timestamp = unix_time();
944 953
945 ip_reset(&ipptp_write->ret_ip_port.ip); 954 sort_client_list(list, length, comp_public_key);
946 ipptp_write->ret_ip_port.port = 0;
947 ipptp_write->ret_timestamp = 0;
948 955
949 /* zero out other address */ 956 Client_data *client = &list[0];
950 memset(ipptp_clear, 0, sizeof(*ipptp_clear)); 957 id_copy(client->public_key, public_key);
951 958
952 return 1; 959 update_client_with_reset(client, &ip_port);
953 } 960 return 1;
954
955 return 0;
956} 961}
957 962
958/* Add node to close list. 963/* Add node to close list.
@@ -975,33 +980,18 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo
975 * index is left as >= LCLIENT_LENGTH */ 980 * index is left as >= LCLIENT_LENGTH */
976 Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; 981 Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i];
977 982
978 if (is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { 983 if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) ||
979 if (!simulate) { 984 !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) {
980 IPPTsPng *ipptp_write = NULL; 985 continue;
981 IPPTsPng *ipptp_clear = NULL; 986 }
982
983 if (ip_port.ip.family == AF_INET) {
984 ipptp_write = &client->assoc4;
985 ipptp_clear = &client->assoc6;
986 } else {
987 ipptp_write = &client->assoc6;
988 ipptp_clear = &client->assoc4;
989 }
990
991 id_copy(client->public_key, public_key);
992 ipptp_write->ip_port = ip_port;
993 ipptp_write->timestamp = unix_time();
994
995 ip_reset(&ipptp_write->ret_ip_port.ip);
996 ipptp_write->ret_ip_port.port = 0;
997 ipptp_write->ret_timestamp = 0;
998
999 /* zero out other address */
1000 memset(ipptp_clear, 0, sizeof(*ipptp_clear));
1001 }
1002 987
988 if (simulate) {
1003 return 0; 989 return 0;
1004 } 990 }
991
992 id_copy(client->public_key, public_key);
993 update_client_with_reset(client, &ip_port);
994 return 0;
1005 } 995 }
1006 996
1007 return -1; 997 return -1;