diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 38552184..47595d06 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, |
@@ -798,7 +803,8 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) | |||
798 | } | 803 | } |
799 | 804 | ||
800 | /* Send the given packet to node with client_id | 805 | /* Send the given packet to node with client_id |
801 | * returns -1 if failure. | 806 | * |
807 | * return -1 if failure. | ||
802 | */ | 808 | */ |
803 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | 809 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) |
804 | { | 810 | { |
@@ -812,11 +818,12 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | |||
812 | return -1; | 818 | return -1; |
813 | } | 819 | } |
814 | 820 | ||
815 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist | 821 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist. |
816 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big | 822 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
817 | * returns the number of ips returned | 823 | * |
818 | * return 0 if we are connected to friend or if no ips were found. | 824 | * return the number of ips returned. |
819 | * returns -1 if no such friend | 825 | * return 0 if we are connected to friend or if no ips were found. |
826 | * return -1 if no such friend. | ||
820 | */ | 827 | */ |
821 | static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | 828 | static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) |
822 | { | 829 | { |
@@ -833,7 +840,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
833 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 840 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
834 | client = &friend->client_list[i]; | 841 | client = &friend->client_list[i]; |
835 | 842 | ||
836 | /* If ip is not zero and node is good */ | 843 | /* If ip is not zero and node is good. */ |
837 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 844 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
838 | 845 | ||
839 | if (id_equal(client->client_id, friend->client_id)) | 846 | if (id_equal(client->client_id, friend->client_id)) |
@@ -849,9 +856,9 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
849 | 856 | ||
850 | 857 | ||
851 | /* Send the following packet to everyone who tells us they are connected to friend_id. | 858 | /* Send the following packet to everyone who tells us they are connected to friend_id. |
852 | * returns the number of nodes it sent the packet to. | 859 | * |
853 | * | 860 | * return ip for friend. |
854 | * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. | 861 | * return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2). |
855 | */ | 862 | */ |
856 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 863 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
857 | { | 864 | { |
@@ -875,7 +882,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
875 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 882 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
876 | client = &friend->client_list[i]; | 883 | client = &friend->client_list[i]; |
877 | 884 | ||
878 | /* If ip is not zero and node is good */ | 885 | /* If ip is not zero and node is good. */ |
879 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 886 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
880 | if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length) | 887 | if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length) |
881 | ++sent; | 888 | ++sent; |
@@ -886,8 +893,9 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
886 | } | 893 | } |
887 | 894 | ||
888 | /* Send the following packet to one random person who tells us they are connected to friend_id. | 895 | /* Send the following packet to one random person who tells us they are connected to friend_id. |
889 | * returns the number of nodes it sent the packet to | 896 | * |
890 | */ | 897 | * return number of nodes the packet was sent to. |
898 | */ | ||
891 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 899 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
892 | { | 900 | { |
893 | int num = friend_number(dht, friend_id); | 901 | int num = friend_number(dht, friend_id); |
@@ -924,9 +932,10 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
924 | 932 | ||
925 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. | 933 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. |
926 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. | 934 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
927 | * returns the number of ips returned | 935 | * |
928 | * return 0 if we are connected to friend or if no ips were found. | 936 | * return number of ips returned. |
929 | * returns -1 if no such friend | 937 | * return 0 if we are connected to friend or if no ips were found. |
938 | * return -1 if no such friend. | ||
930 | */ | 939 | */ |
931 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) | 940 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) |
932 | { | 941 | { |
@@ -1007,7 +1016,8 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, | |||
1007 | /* Get the most common ip in the ip_portlist. | 1016 | /* Get the most common ip in the ip_portlist. |
1008 | * Only return ip if it appears in list min_num or more. | 1017 | * Only return ip if it appears in list min_num or more. |
1009 | * len must not be bigger than MAX_FRIEND_CLIENTS. | 1018 | * len must not be bigger than MAX_FRIEND_CLIENTS. |
1010 | * return ip of 0 if failure. | 1019 | * |
1020 | * return ip of 0 if failure. | ||
1011 | */ | 1021 | */ |
1012 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | 1022 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) |
1013 | { | 1023 | { |
@@ -1033,9 +1043,10 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | |||
1033 | } | 1043 | } |
1034 | 1044 | ||
1035 | /* Return all the ports for one ip in a list. | 1045 | /* Return all the ports for one ip in a list. |
1036 | * portlist must be at least len long | 1046 | * portlist must be at least len long, |
1037 | * where len is the length of ip_portlist | 1047 | * where len is the length of ip_portlist. |
1038 | * returns the number of ports and puts the list of ports in portlist. | 1048 | * |
1049 | * return number of ports and puts the list of ports in portlist. | ||
1039 | */ | 1050 | */ |
1040 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) | 1051 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) |
1041 | { | 1052 | { |
@@ -1061,7 +1072,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
1061 | uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; | 1072 | uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; |
1062 | 1073 | ||
1063 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { | 1074 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { |
1064 | /* TODO: improve port guessing algorithm */ | 1075 | /* TODO: Improve port guessing algorithm. */ |
1065 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); | 1076 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); |
1066 | IP_Port pinging = {{ip, htons(port)}}; | 1077 | IP_Port pinging = {{ip, htons(port)}}; |
1067 | send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); | 1078 | send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); |
@@ -1079,7 +1090,7 @@ static void do_NAT(DHT *dht) | |||
1079 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 1090 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1080 | int num = friend_iplist(dht, ip_list, i); | 1091 | int num = friend_iplist(dht, ip_list, i); |
1081 | 1092 | ||
1082 | /* If already connected or friend is not online don't try to hole punch */ | 1093 | /* If already connected or friend is not online don't try to hole punch. */ |
1083 | if (num < MAX_FRIEND_CLIENTS / 2) | 1094 | if (num < MAX_FRIEND_CLIENTS / 2) |
1084 | continue; | 1095 | continue; |
1085 | 1096 | ||
@@ -1117,8 +1128,9 @@ static void do_NAT(DHT *dht) | |||
1117 | * If the list is full the nodes farthest from our client_id are replaced. | 1128 | * If the list is full the nodes farthest from our client_id are replaced. |
1118 | * The purpose of this list is to enable quick integration of new nodes into the | 1129 | * The purpose of this list is to enable quick integration of new nodes into the |
1119 | * network while preventing amplification attacks. | 1130 | * network while preventing amplification attacks. |
1120 | * return 0 if node was added. | 1131 | * |
1121 | * return -1 if node was not added. | 1132 | * return 0 if node was added. |
1133 | * return -1 if node was not added. | ||
1122 | */ | 1134 | */ |
1123 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | 1135 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) |
1124 | { | 1136 | { |
@@ -1149,7 +1161,7 @@ int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | |||
1149 | } | 1161 | } |
1150 | 1162 | ||
1151 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. | 1163 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. |
1152 | * This function must be run at least once every TIME_TOPING seconds | 1164 | * This function must be run at least once every TIME_TOPING seconds. |
1153 | */ | 1165 | */ |
1154 | static void do_toping(DHT *dht) | 1166 | static void do_toping(DHT *dht) |
1155 | { | 1167 | { |
@@ -1226,8 +1238,9 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1226 | } | 1238 | } |
1227 | 1239 | ||
1228 | /* Load the DHT from data of size size. | 1240 | /* Load the DHT from data of size size. |
1229 | * return -1 if failure. | 1241 | * |
1230 | * return 0 if success. | 1242 | * return -1 if failure. |
1243 | * return 0 if success. | ||
1231 | */ | 1244 | */ |
1232 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | 1245 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) |
1233 | { | 1246 | { |
@@ -1271,8 +1284,8 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | |||
1271 | return 0; | 1284 | return 0; |
1272 | } | 1285 | } |
1273 | 1286 | ||
1274 | /* returns 0 if we are not connected to the DHT. | 1287 | /* return 0 if we are not connected to the DHT. |
1275 | * returns 1 if we are. | 1288 | * return 1 if we are. |
1276 | */ | 1289 | */ |
1277 | int DHT_isconnected(DHT *dht) | 1290 | int DHT_isconnected(DHT *dht) |
1278 | { | 1291 | { |