summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c56
1 files changed, 28 insertions, 28 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 08b4710e..6375b86b 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -119,7 +119,7 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
119 * return 1 if client_id1 is closer 119 * return 1 if client_id1 is closer
120 * return 2 if client_id2 is closer 120 * return 2 if client_id2 is closer
121 */ 121 */
122int id_closest(uint8_t * id, uint8_t * id1, uint8_t * id2) 122static int id_closest(uint8_t * id, uint8_t * id1, uint8_t * id2)
123{ 123{
124 size_t i; 124 size_t i;
125 uint8_t distance1, distance2; 125 uint8_t distance1, distance2;
@@ -137,17 +137,17 @@ int id_closest(uint8_t * id, uint8_t * id1, uint8_t * id2)
137 return 0; 137 return 0;
138} 138}
139 139
140int ipport_equal(IP_Port a, IP_Port b) 140static int ipport_equal(IP_Port a, IP_Port b)
141{ 141{
142 return (a.ip.i == b.ip.i) && (a.port == b.port); 142 return (a.ip.i == b.ip.i) && (a.port == b.port);
143} 143}
144 144
145int id_equal(uint8_t* a, uint8_t* b) 145static int id_equal(uint8_t* a, uint8_t* b)
146{ 146{
147 return memcmp(a, b, CLIENT_ID_SIZE) == 0; 147 return memcmp(a, b, CLIENT_ID_SIZE) == 0;
148} 148}
149 149
150int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout) 150static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
151{ 151{
152 return timestamp + timeout <= time_now; 152 return timestamp + timeout <= time_now;
153} 153}
@@ -159,7 +159,7 @@ int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
159 * 159 *
160 * TODO: maybe optimize this. 160 * TODO: maybe optimize this.
161 */ 161 */
162int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) 162static int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
163{ 163{
164 uint32_t i; 164 uint32_t i;
165 uint64_t temp_time = unix_time(); 165 uint64_t temp_time = unix_time();
@@ -184,7 +184,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
184/* check if client with client_id is already in node format list of length length. 184/* check if client with client_id is already in node format list of length length.
185 * return True(1) or False(0) 185 * return True(1) or False(0)
186 */ 186 */
187int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) 187static int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
188{ 188{
189 uint32_t i; 189 uint32_t i;
190 190
@@ -215,7 +215,7 @@ static int friend_number(uint8_t * client_id)
215 * 215 *
216 * TODO: For the love of based Allah make this function cleaner and much more efficient. 216 * TODO: For the love of based Allah make this function cleaner and much more efficient.
217 */ 217 */
218int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) 218static int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
219{ 219{
220 uint32_t i, j, k; 220 uint32_t i, j, k;
221 uint64_t temp_time = unix_time(); 221 uint64_t temp_time = unix_time();
@@ -301,7 +301,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
301 * return 0 if successful 301 * return 0 if successful
302 * return 1 if not (list contains no bad nodes) 302 * return 1 if not (list contains no bad nodes)
303 */ 303 */
304int replace_bad( Client_data * list, 304static int replace_bad( Client_data * list,
305 uint32_t length, 305 uint32_t length,
306 uint8_t * client_id, 306 uint8_t * client_id,
307 IP_Port ip_port ) 307 IP_Port ip_port )
@@ -325,7 +325,7 @@ int replace_bad( Client_data * list,
325} 325}
326 326
327/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ 327/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
328int replace_good( Client_data * list, 328static int replace_good( Client_data * list,
329 uint32_t length, 329 uint32_t length,
330 uint8_t * client_id, 330 uint8_t * client_id,
331 IP_Port ip_port, 331 IP_Port ip_port,
@@ -351,7 +351,7 @@ int replace_good( Client_data * list,
351/* Attempt to add client with ip_port and client_id to the friends client list 351/* Attempt to add client with ip_port and client_id to the friends client list
352 * and close_clientlist 352 * and close_clientlist
353 */ 353 */
354void addto_lists(IP_Port ip_port, uint8_t * client_id) 354static void addto_lists(IP_Port ip_port, uint8_t * client_id)
355{ 355{
356 uint32_t i; 356 uint32_t i;
357 357
@@ -393,7 +393,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
393/* If client_id is a friend or us, update ret_ip_port 393/* If client_id is a friend or us, update ret_ip_port
394 * nodeclient_id is the id of the node that sent us this info 394 * nodeclient_id is the id of the node that sent us this info
395 */ 395 */
396void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id) 396static void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id)
397{ 397{
398 uint32_t i, j; 398 uint32_t i, j;
399 uint64_t temp_time = unix_time(); 399 uint64_t temp_time = unix_time();
@@ -431,7 +431,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
431 * 431 *
432 * TODO: optimize this 432 * TODO: optimize this
433 */ 433 */
434int is_pinging(IP_Port ip_port, uint64_t ping_id) 434static int is_pinging(IP_Port ip_port, uint64_t ping_id)
435{ 435{
436 uint32_t i; 436 uint32_t i;
437 uint8_t pinging; 437 uint8_t pinging;
@@ -456,7 +456,7 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
456} 456}
457 457
458/* Same as last function but for get_node requests. */ 458/* Same as last function but for get_node requests. */
459int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) 459static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
460{ 460{
461 uint32_t i; 461 uint32_t i;
462 uint8_t pinging; 462 uint8_t pinging;
@@ -486,7 +486,7 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
486 * 486 *
487 * TODO: optimize this 487 * TODO: optimize this
488 */ 488 */
489uint64_t add_pinging(IP_Port ip_port) 489static uint64_t add_pinging(IP_Port ip_port)
490{ 490{
491 uint32_t i, j; 491 uint32_t i, j;
492 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 492 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
@@ -507,7 +507,7 @@ uint64_t add_pinging(IP_Port ip_port)
507} 507}
508 508
509/* Same but for get node requests */ 509/* Same but for get node requests */
510uint64_t add_gettingnodes(IP_Port ip_port) 510static uint64_t add_gettingnodes(IP_Port ip_port)
511{ 511{
512 uint32_t i, j; 512 uint32_t i, j;
513 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 513 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
@@ -676,7 +676,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
676/* Packet handling functions, one to handle each types of packets we receive 676/* Packet handling functions, one to handle each types of packets we receive
677 * Returns 0 if handled correctly, 1 if packet is bad. 677 * Returns 0 if handled correctly, 1 if packet is bad.
678 */ 678 */
679int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) 679static int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
680{ 680{
681 uint64_t ping_id; 681 uint64_t ping_id;
682 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) 682 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
@@ -702,7 +702,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
702 return 0; 702 return 0;
703} 703}
704 704
705int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) 705static int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
706{ 706{
707 uint64_t ping_id; 707 uint64_t ping_id;
708 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) 708 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
@@ -729,7 +729,7 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
729 return 1; 729 return 1;
730} 730}
731 731
732int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) 732static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
733{ 733{
734 uint64_t ping_id; 734 uint64_t ping_id;
735 735
@@ -761,7 +761,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
761 return 0; 761 return 0;
762} 762}
763 763
764int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) 764static int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
765{ 765{
766 uint64_t ping_id; 766 uint64_t ping_id;
767 uint32_t cid_size = 1 + CLIENT_ID_SIZE; 767 uint32_t cid_size = 1 + CLIENT_ID_SIZE;
@@ -873,7 +873,7 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
873/* Ping each client in the "friends" list every 60 seconds. Send a get nodes request 873/* Ping each client in the "friends" list every 60 seconds. Send a get nodes request
874 * every 20 seconds to a random good node for each "friend" in our "friends" list. 874 * every 20 seconds to a random good node for each "friend" in our "friends" list.
875 */ 875 */
876void doDHTFriends() 876static void doDHTFriends(void)
877{ 877{
878 uint32_t i, j; 878 uint32_t i, j;
879 uint64_t temp_time = unix_time(); 879 uint64_t temp_time = unix_time();
@@ -912,7 +912,7 @@ static uint64_t close_lastgetnodes;
912/* Ping each client in the close nodes list every 60 seconds. 912/* Ping each client in the close nodes list every 60 seconds.
913 * Send a get nodes request every 20 seconds to a random good node in the list. 913 * Send a get nodes request every 20 seconds to a random good node in the list.
914 */ 914 */
915void doClose() 915static void doClose(void)
916{ 916{
917 uint32_t i; 917 uint32_t i;
918 uint64_t temp_time = unix_time(); 918 uint64_t temp_time = unix_time();
@@ -1029,7 +1029,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1029/* Send the following packet to one random person who tells us they are connected to friend_id 1029/* Send the following packet to one random person who tells us they are connected to friend_id
1030* returns the number of nodes it sent the packet to 1030* returns the number of nodes it sent the packet to
1031*/ 1031*/
1032int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) 1032static int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1033{ 1033{
1034 int num = friend_number(friend_id); 1034 int num = friend_number(friend_id);
1035 if (num == -1) 1035 if (num == -1)
@@ -1079,7 +1079,7 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
1079/*----------------------------------------------------------------------------------*/ 1079/*----------------------------------------------------------------------------------*/
1080/*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/ 1080/*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/
1081 1081
1082int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) 1082static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
1083{ 1083{
1084 uint8_t data[sizeof(uint64_t) + 1]; 1084 uint8_t data[sizeof(uint64_t) + 1];
1085 uint8_t packet[MAX_DATA_SIZE]; 1085 uint8_t packet[MAX_DATA_SIZE];
@@ -1105,7 +1105,7 @@ int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
1105} 1105}
1106 1106
1107/* Handle a recieved ping request for */ 1107/* Handle a recieved ping request for */
1108int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) 1108static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
1109{ 1109{
1110 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING 1110 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
1111 && length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 1111 && length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
@@ -1211,7 +1211,7 @@ static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t
1211 friends_list[friend_num].punching_index = i; 1211 friends_list[friend_num].punching_index = i;
1212} 1212}
1213 1213
1214static void doNAT() 1214static void doNAT(void)
1215{ 1215{
1216 uint32_t i; 1216 uint32_t i;
1217 uint64_t temp_time = unix_time(); 1217 uint64_t temp_time = unix_time();
@@ -1275,7 +1275,7 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
1275 return 0; 1275 return 0;
1276} 1276}
1277 1277
1278void doDHT() 1278void doDHT(void)
1279{ 1279{
1280 doClose(); 1280 doClose();
1281 doDHTFriends(); 1281 doDHTFriends();
@@ -1283,7 +1283,7 @@ void doDHT()
1283} 1283}
1284 1284
1285/* get the size of the DHT (for saving) */ 1285/* get the size of the DHT (for saving) */
1286uint32_t DHT_size() 1286uint32_t DHT_size(void)
1287{ 1287{
1288 return sizeof(close_clientlist) + sizeof(Friend) * num_friends; 1288 return sizeof(close_clientlist) + sizeof(Friend) * num_friends;
1289} 1289}
@@ -1341,7 +1341,7 @@ int DHT_load(uint8_t * data, uint32_t size)
1341/* returns 0 if we are not connected to the DHT 1341/* returns 0 if we are not connected to the DHT
1342 * returns 1 if we are 1342 * returns 1 if we are
1343 */ 1343 */
1344int DHT_isconnected() 1344int DHT_isconnected(void)
1345{ 1345{
1346 uint32_t i; 1346 uint32_t i;
1347 uint64_t temp_time = unix_time(); 1347 uint64_t temp_time = unix_time();