diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 108 | ||||
-rw-r--r-- | core/DHT.h | 36 | ||||
-rw-r--r-- | core/LAN_discovery.c | 3 | ||||
-rw-r--r-- | core/Lossless_UDP.c | 92 | ||||
-rw-r--r-- | core/Lossless_UDP.h | 42 | ||||
-rw-r--r-- | core/Messenger.c | 14 | ||||
-rw-r--r-- | core/Messenger.h | 6 | ||||
-rw-r--r-- | core/friend_requests.c | 10 | ||||
-rw-r--r-- | core/friend_requests.h | 5 | ||||
-rw-r--r-- | core/net_crypto.c | 54 | ||||
-rw-r--r-- | core/net_crypto.h | 19 | ||||
-rw-r--r-- | core/network.c | 19 | ||||
-rw-r--r-- | core/network.h | 16 | ||||
-rw-r--r-- | core/ping.c | 27 | ||||
-rw-r--r-- | core/ping.h | 14 |
15 files changed, 260 insertions, 205 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(); |
@@ -100,23 +100,23 @@ typedef struct { | |||
100 | Node_format toping[MAX_TOPING]; | 100 | Node_format toping[MAX_TOPING]; |
101 | uint64_t last_toping; | 101 | uint64_t last_toping; |
102 | uint64_t close_lastgetnodes; | 102 | uint64_t close_lastgetnodes; |
103 | void * ping; | 103 | void *ping; |
104 | } DHT; | 104 | } DHT; |
105 | /*----------------------------------------------------------------------------------*/ | 105 | /*----------------------------------------------------------------------------------*/ |
106 | 106 | ||
107 | Client_data *DHT_get_close_list(DHT * dht); | 107 | Client_data *DHT_get_close_list(DHT *dht); |
108 | 108 | ||
109 | /* Add a new friend to the friends list | 109 | /* Add a new friend to the friends list |
110 | client_id must be CLIENT_ID_SIZE bytes long. | 110 | client_id must be CLIENT_ID_SIZE bytes long. |
111 | returns 0 if success | 111 | returns 0 if success |
112 | returns 1 if failure (friends list is full) */ | 112 | returns 1 if failure (friends list is full) */ |
113 | int DHT_addfriend(DHT * dht, uint8_t *client_id); | 113 | int DHT_addfriend(DHT *dht, uint8_t *client_id); |
114 | 114 | ||
115 | /* Delete a friend from the friends list | 115 | /* Delete a friend from the friends list |
116 | client_id must be CLIENT_ID_SIZE bytes long. | 116 | client_id must be CLIENT_ID_SIZE bytes long. |
117 | returns 0 if success | 117 | returns 0 if success |
118 | returns 1 if failure (client_id not in friends list) */ | 118 | returns 1 if failure (client_id not in friends list) */ |
119 | int DHT_delfriend(DHT * dht, uint8_t *client_id); | 119 | int DHT_delfriend(DHT *dht, uint8_t *client_id); |
120 | 120 | ||
121 | /* Get ip of friend | 121 | /* Get ip of friend |
122 | client_id must be CLIENT_ID_SIZE bytes long. | 122 | client_id must be CLIENT_ID_SIZE bytes long. |
@@ -125,14 +125,14 @@ int DHT_delfriend(DHT * dht, uint8_t *client_id); | |||
125 | returns ip if success | 125 | returns ip if success |
126 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 126 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) |
127 | returns ip of 1 if friend is not in list. */ | 127 | returns ip of 1 if friend is not in list. */ |
128 | IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id); | 128 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); |
129 | 129 | ||
130 | /* Run this function at least a couple times per second (It's the main loop) */ | 130 | /* Run this function at least a couple times per second (It's the main loop) */ |
131 | void do_DHT(DHT * dht); | 131 | void do_DHT(DHT *dht); |
132 | 132 | ||
133 | /* Use this function to bootstrap the client | 133 | /* Use this function to bootstrap the client |
134 | Sends a get nodes request to the given node with ip port and public_key */ | 134 | Sends a get nodes request to the given node with ip port and public_key */ |
135 | void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key); | 135 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); |
136 | 136 | ||
137 | /* Add nodes to the toping list | 137 | /* Add nodes to the toping list |
138 | all nodes in this list are pinged every TIME_TOPING seconds | 138 | all nodes in this list are pinged every TIME_TOPING seconds |
@@ -142,17 +142,17 @@ void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key); | |||
142 | network while preventing amplification attacks. | 142 | network while preventing amplification attacks. |
143 | return 0 if node was added | 143 | return 0 if node was added |
144 | return -1 if node was not added */ | 144 | return -1 if node was not added */ |
145 | int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port); | 145 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port); |
146 | 146 | ||
147 | /* ROUTING FUNCTIONS */ | 147 | /* ROUTING FUNCTIONS */ |
148 | 148 | ||
149 | /* send the given packet to node with client_id | 149 | /* send the given packet to node with client_id |
150 | returns -1 if failure */ | 150 | returns -1 if failure */ |
151 | int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length); | 151 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); |
152 | 152 | ||
153 | /* Send the following packet to everyone who tells us they are connected to friend_id | 153 | /* Send the following packet to everyone who tells us they are connected to friend_id |
154 | returns the number of nodes it sent the packet to */ | 154 | returns the number of nodes it sent the packet to */ |
155 | int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); | 155 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); |
156 | 156 | ||
157 | /* NAT PUNCHING FUNCTIONS */ | 157 | /* NAT PUNCHING FUNCTIONS */ |
158 | 158 | ||
@@ -160,31 +160,31 @@ int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t leng | |||
160 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 160 | ip_portlist must be at least MAX_FRIEND_CLIENTS big |
161 | returns the number of ips returned | 161 | returns the number of ips returned |
162 | returns -1 if no such friend*/ | 162 | returns -1 if no such friend*/ |
163 | int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id); | 163 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); |
164 | 164 | ||
165 | /* SAVE/LOAD functions */ | 165 | /* SAVE/LOAD functions */ |
166 | 166 | ||
167 | /* get the size of the DHT (for saving) */ | 167 | /* get the size of the DHT (for saving) */ |
168 | uint32_t DHT_size(DHT * dht); | 168 | uint32_t DHT_size(DHT *dht); |
169 | 169 | ||
170 | /* save the DHT in data where data is an array of size DHT_size() */ | 170 | /* save the DHT in data where data is an array of size DHT_size() */ |
171 | void DHT_save(DHT * dht, uint8_t *data); | 171 | void DHT_save(DHT *dht, uint8_t *data); |
172 | 172 | ||
173 | /* init DHT */ | 173 | /* init DHT */ |
174 | DHT * new_DHT(Net_Crypto *c); | 174 | DHT *new_DHT(Net_Crypto *c); |
175 | 175 | ||
176 | void kill_DHT(DHT * dht); | 176 | void kill_DHT(DHT *dht); |
177 | 177 | ||
178 | /* load the DHT from data of size size; | 178 | /* load the DHT from data of size size; |
179 | return -1 if failure | 179 | return -1 if failure |
180 | return 0 if success */ | 180 | return 0 if success */ |
181 | int DHT_load(DHT * dht, uint8_t *data, uint32_t size); | 181 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size); |
182 | 182 | ||
183 | /* returns 0 if we are not connected to the DHT | 183 | /* returns 0 if we are not connected to the DHT |
184 | returns 1 if we are */ | 184 | returns 1 if we are */ |
185 | int DHT_isconnected(DHT * dht); | 185 | int DHT_isconnected(DHT *dht); |
186 | 186 | ||
187 | void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id); | 187 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id); |
188 | 188 | ||
189 | #ifdef __cplusplus | 189 | #ifdef __cplusplus |
190 | } | 190 | } |
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 07980d26..cbbb384b 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c | |||
@@ -121,9 +121,10 @@ static int LAN_ip(IP ip) | |||
121 | return -1; | 121 | return -1; |
122 | } | 122 | } |
123 | 123 | ||
124 | static int handle_LANdiscovery(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 124 | static int handle_LANdiscovery(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
125 | { | 125 | { |
126 | DHT *dht = object; | 126 | DHT *dht = object; |
127 | |||
127 | if (LAN_ip(source.ip) == -1) | 128 | if (LAN_ip(source.ip) == -1) |
128 | return 1; | 129 | return 1; |
129 | 130 | ||
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 7ece28e1..15e8dea3 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -36,7 +36,7 @@ | |||
36 | * Return -1 if there are no connections like we are looking for | 36 | * Return -1 if there are no connections like we are looking for |
37 | * Return id if it found it | 37 | * Return id if it found it |
38 | */ | 38 | */ |
39 | int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port) | 39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) |
40 | { | 40 | { |
41 | uint32_t i; | 41 | uint32_t i; |
42 | 42 | ||
@@ -57,7 +57,7 @@ int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port) | |||
57 | * | 57 | * |
58 | * TODO: make this better | 58 | * TODO: make this better |
59 | */ | 59 | */ |
60 | static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source) | 60 | static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) |
61 | { | 61 | { |
62 | uint32_t id = 0, i; | 62 | uint32_t id = 0, i; |
63 | 63 | ||
@@ -79,7 +79,7 @@ static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source) | |||
79 | * | 79 | * |
80 | * TODO: make this better | 80 | * TODO: make this better |
81 | */ | 81 | */ |
82 | static void change_handshake(Lossless_UDP * ludp, IP_Port source) | 82 | static void change_handshake(Lossless_UDP *ludp, IP_Port source) |
83 | { | 83 | { |
84 | uint8_t rand = random_int() % 4; | 84 | uint8_t rand = random_int() % 4; |
85 | ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int(); | 85 | ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int(); |
@@ -91,7 +91,7 @@ static void change_handshake(Lossless_UDP * ludp, IP_Port source) | |||
91 | * Return -1 if it could not initialize the connectiont | 91 | * Return -1 if it could not initialize the connectiont |
92 | * If there already was an existing connection to that ip_port return its number. | 92 | * If there already was an existing connection to that ip_port return its number. |
93 | */ | 93 | */ |
94 | int new_connection(Lossless_UDP * ludp, IP_Port ip_port) | 94 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port) |
95 | { | 95 | { |
96 | int connect = getconnection_id(ludp, ip_port); | 96 | int connect = getconnection_id(ludp, ip_port); |
97 | 97 | ||
@@ -148,7 +148,7 @@ int new_connection(Lossless_UDP * ludp, IP_Port ip_port) | |||
148 | * Returns an integer corresponding to the connection id. | 148 | * Returns an integer corresponding to the connection id. |
149 | * Return -1 if it could not initialize the connection. | 149 | * Return -1 if it could not initialize the connection. |
150 | */ | 150 | */ |
151 | static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port) | 151 | static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) |
152 | { | 152 | { |
153 | if (getconnection_id(ludp, ip_port) != -1) | 153 | if (getconnection_id(ludp, ip_port) != -1) |
154 | return -1; | 154 | return -1; |
@@ -200,7 +200,7 @@ static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port) | |||
200 | * Returns an integer corresponding to the next connection in our incoming connection list. | 200 | * Returns an integer corresponding to the next connection in our incoming connection list. |
201 | * Return -1 if there are no new incoming connections in the list. | 201 | * Return -1 if there are no new incoming connections in the list. |
202 | */ | 202 | */ |
203 | int incoming_connection(Lossless_UDP * ludp) | 203 | int incoming_connection(Lossless_UDP *ludp) |
204 | { | 204 | { |
205 | uint32_t i; | 205 | uint32_t i; |
206 | 206 | ||
@@ -215,7 +215,7 @@ int incoming_connection(Lossless_UDP * ludp) | |||
215 | } | 215 | } |
216 | 216 | ||
217 | /* Try to free some memory from the connections array. */ | 217 | /* Try to free some memory from the connections array. */ |
218 | static void free_connections(Lossless_UDP * ludp) | 218 | static void free_connections(Lossless_UDP *ludp) |
219 | { | 219 | { |
220 | uint32_t i; | 220 | uint32_t i; |
221 | 221 | ||
@@ -247,7 +247,7 @@ static void free_connections(Lossless_UDP * ludp) | |||
247 | * Return -1 if it could not kill the connection. | 247 | * Return -1 if it could not kill the connection. |
248 | * Return 0 if killed successfully | 248 | * Return 0 if killed successfully |
249 | */ | 249 | */ |
250 | int kill_connection(Lossless_UDP * ludp, int connection_id) | 250 | int kill_connection(Lossless_UDP *ludp, int connection_id) |
251 | { | 251 | { |
252 | if (connection_id >= 0 && connection_id < ludp->connections_length) { | 252 | if (connection_id >= 0 && connection_id < ludp->connections_length) { |
253 | if (ludp->connections[connection_id].status > 0) { | 253 | if (ludp->connections[connection_id].status > 0) { |
@@ -267,7 +267,7 @@ int kill_connection(Lossless_UDP * ludp, int connection_id) | |||
267 | * Return -1 if it can not kill the connection. | 267 | * Return -1 if it can not kill the connection. |
268 | * Return 0 if it will kill it. | 268 | * Return 0 if it will kill it. |
269 | */ | 269 | */ |
270 | int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds) | 270 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) |
271 | { | 271 | { |
272 | if (connection_id >= 0 && connection_id < ludp->connections_length) { | 272 | if (connection_id >= 0 && connection_id < ludp->connections_length) { |
273 | if (ludp->connections[connection_id].status > 0) { | 273 | if (ludp->connections[connection_id].status > 0) { |
@@ -287,7 +287,7 @@ int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds) | |||
287 | * Return 3 if fully connected. | 287 | * Return 3 if fully connected. |
288 | * Return 4 if timed out and waiting to be killed. | 288 | * Return 4 if timed out and waiting to be killed. |
289 | */ | 289 | */ |
290 | int is_connected(Lossless_UDP * ludp, int connection_id) | 290 | int is_connected(Lossless_UDP *ludp, int connection_id) |
291 | { | 291 | { |
292 | if (connection_id >= 0 && connection_id < ludp->connections_length) | 292 | if (connection_id >= 0 && connection_id < ludp->connections_length) |
293 | return ludp->connections[connection_id].status; | 293 | return ludp->connections[connection_id].status; |
@@ -296,7 +296,7 @@ int is_connected(Lossless_UDP * ludp, int connection_id) | |||
296 | } | 296 | } |
297 | 297 | ||
298 | /* returns the ip_port of the corresponding connection. */ | 298 | /* returns the ip_port of the corresponding connection. */ |
299 | IP_Port connection_ip(Lossless_UDP * ludp, int connection_id) | 299 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) |
300 | { | 300 | { |
301 | if (connection_id >= 0 && connection_id < ludp->connections_length) | 301 | if (connection_id >= 0 && connection_id < ludp->connections_length) |
302 | return ludp->connections[connection_id].ip_port; | 302 | return ludp->connections[connection_id].ip_port; |
@@ -306,7 +306,7 @@ IP_Port connection_ip(Lossless_UDP * ludp, int connection_id) | |||
306 | } | 306 | } |
307 | 307 | ||
308 | /* returns the number of packets in the queue waiting to be successfully sent. */ | 308 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
309 | uint32_t sendqueue(Lossless_UDP * ludp, int connection_id) | 309 | uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) |
310 | { | 310 | { |
311 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 311 | if (connection_id < 0 || connection_id >= ludp->connections_length) |
312 | return 0; | 312 | return 0; |
@@ -315,7 +315,7 @@ uint32_t sendqueue(Lossless_UDP * ludp, int connection_id) | |||
315 | } | 315 | } |
316 | 316 | ||
317 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 317 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
318 | uint32_t recvqueue(Lossless_UDP * ludp, int connection_id) | 318 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) |
319 | { | 319 | { |
320 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 320 | if (connection_id < 0 || connection_id >= ludp->connections_length) |
321 | return 0; | 321 | return 0; |
@@ -325,20 +325,21 @@ uint32_t recvqueue(Lossless_UDP * ludp, int connection_id) | |||
325 | 325 | ||
326 | /* returns the id of the next packet in the queue | 326 | /* returns the id of the next packet in the queue |
327 | return -1 if no packet in queue */ | 327 | return -1 if no packet in queue */ |
328 | char id_packet(Lossless_UDP * ludp, int connection_id) | 328 | char id_packet(Lossless_UDP *ludp, int connection_id) |
329 | { | 329 | { |
330 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 330 | if (connection_id < 0 || connection_id >= ludp->connections_length) |
331 | return -1; | 331 | return -1; |
332 | 332 | ||
333 | if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0) | 333 | if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0) |
334 | return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; | 334 | return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read % |
335 | MAX_QUEUE_NUM].data[0]; | ||
335 | 336 | ||
336 | return -1; | 337 | return -1; |
337 | } | 338 | } |
338 | 339 | ||
339 | /* return 0 if there is no received data in the buffer. | 340 | /* return 0 if there is no received data in the buffer. |
340 | return length of received packet if successful */ | 341 | return length of received packet if successful */ |
341 | int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data) | 342 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) |
342 | { | 343 | { |
343 | if (recvqueue(ludp, connection_id) != 0) { | 344 | if (recvqueue(ludp, connection_id) != 0) { |
344 | uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM; | 345 | uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM; |
@@ -356,7 +357,7 @@ int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data) | |||
356 | * Return 0 if data could not be put in packet queue | 357 | * Return 0 if data could not be put in packet queue |
357 | * Return 1 if data was put into the queue | 358 | * Return 1 if data was put into the queue |
358 | */ | 359 | */ |
359 | int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length) | 360 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) |
360 | { | 361 | { |
361 | if (length > MAX_DATA_SIZE || length == 0) | 362 | if (length > MAX_DATA_SIZE || length == 0) |
362 | return 0; | 363 | return 0; |
@@ -373,7 +374,7 @@ int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t | |||
373 | } | 374 | } |
374 | 375 | ||
375 | /* put the packet numbers the we are missing in requested and return the number */ | 376 | /* put the packet numbers the we are missing in requested and return the number */ |
376 | uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *requested) | 377 | uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) |
377 | { | 378 | { |
378 | uint32_t number = 0; | 379 | uint32_t number = 0; |
379 | uint32_t i; | 380 | uint32_t i; |
@@ -403,7 +404,7 @@ uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *reque | |||
403 | * see http://wiki.tox.im/index.php/Lossless_UDP for more information. | 404 | * see http://wiki.tox.im/index.php/Lossless_UDP for more information. |
404 | */ | 405 | */ |
405 | 406 | ||
406 | static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) | 407 | static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) |
407 | { | 408 | { |
408 | uint8_t packet[1 + 4 + 4]; | 409 | uint8_t packet[1 + 4 + 4]; |
409 | uint32_t temp; | 410 | uint32_t temp; |
@@ -417,7 +418,7 @@ static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handsha | |||
417 | return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); | 418 | return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); |
418 | } | 419 | } |
419 | 420 | ||
420 | static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id) | 421 | static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id) |
421 | { | 422 | { |
422 | uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; | 423 | uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; |
423 | uint16_t index = 0; | 424 | uint16_t index = 0; |
@@ -444,7 +445,7 @@ static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id) | |||
444 | 445 | ||
445 | } | 446 | } |
446 | 447 | ||
447 | static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_t packet_num) | 448 | static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num) |
448 | { | 449 | { |
449 | uint32_t index = packet_num % MAX_QUEUE_NUM; | 450 | uint32_t index = packet_num % MAX_QUEUE_NUM; |
450 | uint32_t temp; | 451 | uint32_t temp; |
@@ -459,7 +460,7 @@ static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_ | |||
459 | } | 460 | } |
460 | 461 | ||
461 | /* sends 1 data packet */ | 462 | /* sends 1 data packet */ |
462 | static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id) | 463 | static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) |
463 | { | 464 | { |
464 | int ret; | 465 | int ret; |
465 | uint32_t buffer[BUFFER_PACKET_NUM]; | 466 | uint32_t buffer[BUFFER_PACKET_NUM]; |
@@ -491,9 +492,10 @@ static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id) | |||
491 | 492 | ||
492 | 493 | ||
493 | /* Return 0 if handled correctly, 1 if packet is bad. */ | 494 | /* Return 0 if handled correctly, 1 if packet is bad. */ |
494 | static int handle_handshake(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 495 | static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
495 | { | 496 | { |
496 | Lossless_UDP * ludp = object; | 497 | Lossless_UDP *ludp = object; |
498 | |||
497 | if (length != (1 + 4 + 4)) | 499 | if (length != (1 + 4 + 4)) |
498 | return 1; | 500 | return 1; |
499 | 501 | ||
@@ -542,7 +544,7 @@ static int SYNC_valid(uint32_t length) | |||
542 | } | 544 | } |
543 | 545 | ||
544 | /* case 1 in handle_SYNC: */ | 546 | /* case 1 in handle_SYNC: */ |
545 | static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 547 | static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
546 | { | 548 | { |
547 | if (handshake_id(ludp, source) == recv_packetnum) { | 549 | if (handshake_id(ludp, source) == recv_packetnum) { |
548 | int x = new_inconnection(ludp, source); | 550 | int x = new_inconnection(ludp, source); |
@@ -564,7 +566,8 @@ static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packe | |||
564 | } | 566 | } |
565 | 567 | ||
566 | /* case 2 in handle_SYNC: */ | 568 | /* case 2 in handle_SYNC: */ |
567 | static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) | 569 | static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, |
570 | uint32_t sent_packetnum) | ||
568 | { | 571 | { |
569 | if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) { | 572 | if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) { |
570 | /* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */ | 573 | /* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */ |
@@ -578,7 +581,8 @@ static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter, | |||
578 | return 1; | 581 | return 1; |
579 | } | 582 | } |
580 | /* case 3 in handle_SYNC: */ | 583 | /* case 3 in handle_SYNC: */ |
581 | static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, | 584 | static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, |
585 | uint32_t sent_packetnum, | ||
582 | uint32_t *req_packets, | 586 | uint32_t *req_packets, |
583 | uint16_t number) | 587 | uint16_t number) |
584 | { | 588 | { |
@@ -614,9 +618,10 @@ static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter, | |||
614 | return 1; | 618 | return 1; |
615 | } | 619 | } |
616 | 620 | ||
617 | static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 621 | static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
618 | { | 622 | { |
619 | Lossless_UDP * ludp = object; | 623 | Lossless_UDP *ludp = object; |
624 | |||
620 | if (!SYNC_valid(length)) | 625 | if (!SYNC_valid(length)) |
621 | return 1; | 626 | return 1; |
622 | 627 | ||
@@ -654,7 +659,7 @@ static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t | |||
654 | * Add a packet to the received buffer and set the recv_packetnum of the | 659 | * Add a packet to the received buffer and set the recv_packetnum of the |
655 | * connection to its proper value. Return 1 if data was too big, 0 if not. | 660 | * connection to its proper value. Return 1 if data was too big, 0 if not. |
656 | */ | 661 | */ |
657 | static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) | 662 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) |
658 | { | 663 | { |
659 | if (size > MAX_DATA_SIZE) | 664 | if (size > MAX_DATA_SIZE) |
660 | return 1; | 665 | return 1; |
@@ -688,9 +693,9 @@ static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, u | |||
688 | return 0; | 693 | return 0; |
689 | } | 694 | } |
690 | 695 | ||
691 | static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 696 | static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
692 | { | 697 | { |
693 | Lossless_UDP * ludp = object; | 698 | Lossless_UDP *ludp = object; |
694 | int connection = getconnection_id(ludp, source); | 699 | int connection = getconnection_id(ludp, source); |
695 | 700 | ||
696 | if (connection == -1) | 701 | if (connection == -1) |
@@ -717,13 +722,16 @@ static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t | |||
717 | * END of packet handling functions | 722 | * END of packet handling functions |
718 | */ | 723 | */ |
719 | 724 | ||
720 | Lossless_UDP * new_lossless_udp(Networking_Core * net) | 725 | Lossless_UDP *new_lossless_udp(Networking_Core *net) |
721 | { | 726 | { |
722 | if(net == NULL) | 727 | if (net == NULL) |
723 | return NULL; | 728 | return NULL; |
724 | Lossless_UDP * temp = calloc(1, sizeof(Lossless_UDP)); | 729 | |
730 | Lossless_UDP *temp = calloc(1, sizeof(Lossless_UDP)); | ||
731 | |||
725 | if (temp == NULL) | 732 | if (temp == NULL) |
726 | return NULL; | 733 | return NULL; |
734 | |||
727 | temp->net = net; | 735 | temp->net = net; |
728 | networking_registerhandler(net, 16, &handle_handshake, temp); | 736 | networking_registerhandler(net, 16, &handle_handshake, temp); |
729 | networking_registerhandler(net, 17, &handle_SYNC, temp); | 737 | networking_registerhandler(net, 17, &handle_SYNC, temp); |
@@ -735,7 +743,7 @@ Lossless_UDP * new_lossless_udp(Networking_Core * net) | |||
735 | * Send handshake requests | 743 | * Send handshake requests |
736 | * handshake packets are sent at the same rate as SYNC packets | 744 | * handshake packets are sent at the same rate as SYNC packets |
737 | */ | 745 | */ |
738 | static void do_new(Lossless_UDP * ludp) | 746 | static void do_new(Lossless_UDP *ludp) |
739 | { | 747 | { |
740 | uint32_t i; | 748 | uint32_t i; |
741 | uint64_t temp_time = current_time(); | 749 | uint64_t temp_time = current_time(); |
@@ -760,7 +768,7 @@ static void do_new(Lossless_UDP * ludp) | |||
760 | } | 768 | } |
761 | } | 769 | } |
762 | 770 | ||
763 | static void do_SYNC(Lossless_UDP * ludp) | 771 | static void do_SYNC(Lossless_UDP *ludp) |
764 | { | 772 | { |
765 | uint32_t i; | 773 | uint32_t i; |
766 | uint64_t temp_time = current_time(); | 774 | uint64_t temp_time = current_time(); |
@@ -774,7 +782,7 @@ static void do_SYNC(Lossless_UDP * ludp) | |||
774 | } | 782 | } |
775 | } | 783 | } |
776 | 784 | ||
777 | static void do_data(Lossless_UDP * ludp) | 785 | static void do_data(Lossless_UDP *ludp) |
778 | { | 786 | { |
779 | uint32_t i; | 787 | uint32_t i; |
780 | uint64_t j; | 788 | uint64_t j; |
@@ -797,7 +805,7 @@ static void do_data(Lossless_UDP * ludp) | |||
797 | * | 805 | * |
798 | * TODO: flow control. | 806 | * TODO: flow control. |
799 | */ | 807 | */ |
800 | static void adjust_rates(Lossless_UDP * ludp) | 808 | static void adjust_rates(Lossless_UDP *ludp) |
801 | { | 809 | { |
802 | uint32_t i; | 810 | uint32_t i; |
803 | uint64_t temp_time = current_time(); | 811 | uint64_t temp_time = current_time(); |
@@ -819,7 +827,7 @@ static void adjust_rates(Lossless_UDP * ludp) | |||
819 | } | 827 | } |
820 | 828 | ||
821 | /* Call this function a couple times per second It's the main loop. */ | 829 | /* Call this function a couple times per second It's the main loop. */ |
822 | void do_lossless_udp(Lossless_UDP * ludp) | 830 | void do_lossless_udp(Lossless_UDP *ludp) |
823 | { | 831 | { |
824 | do_new(ludp); | 832 | do_new(ludp); |
825 | do_SYNC(ludp); | 833 | do_SYNC(ludp); |
@@ -827,7 +835,7 @@ void do_lossless_udp(Lossless_UDP * ludp) | |||
827 | adjust_rates(ludp); | 835 | adjust_rates(ludp); |
828 | } | 836 | } |
829 | 837 | ||
830 | void kill_lossless_udp(Lossless_UDP * ludp) | 838 | void kill_lossless_udp(Lossless_UDP *ludp) |
831 | { | 839 | { |
832 | free(ludp->connections); | 840 | free(ludp->connections); |
833 | free(ludp); | 841 | free(ludp); |
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index 6d0ee6a2..176e86ce 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h | |||
@@ -118,14 +118,14 @@ typedef struct { | |||
118 | } Connection; | 118 | } Connection; |
119 | 119 | ||
120 | typedef struct { | 120 | typedef struct { |
121 | Networking_Core *net; | 121 | Networking_Core *net; |
122 | Connection *connections; | 122 | Connection *connections; |
123 | 123 | ||
124 | uint32_t connections_length; /* Length of connections array */ | 124 | uint32_t connections_length; /* Length of connections array */ |
125 | uint32_t connections_number; /* Number of connections in connections array */ | 125 | uint32_t connections_number; /* Number of connections in connections array */ |
126 | 126 | ||
127 | /* table of random numbers used in handshake_id. */ | 127 | /* table of random numbers used in handshake_id. */ |
128 | uint32_t randtable[6][256]; | 128 | uint32_t randtable[6][256]; |
129 | 129 | ||
130 | } Lossless_UDP; | 130 | } Lossless_UDP; |
131 | 131 | ||
@@ -135,66 +135,66 @@ uint32_t randtable[6][256]; | |||
135 | * Return -1 if it could not initialize the connection. | 135 | * Return -1 if it could not initialize the connection. |
136 | * Return number if there already was an existing connection to that ip_port. | 136 | * Return number if there already was an existing connection to that ip_port. |
137 | */ | 137 | */ |
138 | int new_connection(Lossless_UDP * ludp, IP_Port ip_port); | 138 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port); |
139 | 139 | ||
140 | /* | 140 | /* |
141 | * Get connection id from IP_Port. | 141 | * Get connection id from IP_Port. |
142 | * Return -1 if there are no connections like we are looking for. | 142 | * Return -1 if there are no connections like we are looking for. |
143 | * Return id if it found it . | 143 | * Return id if it found it . |
144 | */ | 144 | */ |
145 | int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port); | 145 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); |
146 | 146 | ||
147 | /* | 147 | /* |
148 | * Returns an int corresponding to the next connection in our imcoming connection list | 148 | * Returns an int corresponding to the next connection in our imcoming connection list |
149 | * Return -1 if there are no new incoming connections in the list. | 149 | * Return -1 if there are no new incoming connections in the list. |
150 | */ | 150 | */ |
151 | int incoming_connection(Lossless_UDP * ludp); | 151 | int incoming_connection(Lossless_UDP *ludp); |
152 | 152 | ||
153 | /* | 153 | /* |
154 | * Return -1 if it could not kill the connection. | 154 | * Return -1 if it could not kill the connection. |
155 | * Return 0 if killed successfully | 155 | * Return 0 if killed successfully |
156 | */ | 156 | */ |
157 | int kill_connection(Lossless_UDP * ludp, int connection_id); | 157 | int kill_connection(Lossless_UDP *ludp, int connection_id); |
158 | 158 | ||
159 | /* | 159 | /* |
160 | * Kill connection in seconds seconds. | 160 | * Kill connection in seconds seconds. |
161 | * Return -1 if it can not kill the connection. | 161 | * Return -1 if it can not kill the connection. |
162 | * Return 0 if it will kill it | 162 | * Return 0 if it will kill it |
163 | */ | 163 | */ |
164 | int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds); | 164 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); |
165 | 165 | ||
166 | /* | 166 | /* |
167 | * Returns the ip_port of the corresponding connection. | 167 | * Returns the ip_port of the corresponding connection. |
168 | * Return 0 if there is no such connection. | 168 | * Return 0 if there is no such connection. |
169 | */ | 169 | */ |
170 | IP_Port connection_ip(Lossless_UDP * ludp, int connection_id); | 170 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id); |
171 | 171 | ||
172 | /* | 172 | /* |
173 | * Returns the id of the next packet in the queue | 173 | * Returns the id of the next packet in the queue |
174 | * Return -1 if no packet in queue | 174 | * Return -1 if no packet in queue |
175 | */ | 175 | */ |
176 | char id_packet(Lossless_UDP * ludp, int connection_id); | 176 | char id_packet(Lossless_UDP *ludp, int connection_id); |
177 | 177 | ||
178 | /* | 178 | /* |
179 | * Return 0 if there is no received data in the buffer. | 179 | * Return 0 if there is no received data in the buffer. |
180 | * Return length of received packet if successful | 180 | * Return length of received packet if successful |
181 | */ | 181 | */ |
182 | int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data); | 182 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data); |
183 | 183 | ||
184 | /* | 184 | /* |
185 | * Return 0 if data could not be put in packet queue | 185 | * Return 0 if data could not be put in packet queue |
186 | * Return 1 if data was put into the queue | 186 | * Return 1 if data was put into the queue |
187 | */ | 187 | */ |
188 | int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length); | 188 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length); |
189 | 189 | ||
190 | /* Returns the number of packets in the queue waiting to be successfully sent. */ | 190 | /* Returns the number of packets in the queue waiting to be successfully sent. */ |
191 | uint32_t sendqueue(Lossless_UDP * ludp, int connection_id); | 191 | uint32_t sendqueue(Lossless_UDP *ludp, int connection_id); |
192 | 192 | ||
193 | /* | 193 | /* |
194 | * returns the number of packets in the queue waiting to be successfully | 194 | * returns the number of packets in the queue waiting to be successfully |
195 | * read with read_packet(...) | 195 | * read with read_packet(...) |
196 | */ | 196 | */ |
197 | uint32_t recvqueue(Lossless_UDP * ludp, int connection_id); | 197 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id); |
198 | 198 | ||
199 | /* Check if connection is connected: | 199 | /* Check if connection is connected: |
200 | * Return 0 no. | 200 | * Return 0 no. |
@@ -203,17 +203,17 @@ uint32_t recvqueue(Lossless_UDP * ludp, int connection_id); | |||
203 | * Return 3 if fully connected. | 203 | * Return 3 if fully connected. |
204 | * Return 4 if timed out and wating to be killed. | 204 | * Return 4 if timed out and wating to be killed. |
205 | */ | 205 | */ |
206 | int is_connected(Lossless_UDP * ludp, int connection_id); | 206 | int is_connected(Lossless_UDP *ludp, int connection_id); |
207 | 207 | ||
208 | /* Call this function a couple times per second It's the main loop. */ | 208 | /* Call this function a couple times per second It's the main loop. */ |
209 | void do_lossless_udp(Lossless_UDP * ludp); | 209 | void do_lossless_udp(Lossless_UDP *ludp); |
210 | 210 | ||
211 | /* | 211 | /* |
212 | * This function sets up LosslessUDP packet handling. | 212 | * This function sets up LosslessUDP packet handling. |
213 | */ | 213 | */ |
214 | Lossless_UDP * new_lossless_udp(Networking_Core * net); | 214 | Lossless_UDP *new_lossless_udp(Networking_Core *net); |
215 | 215 | ||
216 | void kill_lossless_udp(Lossless_UDP * ludp); | 216 | void kill_lossless_udp(Lossless_UDP *ludp); |
217 | 217 | ||
218 | #ifdef __cplusplus | 218 | #ifdef __cplusplus |
219 | } | 219 | } |
diff --git a/core/Messenger.c b/core/Messenger.c index dcf0235f..dd24ab09 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -624,28 +624,36 @@ static void LANdiscovery(Messenger *m) | |||
624 | Messenger *initMessenger(void) | 624 | Messenger *initMessenger(void) |
625 | { | 625 | { |
626 | Messenger *m = calloc(1, sizeof(Messenger)); | 626 | Messenger *m = calloc(1, sizeof(Messenger)); |
627 | |||
627 | if ( ! m ) | 628 | if ( ! m ) |
628 | return NULL; | 629 | return NULL; |
630 | |||
629 | IP ip; | 631 | IP ip; |
630 | ip.i = 0; | 632 | ip.i = 0; |
631 | m->net = new_networking(ip, PORT); | 633 | m->net = new_networking(ip, PORT); |
634 | |||
632 | if (m->net == NULL) { | 635 | if (m->net == NULL) { |
633 | free(m); | 636 | free(m); |
634 | return NULL; | 637 | return NULL; |
635 | } | 638 | } |
639 | |||
636 | m->net_crypto = new_net_crypto(m->net); | 640 | m->net_crypto = new_net_crypto(m->net); |
641 | |||
637 | if (m->net_crypto == NULL) { | 642 | if (m->net_crypto == NULL) { |
638 | kill_networking(m->net); | 643 | kill_networking(m->net); |
639 | free(m); | 644 | free(m); |
640 | return NULL; | 645 | return NULL; |
641 | } | 646 | } |
647 | |||
642 | m->dht = new_DHT(m->net_crypto); | 648 | m->dht = new_DHT(m->net_crypto); |
649 | |||
643 | if (m->dht == NULL) { | 650 | if (m->dht == NULL) { |
644 | kill_net_crypto(m->net_crypto); | 651 | kill_net_crypto(m->net_crypto); |
645 | kill_networking(m->net); | 652 | kill_networking(m->net); |
646 | free(m); | 653 | free(m); |
647 | return NULL; | 654 | return NULL; |
648 | } | 655 | } |
656 | |||
649 | new_keys(m->net_crypto); | 657 | new_keys(m->net_crypto); |
650 | m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); | 658 | m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); |
651 | 659 | ||
@@ -681,7 +689,8 @@ void doFriends(Messenger *m) | |||
681 | 689 | ||
682 | for (i = 0; i < m->numfriends; ++i) { | 690 | for (i = 0; i < m->numfriends; ++i) { |
683 | if (m->friendlist[i].status == FRIEND_ADDED) { | 691 | if (m->friendlist[i].status == FRIEND_ADDED) { |
684 | int fr = send_friendrequest(m->dht, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info, | 692 | int fr = send_friendrequest(m->dht, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, |
693 | m->friendlist[i].info, | ||
685 | m->friendlist[i].info_size); | 694 | m->friendlist[i].info_size); |
686 | 695 | ||
687 | if (fr >= 0) { | 696 | if (fr >= 0) { |
@@ -842,7 +851,8 @@ void doFriends(Messenger *m) | |||
842 | } | 851 | } |
843 | } | 852 | } |
844 | } else { | 853 | } else { |
845 | if (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 854 | if (is_cryptoconnected(m->net_crypto, |
855 | m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | ||
846 | crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); | 856 | crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); |
847 | m->friendlist[i].crypt_connection_id = -1; | 857 | m->friendlist[i].crypt_connection_id = -1; |
848 | set_friend_status(m, i, FRIEND_CONFIRMED); | 858 | set_friend_status(m, i, FRIEND_CONFIRMED); |
diff --git a/core/Messenger.h b/core/Messenger.h index 518becc9..581c4ba9 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -112,10 +112,10 @@ typedef struct { | |||
112 | } Friend; | 112 | } Friend; |
113 | 113 | ||
114 | typedef struct Messenger { | 114 | typedef struct Messenger { |
115 | 115 | ||
116 | Networking_Core *net; | 116 | Networking_Core *net; |
117 | Net_Crypto * net_crypto; | 117 | Net_Crypto *net_crypto; |
118 | DHT * dht; | 118 | DHT *dht; |
119 | Friend_Requests fr; | 119 | Friend_Requests fr; |
120 | uint8_t name[MAX_NAME_LENGTH]; | 120 | uint8_t name[MAX_NAME_LENGTH]; |
121 | uint16_t name_length; | 121 | uint16_t name_length; |
diff --git a/core/friend_requests.c b/core/friend_requests.c index b723de36..ee2da633 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c | |||
@@ -37,7 +37,8 @@ int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8 | |||
37 | memcpy(temp, &nospam_num, sizeof(nospam_num)); | 37 | memcpy(temp, &nospam_num, sizeof(nospam_num)); |
38 | memcpy(temp + sizeof(nospam_num), data, length); | 38 | memcpy(temp + sizeof(nospam_num), data, length); |
39 | uint8_t packet[MAX_DATA_SIZE]; | 39 | uint8_t packet[MAX_DATA_SIZE]; |
40 | int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp, length + sizeof(nospam_num), | 40 | int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp, |
41 | length + sizeof(nospam_num), | ||
41 | 32); /* 32 is friend request packet id */ | 42 | 32); /* 32 is friend request packet id */ |
42 | 43 | ||
43 | if (len == -1) | 44 | if (len == -1) |
@@ -79,7 +80,8 @@ uint32_t get_nospam(Friend_Requests *fr) | |||
79 | 80 | ||
80 | 81 | ||
81 | /* set the function that will be executed when a friend request is received. */ | 82 | /* set the function that will be executed when a friend request is received. */ |
82 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) | 83 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), |
84 | void *userdata) | ||
83 | { | 85 | { |
84 | fr->handle_friendrequest = function; | 86 | fr->handle_friendrequest = function; |
85 | fr->handle_friendrequest_isset = 1; | 87 | fr->handle_friendrequest_isset = 1; |
@@ -111,9 +113,11 @@ static int request_received(Friend_Requests *fr, uint8_t *client_id) | |||
111 | } | 113 | } |
112 | 114 | ||
113 | 115 | ||
114 | static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) | 116 | static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, |
117 | uint32_t length) | ||
115 | { | 118 | { |
116 | Friend_Requests *fr = object; | 119 | Friend_Requests *fr = object; |
120 | |||
117 | if (fr->handle_friendrequest_isset == 0) | 121 | if (fr->handle_friendrequest_isset == 0) |
118 | return 1; | 122 | return 1; |
119 | 123 | ||
diff --git a/core/friend_requests.h b/core/friend_requests.h index cc31155e..2ebd557b 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h | |||
@@ -40,7 +40,7 @@ typedef struct { | |||
40 | /*NOTE: the following is just a temporary fix for the multiple friend requests received at the same time problem | 40 | /*NOTE: the following is just a temporary fix for the multiple friend requests received at the same time problem |
41 | TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/ | 41 | TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/ |
42 | 42 | ||
43 | #define MAX_RECEIVED_STORED 32 | 43 | #define MAX_RECEIVED_STORED 32 |
44 | 44 | ||
45 | uint8_t received_requests[MAX_RECEIVED_STORED][crypto_box_PUBLICKEYBYTES]; | 45 | uint8_t received_requests[MAX_RECEIVED_STORED][crypto_box_PUBLICKEYBYTES]; |
46 | uint16_t received_requests_index; | 46 | uint16_t received_requests_index; |
@@ -57,7 +57,8 @@ uint32_t get_nospam(Friend_Requests *fr); | |||
57 | 57 | ||
58 | /* set the function that will be executed when a friend request for us is received. | 58 | /* set the function that will be executed when a friend request for us is received. |
59 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 59 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
60 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); | 60 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), |
61 | void *userdata); | ||
61 | 62 | ||
62 | /* sets up friendreq packet handlers */ | 63 | /* sets up friendreq packet handlers */ |
63 | void friendreq_init(Friend_Requests *fr, Net_Crypto *c); | 64 | void friendreq_init(Friend_Requests *fr, Net_Crypto *c); |
diff --git a/core/net_crypto.c b/core/net_crypto.c index 6c915f49..e3757ffb 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -212,7 +212,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
212 | request_id is the id of the request (32 = friend request, 254 = ping request) | 212 | request_id is the id of the request (32 = friend request, 254 = ping request) |
213 | returns -1 on failure | 213 | returns -1 on failure |
214 | returns the length of the created packet on success */ | 214 | returns the length of the created packet on success */ |
215 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, uint8_t *data, uint32_t length, uint8_t request_id) | 215 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, |
216 | uint8_t *data, uint32_t length, uint8_t request_id) | ||
216 | { | 217 | { |
217 | if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING) | 218 | if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING) |
218 | return -1; | 219 | return -1; |
@@ -240,7 +241,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
240 | in data if a friend or ping request was sent to us and returns the length of the data. | 241 | in data if a friend or ping request was sent to us and returns the length of the data. |
241 | packet is the request packet and length is its length | 242 | packet is the request packet and length is its length |
242 | return -1 if not valid request. */ | 243 | return -1 if not valid request. */ |
243 | static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length) | 244 | static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, |
245 | uint16_t length) | ||
244 | { | 246 | { |
245 | 247 | ||
246 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 248 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
@@ -274,6 +276,7 @@ void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_hand | |||
274 | static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 276 | static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
275 | { | 277 | { |
276 | DHT *dht = object; | 278 | DHT *dht = object; |
279 | |||
277 | if (packet[0] == 32) { | 280 | if (packet[0] == 32) { |
278 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || | 281 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || |
279 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 282 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
@@ -290,7 +293,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
290 | 293 | ||
291 | if (!dht->c->cryptopackethandlers[number].function) return 1; | 294 | if (!dht->c->cryptopackethandlers[number].function) return 1; |
292 | 295 | ||
293 | dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data, len); | 296 | dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data, |
297 | len); | ||
294 | 298 | ||
295 | } else { /* if request is not for us, try routing it. */ | 299 | } else { /* if request is not for us, try routing it. */ |
296 | if (route_packet(dht, packet + 1, packet, length) == length) //NOTE | 300 | if (route_packet(dht, packet + 1, packet, length) == length) //NOTE |
@@ -304,7 +308,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
304 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 308 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
305 | to peer with connection_id and public_key | 309 | to peer with connection_id and public_key |
306 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ | 310 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ |
307 | static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) | 311 | static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
312 | uint8_t *session_key) | ||
308 | { | 313 | { |
309 | uint8_t temp_data[MAX_DATA_SIZE]; | 314 | uint8_t temp_data[MAX_DATA_SIZE]; |
310 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 315 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
@@ -323,7 +328,8 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
323 | temp_data[0] = 2; | 328 | temp_data[0] = 2; |
324 | memcpy(temp_data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 329 | memcpy(temp_data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
325 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 330 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); |
326 | return write_packet(c->lossless_udp, connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 331 | return write_packet(c->lossless_udp, connection_id, temp_data, |
332 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | ||
327 | } | 333 | } |
328 | 334 | ||
329 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length | 335 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length |
@@ -431,7 +437,7 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
431 | ++c->crypto_connections_length; | 437 | ++c->crypto_connections_length; |
432 | 438 | ||
433 | if (send_cryptohandshake(c, id, public_key, c->crypto_connections[i].recv_nonce, | 439 | if (send_cryptohandshake(c, id, public_key, c->crypto_connections[i].recv_nonce, |
434 | c->crypto_connections[i].sessionpublic_key) == 1) { | 440 | c->crypto_connections[i].sessionpublic_key) == 1) { |
435 | increment_nonce(c->crypto_connections[i].recv_nonce); | 441 | increment_nonce(c->crypto_connections[i].recv_nonce); |
436 | return i; | 442 | return i; |
437 | } | 443 | } |
@@ -456,7 +462,8 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
456 | 462 | ||
457 | for (i = 0; i < MAX_INCOMING; ++i) { | 463 | for (i = 0; i < MAX_INCOMING; ++i) { |
458 | if (c->incoming_connections[i] != -1) { | 464 | if (c->incoming_connections[i] != -1) { |
459 | if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4 || is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) { | 465 | if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4 |
466 | || is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) { | ||
460 | kill_connection(c->lossless_udp, c->incoming_connections[i]); | 467 | kill_connection(c->lossless_udp, c->incoming_connections[i]); |
461 | c->incoming_connections[i] = -1; | 468 | c->incoming_connections[i] = -1; |
462 | continue; | 469 | continue; |
@@ -509,7 +516,8 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
509 | /* accept an incoming connection using the parameters provided by crypto_inbound | 516 | /* accept an incoming connection using the parameters provided by crypto_inbound |
510 | return -1 if not successful | 517 | return -1 if not successful |
511 | returns the crypt_connection_id if successful */ | 518 | returns the crypt_connection_id if successful */ |
512 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) | 519 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
520 | uint8_t *session_key) | ||
513 | { | 521 | { |
514 | uint32_t i; | 522 | uint32_t i; |
515 | 523 | ||
@@ -549,7 +557,8 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
549 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, | 557 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, |
550 | c->crypto_connections[i].sessionsecret_key, | 558 | c->crypto_connections[i].sessionsecret_key, |
551 | c->crypto_connections[i].shared_key); | 559 | c->crypto_connections[i].shared_key); |
552 | c->crypto_connections[i].status = CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ | 560 | c->crypto_connections[i].status = |
561 | CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ | ||
553 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); | 562 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); |
554 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ | 563 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ |
555 | return i; | 564 | return i; |
@@ -651,12 +660,14 @@ static void receive_crypto(Net_Crypto *c) | |||
651 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, | 660 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, |
652 | c->crypto_connections[i].sessionsecret_key, | 661 | c->crypto_connections[i].sessionsecret_key, |
653 | c->crypto_connections[i].shared_key); | 662 | c->crypto_connections[i].shared_key); |
654 | c->crypto_connections[i].status = CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ | 663 | c->crypto_connections[i].status = |
664 | CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ | ||
655 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); | 665 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); |
656 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ | 666 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ |
657 | } | 667 | } |
658 | } | 668 | } |
659 | } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) { // This should not happen kill the connection if it does | 669 | } else if (id_packet(c->lossless_udp, |
670 | c->crypto_connections[i].number) != -1) { // This should not happen kill the connection if it does | ||
660 | crypto_kill(c, i); | 671 | crypto_kill(c, i); |
661 | return; | 672 | return; |
662 | } | 673 | } |
@@ -697,16 +708,21 @@ static void receive_crypto(Net_Crypto *c) | |||
697 | 708 | ||
698 | /* run this to (re)initialize net_crypto | 709 | /* run this to (re)initialize net_crypto |
699 | sets all the global connection variables to their default values. */ | 710 | sets all the global connection variables to their default values. */ |
700 | Net_Crypto * new_net_crypto(Networking_Core * net) | 711 | Net_Crypto *new_net_crypto(Networking_Core *net) |
701 | { | 712 | { |
702 | if (net == NULL) | 713 | if (net == NULL) |
703 | return NULL; | 714 | return NULL; |
704 | Net_Crypto * temp = calloc(1, sizeof(Net_Crypto)); | 715 | |
716 | Net_Crypto *temp = calloc(1, sizeof(Net_Crypto)); | ||
717 | |||
705 | if (temp == NULL) | 718 | if (temp == NULL) |
706 | return NULL; | 719 | return NULL; |
720 | |||
707 | temp->lossless_udp = new_lossless_udp(net); | 721 | temp->lossless_udp = new_lossless_udp(net); |
722 | |||
708 | if (temp->lossless_udp == NULL) | 723 | if (temp->lossless_udp == NULL) |
709 | return NULL; | 724 | return NULL; |
725 | |||
710 | memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); | 726 | memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); |
711 | return temp; | 727 | return temp; |
712 | } | 728 | } |
@@ -722,7 +738,8 @@ static void kill_timedout(Net_Crypto *c) | |||
722 | uint32_t i; | 738 | uint32_t i; |
723 | 739 | ||
724 | for (i = 0; i < c->crypto_connections_length; ++i) { | 740 | for (i = 0; i < c->crypto_connections_length; ++i) { |
725 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION && is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4) | 741 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION |
742 | && is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4) | ||
726 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 743 | c->crypto_connections[i].status = CONN_TIMED_OUT; |
727 | else if (is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4) { | 744 | else if (is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4) { |
728 | kill_connection(c->lossless_udp, c->crypto_connections[i].number); | 745 | kill_connection(c->lossless_udp, c->crypto_connections[i].number); |
@@ -745,9 +762,10 @@ void kill_net_crypto(Net_Crypto *c) | |||
745 | uint32_t i; | 762 | uint32_t i; |
746 | 763 | ||
747 | for (i = 0; i < c->crypto_connections_length; ++i) { | 764 | for (i = 0; i < c->crypto_connections_length; ++i) { |
748 | crypto_kill(c, i); | 765 | crypto_kill(c, i); |
749 | } | 766 | } |
767 | |||
750 | kill_lossless_udp(c->lossless_udp); | 768 | kill_lossless_udp(c->lossless_udp); |
751 | memset(c, 0, sizeof(Net_Crypto)); | 769 | memset(c, 0, sizeof(Net_Crypto)); |
752 | free(c); | 770 | free(c); |
753 | } | 771 | } |
diff --git a/core/net_crypto.h b/core/net_crypto.h index 7b9e9b97..46bcf250 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -47,15 +47,16 @@ typedef struct { | |||
47 | 47 | ||
48 | } Crypto_Connection; | 48 | } Crypto_Connection; |
49 | 49 | ||
50 | typedef int (*cryptopacket_handler_callback)(void * object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, uint32_t len); | 50 | typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, |
51 | uint32_t len); | ||
51 | 52 | ||
52 | typedef struct { | 53 | typedef struct { |
53 | cryptopacket_handler_callback function; | 54 | cryptopacket_handler_callback function; |
54 | void * object; | 55 | void *object; |
55 | }Cryptopacket_Handles; | 56 | } Cryptopacket_Handles; |
56 | 57 | ||
57 | typedef struct { | 58 | typedef struct { |
58 | Lossless_UDP * lossless_udp; | 59 | Lossless_UDP *lossless_udp; |
59 | 60 | ||
60 | Crypto_Connection *crypto_connections; | 61 | Crypto_Connection *crypto_connections; |
61 | 62 | ||
@@ -127,11 +128,12 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
127 | request_id is the id of the request (32 = friend request, 254 = ping request) | 128 | request_id is the id of the request (32 = friend request, 254 = ping request) |
128 | returns -1 on failure | 129 | returns -1 on failure |
129 | returns the length of the created packet on success */ | 130 | returns the length of the created packet on success */ |
130 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, uint8_t *data, uint32_t length, uint8_t request_id); | 131 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, |
132 | uint8_t *data, uint32_t length, uint8_t request_id); | ||
131 | 133 | ||
132 | 134 | ||
133 | /* Function to call when request beginning with byte is received */ | 135 | /* Function to call when request beginning with byte is received */ |
134 | void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void * object); | 136 | void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); |
135 | 137 | ||
136 | /* Start a secure connection with other peer who has public_key and ip_port | 138 | /* Start a secure connection with other peer who has public_key and ip_port |
137 | returns -1 if failure | 139 | returns -1 if failure |
@@ -155,7 +157,8 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
155 | /* accept an incoming connection using the parameters provided by crypto_inbound | 157 | /* accept an incoming connection using the parameters provided by crypto_inbound |
156 | return -1 if not successful | 158 | return -1 if not successful |
157 | returns the crypt_connection_id if successful */ | 159 | returns the crypt_connection_id if successful */ |
158 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); | 160 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
161 | uint8_t *session_key); | ||
159 | 162 | ||
160 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 163 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
161 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 164 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
@@ -177,7 +180,7 @@ void load_keys(Net_Crypto *c, uint8_t *keys); | |||
177 | 180 | ||
178 | /* create new instance of Net_Crypto | 181 | /* create new instance of Net_Crypto |
179 | sets all the global connection variables to their default values. */ | 182 | sets all the global connection variables to their default values. */ |
180 | Net_Crypto * new_net_crypto(Networking_Core * net); | 183 | Net_Crypto *new_net_crypto(Networking_Core *net); |
181 | 184 | ||
182 | /* main loop */ | 185 | /* main loop */ |
183 | void do_net_crypto(Net_Crypto *c); | 186 | void do_net_crypto(Net_Crypto *c); |
diff --git a/core/network.c b/core/network.c index 9ec390fb..849c7e2a 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -86,13 +86,13 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le | |||
86 | return 0; | 86 | return 0; |
87 | } | 87 | } |
88 | 88 | ||
89 | void networking_registerhandler(Networking_Core * net, uint8_t byte, packet_handler_callback cb, void * object) | 89 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object) |
90 | { | 90 | { |
91 | net->packethandlers[byte].function = cb; | 91 | net->packethandlers[byte].function = cb; |
92 | net->packethandlers[byte].object = object; | 92 | net->packethandlers[byte].object = object; |
93 | } | 93 | } |
94 | 94 | ||
95 | void networking_poll(Networking_Core * net) | 95 | void networking_poll(Networking_Core *net) |
96 | { | 96 | { |
97 | IP_Port ip_port; | 97 | IP_Port ip_port; |
98 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 98 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
@@ -112,6 +112,7 @@ static void at_startup(void) | |||
112 | { | 112 | { |
113 | if (at_startup_ran != 0) | 113 | if (at_startup_ran != 0) |
114 | return; | 114 | return; |
115 | |||
115 | #ifdef WIN32 | 116 | #ifdef WIN32 |
116 | WSADATA wsaData; | 117 | WSADATA wsaData; |
117 | 118 | ||
@@ -121,7 +122,7 @@ static void at_startup(void) | |||
121 | #else | 122 | #else |
122 | srandom((uint32_t)current_time()); | 123 | srandom((uint32_t)current_time()); |
123 | #endif | 124 | #endif |
124 | srand((uint32_t)current_time()); | 125 | srand((uint32_t)current_time()); |
125 | at_startup_ran = 1; | 126 | at_startup_ran = 1; |
126 | } | 127 | } |
127 | 128 | ||
@@ -130,7 +131,7 @@ static void at_shutdown(void) | |||
130 | { | 131 | { |
131 | #ifdef WIN32 | 132 | #ifdef WIN32 |
132 | WSACleanup(); | 133 | WSACleanup(); |
133 | #endif | 134 | #endif |
134 | } | 135 | } |
135 | */ | 136 | */ |
136 | 137 | ||
@@ -140,13 +141,15 @@ static void at_shutdown(void) | |||
140 | port is in host byte order (this means don't worry about it) | 141 | port is in host byte order (this means don't worry about it) |
141 | returns Networking_Core object if no problems | 142 | returns Networking_Core object if no problems |
142 | returns NULL if there are problems */ | 143 | returns NULL if there are problems */ |
143 | Networking_Core * new_networking(IP ip, uint16_t port) | 144 | Networking_Core *new_networking(IP ip, uint16_t port) |
144 | { | 145 | { |
145 | at_startup(); | 146 | at_startup(); |
146 | /* initialize our socket */ | 147 | /* initialize our socket */ |
147 | Networking_Core * temp = calloc(1, sizeof(Networking_Core)); | 148 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); |
149 | |||
148 | if (temp == NULL) | 150 | if (temp == NULL) |
149 | return NULL; | 151 | return NULL; |
152 | |||
150 | temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 153 | temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
151 | 154 | ||
152 | /* Check for socket error */ | 155 | /* Check for socket error */ |
@@ -200,7 +203,7 @@ Networking_Core * new_networking(IP ip, uint16_t port) | |||
200 | } | 203 | } |
201 | 204 | ||
202 | /* function to cleanup networking stuff */ | 205 | /* function to cleanup networking stuff */ |
203 | void kill_networking(Networking_Core * net) | 206 | void kill_networking(Networking_Core *net) |
204 | { | 207 | { |
205 | #ifdef WIN32 | 208 | #ifdef WIN32 |
206 | closesocket(net->sock); | 209 | closesocket(net->sock); |
diff --git a/core/network.h b/core/network.h index 47d9fff2..87f45978 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -99,18 +99,18 @@ typedef struct { | |||
99 | /* Function to receive data, ip and port of sender is put into ip_port | 99 | /* Function to receive data, ip and port of sender is put into ip_port |
100 | the packet data into data | 100 | the packet data into data |
101 | the packet length into length. */ | 101 | the packet length into length. */ |
102 | typedef int (*packet_handler_callback)(void * object, IP_Port ip_port, uint8_t *data, uint32_t len); | 102 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); |
103 | 103 | ||
104 | typedef struct { | 104 | typedef struct { |
105 | packet_handler_callback function; | 105 | packet_handler_callback function; |
106 | void * object; | 106 | void *object; |
107 | }Packet_Handles; | 107 | } Packet_Handles; |
108 | 108 | ||
109 | typedef struct { | 109 | typedef struct { |
110 | Packet_Handles packethandlers[256]; | 110 | Packet_Handles packethandlers[256]; |
111 | /* our UDP socket */ | 111 | /* our UDP socket */ |
112 | int sock; | 112 | int sock; |
113 | }Networking_Core; | 113 | } Networking_Core; |
114 | 114 | ||
115 | /* returns current time in milleseconds since the epoch. */ | 115 | /* returns current time in milleseconds since the epoch. */ |
116 | uint64_t current_time(void); | 116 | uint64_t current_time(void); |
@@ -125,10 +125,10 @@ uint32_t random_int(void); | |||
125 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | 125 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); |
126 | 126 | ||
127 | /* Function to call when packet beginning with byte is received */ | 127 | /* Function to call when packet beginning with byte is received */ |
128 | void networking_registerhandler(Networking_Core * net, uint8_t byte, packet_handler_callback cb, void * object); | 128 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
129 | 129 | ||
130 | /* call this several times a second */ | 130 | /* call this several times a second */ |
131 | void networking_poll(Networking_Core * net); | 131 | void networking_poll(Networking_Core *net); |
132 | 132 | ||
133 | /* initialize networking | 133 | /* initialize networking |
134 | bind to ip and port | 134 | bind to ip and port |
@@ -136,10 +136,10 @@ void networking_poll(Networking_Core * net); | |||
136 | port is in host byte order (this means don't worry about it) | 136 | port is in host byte order (this means don't worry about it) |
137 | returns 0 if no problems | 137 | returns 0 if no problems |
138 | returns -1 if there were problems */ | 138 | returns -1 if there were problems */ |
139 | Networking_Core * new_networking(IP ip, uint16_t port); | 139 | Networking_Core *new_networking(IP ip, uint16_t port); |
140 | 140 | ||
141 | /* function to cleanup networking stuff(doesn't do much right now) */ | 141 | /* function to cleanup networking stuff(doesn't do much right now) */ |
142 | void kill_networking(Networking_Core * net); | 142 | void kill_networking(Networking_Core *net); |
143 | 143 | ||
144 | /* | 144 | /* |
145 | resolve_addr(): | 145 | resolve_addr(): |
diff --git a/core/ping.c b/core/ping.c index 5da3c0ca..4bb9c38a 100644 --- a/core/ping.c +++ b/core/ping.c | |||
@@ -29,12 +29,12 @@ typedef struct { | |||
29 | size_t pos_pings; | 29 | size_t pos_pings; |
30 | } PING; | 30 | } PING; |
31 | 31 | ||
32 | void * new_ping(void) | 32 | void *new_ping(void) |
33 | { | 33 | { |
34 | return calloc(1, sizeof(PING)); | 34 | return calloc(1, sizeof(PING)); |
35 | } | 35 | } |
36 | 36 | ||
37 | void kill_ping(void * ping) | 37 | void kill_ping(void *ping) |
38 | { | 38 | { |
39 | free(ping); | 39 | free(ping); |
40 | } | 40 | } |
@@ -44,9 +44,9 @@ static bool is_timeout(uint64_t time) | |||
44 | return (time + PING_TIMEOUT) < now(); | 44 | return (time + PING_TIMEOUT) < now(); |
45 | } | 45 | } |
46 | 46 | ||
47 | static void remove_timeouts(void * ping) // O(n) | 47 | static void remove_timeouts(void *ping) // O(n) |
48 | { | 48 | { |
49 | PING * png = ping; | 49 | PING *png = ping; |
50 | size_t i, id; | 50 | size_t i, id; |
51 | size_t new_pos = png->pos_pings; | 51 | size_t new_pos = png->pos_pings; |
52 | size_t new_num = png->num_pings; | 52 | size_t new_num = png->num_pings; |
@@ -69,9 +69,9 @@ static void remove_timeouts(void * ping) // O(n) | |||
69 | png->pos_pings = new_pos % PING_NUM_MAX; | 69 | png->pos_pings = new_pos % PING_NUM_MAX; |
70 | } | 70 | } |
71 | 71 | ||
72 | uint64_t add_ping(void * ping, IP_Port ipp) // O(n) | 72 | uint64_t add_ping(void *ping, IP_Port ipp) // O(n) |
73 | { | 73 | { |
74 | PING * png = ping; | 74 | PING *png = ping; |
75 | size_t p; | 75 | size_t p; |
76 | 76 | ||
77 | remove_timeouts(ping); | 77 | remove_timeouts(ping); |
@@ -93,9 +93,10 @@ uint64_t add_ping(void * ping, IP_Port ipp) // O(n) | |||
93 | return png->pings[p].id; | 93 | return png->pings[p].id; |
94 | } | 94 | } |
95 | 95 | ||
96 | bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. | 96 | bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. |
97 | { | 97 | { |
98 | PING * png = ping; | 98 | PING *png = ping; |
99 | |||
99 | if (ipp.ip.i == 0 && ping_id == 0) | 100 | if (ipp.ip.i == 0 && ping_id == 0) |
100 | return false; | 101 | return false; |
101 | 102 | ||
@@ -115,7 +116,7 @@ bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: repl | |||
115 | return false; | 116 | return false; |
116 | } | 117 | } |
117 | 118 | ||
118 | int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id) | 119 | int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id) |
119 | { | 120 | { |
120 | pingreq_t pk; | 121 | pingreq_t pk; |
121 | int rc; | 122 | int rc; |
@@ -169,9 +170,9 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64 | |||
169 | return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); | 170 | return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); |
170 | } | 171 | } |
171 | 172 | ||
172 | int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 173 | int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
173 | { | 174 | { |
174 | DHT * dht = object; | 175 | DHT *dht = object; |
175 | pingreq_t *p = (pingreq_t *) packet; | 176 | pingreq_t *p = (pingreq_t *) packet; |
176 | int rc; | 177 | int rc; |
177 | uint64_t ping_id; | 178 | uint64_t ping_id; |
@@ -197,9 +198,9 @@ int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t | |||
197 | return 0; | 198 | return 0; |
198 | } | 199 | } |
199 | 200 | ||
200 | int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length) | 201 | int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
201 | { | 202 | { |
202 | DHT * dht = object; | 203 | DHT *dht = object; |
203 | pingres_t *p = (pingres_t *) packet; | 204 | pingres_t *p = (pingres_t *) packet; |
204 | int rc; | 205 | int rc; |
205 | uint64_t ping_id; | 206 | uint64_t ping_id; |
diff --git a/core/ping.h b/core/ping.h index 1f30392c..c04ec80e 100644 --- a/core/ping.h +++ b/core/ping.h | |||
@@ -7,11 +7,11 @@ | |||
7 | 7 | ||
8 | #include <stdbool.h> | 8 | #include <stdbool.h> |
9 | 9 | ||
10 | void * new_ping(void); | 10 | void *new_ping(void); |
11 | void kill_ping(void * ping); | 11 | void kill_ping(void *ping); |
12 | uint64_t add_ping(void * ping, IP_Port ipp); | 12 | uint64_t add_ping(void *ping, IP_Port ipp); |
13 | bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id); | 13 | bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id); |
14 | int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id); | 14 | int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id); |
15 | int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id); | 15 | int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id); |
16 | int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length); | 16 | int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length); |
17 | int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length); | 17 | int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length); |