summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c367
-rw-r--r--core/DHT.h20
-rw-r--r--core/LAN_discovery.c20
-rw-r--r--core/LAN_discovery.h2
-rw-r--r--core/Lossless_UDP.c174
-rw-r--r--core/Lossless_UDP.h6
-rw-r--r--core/Messenger.c168
-rw-r--r--core/Messenger.h20
-rw-r--r--core/friend_requests.c50
-rw-r--r--core/friend_requests.h4
-rw-r--r--core/net_crypto.c176
-rw-r--r--core/net_crypto.h24
-rw-r--r--core/network.c13
-rw-r--r--core/network.h21
14 files changed, 484 insertions, 581 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 857ac5e8..9c2245c7 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -23,8 +23,7 @@
23 23
24#include "DHT.h" 24#include "DHT.h"
25 25
26typedef struct 26typedef struct {
27{
28 uint8_t client_id[CLIENT_ID_SIZE]; 27 uint8_t client_id[CLIENT_ID_SIZE];
29 IP_Port ip_port; 28 IP_Port ip_port;
30 uint32_t timestamp; 29 uint32_t timestamp;
@@ -32,13 +31,12 @@ typedef struct
32 IP_Port ret_ip_port;/* The ip_port returned by this node for the friend 31 IP_Port ret_ip_port;/* The ip_port returned by this node for the friend
33 (for nodes in friends_list) or us (for nodes in close_clientlist) */ 32 (for nodes in friends_list) or us (for nodes in close_clientlist) */
34 uint32_t ret_timestamp; 33 uint32_t ret_timestamp;
35}Client_data; 34} Client_data;
36 35
37/* maximum number of clients stored per friend. */ 36/* maximum number of clients stored per friend. */
38#define MAX_FRIEND_CLIENTS 8 37#define MAX_FRIEND_CLIENTS 8
39 38
40typedef struct 39typedef struct {
41{
42 uint8_t client_id[CLIENT_ID_SIZE]; 40 uint8_t client_id[CLIENT_ID_SIZE];
43 Client_data client_list[MAX_FRIEND_CLIENTS]; 41 Client_data client_list[MAX_FRIEND_CLIENTS];
44 uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ 42 uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
@@ -49,21 +47,19 @@ typedef struct
49 uint32_t punching_timestamp; 47 uint32_t punching_timestamp;
50 uint64_t NATping_id; 48 uint64_t NATping_id;
51 uint32_t NATping_timestamp; 49 uint32_t NATping_timestamp;
52}Friend; 50} Friend;
53 51
54typedef struct 52typedef struct {
55{
56 uint8_t client_id[CLIENT_ID_SIZE]; 53 uint8_t client_id[CLIENT_ID_SIZE];
57 IP_Port ip_port; 54 IP_Port ip_port;
58}Node_format; 55} Node_format;
59 56
60typedef struct 57typedef struct {
61{
62 IP_Port ip_port; 58 IP_Port ip_port;
63 uint64_t ping_id; 59 uint64_t ping_id;
64 uint32_t timestamp; 60 uint32_t timestamp;
65 61
66}Pinged; 62} Pinged;
67 63
68/* Our client id/public key */ 64/* Our client id/public key */
69uint8_t self_public_key[CLIENT_ID_SIZE]; 65uint8_t self_public_key[CLIENT_ID_SIZE];
@@ -74,7 +70,7 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
74#define LCLIENT_LIST 32 70#define LCLIENT_LIST 32
75static Client_data close_clientlist[LCLIENT_LIST]; 71static Client_data close_clientlist[LCLIENT_LIST];
76 72
77static Friend * friends_list; 73static Friend *friends_list;
78static uint16_t num_friends; 74static uint16_t num_friends;
79 75
80/* The list of ip ports along with the ping_id of what we sent them and a timestamp */ 76/* The list of ip ports along with the ping_id of what we sent them and a timestamp */
@@ -90,19 +86,16 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
90 return 0 if both are same distance 86 return 0 if both are same distance
91 return 1 if client_id1 is closer 87 return 1 if client_id1 is closer
92 return 2 if client_id2 is closer */ 88 return 2 if client_id2 is closer */
93int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ 89int id_closest(uint8_t *client_id, uint8_t *client_id1, uint8_t *client_id2) /* tested */
94{ 90{
95 uint32_t i; 91 uint32_t i;
96 for(i = 0; i < CLIENT_ID_SIZE; ++i) { 92 for (i = 0; i < CLIENT_ID_SIZE; ++i) {
97 if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) { 93 if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i]))
98 return 1; 94 return 1;
99 } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) { 95 else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i]))
100 return 2; 96 return 2;
101 }
102 } 97 }
103
104 return 0; 98 return 0;
105
106} 99}
107 100
108/* check if client with client_id is already in list of length length. 101/* check if client with client_id is already in list of length length.
@@ -110,15 +103,16 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)
110 if the id is already in the list with a different ip_port, update it. 103 if the id is already in the list with a different ip_port, update it.
111 return True(1) or False(0) 104 return True(1) or False(0)
112 TODO: maybe optimize this. */ 105 TODO: maybe optimize this. */
113int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) 106int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
114{ 107{
115 uint32_t i; 108 uint32_t i;
116 uint32_t temp_time = unix_time(); 109 uint32_t temp_time = unix_time();
117 110
118 for(i = 0; i < length; ++i) { 111 for (i = 0; i < length; ++i) {
119 /*If ip_port is assigned to a different client_id replace it*/ 112 /*If ip_port is assigned to a different client_id replace it*/
120 if(list[i].ip_port.ip.i == ip_port.ip.i && 113 if(list[i].ip_port.ip.i == ip_port.ip.i &&
121 list[i].ip_port.port == ip_port.port) { 114 list[i].ip_port.port == ip_port.port)
115 {
122 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 116 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
123 } 117 }
124 118
@@ -136,14 +130,12 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
136 130
137/* check if client with client_id is already in node format list of length length. 131/* check if client with client_id is already in node format list of length length.
138 return True(1) or False(0) */ 132 return True(1) or False(0) */
139int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) 133int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id)
140{ 134{
141 uint32_t i; 135 uint32_t i;
142 for(i = 0; i < length; ++i) { 136 for (i = 0; i < length; ++i) {
143 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { 137 if (memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
144
145 return 1; 138 return 1;
146 }
147 } 139 }
148 return 0; 140 return 0;
149 141
@@ -151,14 +143,12 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
151 143
152/*Return the friend number from the client_id 144/*Return the friend number from the client_id
153 Return -1 if failure, number of friend if success*/ 145 Return -1 if failure, number of friend if success*/
154static int friend_number(uint8_t * client_id) 146static int friend_number(uint8_t *client_id)
155{ 147{
156 uint32_t i; 148 uint32_t i;
157 for(i = 0; i < num_friends; ++i) { 149 for (i = 0; i < num_friends; ++i) {
158 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ 150 if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
159 {
160 return i; 151 return i;
161 }
162 } 152 }
163 return -1; 153 return -1;
164} 154}
@@ -171,14 +161,15 @@ static int friend_number(uint8_t * client_id)
171/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: 161/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
172 put them in the nodes_list and return how many were found. 162 put them in the nodes_list and return how many were found.
173 TODO: Make this function much more efficient. */ 163 TODO: Make this function much more efficient. */
174int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) 164int get_close_nodes(uint8_t *client_id, Node_format * nodes_list)
175{ 165{
176 uint32_t i, j, k; 166 uint32_t i, j, k;
177 int num_nodes=0; 167 int num_nodes=0;
178 uint32_t temp_time = unix_time(); 168 uint32_t temp_time = unix_time();
179 for(i = 0; i < LCLIENT_LIST; ++i) { 169 for (i = 0; i < LCLIENT_LIST; ++i) {
180 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && 170 if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
181 !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { 171 !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id))
172 {
182 /* if node is good and not already in list. */ 173 /* if node is good and not already in list. */
183 if(num_nodes < MAX_SENT_NODES) { 174 if(num_nodes < MAX_SENT_NODES) {
184 memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); 175 memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
@@ -194,12 +185,12 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
194 } 185 }
195 } 186 }
196 } 187 }
197 for(i = 0; i < num_friends; ++i) { 188 for (i = 0; i < num_friends; ++i) {
198 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 189 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
199 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && 190 if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
200 !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { 191 !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) {
201 /* if node is good and not already in list. */ 192 /* if node is good and not already in list. */
202 if(num_nodes < MAX_SENT_NODES) { 193 if (num_nodes < MAX_SENT_NODES) {
203 memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); 194 memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
204 nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; 195 nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port;
205 num_nodes++; 196 num_nodes++;
@@ -220,12 +211,12 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
220/* replace first bad (or empty) node with this one 211/* replace first bad (or empty) node with this one
221 return 0 if successful 212 return 0 if successful
222 return 1 if not (list contains no bad nodes) */ 213 return 1 if not (list contains no bad nodes) */
223int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */ 214int replace_bad(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) /* tested */
224{ 215{
225 uint32_t i; 216 uint32_t i;
226 uint32_t temp_time = unix_time(); 217 uint32_t temp_time = unix_time();
227 for(i = 0; i < length; ++i) { 218 for (i = 0; i < length; ++i) {
228 if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { 219 if (list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ {
229 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 220 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
230 list[i].ip_port = ip_port; 221 list[i].ip_port = ip_port;
231 list[i].timestamp = temp_time; 222 list[i].timestamp = temp_time;
@@ -240,13 +231,13 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
240} 231}
241 232
242/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ 233/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
243int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) 234int replace_good(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port, uint8_t *comp_client_id)
244{ 235{
245 uint32_t i; 236 uint32_t i;
246 uint32_t temp_time = unix_time(); 237 uint32_t temp_time = unix_time();
247 238
248 for(i = 0; i < length; ++i) { 239 for (i = 0; i < length; ++i) {
249 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { 240 if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
250 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 241 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
251 list[i].ip_port = ip_port; 242 list[i].ip_port = ip_port;
252 list[i].timestamp = temp_time; 243 list[i].timestamp = temp_time;
@@ -261,17 +252,14 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po
261} 252}
262 253
263/* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ 254/* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */
264void addto_lists(IP_Port ip_port, uint8_t * client_id) 255void addto_lists(IP_Port ip_port, uint8_t *client_id)
265{ 256{
266 uint32_t i; 257 uint32_t i;
267 258
268 /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ 259 /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */
269 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 260 if (!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
270 261 if (replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port))
271 if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 262 replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); /* 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 */
273 replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key);
274 }
275 } 263 }
276 for(i = 0; i < num_friends; ++i) { 264 for(i = 0; i < num_friends; ++i) {
277 if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { 265 if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) {
@@ -285,30 +273,31 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
285 273
286/* If client_id is a friend or us, update ret_ip_port 274/* If client_id is a friend or us, update ret_ip_port
287 nodeclient_id is the id of the node that sent us this info */ 275 nodeclient_id is the id of the node that sent us this info */
288void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id) 276void returnedip_ports(IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id)
289{ 277{
290 uint32_t i, j; 278 uint32_t i, j;
291 uint32_t temp_time = unix_time(); 279 uint32_t temp_time = unix_time();
292 if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { 280 if (memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) {
293 for(i = 0; i < LCLIENT_LIST; ++i) { 281 for (i = 0; i < LCLIENT_LIST; ++i) {
294 if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { 282 if (memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
295 close_clientlist[i].ret_ip_port = ip_port; 283 close_clientlist[i].ret_ip_port = ip_port;
296 close_clientlist[i].ret_timestamp = temp_time; 284 close_clientlist[i].ret_timestamp = temp_time;
297 return; 285 return;
298 } 286 }
299 } 287 }
300 } 288 }
301 else 289 else {
302 for(i = 0; i < num_friends; ++i) { 290 for (i = 0; i < num_friends; ++i) {
303 if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { 291 if (memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) {
304 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 292 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
305 if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { 293 if (memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) {
306 friends_list[i].client_list[j].ret_ip_port = ip_port; 294 friends_list[i].client_list[j].ret_ip_port = ip_port;
307 friends_list[i].client_list[j].ret_timestamp = temp_time; 295 friends_list[i].client_list[j].ret_timestamp = temp_time;
308 return; 296 return;
309 } 297 }
310 } 298 }
311 } 299 }
300 }
312 } 301 }
313} 302}
314 303
@@ -326,25 +315,23 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
326 uint8_t pinging; 315 uint8_t pinging;
327 uint32_t temp_time = unix_time(); 316 uint32_t temp_time = unix_time();
328 317
329 for(i = 0; i < LPING_ARRAY; ++i ) { 318 for (i = 0; i < LPING_ARRAY; ++i ) {
330 if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { 319 if ((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
331 pinging = 0; 320 pinging = 0;
332 if(ip_port.ip.i != 0) { 321 if (ip_port.ip.i != 0) {
333 if(pings[i].ip_port.ip.i == ip_port.ip.i && 322 if(pings[i].ip_port.ip.i == ip_port.ip.i &&
334 pings[i].ip_port.port == ip_port.port) { 323 pings[i].ip_port.port == ip_port.port)
324 {
335 ++pinging; 325 ++pinging;
336 } 326 }
337 } 327 }
338 if(ping_id != 0) { 328 if (ping_id != 0) {
339 if(pings[i].ping_id == ping_id) 329 if(pings[i].ping_id == ping_id)
340 {
341 ++pinging; 330 ++pinging;
342 } 331
343 } 332 }
344 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { 333 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
345 return 1; 334 return 1;
346 }
347
348 } 335 }
349 } 336 }
350 337
@@ -359,24 +346,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
359 uint8_t pinging; 346 uint8_t pinging;
360 uint32_t temp_time = unix_time(); 347 uint32_t temp_time = unix_time();
361 348
362 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { 349 for(i = 0; i < LSEND_NODES_ARRAY; ++i) {
363 if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { 350 if ((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
364 pinging = 0; 351 pinging = 0;
365 if(ip_port.ip.i != 0) { 352 if (ip_port.ip.i != 0) {
366 if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && 353 if (send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
367 send_nodes[i].ip_port.port == ip_port.port) { 354 send_nodes[i].ip_port.port == ip_port.port) {
368 ++pinging; 355 ++pinging;
369 } 356 }
370 } 357 }
371 if(ping_id != 0) { 358 if (ping_id != 0) {
372 if(send_nodes[i].ping_id == ping_id) { 359 if (send_nodes[i].ping_id == ping_id)
373 ++pinging; 360 ++pinging;
374 }
375 } 361 }
376 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { 362 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
377 return 1; 363 return 1;
378 }
379
380 } 364 }
381 } 365 }
382 366
@@ -394,9 +378,9 @@ uint64_t add_pinging(IP_Port ip_port)
394 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 378 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
395 uint32_t temp_time = unix_time(); 379 uint32_t temp_time = unix_time();
396 380
397 for(i = 0; i < PING_TIMEOUT; ++i ) { 381 for (i = 0; i < PING_TIMEOUT; ++i ) {
398 for(j = 0; j < LPING_ARRAY; ++j ) { 382 for (j = 0; j < LPING_ARRAY; ++j ) {
399 if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { 383 if ((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
400 pings[j].timestamp = temp_time; 384 pings[j].timestamp = temp_time;
401 pings[j].ip_port = ip_port; 385 pings[j].ip_port = ip_port;
402 pings[j].ping_id = ping_id; 386 pings[j].ping_id = ping_id;
@@ -415,9 +399,9 @@ uint64_t add_gettingnodes(IP_Port ip_port)
415 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 399 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
416 uint32_t temp_time = unix_time(); 400 uint32_t temp_time = unix_time();
417 401
418 for(i = 0; i < PING_TIMEOUT; ++i ) { 402 for (i = 0; i < PING_TIMEOUT; ++i ) {
419 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { 403 for (j = 0; j < LSEND_NODES_ARRAY; ++j ) {
420 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { 404 if ((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) {
421 send_nodes[j].timestamp = temp_time; 405 send_nodes[j].timestamp = temp_time;
422 send_nodes[j].ip_port = ip_port; 406 send_nodes[j].ip_port = ip_port;
423 send_nodes[j].ping_id = ping_id; 407 send_nodes[j].ping_id = ping_id;
@@ -431,20 +415,17 @@ uint64_t add_gettingnodes(IP_Port ip_port)
431 415
432/* send a ping request 416/* send a ping request
433 Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ 417 Ping request only works if none has been sent to that ip/port in the last 5 seconds. */
434static int pingreq(IP_Port ip_port, uint8_t * public_key) 418static int pingreq(IP_Port ip_port, uint8_t *public_key)
435{ 419{
436 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { 420 if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
437 return 1; 421 return 1;
438 }
439 422
440 if(is_pinging(ip_port, 0)) { 423 if (is_pinging(ip_port, 0))
441 return 1; 424 return 1;
442 }
443 425
444 uint64_t ping_id = add_pinging(ip_port); 426 uint64_t ping_id = add_pinging(ip_port);
445 if(ping_id == 0) { 427 if (ping_id == 0)
446 return 1; 428 return 1;
447 }
448 429
449 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; 430 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
450 uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; 431 uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
@@ -452,9 +433,8 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
452 random_nonce(nonce); 433 random_nonce(nonce);
453 434
454 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); 435 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
455 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { 436 if(len != sizeof(ping_id) + ENCRYPTION_PADDING)
456 return -1; 437 return -1;
457 }
458 data[0] = 0; 438 data[0] = 0;
459 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); 439 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
460 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 440 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
@@ -465,12 +445,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
465} 445}
466 446
467/* send a ping response */ 447/* send a ping response */
468static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) 448static int pingres(IP_Port ip_port, uint8_t *public_key, uint64_t ping_id)
469{ 449{
470 /* check if packet is gonna be sent to ourself */ 450 /* check if packet is gonna be sent to ourself */
471 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { 451 if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)
472 return 1; 452 return 1;
473 }
474 453
475 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; 454 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
476 uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; 455 uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
@@ -478,9 +457,8 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
478 random_nonce(nonce); 457 random_nonce(nonce);
479 458
480 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); 459 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
481 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { 460 if (len != sizeof(ping_id) + ENCRYPTION_PADDING)
482 return -1; 461 return -1;
483 }
484 data[0] = 1; 462 data[0] = 1;
485 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); 463 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
486 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 464 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
@@ -491,22 +469,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
491} 469}
492 470
493/* send a getnodes request */ 471/* send a getnodes request */
494static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) 472static int getnodes(IP_Port ip_port, uint8_t *public_key, uint8_t *client_id)
495{ 473{
496 /* check if packet is gonna be sent to ourself */ 474 /* check if packet is gonna be sent to ourself */
497 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { 475 if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)
498 return 1; 476 return 1;
499 } 477 if (is_gettingnodes(ip_port, 0))
500
501 if(is_gettingnodes(ip_port, 0)) {
502 return 1; 478 return 1;
503 }
504 479
505 uint64_t ping_id = add_gettingnodes(ip_port); 480 uint64_t ping_id = add_gettingnodes(ip_port);
506 481
507 if(ping_id == 0) { 482 if (ping_id == 0)
508 return 1; 483 return 1;
509 }
510 484
511 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; 485 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
512 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; 486 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
@@ -519,9 +493,8 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
519 493
520 int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); 494 int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt);
521 495
522 if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { 496 if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
523 return -1; 497 return -1;
524 }
525 data[0] = 2; 498 data[0] = 2;
526 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); 499 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
527 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 500 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
@@ -531,11 +504,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
531} 504}
532 505
533/* send a send nodes response */ 506/* send a send nodes response */
534static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) 507static int sendnodes(IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
535{ 508{
536 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { 509 if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
537 return 1; 510 return 1;
538 }
539 511
540 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 512 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
541 + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; 513 + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
@@ -543,9 +515,8 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
543 Node_format nodes_list[MAX_SENT_NODES]; 515 Node_format nodes_list[MAX_SENT_NODES];
544 int num_nodes = get_close_nodes(client_id, nodes_list); 516 int num_nodes = get_close_nodes(client_id, nodes_list);
545 517
546 if(num_nodes == 0) { 518 if (num_nodes == 0)
547 return 0; 519 return 0;
548 }
549 520
550 uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; 521 uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES];
551 uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; 522 uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
@@ -558,9 +529,8 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
558 int len = encrypt_data(public_key, self_secret_key, nonce, plain, 529 int len = encrypt_data(public_key, self_secret_key, nonce, plain,
559 sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); 530 sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt);
560 531
561 if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) { 532 if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
562 return -1; 533 return -1;
563 }
564 534
565 data[0] = 3; 535 data[0] = 3;
566 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); 536 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
@@ -574,27 +544,20 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
574/* Packet handling functions 544/* Packet handling functions
575 One to handle each types of packets we receive 545 One to handle each types of packets we receive
576 return 0 if handled correctly, 1 if packet is bad. */ 546 return 0 if handled correctly, 1 if packet is bad. */
577int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) 547int handle_pingreq(uint8_t *packet, uint32_t length, IP_Port source)
578{ 548{
579 uint64_t ping_id; 549 uint64_t ping_id;
580 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { 550 if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
581 return 1; 551 return 1;
582 }
583 /* check if packet is from ourself. */ 552 /* check if packet is from ourself. */
584 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { 553 if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
585 return 1; 554 return 1;
586 }
587
588
589
590 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, 555 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
591 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 556 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
592 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); 557 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id);
593 if(len != sizeof(ping_id)) { 558 if (len != sizeof(ping_id))
594 return 1; 559 return 1;
595 } 560
596
597
598 pingres(source, packet + 1, ping_id); 561 pingres(source, packet + 1, ping_id);
599 562
600 pingreq(source, packet + 1); /* TODO: make this smarter? */ 563 pingreq(source, packet + 1); /* TODO: make this smarter? */
@@ -603,26 +566,21 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
603 566
604} 567}
605 568
606int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) 569int handle_pingres(uint8_t *packet, uint32_t length, IP_Port source)
607{ 570{
608 uint64_t ping_id; 571 uint64_t ping_id;
609 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { 572 if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
610 return 1; 573 return 1;
611 } 574 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. */ {
613 return 1; 575 return 1;
614 } 576
615
616
617
618 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, 577 int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
619 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 578 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
620 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); 579 sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id);
621 if(len != sizeof(ping_id)) { 580 if (len != sizeof(ping_id))
622 return 1; 581 return 1;
623 }
624 582
625 if(is_pinging(source, ping_id)) { 583 if (is_pinging(source, ping_id)) {
626 addto_lists(source, packet + 1); 584 addto_lists(source, packet + 1);
627 return 0; 585 return 0;
628 } 586 }
@@ -630,16 +588,14 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
630 588
631} 589}
632 590
633int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) 591int handle_getnodes(uint8_t *packet, uint32_t length, IP_Port source)
634{ 592{
635 uint64_t ping_id; 593 uint64_t ping_id;
636 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { 594 if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
637 return 1; 595 return 1;
638 }
639 /* check if packet is from ourself. */ 596 /* check if packet is from ourself. */
640 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { 597 if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
641 return 1; 598 return 1;
642 }
643 599
644 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; 600 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
645 601
@@ -647,10 +603,8 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
647 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 603 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
648 sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); 604 sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain);
649 605
650 if(len != sizeof(ping_id) + CLIENT_ID_SIZE) { 606 if (len != sizeof(ping_id) + CLIENT_ID_SIZE)
651 return 1; 607 return 1;
652 }
653
654 608
655 memcpy(&ping_id, plain, sizeof(ping_id)); 609 memcpy(&ping_id, plain, sizeof(ping_id));
656 sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); 610 sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id);
@@ -664,7 +618,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
664int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) 618int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
665{ 619{
666 uint64_t ping_id; 620 uint64_t ping_id;
667 if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 621 if (length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) //TODO: rewrite this monstrosity
668 + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) || 622 + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) ||
669 (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 623 (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
670 + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || 624 + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 ||
@@ -682,42 +636,36 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
682 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); 636 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain);
683 637
684 638
685 if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) { 639 if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
686 return 1; 640 return 1;
687 }
688
689 memcpy(&ping_id, plain, sizeof(ping_id)); 641 memcpy(&ping_id, plain, sizeof(ping_id));
690 if(!is_gettingnodes(source, ping_id)) { 642 if(!is_gettingnodes(source, ping_id))
691 return 1; 643 return 1;
692 } 644
693
694 Node_format nodes_list[MAX_SENT_NODES]; 645 Node_format nodes_list[MAX_SENT_NODES];
695 memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); 646 memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
696 647
697 addto_lists(source, packet + 1); 648 addto_lists(source, packet + 1);
698 649
699 uint32_t i; 650 uint32_t i;
700 for(i = 0; i < num_nodes; ++i) { 651 for (i = 0; i < num_nodes; ++i)
701 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); 652 pingreq(nodes_list[i].ip_port, nodes_list[i].client_id);
702 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); 653 returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
703 }
704 654
705 return 0; 655 return 0;
706} 656}
707 657
708/* END of packet handling functions */ 658/* END of packet handling functions */
709 659
710int DHT_addfriend(uint8_t * client_id) 660int DHT_addfriend(uint8_t *client_id)
711{ 661{
712 Friend * temp; 662 Friend * temp;
713 if(num_friends == 0) { 663 if (num_friends == 0)
714 temp = malloc(sizeof(Friend)); 664 temp = malloc(sizeof(Friend));
715 } else { 665 else
716 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); 666 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
717 } 667 if (temp == NULL)
718 if(temp == NULL) {
719 return 1; 668 return 1;
720 }
721 669
722 friends_list = temp; 670 friends_list = temp;
723 memset(&friends_list[num_friends], 0, sizeof(Friend)); 671 memset(&friends_list[num_friends], 0, sizeof(Friend));
@@ -727,21 +675,19 @@ int DHT_addfriend(uint8_t * client_id)
727 return 0; 675 return 0;
728} 676}
729 677
730int DHT_delfriend(uint8_t * client_id) 678int DHT_delfriend(uint8_t *client_id)
731{ 679{
732 uint32_t i; 680 uint32_t i;
733 Friend * temp; 681 Friend * temp;
734 for(i = 0; i < num_friends; ++i) { 682 for (i = 0; i < num_friends; ++i) {
735 /* Equal */ 683 /* Equal */
736 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ 684 if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){
737 --num_friends; 685 --num_friends;
738 if(num_friends != i) { 686 if (num_friends != i)
739 memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); 687 memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE);
740 }
741 temp = realloc(friends_list, sizeof(Friend) * (num_friends)); 688 temp = realloc(friends_list, sizeof(Friend) * (num_friends));
742 if(temp != NULL) { 689 if (temp != NULL)
743 friends_list = temp; 690 friends_list = temp;
744 }
745 return 0; 691 return 0;
746 } 692 }
747 } 693 }
@@ -749,12 +695,12 @@ int DHT_delfriend(uint8_t * client_id)
749} 695}
750 696
751/* TODO: Optimize this. */ 697/* TODO: Optimize this. */
752IP_Port DHT_getfriendip(uint8_t * client_id) 698IP_Port DHT_getfriendip(uint8_t *client_id)
753{ 699{
754 uint32_t i, j; 700 uint32_t i, j;
755 IP_Port empty = {{{0}}, 0}; 701 IP_Port empty = {{{0}}, 0};
756 uint32_t temp_time = unix_time(); 702 uint32_t temp_time = unix_time();
757 for(i = 0; i < num_friends; ++i) { 703 for (i = 0; i < num_friends; ++i) {
758 /* Equal */ 704 /* Equal */
759 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { 705 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
760 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 706 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
@@ -789,21 +735,21 @@ void doDHTFriends()
789 uint32_t rand_node; 735 uint32_t rand_node;
790 uint32_t index[MAX_FRIEND_CLIENTS]; 736 uint32_t index[MAX_FRIEND_CLIENTS];
791 737
792 for(i = 0; i < num_friends; ++i) { 738 for (i = 0; i < num_friends; ++i) {
793 uint32_t num_nodes = 0; 739 uint32_t num_nodes = 0;
794 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 740 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
795 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { 741 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) { 742 if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
797 pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); 743 pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
798 friends_list[i].client_list[j].last_pinged = temp_time; 744 friends_list[i].client_list[j].last_pinged = temp_time;
799 } 745 }
800 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ { 746 if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ {
801 index[num_nodes] = j; 747 index[num_nodes] = j;
802 ++num_nodes; 748 ++num_nodes;
803 } 749 }
804 } 750 }
805 } 751 }
806 if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { 752 if (friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
807 rand_node = rand() % num_nodes; 753 rand_node = rand() % num_nodes;
808 getnodes(friends_list[i].client_list[index[rand_node]].ip_port, 754 getnodes(friends_list[i].client_list[index[rand_node]].ip_port,
809 friends_list[i].client_list[index[rand_node]].client_id, 755 friends_list[i].client_list[index[rand_node]].client_id,
@@ -825,23 +771,23 @@ void doClose() /* tested */
825 uint32_t rand_node; 771 uint32_t rand_node;
826 uint32_t index[LCLIENT_LIST]; 772 uint32_t index[LCLIENT_LIST];
827 773
828 for(i = 0; i < LCLIENT_LIST; ++i) { 774 for (i = 0; i < LCLIENT_LIST; ++i) {
829 /* if node is not dead. */ 775 /* if node is not dead. */
830 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { 776 if (close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
831 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) 777 if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
832 { 778 {
833 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); 779 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id);
834 close_clientlist[i].last_pinged = temp_time; 780 close_clientlist[i].last_pinged = temp_time;
835 } 781 }
836 /* if node is good. */ 782 /* if node is good. */
837 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { 783 if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
838 index[num_nodes] = i; 784 index[num_nodes] = i;
839 ++num_nodes; 785 ++num_nodes;
840 } 786 }
841 } 787 }
842 } 788 }
843 789
844 if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { 790 if (close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
845 rand_node = rand() % num_nodes; 791 rand_node = rand() % num_nodes;
846 getnodes(close_clientlist[index[rand_node]].ip_port, 792 getnodes(close_clientlist[index[rand_node]].ip_port,
847 close_clientlist[index[rand_node]].client_id, 793 close_clientlist[index[rand_node]].client_id,
@@ -850,18 +796,18 @@ void doClose() /* tested */
850 } 796 }
851} 797}
852 798
853void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) 799void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key)
854{ 800{
855 getnodes(ip_port, public_key, self_public_key); 801 getnodes(ip_port, public_key, self_public_key);
856} 802}
857 803
858/* send the given packet to node with client_id 804/* send the given packet to node with client_id
859 returns -1 if failure */ 805 returns -1 if failure */
860int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) 806int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length)
861{ 807{
862 uint32_t i; 808 uint32_t i;
863 for(i = 0; i < LCLIENT_LIST; ++i) { 809 for (i = 0; i < LCLIENT_LIST; ++i) {
864 if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { 810 if (memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
865 return sendpacket(close_clientlist[i].ip_port, packet, length); 811 return sendpacket(close_clientlist[i].ip_port, packet, length);
866 } 812 }
867 } 813 }
@@ -873,22 +819,20 @@ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
873 returns the number of ips returned 819 returns the number of ips returned
874 return 0 if we are connected to friend or if no ips were found. 820 return 0 if we are connected to friend or if no ips were found.
875 returns -1 if no such friend*/ 821 returns -1 if no such friend*/
876static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) 822static int friend_iplist(IP_Port *ip_portlist, uint16_t friend_num)
877{ 823{
878 int num_ips = 0; 824 int num_ips = 0;
879 uint32_t i; 825 uint32_t i;
880 uint32_t temp_time = unix_time(); 826 uint32_t temp_time = unix_time();
881 if(friend_num >= num_friends) { 827 if (friend_num >= num_friends)
882 return -1; 828 return -1;
883 } 829 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
884 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i)
885 {
886 /*If ip is not zero and node is good */ 830 /*If ip is not zero and node is good */
887 if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && 831 if (friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 &&
888 friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { 832 friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time)
889 if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) { 833 {
834 if (memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0)
890 return 0; 835 return 0;
891 }
892 ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; 836 ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port;
893 ++num_ips; 837 ++num_ips;
894 } 838 }
@@ -898,21 +842,20 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
898 842
899/* Send the following packet to everyone who tells us they are connected to friend_id 843/* Send the following packet to everyone who tells us they are connected to friend_id
900 returns the number of nodes it sent the packet to */ 844 returns the number of nodes it sent the packet to */
901int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) 845int route_tofriend(uint8_t * friend_id, uint8_t *packet, uint32_t length)
902{ 846{
903 uint32_t i, j; 847 uint32_t i, j;
904 uint32_t sent = 0; 848 uint32_t sent = 0;
905 uint32_t temp_time = unix_time(); 849 uint32_t temp_time = unix_time();
906 for(i = 0; i < num_friends; ++i) { 850 for (i = 0; i < num_friends; ++i) {
907 /* Equal */ 851 /* Equal */
908 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { 852 if (memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) {
909 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 853 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
910 /*If ip is not zero and node is good */ 854 /*If ip is not zero and node is good */
911 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && 855 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 &&
912 friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { 856 friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
913 if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) { 857 if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length)
914 ++sent; 858 ++sent;
915 }
916 } 859 }
917 } 860 }
918 return sent; 861 return sent;
diff --git a/core/DHT.h b/core/DHT.h
index 966645f5..f306026e 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -40,13 +40,13 @@ extern "C" {
40 client_id must be CLIENT_ID_SIZE bytes long. 40 client_id must be CLIENT_ID_SIZE bytes long.
41 returns 0 if success 41 returns 0 if success
42 returns 1 if failure (friends list is full) */ 42 returns 1 if failure (friends list is full) */
43int DHT_addfriend(uint8_t * client_id); 43int DHT_addfriend(uint8_t *client_id);
44 44
45/* Delete a friend from the friends list 45/* Delete a friend from the friends list
46 client_id must be CLIENT_ID_SIZE bytes long. 46 client_id must be CLIENT_ID_SIZE bytes long.
47 returns 0 if success 47 returns 0 if success
48 returns 1 if failure (client_id not in friends list) */ 48 returns 1 if failure (client_id not in friends list) */
49int DHT_delfriend(uint8_t * client_id); 49int DHT_delfriend(uint8_t *client_id);
50 50
51/* Get ip of friend 51/* Get ip of friend
52 client_id must be CLIENT_ID_SIZE bytes long. 52 client_id must be CLIENT_ID_SIZE bytes long.
@@ -55,7 +55,7 @@ int DHT_delfriend(uint8_t * client_id);
55 returns ip if success 55 returns ip if success
56 returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) 56 returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
57 returns ip of 1 if friend is not in list. */ 57 returns ip of 1 if friend is not in list. */
58IP_Port DHT_getfriendip(uint8_t * client_id); 58IP_Port DHT_getfriendip(uint8_t *client_id);
59 59
60/* 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) */
61void doDHT(); 61void doDHT();
@@ -63,21 +63,21 @@ void doDHT();
63/* if we receive a DHT packet we call this function so it can be handled. 63/* if we receive a DHT packet we call this function so it can be handled.
64 return 0 if packet is handled correctly. 64 return 0 if packet is handled correctly.
65 return 1 if it didn't handle the packet or if the packet was shit. */ 65 return 1 if it didn't handle the packet or if the packet was shit. */
66int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); 66int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
67 67
68/* Use this function to bootstrap the client 68/* Use this function to bootstrap the client
69 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 */
70void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); 70void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
71 71
72/* ROUTING FUNCTIONS */ 72/* ROUTING FUNCTIONS */
73 73
74/* send the given packet to node with client_id 74/* send the given packet to node with client_id
75 returns -1 if failure */ 75 returns -1 if failure */
76int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length); 76int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length);
77 77
78/* Send the following packet to everyone who tells us they are connected to friend_id 78/* Send the following packet to everyone who tells us they are connected to friend_id
79 returns the number of nodes it sent the packet to */ 79 returns the number of nodes it sent the packet to */
80int 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);
81 81
82/* NAT PUNCHING FUNCTIONS */ 82/* NAT PUNCHING FUNCTIONS */
83 83
@@ -85,7 +85,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length);
85 ip_portlist must be at least MAX_FRIEND_CLIENTS big 85 ip_portlist must be at least MAX_FRIEND_CLIENTS big
86 returns the number of ips returned 86 returns the number of ips returned
87 returns -1 if no such friend*/ 87 returns -1 if no such friend*/
88int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); 88int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id);
89 89
90/* SAVE/LOAD functions */ 90/* SAVE/LOAD functions */
91 91
@@ -93,12 +93,12 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id);
93uint32_t DHT_size(); 93uint32_t DHT_size();
94 94
95/* save the DHT in data where data is an array of size DHT_size() */ 95/* save the DHT in data where data is an array of size DHT_size() */
96void DHT_save(uint8_t * data); 96void DHT_save(uint8_t *data);
97 97
98/* load the DHT from data of size size; 98/* load the DHT from data of size size;
99 return -1 if failure 99 return -1 if failure
100 return 0 if success */ 100 return 0 if success */
101int DHT_load(uint8_t * data, uint32_t size); 101int DHT_load(uint8_t *data, uint32_t size);
102 102
103/* returns 0 if we are not connected to the DHT 103/* returns 0 if we are not connected to the DHT
104 returns 1 if we are */ 104 returns 1 if we are */
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c
index 3cfcb067..1ac13c33 100644
--- a/core/LAN_discovery.c
+++ b/core/LAN_discovery.c
@@ -37,24 +37,24 @@ IP broadcast_ip()
37 return -1 if it is not */ 37 return -1 if it is not */
38int LAN_ip(IP ip) 38int LAN_ip(IP ip)
39{ 39{
40 if(ip.c[0] == 127)/* Loopback */ 40 if (ip.c[0] == 127)/* Loopback */
41 return 0; 41 return 0;
42 if(ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */ 42 if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */
43 return 0; 43 return 0;
44 if(ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */ 44 if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */
45 return 0; 45 return 0;
46 if(ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */ 46 if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */
47 return 0; 47 return 0;
48 if(ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */ 48 if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */
49 return 0; 49 return 0;
50 return -1; 50 return -1;
51} 51}
52 52
53int handle_LANdiscovery(uint8_t * packet, uint32_t length, IP_Port source) 53int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source)
54{ 54{
55 if(LAN_ip(source.ip) == -1) 55 if (LAN_ip(source.ip) == -1)
56 return 1; 56 return 1;
57 if(length != crypto_box_PUBLICKEYBYTES + 1) 57 if (length != crypto_box_PUBLICKEYBYTES + 1)
58 return 1; 58 return 1;
59 DHT_bootstrap(source, packet + 1); 59 DHT_bootstrap(source, packet + 1);
60 return 0; 60 return 0;
@@ -71,9 +71,9 @@ int send_LANdiscovery(uint16_t port)
71} 71}
72 72
73 73
74int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 74int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
75{ 75{
76 if(packet[0] == 32) 76 if (packet[0] == 32)
77 return handle_LANdiscovery(packet, length, source); 77 return handle_LANdiscovery(packet, length, source);
78 return 1; 78 return 1;
79} 79}
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h
index 655830f9..cca5bd30 100644
--- a/core/LAN_discovery.h
+++ b/core/LAN_discovery.h
@@ -39,7 +39,7 @@ int send_LANdiscovery(uint16_t port);
39/* if we receive a packet we call this function so it can be handled. 39/* if we receive a packet we call this function so it can be handled.
40 return 0 if packet is handled correctly. 40 return 0 if packet is handled correctly.
41 return 1 if it didn't handle the packet or if the packet was shit. */ 41 return 1 if it didn't handle the packet or if the packet was shit. */
42int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); 42int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
43 43
44 44
45 45
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 43f61b5b..f9d20b2f 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -44,14 +44,12 @@ timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION
44/* initial send rate of data. */ 44/* initial send rate of data. */
45#define DATA_SYNC_RATE 30 45#define DATA_SYNC_RATE 30
46 46
47typedef struct 47typedef struct {
48{
49 uint8_t data[MAX_DATA_SIZE]; 48 uint8_t data[MAX_DATA_SIZE];
50 uint16_t size; 49 uint16_t size;
51}Data; 50}Data;
52 51
53typedef struct 52typedef struct {
54{
55 IP_Port ip_port; 53 IP_Port ip_port;
56 uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, 54 uint8_t status; /* 0 if connection is dead, 1 if attempting handshake,
57 2 if handshake is done (we start sending SYNC packets) 55 2 if handshake is done (we start sending SYNC packets)
@@ -97,13 +95,13 @@ static Connection connections[MAX_CONNECTIONS];
97/* get connection id from IP_Port 95/* get connection id from IP_Port
98 return -1 if there are no connections like we are looking for 96 return -1 if there are no connections like we are looking for
99 return id if it found it */ 97 return id if it found it */
100int getconnection_id(IP_Port ip_port) 98int getconnection_id(IP_Port ip_port) {
101{
102 uint32_t i; 99 uint32_t i;
103 for(i = 0; i < MAX_CONNECTIONS; ++i) 100 for (i = 0; i < MAX_CONNECTIONS; ++i) {
104 if(connections[i].ip_port.ip.i == ip_port.ip.i && 101 if (connections[i].ip_port.ip.i == ip_port.ip.i &&
105 connections[i].ip_port.port == ip_port.port && connections[i].status > 0) 102 connections[i].ip_port.port == ip_port.port && connections[i].status > 0)
106 return i; 103 return i;
104 }
107 return -1; 105 return -1;
108} 106}
109 107
@@ -116,12 +114,12 @@ static uint32_t randtable[6][256];
116uint32_t handshake_id(IP_Port source) 114uint32_t handshake_id(IP_Port source)
117{ 115{
118 uint32_t id = 0, i; 116 uint32_t id = 0, i;
119 for(i = 0; i < 6; ++i) { 117 for (i = 0; i < 6; ++i) {
120 if(randtable[i][((uint8_t *)&source)[i]] == 0) 118 if(randtable[i][((uint8_t *)&source)[i]] == 0)
121 randtable[i][((uint8_t *)&source)[i]] = random_int(); 119 randtable[i][((uint8_t *)&source)[i]] = random_int();
122 id ^= randtable[i][((uint8_t *)&source)[i]]; 120 id ^= randtable[i][((uint8_t *)&source)[i]];
123 } 121 }
124 if(id == 0) /* id can't be zero */ 122 if (id == 0) /* id can't be zero */
125 id = 1; 123 id = 1;
126 return id; 124 return id;
127} 125}
@@ -141,10 +139,10 @@ void change_handshake(IP_Port source)
141int new_connection(IP_Port ip_port) 139int new_connection(IP_Port ip_port)
142{ 140{
143 int connect = getconnection_id(ip_port); 141 int connect = getconnection_id(ip_port);
144 if(connect != -1) 142 if (connect != -1)
145 return connect; 143 return connect;
146 uint32_t i; 144 uint32_t i;
147 for(i = 0; i < MAX_CONNECTIONS; ++i) { 145 for (i = 0; i < MAX_CONNECTIONS; ++i) {
148 if(connections[i].status == 0) { 146 if(connections[i].status == 0) {
149 memset(&connections[i], 0, sizeof(Connection)); 147 memset(&connections[i], 0, sizeof(Connection));
150 connections[i].ip_port = ip_port; 148 connections[i].ip_port = ip_port;
@@ -173,11 +171,11 @@ int new_connection(IP_Port ip_port)
173 return -1 if it could not initialize the connection. */ 171 return -1 if it could not initialize the connection. */
174int new_inconnection(IP_Port ip_port) 172int new_inconnection(IP_Port ip_port)
175{ 173{
176 if(getconnection_id(ip_port) != -1) 174 if (getconnection_id(ip_port) != -1)
177 return -1; 175 return -1;
178 uint32_t i; 176 uint32_t i;
179 for(i = 0; i < MAX_CONNECTIONS; ++i) { 177 for (i = 0; i < MAX_CONNECTIONS; ++i) {
180 if(connections[i].status == 0) { 178 if (connections[i].status == 0) {
181 memset(&connections[i], 0, sizeof(Connection)); 179 memset(&connections[i], 0, sizeof(Connection));
182 connections[i].ip_port = ip_port; 180 connections[i].ip_port = ip_port;
183 connections[i].status = 2; 181 connections[i].status = 2;
@@ -202,11 +200,12 @@ int new_inconnection(IP_Port ip_port)
202int incoming_connection() 200int incoming_connection()
203{ 201{
204 uint32_t i; 202 uint32_t i;
205 for(i = 0; i < MAX_CONNECTIONS; ++i) 203 for (i = 0; i < MAX_CONNECTIONS; ++i) {
206 if(connections[i].inbound == 2) { 204 if (connections[i].inbound == 2) {
207 connections[i].inbound = 1; 205 connections[i].inbound = 1;
208 return i; 206 return i;
209 } 207 }
208 }
210 return -1; 209 return -1;
211} 210}
212 211
@@ -214,12 +213,13 @@ int incoming_connection()
214 return 0 if killed successfully */ 213 return 0 if killed successfully */
215int kill_connection(int connection_id) 214int kill_connection(int connection_id)
216{ 215{
217 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 216 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
218 if(connections[connection_id].status > 0) { 217 if (connections[connection_id].status > 0) {
219 connections[connection_id].status = 0; 218 connections[connection_id].status = 0;
220 change_handshake(connections[connection_id].ip_port); 219 change_handshake(connections[connection_id].ip_port);
221 return 0; 220 return 0;
222 } 221 }
222 }
223 return -1; 223 return -1;
224} 224}
225 225
@@ -228,11 +228,12 @@ int kill_connection(int connection_id)
228 return 0 if it will kill it */ 228 return 0 if it will kill it */
229int kill_connection_in(int connection_id, uint32_t seconds) 229int kill_connection_in(int connection_id, uint32_t seconds)
230{ 230{
231 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 231 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
232 if(connections[connection_id].status > 0) { 232 if (connections[connection_id].status > 0) {
233 connections[connection_id].killat = current_time() + 1000000UL*seconds; 233 connections[connection_id].killat = current_time() + 1000000UL*seconds;
234 return 0; 234 return 0;
235 } 235 }
236 }
236 return -1; 237 return -1;
237} 238}
238 239
@@ -244,7 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds)
244 return 4 if timed out and waiting to be killed */ 245 return 4 if timed out and waiting to be killed */
245int is_connected(int connection_id) 246int is_connected(int connection_id)
246{ 247{
247 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 248 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
248 return connections[connection_id].status; 249 return connections[connection_id].status;
249 return 0; 250 return 0;
250} 251}
@@ -252,7 +253,7 @@ int is_connected(int connection_id)
252/* returns the ip_port of the corresponding connection. */ 253/* returns the ip_port of the corresponding connection. */
253IP_Port connection_ip(int connection_id) 254IP_Port connection_ip(int connection_id)
254{ 255{
255 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 256 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
256 return connections[connection_id].ip_port; 257 return connections[connection_id].ip_port;
257 IP_Port zero = {{{0}}, 0}; 258 IP_Port zero = {{{0}}, 0};
258 return zero; 259 return zero;
@@ -274,7 +275,7 @@ uint32_t recvqueue(int connection_id)
274 return -1 if no packet in queue */ 275 return -1 if no packet in queue */
275char id_packet(int connection_id) 276char id_packet(int connection_id)
276{ 277{
277 if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) 278 if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
278 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; 279 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
279 return -1; 280 return -1;
280} 281}
@@ -283,7 +284,7 @@ char id_packet(int connection_id)
283 return length of received packet if successful */ 284 return length of received packet if successful */
284int read_packet(int connection_id, uint8_t * data) 285int read_packet(int connection_id, uint8_t * data)
285{ 286{
286 if(recvqueue(connection_id) != 0) { 287 if (recvqueue(connection_id) != 0) {
287 uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; 288 uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM;
288 uint16_t size = connections[connection_id].recvbuffer[index].size; 289 uint16_t size = connections[connection_id].recvbuffer[index].size;
289 memcpy(data, connections[connection_id].recvbuffer[index].data, size); 290 memcpy(data, connections[connection_id].recvbuffer[index].data, size);
@@ -298,11 +299,11 @@ int read_packet(int connection_id, uint8_t * data)
298 return 1 if data was put into the queue */ 299 return 1 if data was put into the queue */
299int write_packet(int connection_id, uint8_t * data, uint32_t length) 300int write_packet(int connection_id, uint8_t * data, uint32_t length)
300{ 301{
301 if(length > MAX_DATA_SIZE) 302 if (length > MAX_DATA_SIZE)
302 return 0; 303 return 0;
303 if(length == 0) 304 if (length == 0)
304 return 0; 305 return 0;
305 if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { 306 if (sendqueue(connection_id) < BUFFER_PACKET_NUM) {
306 uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; 307 uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
307 memcpy(connections[connection_id].sendbuffer[index].data, data, length); 308 memcpy(connections[connection_id].sendbuffer[index].data, data, length);
308 connections[connection_id].sendbuffer[index].size = length; 309 connections[connection_id].sendbuffer[index].size = length;
@@ -318,14 +319,15 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
318 uint32_t number = 0; 319 uint32_t number = 0;
319 uint32_t i; 320 uint32_t i;
320 uint32_t temp; 321 uint32_t temp;
321 if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ 322 if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
322 return 0; 323 return 0;
323 for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) 324 for (i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++) {
324 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { 325 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
325 temp = htonl(i); 326 temp = htonl(i);
326 memcpy(requested + number, &temp, 4); 327 memcpy(requested + number, &temp, 4);
327 ++number; 328 ++number;
328 } 329 }
330 }
329 if(number == 0) 331 if(number == 0)
330 connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; 332 connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum;
331 return number; 333 return number;
@@ -393,14 +395,14 @@ int send_DATA(uint32_t connection_id)
393{ 395{
394 int ret; 396 int ret;
395 uint32_t buffer[BUFFER_PACKET_NUM]; 397 uint32_t buffer[BUFFER_PACKET_NUM];
396 if(connections[connection_id].num_req_paquets > 0) { 398 if (connections[connection_id].num_req_paquets > 0) {
397 ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); 399 ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
398 connections[connection_id].num_req_paquets--; 400 connections[connection_id].num_req_paquets--;
399 memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); 401 memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4);
400 memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); 402 memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
401 return ret; 403 return ret;
402 } 404 }
403 if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { 405 if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
404 ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); 406 ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
405 connections[connection_id].sent_packetnum++; 407 connections[connection_id].sent_packetnum++;
406 return ret; 408 return ret;
@@ -415,7 +417,7 @@ int send_DATA(uint32_t connection_id)
415 return 0 if handled correctly, 1 if packet is bad. */ 417 return 0 if handled correctly, 1 if packet is bad. */
416int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) 418int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
417{ 419{
418 if(length != (1 + 4 + 4)) 420 if (length != (1 + 4 + 4))
419 return 1; 421 return 1;
420 uint32_t temp; 422 uint32_t temp;
421 uint32_t handshake_id1, handshake_id2; 423 uint32_t handshake_id1, handshake_id2;
@@ -425,13 +427,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
425 memcpy(&temp, packet + 5, 4); 427 memcpy(&temp, packet + 5, 4);
426 handshake_id2 = ntohl(temp); 428 handshake_id2 = ntohl(temp);
427 429
428 if(handshake_id2 == 0) { 430 if (handshake_id2 == 0) {
429 send_handshake(source, handshake_id(source), handshake_id1); 431 send_handshake(source, handshake_id(source), handshake_id1);
430 return 0; 432 return 0;
431 } 433 }
432 if(is_connected(connection) != 1) 434 if (is_connected(connection) != 1)
433 return 1; 435 return 1;
434 if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ 436 if (handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
435 connections[connection].status = 2; 437 connections[connection].status = 2;
436 /* NOTE: is this necessary? 438 /* NOTE: is this necessary?
437 connections[connection].handshake_id2 = handshake_id1; */ 439 connections[connection].handshake_id2 = handshake_id1; */
@@ -448,10 +450,10 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
448 0 if not. */ 450 0 if not. */
449int SYNC_valid(uint32_t length) 451int SYNC_valid(uint32_t length)
450{ 452{
451 if(length < 4 + 4 + 2) 453 if (length < 4 + 4 + 2)
452 return 0; 454 return 0;
453 if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || 455 if (length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
454 ((length - 4 - 4 - 2) % 4) != 0) 456 ((length - 4 - 4 - 2) % 4) != 0)
455 return 0; 457 return 0;
456 return 1; 458 return 1;
457} 459}
@@ -459,9 +461,9 @@ int SYNC_valid(uint32_t length)
459/* case 1: */ 461/* case 1: */
460int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 462int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
461{ 463{
462 if(handshake_id(source) == recv_packetnum) { 464 if (handshake_id(source) == recv_packetnum) {
463 int x = new_inconnection(source); 465 int x = new_inconnection(source);
464 if(x != -1) { 466 if (x != -1) {
465 connections[x].orecv_packetnum = recv_packetnum; 467 connections[x].orecv_packetnum = recv_packetnum;
466 connections[x].sent_packetnum = recv_packetnum; 468 connections[x].sent_packetnum = recv_packetnum;
467 connections[x].sendbuff_packetnum = recv_packetnum; 469 connections[x].sendbuff_packetnum = recv_packetnum;
@@ -479,7 +481,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
479/* case 2: */ 481/* case 2: */
480int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) 482int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
481{ 483{
482 if(recv_packetnum == connections[connection_id].orecv_packetnum) { 484 if (recv_packetnum == connections[connection_id].orecv_packetnum) {
483 /* && sent_packetnum == connections[connection_id].osent_packetnum) */ 485 /* && sent_packetnum == connections[connection_id].osent_packetnum) */
484 connections[connection_id].status = 3; 486 connections[connection_id].status = 3;
485 connections[connection_id].recv_counter = counter; 487 connections[connection_id].recv_counter = counter;
@@ -499,14 +501,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
499 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ 501 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
500 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); 502 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
501 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); 503 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
502 if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ 504 if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
503 connections[connection_id].orecv_packetnum = recv_packetnum; 505 connections[connection_id].orecv_packetnum = recv_packetnum;
504 connections[connection_id].osent_packetnum = sent_packetnum; 506 connections[connection_id].osent_packetnum = sent_packetnum;
505 connections[connection_id].successful_sent = recv_packetnum; 507 connections[connection_id].successful_sent = recv_packetnum;
506 connections[connection_id].last_recvSYNC = current_time(); 508 connections[connection_id].last_recvSYNC = current_time();
507 connections[connection_id].recv_counter = counter; 509 connections[connection_id].recv_counter = counter;
508 ++connections[connection_id].send_counter; 510 ++connections[connection_id].send_counter;
509 for(i = 0; i < number; ++i) { 511 for (i = 0; i < number; ++i) {
510 temp = ntohl(req_packets[i]); 512 temp = ntohl(req_packets[i]);
511 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); 513 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
512 } 514 }
@@ -516,10 +518,10 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
516 return 1; 518 return 1;
517} 519}
518 520
519int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) 521int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source)
520{ 522{
521 523
522 if(!SYNC_valid(length)) 524 if (!SYNC_valid(length))
523 return 1; 525 return 1;
524 int connection = getconnection_id(source); 526 int connection = getconnection_id(source);
525 uint8_t counter; 527 uint8_t counter;
@@ -533,39 +535,39 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
533 recv_packetnum = ntohl(temp); 535 recv_packetnum = ntohl(temp);
534 memcpy(&temp,packet + 6, 4); 536 memcpy(&temp,packet + 6, 4);
535 sent_packetnum = ntohl(temp); 537 sent_packetnum = ntohl(temp);
536 if(number != 0) 538 if (number != 0)
537 memcpy(req_packets, packet + 10, 4 * number); 539 memcpy(req_packets, packet + 10, 4 * number);
538 if(connection == -1) 540 if (connection == -1)
539 return handle_SYNC1(source, recv_packetnum, sent_packetnum); 541 return handle_SYNC1(source, recv_packetnum, sent_packetnum);
540 if(connections[connection].status == 2) 542 if (connections[connection].status == 2)
541 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); 543 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum);
542 if(connections[connection].status == 3) 544 if (connections[connection].status == 3)
543 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); 545 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number);
544 return 0; 546 return 0;
545} 547}
546 548
547/* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. 549/* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value.
548 return 1 if data was too big, 0 if not. */ 550 return 1 if data was too big, 0 if not. */
549int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) 551int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
550{ 552{
551 if(size > MAX_DATA_SIZE) 553 if (size > MAX_DATA_SIZE)
552 return 1; 554 return 1;
553 555
554 uint32_t i; 556 uint32_t i;
555 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; 557 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
556 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; 558 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
557 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { 559 for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
558 if(i == data_num) { 560 if (i == data_num) {
559 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); 561 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
560 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; 562 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
561 connections[connection_id].last_recvdata = current_time(); 563 connections[connection_id].last_recvdata = current_time();
562 if(sent_packet < BUFFER_PACKET_NUM) 564 if (sent_packet < BUFFER_PACKET_NUM)
563 connections[connection_id].osent_packetnum = data_num; 565 connections[connection_id].osent_packetnum = data_num;
564 break; 566 break;
565 } 567 }
566 } 568 }
567 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { 569 for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
568 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) 570 if (connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
569 connections[connection_id].recv_packetnum = i; 571 connections[connection_id].recv_packetnum = i;
570 else 572 else
571 break; 573 break;
@@ -574,17 +576,17 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size
574 return 0; 576 return 0;
575} 577}
576 578
577int handle_data(uint8_t * packet, uint32_t length, IP_Port source) 579int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
578{ 580{
579 int connection = getconnection_id(source); 581 int connection = getconnection_id(source);
580 582
581 if(connection == -1) 583 if (connection == -1)
582 return 1; 584 return 1;
583 585
584 if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ 586 if (connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
585 return 1; 587 return 1;
586 588
587 if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) 589 if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
588 return 1; 590 return 1;
589 uint32_t temp; 591 uint32_t temp;
590 uint32_t number; 592 uint32_t number;
@@ -597,9 +599,9 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
597 599
598/* END of packet handling functions */ 600/* END of packet handling functions */
599 601
600int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 602int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
601{ 603{
602 switch (packet[0]) { 604 switch (packet[0]) { //TODO: check if no break statement is correct???
603 case 16: 605 case 16:
604 return handle_handshake(packet, length, source); 606 return handle_handshake(packet, length, source);
605 607
@@ -622,19 +624,19 @@ void doNew()
622{ 624{
623 uint32_t i; 625 uint32_t i;
624 uint64_t temp_time = current_time(); 626 uint64_t temp_time = current_time();
625 for(i = 0; i < MAX_CONNECTIONS; ++i) { 627 for (i = 0; i < MAX_CONNECTIONS; ++i) {
626 if(connections[i].status == 1) 628 if (connections[i].status == 1)
627 if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { 629 if ((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
628 send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); 630 send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0);
629 connections[i].last_sent = temp_time; 631 connections[i].last_sent = temp_time;
630 } 632 }
631 633
632 /* kill all timed out connections */ 634 /* kill all timed out connections */
633 if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && 635 if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
634 connections[i].status != 4) 636 connections[i].status != 4)
635 /* kill_connection(i); */ 637 /* kill_connection(i); */
636 connections[i].status = 4; 638 connections[i].status = 4;
637 if(connections[i].status > 0 && connections[i].killat < temp_time) 639 if (connections[i].status > 0 && connections[i].killat < temp_time)
638 kill_connection(i); 640 kill_connection(i);
639 } 641 }
640} 642}
@@ -643,9 +645,9 @@ void doSYNC()
643{ 645{
644 uint32_t i; 646 uint32_t i;
645 uint64_t temp_time = current_time(); 647 uint64_t temp_time = current_time();
646 for(i = 0; i < MAX_CONNECTIONS; ++i) { 648 for (i = 0; i < MAX_CONNECTIONS; ++i) {
647 if(connections[i].status == 2 || connections[i].status == 3) 649 if (connections[i].status == 2 || connections[i].status == 3)
648 if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { 650 if ((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
649 send_SYNC(i); 651 send_SYNC(i);
650 connections[i].last_SYNC = temp_time; 652 connections[i].last_SYNC = temp_time;
651 } 653 }
@@ -657,10 +659,10 @@ void doData()
657 uint32_t i; 659 uint32_t i;
658 uint64_t j; 660 uint64_t j;
659 uint64_t temp_time = current_time(); 661 uint64_t temp_time = current_time();
660 for(i = 0; i < MAX_CONNECTIONS; ++i) 662 for (i = 0; i < MAX_CONNECTIONS; ++i)
661 if(connections[i].status == 3 && sendqueue(i) != 0) 663 if (connections[i].status == 3 && sendqueue(i) != 0)
662 if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { 664 if ((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
663 for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) 665 for (j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
664 send_DATA(i); 666 send_DATA(i);
665 connections[i].last_sent = temp_time; 667 connections[i].last_sent = temp_time;
666 } 668 }
@@ -675,15 +677,15 @@ void adjustRates()
675{ 677{
676 uint32_t i; 678 uint32_t i;
677 uint64_t temp_time = current_time(); 679 uint64_t temp_time = current_time();
678 for(i = 0; i < MAX_CONNECTIONS; ++i) { 680 for (i = 0; i < MAX_CONNECTIONS; ++i) {
679 if(connections[i].status == 1 || connections[i].status == 2) 681 if (connections[i].status == 1 || connections[i].status == 2)
680 connections[i].SYNC_rate = MAX_SYNC_RATE; 682 connections[i].SYNC_rate = MAX_SYNC_RATE;
681 if(connections[i].status == 3) { 683 if (connections[i].status == 3) {
682 if(sendqueue(i) != 0) { 684 if (sendqueue(i) != 0) {
683 connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; 685 connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
684 connections[i].SYNC_rate = MAX_SYNC_RATE; 686 connections[i].SYNC_rate = MAX_SYNC_RATE;
685 } 687 }
686 else if(connections[i].last_recvdata + 1000000UL > temp_time) 688 else if (connections[i].last_recvdata + 1000000UL > temp_time)
687 connections[i].SYNC_rate = MAX_SYNC_RATE; 689 connections[i].SYNC_rate = MAX_SYNC_RATE;
688 else 690 else
689 connections[i].SYNC_rate = SYNC_RATE; 691 connections[i].SYNC_rate = SYNC_RATE;
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h
index a9f1bb15..d27fea79 100644
--- a/core/Lossless_UDP.h
+++ b/core/Lossless_UDP.h
@@ -69,11 +69,11 @@ char id_packet(int connection_id);
69 69
70/* return 0 if there is no received data in the buffer. 70/* return 0 if there is no received data in the buffer.
71 return length of received packet if successful */ 71 return length of received packet if successful */
72int read_packet(int connection_id, uint8_t * data); 72int read_packet(int connection_id, uint8_t *data);
73 73
74/* return 0 if data could not be put in packet queue 74/* return 0 if data could not be put in packet queue
75 return 1 if data was put into the queue */ 75 return 1 if data was put into the queue */
76int write_packet(int connection_id, uint8_t * data, uint32_t length); 76int write_packet(int connection_id, uint8_t *data, uint32_t length);
77 77
78/* 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. */
79uint32_t sendqueue(int connection_id); 79uint32_t sendqueue(int connection_id);
@@ -97,7 +97,7 @@ void doLossless_UDP();
97/* if we receive a Lossless_UDP packet we call this function so it can be handled. 97/* if we receive a Lossless_UDP packet we call this function so it can be handled.
98 return 0 if packet is handled correctly. 98 return 0 if packet is handled correctly.
99 return 1 if it didn't handle the packet or if the packet was shit. */ 99 return 1 if it didn't handle the packet or if the packet was shit. */
100int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); 100int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
101 101
102#ifdef __cplusplus 102#ifdef __cplusplus
103} 103}
diff --git a/core/Messenger.c b/core/Messenger.c
index 69d33172..fd95869f 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -24,8 +24,7 @@
24#include "Messenger.h" 24#include "Messenger.h"
25#define MIN(a,b) (((a)<(b))?(a):(b)) 25#define MIN(a,b) (((a)<(b))?(a):(b))
26 26
27typedef struct 27typedef struct {
28{
29 uint8_t client_id[CLIENT_ID_SIZE]; 28 uint8_t client_id[CLIENT_ID_SIZE];
30 int crypt_connection_id; 29 int crypt_connection_id;
31 int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ 30 int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
@@ -37,7 +36,7 @@ typedef struct
37 uint16_t userstatus_length; 36 uint16_t userstatus_length;
38 uint8_t userstatus_sent; 37 uint8_t userstatus_sent;
39 uint16_t info_size; /* length of the info */ 38 uint16_t info_size; /* length of the info */
40}Friend; 39} Friend;
41 40
42uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 41uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
43 42
@@ -57,14 +56,15 @@ static uint32_t numfriends;
57 56
58/* return the friend id associated to that public key. 57/* return the friend id associated to that public key.
59 return -1 if no such friend */ 58 return -1 if no such friend */
60int getfriend_id(uint8_t * client_id) 59int getfriend_id(uint8_t *client_id)
61{ 60{
62 uint32_t i; 61 uint32_t i;
63 62
64 for(i = 0; i < numfriends; ++i) 63 for (i = 0; i < numfriends; ++i) {
65 if(friendlist[i].status > 0) 64 if (friendlist[i].status > 0)
66 if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) 65 if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
67 return i; 66 return i;
67 }
68 68
69 return -1; 69 return -1;
70} 70}
@@ -73,12 +73,12 @@ int getfriend_id(uint8_t * client_id)
73 make sure that client_id is of size CLIENT_ID_SIZE. 73 make sure that client_id is of size CLIENT_ID_SIZE.
74 return 0 if success 74 return 0 if success
75 return -1 if failure. */ 75 return -1 if failure. */
76int getclient_id(int friend_id, uint8_t * client_id) 76int getclient_id(int friend_id, uint8_t *client_id)
77{ 77{
78 if(friend_id >= numfriends || friend_id < 0) 78 if (friend_id >= numfriends || friend_id < 0)
79 return -1; 79 return -1;
80 80
81 if(friendlist[friend_id].status > 0) { 81 if (friendlist[friend_id].status > 0) {
82 memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); 82 memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE);
83 return 0; 83 return 0;
84 } 84 }
@@ -92,20 +92,18 @@ int getclient_id(int friend_id, uint8_t * client_id)
92 data is the data and length is the length 92 data is the data and length is the length
93 returns the friend number if success 93 returns the friend number if success
94 return -1 if failure. */ 94 return -1 if failure. */
95int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) 95int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
96{ 96{
97 if(length == 0 || length >= 97 if (length == 0 || length >=
98 (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) 98 (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES))
99 return -1; 99 return -1;
100 if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 100 if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
101 return -1; 101 return -1;
102 if(getfriend_id(client_id) != -1) 102 if (getfriend_id(client_id) != -1)
103 return -1; 103 return -1;
104 uint32_t i; 104 uint32_t i;
105 for(i = 0; i <= numfriends; ++i) 105 for (i = 0; i <= numfriends; ++i) {
106 { 106 if(friendlist[i].status == 0) {
107 if(friendlist[i].status == 0)
108 {
109 DHT_addfriend(client_id); 107 DHT_addfriend(client_id);
110 friendlist[i].status = 1; 108 friendlist[i].status = 1;
111 friendlist[i].crypt_connection_id = -1; 109 friendlist[i].crypt_connection_id = -1;
@@ -125,13 +123,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
125 123
126int m_addfriend_norequest(uint8_t * client_id) 124int m_addfriend_norequest(uint8_t * client_id)
127{ 125{
128 if(getfriend_id(client_id) != -1) 126 if (getfriend_id(client_id) != -1)
129 return -1; 127 return -1;
130 uint32_t i; 128 uint32_t i;
131 for(i = 0; i <= numfriends; ++i) 129 for (i = 0; i <= numfriends; ++i) {
132 { 130 if(friendlist[i].status == 0) {
133 if(friendlist[i].status == 0)
134 {
135 DHT_addfriend(client_id); 131 DHT_addfriend(client_id);
136 friendlist[i].status = 2; 132 friendlist[i].status = 2;
137 friendlist[i].crypt_connection_id = -1; 133 friendlist[i].crypt_connection_id = -1;
@@ -151,7 +147,7 @@ int m_addfriend_norequest(uint8_t * client_id)
151 return -1 if failure */ 147 return -1 if failure */
152int m_delfriend(int friendnumber) 148int m_delfriend(int friendnumber)
153{ 149{
154 if(friendnumber >= numfriends || friendnumber < 0) 150 if (friendnumber >= numfriends || friendnumber < 0)
155 return -1; 151 return -1;
156 152
157 DHT_delfriend(friendlist[friendnumber].client_id); 153 DHT_delfriend(friendlist[friendnumber].client_id);
@@ -159,9 +155,10 @@ int m_delfriend(int friendnumber)
159 free(friendlist[friendnumber].userstatus); 155 free(friendlist[friendnumber].userstatus);
160 memset(&friendlist[friendnumber], 0, sizeof(Friend)); 156 memset(&friendlist[friendnumber], 0, sizeof(Friend));
161 uint32_t i; 157 uint32_t i;
162 for(i = numfriends; i != 0; --i) 158 for (i = numfriends; i != 0; --i) {
163 if(friendlist[i].status != 0) 159 if (friendlist[i].status != 0)
164 break; 160 break;
161 }
165 numfriends = i; 162 numfriends = i;
166 return 0; 163 return 0;
167} 164}
@@ -173,7 +170,7 @@ int m_delfriend(int friendnumber)
173 return 0 if there is no friend with that number */ 170 return 0 if there is no friend with that number */
174int m_friendstatus(int friendnumber) 171int m_friendstatus(int friendnumber)
175{ 172{
176 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) 173 if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
177 return 0; 174 return 0;
178 return friendlist[friendnumber].status; 175 return friendlist[friendnumber].status;
179} 176}
@@ -181,11 +178,11 @@ int m_friendstatus(int friendnumber)
181/* send a text chat message to an online friend 178/* send a text chat message to an online friend
182 return 1 if packet was successfully put into the send queue 179 return 1 if packet was successfully put into the send queue
183 return 0 if it was not */ 180 return 0 if it was not */
184int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) 181int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
185{ 182{
186 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) 183 if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
187 return 0; 184 return 0;
188 if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) 185 if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
189 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ 186 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
190 return 0; 187 return 0;
191 uint8_t temp[MAX_DATA_SIZE]; 188 uint8_t temp[MAX_DATA_SIZE];
@@ -208,7 +205,7 @@ static int m_sendname(int friendnumber, uint8_t * name)
208 return -1 if failure */ 205 return -1 if failure */
209static int setfriendname(int friendnumber, uint8_t * name) 206static int setfriendname(int friendnumber, uint8_t * name)
210{ 207{
211 if(friendnumber >= numfriends || friendnumber < 0) 208 if (friendnumber >= numfriends || friendnumber < 0)
212 return -1; 209 return -1;
213 memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 210 memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
214 return 0; 211 return 0;
@@ -220,11 +217,11 @@ static int setfriendname(int friendnumber, uint8_t * name)
220 return -1 if failure */ 217 return -1 if failure */
221int setname(uint8_t * name, uint16_t length) 218int setname(uint8_t * name, uint16_t length)
222{ 219{
223 if(length > MAX_NAME_LENGTH) 220 if (length > MAX_NAME_LENGTH)
224 return -1; 221 return -1;
225 memcpy(self_name, name, length); 222 memcpy(self_name, name, length);
226 uint32_t i; 223 uint32_t i;
227 for(i = 0; i < numfriends; ++i) 224 for (i = 0; i < numfriends; ++i)
228 friendlist[i].name_sent = 0; 225 friendlist[i].name_sent = 0;
229 return 0; 226 return 0;
230} 227}
@@ -236,7 +233,7 @@ int setname(uint8_t * name, uint16_t length)
236 return -1 if failure */ 233 return -1 if failure */
237int getname(int friendnumber, uint8_t * name) 234int getname(int friendnumber, uint8_t * name)
238{ 235{
239 if(friendnumber >= numfriends || friendnumber < 0) 236 if (friendnumber >= numfriends || friendnumber < 0)
240 return -1; 237 return -1;
241 memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); 238 memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH);
242 return 0; 239 return 0;
@@ -244,7 +241,7 @@ int getname(int friendnumber, uint8_t * name)
244 241
245int m_set_userstatus(uint8_t *status, uint16_t length) 242int m_set_userstatus(uint8_t *status, uint16_t length)
246{ 243{
247 if(length > MAX_USERSTATUS_LENGTH) 244 if (length > MAX_USERSTATUS_LENGTH)
248 return -1; 245 return -1;
249 uint8_t *newstatus = calloc(length, 1); 246 uint8_t *newstatus = calloc(length, 1);
250 memcpy(newstatus, status, length); 247 memcpy(newstatus, status, length);
@@ -253,7 +250,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
253 self_userstatus_len = length; 250 self_userstatus_len = length;
254 251
255 uint32_t i; 252 uint32_t i;
256 for(i = 0; i < numfriends; ++i) 253 for (i = 0; i < numfriends; ++i)
257 friendlist[i].userstatus_sent = 0; 254 friendlist[i].userstatus_sent = 0;
258 return 0; 255 return 0;
259} 256}
@@ -262,7 +259,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
262 guaranteed to be at most MAX_USERSTATUS_LENGTH */ 259 guaranteed to be at most MAX_USERSTATUS_LENGTH */
263int m_get_userstatus_size(int friendnumber) 260int m_get_userstatus_size(int friendnumber)
264{ 261{
265 if(friendnumber >= numfriends || friendnumber < 0) 262 if (friendnumber >= numfriends || friendnumber < 0)
266 return -1; 263 return -1;
267 return friendlist[friendnumber].userstatus_length; 264 return friendlist[friendnumber].userstatus_length;
268} 265}
@@ -271,7 +268,7 @@ int m_get_userstatus_size(int friendnumber)
271 bytes, use m_get_userstatus_size to find out how much you need to allocate */ 268 bytes, use m_get_userstatus_size to find out how much you need to allocate */
272int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) 269int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
273{ 270{
274 if(friendnumber >= numfriends || friendnumber < 0) 271 if (friendnumber >= numfriends || friendnumber < 0)
275 return -1; 272 return -1;
276 memset(buf, 0, maxlen); 273 memset(buf, 0, maxlen);
277 memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); 274 memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
@@ -290,7 +287,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
290 287
291static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) 288static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length)
292{ 289{
293 if(friendnumber >= numfriends || friendnumber < 0) 290 if (friendnumber >= numfriends || friendnumber < 0)
294 return -1; 291 return -1;
295 uint8_t *newstatus = calloc(length, 1); 292 uint8_t *newstatus = calloc(length, 1);
296 memcpy(newstatus, status, length); 293 memcpy(newstatus, status, length);
@@ -350,35 +347,29 @@ int initMessenger()
350 return 0; 347 return 0;
351} 348}
352 349
350//TODO: make this function not suck.
353static void doFriends() 351static void doFriends()
354{/* TODO: add incoming connections and some other stuff. */ 352{/* TODO: add incoming connections and some other stuff. */
355 uint32_t i; 353 uint32_t i;
356 int len; 354 int len;
357 uint8_t temp[MAX_DATA_SIZE]; 355 uint8_t temp[MAX_DATA_SIZE];
358 for(i = 0; i < numfriends; ++i) 356 for (i = 0; i < numfriends; ++i) {
359 { 357 if (friendlist[i].status == 1) {
360 if(friendlist[i].status == 1)
361 {
362 int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); 358 int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
363 if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case 359 if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
364 of packet loss */
365 friendlist[i].status = 2; 360 friendlist[i].status = 2;
366 else 361 else if (fr > 0)
367 if(fr > 0)
368 friendlist[i].status = 2; 362 friendlist[i].status = 2;
369 } 363 }
370 if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ 364 if (friendlist[i].status == 2 || friendlist[i].status == 3) { /* friend is not online */
371 { 365 if (friendlist[i].status == 2) {
372 if(friendlist[i].status == 2) 366 if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
373 {
374 if(friendlist[i].friend_request_id + 10 < unix_time()) /*I know this is hackish but it should work.*/
375 {
376 send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); 367 send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
377 friendlist[i].friend_request_id = unix_time(); 368 friendlist[i].friend_request_id = unix_time();
378 } 369 }
379 } 370 }
380 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 371 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
381 switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) { 372 switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) {
382 case 0: 373 case 0:
383 if (friendip.ip.i > 1) 374 if (friendip.ip.i > 1)
384 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); 375 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
@@ -394,24 +385,23 @@ static void doFriends()
394 break; 385 break;
395 } 386 }
396 } 387 }
397 while(friendlist[i].status == 4) /* friend is online */ 388 while (friendlist[i].status == 4) { /* friend is online */
398 { 389 if (friendlist[i].name_sent == 0) {
399 if(friendlist[i].name_sent == 0) 390 if (m_sendname(i, self_name))
400 if(m_sendname(i, self_name))
401 friendlist[i].name_sent = 1; 391 friendlist[i].name_sent = 1;
402 if(friendlist[i].userstatus_sent == 0) 392 }
403 if(send_userstatus(i, self_userstatus, self_userstatus_len)) 393 if (friendlist[i].userstatus_sent == 0) {
394 if (send_userstatus(i, self_userstatus, self_userstatus_len))
404 friendlist[i].userstatus_sent = 1; 395 friendlist[i].userstatus_sent = 1;
396 }
405 len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); 397 len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
406 if(len > 0) 398 if (len > 0) {
407 { 399 switch (temp[0]) {
408 switch(temp[0]) {
409 case PACKET_ID_NICKNAME: { 400 case PACKET_ID_NICKNAME: {
410 if (len != MAX_NAME_LENGTH + 1) break; 401 if (len != MAX_NAME_LENGTH + 1)
402 break;
411 if(friend_namechange_isset) 403 if(friend_namechange_isset)
412 {
413 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ 404 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */
414 }
415 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); 405 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
416 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ 406 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */
417 break; 407 break;
@@ -419,25 +409,21 @@ static void doFriends()
419 case PACKET_ID_USERSTATUS: { 409 case PACKET_ID_USERSTATUS: {
420 uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); 410 uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
421 memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 411 memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
422 if(friend_statuschange_isset) 412 if (friend_statuschange_isset)
423 { 413 friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
424 friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
425 }
426 set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 414 set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
427 free(status); 415 free(status);
428 break; 416 break;
429 } 417 }
430 case PACKET_ID_MESSAGE: { 418 case PACKET_ID_MESSAGE: {
431 if(friend_message_isset) 419 if (friend_message_isset)
432 (*friend_message)(i, temp + 1, len - 1); 420 (*friend_message)(i, temp + 1, len - 1);
433 break; 421 break;
434 } 422 }
435 } 423 }
436 } 424 }
437 else 425 else {
438 { 426 if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
439 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */
440 {
441 crypto_kill(friendlist[i].crypt_connection_id); 427 crypto_kill(friendlist[i].crypt_connection_id);
442 friendlist[i].crypt_connection_id = -1; 428 friendlist[i].crypt_connection_id = -1;
443 friendlist[i].status = 3; 429 friendlist[i].status = 3;
@@ -454,11 +440,9 @@ static void doInbound()
454 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 440 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
455 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 441 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
456 int inconnection = crypto_inbound(public_key, secret_nonce, session_key); 442 int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
457 if(inconnection != -1) 443 if (inconnection != -1) {
458 {
459 int friend_id = getfriend_id(public_key); 444 int friend_id = getfriend_id(public_key);
460 if(friend_id != -1) 445 if (friend_id != -1) {
461 {
462 crypto_kill(friendlist[friend_id].crypt_connection_id); 446 crypto_kill(friendlist[friend_id].crypt_connection_id);
463 friendlist[friend_id].crypt_connection_id = 447 friendlist[friend_id].crypt_connection_id =
464 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); 448 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
@@ -476,8 +460,7 @@ static uint32_t last_LANdiscovery;
476/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ 460/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
477static void LANdiscovery() 461static void LANdiscovery()
478{ 462{
479 if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) 463 if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
480 {
481 send_LANdiscovery(htons(PORT)); 464 send_LANdiscovery(htons(PORT));
482 last_LANdiscovery = unix_time(); 465 last_LANdiscovery = unix_time();
483 } 466 }
@@ -490,12 +473,11 @@ void doMessenger()
490 IP_Port ip_port; 473 IP_Port ip_port;
491 uint8_t data[MAX_UDP_PACKET_SIZE]; 474 uint8_t data[MAX_UDP_PACKET_SIZE];
492 uint32_t length; 475 uint32_t length;
493 while(receivepacket(&ip_port, data, &length) != -1) 476 while (receivepacket(&ip_port, data, &length) != -1) {
494 {
495#ifdef DEBUG 477#ifdef DEBUG
496 /* if(rand() % 3 != 1) //simulate packet loss */ 478 /* if(rand() % 3 != 1) //simulate packet loss */
497 /* { */ 479 /* { */
498 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && 480 if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
499 friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) 481 friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port))
500 /* if packet is discarded */ 482 /* if packet is discarded */
501 printf("Received unhandled packet with length: %u\n", length); 483 printf("Received unhandled packet with length: %u\n", length);
@@ -527,7 +509,7 @@ uint32_t Messenger_size()
527} 509}
528 510
529/* save the messenger in data of size Messenger_size() */ 511/* save the messenger in data of size Messenger_size() */
530void Messenger_save(uint8_t * data) 512void Messenger_save(uint8_t *data)
531{ 513{
532 save_keys(data); 514 save_keys(data);
533 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 515 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
@@ -545,9 +527,9 @@ void Messenger_save(uint8_t * data)
545/* load the messenger from data of size length. */ 527/* load the messenger from data of size length. */
546int Messenger_load(uint8_t * data, uint32_t length) 528int Messenger_load(uint8_t * data, uint32_t length)
547{ 529{
548 if(length == ~0) 530 if (length == ~0)
549 return -1; 531 return -1;
550 if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) 532 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
551 return -1; 533 return -1;
552 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; 534 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2;
553 load_keys(data); 535 load_keys(data);
@@ -556,15 +538,15 @@ int Messenger_load(uint8_t * data, uint32_t length)
556 memcpy(&size, data, sizeof(size)); 538 memcpy(&size, data, sizeof(size));
557 data += sizeof(size); 539 data += sizeof(size);
558 540
559 if(length < size) 541 if (length < size)
560 return -1; 542 return -1;
561 length -= size; 543 length -= size;
562 if(DHT_load(data, size) == -1) 544 if (DHT_load(data, size) == -1)
563 return -1; 545 return -1;
564 data += size; 546 data += size;
565 memcpy(&size, data, sizeof(size)); 547 memcpy(&size, data, sizeof(size));
566 data += sizeof(size); 548 data += sizeof(size);
567 if(length != size || length % sizeof(Friend) != 0) 549 if (length != size || length % sizeof(Friend) != 0)
568 return -1; 550 return -1;
569 551
570 Friend * temp = malloc(size); 552 Friend * temp = malloc(size);
@@ -573,10 +555,8 @@ int Messenger_load(uint8_t * data, uint32_t length)
573 uint16_t num = size / sizeof(Friend); 555 uint16_t num = size / sizeof(Friend);
574 556
575 uint32_t i; 557 uint32_t i;
576 for(i = 0; i < num; ++i) 558 for (i = 0; i < num; ++i) {
577 { 559 if(temp[i].status != 0) {
578 if(temp[i].status != 0)
579 {
580 int fnum = m_addfriend_norequest(temp[i].client_id); 560 int fnum = m_addfriend_norequest(temp[i].client_id);
581 setfriendname(fnum, temp[i].name); 561 setfriendname(fnum, temp[i].name);
582 /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ 562 /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
diff --git a/core/Messenger.h b/core/Messenger.h
index 1067d156..0e2606d9 100644
--- a/core/Messenger.h
+++ b/core/Messenger.h
@@ -51,23 +51,23 @@ extern "C" {
51 data is the data and length is the length 51 data is the data and length is the length
52 returns the friend number if success 52 returns the friend number if success
53 return -1 if failure. */ 53 return -1 if failure. */
54int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); 54int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
55 55
56 56
57/* add a friend without sending a friendrequest. 57/* add a friend without sending a friendrequest.
58 returns the friend number if success 58 returns the friend number if success
59 return -1 if failure. */ 59 return -1 if failure. */
60int m_addfriend_norequest(uint8_t * client_id); 60int m_addfriend_norequest(uint8_t *client_id);
61 61
62/* return the friend id associated to that client id. 62/* return the friend id associated to that client id.
63 return -1 if no such friend */ 63 return -1 if no such friend */
64int getfriend_id(uint8_t * client_id); 64int getfriend_id(uint8_t *client_id);
65 65
66/* copies the public key associated to that friend id into client_id buffer. 66/* copies the public key associated to that friend id into client_id buffer.
67 make sure that client_id is of size CLIENT_ID_SIZE. 67 make sure that client_id is of size CLIENT_ID_SIZE.
68 return 0 if success 68 return 0 if success
69 return -1 if failure */ 69 return -1 if failure */
70int getclient_id(int friend_id, uint8_t * client_id); 70int getclient_id(int friend_id, uint8_t *client_id);
71 71
72/* remove a friend */ 72/* remove a friend */
73int m_delfriend(int friendnumber); 73int m_delfriend(int friendnumber);
@@ -82,20 +82,20 @@ int m_friendstatus(int friendnumber);
82/* send a text chat message to an online friend 82/* send a text chat message to an online friend
83 returns 1 if packet was successfully put into the send queue 83 returns 1 if packet was successfully put into the send queue
84 return 0 if it was not */ 84 return 0 if it was not */
85int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); 85int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
86 86
87/* Set our nickname 87/* Set our nickname
88 name must be a string of maximum MAX_NAME_LENGTH length. 88 name must be a string of maximum MAX_NAME_LENGTH length.
89 return 0 if success 89 return 0 if success
90 return -1 if failure */ 90 return -1 if failure */
91int setname(uint8_t * name, uint16_t length); 91int setname(uint8_t *name, uint16_t length);
92 92
93/* get name of friendnumber 93/* get name of friendnumber
94 put it in name 94 put it in name
95 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 95 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
96 return 0 if success 96 return 0 if success
97 return -1 if failure */ 97 return -1 if failure */
98int getname(int friendnumber, uint8_t * name); 98int getname(int friendnumber, uint8_t *name);
99 99
100/* set our user status 100/* set our user status
101 you are responsible for freeing status after 101 you are responsible for freeing status after
@@ -109,7 +109,7 @@ int m_get_userstatus_size(int friendnumber);
109 109
110/* copy friendnumber's userstatus into buf, truncating if size is over maxlen 110/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
111 get the size you need to allocate from m_get_userstatus_size */ 111 get the size you need to allocate from m_get_userstatus_size */
112int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); 112int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
113 113
114/* set the function that will be executed when a friend request is received. 114/* set the function that will be executed when a friend request is received.
115 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 115 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
@@ -143,10 +143,10 @@ void doMessenger();
143uint32_t Messenger_size(); 143uint32_t Messenger_size();
144 144
145/* save the messenger in data (must be allocated memory of size Messenger_size()) */ 145/* save the messenger in data (must be allocated memory of size Messenger_size()) */
146void Messenger_save(uint8_t * data); 146void Messenger_save(uint8_t *data);
147 147
148/* load the messenger from data of size length */ 148/* load the messenger from data of size length */
149int Messenger_load(uint8_t * data, uint32_t length); 149int Messenger_load(uint8_t *data, uint32_t length);
150 150
151#ifdef __cplusplus 151#ifdef __cplusplus
152} 152}
diff --git a/core/friend_requests.c b/core/friend_requests.c
index d1b0da57..7a2cdc24 100644
--- a/core/friend_requests.c
+++ b/core/friend_requests.c
@@ -35,24 +35,23 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
35 uint8_t packet[MAX_DATA_SIZE]; 35 uint8_t packet[MAX_DATA_SIZE];
36 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ 36 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
37 37
38 if(len == -1) 38 if (len == -1)
39 return -1; 39 return -1;
40 40
41 IP_Port ip_port = DHT_getfriendip(public_key); 41 IP_Port ip_port = DHT_getfriendip(public_key);
42 42
43 if(ip_port.ip.i == 1) 43 if (ip_port.ip.i == 1)
44 return -1; 44 return -1;
45 45
46 if(ip_port.ip.i != 0) 46 if (ip_port.ip.i != 0) {
47 { 47 if (sendpacket(ip_port, packet, len) != -1)
48 if(sendpacket(ip_port, packet, len) != -1)
49 return 0; 48 return 0;
50 return -1; 49 return -1;
51 } 50 }
52 51
53 int num = route_tofriend(public_key, packet, len); 52 int num = route_tofriend(public_key, packet, len);
54 53
55 if(num == 0) 54 if (num == 0)
56 return -1; 55 return -1;
57 56
58 return num; 57 return num;
@@ -62,8 +61,7 @@ static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
62static uint8_t handle_friendrequest_isset = 0; 61static uint8_t handle_friendrequest_isset = 0;
63 62
64/* set the function that will be executed when a friend request is received. */ 63/* set the function that will be executed when a friend request is received. */
65void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) 64void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) {
66{
67 handle_friendrequest = function; 65 handle_friendrequest = function;
68 handle_friendrequest_isset = 1; 66 handle_friendrequest_isset = 1;
69} 67}
@@ -78,9 +76,8 @@ static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES]
78static uint16_t recieved_requests_index; 76static uint16_t recieved_requests_index;
79 77
80/*Add to list of recieved friend requests*/ 78/*Add to list of recieved friend requests*/
81static void addto_recievedlist(uint8_t * client_id) 79static void addto_recievedlist(uint8_t * client_id) {
82{ 80 if (recieved_requests_index >= MAX_RECIEVED_STORED)
83 if(recieved_requests_index >= MAX_RECIEVED_STORED)
84 recieved_requests_index = 0; 81 recieved_requests_index = 0;
85 82
86 memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); 83 memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
@@ -89,46 +86,43 @@ static void addto_recievedlist(uint8_t * client_id)
89 86
90/* Check if a friend request was already recieved 87/* Check if a friend request was already recieved
91 return 0 if not, 1 if we did */ 88 return 0 if not, 1 if we did */
92static int request_recieved(uint8_t * client_id) 89static int request_recieved(uint8_t * client_id) {
93{
94 uint32_t i; 90 uint32_t i;
95 91
96 for(i = 0; i < MAX_RECIEVED_STORED; ++i) 92 for (i = 0; i < MAX_RECIEVED_STORED; ++i) {
97 if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) 93 if (memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
98 return 1; 94 return 1;
95 }
99 96
100 return 0; 97 return 0;
101} 98}
102 99
103 100
104int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 101int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) {
105{ 102 if (packet[0] == 32) {
106 103 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
107 if(packet[0] == 32) 104 length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
108 {
109 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
110 length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
111 return 1; 105 return 1;
112 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */ 106 if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
113 { 107 if (handle_friendrequest_isset == 0)
114 if(handle_friendrequest_isset == 0)
115 return 1; 108 return 1;
116 109
117 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 110 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
118 uint8_t data[MAX_DATA_SIZE]; 111 uint8_t data[MAX_DATA_SIZE];
119 int len = handle_request(public_key, data, packet, length); 112 int len = handle_request(public_key, data, packet, length);
120 113
121 if(len == -1) 114 if (len == -1)
122 return 1; 115 return 1;
123 if(request_recieved(public_key)) 116 if (request_recieved(public_key))
124 return 1; 117 return 1;
125 118
126 addto_recievedlist(public_key); 119 addto_recievedlist(public_key);
127 (*handle_friendrequest)(public_key, data, len); 120 (*handle_friendrequest)(public_key, data, len);
128 } 121 }
129 else /* if request is not for us, try routing it. */ 122 else {/* if request is not for us, try routing it. */
130 if(route_packet(packet + 1, packet, length) == length) 123 if(route_packet(packet + 1, packet, length) == length)
131 return 0; 124 return 0;
125 }
132 } 126 }
133 return 1; 127 return 1;
134} 128}
diff --git a/core/friend_requests.h b/core/friend_requests.h
index 29bc2b88..8988403c 100644
--- a/core/friend_requests.h
+++ b/core/friend_requests.h
@@ -33,7 +33,7 @@ extern "C" {
33 33
34/* Try to send a friendrequest to peer with public_key 34/* Try to send a friendrequest to peer with public_key
35 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. */
36int 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);
37 37
38/* 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.
39 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) */
@@ -42,7 +42,7 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
42/* if we receive a packet we call this function so it can be handled. 42/* if we receive a packet we call this function so it can be handled.
43 return 0 if packet is handled correctly. 43 return 0 if packet is handled correctly.
44 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. */
45int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); 45int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
46 46
47#ifdef __cplusplus 47#ifdef __cplusplus
48} 48}
diff --git a/core/net_crypto.c b/core/net_crypto.c
index 28cb83e8..c1571467 100644
--- a/core/net_crypto.c
+++ b/core/net_crypto.c
@@ -30,8 +30,7 @@
30uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 30uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
31uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 31uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
32 32
33typedef struct 33typedef struct {
34{
35 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ 34 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
36 uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ 35 uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
37 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ 36 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
@@ -43,7 +42,7 @@ typedef struct
43 4 if the connection is timed out. */ 42 4 if the connection is timed out. */
44 uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ 43 uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
45 44
46}Crypto_Connection; 45} Crypto_Connection;
47 46
48#define MAX_CRYPTO_CONNECTIONS 256 47#define MAX_CRYPTO_CONNECTIONS 256
49 48
@@ -58,10 +57,10 @@ static int incoming_connections[MAX_INCOMING];
58 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce 57 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
59 return -1 if there was a problem. 58 return -1 if there was a problem.
60 return length of encrypted data if everything was fine. */ 59 return length of encrypted data if everything was fine. */
61int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 60int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
62 uint8_t * plain, uint32_t length, uint8_t * encrypted) 61 uint8_t *plain, uint32_t length, uint8_t *encrypted)
63{ 62{
64 if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) 63 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0)
65 return -1; 64 return -1;
66 65
67 uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; 66 uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0};
@@ -73,7 +72,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
73 crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); 72 crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
74 73
75 /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ 74 /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */
76 if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) 75 if (memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
77 return -1; 76 return -1;
78 77
79 /* unpad the encrypted message */ 78 /* unpad the encrypted message */
@@ -85,10 +84,10 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
85 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce 84 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
86 return -1 if there was a problem(decryption failed) 85 return -1 if there was a problem(decryption failed)
87 return length of plain data if everything was fine. */ 86 return length of plain data if everything was fine. */
88int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 87int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
89 uint8_t * encrypted, uint32_t length, uint8_t * plain) 88 uint8_t *encrypted, uint32_t length, uint8_t *plain)
90{ 89{
91 if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) 90 if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
92 return -1; 91 return -1;
93 92
94 uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; 93 uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES];
@@ -97,12 +96,12 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
97 96
98 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ 97 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
99 98
100 if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, 99 if (crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
101 nonce, public_key, secret_key) == -1) 100 nonce, public_key, secret_key) == -1)
102 return -1; 101 return -1;
103 102
104 /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ 103 /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */
105 if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) 104 if (memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
106 return -1; 105 return -1;
107 106
108 /* unpad the plain message */ 107 /* unpad the plain message */
@@ -111,10 +110,10 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
111} 110}
112 111
113/* increment the given nonce by 1 */ 112/* increment the given nonce by 1 */
114void increment_nonce(uint8_t * nonce) 113void increment_nonce(uint8_t *nonce)
115{ 114{
116 uint32_t i; 115 uint32_t i;
117 for(i = 0; i < crypto_box_NONCEBYTES; ++i) { 116 for (i = 0; i < crypto_box_NONCEBYTES; ++i) {
118 ++nonce[i]; 117 ++nonce[i];
119 if(nonce[i] != 0) 118 if(nonce[i] != 0)
120 break; 119 break;
@@ -122,7 +121,7 @@ void increment_nonce(uint8_t * nonce)
122} 121}
123 122
124/* fill the given nonce with random bytes. */ 123/* fill the given nonce with random bytes. */
125void random_nonce(uint8_t * nonce) 124void random_nonce(uint8_t *nonce)
126{ 125{
127 uint32_t i, temp; 126 uint32_t i, temp;
128 for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { 127 for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) {
@@ -134,22 +133,22 @@ void random_nonce(uint8_t * nonce)
134/* return 0 if there is no received data in the buffer 133/* return 0 if there is no received data in the buffer
135 return -1 if the packet was discarded. 134 return -1 if the packet was discarded.
136 return length of received data if successful */ 135 return length of received data if successful */
137int read_cryptpacket(int crypt_connection_id, uint8_t * data) 136int read_cryptpacket(int crypt_connection_id, uint8_t *data)
138{ 137{
139 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) 138 if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
140 return 0; 139 return 0;
141 if(crypto_connections[crypt_connection_id].status != 3) 140 if (crypto_connections[crypt_connection_id].status != 3)
142 return 0; 141 return 0;
143 uint8_t temp_data[MAX_DATA_SIZE]; 142 uint8_t temp_data[MAX_DATA_SIZE];
144 int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); 143 int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data);
145 if(length == 0) 144 if (length == 0)
146 return 0; 145 return 0;
147 if(temp_data[0] != 3) 146 if (temp_data[0] != 3)
148 return -1; 147 return -1;
149 int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, 148 int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key,
150 crypto_connections[crypt_connection_id].sessionsecret_key, 149 crypto_connections[crypt_connection_id].sessionsecret_key,
151 crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); 150 crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data);
152 if(len != -1) { 151 if (len != -1) {
153 increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); 152 increment_nonce(crypto_connections[crypt_connection_id].recv_nonce);
154 return len; 153 return len;
155 } 154 }
@@ -158,22 +157,22 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data)
158 157
159/* return 0 if data could not be put in packet queue 158/* return 0 if data could not be put in packet queue
160 return 1 if data was put into the queue */ 159 return 1 if data was put into the queue */
161int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) 160int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length)
162{ 161{
163 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) 162 if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
164 return 0; 163 return 0;
165 if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) 164 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
166 return 0; 165 return 0;
167 if(crypto_connections[crypt_connection_id].status != 3) 166 if (crypto_connections[crypt_connection_id].status != 3)
168 return 0; 167 return 0;
169 uint8_t temp_data[MAX_DATA_SIZE]; 168 uint8_t temp_data[MAX_DATA_SIZE];
170 int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, 169 int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key,
171 crypto_connections[crypt_connection_id].sessionsecret_key, 170 crypto_connections[crypt_connection_id].sessionsecret_key,
172 crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); 171 crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1);
173 if(len == -1) 172 if (len == -1)
174 return 0; 173 return 0;
175 temp_data[0] = 3; 174 temp_data[0] = 3;
176 if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) 175 if (write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0)
177 return 0; 176 return 0;
178 increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); 177 increment_nonce(crypto_connections[crypt_connection_id].sent_nonce);
179 return 1; 178 return 1;
@@ -185,15 +184,15 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
185 request_id is the id of the request (32 = friend request, 254 = ping request) 184 request_id is the id of the request (32 = friend request, 254 = ping request)
186 returns -1 on failure 185 returns -1 on failure
187 returns the length of the created packet on success */ 186 returns the length of the created packet on success */
188int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) 187int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id)
189{ 188{
190 if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) 189 if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING)
191 return -1; 190 return -1;
192 uint8_t nonce[crypto_box_NONCEBYTES]; 191 uint8_t nonce[crypto_box_NONCEBYTES];
193 random_nonce(nonce); 192 random_nonce(nonce);
194 int len = encrypt_data(public_key, self_secret_key, nonce, data, length, 193 int len = encrypt_data(public_key, self_secret_key, nonce, data, length,
195 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); 194 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
196 if(len == -1) 195 if (len == -1)
197 return -1; 196 return -1;
198 packet[0] = request_id; 197 packet[0] = request_id;
199 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); 198 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
@@ -207,10 +206,10 @@ int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint3
207 in data if a friend or ping request was sent to us and returns the length of the data. 206 in data if a friend or ping request was sent to us and returns the length of the data.
208 packet is the request packet and length is its length 207 packet is the request packet and length is its length
209 return -1 if not valid request. */ 208 return -1 if not valid request. */
210int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length) 209int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length)
211{ 210{
212 211
213 if(length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && 212 if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
214 length <= MAX_DATA_SIZE + ENCRYPTION_PADDING && 213 length <= MAX_DATA_SIZE + ENCRYPTION_PADDING &&
215 memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 214 memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
216 { 215 {
@@ -230,7 +229,7 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1
230/* Send a crypto handshake packet containing an encrypted secret nonce and session public key 229/* Send a crypto handshake packet containing an encrypted secret nonce and session public key
231 to peer with connection_id and public_key 230 to peer with connection_id and public_key
232 the packet is encrypted with a random nonce which is sent in plain text with the packet */ 231 the packet is encrypted with a random nonce which is sent in plain text with the packet */
233int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) 232int send_cryptohandshake(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
234{ 233{
235 uint8_t temp_data[MAX_DATA_SIZE]; 234 uint8_t temp_data[MAX_DATA_SIZE];
236 uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; 235 uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
@@ -242,7 +241,7 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr
242 241
243 int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 242 int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
244 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); 243 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data);
245 if(len == -1) 244 if (len == -1)
246 return 0; 245 return 0;
247 temp_data[0] = 2; 246 temp_data[0] = 2;
248 memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); 247 memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
@@ -253,16 +252,16 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr
253/* Extract secret nonce, session public key and public_key from a packet(data) with length length 252/* Extract secret nonce, session public key and public_key from a packet(data) with length length
254 return 1 if successful 253 return 1 if successful
255 return 0 if failure */ 254 return 0 if failure */
256int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, 255int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
257 uint8_t * session_key, uint8_t * data, uint16_t length) 256 uint8_t *session_key, uint8_t *data, uint16_t length)
258{ 257{
259 int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); 258 int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
260 if(length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES 259 if (length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
261 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) 260 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad)
262 { 261 {
263 return 0; 262 return 0;
264 } 263 }
265 if(data[0] != 2) 264 if (data[0] != 2)
266 return 0; 265 return 0;
267 uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; 266 uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
268 267
@@ -272,7 +271,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
272 data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, 271 data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
273 crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); 272 crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp);
274 273
275 if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) 274 if (len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES)
276 return 0; 275 return 0;
277 276
278 memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); 277 memcpy(secret_nonce, temp, crypto_box_NONCEBYTES);
@@ -283,33 +282,33 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
283/* get crypto connection id from public key of peer 282/* get crypto connection id from public key of peer
284 return -1 if there are no connections like we are looking for 283 return -1 if there are no connections like we are looking for
285 return id if it found it */ 284 return id if it found it */
286int getcryptconnection_id(uint8_t * public_key) 285int getcryptconnection_id(uint8_t *public_key)
287{ 286{
288 uint32_t i; 287 uint32_t i;
289 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) 288 for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
290 if(crypto_connections[i].status > 0) 289 if (crypto_connections[i].status > 0)
291 if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) 290 if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
292 return i; 291 return i;
292 }
293 return -1; 293 return -1;
294} 294}
295 295
296/* Start a secure connection with other peer who has public_key and ip_port 296/* Start a secure connection with other peer who has public_key and ip_port
297 returns -1 if failure 297 returns -1 if failure
298 returns crypt_connection_id of the initialized connection if everything went well. */ 298 returns crypt_connection_id of the initialized connection if everything went well. */
299int crypto_connect(uint8_t * public_key, IP_Port ip_port) 299int crypto_connect(uint8_t *public_key, IP_Port ip_port)
300{ 300{
301 uint32_t i; 301 uint32_t i;
302 int id = getcryptconnection_id(public_key); 302 int id = getcryptconnection_id(public_key);
303 if(id != -1) { 303 if (id != -1) {
304 IP_Port c_ip = connection_ip(crypto_connections[id].number); 304 IP_Port c_ip = connection_ip(crypto_connections[id].number);
305 if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) 305 if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port)
306 return -1; 306 return -1;
307 } 307 }
308 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) 308 for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
309 { 309 if (crypto_connections[i].status == 0) {
310 if(crypto_connections[i].status == 0) {
311 int id = new_connection(ip_port); 310 int id = new_connection(ip_port);
312 if(id == -1) 311 if (id == -1)
313 return -1; 312 return -1;
314 crypto_connections[i].number = id; 313 crypto_connections[i].number = id;
315 crypto_connections[i].status = 1; 314 crypto_connections[i].status = 1;
@@ -317,8 +316,8 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
317 memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); 316 memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
318 crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); 317 crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
319 318
320 if(send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, 319 if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
321 crypto_connections[i].sessionpublic_key) == 1) 320 crypto_connections[i].sessionpublic_key) == 1)
322 { 321 {
323 increment_nonce(crypto_connections[i].recv_nonce); 322 increment_nonce(crypto_connections[i].recv_nonce);
324 return i; 323 return i;
@@ -336,21 +335,20 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
336 and the session public key for the connection in session_key 335 and the session public key for the connection in session_key
337 to accept it see: accept_crypto_inbound(...) 336 to accept it see: accept_crypto_inbound(...)
338 to refuse it just call kill_connection(...) on the connection id */ 337 to refuse it just call kill_connection(...) on the connection id */
339int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) 338int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
340{ 339{
341 uint32_t i; 340 uint32_t i;
342 for(i = 0; i < MAX_INCOMING; ++i) 341 for (i = 0; i < MAX_INCOMING; ++i) {
343 { 342 if (incoming_connections[i] != -1) {
344 if(incoming_connections[i] != -1) { 343 if (is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) {
345 if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) {
346 kill_connection(incoming_connections[i]); 344 kill_connection(incoming_connections[i]);
347 incoming_connections[i] = -1; 345 incoming_connections[i] = -1;
348 continue; 346 continue;
349 } 347 }
350 if(id_packet(incoming_connections[i]) == 2) { 348 if (id_packet(incoming_connections[i]) == 2) {
351 uint8_t temp_data[MAX_DATA_SIZE]; 349 uint8_t temp_data[MAX_DATA_SIZE];
352 uint16_t len = read_packet(incoming_connections[i], temp_data); 350 uint16_t len = read_packet(incoming_connections[i], temp_data);
353 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { 351 if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
354 int connection_id = incoming_connections[i]; 352 int connection_id = incoming_connections[i];
355 incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ 353 incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */
356 return connection_id; 354 return connection_id;
@@ -366,9 +364,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
366 return 1 if there was a problem. */ 364 return 1 if there was a problem. */
367int crypto_kill(int crypt_connection_id) 365int crypto_kill(int crypt_connection_id)
368{ 366{
369 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) 367 if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
370 return 1; 368 return 1;
371 if(crypto_connections[crypt_connection_id].status != 0) { 369 if (crypto_connections[crypt_connection_id].status != 0) {
372 crypto_connections[crypt_connection_id].status = 0; 370 crypto_connections[crypt_connection_id].status = 0;
373 kill_connection(crypto_connections[crypt_connection_id].number); 371 kill_connection(crypto_connections[crypt_connection_id].number);
374 crypto_connections[crypt_connection_id].number = ~0; 372 crypto_connections[crypt_connection_id].number = ~0;
@@ -380,18 +378,17 @@ int crypto_kill(int crypt_connection_id)
380/* accept an incoming connection using the parameters provided by crypto_inbound 378/* accept an incoming connection using the parameters provided by crypto_inbound
381 return -1 if not successful 379 return -1 if not successful
382 returns the crypt_connection_id if successful */ 380 returns the crypt_connection_id if successful */
383int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) 381int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
384{ 382{
385 uint32_t i; 383 uint32_t i;
386 if(connection_id == -1) 384 if (connection_id == -1)
387 return -1; 385 return -1;
388 /* 386 /*
389 if(getcryptconnection_id(public_key) != -1) 387 if(getcryptconnection_id(public_key) != -1)
390 { 388 {
391 return -1; 389 return -1;
392 }*/ 390 }*/
393 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) 391 for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
394 {
395 if(crypto_connections[i].status == 0) { 392 if(crypto_connections[i].status == 0) {
396 crypto_connections[i].number = connection_id; 393 crypto_connections[i].number = connection_id;
397 crypto_connections[i].status = 2; 394 crypto_connections[i].status = 2;
@@ -403,7 +400,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
403 400
404 crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); 401 crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
405 402
406 if(send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce, 403 if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce,
407 crypto_connections[i].sessionpublic_key) == 1) 404 crypto_connections[i].sessionpublic_key) == 1)
408 { 405 {
409 increment_nonce(crypto_connections[i].recv_nonce); 406 increment_nonce(crypto_connections[i].recv_nonce);
@@ -424,7 +421,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
424 4 if the connection is timed out and waiting to be killed */ 421 4 if the connection is timed out and waiting to be killed */
425int is_cryptoconnected(int crypt_connection_id) 422int is_cryptoconnected(int crypt_connection_id)
426{ 423{
427 if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) 424 if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
428 return crypto_connections[crypt_connection_id].status; 425 return crypto_connections[crypt_connection_id].status;
429 return 0; 426 return 0;
430} 427}
@@ -438,7 +435,7 @@ void new_keys()
438 435
439/* save the public and private keys to the keys array 436/* save the public and private keys to the keys array
440 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ 437 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
441void save_keys(uint8_t * keys) 438void save_keys(uint8_t *keys)
442{ 439{
443 memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES); 440 memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
444 memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES); 441 memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
@@ -446,7 +443,7 @@ void save_keys(uint8_t * keys)
446 443
447/* load the public and private keys from the keys array 444/* load the public and private keys from the keys array
448 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ 445 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
449void load_keys(uint8_t * keys) 446void load_keys(uint8_t *keys)
450{ 447{
451 memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES); 448 memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
452 memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); 449 memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
@@ -459,8 +456,8 @@ void load_keys(uint8_t * keys)
459int new_incoming(int id) 456int new_incoming(int id)
460{ 457{
461 uint32_t i; 458 uint32_t i;
462 for(i = 0; i < MAX_INCOMING; ++i) { 459 for (i = 0; i < MAX_INCOMING; ++i) {
463 if(incoming_connections[i] == -1) { 460 if (incoming_connections[i] == -1) {
464 incoming_connections[i] = id; 461 incoming_connections[i] = id;
465 return 0; 462 return 0;
466 } 463 }
@@ -473,7 +470,7 @@ int new_incoming(int id)
473static void handle_incomings() 470static void handle_incomings()
474{ 471{
475 int income; 472 int income;
476 while(1) { 473 while (1) {
477 income = incoming_connection(); 474 income = incoming_connection();
478 if(income == -1 || new_incoming(income) ) 475 if(income == -1 || new_incoming(income) )
479 break; 476 break;
@@ -484,22 +481,20 @@ static void handle_incomings()
484static void receive_crypto() 481static void receive_crypto()
485{ 482{
486 uint32_t i; 483 uint32_t i;
487 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) 484 for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
488 { 485 if (crypto_connections[i].status == 1) {
489 if(crypto_connections[i].status == 1)
490 {
491 uint8_t temp_data[MAX_DATA_SIZE]; 486 uint8_t temp_data[MAX_DATA_SIZE];
492 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 487 uint8_t secret_nonce[crypto_box_NONCEBYTES];
493 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 488 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
494 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 489 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
495 uint16_t len; 490 uint16_t len;
496 if(id_packet(crypto_connections[i].number) == 1) 491 if (id_packet(crypto_connections[i].number) == 1)
497 /* if the packet is a friend request drop it (because we are already friends) */ 492 /* if the packet is a friend request drop it (because we are already friends) */
498 len = read_packet(crypto_connections[i].number, temp_data); 493 len = read_packet(crypto_connections[i].number, temp_data);
499 if(id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */ 494 if (id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */
500 len = read_packet(crypto_connections[i].number, temp_data); 495 len = read_packet(crypto_connections[i].number, temp_data);
501 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { 496 if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
502 if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { 497 if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
503 memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); 498 memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES);
504 memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); 499 memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
505 increment_nonce(crypto_connections[i].sent_nonce); 500 increment_nonce(crypto_connections[i].sent_nonce);
@@ -510,15 +505,12 @@ static void receive_crypto()
510 } 505 }
511 } 506 }
512 } 507 }
513 else if(id_packet(crypto_connections[i].number) != -1) 508 else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does
514 /* This should not happen
515 kill the connection if it does */
516 crypto_kill(crypto_connections[i].number); 509 crypto_kill(crypto_connections[i].number);
517 510
518 } 511 }
519 if(crypto_connections[i].status == 2) 512 if (crypto_connections[i].status == 2) {
520 { 513 if (id_packet(crypto_connections[i].number) == 3) {
521 if(id_packet(crypto_connections[i].number) == 3) {
522 uint8_t temp_data[MAX_DATA_SIZE]; 514 uint8_t temp_data[MAX_DATA_SIZE];
523 uint8_t data[MAX_DATA_SIZE]; 515 uint8_t data[MAX_DATA_SIZE];
524 int length = read_packet(crypto_connections[i].number, temp_data); 516 int length = read_packet(crypto_connections[i].number, temp_data);
@@ -526,7 +518,7 @@ static void receive_crypto()
526 crypto_connections[i].sessionsecret_key, 518 crypto_connections[i].sessionsecret_key,
527 crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); 519 crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data);
528 uint32_t zero = 0; 520 uint32_t zero = 0;
529 if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { 521 if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) {
530 increment_nonce(crypto_connections[i].recv_nonce); 522 increment_nonce(crypto_connections[i].recv_nonce);
531 crypto_connections[i].status = 3; 523 crypto_connections[i].status = 3;
532 524
@@ -534,9 +526,7 @@ static void receive_crypto()
534 kill_connection_in(crypto_connections[i].number, 3000000); 526 kill_connection_in(crypto_connections[i].number, 3000000);
535 } 527 }
536 else 528 else
537 /* This should not happen 529 crypto_kill(crypto_connections[i].number); // This should not happen kill the connection if it does
538 kill the connection if it does */
539 crypto_kill(crypto_connections[i].number);
540 } 530 }
541 else if(id_packet(crypto_connections[i].number) != -1) 531 else if(id_packet(crypto_connections[i].number) != -1)
542 /* This should not happen 532 /* This should not happen
@@ -553,17 +543,17 @@ void initNetCrypto()
553 memset(crypto_connections, 0 ,sizeof(crypto_connections)); 543 memset(crypto_connections, 0 ,sizeof(crypto_connections));
554 memset(incoming_connections, -1 ,sizeof(incoming_connections)); 544 memset(incoming_connections, -1 ,sizeof(incoming_connections));
555 uint32_t i; 545 uint32_t i;
556 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) 546 for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
557 crypto_connections[i].number = ~0; 547 crypto_connections[i].number = ~0;
558} 548}
559 549
560static void killTimedout() 550static void killTimedout()
561{ 551{
562 uint32_t i; 552 uint32_t i;
563 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { 553 for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
564 if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) 554 if (crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
565 crypto_connections[i].status = 4; 555 crypto_connections[i].status = 4;
566 else if(is_connected(crypto_connections[i].number) == 4) { 556 else if (is_connected(crypto_connections[i].number) == 4) {
567 kill_connection(crypto_connections[i].number); 557 kill_connection(crypto_connections[i].number);
568 crypto_connections[i].number = ~0; 558 crypto_connections[i].number = ~0;
569 } 559 }
diff --git a/core/net_crypto.h b/core/net_crypto.h
index b5bab17a..d4fe1313 100644
--- a/core/net_crypto.h
+++ b/core/net_crypto.h
@@ -40,29 +40,29 @@ extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
40 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce 40 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
41 return -1 if there was a problem. 41 return -1 if there was a problem.
42 return length of encrypted data if everything was fine. */ 42 return length of encrypted data if everything was fine. */
43int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 43int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
44 uint8_t * plain, uint32_t length, uint8_t * encrypted); 44 uint8_t *plain, uint32_t length, uint8_t *encrypted);
45 45
46 46
47/* decrypts encrypted of length length to plain of length length - 16 using the 47/* decrypts encrypted of length length to plain of length length - 16 using the
48 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce 48 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
49 return -1 if there was a problem(decryption failed) 49 return -1 if there was a problem(decryption failed)
50 return length of plain data if everything was fine. */ 50 return length of plain data if everything was fine. */
51int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 51int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
52 uint8_t * encrypted, uint32_t length, uint8_t * plain); 52 uint8_t *encrypted, uint32_t length, uint8_t *plain);
53 53
54 54
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/* return 0 if there is no received data in the buffer 58/* return 0 if there is no received data in the buffer
59 return -1 if the packet was discarded. 59 return -1 if the packet was discarded.
60 return length of received data if successful */ 60 return length of received data if successful */
61int read_cryptpacket(int crypt_connection_id, uint8_t * data); 61int read_cryptpacket(int crypt_connection_id, uint8_t *data);
62 62
63/* return 0 if data could not be put in packet queue 63/* return 0 if data could not be put in packet queue
64 return 1 if data was put into the queue */ 64 return 1 if data was put into the queue */
65int 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);
66 66
67/* create a request to peer with public_key. 67/* create a request to peer with public_key.
68 packet must be an array of MAX_DATA_SIZE big. 68 packet must be an array of MAX_DATA_SIZE big.
@@ -70,18 +70,18 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
70 request_id is the id of the request (32 = friend request, 254 = ping request) 70 request_id is the id of the request (32 = friend request, 254 = ping request)
71 returns -1 on failure 71 returns -1 on failure
72 returns the length of the created packet on success */ 72 returns the length of the created packet on success */
73int 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);
74 74
75/* 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
76 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.
77 packet is the request packet and length is its length 77 packet is the request packet and length is its length
78 return -1 if not valid request. */ 78 return -1 if not valid request. */
79int 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);
80 80
81/* 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
82 returns -1 if failure 82 returns -1 if failure
83 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. */
84int crypto_connect(uint8_t * public_key, IP_Port ip_port); 84int crypto_connect(uint8_t *public_key, IP_Port ip_port);
85 85
86/* kill a crypto connection 86/* kill a crypto connection
87 return 0 if killed successfully 87 return 0 if killed successfully
@@ -95,12 +95,12 @@ int crypto_kill(int crypt_connection_id);
95 and the session public key for the connection in session_key 95 and the session public key for the connection in session_key
96 to accept it see: accept_crypto_inbound(...) 96 to accept it see: accept_crypto_inbound(...)
97 to refuse it just call kill_connection(...) on the connection id */ 97 to refuse it just call kill_connection(...) on the connection id */
98int 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);
99 99
100/* accept an incoming connection using the parameters provided by crypto_inbound 100/* accept an incoming connection using the parameters provided by crypto_inbound
101 return -1 if not successful 101 return -1 if not successful
102 returns the crypt_connection_id if successful */ 102 returns the crypt_connection_id if successful */
103int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); 103int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key);
104 104
105/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 105/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
106 (we have received a handshake but no empty data packet), 3 if the connection is established. 106 (we have received a handshake but no empty data packet), 3 if the connection is established.
diff --git a/core/network.c b/core/network.c
index 549427bf..160db880 100644
--- a/core/network.c
+++ b/core/network.c
@@ -79,13 +79,10 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
79 #else 79 #else
80 uint32_t addrlen = sizeof(addr); 80 uint32_t addrlen = sizeof(addr);
81 #endif 81 #endif
82 (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 82 (*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen);
83 if(*(int32_t *)length <= 0) 83 if (*(int32_t*)length <= 0)
84 { 84 return -1; /* nothing received or empty packet */
85 /* nothing received 85
86 or empty packet */
87 return -1;
88 }
89 ip_port->ip = addr.ip; 86 ip_port->ip = addr.ip;
90 ip_port->port = addr.port; 87 ip_port->port = addr.port;
91 return 0; 88 return 0;
@@ -101,7 +98,7 @@ int init_networking(IP ip, uint16_t port)
101{ 98{
102 #ifdef WIN32 99 #ifdef WIN32
103 WSADATA wsaData; 100 WSADATA wsaData;
104 if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) 101 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
105 return -1; 102 return -1;
106 #else 103 #else
107 srandom((uint32_t)current_time()); 104 srandom((uint32_t)current_time());
diff --git a/core/network.h b/core/network.h
index dcbd4160..8b22a09c 100644
--- a/core/network.h
+++ b/core/network.h
@@ -68,23 +68,20 @@ extern "C" {
68 68
69#define MAX_UDP_PACKET_SIZE 65507 69#define MAX_UDP_PACKET_SIZE 65507
70 70
71typedef union 71typedef union {
72{
73 uint8_t c[4]; 72 uint8_t c[4];
74 uint16_t s[2]; 73 uint16_t s[2];
75 uint32_t i; 74 uint32_t i;
76}IP; 75} IP;
77 76
78typedef struct 77typedef struct {
79{
80 IP ip; 78 IP ip;
81 uint16_t port; 79 uint16_t port;
82 /* not used for anything right now */ 80 /* not used for anything right now */
83 uint16_t padding; 81 uint16_t padding;
84}IP_Port; 82} IP_Port;
85 83
86typedef struct 84typedef struct {
87{
88 int16_t family; 85 int16_t family;
89 uint16_t port; 86 uint16_t port;
90 IP ip; 87 IP ip;
@@ -92,7 +89,7 @@ typedef struct
92 #ifdef ENABLE_IPV6 89 #ifdef ENABLE_IPV6
93 uint8_t zeroes2[12]; 90 uint8_t zeroes2[12];
94 #endif 91 #endif
95}ADDR; 92} ADDR;
96 93
97/* returns current time in milleseconds since the epoch. */ 94/* returns current time in milleseconds since the epoch. */
98uint64_t current_time(); 95uint64_t current_time();
@@ -104,12 +101,12 @@ uint32_t random_int();
104/* Basic network functions: */ 101/* Basic network functions: */
105 102
106/* Function to send packet(data) of length length to ip_port */ 103/* Function to send packet(data) of length length to ip_port */
107int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length); 104int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length);
108 105
109/* Function to receive data, ip and port of sender is put into ip_port 106/* Function to receive data, ip and port of sender is put into ip_port
110 the packet data into data 107 the packet data into data
111 the packet length into length. */ 108 the packet length into length. */
112int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); 109int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length);
113 110
114/* initialize networking 111/* initialize networking
115 bind to ip and port 112 bind to ip and port
@@ -117,7 +114,7 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
117 port is in host byte order (this means don't worry about it) 114 port is in host byte order (this means don't worry about it)
118 returns 0 if no problems 115 returns 0 if no problems
119 returns -1 if there were problems */ 116 returns -1 if there were problems */
120int init_networking(IP ip ,uint16_t port); 117int init_networking(IP ip, uint16_t port);
121 118
122/* function to cleanup networking stuff(doesn't do much right now) */ 119/* function to cleanup networking stuff(doesn't do much right now) */
123void shutdown_networking(); 120void shutdown_networking();