summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/DHT.c559
-rw-r--r--core/DHT.h11
-rw-r--r--core/Lossless_UDP.c17
-rw-r--r--core/Lossless_UDP.h9
-rw-r--r--core/Messenger.c14
-rw-r--r--core/Messenger.h8
-rw-r--r--core/friend_requests.c3
-rw-r--r--core/friend_requests.h5
-rw-r--r--core/net_crypto.c12
-rw-r--r--core/net_crypto.h6
-rw-r--r--core/network.c3
-rw-r--r--core/network.h4
-rw-r--r--testing/DHT_cryptosendfiletest.c119
-rw-r--r--testing/DHT_sendfiletest.c60
-rw-r--r--testing/DHT_test.c53
-rw-r--r--testing/Lossless_UDP_testclient.c72
-rw-r--r--testing/Lossless_UDP_testserver.c48
-rw-r--r--testing/Messenger_test.c19
-rw-r--r--testing/nTox.c13
-rw-r--r--testing/nTox.h1
20 files changed, 335 insertions, 701 deletions
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 @@
23 23
24#include "DHT.h" 24#include "DHT.h"
25 25
26
27typedef struct 26typedef struct
28{ 27{
29 uint8_t client_id[CLIENT_ID_SIZE]; 28 uint8_t client_id[CLIENT_ID_SIZE];
@@ -34,8 +33,10 @@ typedef struct
34 (for nodes in friends_list) or us (for nodes in close_clientlist) */ 33 (for nodes in friends_list) or us (for nodes in close_clientlist) */
35 uint32_t ret_timestamp; 34 uint32_t ret_timestamp;
36}Client_data; 35}Client_data;
36
37/* maximum number of clients stored per friend. */ 37/* maximum number of clients stored per friend. */
38#define MAX_FRIEND_CLIENTS 8 38#define MAX_FRIEND_CLIENTS 8
39
39typedef struct 40typedef struct
40{ 41{
41 uint8_t client_id[CLIENT_ID_SIZE]; 42 uint8_t client_id[CLIENT_ID_SIZE];
@@ -50,7 +51,6 @@ typedef struct
50 uint32_t NATping_timestamp; 51 uint32_t NATping_timestamp;
51}Friend; 52}Friend;
52 53
53
54typedef struct 54typedef struct
55{ 55{
56 uint8_t client_id[CLIENT_ID_SIZE]; 56 uint8_t client_id[CLIENT_ID_SIZE];
@@ -74,8 +74,6 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
74#define LCLIENT_LIST 32 74#define LCLIENT_LIST 32
75static Client_data close_clientlist[LCLIENT_LIST]; 75static Client_data close_clientlist[LCLIENT_LIST];
76 76
77
78
79static Friend * friends_list; 77static Friend * friends_list;
80static uint16_t num_friends; 78static uint16_t num_friends;
81 79
@@ -88,7 +86,6 @@ static Pinged pings[LPING_ARRAY];
88 86
89static Pinged send_nodes[LSEND_NODES_ARRAY]; 87static Pinged send_nodes[LSEND_NODES_ARRAY];
90 88
91
92/* Compares client_id1 and client_id2 with client_id 89/* Compares client_id1 and client_id2 with client_id
93 return 0 if both are same distance 90 return 0 if both are same distance
94 return 1 if client_id1 is closer 91 return 1 if client_id1 is closer
@@ -96,17 +93,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
96int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ 93int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */
97{ 94{
98 uint32_t i; 95 uint32_t i;
99 for(i = 0; i < CLIENT_ID_SIZE; ++i) 96 for(i = 0; i < CLIENT_ID_SIZE; ++i) {
100 { 97 if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) {
101 if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i]))
102 {
103 return 1; 98 return 1;
104 } 99 } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) {
105 else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i]))
106 {
107 return 2; 100 return 2;
108 } 101 }
109
110 } 102 }
111 103
112 return 0; 104 return 0;
@@ -123,17 +115,14 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
123 uint32_t i; 115 uint32_t i;
124 uint32_t temp_time = unix_time(); 116 uint32_t temp_time = unix_time();
125 117
126 for(i = 0; i < length; ++i) 118 for(i = 0; i < length; ++i) {
127 {
128 /*If ip_port is assigned to a different client_id replace it*/ 119 /*If ip_port is assigned to a different client_id replace it*/
129 if(list[i].ip_port.ip.i == ip_port.ip.i && 120 if(list[i].ip_port.ip.i == ip_port.ip.i &&
130 list[i].ip_port.port == ip_port.port) 121 list[i].ip_port.port == ip_port.port) {
131 {
132 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 122 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
133 } 123 }
134 124
135 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) 125 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
136 {
137 /* Refresh the client timestamp. */ 126 /* Refresh the client timestamp. */
138 list[i].timestamp = temp_time; 127 list[i].timestamp = temp_time;
139 list[i].ip_port.ip.i = ip_port.ip.i; 128 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_
150int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) 139int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
151{ 140{
152 uint32_t i; 141 uint32_t i;
153 for(i = 0; i < length; ++i) 142 for(i = 0; i < length; ++i) {
154 { 143 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
155 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
156 {
157 144
158 return 1; 145 return 1;
159 } 146 }
@@ -167,8 +154,7 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
167static int friend_number(uint8_t * client_id) 154static int friend_number(uint8_t * client_id)
168{ 155{
169 uint32_t i; 156 uint32_t i;
170 for(i = 0; i < num_friends; ++i) 157 for(i = 0; i < num_friends; ++i) {
171 {
172 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ 158 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
173 { 159 {
174 return i; 160 return i;
@@ -177,13 +163,11 @@ static int friend_number(uint8_t * client_id)
177 return -1; 163 return -1;
178} 164}
179 165
180
181/* the number of seconds for a non responsive node to become bad. */ 166/* the number of seconds for a non responsive node to become bad. */
182#define BAD_NODE_TIMEOUT 70 167#define BAD_NODE_TIMEOUT 70
183/* the max number of nodes to send with send nodes. */ 168/* the max number of nodes to send with send nodes. */
184#define MAX_SENT_NODES 8 169#define MAX_SENT_NODES 8
185 170
186
187/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: 171/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
188 put them in the nodes_list and return how many were found. 172 put them in the nodes_list and return how many were found.
189 TODO: Make this function much more efficient. */ 173 TODO: Make this function much more efficient. */
@@ -192,48 +176,35 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
192 uint32_t i, j, k; 176 uint32_t i, j, k;
193 int num_nodes=0; 177 int num_nodes=0;
194 uint32_t temp_time = unix_time(); 178 uint32_t temp_time = unix_time();
195 for(i = 0; i < LCLIENT_LIST; ++i) 179 for(i = 0; i < LCLIENT_LIST; ++i) {
196 {
197 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && 180 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
198 !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) 181 !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) {
199 /* if node is good and not already in list. */ 182 /* if node is good and not already in list. */
200 { 183 if(num_nodes < MAX_SENT_NODES) {
201 if(num_nodes < MAX_SENT_NODES)
202 {
203 memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); 184 memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
204 nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; 185 nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
205 num_nodes++; 186 num_nodes++;
206 } 187 }
207 else for(j = 0; j < MAX_SENT_NODES; ++j) 188 else for(j = 0; j < MAX_SENT_NODES; ++j) {
208 { 189 if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) {
209 if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2)
210 {
211 memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); 190 memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
212 nodes_list[j].ip_port = close_clientlist[i].ip_port; 191 nodes_list[j].ip_port = close_clientlist[i].ip_port;
213 break; 192 break;
214 } 193 }
215 } 194 }
216 } 195 }
217
218 } 196 }
219 for(i = 0; i < num_friends; ++i) 197 for(i = 0; i < num_friends; ++i) {
220 { 198 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
221 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
222 {
223 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && 199 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
224 !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) 200 !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) {
225 /* if node is good and not already in list. */ 201 /* if node is good and not already in list. */
226 { 202 if(num_nodes < MAX_SENT_NODES) {
227 if(num_nodes < MAX_SENT_NODES)
228 {
229 memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); 203 memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
230 nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; 204 nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port;
231 num_nodes++; 205 num_nodes++;
232 } 206 } else for(k = 0; k < MAX_SENT_NODES; ++k) {
233 else for(k = 0; k < MAX_SENT_NODES; ++k) 207 if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) {
234 {
235 if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2)
236 {
237 memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); 208 memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
238 nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; 209 nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port;
239 break; 210 break;
@@ -243,12 +214,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
243 } 214 }
244 } 215 }
245 216
246 return num_nodes; 217 return num_nodes;
247
248} 218}
249 219
250
251
252/* replace first bad (or empty) node with this one 220/* replace first bad (or empty) node with this one
253 return 0 if successful 221 return 0 if successful
254 return 1 if not (list contains no bad nodes) */ 222 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
256{ 224{
257 uint32_t i; 225 uint32_t i;
258 uint32_t temp_time = unix_time(); 226 uint32_t temp_time = unix_time();
259 for(i = 0; i < length; ++i) 227 for(i = 0; i < length; ++i) {
260 { 228 if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ {
261 if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */
262 {
263 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 229 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
264 list[i].ip_port = ip_port; 230 list[i].ip_port = ip_port;
265 list[i].timestamp = temp_time; 231 list[i].timestamp = temp_time;
@@ -279,10 +245,8 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po
279 uint32_t i; 245 uint32_t i;
280 uint32_t temp_time = unix_time(); 246 uint32_t temp_time = unix_time();
281 247
282 for(i = 0; i < length; ++i) 248 for(i = 0; i < length; ++i) {
283 { 249 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
284 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2)
285 {
286 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 250 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
287 list[i].ip_port = ip_port; 251 list[i].ip_port = ip_port;
288 list[i].timestamp = temp_time; 252 list[i].timestamp = temp_time;
@@ -302,23 +266,16 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
302 uint32_t i; 266 uint32_t i;
303 267
304 /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ 268 /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */
305 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) 269 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
306 {
307 270
308 if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) 271 if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
309 {
310 /* if we can't replace bad nodes we try replacing good ones */ 272 /* if we can't replace bad nodes we try replacing good ones */
311 replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); 273 replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key);
312 } 274 }
313
314 } 275 }
315 for(i = 0; i < num_friends; ++i) 276 for(i = 0; i < num_friends; ++i) {
316 { 277 if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) {
317 if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) 278 if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) {
318 {
319
320 if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port))
321 {
322 /* if we can't replace bad nodes we try replacing good ones. */ 279 /* if we can't replace bad nodes we try replacing good ones. */
323 replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); 280 replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id);
324 } 281 }
@@ -332,12 +289,9 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
332{ 289{
333 uint32_t i, j; 290 uint32_t i, j;
334 uint32_t temp_time = unix_time(); 291 uint32_t temp_time = unix_time();
335 if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) 292 if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) {
336 { 293 for(i = 0; i < LCLIENT_LIST; ++i) {
337 for(i = 0; i < LCLIENT_LIST; ++i) 294 if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
338 {
339 if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0)
340 {
341 close_clientlist[i].ret_ip_port = ip_port; 295 close_clientlist[i].ret_ip_port = ip_port;
342 close_clientlist[i].ret_timestamp = temp_time; 296 close_clientlist[i].ret_timestamp = temp_time;
343 return; 297 return;
@@ -345,14 +299,10 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
345 } 299 }
346 } 300 }
347 else 301 else
348 for(i = 0; i < num_friends; ++i) 302 for(i = 0; i < num_friends; ++i) {
349 { 303 if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) {
350 if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) 304 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
351 { 305 if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) {
352 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
353 {
354 if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0)
355 {
356 friends_list[i].client_list[j].ret_ip_port = ip_port; 306 friends_list[i].client_list[j].ret_ip_port = ip_port;
357 friends_list[i].client_list[j].ret_timestamp = temp_time; 307 friends_list[i].client_list[j].ret_timestamp = temp_time;
358 return; 308 return;
@@ -364,6 +314,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
364 314
365/* ping timeout in seconds */ 315/* ping timeout in seconds */
366#define PING_TIMEOUT 5 316#define PING_TIMEOUT 5
317
367/* check if we are currently pinging an ip_port and/or a ping_id 318/* check if we are currently pinging an ip_port and/or a ping_id
368 variables with values of zero will not be checked. 319 variables with values of zero will not be checked.
369 if we are already, return 1 320 if we are already, return 1
@@ -375,28 +326,22 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
375 uint8_t pinging; 326 uint8_t pinging;
376 uint32_t temp_time = unix_time(); 327 uint32_t temp_time = unix_time();
377 328
378 for(i = 0; i < LPING_ARRAY; ++i ) 329 for(i = 0; i < LPING_ARRAY; ++i ) {
379 { 330 if((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
380 if((pings[i].timestamp + PING_TIMEOUT) > temp_time)
381 {
382 pinging = 0; 331 pinging = 0;
383 if(ip_port.ip.i != 0) 332 if(ip_port.ip.i != 0) {
384 {
385 if(pings[i].ip_port.ip.i == ip_port.ip.i && 333 if(pings[i].ip_port.ip.i == ip_port.ip.i &&
386 pings[i].ip_port.port == ip_port.port) 334 pings[i].ip_port.port == ip_port.port) {
387 {
388 ++pinging; 335 ++pinging;
389 } 336 }
390 } 337 }
391 if(ping_id != 0) 338 if(ping_id != 0) {
392 {
393 if(pings[i].ping_id == ping_id) 339 if(pings[i].ping_id == ping_id)
394 { 340 {
395 ++pinging; 341 ++pinging;
396 } 342 }
397 } 343 }
398 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) 344 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
399 {
400 return 1; 345 return 1;
401 } 346 }
402 347
@@ -407,7 +352,6 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
407 352
408} 353}
409 354
410
411/* Same as last function but for get_node requests. */ 355/* Same as last function but for get_node requests. */
412int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) 356int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
413{ 357{
@@ -415,28 +359,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
415 uint8_t pinging; 359 uint8_t pinging;
416 uint32_t temp_time = unix_time(); 360 uint32_t temp_time = unix_time();
417 361
418 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) 362 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) {
419 { 363 if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
420 if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time)
421 {
422 pinging = 0; 364 pinging = 0;
423 if(ip_port.ip.i != 0) 365 if(ip_port.ip.i != 0) {
424 {
425 if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && 366 if(send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
426 send_nodes[i].ip_port.port == ip_port.port) 367 send_nodes[i].ip_port.port == ip_port.port) {
427 {
428 ++pinging; 368 ++pinging;
429 } 369 }
430 } 370 }
431 if(ping_id != 0) 371 if(ping_id != 0) {
432 { 372 if(send_nodes[i].ping_id == ping_id) {
433 if(send_nodes[i].ping_id == ping_id)
434 {
435 ++pinging; 373 ++pinging;
436 } 374 }
437 } 375 }
438 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) 376 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
439 {
440 return 1; 377 return 1;
441 } 378 }
442 379
@@ -447,7 +384,6 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
447 384
448} 385}
449 386
450
451/* Add a new ping request to the list of ping requests 387/* Add a new ping request to the list of ping requests
452 returns the ping_id to put in the ping request 388 returns the ping_id to put in the ping request
453 returns 0 if problem. 389 returns 0 if problem.
@@ -458,12 +394,9 @@ uint64_t add_pinging(IP_Port ip_port)
458 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 394 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
459 uint32_t temp_time = unix_time(); 395 uint32_t temp_time = unix_time();
460 396
461 for(i = 0; i < PING_TIMEOUT; ++i ) 397 for(i = 0; i < PING_TIMEOUT; ++i ) {
462 { 398 for(j = 0; j < LPING_ARRAY; ++j ) {
463 for(j = 0; j < LPING_ARRAY; ++j ) 399 if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
464 {
465 if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time)
466 {
467 pings[j].timestamp = temp_time; 400 pings[j].timestamp = temp_time;
468 pings[j].ip_port = ip_port; 401 pings[j].ip_port = ip_port;
469 pings[j].ping_id = ping_id; 402 pings[j].ping_id = ping_id;
@@ -482,12 +415,9 @@ uint64_t add_gettingnodes(IP_Port ip_port)
482 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 415 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
483 uint32_t temp_time = unix_time(); 416 uint32_t temp_time = unix_time();
484 417
485 for(i = 0; i < PING_TIMEOUT; ++i ) 418 for(i = 0; i < PING_TIMEOUT; ++i ) {
486 { 419 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) {
487 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) 420 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) {
488 {
489 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time)
490 {
491 send_nodes[j].timestamp = temp_time; 421 send_nodes[j].timestamp = temp_time;
492 send_nodes[j].ip_port = ip_port; 422 send_nodes[j].ip_port = ip_port;
493 send_nodes[j].ping_id = ping_id; 423 send_nodes[j].ping_id = ping_id;
@@ -499,25 +429,20 @@ uint64_t add_gettingnodes(IP_Port ip_port)
499 429
500} 430}
501 431
502
503
504/* send a ping request 432/* send a ping request
505 Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ 433 Ping request only works if none has been sent to that ip/port in the last 5 seconds. */
506static int pingreq(IP_Port ip_port, uint8_t * public_key) 434static int pingreq(IP_Port ip_port, uint8_t * public_key)
507{ 435{
508 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ 436 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ {
509 {
510 return 1; 437 return 1;
511 } 438 }
512 439
513 if(is_pinging(ip_port, 0)) 440 if(is_pinging(ip_port, 0)) {
514 {
515 return 1; 441 return 1;
516 } 442 }
517 443
518 uint64_t ping_id = add_pinging(ip_port); 444 uint64_t ping_id = add_pinging(ip_port);
519 if(ping_id == 0) 445 if(ping_id == 0) {
520 {
521 return 1; 446 return 1;
522 } 447 }
523 448
@@ -527,8 +452,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
527 random_nonce(nonce); 452 random_nonce(nonce);
528 453
529 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); 454 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
530 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) 455 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) {
531 {
532 return -1; 456 return -1;
533 } 457 }
534 data[0] = 0; 458 data[0] = 0;
@@ -540,12 +464,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
540 464
541} 465}
542 466
543
544/* send a ping response */ 467/* send a ping response */
545static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) 468static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
546{ 469{
547 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ 470 /* check if packet is gonna be sent to ourself */
548 { 471 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) {
549 return 1; 472 return 1;
550 } 473 }
551 474
@@ -555,8 +478,7 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
555 random_nonce(nonce); 478 random_nonce(nonce);
556 479
557 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); 480 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
558 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) 481 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) {
559 {
560 return -1; 482 return -1;
561 } 483 }
562 data[0] = 1; 484 data[0] = 1;
@@ -571,20 +493,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
571/* send a getnodes request */ 493/* send a getnodes request */
572static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) 494static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
573{ 495{
574 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ 496 /* check if packet is gonna be sent to ourself */
575 { 497 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) {
576 return 1; 498 return 1;
577 } 499 }
578 500
579 if(is_gettingnodes(ip_port, 0)) 501 if(is_gettingnodes(ip_port, 0)) {
580 {
581 return 1; 502 return 1;
582 } 503 }
583 504
584 uint64_t ping_id = add_gettingnodes(ip_port); 505 uint64_t ping_id = add_gettingnodes(ip_port);
585 506
586 if(ping_id == 0) 507 if(ping_id == 0) {
587 {
588 return 1; 508 return 1;
589 } 509 }
590 510
@@ -599,8 +519,7 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
599 519
600 int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); 520 int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt);
601 521
602 if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) 522 if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) {
603 {
604 return -1; 523 return -1;
605 } 524 }
606 data[0] = 2; 525 data[0] = 2;
@@ -611,12 +530,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
611 530
612} 531}
613 532
614
615/* send a send nodes response */ 533/* send a send nodes response */
616static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) 534static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
617{ 535{
618 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ 536 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ {
619 {
620 return 1; 537 return 1;
621 } 538 }
622 539
@@ -626,8 +543,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
626 Node_format nodes_list[MAX_SENT_NODES]; 543 Node_format nodes_list[MAX_SENT_NODES];
627 int num_nodes = get_close_nodes(client_id, nodes_list); 544 int num_nodes = get_close_nodes(client_id, nodes_list);
628 545
629 if(num_nodes == 0) 546 if(num_nodes == 0) {
630 {
631 return 0; 547 return 0;
632 } 548 }
633 549
@@ -642,8 +558,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
642 int len = encrypt_data(public_key, self_secret_key, nonce, plain, 558 int len = encrypt_data(public_key, self_secret_key, nonce, plain,
643 sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); 559 sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt);
644 560
645 if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) 561 if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) {
646 {
647 return -1; 562 return -1;
648 } 563 }
649 564
@@ -656,20 +571,17 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
656 571
657} 572}
658 573
659
660
661/* Packet handling functions 574/* Packet handling functions
662 One to handle each types of packets we receive 575 One to handle each types of packets we receive
663 return 0 if handled correctly, 1 if packet is bad. */ 576 return 0 if handled correctly, 1 if packet is bad. */
664int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) 577int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
665{ 578{
666 uint64_t ping_id; 579 uint64_t ping_id;
667 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) 580 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) {
668 {
669 return 1; 581 return 1;
670 } 582 }
671 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ 583 /* check if packet is from ourself. */
672 { 584 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) {
673 return 1; 585 return 1;
674 } 586 }
675 587
@@ -678,8 +590,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
678 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, 590 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
679 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 591 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
680 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); 592 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id);
681 if(len != sizeof(ping_id)) 593 if(len != sizeof(ping_id)) {
682 {
683 return 1; 594 return 1;
684 } 595 }
685 596
@@ -695,12 +606,10 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
695int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) 606int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
696{ 607{
697 uint64_t ping_id; 608 uint64_t ping_id;
698 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) 609 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) {
699 {
700 return 1; 610 return 1;
701 } 611 }
702 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ 612 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ {
703 {
704 return 1; 613 return 1;
705 } 614 }
706 615
@@ -709,13 +618,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
709 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, 618 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
710 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 619 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
711 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); 620 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id);
712 if(len != sizeof(ping_id)) 621 if(len != sizeof(ping_id)) {
713 {
714 return 1; 622 return 1;
715 } 623 }
716 624
717 if(is_pinging(source, ping_id)) 625 if(is_pinging(source, ping_id)) {
718 {
719 addto_lists(source, packet + 1); 626 addto_lists(source, packet + 1);
720 return 0; 627 return 0;
721 } 628 }
@@ -726,12 +633,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
726int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) 633int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
727{ 634{
728 uint64_t ping_id; 635 uint64_t ping_id;
729 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) 636 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) {
730 {
731 return 1; 637 return 1;
732 } 638 }
733 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ 639 /* check if packet is from ourself. */
734 { 640 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) {
735 return 1; 641 return 1;
736 } 642 }
737 643
@@ -741,8 +647,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
741 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 647 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
742 sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); 648 sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain);
743 649
744 if(len != sizeof(ping_id) + CLIENT_ID_SIZE) 650 if(len != sizeof(ping_id) + CLIENT_ID_SIZE) {
745 {
746 return 1; 651 return 1;
747 } 652 }
748 653
@@ -764,8 +669,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
764 (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 669 (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
765 + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || 670 + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 ||
766 length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 671 length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
767 + sizeof(Node_format) + ENCRYPTION_PADDING) 672 + sizeof(Node_format) + ENCRYPTION_PADDING) {
768 {
769 return 1; 673 return 1;
770 } 674 }
771 uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES 675 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)
778 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); 682 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain);
779 683
780 684
781 if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) 685 if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) {
782 {
783 return 1; 686 return 1;
784 } 687 }
785 688
786 memcpy(&ping_id, plain, sizeof(ping_id)); 689 memcpy(&ping_id, plain, sizeof(ping_id));
787 if(!is_gettingnodes(source, ping_id)) 690 if(!is_gettingnodes(source, ping_id)) {
788 {
789 return 1; 691 return 1;
790 } 692 }
791 693
@@ -795,8 +697,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
795 addto_lists(source, packet + 1); 697 addto_lists(source, packet + 1);
796 698
797 uint32_t i; 699 uint32_t i;
798 for(i = 0; i < num_nodes; ++i) 700 for(i = 0; i < num_nodes; ++i) {
799 {
800 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); 701 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id);
801 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); 702 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
802 } 703 }
@@ -809,16 +710,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
809int DHT_addfriend(uint8_t * client_id) 710int DHT_addfriend(uint8_t * client_id)
810{ 711{
811 Friend * temp; 712 Friend * temp;
812 if(num_friends == 0) 713 if(num_friends == 0) {
813 {
814 temp = malloc(sizeof(Friend)); 714 temp = malloc(sizeof(Friend));
815 } 715 } else {
816 else
817 {
818 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); 716 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
819 } 717 }
820 if(temp == NULL) 718 if(temp == NULL) {
821 {
822 return 1; 719 return 1;
823 } 720 }
824 721
@@ -830,24 +727,19 @@ int DHT_addfriend(uint8_t * client_id)
830 return 0; 727 return 0;
831} 728}
832 729
833
834
835int DHT_delfriend(uint8_t * client_id) 730int DHT_delfriend(uint8_t * client_id)
836{ 731{
837 uint32_t i; 732 uint32_t i;
838 Friend * temp; 733 Friend * temp;
839 for(i = 0; i < num_friends; ++i) 734 for(i = 0; i < num_friends; ++i) {
840 { 735 /* Equal */
841 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ 736 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){
842 {
843 --num_friends; 737 --num_friends;
844 if(num_friends != i) 738 if(num_friends != i) {
845 {
846 memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); 739 memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE);
847 } 740 }
848 temp = realloc(friends_list, sizeof(Friend) * (num_friends)); 741 temp = realloc(friends_list, sizeof(Friend) * (num_friends));
849 if(temp != NULL) 742 if(temp != NULL) {
850 {
851 friends_list = temp; 743 friends_list = temp;
852 } 744 }
853 return 0; 745 return 0;
@@ -856,38 +748,28 @@ int DHT_delfriend(uint8_t * client_id)
856 return 1; 748 return 1;
857} 749}
858 750
859
860
861
862/* TODO: Optimize this. */ 751/* TODO: Optimize this. */
863IP_Port DHT_getfriendip(uint8_t * client_id) 752IP_Port DHT_getfriendip(uint8_t * client_id)
864{ 753{
865 uint32_t i, j; 754 uint32_t i, j;
866 IP_Port empty = {{{0}}, 0}; 755 IP_Port empty = {{{0}}, 0};
867 uint32_t temp_time = unix_time(); 756 uint32_t temp_time = unix_time();
868 for(i = 0; i < num_friends; ++i) 757 for(i = 0; i < num_friends; ++i) {
869 { 758 /* Equal */
870 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ 759 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)1 {
871 { 760 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
872 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
873 {
874 if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && 761 if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 &&
875 friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) 762 friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) {
876 {
877 return friends_list[i].client_list[j].ip_port; 763 return friends_list[i].client_list[j].ip_port;
878 } 764 }
879 765 }
880 }
881
882 return empty; 766 return empty;
883 } 767 }
884 } 768 }
885 empty.ip.i = 1; 769 empty.ip.i = 1;
886 return empty; 770 return empty;
887 771
888} 772}
889
890
891 773
892/* The timeout after which a node is discarded completely. */ 774/* The timeout after which a node is discarded completely. */
893#define Kill_NODE_TIMEOUT 300 775#define Kill_NODE_TIMEOUT 300
@@ -900,8 +782,6 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
900 782
901/* Ping each client in the "friends" list every 60 seconds. 783/* Ping each client in the "friends" list every 60 seconds.
902 Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ 784 Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */
903
904
905void doDHTFriends() 785void doDHTFriends()
906{ 786{
907 uint32_t i, j; 787 uint32_t i, j;
@@ -909,27 +789,21 @@ void doDHTFriends()
909 uint32_t rand_node; 789 uint32_t rand_node;
910 uint32_t index[MAX_FRIEND_CLIENTS]; 790 uint32_t index[MAX_FRIEND_CLIENTS];
911 791
912 for(i = 0; i < num_friends; ++i) 792 for(i = 0; i < num_friends; ++i) {
913 {
914 uint32_t num_nodes = 0; 793 uint32_t num_nodes = 0;
915 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 794 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
916 { 795 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ {
917 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ 796 if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
918 {
919 if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time)
920 {
921 pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); 797 pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
922 friends_list[i].client_list[j].last_pinged = temp_time; 798 friends_list[i].client_list[j].last_pinged = temp_time;
923 } 799 }
924 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ 800 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ {
925 {
926 index[num_nodes] = j; 801 index[num_nodes] = j;
927 ++num_nodes; 802 ++num_nodes;
928 } 803 }
929 } 804 }
930 } 805 }
931 if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) 806 if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
932 {
933 rand_node = rand() % num_nodes; 807 rand_node = rand() % num_nodes;
934 getnodes(friends_list[i].client_list[index[rand_node]].ip_port, 808 getnodes(friends_list[i].client_list[index[rand_node]].ip_port,
935 friends_list[i].client_list[index[rand_node]].client_id, 809 friends_list[i].client_list[index[rand_node]].client_id,
@@ -951,62 +825,49 @@ void doClose() /* tested */
951 uint32_t rand_node; 825 uint32_t rand_node;
952 uint32_t index[LCLIENT_LIST]; 826 uint32_t index[LCLIENT_LIST];
953 827
954 for(i = 0; i < LCLIENT_LIST; ++i) 828 for(i = 0; i < LCLIENT_LIST; ++i) {
955 { 829 /* if node is not dead. */
956 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ 830 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
957 {
958 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) 831 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
959 { 832 {
960 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); 833 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id);
961 close_clientlist[i].last_pinged = temp_time; 834 close_clientlist[i].last_pinged = temp_time;
962 } 835 }
963 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ 836 /* if node is good. */
964 { 837 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
965 index[num_nodes] = i; 838 index[num_nodes] = i;
966 ++num_nodes; 839 ++num_nodes;
967 } 840 }
968 } 841 }
969 } 842 }
970 843
971 if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) 844 if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
972 {
973 rand_node = rand() % num_nodes; 845 rand_node = rand() % num_nodes;
974 getnodes(close_clientlist[index[rand_node]].ip_port, 846 getnodes(close_clientlist[index[rand_node]].ip_port,
975 close_clientlist[index[rand_node]].client_id, 847 close_clientlist[index[rand_node]].client_id,
976 self_public_key); 848 self_public_key);
977 close_lastgetnodes = temp_time; 849 close_lastgetnodes = temp_time;
978 } 850 }
979
980} 851}
981 852
982
983
984
985
986
987void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) 853void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
988{ 854{
989 getnodes(ip_port, public_key, self_public_key); 855 getnodes(ip_port, public_key, self_public_key);
990} 856}
991 857
992
993
994/* send the given packet to node with client_id 858/* send the given packet to node with client_id
995 returns -1 if failure */ 859 returns -1 if failure */
996int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) 860int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
997{ 861{
998 uint32_t i; 862 uint32_t i;
999 for(i = 0; i < LCLIENT_LIST; ++i) 863 for(i = 0; i < LCLIENT_LIST; ++i) {
1000 { 864 if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
1001 if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0)
1002 {
1003 return sendpacket(close_clientlist[i].ip_port, packet, length); 865 return sendpacket(close_clientlist[i].ip_port, packet, length);
1004 } 866 }
1005 } 867 }
1006 return -1; 868 return -1;
1007} 869}
1008 870
1009
1010/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist 871/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist
1011 ip_portlist must be at least MAX_FRIEND_CLIENTS big 872 ip_portlist must be at least MAX_FRIEND_CLIENTS big
1012 returns the number of ips returned 873 returns the number of ips returned
@@ -1017,18 +878,15 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
1017 int num_ips = 0; 878 int num_ips = 0;
1018 uint32_t i; 879 uint32_t i;
1019 uint32_t temp_time = unix_time(); 880 uint32_t temp_time = unix_time();
1020 if(friend_num >= num_friends) 881 if(friend_num >= num_friends) {
1021 {
1022 return -1; 882 return -1;
1023 } 883 }
1024 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) 884 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i)
1025 { 885 {
886 /*If ip is not zero and node is good */
1026 if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && 887 if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 &&
1027 friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) 888 friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
1028 /*If ip is not zero and node is good */ 889 if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) {
1029 {
1030 if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 )
1031 {
1032 return 0; 890 return 0;
1033 } 891 }
1034 ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; 892 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)
1038 return num_ips; 896 return num_ips;
1039} 897}
1040 898
1041
1042/* Send the following packet to everyone who tells us they are connected to friend_id 899/* Send the following packet to everyone who tells us they are connected to friend_id
1043 returns the number of nodes it sent the packet to */ 900 returns the number of nodes it sent the packet to */
1044int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) 901int 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)
1046 uint32_t i, j; 903 uint32_t i, j;
1047 uint32_t sent = 0; 904 uint32_t sent = 0;
1048 uint32_t temp_time = unix_time(); 905 uint32_t temp_time = unix_time();
1049 for(i = 0; i < num_friends; ++i) 906 for(i = 0; i < num_friends; ++i) {
1050 { 907 /* Equal */
1051 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ 908 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) {
1052 { 909 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
1053 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 910 /*If ip is not zero and node is good */
1054 {
1055 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && 911 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 &&
1056 friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) 912 friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
1057 /*If ip is not zero and node is good */ 913 if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) {
1058 {
1059 if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length)
1060 {
1061 ++sent; 914 ++sent;
1062 } 915 }
1063 } 916 }
@@ -1073,8 +926,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1073int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) 926int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1074{ 927{
1075 int num = friend_number(friend_id); 928 int num = friend_number(friend_id);
1076 if(num == -1) 929 if(num == -1) {
1077 {
1078 return 0; 930 return 0;
1079 } 931 }
1080 932
@@ -1082,22 +934,18 @@ int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1082 int n = 0; 934 int n = 0;
1083 uint32_t i; 935 uint32_t i;
1084 uint32_t temp_time = unix_time(); 936 uint32_t temp_time = unix_time();
1085 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) 937 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1086 { 938 /*If ip is not zero and node is good */
1087 if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && 939 if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 &&
1088 friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) 940 friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
1089 /*If ip is not zero and node is good */
1090 {
1091 ip_list[n] = friends_list[num].client_list[i].ip_port; 941 ip_list[n] = friends_list[num].client_list[i].ip_port;
1092 ++n; 942 ++n;
1093 } 943 }
1094 } 944 }
1095 if(n < 1) 945 if(n < 1) {
1096 {
1097 return 0; 946 return 0;
1098 } 947 }
1099 if(sendpacket(ip_list[rand() % n], packet, length) == length) 948 if(sendpacket(ip_list[rand() % n], packet, length) == length) {
1100 {
1101 return 1; 949 return 1;
1102 } 950 }
1103 return 0; 951 return 0;
@@ -1112,10 +960,9 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
1112{ 960{
1113 961
1114 uint32_t i; 962 uint32_t i;
1115 for(i = 0; i < num_friends; ++i) 963 for(i = 0; i < num_friends; ++i) {
1116 { 964 /* Equal */
1117 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ 965 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) {
1118 {
1119 return friend_iplist(ip_portlist, i); 966 return friend_iplist(ip_portlist, i);
1120 } 967 }
1121 } 968 }
@@ -1132,60 +979,49 @@ int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
1132 979
1133 uint8_t packet[MAX_DATA_SIZE]; 980 uint8_t packet[MAX_DATA_SIZE];
1134 int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ 981 int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */
1135 if(len == -1) 982 if(len == -1) {
1136 {
1137 return -1; 983 return -1;
1138 } 984 }
1139 int num = 0; 985 int num = 0;
1140 if(type == 0) 986 if(type == 0) {
1141 {
1142 num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ 987 num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/
1143 } 988 }
1144 else if(type == 1) 989 else if(type == 1) {
1145 {
1146 num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ 990 num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/
1147 } 991 }
1148 if(num == 0) 992 if(num == 0) {
1149 {
1150 return -1; 993 return -1;
1151 } 994 }
1152 return num; 995 return num;
1153} 996}
1154 997
1155
1156/* Handle a recieved ping request for */ 998/* Handle a recieved ping request for */
1157int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) 999int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
1158{ 1000{
1159 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && 1001 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
1160 length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 1002 length > MAX_DATA_SIZE + ENCRYPTION_PADDING) {
1161 {
1162 return 1; 1003 return 1;
1163 } 1004 }
1164 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us. 1005 /* check if request is for us. */
1165 { 1006 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
1166 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 1007 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
1167 uint8_t data[MAX_DATA_SIZE]; 1008 uint8_t data[MAX_DATA_SIZE];
1168 int len = handle_request(public_key, data, packet, length); 1009 int len = handle_request(public_key, data, packet, length);
1169 if(len != sizeof(uint64_t) + 1) 1010 if(len != sizeof(uint64_t) + 1) {
1170 {
1171 return 1; 1011 return 1;
1172 } 1012 }
1173 uint64_t ping_id; 1013 uint64_t ping_id;
1174 memcpy(&ping_id, data + 1, sizeof(uint64_t)); 1014 memcpy(&ping_id, data + 1, sizeof(uint64_t));
1175 1015
1176 int friendnumber = friend_number(public_key); 1016 int friendnumber = friend_number(public_key);
1177 if(friendnumber == -1) 1017 if(friendnumber == -1) {
1178 {
1179 return 1; 1018 return 1;
1180 } 1019 }
1181 1020
1182 if(data[0] == 0) 1021 if(data[0] == 0) {
1183 {
1184 send_NATping(public_key, ping_id, 1);/*1 is reply*/ 1022 send_NATping(public_key, ping_id, 1);/*1 is reply*/
1185 return 0; 1023 return 0;
1186 } 1024 } else if (data[0] == 1) {
1187 else if (data[0] == 1)
1188 {
1189 if(friends_list[friendnumber].NATping_id == ping_id) 1025 if(friends_list[friendnumber].NATping_id == ping_id)
1190 { 1026 {
1191 friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); 1027 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)
1195 } 1031 }
1196 return 1; 1032 return 1;
1197 } 1033 }
1198 else//if request is not for us, try routing it. 1034 /* if request is not for us, try routing it. */
1199 { 1035 else {
1200 if(route_packet(packet + 1, packet, length) == length) 1036 if(route_packet(packet + 1, packet, length) == length) {
1201 {
1202 return 0; 1037 return 0;
1203 } 1038 }
1204 } 1039 }
@@ -1212,24 +1047,19 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
1212static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) 1047static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num)
1213{ 1048{
1214 IP zero = {{0}}; 1049 IP zero = {{0}};
1215 if(len > MAX_FRIEND_CLIENTS) 1050 if(len > MAX_FRIEND_CLIENTS) {
1216 {
1217 return zero; 1051 return zero;
1218 } 1052 }
1219 1053
1220 uint32_t i, j; 1054 uint32_t i, j;
1221 uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; 1055 uint16_t numbers[MAX_FRIEND_CLIENTS] = {0};
1222 for(i = 0; i < len; ++i) 1056 for(i = 0; i < len; ++i) {
1223 { 1057 for(j = 0; j < len; ++j) {
1224 for(j = 0; j < len; ++j) 1058 if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) {
1225 {
1226 if(ip_portlist[i].ip.i == ip_portlist[j].ip.i)
1227 {
1228 ++numbers[i]; 1059 ++numbers[i];
1229 } 1060 }
1230 } 1061 }
1231 if(numbers[i] >= min_num) 1062 if(numbers[i] >= min_num) {
1232 {
1233 return ip_portlist[i].ip; 1063 return ip_portlist[i].ip;
1234 } 1064 }
1235 } 1065 }
@@ -1244,10 +1074,8 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_
1244{ 1074{
1245 uint32_t i; 1075 uint32_t i;
1246 uint16_t num = 0; 1076 uint16_t num = 0;
1247 for(i = 0; i < len; ++i) 1077 for(i = 0; i < len; ++i) {
1248 { 1078 if(ip_portlist[i].ip.i == ip.i) {
1249 if(ip_portlist[i].ip.i == ip.i)
1250 {
1251 portlist[num] = ntohs(ip_portlist[i].port); 1079 portlist[num] = ntohs(ip_portlist[i].port);
1252 ++num; 1080 ++num;
1253 } 1081 }
@@ -1259,14 +1087,12 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_
1259 1087
1260static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) 1088static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num)
1261{ 1089{
1262 if(numports > MAX_FRIEND_CLIENTS || numports == 0) 1090 if(numports > MAX_FRIEND_CLIENTS || numports == 0) {
1263 {
1264 return; 1091 return;
1265 } 1092 }
1266 uint32_t i; 1093 uint32_t i;
1267 uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; 1094 uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS;
1268 for(i = friends_list[friend_num].punching_index; i != top; i++) 1095 for(i = friends_list[friend_num].punching_index; i != top; i++) {
1269 {
1270 /*TODO: improve port guessing algorithm*/ 1096 /*TODO: improve port guessing algorithm*/
1271 uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); 1097 uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1);
1272 IP_Port pinging = {ip, htons(port)}; 1098 IP_Port pinging = {ip, htons(port)};
@@ -1282,28 +1108,22 @@ static void doNAT()
1282{ 1108{
1283 uint32_t i; 1109 uint32_t i;
1284 uint32_t temp_time = unix_time(); 1110 uint32_t temp_time = unix_time();
1285 for(i = 0; i < num_friends; ++i) 1111 for(i = 0; i < num_friends; ++i) {
1286 {
1287 IP_Port ip_list[MAX_FRIEND_CLIENTS]; 1112 IP_Port ip_list[MAX_FRIEND_CLIENTS];
1288 int num = friend_iplist(ip_list, i); 1113 int num = friend_iplist(ip_list, i);
1289 /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ 1114 /*If we are connected to friend or if friend is not online don't try to hole punch with him*/
1290 if(num < MAX_FRIEND_CLIENTS/2) 1115 if(num < MAX_FRIEND_CLIENTS/2) {
1291 {
1292 continue; 1116 continue;
1293 } 1117 }
1294 if(friends_list[i].hole_punching != 1) 1118 if(friends_list[i].hole_punching != 1) {
1295 { 1119 if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) {
1296 if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time)
1297 {
1298 send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ 1120 send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/
1299 friends_list[i].NATping_timestamp = temp_time; 1121 friends_list[i].NATping_timestamp = temp_time;
1300 } 1122 }
1301 } 1123 }
1302 else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) 1124 else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) {
1303 {
1304 IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); 1125 IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2);
1305 if(ip.i == 0) 1126 if(ip.i == 0) {
1306 {
1307 continue; 1127 continue;
1308 } 1128 }
1309 uint16_t port_list[MAX_FRIEND_CLIENTS]; 1129 uint16_t port_list[MAX_FRIEND_CLIENTS];
@@ -1318,7 +1138,6 @@ static void doNAT()
1318 1138
1319/*END OF NAT PUNCHING FUNCTIONS*/ 1139/*END OF NAT PUNCHING FUNCTIONS*/
1320 1140
1321
1322int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 1141int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
1323{ 1142{
1324 switch (packet[0]) { 1143 switch (packet[0]) {
@@ -1346,7 +1165,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
1346 1165
1347} 1166}
1348 1167
1349
1350void doDHT() 1168void doDHT()
1351{ 1169{
1352 doClose(); 1170 doClose();
@@ -1354,8 +1172,6 @@ void doDHT()
1354 doNAT(); 1172 doNAT();
1355} 1173}
1356 1174
1357
1358
1359/* get the size of the DHT (for saving) */ 1175/* get the size of the DHT (for saving) */
1360uint32_t DHT_size() 1176uint32_t DHT_size()
1361{ 1177{
@@ -1374,12 +1190,10 @@ void DHT_save(uint8_t * data)
1374 return 0 if success */ 1190 return 0 if success */
1375int DHT_load(uint8_t * data, uint32_t size) 1191int DHT_load(uint8_t * data, uint32_t size)
1376{ 1192{
1377 if(size < sizeof(close_clientlist)) 1193 if(size < sizeof(close_clientlist)) {
1378 {
1379 return -1; 1194 return -1;
1380 } 1195 }
1381 if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) 1196 if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) {
1382 {
1383 return -1; 1197 return -1;
1384 } 1198 }
1385 uint32_t i, j; 1199 uint32_t i, j;
@@ -1388,17 +1202,13 @@ int DHT_load(uint8_t * data, uint32_t size)
1388 1202
1389 temp = (size - sizeof(close_clientlist))/sizeof(Friend); 1203 temp = (size - sizeof(close_clientlist))/sizeof(Friend);
1390 1204
1391 if(temp != 0) 1205 if(temp != 0) {
1392 {
1393 Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); 1206 Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist));
1394 1207
1395 for(i = 0; i < temp; ++i) 1208 for(i = 0; i < temp; ++i) {
1396 {
1397 DHT_addfriend(tempfriends_list[i].client_id); 1209 DHT_addfriend(tempfriends_list[i].client_id);
1398 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 1210 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
1399 { 1211 if(tempfriends_list[i].client_list[j].timestamp != 0) {
1400 if(tempfriends_list[i].client_list[j].timestamp != 0)
1401 {
1402 getnodes(tempfriends_list[i].client_list[j].ip_port, 1212 getnodes(tempfriends_list[i].client_list[j].ip_port,
1403 tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); 1213 tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id);
1404 } 1214 }
@@ -1407,10 +1217,8 @@ int DHT_load(uint8_t * data, uint32_t size)
1407 } 1217 }
1408 Client_data * tempclose_clientlist = (Client_data *)data; 1218 Client_data * tempclose_clientlist = (Client_data *)data;
1409 1219
1410 for(i = 0; i < LCLIENT_LIST; ++i) 1220 for(i = 0; i < LCLIENT_LIST; ++i) {
1411 { 1221 if(tempclose_clientlist[i].timestamp != 0) {
1412 if(tempclose_clientlist[i].timestamp != 0)
1413 {
1414 DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); 1222 DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id);
1415 } 1223 }
1416 } 1224 }
@@ -1423,13 +1231,10 @@ int DHT_isconnected()
1423{ 1231{
1424 uint32_t i; 1232 uint32_t i;
1425 uint32_t temp_time = unix_time(); 1233 uint32_t temp_time = unix_time();
1426 for(i = 0; i < LCLIENT_LIST; ++i) 1234 for(i = 0; i < LCLIENT_LIST; ++i) {
1427 { 1235 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
1428 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)
1429 {
1430 return 1; 1236 return 1;
1431 } 1237 }
1432 } 1238 }
1433 return 0; 1239 return 0;
1434} 1240} \ No newline at end of file
1435
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 @@
21 * 21 *
22 */ 22 */
23 23
24
25#ifndef DHT_H 24#ifndef DHT_H
26#define DHT_H 25#define DHT_H
27 26
@@ -37,8 +36,6 @@ extern "C" {
37/* size of the client_id in bytes */ 36/* size of the client_id in bytes */
38#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES 37#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
39 38
40
41
42/* Add a new friend to the friends list 39/* Add a new friend to the friends list
43 client_id must be CLIENT_ID_SIZE bytes long. 40 client_id must be CLIENT_ID_SIZE bytes long.
44 returns 0 if success 41 returns 0 if success
@@ -51,7 +48,6 @@ int DHT_addfriend(uint8_t * client_id);
51 returns 1 if failure (client_id not in friends list) */ 48 returns 1 if failure (client_id not in friends list) */
52int DHT_delfriend(uint8_t * client_id); 49int DHT_delfriend(uint8_t * client_id);
53 50
54
55/* Get ip of friend 51/* Get ip of friend
56 client_id must be CLIENT_ID_SIZE bytes long. 52 client_id must be CLIENT_ID_SIZE bytes long.
57 ip must be 4 bytes long. 53 ip must be 4 bytes long.
@@ -61,7 +57,6 @@ int DHT_delfriend(uint8_t * client_id);
61 returns ip of 1 if friend is not in list. */ 57 returns ip of 1 if friend is not in list. */
62IP_Port DHT_getfriendip(uint8_t * client_id); 58IP_Port DHT_getfriendip(uint8_t * client_id);
63 59
64
65/* Run this function at least a couple times per second (It's the main loop) */ 60/* Run this function at least a couple times per second (It's the main loop) */
66void doDHT(); 61void doDHT();
67 62
@@ -74,8 +69,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
74 Sends a get nodes request to the given node with ip port and public_key */ 69 Sends a get nodes request to the given node with ip port and public_key */
75void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); 70void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key);
76 71
77
78
79/* ROUTING FUNCTIONS */ 72/* ROUTING FUNCTIONS */
80 73
81/* send the given packet to node with client_id 74/* 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);
86 returns the number of nodes it sent the packet to */ 79 returns the number of nodes it sent the packet to */
87int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); 80int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length);
88 81
89
90
91/* NAT PUNCHING FUNCTIONS */ 82/* NAT PUNCHING FUNCTIONS */
92 83
93/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist 84/* 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);
96 returns -1 if no such friend*/ 87 returns -1 if no such friend*/
97int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); 88int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id);
98 89
99
100
101/* SAVE/LOAD functions */ 90/* SAVE/LOAD functions */
102 91
103/* get the size of the DHT (for saving) */ 92/* 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 @@
25 There are a couple of useless variables to get rid of. */ 25 There are a couple of useless variables to get rid of. */
26#include "Lossless_UDP.h" 26#include "Lossless_UDP.h"
27 27
28
29
30/* maximum data packets in sent and receive queues. */ 28/* maximum data packets in sent and receive queues. */
31#define MAX_QUEUE_NUM 16 29#define MAX_QUEUE_NUM 16
32 30
@@ -88,7 +86,6 @@ typedef struct
88 uint8_t timeout; /* connection timeout in seconds. */ 86 uint8_t timeout; /* connection timeout in seconds. */
89}Connection; 87}Connection;
90 88
91
92#define MAX_CONNECTIONS 256 89#define MAX_CONNECTIONS 256
93 90
94static Connection connections[MAX_CONNECTIONS]; 91static Connection connections[MAX_CONNECTIONS];
@@ -117,7 +114,6 @@ int getconnection_id(IP_Port ip_port)
117/* table of random numbers used below. */ 114/* table of random numbers used below. */
118static uint32_t randtable[6][256]; 115static uint32_t randtable[6][256];
119 116
120
121/* generate a handshake_id which depends on the ip_port. 117/* generate a handshake_id which depends on the ip_port.
122 this function will always give one unique handshake_id per ip_port. 118 this function will always give one unique handshake_id per ip_port.
123 TODO: make this better */ 119 TODO: make this better */
@@ -138,6 +134,7 @@ uint32_t handshake_id(IP_Port source)
138 } 134 }
139 return id; 135 return id;
140} 136}
137
141/* change the hnshake id associated with that ip_port 138/* change the hnshake id associated with that ip_port
142 TODO: make this better */ 139 TODO: make this better */
143void change_handshake(IP_Port source) 140void change_handshake(IP_Port source)
@@ -146,7 +143,6 @@ void change_handshake(IP_Port source)
146 randtable[rand][((uint8_t *)&source)[rand]] = random_int(); 143 randtable[rand][((uint8_t *)&source)[rand]] = random_int();
147} 144}
148 145
149
150/* initialize a new connection to ip_port 146/* initialize a new connection to ip_port
151 returns an integer corresponding to the connection id. 147 returns an integer corresponding to the connection id.
152 return -1 if it could not initialize the connection. 148 return -1 if it could not initialize the connection.
@@ -314,6 +310,7 @@ char id_packet(int connection_id)
314 } 310 }
315 return -1; 311 return -1;
316} 312}
313
317/* return 0 if there is no received data in the buffer. 314/* return 0 if there is no received data in the buffer.
318 return length of received packet if successful */ 315 return length of received packet if successful */
319int read_packet(int connection_id, uint8_t * data) 316int read_packet(int connection_id, uint8_t * data)
@@ -353,9 +350,6 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length)
353 return 0; 350 return 0;
354} 351}
355 352
356
357
358
359/* put the packet numbers the we are missing in requested and return the number */ 353/* put the packet numbers the we are missing in requested and return the number */
360uint32_t missing_packets(int connection_id, uint32_t * requested) 354uint32_t missing_packets(int connection_id, uint32_t * requested)
361{ 355{
@@ -386,8 +380,6 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
386/* Packet sending functions 380/* Packet sending functions
387 One per packet type. 381 One per packet type.
388 see docs/Lossless_UDP.txt for more information. */ 382 see docs/Lossless_UDP.txt for more information. */
389
390
391int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) 383int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
392{ 384{
393 uint8_t packet[1 + 4 + 4]; 385 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
402 394
403} 395}
404 396
405
406int send_SYNC(uint32_t connection_id) 397int send_SYNC(uint32_t connection_id)
407{ 398{
408 399
@@ -468,8 +459,6 @@ int send_DATA(uint32_t connection_id)
468 459
469/* END of packet sending functions */ 460/* END of packet sending functions */
470 461
471
472
473/* Packet handling functions 462/* Packet handling functions
474 One to handle each type of packets we receive 463 One to handle each type of packets we receive
475 return 0 if handled correctly, 1 if packet is bad. */ 464 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)
700 689
701/* END of packet handling functions */ 690/* END of packet handling functions */
702 691
703
704int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 692int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
705{ 693{
706 694
@@ -828,6 +816,7 @@ void adjustRates()
828 } 816 }
829 } 817 }
830} 818}
819
831/* Call this function a couple times per second 820/* Call this function a couple times per second
832 It's the main loop. */ 821 It's the main loop. */
833void doLossless_UDP() 822void 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" {
33/* maximum length of the data in the data packets */ 33/* maximum length of the data in the data packets */
34#define MAX_DATA_SIZE 1024 34#define MAX_DATA_SIZE 1024
35 35
36
37
38/* Functions */ 36/* Functions */
39 37
40/* initialize a new connection to ip_port 38/* initialize a new connection to ip_port
@@ -52,7 +50,6 @@ int getconnection_id(IP_Port ip_port);
52 return -1 if there are no new incoming connections in the list. */ 50 return -1 if there are no new incoming connections in the list. */
53int incoming_connection(); 51int incoming_connection();
54 52
55
56/* return -1 if it could not kill the connection. 53/* return -1 if it could not kill the connection.
57 return 0 if killed successfully */ 54 return 0 if killed successfully */
58int kill_connection(int connection_id); 55int kill_connection(int connection_id);
@@ -74,21 +71,16 @@ char id_packet(int connection_id);
74 return length of received packet if successful */ 71 return length of received packet if successful */
75int read_packet(int connection_id, uint8_t * data); 72int read_packet(int connection_id, uint8_t * data);
76 73
77
78/* return 0 if data could not be put in packet queue 74/* return 0 if data could not be put in packet queue
79 return 1 if data was put into the queue */ 75 return 1 if data was put into the queue */
80int write_packet(int connection_id, uint8_t * data, uint32_t length); 76int write_packet(int connection_id, uint8_t * data, uint32_t length);
81 77
82
83
84/* returns the number of packets in the queue waiting to be successfully sent. */ 78/* returns the number of packets in the queue waiting to be successfully sent. */
85uint32_t sendqueue(int connection_id); 79uint32_t sendqueue(int connection_id);
86 80
87
88/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ 81/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
89uint32_t recvqueue(int connection_id); 82uint32_t recvqueue(int connection_id);
90 83
91
92/* check if connection is connected 84/* check if connection is connected
93 return 0 no. 85 return 0 no.
94 return 1 if attempting handshake 86 return 1 if attempting handshake
@@ -97,7 +89,6 @@ uint32_t recvqueue(int connection_id);
97 return 4 if timed out and wating to be killed */ 89 return 4 if timed out and wating to be killed */
98int is_connected(int connection_id); 90int is_connected(int connection_id);
99 91
100
101/* Call this function a couple times per second 92/* Call this function a couple times per second
102 It's the main loop. */ 93 It's the main loop. */
103void doLossless_UDP(); 94void 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
38 uint8_t userstatus_sent; 38 uint8_t userstatus_sent;
39 uint16_t info_size; /* length of the info */ 39 uint16_t info_size; /* length of the info */
40}Friend; 40}Friend;
41
42
43 41
44uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 42uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
45 43
@@ -57,7 +55,6 @@ static uint32_t numfriends;
57 0 if we are offline 55 0 if we are offline
58 static uint8_t online; */ 56 static uint8_t online; */
59 57
60
61/* return the friend id associated to that public key. 58/* return the friend id associated to that public key.
62 return -1 if no such friend */ 59 return -1 if no such friend */
63int getfriend_id(uint8_t * client_id) 60int getfriend_id(uint8_t * client_id)
@@ -76,7 +73,6 @@ int getfriend_id(uint8_t * client_id)
76 return -1; 73 return -1;
77} 74}
78 75
79
80/* copies the public key associated to that friend id into client_id buffer. 76/* copies the public key associated to that friend id into client_id buffer.
81 make sure that client_id is of size CLIENT_ID_SIZE. 77 make sure that client_id is of size CLIENT_ID_SIZE.
82 return 0 if success 78 return 0 if success
@@ -96,7 +92,6 @@ int getclient_id(int friend_id, uint8_t * client_id)
96 return -1; 92 return -1;
97} 93}
98 94
99
100/* add a friend 95/* add a friend
101 set the data that will be sent along with friend request 96 set the data that will be sent along with friend request
102 client_id is the client id of the friend 97 client_id is the client id of the friend
@@ -191,7 +186,6 @@ int m_delfriend(int friendnumber)
191 return 0; 186 return 0;
192} 187}
193 188
194
195/* return 4 if friend is online 189/* return 4 if friend is online
196 return 3 if friend is confirmed 190 return 3 if friend is confirmed
197 return 2 if the friend request was sent 191 return 2 if the friend request was sent
@@ -206,7 +200,6 @@ int m_friendstatus(int friendnumber)
206 return friendlist[friendnumber].status; 200 return friendlist[friendnumber].status;
207} 201}
208 202
209
210/* send a text chat message to an online friend 203/* send a text chat message to an online friend
211 return 1 if packet was successfully put into the send queue 204 return 1 if packet was successfully put into the send queue
212 return 0 if it was not */ 205 return 0 if it was not */
@@ -239,7 +232,6 @@ static int m_sendname(int friendnumber, uint8_t * name)
239/* set the name of a friend 232/* set the name of a friend
240 return 0 if success 233 return 0 if success
241 return -1 if failure */ 234 return -1 if failure */
242
243static int setfriendname(int friendnumber, uint8_t * name) 235static int setfriendname(int friendnumber, uint8_t * name)
244{ 236{
245 if(friendnumber >= numfriends || friendnumber < 0) 237 if(friendnumber >= numfriends || friendnumber < 0)
@@ -250,7 +242,6 @@ static int setfriendname(int friendnumber, uint8_t * name)
250 return 0; 242 return 0;
251} 243}
252 244
253
254/* Set our nickname 245/* Set our nickname
255 name must be a string of maximum MAX_NAME_LENGTH length. 246 name must be a string of maximum MAX_NAME_LENGTH length.
256 return 0 if success 247 return 0 if success
@@ -352,6 +343,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
352 friendlist[friendnumber].userstatus_length = length; 343 friendlist[friendnumber].userstatus_length = length;
353 return 0; 344 return 0;
354} 345}
346
355/* 347/*
356static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 348static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
357static uint8_t friend_request_isset = 0; 349static uint8_t friend_request_isset = 0;
@@ -362,7 +354,6 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
362 callback_friendrequest(function); 354 callback_friendrequest(function);
363} 355}
364 356
365
366static void (*friend_message)(int, uint8_t *, uint16_t); 357static void (*friend_message)(int, uint8_t *, uint16_t);
367static uint8_t friend_message_isset = 0; 358static uint8_t friend_message_isset = 0;
368 359
@@ -373,7 +364,6 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
373 friend_message_isset = 1; 364 friend_message_isset = 1;
374} 365}
375 366
376
377static void (*friend_namechange)(int, uint8_t *, uint16_t); 367static void (*friend_namechange)(int, uint8_t *, uint16_t);
378static uint8_t friend_namechange_isset = 0; 368static uint8_t friend_namechange_isset = 0;
379void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) 369void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
@@ -520,8 +510,6 @@ static void doFriends()
520 } 510 }
521} 511}
522 512
523
524
525static void doInbound() 513static void doInbound()
526{ 514{
527 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 515 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 @@
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 * 23 *
24 */ 24 */
25 25
26
27#ifndef MESSENGER_H 26#ifndef MESSENGER_H
28#define MESSENGER_H 27#define MESSENGER_H
29 28
@@ -79,7 +78,6 @@ int m_delfriend(int friendnumber);
79 return 0 if there is no friend with that number */ 78 return 0 if there is no friend with that number */
80int m_friendstatus(int friendnumber); 79int m_friendstatus(int friendnumber);
81 80
82
83/* send a text chat message to an online friend 81/* send a text chat message to an online friend
84 returns 1 if packet was successfully put into the send queue 82 returns 1 if packet was successfully put into the send queue
85 return 0 if it was not */ 83 return 0 if it was not */
@@ -91,7 +89,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length);
91 return -1 if failure */ 89 return -1 if failure */
92int setname(uint8_t * name, uint16_t length); 90int setname(uint8_t * name, uint16_t length);
93 91
94
95/* get name of friendnumber 92/* get name of friendnumber
96 put it in name 93 put it in name
97 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 94 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);
117 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 114 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
118void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); 115void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
119 116
120
121/* set the function that will be executed when a message from a friend is received. 117/* set the function that will be executed when a message from a friend is received.
122 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ 118 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
123void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); 119void 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));
137 returns -1 if there are problems */ 133 returns -1 if there are problems */
138int initMessenger(); 134int initMessenger();
139 135
140
141/* the main loop that needs to be run at least 200 times per second */ 136/* the main loop that needs to be run at least 200 times per second */
142void doMessenger(); 137void doMessenger();
143 138
144
145/* SAVING AND LOADING FUNCTIONS: */ 139/* SAVING AND LOADING FUNCTIONS: */
146 140
147/* returns the size of the messenger data (for saving) */ 141/* 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 @@
25 25
26uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 26uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
27 27
28
29/* Try to send a friendrequest to peer with public_key 28/* Try to send a friendrequest to peer with public_key
30 data is the data in the request and length is the length. 29 data is the data in the request and length is the length.
31 return -1 if failure. 30 return -1 if failure.
@@ -61,7 +60,6 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
61 return num; 60 return num;
62} 61}
63 62
64
65static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); 63static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
66static uint8_t handle_friendrequest_isset = 0; 64static uint8_t handle_friendrequest_isset = 0;
67 65
@@ -72,7 +70,6 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
72 handle_friendrequest_isset = 1; 70 handle_friendrequest_isset = 1;
73} 71}
74 72
75
76int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 73int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
77{ 74{
78 75
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 @@
21 * 21 *
22 */ 22 */
23 23
24
25#ifndef FRIEND_REQUESTS_H 24#ifndef FRIEND_REQUESTS_H
26#define FRIEND_REQUESTS_H 25#define FRIEND_REQUESTS_H
27 26
28
29#include "DHT.h" 27#include "DHT.h"
30#include "net_crypto.h" 28#include "net_crypto.h"
31 29
@@ -37,7 +35,6 @@ extern "C" {
37 data is the data in the request and length is the length. */ 35 data is the data in the request and length is the length. */
38int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); 36int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length);
39 37
40
41/* set the function that will be executed when a friend request for us is received. 38/* set the function that will be executed when a friend request for us is received.
42 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 39 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
43void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); 40void 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));
47 return 1 if it didn't handle the packet or if the packet was shit. */ 44 return 1 if it didn't handle the packet or if the packet was shit. */
48int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); 45int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
49 46
50
51
52#ifdef __cplusplus 47#ifdef __cplusplus
53} 48}
54#endif 49#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 @@
26 26
27#include "net_crypto.h" 27#include "net_crypto.h"
28 28
29
30/* Our public and secret keys. */ 29/* Our public and secret keys. */
31uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 30uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
32uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 31uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
33 32
34
35typedef struct 33typedef struct
36{ 34{
37 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ 35 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)
177 return -1; 175 return -1;
178} 176}
179 177
180
181/* return 0 if data could not be put in packet queue 178/* return 0 if data could not be put in packet queue
182 return 1 if data was put into the queue */ 179 return 1 if data was put into the queue */
183int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) 180int 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
267 } 264 }
268} 265}
269 266
270
271/* Send a crypto handshake packet containing an encrypted secret nonce and session public key 267/* Send a crypto handshake packet containing an encrypted secret nonce and session public key
272 to peer with connection_id and public_key 268 to peer with connection_id and public_key
273 the packet is encrypted with a random nonce which is sent in plain text with the packet */ 269 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,
327 return 1; 323 return 1;
328} 324}
329 325
330
331
332
333/* get crypto connection id from public key of peer 326/* get crypto connection id from public key of peer
334 return -1 if there are no connections like we are looking for 327 return -1 if there are no connections like we are looking for
335 return id if it found it */ 328 return id if it found it */
@@ -349,7 +342,6 @@ int getcryptconnection_id(uint8_t * public_key)
349 return -1; 342 return -1;
350} 343}
351 344
352
353/* Start a secure connection with other peer who has public_key and ip_port 345/* Start a secure connection with other peer who has public_key and ip_port
354 returns -1 if failure 346 returns -1 if failure
355 returns crypt_connection_id of the initialized connection if everything went well. */ 347 returns crypt_connection_id of the initialized connection if everything went well. */
@@ -447,7 +439,6 @@ int crypto_kill(int crypt_connection_id)
447 return 1; 439 return 1;
448} 440}
449 441
450
451/* accept an incoming connection using the parameters provided by crypto_inbound 442/* accept an incoming connection using the parameters provided by crypto_inbound
452 return -1 if not successful 443 return -1 if not successful
453 returns the crypt_connection_id if successful */ 444 returns the crypt_connection_id if successful */
@@ -505,7 +496,6 @@ int is_cryptoconnected(int crypt_connection_id)
505 return 0; 496 return 0;
506} 497}
507 498
508
509/* Generate our public and private keys 499/* Generate our public and private keys
510 Only call this function the first time the program starts. */ 500 Only call this function the first time the program starts. */
511void new_keys() 501void new_keys()
@@ -681,4 +671,4 @@ void doNetCrypto()
681 handle_incomings(); 671 handle_incomings();
682 receive_crypto(); 672 receive_crypto();
683 killTimedout(); 673 killTimedout();
684} 674} \ 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,
55/* fill the given nonce with random bytes. */ 55/* fill the given nonce with random bytes. */
56void random_nonce(uint8_t * nonce); 56void random_nonce(uint8_t * nonce);
57 57
58
59/* return 0 if there is no received data in the buffer 58/* return 0 if there is no received data in the buffer
60 return -1 if the packet was discarded. 59 return -1 if the packet was discarded.
61 return length of received data if successful */ 60 return length of received data if successful */
62int read_cryptpacket(int crypt_connection_id, uint8_t * data); 61int read_cryptpacket(int crypt_connection_id, uint8_t * data);
63 62
64
65/* return 0 if data could not be put in packet queue 63/* return 0 if data could not be put in packet queue
66 return 1 if data was put into the queue */ 64 return 1 if data was put into the queue */
67int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); 65int 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);
74 returns the length of the created packet on success */ 72 returns the length of the created packet on success */
75int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); 73int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id);
76 74
77
78/* puts the senders public key in the request in public_key, the data from the request 75/* puts the senders public key in the request in public_key, the data from the request
79 in data if a friend or ping request was sent to us and returns the length of the data. 76 in data if a friend or ping request was sent to us and returns the length of the data.
80 packet is the request packet and length is its length 77 packet is the request packet and length is its length
81 return -1 if not valid request. */ 78 return -1 if not valid request. */
82int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); 79int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length);
83 80
84
85/* Start a secure connection with other peer who has public_key and ip_port 81/* Start a secure connection with other peer who has public_key and ip_port
86 returns -1 if failure 82 returns -1 if failure
87 returns crypt_connection_id of the initialized connection if everything went well. */ 83 returns crypt_connection_id of the initialized connection if everything went well. */
88int crypto_connect(uint8_t * public_key, IP_Port ip_port); 84int crypto_connect(uint8_t * public_key, IP_Port ip_port);
89 85
90
91/* kill a crypto connection 86/* kill a crypto connection
92 return 0 if killed successfully 87 return 0 if killed successfully
93 return 1 if there was a problem. */ 88 return 1 if there was a problem. */
@@ -102,7 +97,6 @@ int crypto_kill(int crypt_connection_id);
102 to refuse it just call kill_connection(...) on the connection id */ 97 to refuse it just call kill_connection(...) on the connection id */
103int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); 98int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
104 99
105
106/* accept an incoming connection using the parameters provided by crypto_inbound 100/* accept an incoming connection using the parameters provided by crypto_inbound
107 return -1 if not successful 101 return -1 if not successful
108 returns the crypt_connection_id if successful */ 102 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 @@
23 23
24#include "network.h" 24#include "network.h"
25 25
26
27/* returns current UNIX time in microseconds (us). */ 26/* returns current UNIX time in microseconds (us). */
28uint64_t current_time() 27uint64_t current_time()
29{ 28{
@@ -128,7 +127,6 @@ int init_networking(IP ip, uint16_t port)
128 return -1; 127 return -1;
129 #endif 128 #endif
130 129
131
132 /* Functions to increase the size of the send and receive UDP buffers 130 /* Functions to increase the size of the send and receive UDP buffers
133 NOTE: uncomment if necessary */ 131 NOTE: uncomment if necessary */
134 /* 132 /*
@@ -146,7 +144,6 @@ int init_networking(IP ip, uint16_t port)
146 /*Enable broadcast on socket*/ 144 /*Enable broadcast on socket*/
147 int broadcast = 1; 145 int broadcast = 1;
148 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); 146 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast));
149
150 147
151 /* Set socket nonblocking */ 148 /* Set socket nonblocking */
152 #ifdef WIN32 149 #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 @@
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23
24 23
25#ifndef NETWORK_H 24#ifndef NETWORK_H
26#define NETWORK_H 25#define NETWORK_H
@@ -31,8 +30,6 @@
31#include <string.h> 30#include <string.h>
32#include <time.h> 31#include <time.h>
33 32
34
35
36#ifdef WIN32 /* Put win32 includes here */ 33#ifdef WIN32 /* Put win32 includes here */
37//Windows XP 34//Windows XP
38#define WINVER 0x0501 35#define WINVER 0x0501
@@ -97,7 +94,6 @@ typedef struct
97 #endif 94 #endif
98}ADDR; 95}ADDR;
99 96
100
101/* returns current time in milleseconds since the epoch. */ 97/* returns current time in milleseconds since the epoch. */
102uint64_t current_time(); 98uint64_t current_time();
103 99
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 @@
41 41
42#include <string.h> 42#include <string.h>
43 43
44//Sleep function (x = milliseconds) 44/* Sleep function (x = milliseconds) */
45#ifdef WIN32 45#ifdef WIN32
46 46
47#define c_sleep(x) Sleep(1*x) 47#define c_sleep(x) Sleep(1*x)
@@ -60,16 +60,14 @@ void printip(IP_Port ip_port)
60 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)); 60 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));
61} 61}
62 62
63 63/* horrible function from one of my first C programs.
64//horrible function from one of my first C programs. 64 *only here because I was too lazy to write a proper one. */
65//only here because I was too lazy to write a proper one.
66unsigned char * hex_string_to_bin(char hex_string[]) 65unsigned char * hex_string_to_bin(char hex_string[])
67{ 66{
68 unsigned char * val = malloc(strlen(hex_string)); 67 unsigned char * val = malloc(strlen(hex_string));
69 char * pos = hex_string; 68 char * pos = hex_string;
70 int i=0; 69 int i=0;
71 while(i < strlen(hex_string)) 70 while(i < strlen(hex_string)) {
72 {
73 sscanf(pos,"%2hhx",&val[i]); 71 sscanf(pos,"%2hhx",&val[i]);
74 pos+=2; 72 pos+=2;
75 i++; 73 i++;
@@ -88,8 +86,7 @@ int main(int argc, char *argv[])
88 new_keys(); 86 new_keys();
89 printf("OUR ID: "); 87 printf("OUR ID: ");
90 uint32_t i; 88 uint32_t i;
91 for(i = 0; i < 32; i++) 89 for(i = 0; i < 32; i++) {
92 {
93 if(self_public_key[i] < 16) 90 if(self_public_key[i] < 16)
94 printf("0"); 91 printf("0");
95 printf("%hhX",self_public_key[i]); 92 printf("%hhX",self_public_key[i]);
@@ -105,8 +102,7 @@ int main(int argc, char *argv[])
105 uint8_t friend_id[32]; 102 uint8_t friend_id[32];
106 memcpy(friend_id, hex_string_to_bin(temp_id), 32); 103 memcpy(friend_id, hex_string_to_bin(temp_id), 32);
107 104
108 105 /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */
109 //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
110 106
111 107
112 DHT_addfriend(friend_id); 108 DHT_addfriend(friend_id);
@@ -118,15 +114,13 @@ int main(int argc, char *argv[])
118 int friendrequest = -1; 114 int friendrequest = -1;
119 uint8_t request_data[512]; 115 uint8_t request_data[512];
120 116
121 //initialize networking 117 /* initialize networking
122 //bind to ip 0.0.0.0:PORT 118 * bind to ip 0.0.0.0:PORT */
123 IP ip; 119 IP ip;
124 ip.i = 0; 120 ip.i = 0;
125 init_networking(ip, PORT); 121 init_networking(ip, PORT);
126 initNetCrypto(); 122 initNetCrypto();
127 123
128
129
130 perror("Initialization"); 124 perror("Initialization");
131 IP_Port bootstrap_ip_port; 125 IP_Port bootstrap_ip_port;
132 bootstrap_ip_port.port = htons(atoi(argv[2])); 126 bootstrap_ip_port.port = htons(atoi(argv[2]));
@@ -147,122 +141,99 @@ int main(int argc, char *argv[])
147 if ( file2==NULL ){return 1;} 141 if ( file2==NULL ){return 1;}
148 read1 = fread(buffer1, 1, 128, file1); 142 read1 = fread(buffer1, 1, 128, file1);
149 143
150 while(1) 144 while(1) {
151 { 145 while(receivepacket(&ip_port, data, &length) != -1) {
152 146 if(rand() % 3 != 1) { /* simulate packet loss */
153 while(receivepacket(&ip_port, data, &length) != -1) 147 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
154 { 148 /* if packet is not recognized */
155 if(rand() % 3 != 1)//simulate packet loss
156 {
157 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
158 {
159 //if packet is not recognized
160 printf("Received unhandled packet with length: %u\n", length); 149 printf("Received unhandled packet with length: %u\n", length);
161 } 150 } else {
162 else
163 {
164 printf("Received handled packet with length: %u\n", length); 151 printf("Received handled packet with length: %u\n", length);
165 } 152 }
166 } 153 }
167 } 154 }
168 friend_ip = DHT_getfriendip(friend_id); 155 friend_ip = DHT_getfriendip(friend_id);
169 if(friend_ip.ip.i != 0) 156 if(friend_ip.ip.i != 0) {
170 { 157 if(connection == -1 && friendrequest == -1) {
171 if(connection == -1 && friendrequest == -1)
172 {
173 printf("Sending friend request to peer:"); 158 printf("Sending friend request to peer:");
174 printip(friend_ip); 159 printip(friend_ip);
175 friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); 160 friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12);
176 //connection = crypto_connect((uint8_t *)friend_id, friend_ip); 161 /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
177 //connection = new_connection(friend_ip); 162 /* connection = new_connection(friend_ip); */
178 } 163 }
179 if(check_friendrequest(friendrequest) == 1) 164 if(check_friendrequest(friendrequest) == 1) {
180 {
181 printf("Started connecting to friend:"); 165 printf("Started connecting to friend:");
182 connection = crypto_connect(friend_id, friend_ip); 166 connection = crypto_connect(friend_id, friend_ip);
183 } 167 }
184 } 168 }
185 if(inconnection == -1) 169 if(inconnection == -1) {
186 {
187 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 170 uint8_t secret_nonce[crypto_box_NONCEBYTES];
188 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 171 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
189 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 172 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
190 inconnection = crypto_inbound(public_key, secret_nonce, session_key); 173 inconnection = crypto_inbound(public_key, secret_nonce, session_key);
191 inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); 174 inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
192 //inconnection = incoming_connection(); 175 /* inconnection = incoming_connection(); */
193 if(inconnection != -1) 176 if(inconnection != -1) {
194 {
195 printf("Someone connected to us:\n"); 177 printf("Someone connected to us:\n");
196 // printip(connection_ip(inconnection)); 178 /* printip(connection_ip(inconnection)); */
197 } 179 }
198 } 180 }
199 if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) 181 if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
200 {
201 printf("RECIEVED FRIEND REQUEST: %s\n", request_data); 182 printf("RECIEVED FRIEND REQUEST: %s\n", request_data);
202 } 183 }
203 184
204 //if someone connected to us write what he sends to a file 185 /* if someone connected to us write what he sends to a file
205 //also send him our file. 186 * also send him our file. */
206 if(inconnection != -1) 187 if(inconnection != -1) {
207 { 188 if(write_cryptpacket(inconnection, buffer1, read1)) {
208 if(write_cryptpacket(inconnection, buffer1, read1))
209 {
210 printf("Wrote data1.\n"); 189 printf("Wrote data1.\n");
211 read1 = fread(buffer1, 1, 128, file1); 190 read1 = fread(buffer1, 1, 128, file1);
212 } 191 }
213 read2 = read_cryptpacket(inconnection, buffer2); 192 read2 = read_cryptpacket(inconnection, buffer2);
214 if(read2 != 0) 193 if(read2 != 0) {
215 {
216 printf("Received data1.\n"); 194 printf("Received data1.\n");
217 if(!fwrite(buffer2, read2, 1, file2)) 195 if(!fwrite(buffer2, read2, 1, file2)) {
218 {
219 printf("file write error1\n"); 196 printf("file write error1\n");
220 } 197 }
221 if(read2 < 128) 198 if(read2 < 128) {
222 {
223 printf("Closed file1 %u\n", read2); 199 printf("Closed file1 %u\n", read2);
224 fclose(file2); 200 fclose(file2);
225 } 201 }
226 } 202 }
227 else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out. 203 /* if buffer is empty and the connection timed out. */
228 { 204 else if(is_cryptoconnected(inconnection) == 4) {
229 crypto_kill(inconnection); 205 crypto_kill(inconnection);
230 } 206 }
231 } 207 }
232 //if we are connected to a friend send him data from the file. 208 /* if we are connected to a friend send him data from the file.
233 //also put what he sends us in a file. 209 * also put what he sends us in a file. */
234 if(is_cryptoconnected(connection) >= 3) 210 if(is_cryptoconnected(connection) >= 3) {
235 { 211 if(write_cryptpacket(0, buffer1, read1)) {
236 if(write_cryptpacket(0, buffer1, read1))
237 {
238 printf("Wrote data2.\n"); 212 printf("Wrote data2.\n");
239 read1 = fread(buffer1, 1, 128, file1); 213 read1 = fread(buffer1, 1, 128, file1);
240 } 214 }
241 read2 = read_cryptpacket(0, buffer2); 215 read2 = read_cryptpacket(0, buffer2);
242 if(read2 != 0) 216 if(read2 != 0) {
243 {
244 printf("Received data2.\n"); 217 printf("Received data2.\n");
245 if(!fwrite(buffer2, read2, 1, file2)) 218 if(!fwrite(buffer2, read2, 1, file2)) {
246 {
247 printf("file write error2\n"); 219 printf("file write error2\n");
248 } 220 }
249 if(read2 < 128) 221 if(read2 < 128) {
250 {
251 printf("Closed file2 %u\n", read2); 222 printf("Closed file2 %u\n", read2);
252 fclose(file2); 223 fclose(file2);
253 } 224 }
254 } 225 }
255 else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out. 226 /* if buffer is empty and the connection timed out. */
256 { 227 else if(is_cryptoconnected(connection) == 4) {
257 crypto_kill(connection); 228 crypto_kill(connection);
258 } 229 }
259 } 230 }
260 doDHT(); 231 doDHT();
261 doLossless_UDP(); 232 doLossless_UDP();
262 doNetCrypto(); 233 doNetCrypto();
263 //print_clientlist(); 234 /*print_clientlist();
264 //print_friendlist(); 235 *print_friendlist();
265 //c_sleep(300); 236 *c_sleep(300); */
266 c_sleep(1); 237 c_sleep(1);
267 } 238 }
268 239
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 @@
33 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 33 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
34 * 34 *
35 */ 35 */
36
36#include "../core/network.h" 37#include "../core/network.h"
37#include "../core/DHT.h" 38#include "../core/DHT.h"
38#include "../core/Lossless_UDP.h" 39#include "../core/Lossless_UDP.h"
@@ -100,68 +101,53 @@ int main(int argc, char *argv[])
100 if ( file2==NULL ){return 1;} 101 if ( file2==NULL ){return 1;}
101 read1 = fread(buffer1, 1, 128, file1); 102 read1 = fread(buffer1, 1, 128, file1);
102 103
103 while(1) 104 while(1) {
104 {
105 105
106 while(receivepacket(&ip_port, data, &length) != -1) 106 while(receivepacket(&ip_port, data, &length) != -1) {
107 { 107 if(rand() % 3 != 1) { /* simulate packet loss */
108 if(rand() % 3 != 1)//simulate packet loss 108 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
109 { 109 /* if packet is not recognized */
110 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
111 {
112 //if packet is not recognized
113 printf("Received unhandled packet with length: %u\n", length); 110 printf("Received unhandled packet with length: %u\n", length);
114 } 111 } else {
115 else
116 {
117 printf("Received handled packet with length: %u\n", length); 112 printf("Received handled packet with length: %u\n", length);
118 } 113 }
119 } 114 }
120 } 115 }
121 friend_ip = DHT_getfriendip((uint8_t *)argv[3]); 116 friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
122 if(friend_ip.ip.i != 0) 117 if(friend_ip.ip.i != 0) {
123 { 118 if(connection == -1) {
124 if(connection == -1)
125 {
126 printf("Started connecting to friend:"); 119 printf("Started connecting to friend:");
127 printip(friend_ip); 120 printip(friend_ip);
128 connection = new_connection(friend_ip); 121 connection = new_connection(friend_ip);
129 } 122 }
130 } 123 }
131 if(inconnection == -1) 124 if(inconnection == -1) {
132 {
133 inconnection = incoming_connection(); 125 inconnection = incoming_connection();
134 if(inconnection != -1) 126 if(inconnection != -1) {
135 {
136 printf("Someone connected to us:"); 127 printf("Someone connected to us:");
137 printip(connection_ip(inconnection)); 128 printip(connection_ip(inconnection));
138 } 129 }
139 } 130 }
140 //if someone connected to us write what he sends to a file 131 /* if someone connected to us write what he sends to a file */
141 //also send him our file. 132 /* also send him our file. */
142 if(inconnection != -1) 133 if(inconnection != -1) {
143 { 134 if(write_packet(inconnection, buffer1, read1)) {
144 if(write_packet(inconnection, buffer1, read1))
145 {
146 printf("Wrote data.\n"); 135 printf("Wrote data.\n");
147 read1 = fread(buffer1, 1, 128, file1); 136 read1 = fread(buffer1, 1, 128, file1);
148 } 137 }
149 read2 = read_packet(inconnection, buffer2); 138 read2 = read_packet(inconnection, buffer2);
150 if(read2 != 0) 139 if(read2 != 0) {
151 {
152 printf("Received data.\n"); 140 printf("Received data.\n");
153 if(!fwrite(buffer2, read2, 1, file2)) 141 if(!fwrite(buffer2, read2, 1, file2)) {
154 {
155 printf("file write error\n"); 142 printf("file write error\n");
156 } 143 }
157 if(read2 < 128) 144 if(read2 < 128) {
158 {
159 fclose(file2); 145 fclose(file2);
160 } 146 }
161 } 147 }
162 } 148 }
163 //if we are connected to a friend send him data from the file. 149 /* if we are connected to a friend send him data from the file.
164 //also put what he sends us in a file. 150 * also put what he sends us in a file. */
165 if(is_connected(connection) == 3) 151 if(is_connected(connection) == 3)
166 { 152 {
167 if(write_packet(0, buffer1, read1)) 153 if(write_packet(0, buffer1, read1))
@@ -185,9 +171,9 @@ int main(int argc, char *argv[])
185 } 171 }
186 doDHT(); 172 doDHT();
187 doLossless_UDP(); 173 doLossless_UDP();
188 //print_clientlist(); 174 /* print_clientlist();
189 //print_friendlist(); 175 * print_friendlist();
190 //c_sleep(300); 176 * c_sleep(300); */
191 c_sleep(1); 177 c_sleep(1);
192 } 178 }
193 179
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 @@
47 47
48#define PORT 33445 48#define PORT 33445
49 49
50
51void print_clientlist() 50void print_clientlist()
52{ 51{
53 uint32_t i, j; 52 uint32_t i, j;
54 IP_Port p_ip; 53 IP_Port p_ip;
55 printf("___________________CLOSE________________________________\n"); 54 printf("___________________CLOSE________________________________\n");
56 for(i = 0; i < 4; i++) 55 for(i = 0; i < 4; i++) {
57 {
58 printf("ClientID: "); 56 printf("ClientID: ");
59 for(j = 0; j < 32; j++) 57 for(j = 0; j < 32; j++) {
60 {
61 printf("%c", close_clientlist[i].client_id[j]); 58 printf("%c", close_clientlist[i].client_id[j]);
62 } 59 }
63 p_ip = close_clientlist[i].ip_port; 60 p_ip = close_clientlist[i].ip_port;
@@ -75,12 +72,10 @@ void print_friendlist()
75 uint32_t i, j, k; 72 uint32_t i, j, k;
76 IP_Port p_ip; 73 IP_Port p_ip;
77 printf("_________________FRIENDS__________________________________\n"); 74 printf("_________________FRIENDS__________________________________\n");
78 for(k = 0; k < num_friends; k++) 75 for(k = 0; k < num_friends; k++) {
79 {
80 printf("FRIEND %u\n", k); 76 printf("FRIEND %u\n", k);
81 printf("ID: "); 77 printf("ID: ");
82 for(j = 0; j < 32; j++) 78 for(j = 0; j < 32; j++) {
83 {
84 printf("%c", friends_list[k].client_id[j]); 79 printf("%c", friends_list[k].client_id[j]);
85 } 80 }
86 p_ip = DHT_getfriendip(friends_list[k].client_id); 81 p_ip = DHT_getfriendip(friends_list[k].client_id);
@@ -88,11 +83,9 @@ void print_friendlist()
88 83
89 printf("\nCLIENTS IN LIST:\n\n"); 84 printf("\nCLIENTS IN LIST:\n\n");
90 85
91 for(i = 0; i < 4; i++) 86 for(i = 0; i < 4; i++) {
92 {
93 printf("ClientID: "); 87 printf("ClientID: ");
94 for(j = 0; j < 32; j++) 88 for(j = 0; j < 32; j++) {
95 {
96 if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16) 89 if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16)
97 printf("0"); 90 printf("0");
98 printf("%hhX", friends_list[k].client_list[i].client_id[j]); 91 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)
113 uint32_t i; 106 uint32_t i;
114 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 107 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
115 printf("--------------------BEGIN-----------------------------\n"); 108 printf("--------------------BEGIN-----------------------------\n");
116 for(i = 0; i < length; i++) 109 for(i = 0; i < length; i++) {
117 {
118 if(data[i] < 16) 110 if(data[i] < 16)
119 printf("0"); 111 printf("0");
120 printf("%hhX",data[i]); 112 printf("%hhX",data[i]);
@@ -129,8 +121,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
129 unsigned char * val = malloc(strlen(hex_string)); 121 unsigned char * val = malloc(strlen(hex_string));
130 char * pos = hex_string; 122 char * pos = hex_string;
131 int i=0; 123 int i=0;
132 while(i < strlen(hex_string)) 124 while(i < strlen(hex_string)) {
133 {
134 sscanf(pos,"%2hhx",&val[i]); 125 sscanf(pos,"%2hhx",&val[i]);
135 pos+=2; 126 pos+=2;
136 i++; 127 i++;
@@ -149,8 +140,7 @@ int main(int argc, char *argv[])
149 new_keys(); 140 new_keys();
150 printf("OUR ID: "); 141 printf("OUR ID: ");
151 uint32_t i; 142 uint32_t i;
152 for(i = 0; i < 32; i++) 143 for(i = 0; i < 32; i++) {
153 {
154 if(self_public_key[i] < 16) 144 if(self_public_key[i] < 16)
155 printf("0"); 145 printf("0");
156 printf("%hhX",self_public_key[i]); 146 printf("%hhX",self_public_key[i]);
@@ -161,8 +151,8 @@ int main(int argc, char *argv[])
161 scanf("%s", temp_id); 151 scanf("%s", temp_id);
162 DHT_addfriend(hex_string_to_bin(temp_id)); 152 DHT_addfriend(hex_string_to_bin(temp_id));
163 153
164 //initialize networking 154 /* initialize networking */
165 //bind to ip 0.0.0.0:PORT 155 /* bind to ip 0.0.0.0:PORT */
166 IP ip; 156 IP ip;
167 ip.i = 0; 157 ip.i = 0;
168 init_networking(ip, PORT); 158 init_networking(ip, PORT);
@@ -171,10 +161,10 @@ int main(int argc, char *argv[])
171 perror("Initialization"); 161 perror("Initialization");
172 IP_Port bootstrap_ip_port; 162 IP_Port bootstrap_ip_port;
173 bootstrap_ip_port.port = htons(atoi(argv[2])); 163 bootstrap_ip_port.port = htons(atoi(argv[2]));
174 //bootstrap_ip_port.ip.c[0] = 127; 164 /* bootstrap_ip_port.ip.c[0] = 127;
175 //bootstrap_ip_port.ip.c[1] = 0; 165 * bootstrap_ip_port.ip.c[1] = 0;
176 //bootstrap_ip_port.ip.c[2] = 0; 166 * bootstrap_ip_port.ip.c[2] = 0;
177 //bootstrap_ip_port.ip.c[3] = 1; 167 * bootstrap_ip_port.ip.c[3] = 1; */
178 bootstrap_ip_port.ip.i = inet_addr(argv[1]); 168 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
179 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 169 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
180 170
@@ -182,20 +172,15 @@ int main(int argc, char *argv[])
182 uint8_t data[MAX_UDP_PACKET_SIZE]; 172 uint8_t data[MAX_UDP_PACKET_SIZE];
183 uint32_t length; 173 uint32_t length;
184 174
185 while(1) 175 while(1) {
186 {
187 176
188 doDHT(); 177 doDHT();
189 178
190 while(receivepacket(&ip_port, data, &length) != -1) 179 while(receivepacket(&ip_port, data, &length) != -1) {
191 { 180 if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
192 if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port))
193 {
194 //unhandled packet 181 //unhandled packet
195 printpacket(data, length, ip_port); 182 printpacket(data, length, ip_port);
196 } 183 } else {
197 else
198 {
199 printf("Received handled packet with length: %u\n", length); 184 printf("Received handled packet with length: %u\n", length);
200 } 185 }
201 } 186 }
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 @@
42 42
43#endif 43#endif
44 44
45
46#define PORT 33446 45#define PORT 33446
47 46
48void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) 47void 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)
50 uint32_t i; 49 uint32_t i;
51 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 50 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
52 printf("--------------------BEGIN-----------------------------\n"); 51 printf("--------------------BEGIN-----------------------------\n");
53 for(i = 0; i < length; i++) 52 for(i = 0; i < length; i++) {
54 {
55 if(data[i] < 16) 53 if(data[i] < 16)
56 printf("0"); 54 printf("0");
57 printf("%hhX",data[i]); 55 printf("%hhX",data[i]);
@@ -117,39 +115,34 @@ void printconnection(int connection_id)
117 115
118} 116}
119*/ 117*/
120//recieve packets and send them to the packethandler 118
121//run doLossless_UDP(); 119/*( recieve packets and send them to the packethandler */
120/*run doLossless_UDP(); */
122void Lossless_UDP() 121void Lossless_UDP()
123{ 122{
124 IP_Port ip_port; 123 IP_Port ip_port;
125 uint8_t data[MAX_UDP_PACKET_SIZE]; 124 uint8_t data[MAX_UDP_PACKET_SIZE];
126 uint32_t length; 125 uint32_t length;
127 while(receivepacket(&ip_port, data, &length) != -1) 126 while(receivepacket(&ip_port, data, &length) != -1) {
128 {
129 printf("packet with length: %u\n", length); 127 printf("packet with length: %u\n", length);
130 //if(rand() % 3 != 1)//add packet loss 128 /* if(rand() % 3 != 1)//add packet loss
131 // { 129 { */
132 if(LosslessUDP_handlepacket(data, length, ip_port)) 130 if(LosslessUDP_handlepacket(data, length, ip_port)) {
133 {
134 printpacket(data, length, ip_port); 131 printpacket(data, length, ip_port);
135 } 132 } else {
136 else
137 {
138 //printconnection(0); 133 //printconnection(0);
139 printf("Received handled packet with length: %u\n", length); 134 printf("Received handled packet with length: %u\n", length);
140 } 135 }
141 // } 136 /* } */
142 } 137 }
143 138
144 doLossless_UDP(); 139 doLossless_UDP();
145 140
146} 141}
147 142
148
149int main(int argc, char *argv[]) 143int main(int argc, char *argv[])
150{ 144{
151 if (argc < 4) 145 if (argc < 4) {
152 {
153 printf("usage: %s ip port filename\n", argv[0]); 146 printf("usage: %s ip port filename\n", argv[0]);
154 exit(0); 147 exit(0);
155 } 148 }
@@ -161,8 +154,8 @@ int main(int argc, char *argv[])
161 if ( file==NULL ){return 1;} 154 if ( file==NULL ){return 1;}
162 155
163 156
164 //initialize networking 157 /* initialize networking */
165 //bind to ip 0.0.0.0:PORT 158 /* bind to ip 0.0.0.0:PORT */
166 IP ip; 159 IP ip;
167 ip.i = 0; 160 ip.i = 0;
168 init_networking(ip, PORT); 161 init_networking(ip, PORT);
@@ -173,17 +166,14 @@ int main(int argc, char *argv[])
173 printip(serverip); 166 printip(serverip);
174 int connection = new_connection(serverip); 167 int connection = new_connection(serverip);
175 uint64_t timer = current_time(); 168 uint64_t timer = current_time();
176 while(1) 169 while(1) {
177 { 170 /* printconnection(connection); */
178 // printconnection(connection);
179 Lossless_UDP(); 171 Lossless_UDP();
180 if(is_connected(connection) == 3) 172 if(is_connected(connection) == 3) {
181 {
182 printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); 173 printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
183 break; 174 break;
184 } 175 }
185 if(is_connected(connection) == 0) 176 if(is_connected(connection) == 0) {
186 {
187 printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); 177 printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
188 return 1; 178 return 1;
189 } 179 }
@@ -192,38 +182,32 @@ int main(int argc, char *argv[])
192 timer = current_time(); 182 timer = current_time();
193 183
194 184
195 //read first part of file 185 /*read first part of file */
196 read = fread(buffer, 1, 512, file); 186 read = fread(buffer, 1, 512, file);
197 187
198 while(1) 188 while(1) {
199 { 189 /* printconnection(connection); */
200 //printconnection(connection);
201 Lossless_UDP(); 190 Lossless_UDP();
202 if(is_connected(connection) == 3) 191 if(is_connected(connection) == 3) {
203 {
204 192
205 if(write_packet(connection, buffer, read)) 193 if(write_packet(connection, buffer, read)) {
206 { 194 /* printf("Wrote data.\n"); */
207 //printf("Wrote data.\n");
208 read = fread(buffer, 1, 512, file); 195 read = fread(buffer, 1, 512, file);
209 196
210 } 197 }
211 //printf("%u\n", sendqueue(connection)); 198 /* printf("%u\n", sendqueue(connection)); */
212 if(sendqueue(connection) == 0) 199 if(sendqueue(connection) == 0) {
213 { 200 if(read == 0) {
214 if(read == 0)
215 {
216 printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer)); 201 printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
217 break; 202 break;
218 } 203 }
219 } 204 }
220 } 205 }
221 else 206 else {
222 {
223 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); 207 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
224 return 0; 208 return 0;
225 } 209 }
226 //c_sleep(1); 210 /* c_sleep(1); */
227 } 211 }
228 212
229 return 0; 213 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 @@
43 43
44#endif 44#endif
45 45
46
47#define PORT 33445 46#define PORT 33445
48 47
49void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) 48void 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)
51 uint32_t i; 50 uint32_t i;
52 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 51 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
53 printf("--------------------BEGIN-----------------------------\n"); 52 printf("--------------------BEGIN-----------------------------\n");
54 for(i = 0; i < length; i++) 53 for(i = 0; i < length; i++) {
55 {
56 if(data[i] < 16) 54 if(data[i] < 16)
57 printf("0"); 55 printf("0");
58 printf("%hhX",data[i]); 56 printf("%hhX",data[i]);
59 } 57 }
60 printf("\n--------------------END-----------------------------\n\n\n"); 58 printf("\n--------------------END-----------------------------\n\n\n");
61} 59}
60
62/* 61/*
63void printpackets(Data test) 62void printpackets(Data test)
64{ 63{
@@ -113,23 +112,20 @@ void printconnection(int connection_id)
113 112
114} 113}
115*/ 114*/
116//recieve packets and send them to the packethandler 115
117//run doLossless_UDP(); 116/* recieve packets and send them to the packethandler
117 * run doLossless_UDP(); */
118void Lossless_UDP() 118void Lossless_UDP()
119{ 119{
120 IP_Port ip_port; 120 IP_Port ip_port;
121 uint8_t data[MAX_UDP_PACKET_SIZE]; 121 uint8_t data[MAX_UDP_PACKET_SIZE];
122 uint32_t length; 122 uint32_t length;
123 while(receivepacket(&ip_port, data, &length) != -1) 123 while(receivepacket(&ip_port, data, &length) != -1) {
124 {
125 //if(rand() % 3 != 1)//add packet loss 124 //if(rand() % 3 != 1)//add packet loss
126 //{ 125 //{
127 if(LosslessUDP_handlepacket(data, length, ip_port)) 126 if(LosslessUDP_handlepacket(data, length, ip_port)) {
128 {
129 printpacket(data, length, ip_port); 127 printpacket(data, length, ip_port);
130 } 128 } else {
131 else
132 {
133 //printconnection(0); 129 //printconnection(0);
134 printf("Received handled packet with length: %u\n", length); 130 printf("Received handled packet with length: %u\n", length);
135 } 131 }
@@ -137,14 +133,12 @@ void Lossless_UDP()
137 } 133 }
138 134
139 doLossless_UDP(); 135 doLossless_UDP();
140
141} 136}
142 137
143 138
144int main(int argc, char *argv[]) 139int main(int argc, char *argv[])
145{ 140{
146 if (argc < 2) 141 if (argc < 2) {
147 {
148 printf("usage: %s filename\n", argv[0]); 142 printf("usage: %s filename\n", argv[0]);
149 exit(0); 143 exit(0);
150 } 144 }
@@ -167,14 +161,11 @@ int main(int argc, char *argv[])
167 uint64_t timer = current_time(); 161 uint64_t timer = current_time();
168 162
169 163
170 while(1) 164 while(1) {
171 {
172 Lossless_UDP(); 165 Lossless_UDP();
173 connection = incoming_connection(); 166 connection = incoming_connection();
174 if(connection != -1) 167 if(connection != -1) {
175 { 168 if(is_connected(connection) == 2) {
176 if(is_connected(connection) == 2)
177 {
178 printf("Recieved the connection.\n"); 169 printf("Recieved the connection.\n");
179 170
180 } 171 }
@@ -185,25 +176,20 @@ int main(int argc, char *argv[])
185 176
186 timer = current_time(); 177 timer = current_time();
187 178
188 while(1) 179 while(1) {
189 {
190 //printconnection(0); 180 //printconnection(0);
191 Lossless_UDP(); 181 Lossless_UDP();
192 if(is_connected(connection) >= 2) 182 if(is_connected(connection) >= 2) {
193 {
194 kill_connection_in(connection, 3000000); 183 kill_connection_in(connection, 3000000);
195 read = read_packet(connection, buffer); 184 read = read_packet(connection, buffer);
196 if(read != 0) 185 if(read != 0) {
197 {
198 // printf("Recieved data.\n"); 186 // printf("Recieved data.\n");
199 if(!fwrite(buffer, read, 1, file)) 187 if(!fwrite(buffer, read, 1, file)) {
200 {
201 printf("file write error\n"); 188 printf("file write error\n");
202 } 189 }
203 } 190 }
204 } 191 }
205 if(is_connected(connection) == 4) 192 if(is_connected(connection) == 4) {
206 {
207 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); 193 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
208 fclose(file); 194 fclose(file);
209 return 1; 195 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[])
104 exit(0); 104 exit(0);
105 } 105 }
106 initMessenger(); 106 initMessenger();
107 if(argc > 3) 107 if(argc > 3) {
108 {
109 IP_Port bootstrap_ip_port; 108 IP_Port bootstrap_ip_port;
110 bootstrap_ip_port.port = htons(atoi(argv[2])); 109 bootstrap_ip_port.port = htons(atoi(argv[2]));
111 bootstrap_ip_port.ip.i = inet_addr(argv[1]); 110 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
112 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 111 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
113 } 112 } else {
114 else
115 {
116 FILE *file = fopen(argv[1], "rb"); 113 FILE *file = fopen(argv[1], "rb");
117 if ( file==NULL ){return 1;} 114 if ( file==NULL ){return 1;}
118 int read; 115 int read;
@@ -127,8 +124,7 @@ int main(int argc, char *argv[])
127 124
128 printf("OUR ID: "); 125 printf("OUR ID: ");
129 uint32_t i; 126 uint32_t i;
130 for(i = 0; i < 32; i++) 127 for(i = 0; i < 32; i++) {
131 {
132 if(self_public_key[i] < 16) 128 if(self_public_key[i] < 16)
133 printf("0"); 129 printf("0");
134 printf("%hhX",self_public_key[i]); 130 printf("%hhX",self_public_key[i]);
@@ -138,16 +134,14 @@ int main(int argc, char *argv[])
138 134
139 char temp_id[128]; 135 char temp_id[128];
140 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); 136 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
141 if(scanf("%s", temp_id) != 1) 137 if(scanf("%s", temp_id) != 1) {
142 {
143 return 1; 138 return 1;
144 } 139 }
145 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 140 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
146 141
147 perror("Initialization"); 142 perror("Initialization");
148 143
149 while(1) 144 while(1) {
150 {
151 uint8_t name[128]; 145 uint8_t name[128];
152 getname(num, name); 146 getname(num, name);
153 printf("%s\n", name); 147 printf("%s\n", name);
@@ -162,6 +156,5 @@ int main(int argc, char *argv[])
162 fwrite(buffer, 1, Messenger_size(), file); 156 fwrite(buffer, 1, Messenger_size(), file);
163 free(buffer); 157 free(buffer);
164 fclose(file); 158 fclose(file);
165 } 159 }
166
167} 160}
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[])
51 unsigned char * val = malloc(strlen(hex_string)); 51 unsigned char * val = malloc(strlen(hex_string));
52 char * pos = hex_string; 52 char * pos = hex_string;
53 int i=0; 53 int i=0;
54 while(i < strlen(hex_string)) 54 while(i < strlen(hex_string)) {
55 {
56 sscanf(pos,"%2hhx",&val[i]); 55 sscanf(pos,"%2hhx",&val[i]);
57 pos+=2; 56 pos+=2;
58 i++; 57 i++;
@@ -190,6 +189,7 @@ void do_refresh()
190 clrtoeol(); 189 clrtoeol();
191 refresh(); 190 refresh();
192} 191}
192
193void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) 193void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
194{ 194{
195 new_lines("[i] received friend request"); 195 new_lines("[i] received friend request");
@@ -205,6 +205,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
205 new_lines(numchar); 205 new_lines(numchar);
206 } 206 }
207} 207}
208
208void print_message(int friendnumber, uint8_t * string, uint16_t length) 209void print_message(int friendnumber, uint8_t * string, uint16_t length)
209{ 210{
210 char name[MAX_NAME_LENGTH]; 211 char name[MAX_NAME_LENGTH];
@@ -220,6 +221,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
220 sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this 221 sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this
221 new_lines(msg); 222 new_lines(msg);
222} 223}
224
223void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { 225void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
224 char name[MAX_NAME_LENGTH]; 226 char name[MAX_NAME_LENGTH];
225 getname(friendnumber, (uint8_t*)name); 227 getname(friendnumber, (uint8_t*)name);
@@ -227,6 +229,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
227 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); 229 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
228 new_lines(msg); 230 new_lines(msg);
229} 231}
232
230void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { 233void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
231 char name[MAX_NAME_LENGTH]; 234 char name[MAX_NAME_LENGTH];
232 getname(friendnumber, (uint8_t*)name); 235 getname(friendnumber, (uint8_t*)name);
@@ -234,6 +237,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
234 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); 237 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
235 new_lines(msg); 238 new_lines(msg);
236} 239}
240
237void load_key(){ 241void load_key(){
238 FILE *data_file = NULL; 242 FILE *data_file = NULL;
239 if ((data_file = fopen("data","r"))) { 243 if ((data_file = fopen("data","r"))) {
@@ -260,6 +264,7 @@ void load_key(){
260 } 264 }
261 fclose(data_file); 265 fclose(data_file);
262} 266}
267
263int main(int argc, char *argv[]) 268int main(int argc, char *argv[])
264{ 269{
265 if (argc < 4) { 270 if (argc < 4) {
@@ -317,9 +322,7 @@ int main(int argc, char *argv[])
317 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 322 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
318 nodelay(stdscr, TRUE); 323 nodelay(stdscr, TRUE);
319 while(true) { 324 while(true) {
320 325 if (on == 0 && DHT_isconnected()) {
321 if (on == 0 && DHT_isconnected())
322 {
323 new_lines("[i] connected to DHT\n[i] define username with /n"); 326 new_lines("[i] connected to DHT\n[i] define username with /n");
324 on = 1; 327 on = 1;
325 } 328 }
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 @@
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23
23#ifndef NTOX_H 24#ifndef NTOX_H
24#define NTOX_H 25#define NTOX_H
25 26