diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 99 |
1 files changed, 56 insertions, 43 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 19f8a30c..749eb78c 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -72,10 +72,11 @@ Client_data *DHT_get_close_list(DHT *dht) | |||
72 | return dht->close_clientlist; | 72 | return dht->close_clientlist; |
73 | } | 73 | } |
74 | 74 | ||
75 | /* Compares client_id1 and client_id2 with client_id | 75 | /* Compares client_id1 and client_id2 with client_id. |
76 | * return 0 if both are same distance | 76 | * |
77 | * return 1 if client_id1 is closer | 77 | * return 0 if both are same distance. |
78 | * return 2 if client_id2 is closer | 78 | * return 1 if client_id1 is closer. |
79 | * return 2 if client_id2 is closer. | ||
79 | */ | 80 | */ |
80 | static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2) | 81 | static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2) |
81 | { | 82 | { |
@@ -128,9 +129,9 @@ static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout) | |||
128 | /* Check if client with client_id is already in list of length length. | 129 | /* Check if client with client_id is already in list of length length. |
129 | * If it is then set its corresponding timestamp to current time. | 130 | * If it is then set its corresponding timestamp to current time. |
130 | * If the id is already in the list with a different ip_port, update it. | 131 | * If the id is already in the list with a different ip_port, update it. |
131 | * return True(1) or False(0) | 132 | * TODO: Maybe optimize this. |
132 | * | 133 | * |
133 | * TODO: maybe optimize this. | 134 | * return True(1) or False(0) |
134 | */ | 135 | */ |
135 | static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) | 136 | static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) |
136 | { | 137 | { |
@@ -156,7 +157,9 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id | |||
156 | } | 157 | } |
157 | 158 | ||
158 | /* Check if client with client_id is already in node format list of length length. | 159 | /* Check if client with client_id is already in node format list of length length. |
159 | * return True(1) or False(0) | 160 | * |
161 | * return 1 if true. | ||
162 | * return 2 if false. | ||
160 | */ | 163 | */ |
161 | static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) | 164 | static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) |
162 | { | 165 | { |
@@ -170,7 +173,8 @@ static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *clien | |||
170 | return 0; | 173 | return 0; |
171 | } | 174 | } |
172 | 175 | ||
173 | /* Returns the friend number from the client_id, or -1 if a failure occurs | 176 | /* return friend number from the client_id. |
177 | * return -1 if a failure occurs. | ||
174 | */ | 178 | */ |
175 | static int friend_number(DHT *dht, uint8_t *client_id) | 179 | static int friend_number(DHT *dht, uint8_t *client_id) |
176 | { | 180 | { |
@@ -274,9 +278,10 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list | |||
274 | return num_nodes; | 278 | return num_nodes; |
275 | } | 279 | } |
276 | 280 | ||
277 | /* Replace first bad (or empty) node with this one | 281 | /* Replace first bad (or empty) node with this one. |
278 | * return 0 if successful | 282 | * |
279 | * return 1 if not (list contains no bad nodes) | 283 | * return 0 if successful. |
284 | * return 1 if not (list contains no bad nodes). | ||
280 | */ | 285 | */ |
281 | static int replace_bad( Client_data *list, | 286 | static int replace_bad( Client_data *list, |
282 | uint32_t length, | 287 | uint32_t length, |
@@ -302,8 +307,8 @@ static int replace_bad( Client_data *list, | |||
302 | return 1; | 307 | return 1; |
303 | } | 308 | } |
304 | 309 | ||
305 | /*Sort the list. It will be sorted from furthest to closest. | 310 | /* Sort the list. It will be sorted from furthest to closest. |
306 | * Turns list into data that quick sort can use and reverts it back. | 311 | * Turns list into data that quick sort can use and reverts it back. |
307 | */ | 312 | */ |
308 | static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) | 313 | static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) |
309 | { | 314 | { |
@@ -350,7 +355,7 @@ static int replace_good( Client_data *list, | |||
350 | } | 355 | } |
351 | 356 | ||
352 | /* Attempt to add client with ip_port and client_id to the friends client list | 357 | /* Attempt to add client with ip_port and client_id to the friends client list |
353 | * and close_clientlist | 358 | * and close_clientlist. |
354 | */ | 359 | */ |
355 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) | 360 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) |
356 | { | 361 | { |
@@ -361,7 +366,7 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) | |||
361 | */ | 366 | */ |
362 | if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 367 | if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
363 | if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 368 | if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
364 | /* If we can't replace bad nodes we try replacing good ones */ | 369 | /* If we can't replace bad nodes we try replacing good ones. */ |
365 | replace_good( dht->close_clientlist, | 370 | replace_good( dht->close_clientlist, |
366 | LCLIENT_LIST, | 371 | LCLIENT_LIST, |
367 | client_id, | 372 | client_id, |
@@ -801,7 +806,8 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) | |||
801 | } | 806 | } |
802 | 807 | ||
803 | /* Send the given packet to node with client_id | 808 | /* Send the given packet to node with client_id |
804 | * returns -1 if failure. | 809 | * |
810 | * return -1 if failure. | ||
805 | */ | 811 | */ |
806 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | 812 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) |
807 | { | 813 | { |
@@ -815,11 +821,12 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | |||
815 | return -1; | 821 | return -1; |
816 | } | 822 | } |
817 | 823 | ||
818 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist | 824 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist. |
819 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big | 825 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
820 | * returns the number of ips returned | 826 | * |
821 | * return 0 if we are connected to friend or if no ips were found. | 827 | * return the number of ips returned. |
822 | * returns -1 if no such friend | 828 | * return 0 if we are connected to friend or if no ips were found. |
829 | * return -1 if no such friend. | ||
823 | */ | 830 | */ |
824 | static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | 831 | static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) |
825 | { | 832 | { |
@@ -836,7 +843,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
836 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 843 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
837 | client = &friend->client_list[i]; | 844 | client = &friend->client_list[i]; |
838 | 845 | ||
839 | /* If ip is not zero and node is good */ | 846 | /* If ip is not zero and node is good. */ |
840 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 847 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
841 | 848 | ||
842 | if (id_equal(client->client_id, friend->client_id)) | 849 | if (id_equal(client->client_id, friend->client_id)) |
@@ -852,9 +859,9 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
852 | 859 | ||
853 | 860 | ||
854 | /* Send the following packet to everyone who tells us they are connected to friend_id. | 861 | /* Send the following packet to everyone who tells us they are connected to friend_id. |
855 | * returns the number of nodes it sent the packet to. | ||
856 | * | 862 | * |
857 | * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. | 863 | * return ip for friend. |
864 | * return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2). | ||
858 | */ | 865 | */ |
859 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 866 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
860 | { | 867 | { |
@@ -878,7 +885,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
878 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 885 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
879 | client = &friend->client_list[i]; | 886 | client = &friend->client_list[i]; |
880 | 887 | ||
881 | /* If ip is not zero and node is good */ | 888 | /* If ip is not zero and node is good. */ |
882 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 889 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
883 | int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length); | 890 | int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length); |
884 | 891 | ||
@@ -891,8 +898,9 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
891 | } | 898 | } |
892 | 899 | ||
893 | /* Send the following packet to one random person who tells us they are connected to friend_id. | 900 | /* Send the following packet to one random person who tells us they are connected to friend_id. |
894 | * returns the number of nodes it sent the packet to | 901 | * |
895 | */ | 902 | * return number of nodes the packet was sent to. |
903 | */ | ||
896 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 904 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
897 | { | 905 | { |
898 | int num = friend_number(dht, friend_id); | 906 | int num = friend_number(dht, friend_id); |
@@ -931,9 +939,10 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
931 | 939 | ||
932 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. | 940 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. |
933 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. | 941 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
934 | * returns the number of ips returned | 942 | * |
935 | * return 0 if we are connected to friend or if no ips were found. | 943 | * return number of ips returned. |
936 | * returns -1 if no such friend | 944 | * return 0 if we are connected to friend or if no ips were found. |
945 | * return -1 if no such friend. | ||
937 | */ | 946 | */ |
938 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) | 947 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) |
939 | { | 948 | { |
@@ -1014,7 +1023,8 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, | |||
1014 | /* Get the most common ip in the ip_portlist. | 1023 | /* Get the most common ip in the ip_portlist. |
1015 | * Only return ip if it appears in list min_num or more. | 1024 | * Only return ip if it appears in list min_num or more. |
1016 | * len must not be bigger than MAX_FRIEND_CLIENTS. | 1025 | * len must not be bigger than MAX_FRIEND_CLIENTS. |
1017 | * return ip of 0 if failure. | 1026 | * |
1027 | * return ip of 0 if failure. | ||
1018 | */ | 1028 | */ |
1019 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | 1029 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) |
1020 | { | 1030 | { |
@@ -1040,9 +1050,10 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | |||
1040 | } | 1050 | } |
1041 | 1051 | ||
1042 | /* Return all the ports for one ip in a list. | 1052 | /* Return all the ports for one ip in a list. |
1043 | * portlist must be at least len long | 1053 | * portlist must be at least len long, |
1044 | * where len is the length of ip_portlist | 1054 | * where len is the length of ip_portlist. |
1045 | * returns the number of ports and puts the list of ports in portlist. | 1055 | * |
1056 | * return number of ports and puts the list of ports in portlist. | ||
1046 | */ | 1057 | */ |
1047 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) | 1058 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) |
1048 | { | 1059 | { |
@@ -1068,7 +1079,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
1068 | uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; | 1079 | uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; |
1069 | 1080 | ||
1070 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { | 1081 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { |
1071 | /* TODO: improve port guessing algorithm */ | 1082 | /* TODO: Improve port guessing algorithm. */ |
1072 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); | 1083 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); |
1073 | IP_Port pinging = {{ip, htons(port), 0}}; | 1084 | IP_Port pinging = {{ip, htons(port), 0}}; |
1074 | send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); | 1085 | send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); |
@@ -1086,7 +1097,7 @@ static void do_NAT(DHT *dht) | |||
1086 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 1097 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1087 | int num = friend_iplist(dht, ip_list, i); | 1098 | int num = friend_iplist(dht, ip_list, i); |
1088 | 1099 | ||
1089 | /* If already connected or friend is not online don't try to hole punch */ | 1100 | /* If already connected or friend is not online don't try to hole punch. */ |
1090 | if (num < MAX_FRIEND_CLIENTS / 2) | 1101 | if (num < MAX_FRIEND_CLIENTS / 2) |
1091 | continue; | 1102 | continue; |
1092 | 1103 | ||
@@ -1124,8 +1135,9 @@ static void do_NAT(DHT *dht) | |||
1124 | * If the list is full the nodes farthest from our client_id are replaced. | 1135 | * If the list is full the nodes farthest from our client_id are replaced. |
1125 | * The purpose of this list is to enable quick integration of new nodes into the | 1136 | * The purpose of this list is to enable quick integration of new nodes into the |
1126 | * network while preventing amplification attacks. | 1137 | * network while preventing amplification attacks. |
1127 | * return 0 if node was added. | 1138 | * |
1128 | * return -1 if node was not added. | 1139 | * return 0 if node was added. |
1140 | * return -1 if node was not added. | ||
1129 | */ | 1141 | */ |
1130 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | 1142 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) |
1131 | { | 1143 | { |
@@ -1156,7 +1168,7 @@ int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | |||
1156 | } | 1168 | } |
1157 | 1169 | ||
1158 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. | 1170 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. |
1159 | * This function must be run at least once every TIME_TOPING seconds | 1171 | * This function must be run at least once every TIME_TOPING seconds. |
1160 | */ | 1172 | */ |
1161 | static void do_toping(DHT *dht) | 1173 | static void do_toping(DHT *dht) |
1162 | { | 1174 | { |
@@ -1233,8 +1245,9 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1233 | } | 1245 | } |
1234 | 1246 | ||
1235 | /* Load the DHT from data of size size. | 1247 | /* Load the DHT from data of size size. |
1236 | * return -1 if failure. | 1248 | * |
1237 | * return 0 if success. | 1249 | * return -1 if failure. |
1250 | * return 0 if success. | ||
1238 | */ | 1251 | */ |
1239 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | 1252 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) |
1240 | { | 1253 | { |
@@ -1278,8 +1291,8 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | |||
1278 | return 0; | 1291 | return 0; |
1279 | } | 1292 | } |
1280 | 1293 | ||
1281 | /* returns 0 if we are not connected to the DHT. | 1294 | /* return 0 if we are not connected to the DHT. |
1282 | * returns 1 if we are. | 1295 | * return 1 if we are. |
1283 | */ | 1296 | */ |
1284 | int DHT_isconnected(DHT *dht) | 1297 | int DHT_isconnected(DHT *dht) |
1285 | { | 1298 | { |