From f377eb9ca26fc1d476938b441bb256837bbee4f5 Mon Sep 17 00:00:00 2001 From: SilentSand Date: Thu, 25 Jul 2013 21:45:56 -0400 Subject: Licensing Added the GPLv3 license to some files in addition to fixing some comments at the beginning of the files. --- core/Messenger.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'core/Messenger.c') diff --git a/core/Messenger.c b/core/Messenger.c index 042d5325..11613cbb 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -1,26 +1,25 @@ /* Messenger.c -* -* An implementation of a simple text chat only messenger on the tox network core. -* - - Copyright (C) 2013 Tox project All Rights Reserved. - - This file is part of Tox. - - Tox is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Tox is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Tox. If not, see . - -*/ + * + * An implementation of a simple text chat only messenger on the tox network core. + * + * Copyright (C) 2013 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ #include "Messenger.h" #define MIN(a,b) (((a)<(b))?(a):(b)) -- cgit v1.2.3 From 59b34e423beb75fcd3e3c8abc2c05fe60aca26bc Mon Sep 17 00:00:00 2001 From: SilentSand Date: Fri, 26 Jul 2013 04:02:17 -0400 Subject: Formatting. Many stylistic changes, mostly formatting code more closely to the coding style. --- core/DHT.c | 559 +++++++++++++------------------------- core/DHT.h | 11 - core/Lossless_UDP.c | 17 +- core/Lossless_UDP.h | 9 - core/Messenger.c | 14 +- core/Messenger.h | 8 +- core/friend_requests.c | 3 - core/friend_requests.h | 5 - core/net_crypto.c | 12 +- core/net_crypto.h | 6 - core/network.c | 3 - core/network.h | 4 - testing/DHT_cryptosendfiletest.c | 119 +++----- testing/DHT_sendfiletest.c | 60 ++-- testing/DHT_test.c | 53 ++-- testing/Lossless_UDP_testclient.c | 72 ++--- testing/Lossless_UDP_testserver.c | 48 ++-- testing/Messenger_test.c | 19 +- testing/nTox.c | 13 +- testing/nTox.h | 1 + 20 files changed, 335 insertions(+), 701 deletions(-) (limited to 'core/Messenger.c') diff --git a/core/DHT.c b/core/DHT.c index bf506d0b..3fca85e6 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -23,7 +23,6 @@ #include "DHT.h" - typedef struct { uint8_t client_id[CLIENT_ID_SIZE]; @@ -34,8 +33,10 @@ typedef struct (for nodes in friends_list) or us (for nodes in close_clientlist) */ uint32_t ret_timestamp; }Client_data; + /* maximum number of clients stored per friend. */ #define MAX_FRIEND_CLIENTS 8 + typedef struct { uint8_t client_id[CLIENT_ID_SIZE]; @@ -50,7 +51,6 @@ typedef struct uint32_t NATping_timestamp; }Friend; - typedef struct { uint8_t client_id[CLIENT_ID_SIZE]; @@ -74,8 +74,6 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; #define LCLIENT_LIST 32 static Client_data close_clientlist[LCLIENT_LIST]; - - static Friend * friends_list; static uint16_t num_friends; @@ -88,7 +86,6 @@ static Pinged pings[LPING_ARRAY]; static Pinged send_nodes[LSEND_NODES_ARRAY]; - /* Compares client_id1 and client_id2 with client_id return 0 if both are same distance return 1 if client_id1 is closer @@ -96,17 +93,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY]; int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ { uint32_t i; - for(i = 0; i < CLIENT_ID_SIZE; ++i) - { - if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) - { + for(i = 0; i < CLIENT_ID_SIZE; ++i) { + if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) { return 1; - } - else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) - { + } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) { return 2; } - } return 0; @@ -123,17 +115,14 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ uint32_t i; uint32_t temp_time = unix_time(); - for(i = 0; i < length; ++i) - { + for(i = 0; i < length; ++i) { /*If ip_port is assigned to a different client_id replace it*/ if(list[i].ip_port.ip.i == ip_port.ip.i && - list[i].ip_port.port == ip_port.port) - { + list[i].ip_port.port == ip_port.port) { memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); } - if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) - { + if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { /* Refresh the client timestamp. */ list[i].timestamp = temp_time; list[i].ip_port.ip.i = ip_port.ip.i; @@ -150,10 +139,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) { uint32_t i; - for(i = 0; i < length; ++i) - { - if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) - { + for(i = 0; i < length; ++i) { + if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { return 1; } @@ -167,8 +154,7 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) static int friend_number(uint8_t * client_id) { uint32_t i; - for(i = 0; i < num_friends; ++i) - { + for(i = 0; i < num_friends; ++i) { if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ { return i; @@ -177,13 +163,11 @@ static int friend_number(uint8_t * client_id) return -1; } - /* the number of seconds for a non responsive node to become bad. */ #define BAD_NODE_TIMEOUT 70 /* the max number of nodes to send with send nodes. */ #define MAX_SENT_NODES 8 - /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: put them in the nodes_list and return how many were found. TODO: Make this function much more efficient. */ @@ -192,48 +176,35 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) uint32_t i, j, k; int num_nodes=0; uint32_t temp_time = unix_time(); - for(i = 0; i < LCLIENT_LIST; ++i) - { + for(i = 0; i < LCLIENT_LIST; ++i) { if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && - !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) + !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { /* if node is good and not already in list. */ - { - if(num_nodes < MAX_SENT_NODES) - { + if(num_nodes < MAX_SENT_NODES) { memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; num_nodes++; } - else for(j = 0; j < MAX_SENT_NODES; ++j) - { - if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) - { + else for(j = 0; j < MAX_SENT_NODES; ++j) { + if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) { memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); nodes_list[j].ip_port = close_clientlist[i].ip_port; break; } } - } - + } } - for(i = 0; i < num_friends; ++i) - { - for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) - { + for(i = 0; i < num_friends; ++i) { + for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && - !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) + !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { /* if node is good and not already in list. */ - { - if(num_nodes < MAX_SENT_NODES) - { + if(num_nodes < MAX_SENT_NODES) { memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; num_nodes++; - } - else for(k = 0; k < MAX_SENT_NODES; ++k) - { - if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) - { + } else for(k = 0; k < MAX_SENT_NODES; ++k) { + if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) { memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; break; @@ -243,12 +214,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) } } - return num_nodes; - + return num_nodes; } - - /* replace first bad (or empty) node with this one return 0 if successful return 1 if not (list contains no bad nodes) */ @@ -256,10 +224,8 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por { uint32_t i; uint32_t temp_time = unix_time(); - for(i = 0; i < length; ++i) - { - if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ - { + for(i = 0; i < length; ++i) { + if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); list[i].ip_port = ip_port; list[i].timestamp = temp_time; @@ -279,10 +245,8 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po uint32_t i; uint32_t temp_time = unix_time(); - for(i = 0; i < length; ++i) - { - if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) - { + for(i = 0; i < length; ++i) { + if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); list[i].ip_port = ip_port; list[i].timestamp = temp_time; @@ -302,23 +266,16 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) uint32_t i; /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ - if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) - { + if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { - if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) - { + if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { /* if we can't replace bad nodes we try replacing good ones */ replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); - } - + } } - for(i = 0; i < num_friends; ++i) - { - if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) - { - - if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) - { + for(i = 0; i < num_friends; ++i) { + if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { + if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { /* if we can't replace bad nodes we try replacing good ones. */ replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); } @@ -332,12 +289,9 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient { uint32_t i, j; uint32_t temp_time = unix_time(); - if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) - { - for(i = 0; i < LCLIENT_LIST; ++i) - { - if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) - { + if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { + for(i = 0; i < LCLIENT_LIST; ++i) { + if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { close_clientlist[i].ret_ip_port = ip_port; close_clientlist[i].ret_timestamp = temp_time; return; @@ -345,14 +299,10 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient } } else - for(i = 0; i < num_friends; ++i) - { - if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) - { - for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) - { - if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) - { + for(i = 0; i < num_friends; ++i) { + if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { + for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { + if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { friends_list[i].client_list[j].ret_ip_port = ip_port; friends_list[i].client_list[j].ret_timestamp = temp_time; return; @@ -364,6 +314,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient /* ping timeout in seconds */ #define PING_TIMEOUT 5 + /* check if we are currently pinging an ip_port and/or a ping_id variables with values of zero will not be checked. if we are already, return 1 @@ -375,28 +326,22 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) uint8_t pinging; uint32_t temp_time = unix_time(); - for(i = 0; i < LPING_ARRAY; ++i ) - { - if((pings[i].timestamp + PING_TIMEOUT) > temp_time) - { + for(i = 0; i < LPING_ARRAY; ++i ) { + if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { pinging = 0; - if(ip_port.ip.i != 0) - { + if(ip_port.ip.i != 0) { if(pings[i].ip_port.ip.i == ip_port.ip.i && - pings[i].ip_port.port == ip_port.port) - { + pings[i].ip_port.port == ip_port.port) { ++pinging; } } - if(ping_id != 0) - { + if(ping_id != 0) { if(pings[i].ping_id == ping_id) { ++pinging; } } - if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) - { + if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { return 1; } @@ -407,7 +352,6 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) } - /* Same as last function but for get_node requests. */ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) { @@ -415,28 +359,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) uint8_t pinging; uint32_t temp_time = unix_time(); - for(i = 0; i < LSEND_NODES_ARRAY; ++i ) - { - if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) - { + for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { + if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { pinging = 0; - if(ip_port.ip.i != 0) - { + if(ip_port.ip.i != 0) { if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && - send_nodes[i].ip_port.port == ip_port.port) - { + send_nodes[i].ip_port.port == ip_port.port) { ++pinging; } } - if(ping_id != 0) - { - if(send_nodes[i].ping_id == ping_id) - { + if(ping_id != 0) { + if(send_nodes[i].ping_id == ping_id) { ++pinging; } } - if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) - { + if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { return 1; } @@ -447,7 +384,6 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) } - /* Add a new ping request to the list of ping requests returns the ping_id to put in the ping request returns 0 if problem. @@ -458,12 +394,9 @@ uint64_t add_pinging(IP_Port ip_port) uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); uint32_t temp_time = unix_time(); - for(i = 0; i < PING_TIMEOUT; ++i ) - { - for(j = 0; j < LPING_ARRAY; ++j ) - { - if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) - { + for(i = 0; i < PING_TIMEOUT; ++i ) { + for(j = 0; j < LPING_ARRAY; ++j ) { + if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { pings[j].timestamp = temp_time; pings[j].ip_port = ip_port; pings[j].ping_id = ping_id; @@ -482,12 +415,9 @@ uint64_t add_gettingnodes(IP_Port ip_port) uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); uint32_t temp_time = unix_time(); - for(i = 0; i < PING_TIMEOUT; ++i ) - { - for(j = 0; j < LSEND_NODES_ARRAY; ++j ) - { - if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) - { + for(i = 0; i < PING_TIMEOUT; ++i ) { + for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { + if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { send_nodes[j].timestamp = temp_time; send_nodes[j].ip_port = ip_port; send_nodes[j].ping_id = ping_id; @@ -499,25 +429,20 @@ uint64_t add_gettingnodes(IP_Port ip_port) } - - /* send a ping request Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ static int pingreq(IP_Port ip_port, uint8_t * public_key) { - if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ - { + if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { return 1; } - if(is_pinging(ip_port, 0)) - { + if(is_pinging(ip_port, 0)) { return 1; } uint64_t ping_id = add_pinging(ip_port); - if(ping_id == 0) - { + if(ping_id == 0) { return 1; } @@ -527,8 +452,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) random_nonce(nonce); int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); - if(len != sizeof(ping_id) + ENCRYPTION_PADDING) - { + if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { return -1; } data[0] = 0; @@ -540,12 +464,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) } - /* send a ping response */ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) { - if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ - { + /* check if packet is gonna be sent to ourself */ + if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { return 1; } @@ -555,8 +478,7 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) random_nonce(nonce); int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); - if(len != sizeof(ping_id) + ENCRYPTION_PADDING) - { + if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { return -1; } data[0] = 1; @@ -571,20 +493,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) /* send a getnodes request */ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) { - if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ - { + /* check if packet is gonna be sent to ourself */ + if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { return 1; } - if(is_gettingnodes(ip_port, 0)) - { + if(is_gettingnodes(ip_port, 0)) { return 1; } uint64_t ping_id = add_gettingnodes(ip_port); - if(ping_id == 0) - { + if(ping_id == 0) { return 1; } @@ -599,8 +519,7 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); - if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) - { + if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { return -1; } data[0] = 2; @@ -611,12 +530,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) } - /* send a send nodes response */ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) { - if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ - { + if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { return 1; } @@ -626,8 +543,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, Node_format nodes_list[MAX_SENT_NODES]; int num_nodes = get_close_nodes(client_id, nodes_list); - if(num_nodes == 0) - { + if(num_nodes == 0) { return 0; } @@ -642,8 +558,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); - if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) - { + if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) { return -1; } @@ -656,20 +571,17 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, } - - /* Packet handling functions One to handle each types of packets we receive return 0 if handled correctly, 1 if packet is bad. */ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) { uint64_t ping_id; - if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) - { + if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { return 1; } - if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ - { + /* check if packet is from ourself. */ + if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { return 1; } @@ -678,8 +590,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); - if(len != sizeof(ping_id)) - { + if(len != sizeof(ping_id)) { return 1; } @@ -695,12 +606,10 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) { uint64_t ping_id; - if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) - { + if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { return 1; } - if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ - { + if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ { return 1; } @@ -709,13 +618,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); - if(len != sizeof(ping_id)) - { + if(len != sizeof(ping_id)) { return 1; } - if(is_pinging(source, ping_id)) - { + if(is_pinging(source, ping_id)) { addto_lists(source, packet + 1); return 0; } @@ -726,12 +633,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) { uint64_t ping_id; - if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) - { + if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { return 1; } - if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ - { + /* check if packet is from ourself. */ + if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { return 1; } @@ -741,8 +647,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); - if(len != sizeof(ping_id) + CLIENT_ID_SIZE) - { + if(len != sizeof(ping_id) + CLIENT_ID_SIZE) { return 1; } @@ -764,8 +669,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) - + sizeof(Node_format) + ENCRYPTION_PADDING) - { + + sizeof(Node_format) + ENCRYPTION_PADDING) { return 1; } uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES @@ -778,14 +682,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); - if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) - { + if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) { return 1; } memcpy(&ping_id, plain, sizeof(ping_id)); - if(!is_gettingnodes(source, ping_id)) - { + if(!is_gettingnodes(source, ping_id)) { return 1; } @@ -795,8 +697,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) addto_lists(source, packet + 1); uint32_t i; - for(i = 0; i < num_nodes; ++i) - { + for(i = 0; i < num_nodes; ++i) { pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); } @@ -809,16 +710,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) int DHT_addfriend(uint8_t * client_id) { Friend * temp; - if(num_friends == 0) - { + if(num_friends == 0) { temp = malloc(sizeof(Friend)); - } - else - { + } else { temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); } - if(temp == NULL) - { + if(temp == NULL) { return 1; } @@ -830,24 +727,19 @@ int DHT_addfriend(uint8_t * client_id) return 0; } - - int DHT_delfriend(uint8_t * client_id) { uint32_t i; Friend * temp; - for(i = 0; i < num_friends; ++i) - { - if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ - { + for(i = 0; i < num_friends; ++i) { + /* Equal */ + if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ --num_friends; - if(num_friends != i) - { + if(num_friends != i) { memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); } temp = realloc(friends_list, sizeof(Friend) * (num_friends)); - if(temp != NULL) - { + if(temp != NULL) { friends_list = temp; } return 0; @@ -856,38 +748,28 @@ int DHT_delfriend(uint8_t * client_id) return 1; } - - - /* TODO: Optimize this. */ IP_Port DHT_getfriendip(uint8_t * client_id) { uint32_t i, j; IP_Port empty = {{{0}}, 0}; uint32_t temp_time = unix_time(); - for(i = 0; i < num_friends; ++i) - { - if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ - { - for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) - { + for(i = 0; i < num_friends; ++i) { + /* Equal */ + if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)1 { + for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && - friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) - { + friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) { return friends_list[i].client_list[j].ip_port; - } - - } - + } + } return empty; } } empty.ip.i = 1; return empty; -} - - +} /* The timeout after which a node is discarded completely. */ #define Kill_NODE_TIMEOUT 300 @@ -900,8 +782,6 @@ IP_Port DHT_getfriendip(uint8_t * client_id) /* Ping each client in the "friends" list every 60 seconds. Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ - - void doDHTFriends() { uint32_t i, j; @@ -909,27 +789,21 @@ void doDHTFriends() uint32_t rand_node; uint32_t index[MAX_FRIEND_CLIENTS]; - for(i = 0; i < num_friends; ++i) - { + for(i = 0; i < num_friends; ++i) { uint32_t num_nodes = 0; - for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) - { - if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ - { - if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) - { + for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { + if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { + if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); friends_list[i].client_list[j].last_pinged = temp_time; } - if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ - { + if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ { index[num_nodes] = j; ++num_nodes; } } } - if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) - { + if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { rand_node = rand() % num_nodes; getnodes(friends_list[i].client_list[index[rand_node]].ip_port, friends_list[i].client_list[index[rand_node]].client_id, @@ -951,62 +825,49 @@ void doClose() /* tested */ uint32_t rand_node; uint32_t index[LCLIENT_LIST]; - for(i = 0; i < LCLIENT_LIST; ++i) - { - if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ - { + for(i = 0; i < LCLIENT_LIST; ++i) { + /* if node is not dead. */ + if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); close_clientlist[i].last_pinged = temp_time; } - if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ - { + /* if node is good. */ + if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { index[num_nodes] = i; ++num_nodes; } } } - if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) - { + if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { rand_node = rand() % num_nodes; getnodes(close_clientlist[index[rand_node]].ip_port, close_clientlist[index[rand_node]].client_id, self_public_key); close_lastgetnodes = temp_time; } - } - - - - - void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) { getnodes(ip_port, public_key, self_public_key); } - - /* send the given packet to node with client_id returns -1 if failure */ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) { uint32_t i; - for(i = 0; i < LCLIENT_LIST; ++i) - { - if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) - { + for(i = 0; i < LCLIENT_LIST; ++i) { + if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { return sendpacket(close_clientlist[i].ip_port, packet, length); } } return -1; } - /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist ip_portlist must be at least MAX_FRIEND_CLIENTS big returns the number of ips returned @@ -1017,18 +878,15 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) int num_ips = 0; uint32_t i; uint32_t temp_time = unix_time(); - if(friend_num >= num_friends) - { + if(friend_num >= num_friends) { return -1; } for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) { + /*If ip is not zero and node is good */ if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && - friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) - /*If ip is not zero and node is good */ - { - if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) - { + friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { + if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) { return 0; } ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; @@ -1038,7 +896,6 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) return num_ips; } - /* Send the following packet to everyone who tells us they are connected to friend_id returns the number of nodes it sent the packet to */ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) @@ -1046,18 +903,14 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) uint32_t i, j; uint32_t sent = 0; uint32_t temp_time = unix_time(); - for(i = 0; i < num_friends; ++i) - { - if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ - { - for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) - { + for(i = 0; i < num_friends; ++i) { + /* Equal */ + if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { + for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { + /*If ip is not zero and node is good */ if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && - friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) - /*If ip is not zero and node is good */ - { - if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) - { + friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { + if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) { ++sent; } } @@ -1073,8 +926,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) { int num = friend_number(friend_id); - if(num == -1) - { + if(num == -1) { return 0; } @@ -1082,22 +934,18 @@ int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) int n = 0; uint32_t i; uint32_t temp_time = unix_time(); - for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) - { + for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) { + /*If ip is not zero and node is good */ if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && - friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) - /*If ip is not zero and node is good */ - { + friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { ip_list[n] = friends_list[num].client_list[i].ip_port; ++n; } } - if(n < 1) - { + if(n < 1) { return 0; } - if(sendpacket(ip_list[rand() % n], packet, length) == length) - { + if(sendpacket(ip_list[rand() % n], packet, length) == length) { return 1; } return 0; @@ -1112,10 +960,9 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) { uint32_t i; - for(i = 0; i < num_friends; ++i) - { - if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ - { + for(i = 0; i < num_friends; ++i) { + /* Equal */ + if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { return friend_iplist(ip_portlist, i); } } @@ -1132,60 +979,49 @@ int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) uint8_t packet[MAX_DATA_SIZE]; int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ - if(len == -1) - { + if(len == -1) { return -1; } int num = 0; - if(type == 0) - { + if(type == 0) { num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ } - else if(type == 1) - { + else if(type == 1) { num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ } - if(num == 0) - { + if(num == 0) { return -1; } return num; } - /* Handle a recieved ping request for */ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) { if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && - length > MAX_DATA_SIZE + ENCRYPTION_PADDING) - { + length > MAX_DATA_SIZE + ENCRYPTION_PADDING) { return 1; } - if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us. - { + /* check if request is for us. */ + if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t data[MAX_DATA_SIZE]; int len = handle_request(public_key, data, packet, length); - if(len != sizeof(uint64_t) + 1) - { + if(len != sizeof(uint64_t) + 1) { return 1; } uint64_t ping_id; memcpy(&ping_id, data + 1, sizeof(uint64_t)); int friendnumber = friend_number(public_key); - if(friendnumber == -1) - { + if(friendnumber == -1) { return 1; } - if(data[0] == 0) - { + if(data[0] == 0) { send_NATping(public_key, ping_id, 1);/*1 is reply*/ return 0; - } - else if (data[0] == 1) - { + } else if (data[0] == 1) { if(friends_list[friendnumber].NATping_id == ping_id) { friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); @@ -1195,10 +1031,9 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) } return 1; } - else//if request is not for us, try routing it. - { - if(route_packet(packet + 1, packet, length) == length) - { + /* if request is not for us, try routing it. */ + else { + if(route_packet(packet + 1, packet, length) == length) { return 0; } } @@ -1212,24 +1047,19 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) { IP zero = {{0}}; - if(len > MAX_FRIEND_CLIENTS) - { + if(len > MAX_FRIEND_CLIENTS) { return zero; } uint32_t i, j; uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; - for(i = 0; i < len; ++i) - { - for(j = 0; j < len; ++j) - { - if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) - { + for(i = 0; i < len; ++i) { + for(j = 0; j < len; ++j) { + if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) { ++numbers[i]; } } - if(numbers[i] >= min_num) - { + if(numbers[i] >= min_num) { return ip_portlist[i].ip; } } @@ -1244,10 +1074,8 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ { uint32_t i; uint16_t num = 0; - for(i = 0; i < len; ++i) - { - if(ip_portlist[i].ip.i == ip.i) - { + for(i = 0; i < len; ++i) { + if(ip_portlist[i].ip.i == ip.i) { portlist[num] = ntohs(ip_portlist[i].port); ++num; } @@ -1259,14 +1087,12 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) { - if(numports > MAX_FRIEND_CLIENTS || numports == 0) - { + if(numports > MAX_FRIEND_CLIENTS || numports == 0) { return; } uint32_t i; uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; - for(i = friends_list[friend_num].punching_index; i != top; i++) - { + for(i = friends_list[friend_num].punching_index; i != top; i++) { /*TODO: improve port guessing algorithm*/ uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); IP_Port pinging = {ip, htons(port)}; @@ -1282,28 +1108,22 @@ static void doNAT() { uint32_t i; uint32_t temp_time = unix_time(); - for(i = 0; i < num_friends; ++i) - { + for(i = 0; i < num_friends; ++i) { IP_Port ip_list[MAX_FRIEND_CLIENTS]; int num = friend_iplist(ip_list, i); /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ - if(num < MAX_FRIEND_CLIENTS/2) - { + if(num < MAX_FRIEND_CLIENTS/2) { continue; } - if(friends_list[i].hole_punching != 1) - { - if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) - { + if(friends_list[i].hole_punching != 1) { + if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ friends_list[i].NATping_timestamp = temp_time; } } - else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) - { + else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) { IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); - if(ip.i == 0) - { + if(ip.i == 0) { continue; } uint16_t port_list[MAX_FRIEND_CLIENTS]; @@ -1318,7 +1138,6 @@ static void doNAT() /*END OF NAT PUNCHING FUNCTIONS*/ - int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) { switch (packet[0]) { @@ -1346,7 +1165,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) } - void doDHT() { doClose(); @@ -1354,8 +1172,6 @@ void doDHT() doNAT(); } - - /* get the size of the DHT (for saving) */ uint32_t DHT_size() { @@ -1374,12 +1190,10 @@ void DHT_save(uint8_t * data) return 0 if success */ int DHT_load(uint8_t * data, uint32_t size) { - if(size < sizeof(close_clientlist)) - { + if(size < sizeof(close_clientlist)) { return -1; } - if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) - { + if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) { return -1; } uint32_t i, j; @@ -1388,17 +1202,13 @@ int DHT_load(uint8_t * data, uint32_t size) temp = (size - sizeof(close_clientlist))/sizeof(Friend); - if(temp != 0) - { + if(temp != 0) { Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); - for(i = 0; i < temp; ++i) - { + for(i = 0; i < temp; ++i) { DHT_addfriend(tempfriends_list[i].client_id); - for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) - { - if(tempfriends_list[i].client_list[j].timestamp != 0) - { + for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { + if(tempfriends_list[i].client_list[j].timestamp != 0) { getnodes(tempfriends_list[i].client_list[j].ip_port, tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); } @@ -1407,10 +1217,8 @@ int DHT_load(uint8_t * data, uint32_t size) } Client_data * tempclose_clientlist = (Client_data *)data; - for(i = 0; i < LCLIENT_LIST; ++i) - { - if(tempclose_clientlist[i].timestamp != 0) - { + for(i = 0; i < LCLIENT_LIST; ++i) { + if(tempclose_clientlist[i].timestamp != 0) { DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); } } @@ -1423,13 +1231,10 @@ int DHT_isconnected() { uint32_t i; uint32_t temp_time = unix_time(); - for(i = 0; i < LCLIENT_LIST; ++i) - { - if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) - { + for(i = 0; i < LCLIENT_LIST; ++i) { + if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { return 1; } } return 0; -} - +} \ No newline at end of file diff --git a/core/DHT.h b/core/DHT.h index 849bb81f..966645f5 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -21,7 +21,6 @@ * */ - #ifndef DHT_H #define DHT_H @@ -37,8 +36,6 @@ extern "C" { /* size of the client_id in bytes */ #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES - - /* Add a new friend to the friends list client_id must be CLIENT_ID_SIZE bytes long. returns 0 if success @@ -51,7 +48,6 @@ int DHT_addfriend(uint8_t * client_id); returns 1 if failure (client_id not in friends list) */ int DHT_delfriend(uint8_t * client_id); - /* Get ip of friend client_id must be CLIENT_ID_SIZE bytes long. ip must be 4 bytes long. @@ -61,7 +57,6 @@ int DHT_delfriend(uint8_t * client_id); returns ip of 1 if friend is not in list. */ IP_Port DHT_getfriendip(uint8_t * client_id); - /* Run this function at least a couple times per second (It's the main loop) */ void doDHT(); @@ -74,8 +69,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); Sends a get nodes request to the given node with ip port and public_key */ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); - - /* ROUTING FUNCTIONS */ /* send the given packet to node with client_id @@ -86,8 +79,6 @@ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length); returns the number of nodes it sent the packet to */ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); - - /* NAT PUNCHING FUNCTIONS */ /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist @@ -96,8 +87,6 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); returns -1 if no such friend*/ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); - - /* SAVE/LOAD functions */ /* get the size of the DHT (for saving) */ diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 47d63a71..83bb1785 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -25,8 +25,6 @@ There are a couple of useless variables to get rid of. */ #include "Lossless_UDP.h" - - /* maximum data packets in sent and receive queues. */ #define MAX_QUEUE_NUM 16 @@ -88,7 +86,6 @@ typedef struct uint8_t timeout; /* connection timeout in seconds. */ }Connection; - #define MAX_CONNECTIONS 256 static Connection connections[MAX_CONNECTIONS]; @@ -117,7 +114,6 @@ int getconnection_id(IP_Port ip_port) /* table of random numbers used below. */ static uint32_t randtable[6][256]; - /* generate a handshake_id which depends on the ip_port. this function will always give one unique handshake_id per ip_port. TODO: make this better */ @@ -138,6 +134,7 @@ uint32_t handshake_id(IP_Port source) } return id; } + /* change the hnshake id associated with that ip_port TODO: make this better */ void change_handshake(IP_Port source) @@ -146,7 +143,6 @@ void change_handshake(IP_Port source) randtable[rand][((uint8_t *)&source)[rand]] = random_int(); } - /* initialize a new connection to ip_port returns an integer corresponding to the connection id. return -1 if it could not initialize the connection. @@ -314,6 +310,7 @@ char id_packet(int connection_id) } return -1; } + /* return 0 if there is no received data in the buffer. return length of received packet if successful */ int read_packet(int connection_id, uint8_t * data) @@ -353,9 +350,6 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length) return 0; } - - - /* put the packet numbers the we are missing in requested and return the number */ uint32_t missing_packets(int connection_id, uint32_t * requested) { @@ -386,8 +380,6 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) /* Packet sending functions One per packet type. see docs/Lossless_UDP.txt for more information. */ - - int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) { uint8_t packet[1 + 4 + 4]; @@ -402,7 +394,6 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i } - int send_SYNC(uint32_t connection_id) { @@ -468,8 +459,6 @@ int send_DATA(uint32_t connection_id) /* END of packet sending functions */ - - /* Packet handling functions One to handle each type of packets we receive return 0 if handled correctly, 1 if packet is bad. */ @@ -700,7 +689,6 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) /* END of packet handling functions */ - int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) { @@ -828,6 +816,7 @@ void adjustRates() } } } + /* Call this function a couple times per second It's the main loop. */ void doLossless_UDP() diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index 4b156301..a9f1bb15 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h @@ -33,8 +33,6 @@ extern "C" { /* maximum length of the data in the data packets */ #define MAX_DATA_SIZE 1024 - - /* Functions */ /* initialize a new connection to ip_port @@ -52,7 +50,6 @@ int getconnection_id(IP_Port ip_port); return -1 if there are no new incoming connections in the list. */ int incoming_connection(); - /* return -1 if it could not kill the connection. return 0 if killed successfully */ int kill_connection(int connection_id); @@ -74,21 +71,16 @@ char id_packet(int connection_id); return length of received packet if successful */ int read_packet(int connection_id, uint8_t * data); - /* return 0 if data could not be put in packet queue return 1 if data was put into the queue */ int write_packet(int connection_id, uint8_t * data, uint32_t length); - - /* returns the number of packets in the queue waiting to be successfully sent. */ uint32_t sendqueue(int connection_id); - /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ uint32_t recvqueue(int connection_id); - /* check if connection is connected return 0 no. return 1 if attempting handshake @@ -97,7 +89,6 @@ uint32_t recvqueue(int connection_id); return 4 if timed out and wating to be killed */ int is_connected(int connection_id); - /* Call this function a couple times per second It's the main loop. */ void doLossless_UDP(); diff --git a/core/Messenger.c b/core/Messenger.c index 11613cbb..d58e895c 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -38,8 +38,6 @@ typedef struct uint8_t userstatus_sent; uint16_t info_size; /* length of the info */ }Friend; - - uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; @@ -57,7 +55,6 @@ static uint32_t numfriends; 0 if we are offline static uint8_t online; */ - /* return the friend id associated to that public key. return -1 if no such friend */ int getfriend_id(uint8_t * client_id) @@ -76,7 +73,6 @@ int getfriend_id(uint8_t * client_id) return -1; } - /* copies the public key associated to that friend id into client_id buffer. make sure that client_id is of size CLIENT_ID_SIZE. return 0 if success @@ -96,7 +92,6 @@ int getclient_id(int friend_id, uint8_t * client_id) return -1; } - /* add a friend set the data that will be sent along with friend request client_id is the client id of the friend @@ -191,7 +186,6 @@ int m_delfriend(int friendnumber) return 0; } - /* return 4 if friend is online return 3 if friend is confirmed return 2 if the friend request was sent @@ -206,7 +200,6 @@ int m_friendstatus(int friendnumber) return friendlist[friendnumber].status; } - /* send a text chat message to an online friend return 1 if packet was successfully put into the send queue return 0 if it was not */ @@ -239,7 +232,6 @@ static int m_sendname(int friendnumber, uint8_t * name) /* set the name of a friend return 0 if success return -1 if failure */ - static int setfriendname(int friendnumber, uint8_t * name) { if(friendnumber >= numfriends || friendnumber < 0) @@ -250,7 +242,6 @@ static int setfriendname(int friendnumber, uint8_t * name) return 0; } - /* Set our nickname name must be a string of maximum MAX_NAME_LENGTH length. return 0 if success @@ -352,6 +343,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le friendlist[friendnumber].userstatus_length = length; return 0; } + /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); static uint8_t friend_request_isset = 0; @@ -362,7 +354,6 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) callback_friendrequest(function); } - static void (*friend_message)(int, uint8_t *, uint16_t); static uint8_t friend_message_isset = 0; @@ -373,7 +364,6 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) friend_message_isset = 1; } - static void (*friend_namechange)(int, uint8_t *, uint16_t); static uint8_t friend_namechange_isset = 0; void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) @@ -520,8 +510,6 @@ static void doFriends() } } - - static void doInbound() { uint8_t secret_nonce[crypto_box_NONCEBYTES]; diff --git a/core/Messenger.h b/core/Messenger.h index f699c022..c0432e68 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -22,8 +22,7 @@ * along with Tox. If not, see . * */ - - + #ifndef MESSENGER_H #define MESSENGER_H @@ -79,7 +78,6 @@ int m_delfriend(int friendnumber); return 0 if there is no friend with that number */ int m_friendstatus(int friendnumber); - /* send a text chat message to an online friend returns 1 if packet was successfully put into the send queue return 0 if it was not */ @@ -91,7 +89,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); return -1 if failure */ int setname(uint8_t * name, uint16_t length); - /* get name of friendnumber put it in name name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. @@ -117,7 +114,6 @@ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); - /* set the function that will be executed when a message from a friend is received. function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); @@ -137,11 +133,9 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); returns -1 if there are problems */ int initMessenger(); - /* the main loop that needs to be run at least 200 times per second */ void doMessenger(); - /* SAVING AND LOADING FUNCTIONS: */ /* returns the size of the messenger data (for saving) */ diff --git a/core/friend_requests.c b/core/friend_requests.c index ba3f7535..d24dd4b4 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c @@ -25,7 +25,6 @@ uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; - /* Try to send a friendrequest to peer with public_key data is the data in the request and length is the length. return -1 if failure. @@ -61,7 +60,6 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) return num; } - static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); static uint8_t handle_friendrequest_isset = 0; @@ -72,7 +70,6 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) handle_friendrequest_isset = 1; } - int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) { diff --git a/core/friend_requests.h b/core/friend_requests.h index fb19a709..29bc2b88 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h @@ -21,11 +21,9 @@ * */ - #ifndef FRIEND_REQUESTS_H #define FRIEND_REQUESTS_H - #include "DHT.h" #include "net_crypto.h" @@ -37,7 +35,6 @@ extern "C" { data is the data in the request and length is the length. */ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); - /* set the function that will be executed when a friend request for us is received. function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); @@ -47,8 +44,6 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); return 1 if it didn't handle the packet or if the packet was shit. */ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); - - #ifdef __cplusplus } #endif diff --git a/core/net_crypto.c b/core/net_crypto.c index a16537e8..044845f0 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -26,12 +26,10 @@ #include "net_crypto.h" - /* Our public and secret keys. */ uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; - typedef struct { uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ @@ -177,7 +175,6 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data) return -1; } - /* return 0 if data could not be put in packet queue return 1 if data was put into the queue */ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) @@ -267,7 +264,6 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1 } } - /* Send a crypto handshake packet containing an encrypted secret nonce and session public key to peer with connection_id and public_key the packet is encrypted with a random nonce which is sent in plain text with the packet */ @@ -327,9 +323,6 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, return 1; } - - - /* get crypto connection id from public key of peer return -1 if there are no connections like we are looking for return id if it found it */ @@ -349,7 +342,6 @@ int getcryptconnection_id(uint8_t * public_key) return -1; } - /* Start a secure connection with other peer who has public_key and ip_port returns -1 if failure returns crypt_connection_id of the initialized connection if everything went well. */ @@ -447,7 +439,6 @@ int crypto_kill(int crypt_connection_id) return 1; } - /* accept an incoming connection using the parameters provided by crypto_inbound return -1 if not successful returns the crypt_connection_id if successful */ @@ -505,7 +496,6 @@ int is_cryptoconnected(int crypt_connection_id) return 0; } - /* Generate our public and private keys Only call this function the first time the program starts. */ void new_keys() @@ -681,4 +671,4 @@ void doNetCrypto() handle_incomings(); receive_crypto(); killTimedout(); -} +} \ No newline at end of file diff --git a/core/net_crypto.h b/core/net_crypto.h index 0dba0552..b5bab17a 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h @@ -55,13 +55,11 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, /* fill the given nonce with random bytes. */ void random_nonce(uint8_t * nonce); - /* return 0 if there is no received data in the buffer return -1 if the packet was discarded. return length of received data if successful */ int read_cryptpacket(int crypt_connection_id, uint8_t * data); - /* return 0 if data could not be put in packet queue return 1 if data was put into the queue */ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); @@ -74,20 +72,17 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); returns the length of the created packet on success */ int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); - /* puts the senders public key in the request in public_key, the data from the request in data if a friend or ping request was sent to us and returns the length of the data. packet is the request packet and length is its length return -1 if not valid request. */ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); - /* Start a secure connection with other peer who has public_key and ip_port returns -1 if failure returns crypt_connection_id of the initialized connection if everything went well. */ int crypto_connect(uint8_t * public_key, IP_Port ip_port); - /* kill a crypto connection return 0 if killed successfully return 1 if there was a problem. */ @@ -102,7 +97,6 @@ int crypto_kill(int crypt_connection_id); to refuse it just call kill_connection(...) on the connection id */ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); - /* accept an incoming connection using the parameters provided by crypto_inbound return -1 if not successful returns the crypt_connection_id if successful */ diff --git a/core/network.c b/core/network.c index 8c9cd0bd..5aa0d304 100644 --- a/core/network.c +++ b/core/network.c @@ -23,7 +23,6 @@ #include "network.h" - /* returns current UNIX time in microseconds (us). */ uint64_t current_time() { @@ -128,7 +127,6 @@ int init_networking(IP ip, uint16_t port) return -1; #endif - /* Functions to increase the size of the send and receive UDP buffers NOTE: uncomment if necessary */ /* @@ -146,7 +144,6 @@ int init_networking(IP ip, uint16_t port) /*Enable broadcast on socket*/ int broadcast = 1; setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); - /* Set socket nonblocking */ #ifdef WIN32 diff --git a/core/network.h b/core/network.h index e6bf1a7a..dcbd4160 100644 --- a/core/network.h +++ b/core/network.h @@ -20,7 +20,6 @@ * along with Tox. If not, see . * */ - #ifndef NETWORK_H #define NETWORK_H @@ -31,8 +30,6 @@ #include #include - - #ifdef WIN32 /* Put win32 includes here */ //Windows XP #define WINVER 0x0501 @@ -97,7 +94,6 @@ typedef struct #endif }ADDR; - /* returns current time in milleseconds since the epoch. */ uint64_t current_time(); diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 6aaac677..b06ddea5 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c @@ -41,7 +41,7 @@ #include -//Sleep function (x = milliseconds) +/* Sleep function (x = milliseconds) */ #ifdef WIN32 #define c_sleep(x) Sleep(1*x) @@ -60,16 +60,14 @@ void printip(IP_Port ip_port) printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); } - -//horrible function from one of my first C programs. -//only here because I was too lazy to write a proper one. +/* horrible function from one of my first C programs. + *only here because I was too lazy to write a proper one. */ unsigned char * hex_string_to_bin(char hex_string[]) { unsigned char * val = malloc(strlen(hex_string)); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) - { + while(i < strlen(hex_string)) { sscanf(pos,"%2hhx",&val[i]); pos+=2; i++; @@ -88,8 +86,7 @@ int main(int argc, char *argv[]) new_keys(); printf("OUR ID: "); uint32_t i; - for(i = 0; i < 32; i++) - { + for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); @@ -105,8 +102,7 @@ int main(int argc, char *argv[]) uint8_t friend_id[32]; memcpy(friend_id, hex_string_to_bin(temp_id), 32); - - //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); + /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ DHT_addfriend(friend_id); @@ -118,15 +114,13 @@ int main(int argc, char *argv[]) int friendrequest = -1; uint8_t request_data[512]; - //initialize networking - //bind to ip 0.0.0.0:PORT + /* initialize networking + * bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); initNetCrypto(); - - perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); @@ -147,122 +141,99 @@ int main(int argc, char *argv[]) if ( file2==NULL ){return 1;} read1 = fread(buffer1, 1, 128, file1); - while(1) - { - - while(receivepacket(&ip_port, data, &length) != -1) - { - if(rand() % 3 != 1)//simulate packet loss - { - if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) - { - //if packet is not recognized + while(1) { + while(receivepacket(&ip_port, data, &length) != -1) { + if(rand() % 3 != 1) { /* simulate packet loss */ + if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { + /* if packet is not recognized */ printf("Received unhandled packet with length: %u\n", length); - } - else - { + } else { printf("Received handled packet with length: %u\n", length); } } } friend_ip = DHT_getfriendip(friend_id); - if(friend_ip.ip.i != 0) - { - if(connection == -1 && friendrequest == -1) - { + if(friend_ip.ip.i != 0) { + if(connection == -1 && friendrequest == -1) { printf("Sending friend request to peer:"); printip(friend_ip); friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); - //connection = crypto_connect((uint8_t *)friend_id, friend_ip); - //connection = new_connection(friend_ip); + /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ + /* connection = new_connection(friend_ip); */ } - if(check_friendrequest(friendrequest) == 1) - { + if(check_friendrequest(friendrequest) == 1) { printf("Started connecting to friend:"); connection = crypto_connect(friend_id, friend_ip); } } - if(inconnection == -1) - { + if(inconnection == -1) { uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES]; inconnection = crypto_inbound(public_key, secret_nonce, session_key); inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); - //inconnection = incoming_connection(); - if(inconnection != -1) - { + /* inconnection = incoming_connection(); */ + if(inconnection != -1) { printf("Someone connected to us:\n"); - // printip(connection_ip(inconnection)); + /* printip(connection_ip(inconnection)); */ } } - if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) - { + if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { printf("RECIEVED FRIEND REQUEST: %s\n", request_data); } - //if someone connected to us write what he sends to a file - //also send him our file. - if(inconnection != -1) - { - if(write_cryptpacket(inconnection, buffer1, read1)) - { + /* if someone connected to us write what he sends to a file + * also send him our file. */ + if(inconnection != -1) { + if(write_cryptpacket(inconnection, buffer1, read1)) { printf("Wrote data1.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_cryptpacket(inconnection, buffer2); - if(read2 != 0) - { + if(read2 != 0) { printf("Received data1.\n"); - if(!fwrite(buffer2, read2, 1, file2)) - { + if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error1\n"); } - if(read2 < 128) - { + if(read2 < 128) { printf("Closed file1 %u\n", read2); fclose(file2); } } - else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out. - { + /* if buffer is empty and the connection timed out. */ + else if(is_cryptoconnected(inconnection) == 4) { crypto_kill(inconnection); } } - //if we are connected to a friend send him data from the file. - //also put what he sends us in a file. - if(is_cryptoconnected(connection) >= 3) - { - if(write_cryptpacket(0, buffer1, read1)) - { + /* if we are connected to a friend send him data from the file. + * also put what he sends us in a file. */ + if(is_cryptoconnected(connection) >= 3) { + if(write_cryptpacket(0, buffer1, read1)) { printf("Wrote data2.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_cryptpacket(0, buffer2); - if(read2 != 0) - { + if(read2 != 0) { printf("Received data2.\n"); - if(!fwrite(buffer2, read2, 1, file2)) - { + if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error2\n"); } - if(read2 < 128) - { + if(read2 < 128) { printf("Closed file2 %u\n", read2); fclose(file2); } } - else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out. - { + /* if buffer is empty and the connection timed out. */ + else if(is_cryptoconnected(connection) == 4) { crypto_kill(connection); } } doDHT(); doLossless_UDP(); doNetCrypto(); - //print_clientlist(); - //print_friendlist(); - //c_sleep(300); + /*print_clientlist(); + *print_friendlist(); + *c_sleep(300); */ c_sleep(1); } diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c index c9865843..5b5d6c9f 100644 --- a/testing/DHT_sendfiletest.c +++ b/testing/DHT_sendfiletest.c @@ -33,6 +33,7 @@ * along with Tox. If not, see . * */ + #include "../core/network.h" #include "../core/DHT.h" #include "../core/Lossless_UDP.h" @@ -100,68 +101,53 @@ int main(int argc, char *argv[]) if ( file2==NULL ){return 1;} read1 = fread(buffer1, 1, 128, file1); - while(1) - { + while(1) { - while(receivepacket(&ip_port, data, &length) != -1) - { - if(rand() % 3 != 1)//simulate packet loss - { - if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) - { - //if packet is not recognized + while(receivepacket(&ip_port, data, &length) != -1) { + if(rand() % 3 != 1) { /* simulate packet loss */ + if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { + /* if packet is not recognized */ printf("Received unhandled packet with length: %u\n", length); - } - else - { + } else { printf("Received handled packet with length: %u\n", length); } } } friend_ip = DHT_getfriendip((uint8_t *)argv[3]); - if(friend_ip.ip.i != 0) - { - if(connection == -1) - { + if(friend_ip.ip.i != 0) { + if(connection == -1) { printf("Started connecting to friend:"); printip(friend_ip); connection = new_connection(friend_ip); } } - if(inconnection == -1) - { + if(inconnection == -1) { inconnection = incoming_connection(); - if(inconnection != -1) - { + if(inconnection != -1) { printf("Someone connected to us:"); printip(connection_ip(inconnection)); } } - //if someone connected to us write what he sends to a file - //also send him our file. - if(inconnection != -1) - { - if(write_packet(inconnection, buffer1, read1)) - { + /* if someone connected to us write what he sends to a file */ + /* also send him our file. */ + if(inconnection != -1) { + if(write_packet(inconnection, buffer1, read1)) { printf("Wrote data.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_packet(inconnection, buffer2); - if(read2 != 0) - { + if(read2 != 0) { printf("Received data.\n"); - if(!fwrite(buffer2, read2, 1, file2)) - { + if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error\n"); } - if(read2 < 128) - { + if(read2 < 128) { fclose(file2); } } } - //if we are connected to a friend send him data from the file. - //also put what he sends us in a file. + /* if we are connected to a friend send him data from the file. + * also put what he sends us in a file. */ if(is_connected(connection) == 3) { if(write_packet(0, buffer1, read1)) @@ -185,9 +171,9 @@ int main(int argc, char *argv[]) } doDHT(); doLossless_UDP(); - //print_clientlist(); - //print_friendlist(); - //c_sleep(300); + /* print_clientlist(); + * print_friendlist(); + * c_sleep(300); */ c_sleep(1); } diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 80019029..bb482595 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -47,17 +47,14 @@ #define PORT 33445 - void print_clientlist() { uint32_t i, j; IP_Port p_ip; printf("___________________CLOSE________________________________\n"); - for(i = 0; i < 4; i++) - { + for(i = 0; i < 4; i++) { printf("ClientID: "); - for(j = 0; j < 32; j++) - { + for(j = 0; j < 32; j++) { printf("%c", close_clientlist[i].client_id[j]); } p_ip = close_clientlist[i].ip_port; @@ -75,12 +72,10 @@ void print_friendlist() uint32_t i, j, k; IP_Port p_ip; printf("_________________FRIENDS__________________________________\n"); - for(k = 0; k < num_friends; k++) - { + for(k = 0; k < num_friends; k++) { printf("FRIEND %u\n", k); printf("ID: "); - for(j = 0; j < 32; j++) - { + for(j = 0; j < 32; j++) { printf("%c", friends_list[k].client_id[j]); } p_ip = DHT_getfriendip(friends_list[k].client_id); @@ -88,11 +83,9 @@ void print_friendlist() printf("\nCLIENTS IN LIST:\n\n"); - for(i = 0; i < 4; i++) - { + for(i = 0; i < 4; i++) { printf("ClientID: "); - for(j = 0; j < 32; j++) - { + for(j = 0; j < 32; j++) { if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16) printf("0"); printf("%hhX", friends_list[k].client_list[i].client_id[j]); @@ -113,8 +106,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) uint32_t i; printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("--------------------BEGIN-----------------------------\n"); - for(i = 0; i < length; i++) - { + for(i = 0; i < length; i++) { if(data[i] < 16) printf("0"); printf("%hhX",data[i]); @@ -129,8 +121,7 @@ unsigned char * hex_string_to_bin(char hex_string[]) unsigned char * val = malloc(strlen(hex_string)); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) - { + while(i < strlen(hex_string)) { sscanf(pos,"%2hhx",&val[i]); pos+=2; i++; @@ -149,8 +140,7 @@ int main(int argc, char *argv[]) new_keys(); printf("OUR ID: "); uint32_t i; - for(i = 0; i < 32; i++) - { + for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); @@ -161,8 +151,8 @@ int main(int argc, char *argv[]) scanf("%s", temp_id); DHT_addfriend(hex_string_to_bin(temp_id)); - //initialize networking - //bind to ip 0.0.0.0:PORT + /* initialize networking */ + /* bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); @@ -171,10 +161,10 @@ int main(int argc, char *argv[]) perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); - //bootstrap_ip_port.ip.c[0] = 127; - //bootstrap_ip_port.ip.c[1] = 0; - //bootstrap_ip_port.ip.c[2] = 0; - //bootstrap_ip_port.ip.c[3] = 1; + /* bootstrap_ip_port.ip.c[0] = 127; + * bootstrap_ip_port.ip.c[1] = 0; + * bootstrap_ip_port.ip.c[2] = 0; + * bootstrap_ip_port.ip.c[3] = 1; */ bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); @@ -182,20 +172,15 @@ int main(int argc, char *argv[]) uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; - while(1) - { + while(1) { doDHT(); - while(receivepacket(&ip_port, data, &length) != -1) - { - if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) - { + while(receivepacket(&ip_port, data, &length) != -1) { + if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { //unhandled packet printpacket(data, length, ip_port); - } - else - { + } else { printf("Received handled packet with length: %u\n", length); } } diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 07ab319b..8f23528c 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -42,7 +42,6 @@ #endif - #define PORT 33446 void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) @@ -50,8 +49,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) uint32_t i; printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("--------------------BEGIN-----------------------------\n"); - for(i = 0; i < length; i++) - { + for(i = 0; i < length; i++) { if(data[i] < 16) printf("0"); printf("%hhX",data[i]); @@ -117,39 +115,34 @@ void printconnection(int connection_id) } */ -//recieve packets and send them to the packethandler -//run doLossless_UDP(); + +/*( recieve packets and send them to the packethandler */ +/*run doLossless_UDP(); */ void Lossless_UDP() { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; - while(receivepacket(&ip_port, data, &length) != -1) - { + while(receivepacket(&ip_port, data, &length) != -1) { printf("packet with length: %u\n", length); - //if(rand() % 3 != 1)//add packet loss - // { - if(LosslessUDP_handlepacket(data, length, ip_port)) - { + /* if(rand() % 3 != 1)//add packet loss + { */ + if(LosslessUDP_handlepacket(data, length, ip_port)) { printpacket(data, length, ip_port); - } - else - { + } else { //printconnection(0); printf("Received handled packet with length: %u\n", length); } - // } + /* } */ } doLossless_UDP(); } - int main(int argc, char *argv[]) { - if (argc < 4) - { + if (argc < 4) { printf("usage: %s ip port filename\n", argv[0]); exit(0); } @@ -161,8 +154,8 @@ int main(int argc, char *argv[]) if ( file==NULL ){return 1;} - //initialize networking - //bind to ip 0.0.0.0:PORT + /* initialize networking */ + /* bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); @@ -173,17 +166,14 @@ int main(int argc, char *argv[]) printip(serverip); int connection = new_connection(serverip); uint64_t timer = current_time(); - while(1) - { - // printconnection(connection); + while(1) { + /* printconnection(connection); */ Lossless_UDP(); - if(is_connected(connection) == 3) - { + if(is_connected(connection) == 3) { printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); break; } - if(is_connected(connection) == 0) - { + if(is_connected(connection) == 0) { printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); return 1; } @@ -192,38 +182,32 @@ int main(int argc, char *argv[]) timer = current_time(); - //read first part of file + /*read first part of file */ read = fread(buffer, 1, 512, file); - while(1) - { - //printconnection(connection); + while(1) { + /* printconnection(connection); */ Lossless_UDP(); - if(is_connected(connection) == 3) - { + if(is_connected(connection) == 3) { - if(write_packet(connection, buffer, read)) - { - //printf("Wrote data.\n"); + if(write_packet(connection, buffer, read)) { + /* printf("Wrote data.\n"); */ read = fread(buffer, 1, 512, file); } - //printf("%u\n", sendqueue(connection)); - if(sendqueue(connection) == 0) - { - if(read == 0) - { + /* printf("%u\n", sendqueue(connection)); */ + if(sendqueue(connection) == 0) { + if(read == 0) { printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer)); break; } } } - else - { + else { printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); return 0; } - //c_sleep(1); + /* c_sleep(1); */ } return 0; diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 0ed214e9..a8097048 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -43,7 +43,6 @@ #endif - #define PORT 33445 void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) @@ -51,14 +50,14 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) uint32_t i; printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("--------------------BEGIN-----------------------------\n"); - for(i = 0; i < length; i++) - { + for(i = 0; i < length; i++) { if(data[i] < 16) printf("0"); printf("%hhX",data[i]); } printf("\n--------------------END-----------------------------\n\n\n"); } + /* void printpackets(Data test) { @@ -113,23 +112,20 @@ void printconnection(int connection_id) } */ -//recieve packets and send them to the packethandler -//run doLossless_UDP(); + +/* recieve packets and send them to the packethandler + * run doLossless_UDP(); */ void Lossless_UDP() { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; - while(receivepacket(&ip_port, data, &length) != -1) - { + while(receivepacket(&ip_port, data, &length) != -1) { //if(rand() % 3 != 1)//add packet loss //{ - if(LosslessUDP_handlepacket(data, length, ip_port)) - { + if(LosslessUDP_handlepacket(data, length, ip_port)) { printpacket(data, length, ip_port); - } - else - { + } else { //printconnection(0); printf("Received handled packet with length: %u\n", length); } @@ -137,14 +133,12 @@ void Lossless_UDP() } doLossless_UDP(); - } int main(int argc, char *argv[]) { - if (argc < 2) - { + if (argc < 2) { printf("usage: %s filename\n", argv[0]); exit(0); } @@ -167,14 +161,11 @@ int main(int argc, char *argv[]) uint64_t timer = current_time(); - while(1) - { + while(1) { Lossless_UDP(); connection = incoming_connection(); - if(connection != -1) - { - if(is_connected(connection) == 2) - { + if(connection != -1) { + if(is_connected(connection) == 2) { printf("Recieved the connection.\n"); } @@ -185,25 +176,20 @@ int main(int argc, char *argv[]) timer = current_time(); - while(1) - { + while(1) { //printconnection(0); Lossless_UDP(); - if(is_connected(connection) >= 2) - { + if(is_connected(connection) >= 2) { kill_connection_in(connection, 3000000); read = read_packet(connection, buffer); - if(read != 0) - { + if(read != 0) { // printf("Recieved data.\n"); - if(!fwrite(buffer, read, 1, file)) - { + if(!fwrite(buffer, read, 1, file)) { printf("file write error\n"); } } } - if(is_connected(connection) == 4) - { + if(is_connected(connection) == 4) { printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); fclose(file); return 1; diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index e5da16e9..19ab4cc3 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -104,15 +104,12 @@ int main(int argc, char *argv[]) exit(0); } initMessenger(); - if(argc > 3) - { + if(argc > 3) { IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); - } - else - { + } else { FILE *file = fopen(argv[1], "rb"); if ( file==NULL ){return 1;} int read; @@ -127,8 +124,7 @@ int main(int argc, char *argv[]) printf("OUR ID: "); uint32_t i; - for(i = 0; i < 32; i++) - { + for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); @@ -138,16 +134,14 @@ int main(int argc, char *argv[]) char temp_id[128]; printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); - if(scanf("%s", temp_id) != 1) - { + if(scanf("%s", temp_id) != 1) { return 1; } int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); - while(1) - { + while(1) { uint8_t name[128]; getname(num, name); printf("%s\n", name); @@ -162,6 +156,5 @@ int main(int argc, char *argv[]) fwrite(buffer, 1, Messenger_size(), file); free(buffer); fclose(file); - } - + } } diff --git a/testing/nTox.c b/testing/nTox.c index 87a87007..caa7d45c 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -51,8 +51,7 @@ unsigned char * hex_string_to_bin(char hex_string[]) unsigned char * val = malloc(strlen(hex_string)); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) - { + while(i < strlen(hex_string)) { sscanf(pos,"%2hhx",&val[i]); pos+=2; i++; @@ -190,6 +189,7 @@ void do_refresh() clrtoeol(); refresh(); } + void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) { new_lines("[i] received friend request"); @@ -205,6 +205,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) new_lines(numchar); } } + void print_message(int friendnumber, uint8_t * string, uint16_t length) { char name[MAX_NAME_LENGTH]; @@ -220,6 +221,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this new_lines(msg); } + void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; getname(friendnumber, (uint8_t*)name); @@ -227,6 +229,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); new_lines(msg); } + void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; getname(friendnumber, (uint8_t*)name); @@ -234,6 +237,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); new_lines(msg); } + void load_key(){ FILE *data_file = NULL; if ((data_file = fopen("data","r"))) { @@ -260,6 +264,7 @@ void load_key(){ } fclose(data_file); } + int main(int argc, char *argv[]) { if (argc < 4) { @@ -317,9 +322,7 @@ int main(int argc, char *argv[]) DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); nodelay(stdscr, TRUE); while(true) { - - if (on == 0 && DHT_isconnected()) - { + if (on == 0 && DHT_isconnected()) { new_lines("[i] connected to DHT\n[i] define username with /n"); on = 1; } diff --git a/testing/nTox.h b/testing/nTox.h index 5a86830f..9b69d959 100644 --- a/testing/nTox.h +++ b/testing/nTox.h @@ -20,6 +20,7 @@ * along with Tox. If not, see . * */ + #ifndef NTOX_H #define NTOX_H -- cgit v1.2.3