diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 108 |
1 files changed, 57 insertions, 51 deletions
@@ -54,7 +54,7 @@ | |||
54 | #define TIME_TOPING 5 | 54 | #define TIME_TOPING 5 |
55 | 55 | ||
56 | 56 | ||
57 | Client_data *DHT_get_close_list(DHT * dht) | 57 | Client_data *DHT_get_close_list(DHT *dht) |
58 | { | 58 | { |
59 | return dht->close_clientlist; | 59 | return dht->close_clientlist; |
60 | } | 60 | } |
@@ -146,7 +146,7 @@ static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *clien | |||
146 | 146 | ||
147 | /* Returns the friend number from the client_id, or -1 if a failure occurs | 147 | /* Returns the friend number from the client_id, or -1 if a failure occurs |
148 | */ | 148 | */ |
149 | static int friend_number(DHT * dht, uint8_t *client_id) | 149 | static int friend_number(DHT *dht, uint8_t *client_id) |
150 | { | 150 | { |
151 | uint32_t i; | 151 | uint32_t i; |
152 | 152 | ||
@@ -163,7 +163,7 @@ static int friend_number(DHT * dht, uint8_t *client_id) | |||
163 | * | 163 | * |
164 | * TODO: For the love of based Allah make this function cleaner and much more efficient. | 164 | * TODO: For the love of based Allah make this function cleaner and much more efficient. |
165 | */ | 165 | */ |
166 | static int get_close_nodes(DHT * dht, uint8_t *client_id, Node_format *nodes_list) | 166 | static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list) |
167 | { | 167 | { |
168 | uint32_t i, j, k; | 168 | uint32_t i, j, k; |
169 | uint64_t temp_time = unix_time(); | 169 | uint64_t temp_time = unix_time(); |
@@ -330,7 +330,7 @@ static int replace_good( Client_data *list, | |||
330 | /* Attempt to add client with ip_port and client_id to the friends client list | 330 | /* Attempt to add client with ip_port and client_id to the friends client list |
331 | * and close_clientlist | 331 | * and close_clientlist |
332 | */ | 332 | */ |
333 | void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id) | 333 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) |
334 | { | 334 | { |
335 | uint32_t i; | 335 | uint32_t i; |
336 | 336 | ||
@@ -372,7 +372,7 @@ void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id) | |||
372 | /* If client_id is a friend or us, update ret_ip_port | 372 | /* If client_id is a friend or us, update ret_ip_port |
373 | * nodeclient_id is the id of the node that sent us this info | 373 | * nodeclient_id is the id of the node that sent us this info |
374 | */ | 374 | */ |
375 | static void returnedip_ports(DHT * dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) | 375 | static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) |
376 | { | 376 | { |
377 | uint32_t i, j; | 377 | uint32_t i, j; |
378 | uint64_t temp_time = unix_time(); | 378 | uint64_t temp_time = unix_time(); |
@@ -406,7 +406,7 @@ static void returnedip_ports(DHT * dht, IP_Port ip_port, uint8_t *client_id, uin | |||
406 | } | 406 | } |
407 | 407 | ||
408 | /* Same as last function but for get_node requests. */ | 408 | /* Same as last function but for get_node requests. */ |
409 | static int is_gettingnodes(DHT * dht, IP_Port ip_port, uint64_t ping_id) | 409 | static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) |
410 | { | 410 | { |
411 | uint32_t i; | 411 | uint32_t i; |
412 | uint8_t pinging; | 412 | uint8_t pinging; |
@@ -431,7 +431,7 @@ static int is_gettingnodes(DHT * dht, IP_Port ip_port, uint64_t ping_id) | |||
431 | } | 431 | } |
432 | 432 | ||
433 | /* Same but for get node requests */ | 433 | /* Same but for get node requests */ |
434 | static uint64_t add_gettingnodes(DHT * dht, IP_Port ip_port) | 434 | static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port) |
435 | { | 435 | { |
436 | uint32_t i, j; | 436 | uint32_t i, j; |
437 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 437 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
@@ -452,7 +452,7 @@ static uint64_t add_gettingnodes(DHT * dht, IP_Port ip_port) | |||
452 | } | 452 | } |
453 | 453 | ||
454 | /* send a getnodes request */ | 454 | /* send a getnodes request */ |
455 | static int getnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id) | 455 | static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id) |
456 | { | 456 | { |
457 | /* check if packet is gonna be sent to ourself */ | 457 | /* check if packet is gonna be sent to ourself */ |
458 | if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0)) | 458 | if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0)) |
@@ -491,7 +491,7 @@ static int getnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
491 | } | 491 | } |
492 | 492 | ||
493 | /* send a send nodes response */ | 493 | /* send a send nodes response */ |
494 | static int sendnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) | 494 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) |
495 | { | 495 | { |
496 | /* check if packet is gonna be sent to ourself */ | 496 | /* check if packet is gonna be sent to ourself */ |
497 | if (id_equal(public_key, dht->c->self_public_key)) | 497 | if (id_equal(public_key, dht->c->self_public_key)) |
@@ -532,9 +532,9 @@ static int sendnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *c | |||
532 | return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | 532 | return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); |
533 | } | 533 | } |
534 | 534 | ||
535 | static int handle_getnodes(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 535 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
536 | { | 536 | { |
537 | DHT * dht = object; | 537 | DHT *dht = object; |
538 | uint64_t ping_id; | 538 | uint64_t ping_id; |
539 | 539 | ||
540 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | 540 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
@@ -565,9 +565,9 @@ static int handle_getnodes(void * object, IP_Port source, uint8_t *packet, uint3 | |||
565 | return 0; | 565 | return 0; |
566 | } | 566 | } |
567 | 567 | ||
568 | static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 568 | static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
569 | { | 569 | { |
570 | DHT * dht = object; | 570 | DHT *dht = object; |
571 | uint64_t ping_id; | 571 | uint64_t ping_id; |
572 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | 572 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
573 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; | 573 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; |
@@ -613,7 +613,7 @@ static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint | |||
613 | /*----------------------------------------------------------------------------------*/ | 613 | /*----------------------------------------------------------------------------------*/ |
614 | /*------------------------END of packet handling functions--------------------------*/ | 614 | /*------------------------END of packet handling functions--------------------------*/ |
615 | 615 | ||
616 | int DHT_addfriend(DHT * dht, uint8_t *client_id) | 616 | int DHT_addfriend(DHT *dht, uint8_t *client_id) |
617 | { | 617 | { |
618 | if (friend_number(dht, client_id) != -1) /*Is friend already in DHT?*/ | 618 | if (friend_number(dht, client_id) != -1) /*Is friend already in DHT?*/ |
619 | return 1; | 619 | return 1; |
@@ -633,7 +633,7 @@ int DHT_addfriend(DHT * dht, uint8_t *client_id) | |||
633 | return 0; | 633 | return 0; |
634 | } | 634 | } |
635 | 635 | ||
636 | int DHT_delfriend(DHT * dht, uint8_t *client_id) | 636 | int DHT_delfriend(DHT *dht, uint8_t *client_id) |
637 | { | 637 | { |
638 | uint32_t i; | 638 | uint32_t i; |
639 | DHT_Friend *temp; | 639 | DHT_Friend *temp; |
@@ -669,7 +669,7 @@ int DHT_delfriend(DHT * dht, uint8_t *client_id) | |||
669 | } | 669 | } |
670 | 670 | ||
671 | /* TODO: Optimize this. */ | 671 | /* TODO: Optimize this. */ |
672 | IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id) | 672 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id) |
673 | { | 673 | { |
674 | uint32_t i, j; | 674 | uint32_t i, j; |
675 | uint64_t temp_time = unix_time(); | 675 | uint64_t temp_time = unix_time(); |
@@ -695,7 +695,7 @@ IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id) | |||
695 | /* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request | 695 | /* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request |
696 | * every GET_NODE_INTERVAL seconds to a random good node for each "friend" in our "friends" list. | 696 | * every GET_NODE_INTERVAL seconds to a random good node for each "friend" in our "friends" list. |
697 | */ | 697 | */ |
698 | static void do_DHT_friends(DHT * dht) | 698 | static void do_DHT_friends(DHT *dht) |
699 | { | 699 | { |
700 | uint32_t i, j; | 700 | uint32_t i, j; |
701 | uint64_t temp_time = unix_time(); | 701 | uint64_t temp_time = unix_time(); |
@@ -710,7 +710,7 @@ static void do_DHT_friends(DHT * dht) | |||
710 | if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { | 710 | if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { |
711 | if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { | 711 | if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
712 | send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, | 712 | send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, |
713 | (clientid_t *) &dht->friends_list[i].client_list[j].client_id ); | 713 | (clientid_t *) &dht->friends_list[i].client_list[j].client_id ); |
714 | dht->friends_list[i].client_list[j].last_pinged = temp_time; | 714 | dht->friends_list[i].client_list[j].last_pinged = temp_time; |
715 | } | 715 | } |
716 | 716 | ||
@@ -725,8 +725,8 @@ static void do_DHT_friends(DHT * dht) | |||
725 | if (dht->friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { | 725 | if (dht->friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
726 | rand_node = rand() % num_nodes; | 726 | rand_node = rand() % num_nodes; |
727 | getnodes(dht, dht->friends_list[i].client_list[index[rand_node]].ip_port, | 727 | getnodes(dht, dht->friends_list[i].client_list[index[rand_node]].ip_port, |
728 | dht->friends_list[i].client_list[index[rand_node]].client_id, | 728 | dht->friends_list[i].client_list[index[rand_node]].client_id, |
729 | dht->friends_list[i].client_id ); | 729 | dht->friends_list[i].client_id ); |
730 | dht->friends_list[i].lastgetnode = temp_time; | 730 | dht->friends_list[i].lastgetnode = temp_time; |
731 | } | 731 | } |
732 | } | 732 | } |
@@ -735,7 +735,7 @@ static void do_DHT_friends(DHT * dht) | |||
735 | /* Ping each client in the close nodes list every PING_INTERVAL seconds. | 735 | /* Ping each client in the close nodes list every PING_INTERVAL seconds. |
736 | * Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list. | 736 | * Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list. |
737 | */ | 737 | */ |
738 | static void do_Close(DHT * dht) | 738 | static void do_Close(DHT *dht) |
739 | { | 739 | { |
740 | uint32_t i; | 740 | uint32_t i; |
741 | uint64_t temp_time = unix_time(); | 741 | uint64_t temp_time = unix_time(); |
@@ -748,7 +748,7 @@ static void do_Close(DHT * dht) | |||
748 | if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { | 748 | if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { |
749 | if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { | 749 | if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { |
750 | send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, | 750 | send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, |
751 | (clientid_t *) &dht->close_clientlist[i].client_id ); | 751 | (clientid_t *) &dht->close_clientlist[i].client_id ); |
752 | dht->close_clientlist[i].last_pinged = temp_time; | 752 | dht->close_clientlist[i].last_pinged = temp_time; |
753 | } | 753 | } |
754 | 754 | ||
@@ -763,13 +763,13 @@ static void do_Close(DHT * dht) | |||
763 | if (dht->close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { | 763 | if (dht->close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
764 | rand_node = rand() % num_nodes; | 764 | rand_node = rand() % num_nodes; |
765 | getnodes(dht, dht->close_clientlist[index[rand_node]].ip_port, | 765 | getnodes(dht, dht->close_clientlist[index[rand_node]].ip_port, |
766 | dht->close_clientlist[index[rand_node]].client_id, | 766 | dht->close_clientlist[index[rand_node]].client_id, |
767 | dht->c->self_public_key ); | 767 | dht->c->self_public_key ); |
768 | dht->close_lastgetnodes = temp_time; | 768 | dht->close_lastgetnodes = temp_time; |
769 | } | 769 | } |
770 | } | 770 | } |
771 | 771 | ||
772 | void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key) | 772 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) |
773 | { | 773 | { |
774 | getnodes(dht, ip_port, public_key, dht->c->self_public_key); | 774 | getnodes(dht, ip_port, public_key, dht->c->self_public_key); |
775 | send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); | 775 | send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); |
@@ -778,7 +778,7 @@ void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key) | |||
778 | /* send the given packet to node with client_id | 778 | /* send the given packet to node with client_id |
779 | * returns -1 if failure | 779 | * returns -1 if failure |
780 | */ | 780 | */ |
781 | int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | 781 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) |
782 | { | 782 | { |
783 | uint32_t i; | 783 | uint32_t i; |
784 | 784 | ||
@@ -796,7 +796,7 @@ int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length | |||
796 | * return 0 if we are connected to friend or if no ips were found. | 796 | * return 0 if we are connected to friend or if no ips were found. |
797 | * returns -1 if no such friend | 797 | * returns -1 if no such friend |
798 | */ | 798 | */ |
799 | static int friend_iplist(DHT * dht, IP_Port *ip_portlist, uint16_t friend_num) | 799 | static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) |
800 | { | 800 | { |
801 | int num_ips = 0; | 801 | int num_ips = 0; |
802 | uint32_t i; | 802 | uint32_t i; |
@@ -831,7 +831,7 @@ static int friend_iplist(DHT * dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
831 | * | 831 | * |
832 | * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. | 832 | * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. |
833 | */ | 833 | */ |
834 | int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 834 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
835 | { | 835 | { |
836 | int num = friend_number(dht, friend_id); | 836 | int num = friend_number(dht, friend_id); |
837 | 837 | ||
@@ -866,7 +866,7 @@ int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t leng | |||
866 | /* Send the following packet to one random person who tells us they are connected to friend_id | 866 | /* Send the following packet to one random person who tells us they are connected to friend_id |
867 | * returns the number of nodes it sent the packet to | 867 | * returns the number of nodes it sent the packet to |
868 | */ | 868 | */ |
869 | static int routeone_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 869 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
870 | { | 870 | { |
871 | int num = friend_number(dht, friend_id); | 871 | int num = friend_number(dht, friend_id); |
872 | 872 | ||
@@ -906,7 +906,7 @@ static int routeone_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uin | |||
906 | * return 0 if we are connected to friend or if no ips were found. | 906 | * return 0 if we are connected to friend or if no ips were found. |
907 | * returns -1 if no such friend | 907 | * returns -1 if no such friend |
908 | */ | 908 | */ |
909 | int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id) | 909 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) |
910 | { | 910 | { |
911 | uint32_t i; | 911 | uint32_t i; |
912 | 912 | ||
@@ -922,7 +922,7 @@ int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id) | |||
922 | /*----------------------------------------------------------------------------------*/ | 922 | /*----------------------------------------------------------------------------------*/ |
923 | /*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/ | 923 | /*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/ |
924 | 924 | ||
925 | static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_t type) | 925 | static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type) |
926 | { | 926 | { |
927 | uint8_t data[sizeof(uint64_t) + 1]; | 927 | uint8_t data[sizeof(uint64_t) + 1]; |
928 | uint8_t packet[MAX_DATA_SIZE]; | 928 | uint8_t packet[MAX_DATA_SIZE]; |
@@ -932,7 +932,8 @@ static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_ | |||
932 | data[0] = type; | 932 | data[0] = type; |
933 | memcpy(data + 1, &ping_id, sizeof(uint64_t)); | 933 | memcpy(data + 1, &ping_id, sizeof(uint64_t)); |
934 | /* 254 is NAT ping request packet id */ | 934 | /* 254 is NAT ping request packet id */ |
935 | int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data, sizeof(uint64_t) + 1, 254); | 935 | int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data, |
936 | sizeof(uint64_t) + 1, 254); | ||
936 | 937 | ||
937 | if (len == -1) | 938 | if (len == -1) |
938 | return -1; | 939 | return -1; |
@@ -949,9 +950,9 @@ static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_ | |||
949 | } | 950 | } |
950 | 951 | ||
951 | /* Handle a received ping request for */ | 952 | /* Handle a received ping request for */ |
952 | static int handle_NATping(void * object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) | 953 | static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) |
953 | { | 954 | { |
954 | DHT * dht = object; | 955 | DHT *dht = object; |
955 | uint64_t ping_id; | 956 | uint64_t ping_id; |
956 | memcpy(&ping_id, packet + 1, sizeof(uint64_t)); | 957 | memcpy(&ping_id, packet + 1, sizeof(uint64_t)); |
957 | 958 | ||
@@ -1026,7 +1027,7 @@ static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t | |||
1026 | return num; | 1027 | return num; |
1027 | } | 1028 | } |
1028 | 1029 | ||
1029 | static void punch_holes(DHT * dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num) | 1030 | static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num) |
1030 | { | 1031 | { |
1031 | if (numports > MAX_FRIEND_CLIENTS || numports == 0) | 1032 | if (numports > MAX_FRIEND_CLIENTS || numports == 0) |
1032 | return; | 1033 | return; |
@@ -1044,7 +1045,7 @@ static void punch_holes(DHT * dht, IP ip, uint16_t *port_list, uint16_t numports | |||
1044 | dht->friends_list[friend_num].punching_index = i; | 1045 | dht->friends_list[friend_num].punching_index = i; |
1045 | } | 1046 | } |
1046 | 1047 | ||
1047 | static void do_NAT(DHT * dht) | 1048 | static void do_NAT(DHT *dht) |
1048 | { | 1049 | { |
1049 | uint32_t i; | 1050 | uint32_t i; |
1050 | uint64_t temp_time = unix_time(); | 1051 | uint64_t temp_time = unix_time(); |
@@ -1093,7 +1094,7 @@ static void do_NAT(DHT * dht) | |||
1093 | network while preventing amplification attacks. | 1094 | network while preventing amplification attacks. |
1094 | return 0 if node was added | 1095 | return 0 if node was added |
1095 | return -1 if node was not added */ | 1096 | return -1 if node was not added */ |
1096 | int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port) | 1097 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) |
1097 | { | 1098 | { |
1098 | if (ip_port.ip.i == 0) | 1099 | if (ip_port.ip.i == 0) |
1099 | return -1; | 1100 | return -1; |
@@ -1123,7 +1124,7 @@ int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port) | |||
1123 | 1124 | ||
1124 | /*Ping all the valid nodes in the toping list every TIME_TOPING seconds | 1125 | /*Ping all the valid nodes in the toping list every TIME_TOPING seconds |
1125 | this function must be run at least once every TIME_TOPING seconds*/ | 1126 | this function must be run at least once every TIME_TOPING seconds*/ |
1126 | static void do_toping(DHT * dht) | 1127 | static void do_toping(DHT *dht) |
1127 | { | 1128 | { |
1128 | uint64_t temp_time = unix_time(); | 1129 | uint64_t temp_time = unix_time(); |
1129 | 1130 | ||
@@ -1143,18 +1144,23 @@ static void do_toping(DHT * dht) | |||
1143 | } | 1144 | } |
1144 | 1145 | ||
1145 | 1146 | ||
1146 | DHT * new_DHT(Net_Crypto *c) | 1147 | DHT *new_DHT(Net_Crypto *c) |
1147 | { | 1148 | { |
1148 | if (c == NULL) | 1149 | if (c == NULL) |
1149 | return NULL; | 1150 | return NULL; |
1150 | DHT * temp = calloc(1, sizeof(DHT)); | 1151 | |
1152 | DHT *temp = calloc(1, sizeof(DHT)); | ||
1153 | |||
1151 | if (temp == NULL) | 1154 | if (temp == NULL) |
1152 | return NULL; | 1155 | return NULL; |
1156 | |||
1153 | temp->ping = new_ping(); | 1157 | temp->ping = new_ping(); |
1154 | if (temp->ping == NULL){ | 1158 | |
1155 | kill_DHT(temp); | 1159 | if (temp->ping == NULL) { |
1156 | return NULL; | 1160 | kill_DHT(temp); |
1161 | return NULL; | ||
1157 | } | 1162 | } |
1163 | |||
1158 | temp->c = c; | 1164 | temp->c = c; |
1159 | networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp); | 1165 | networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp); |
1160 | networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp); | 1166 | networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp); |
@@ -1164,14 +1170,14 @@ DHT * new_DHT(Net_Crypto *c) | |||
1164 | return temp; | 1170 | return temp; |
1165 | } | 1171 | } |
1166 | 1172 | ||
1167 | void do_DHT(DHT * dht) | 1173 | void do_DHT(DHT *dht) |
1168 | { | 1174 | { |
1169 | do_Close(dht); | 1175 | do_Close(dht); |
1170 | do_DHT_friends(dht); | 1176 | do_DHT_friends(dht); |
1171 | do_NAT(dht); | 1177 | do_NAT(dht); |
1172 | do_toping(dht); | 1178 | do_toping(dht); |
1173 | } | 1179 | } |
1174 | void kill_DHT(DHT * dht) | 1180 | void kill_DHT(DHT *dht) |
1175 | { | 1181 | { |
1176 | kill_ping(dht->ping); | 1182 | kill_ping(dht->ping); |
1177 | free(dht->friends_list); | 1183 | free(dht->friends_list); |
@@ -1179,13 +1185,13 @@ void kill_DHT(DHT * dht) | |||
1179 | } | 1185 | } |
1180 | 1186 | ||
1181 | /* get the size of the DHT (for saving) */ | 1187 | /* get the size of the DHT (for saving) */ |
1182 | uint32_t DHT_size(DHT * dht) | 1188 | uint32_t DHT_size(DHT *dht) |
1183 | { | 1189 | { |
1184 | return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends; | 1190 | return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends; |
1185 | } | 1191 | } |
1186 | 1192 | ||
1187 | /* save the DHT in data where data is an array of size DHT_size() */ | 1193 | /* save the DHT in data where data is an array of size DHT_size() */ |
1188 | void DHT_save(DHT * dht, uint8_t *data) | 1194 | void DHT_save(DHT *dht, uint8_t *data) |
1189 | { | 1195 | { |
1190 | memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist)); | 1196 | memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist)); |
1191 | memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends); | 1197 | memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends); |
@@ -1195,7 +1201,7 @@ void DHT_save(DHT * dht, uint8_t *data) | |||
1195 | * return -1 if failure | 1201 | * return -1 if failure |
1196 | * return 0 if success | 1202 | * return 0 if success |
1197 | */ | 1203 | */ |
1198 | int DHT_load(DHT * dht, uint8_t *data, uint32_t size) | 1204 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) |
1199 | { | 1205 | { |
1200 | if (size < sizeof(dht->close_clientlist)) | 1206 | if (size < sizeof(dht->close_clientlist)) |
1201 | return -1; | 1207 | return -1; |
@@ -1231,7 +1237,7 @@ int DHT_load(DHT * dht, uint8_t *data, uint32_t size) | |||
1231 | for (i = 0; i < LCLIENT_LIST; ++i) { | 1237 | for (i = 0; i < LCLIENT_LIST; ++i) { |
1232 | if (tempclose_clientlist[i].timestamp != 0) | 1238 | if (tempclose_clientlist[i].timestamp != 0) |
1233 | DHT_bootstrap(dht, tempclose_clientlist[i].ip_port, | 1239 | DHT_bootstrap(dht, tempclose_clientlist[i].ip_port, |
1234 | tempclose_clientlist[i].client_id ); | 1240 | tempclose_clientlist[i].client_id ); |
1235 | } | 1241 | } |
1236 | 1242 | ||
1237 | return 0; | 1243 | return 0; |
@@ -1240,7 +1246,7 @@ int DHT_load(DHT * dht, uint8_t *data, uint32_t size) | |||
1240 | /* returns 0 if we are not connected to the DHT | 1246 | /* returns 0 if we are not connected to the DHT |
1241 | * returns 1 if we are | 1247 | * returns 1 if we are |
1242 | */ | 1248 | */ |
1243 | int DHT_isconnected(DHT * dht) | 1249 | int DHT_isconnected(DHT *dht) |
1244 | { | 1250 | { |
1245 | uint32_t i; | 1251 | uint32_t i; |
1246 | uint64_t temp_time = unix_time(); | 1252 | uint64_t temp_time = unix_time(); |