summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/DHT.c108
-rw-r--r--core/DHT.h36
-rw-r--r--core/LAN_discovery.c3
-rw-r--r--core/Lossless_UDP.c92
-rw-r--r--core/Lossless_UDP.h42
-rw-r--r--core/Messenger.c14
-rw-r--r--core/Messenger.h6
-rw-r--r--core/friend_requests.c10
-rw-r--r--core/friend_requests.h5
-rw-r--r--core/net_crypto.c54
-rw-r--r--core/net_crypto.h19
-rw-r--r--core/network.c19
-rw-r--r--core/network.h16
-rw-r--r--core/ping.c27
-rw-r--r--core/ping.h14
-rw-r--r--other/DHT_bootstrap.c4
-rw-r--r--other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c9
-rw-r--r--testing/DHT_test.c12
-rw-r--r--testing/Lossless_UDP_testclient.c41
-rw-r--r--testing/Lossless_UDP_testserver.c14
-rw-r--r--testing/toxic/chat.c2
-rw-r--r--testing/toxic/main.c4
-rw-r--r--testing/toxic/prompt.c2
-rw-r--r--testing/toxic/windows.c46
24 files changed, 332 insertions, 267 deletions
diff --git a/core/DHT.c b/core/DHT.c
index c8994437..ab631604 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -54,7 +54,7 @@
54#define TIME_TOPING 5 54#define TIME_TOPING 5
55 55
56 56
57Client_data *DHT_get_close_list(DHT * dht) 57Client_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 */
149static int friend_number(DHT * dht, uint8_t *client_id) 149static 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 */
166static int get_close_nodes(DHT * dht, uint8_t *client_id, Node_format *nodes_list) 166static 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 */
333void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id) 333void 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 */
375static void returnedip_ports(DHT * dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) 375static 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. */
409static int is_gettingnodes(DHT * dht, IP_Port ip_port, uint64_t ping_id) 409static 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 */
434static uint64_t add_gettingnodes(DHT * dht, IP_Port ip_port) 434static 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 */
455static int getnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id) 455static 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 */
494static int sendnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) 494static 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
535static int handle_getnodes(void * object, IP_Port source, uint8_t *packet, uint32_t length) 535static 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
568static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint32_t length) 568static 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
616int DHT_addfriend(DHT * dht, uint8_t *client_id) 616int 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
636int DHT_delfriend(DHT * dht, uint8_t *client_id) 636int 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. */
672IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id) 672IP_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 */
698static void do_DHT_friends(DHT * dht) 698static 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 */
738static void do_Close(DHT * dht) 738static 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
772void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key) 772void 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 */
781int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length) 781int 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 */
799static int friend_iplist(DHT * dht, IP_Port *ip_portlist, uint16_t friend_num) 799static 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 */
834int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) 834int 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*/
869static int routeone_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) 869static 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 */
909int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id) 909int 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
925static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_t type) 925static 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 */
952static int handle_NATping(void * object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) 953static 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
1029static void punch_holes(DHT * dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num) 1030static 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
1047static void do_NAT(DHT * dht) 1048static 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 */
1096int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port) 1097int 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*/
1126static void do_toping(DHT * dht) 1127static 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
1146DHT * new_DHT(Net_Crypto *c) 1147DHT *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
1167void do_DHT(DHT * dht) 1173void 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}
1174void kill_DHT(DHT * dht) 1180void 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) */
1182uint32_t DHT_size(DHT * dht) 1188uint32_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() */
1188void DHT_save(DHT * dht, uint8_t *data) 1194void 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 */
1198int DHT_load(DHT * dht, uint8_t *data, uint32_t size) 1204int 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 */
1243int DHT_isconnected(DHT * dht) 1249int 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();
diff --git a/core/DHT.h b/core/DHT.h
index 7d926b13..6295581b 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -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
107Client_data *DHT_get_close_list(DHT * dht); 107Client_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) */
113int DHT_addfriend(DHT * dht, uint8_t *client_id); 113int 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) */
119int DHT_delfriend(DHT * dht, uint8_t *client_id); 119int 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. */
128IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id); 128IP_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) */
131void do_DHT(DHT * dht); 131void 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 */
135void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key); 135void 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 */
145int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port); 145int 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 */
151int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length); 151int 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 */
155int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); 155int 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*/
163int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id); 163int 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) */
168uint32_t DHT_size(DHT * dht); 168uint32_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() */
171void DHT_save(DHT * dht, uint8_t *data); 171void DHT_save(DHT *dht, uint8_t *data);
172 172
173/* init DHT */ 173/* init DHT */
174DHT * new_DHT(Net_Crypto *c); 174DHT *new_DHT(Net_Crypto *c);
175 175
176void kill_DHT(DHT * dht); 176void 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 */
181int DHT_load(DHT * dht, uint8_t *data, uint32_t size); 181int 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 */
185int DHT_isconnected(DHT * dht); 185int DHT_isconnected(DHT *dht);
186 186
187void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id); 187void 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
124static int handle_LANdiscovery(void * object, IP_Port source, uint8_t *packet, uint32_t length) 124static 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 */
39int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port) 39int 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 */
60static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source) 60static 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 */
82static void change_handshake(Lossless_UDP * ludp, IP_Port source) 82static 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 */
94int new_connection(Lossless_UDP * ludp, IP_Port ip_port) 94int 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 */
151static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port) 151static 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 */
203int incoming_connection(Lossless_UDP * ludp) 203int 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. */
218static void free_connections(Lossless_UDP * ludp) 218static 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 */
250int kill_connection(Lossless_UDP * ludp, int connection_id) 250int 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 */
270int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds) 270int 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 */
290int is_connected(Lossless_UDP * ludp, int connection_id) 290int 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. */
299IP_Port connection_ip(Lossless_UDP * ludp, int connection_id) 299IP_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. */
309uint32_t sendqueue(Lossless_UDP * ludp, int connection_id) 309uint32_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(...) */
318uint32_t recvqueue(Lossless_UDP * ludp, int connection_id) 318uint32_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 */
328char id_packet(Lossless_UDP * ludp, int connection_id) 328char 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 */
341int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data) 342int 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 */
359int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length) 360int 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 */
376uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *requested) 377uint32_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
406static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) 407static 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
420static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id) 421static 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
447static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_t packet_num) 448static 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 */
462static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id) 463static 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. */
494static int handle_handshake(void * object, IP_Port source, uint8_t *packet, uint32_t length) 495static 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: */
545static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 547static 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: */
567static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) 569static 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: */
581static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, 584static 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
617static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t length) 621static 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 */
657static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) 662static 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
691static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t length) 696static 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
720Lossless_UDP * new_lossless_udp(Networking_Core * net) 725Lossless_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 */
738static void do_new(Lossless_UDP * ludp) 746static 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
763static void do_SYNC(Lossless_UDP * ludp) 771static 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
777static void do_data(Lossless_UDP * ludp) 785static 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 */
800static void adjust_rates(Lossless_UDP * ludp) 808static 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. */
822void do_lossless_udp(Lossless_UDP * ludp) 830void 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
830void kill_lossless_udp(Lossless_UDP * ludp) 838void 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
120typedef struct { 120typedef struct {
121Networking_Core *net; 121 Networking_Core *net;
122Connection *connections; 122 Connection *connections;
123 123
124uint32_t connections_length; /* Length of connections array */ 124 uint32_t connections_length; /* Length of connections array */
125uint32_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. */
128uint32_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 */
138int new_connection(Lossless_UDP * ludp, IP_Port ip_port); 138int 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 */
145int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port); 145int 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 */
151int incoming_connection(Lossless_UDP * ludp); 151int 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 */
157int kill_connection(Lossless_UDP * ludp, int connection_id); 157int 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 */
164int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds); 164int 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 */
170IP_Port connection_ip(Lossless_UDP * ludp, int connection_id); 170IP_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 */
176char id_packet(Lossless_UDP * ludp, int connection_id); 176char 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 */
182int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data); 182int 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 */
188int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length); 188int 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. */
191uint32_t sendqueue(Lossless_UDP * ludp, int connection_id); 191uint32_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 */
197uint32_t recvqueue(Lossless_UDP * ludp, int connection_id); 197uint32_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 */
206int is_connected(Lossless_UDP * ludp, int connection_id); 206int 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. */
209void do_lossless_udp(Lossless_UDP * ludp); 209void 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 */
214Lossless_UDP * new_lossless_udp(Networking_Core * net); 214Lossless_UDP *new_lossless_udp(Networking_Core *net);
215 215
216void kill_lossless_udp(Lossless_UDP * ludp); 216void 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)
624Messenger *initMessenger(void) 624Messenger *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
114typedef struct Messenger { 114typedef 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. */
82void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 83void 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
114static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) 116static 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) */
60void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); 60void 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 */
63void friendreq_init(Friend_Requests *fr, Net_Crypto *c); 64void 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 */
215int 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) 215int 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. */
243static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length) 244static 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
274static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length) 276static 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 */
307static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) 311static 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 */
512int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) 519int 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. */
700Net_Crypto * new_net_crypto(Networking_Core * net) 711Net_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
50typedef int (*cryptopacket_handler_callback)(void * object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, uint32_t len); 50typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data,
51 uint32_t len);
51 52
52typedef struct { 53typedef 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
57typedef struct { 58typedef 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 */
130int 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); 131int 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 */
134void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void * object); 136void 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 */
158int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); 160int 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. */
180Net_Crypto * new_net_crypto(Networking_Core * net); 183Net_Crypto *new_net_crypto(Networking_Core *net);
181 184
182/* main loop */ 185/* main loop */
183void do_net_crypto(Net_Crypto *c); 186void 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
89void networking_registerhandler(Networking_Core * net, uint8_t byte, packet_handler_callback cb, void * object) 89void 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
95void networking_poll(Networking_Core * net) 95void 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 */
143Networking_Core * new_networking(IP ip, uint16_t port) 144Networking_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 */
203void kill_networking(Networking_Core * net) 206void 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. */
102typedef int (*packet_handler_callback)(void * object, IP_Port ip_port, uint8_t *data, uint32_t len); 102typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len);
103 103
104typedef struct { 104typedef 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
109typedef struct { 109typedef 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. */
116uint64_t current_time(void); 116uint64_t current_time(void);
@@ -125,10 +125,10 @@ uint32_t random_int(void);
125int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); 125int 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 */
128void networking_registerhandler(Networking_Core * net, uint8_t byte, packet_handler_callback cb, void * object); 128void 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 */
131void networking_poll(Networking_Core * net); 131void 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 */
139Networking_Core * new_networking(IP ip, uint16_t port); 139Networking_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) */
142void kill_networking(Networking_Core * net); 142void 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
32void * new_ping(void) 32void *new_ping(void)
33{ 33{
34 return calloc(1, sizeof(PING)); 34 return calloc(1, sizeof(PING));
35} 35}
36 36
37void kill_ping(void * ping) 37void 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
47static void remove_timeouts(void * ping) // O(n) 47static 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
72uint64_t add_ping(void * ping, IP_Port ipp) // O(n) 72uint64_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
96bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. 96bool 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
118int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id) 119int 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
172int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length) 173int 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
200int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length) 201int 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
10void * new_ping(void); 10void *new_ping(void);
11void kill_ping(void * ping); 11void kill_ping(void *ping);
12uint64_t add_ping(void * ping, IP_Port ipp); 12uint64_t add_ping(void *ping, IP_Port ipp);
13bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id); 13bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id);
14int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id); 14int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id);
15int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id); 15int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id);
16int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length); 16int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length);
17int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length); 17int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length);
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c
index 4a91679c..5d97b37a 100644
--- a/other/DHT_bootstrap.c
+++ b/other/DHT_bootstrap.c
@@ -44,7 +44,7 @@
44 44
45 45
46 46
47void manage_keys(DHT * dht) 47void manage_keys(DHT *dht)
48{ 48{
49 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 49 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
50 uint8_t keys[KEYS_SIZE]; 50 uint8_t keys[KEYS_SIZE];
@@ -85,7 +85,7 @@ int main(int argc, char *argv[])
85 //bind to ip 0.0.0.0:PORT 85 //bind to ip 0.0.0.0:PORT
86 IP ip; 86 IP ip;
87 ip.i = 0; 87 ip.i = 0;
88 DHT * dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); 88 DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
89 init_cryptopackets(dht); 89 init_cryptopackets(dht);
90 manage_keys(dht); 90 manage_keys(dht);
91 printf("Public key: "); 91 printf("Public key: ");
diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
index 8eb1530b..81ac538b 100644
--- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
+++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
@@ -77,7 +77,7 @@ and connect to them.
77returns 1 if the connection to the DHT is up 77returns 1 if the connection to the DHT is up
78returns -1 if all attempts failed 78returns -1 if all attempts failed
79*/ 79*/
80int connect_to_servers(DHT * dht, struct server_info_s *info) 80int connect_to_servers(DHT *dht, struct server_info_s *info)
81{ 81{
82 int i; 82 int i;
83 int c; 83 int c;
@@ -115,7 +115,7 @@ int connect_to_servers(DHT * dht, struct server_info_s *info)
115 return 0; 115 return 0;
116} 116}
117 117
118void manage_keys(DHT * dht, char *keys_file) 118void manage_keys(DHT *dht, char *keys_file)
119{ 119{
120 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 120 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
121 uint8_t keys[KEYS_SIZE]; 121 uint8_t keys[KEYS_SIZE];
@@ -293,13 +293,14 @@ int main(int argc, char *argv[])
293 printf("Please specify a configuration file.\n"); 293 printf("Please specify a configuration file.\n");
294 exit(EXIT_FAILURE); 294 exit(EXIT_FAILURE);
295 } 295 }
296
296 server_conf = configure_server(argv[1]); 297 server_conf = configure_server(argv[1]);
297 298
298 /* initialize networking 299 /* initialize networking
299 bind to ip 0.0.0.0:PORT */ 300 bind to ip 0.0.0.0:PORT */
300 IP ip; 301 IP ip;
301 ip.i = 0; 302 ip.i = 0;
302 DHT * dht = new_DHT(new_net_crypto(new_networking(ip, server_conf.port))); 303 DHT *dht = new_DHT(new_net_crypto(new_networking(ip, server_conf.port)));
303 /* Read the config file */ 304 /* Read the config file */
304 printf("PID file: %s\n", server_conf.pid_file); 305 printf("PID file: %s\n", server_conf.pid_file);
305 printf("Key file: %s\n", server_conf.keys_file); 306 printf("Key file: %s\n", server_conf.keys_file);
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index a9575fd5..8ab9157e 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -48,7 +48,7 @@
48 48
49#define PORT 33445 49#define PORT 33445
50 50
51void print_clientlist(DHT * dht) 51void print_clientlist(DHT *dht)
52{ 52{
53 uint32_t i, j; 53 uint32_t i, j;
54 IP_Port p_ip; 54 IP_Port p_ip;
@@ -71,7 +71,7 @@ void print_clientlist(DHT * dht)
71 } 71 }
72} 72}
73 73
74void print_friendlist(DHT * dht) 74void print_friendlist(DHT *dht)
75{ 75{
76 uint32_t i, j, k; 76 uint32_t i, j, k;
77 IP_Port p_ip; 77 IP_Port p_ip;
@@ -104,7 +104,7 @@ void print_friendlist(DHT * dht)
104 printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); 104 printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
105 printf("\nTimestamp: %llu", (long long unsigned int) dht->friends_list[k].client_list[i].timestamp); 105 printf("\nTimestamp: %llu", (long long unsigned int) dht->friends_list[k].client_list[i].timestamp);
106 printf("\nLast pinged: %llu\n", (long long unsigned int) dht->friends_list[k].client_list[i].last_pinged); 106 printf("\nLast pinged: %llu\n", (long long unsigned int) dht->friends_list[k].client_list[i].last_pinged);
107 p_ip =dht->friends_list[k].client_list[i].ret_ip_port; 107 p_ip = dht->friends_list[k].client_list[i].ret_ip_port;
108 printf("ret IP: %u.%u.%u.%u:%u\n", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); 108 printf("ret IP: %u.%u.%u.%u:%u\n", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
109 printf("Timestamp: %llu\n", (long long unsigned int)dht->friends_list[k].client_list[i].ret_timestamp); 109 printf("Timestamp: %llu\n", (long long unsigned int)dht->friends_list[k].client_list[i].ret_timestamp);
110 } 110 }
@@ -134,9 +134,10 @@ int main(int argc, char *argv[])
134 /* bind to ip 0.0.0.0:PORT */ 134 /* bind to ip 0.0.0.0:PORT */
135 IP ip; 135 IP ip;
136 ip.i = 0; 136 ip.i = 0;
137 137
138 DHT * dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); 138 DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
139 init_cryptopackets(dht); 139 init_cryptopackets(dht);
140
140 if (argc < 4) { 141 if (argc < 4) {
141 printf("usage %s ip port public_key\n", argv[0]); 142 printf("usage %s ip port public_key\n", argv[0]);
142 exit(0); 143 exit(0);
@@ -198,5 +199,6 @@ int main(int argc, char *argv[])
198 print_friendlist(dht); 199 print_friendlist(dht);
199 c_sleep(300); 200 c_sleep(300);
200 } 201 }
202
201 return 0; 203 return 0;
202} 204}
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c
index 0218de4f..09e9e0a3 100644
--- a/testing/Lossless_UDP_testclient.c
+++ b/testing/Lossless_UDP_testclient.c
@@ -124,25 +124,25 @@ void printconnection(int connection_id)
124/*run doLossless_UDP(); */ 124/*run doLossless_UDP(); */
125//void Lossless_UDP() 125//void Lossless_UDP()
126//{ 126//{
127 /* IP_Port ip_port; 127/* IP_Port ip_port;
128 uint8_t data[MAX_UDP_PACKET_SIZE]; 128 uint8_t data[MAX_UDP_PACKET_SIZE];
129 uint32_t length; 129 uint32_t length;
130 while (receivepacket(&ip_port, data, &length) != -1) { 130 while (receivepacket(&ip_port, data, &length) != -1) {
131 printf("packet with length: %u\n", length); */ 131 printf("packet with length: %u\n", length); */
132 /* if(rand() % 3 != 1)//add packet loss 132/* if(rand() % 3 != 1)//add packet loss
133 { */ 133 { */
134 /* 134/*
135 if (LosslessUDP_handlepacket(data, length, ip_port)) 135 if (LosslessUDP_handlepacket(data, length, ip_port))
136 printpacket(data, length, ip_port); 136 printpacket(data, length, ip_port);
137 else 137 else
138 printf("Received handled packet with length: %u\n", length); //printconnection(0); */ 138 printf("Received handled packet with length: %u\n", length); //printconnection(0); */
139 139
140 /* } */ 140/* } */
141 /* }*/ 141/* }*/
142 142
143 //networking_poll(); 143//networking_poll();
144 144
145 //doLossless_UDP(); 145//doLossless_UDP();
146 146
147//} 147//}
148 148
@@ -166,7 +166,7 @@ int main(int argc, char *argv[])
166 /* bind to ip 0.0.0.0:PORT */ 166 /* bind to ip 0.0.0.0:PORT */
167 IP ip; 167 IP ip;
168 ip.i = 0; 168 ip.i = 0;
169 Lossless_UDP * ludp = new_lossless_udp(new_networking(ip, PORT)); 169 Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
170 perror("Initialization"); 170 perror("Initialization");
171 IP_Port serverip; 171 IP_Port serverip;
172 serverip.ip.i = inet_addr(argv[1]); 172 serverip.ip.i = inet_addr(argv[1]);
@@ -203,6 +203,7 @@ int main(int argc, char *argv[])
203 /* printconnection(connection); */ 203 /* printconnection(connection); */
204 networking_poll(ludp->net); 204 networking_poll(ludp->net);
205 do_lossless_udp(ludp); 205 do_lossless_udp(ludp);
206
206 if (is_connected(ludp, connection) == 3) { 207 if (is_connected(ludp, connection) == 3) {
207 208
208 if (write_packet(ludp, connection, buffer, read)) { 209 if (write_packet(ludp, connection, buffer, read)) {
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c
index c3212888..a82b787a 100644
--- a/testing/Lossless_UDP_testserver.c
+++ b/testing/Lossless_UDP_testserver.c
@@ -124,20 +124,20 @@ void printconnection(int connection_id)
124// uint8_t data[MAX_UDP_PACKET_SIZE]; 124// uint8_t data[MAX_UDP_PACKET_SIZE];
125// uint32_t length; 125// uint32_t length;
126// while (receivepacket(&ip_port, data, &length) != -1) { 126// while (receivepacket(&ip_port, data, &length) != -1) {
127 //if(rand() % 3 != 1)//add packet loss 127//if(rand() % 3 != 1)//add packet loss
128 //{ 128//{
129// if (LosslessUDP_handlepacket(data, length, ip_port)) { 129// if (LosslessUDP_handlepacket(data, length, ip_port)) {
130// printpacket(data, length, ip_port); 130// printpacket(data, length, ip_port);
131// } else { 131// } else {
132 //printconnection(0); 132//printconnection(0);
133// printf("Received handled packet with length: %u\n", length); 133// printf("Received handled packet with length: %u\n", length);
134// } 134// }
135 //} 135//}
136// } 136// }
137 137
138 // networking_poll(); 138// networking_poll();
139 139
140 //doLossless_UDP(); 140//doLossless_UDP();
141//} 141//}
142 142
143 143
@@ -161,7 +161,7 @@ int main(int argc, char *argv[])
161 //bind to ip 0.0.0.0:PORT 161 //bind to ip 0.0.0.0:PORT
162 IP ip; 162 IP ip;
163 ip.i = 0; 163 ip.i = 0;
164 Lossless_UDP * ludp = new_lossless_udp(new_networking(ip, PORT)); 164 Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
165 perror("Initialization"); 165 perror("Initialization");
166 166
167 int connection; 167 int connection;
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index d3e68ec6..c7979843 100644
--- a/testing/toxic/chat.c
+++ b/testing/toxic/chat.c
@@ -123,7 +123,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
123 123
124 status[len - 1] = '\0'; 124 status[len - 1] = '\0';
125 fix_name(status); 125 fix_name(status);
126 126
127 wattron(ctx->history, COLOR_PAIR(3)); 127 wattron(ctx->history, COLOR_PAIR(3));
128 wprintw(ctx->history, "* Your partner changed status to '%s'\n", status); 128 wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
129 wattroff(ctx->history, COLOR_PAIR(3)); 129 wattroff(ctx->history, COLOR_PAIR(3));
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 6f6c9db4..e5525e94 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -93,7 +93,7 @@ static Messenger *init_tox()
93int init_connection(Messenger *m) 93int init_connection(Messenger *m)
94{ 94{
95 FILE *fp = NULL; 95 FILE *fp = NULL;
96 96
97 if (DHT_isconnected(m->dht)) 97 if (DHT_isconnected(m->dht))
98 return 0; 98 return 0;
99 99
@@ -297,7 +297,7 @@ int main(int argc, char *argv[])
297 strcpy(DATA_FILE, user_config_dir); 297 strcpy(DATA_FILE, user_config_dir);
298 strcat(DATA_FILE, CONFIGDIR); 298 strcat(DATA_FILE, CONFIGDIR);
299 strcat(DATA_FILE, "data"); 299 strcat(DATA_FILE, "data");
300 300
301 SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); 301 SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
302 strcpy(SRVLIST_FILE, user_config_dir); 302 strcpy(SRVLIST_FILE, user_config_dir);
303 strcat(SRVLIST_FILE, CONFIGDIR); 303 strcat(SRVLIST_FILE, CONFIGDIR);
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index 12f8a201..e194a90e 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -365,7 +365,9 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
365 cmd[i] = '\0'; 365 cmd[i] = '\0';
366 366
367 int j = i; 367 int j = i;
368
368 while (++j < MAX_STR_SIZE && isspace(cmd[j])); 369 while (++j < MAX_STR_SIZE && isspace(cmd[j]));
370
369 i = j - 1; 371 i = j - 1;
370 372
371 numargs++; 373 numargs++;
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c
index de924329..8fdf4e19 100644
--- a/testing/toxic/windows.c
+++ b/testing/toxic/windows.c
@@ -88,23 +88,25 @@ int add_window(Messenger *m, ToxWindow w)
88{ 88{
89 if (LINES < 2) 89 if (LINES < 2)
90 return -1; 90 return -1;
91 91
92 int i; 92 int i;
93 for(i = 0; i < MAX_WINDOWS_NUM; i++) { 93
94 if (windows[i].window) 94 for (i = 0; i < MAX_WINDOWS_NUM; i++) {
95 if (windows[i].window)
95 continue; 96 continue;
96 97
97 w.window = newwin(LINES - 2, COLS, 0, 0); 98 w.window = newwin(LINES - 2, COLS, 0, 0);
99
98 if (w.window == NULL) 100 if (w.window == NULL)
99 return -1; 101 return -1;
100 102
101 windows[i] = w; 103 windows[i] = w;
102 w.onInit(&w, m); 104 w.onInit(&w, m);
103 105
104 active_window = windows+i; 106 active_window = windows + i;
105 return i; 107 return i;
106 } 108 }
107 109
108 return -1; 110 return -1;
109} 111}
110 112
@@ -113,8 +115,10 @@ void del_window(ToxWindow *w)
113{ 115{
114 active_window = windows; // Go to prompt screen 116 active_window = windows; // Go to prompt screen
115 delwin(w->window); 117 delwin(w->window);
118
116 if (w->x) 119 if (w->x)
117 free(w->x); 120 free(w->x);
121
118 w->window = NULL; 122 w->window = NULL;
119 memset(w, 0, sizeof(ToxWindow)); 123 memset(w, 0, sizeof(ToxWindow));
120 clear(); 124 clear();
@@ -124,19 +128,19 @@ void del_window(ToxWindow *w)
124/* Shows next window when tab or back-tab is pressed */ 128/* Shows next window when tab or back-tab is pressed */
125void set_next_window(int ch) 129void set_next_window(int ch)
126{ 130{
127 ToxWindow *end = windows+MAX_WINDOWS_NUM-1; 131 ToxWindow *end = windows + MAX_WINDOWS_NUM - 1;
128 ToxWindow *inf = active_window; 132 ToxWindow *inf = active_window;
129 while(true) { 133
134 while (true) {
130 if (ch == '\t') { 135 if (ch == '\t') {
131 if (++active_window > end) 136 if (++active_window > end)
132 active_window = windows; 137 active_window = windows;
133 } else 138 } else if (--active_window < windows)
134 if (--active_window < windows) 139 active_window = end;
135 active_window = end; 140
136
137 if (active_window->window) 141 if (active_window->window)
138 return; 142 return;
139 143
140 if (active_window == inf) { // infinite loop check 144 if (active_window == inf) { // infinite loop check
141 endwin(); 145 endwin();
142 exit(2); 146 exit(2);
@@ -148,14 +152,14 @@ void set_active_window(int index)
148{ 152{
149 if (index < 0 || index >= MAX_WINDOWS_NUM) 153 if (index < 0 || index >= MAX_WINDOWS_NUM)
150 return; 154 return;
151 155
152 active_window = windows+index; 156 active_window = windows + index;
153} 157}
154 158
155ToxWindow *init_windows() 159ToxWindow *init_windows()
156{ 160{
157 int n_prompt = add_window(m, new_prompt()); 161 int n_prompt = add_window(m, new_prompt());
158 162
159 if (n_prompt == -1 163 if (n_prompt == -1
160 || add_window(m, new_friendlist()) == -1 164 || add_window(m, new_friendlist()) == -1
161 || add_window(m, new_dhtstatus()) == -1) { 165 || add_window(m, new_dhtstatus()) == -1) {
@@ -166,7 +170,7 @@ ToxWindow *init_windows()
166 170
167 prompt = &windows[n_prompt]; 171 prompt = &windows[n_prompt];
168 active_window = prompt; 172 active_window = prompt;
169 173
170 return prompt; 174 return prompt;
171} 175}
172 176
@@ -189,7 +193,7 @@ static void draw_bar()
189 193
190 for (i = 0; i < (MAX_WINDOWS_NUM); ++i) { 194 for (i = 0; i < (MAX_WINDOWS_NUM); ++i) {
191 if (windows[i].window) { 195 if (windows[i].window) {
192 if (windows+i == active_window) 196 if (windows + i == active_window)
193 attron(A_BOLD); 197 attron(A_BOLD);
194 198
195 odd = (odd + 1) % blinkrate; 199 odd = (odd + 1) % blinkrate;
@@ -197,13 +201,13 @@ static void draw_bar()
197 if (windows[i].blink && (odd < (blinkrate / 2))) 201 if (windows[i].blink && (odd < (blinkrate / 2)))
198 attron(COLOR_PAIR(3)); 202 attron(COLOR_PAIR(3));
199 203
200 clrtoeol(); 204 clrtoeol();
201 printw(" %s", windows[i].title); 205 printw(" %s", windows[i].title);
202 206
203 if (windows[i].blink && (odd < (blinkrate / 2))) 207 if (windows[i].blink && (odd < (blinkrate / 2)))
204 attroff(COLOR_PAIR(3)); 208 attroff(COLOR_PAIR(3));
205 209
206 if (windows+i == active_window) { 210 if (windows + i == active_window) {
207 attroff(A_BOLD); 211 attroff(A_BOLD);
208 } 212 }
209 } 213 }