diff options
-rw-r--r-- | core/DHT.c | 194 | ||||
-rw-r--r-- | core/DHT.h | 64 | ||||
-rw-r--r-- | core/Lossless_UDP.c | 226 | ||||
-rw-r--r-- | core/Lossless_UDP.h | 74 | ||||
-rw-r--r-- | core/Messenger.c | 140 | ||||
-rw-r--r-- | core/Messenger.h | 120 |
6 files changed, 404 insertions, 414 deletions
@@ -35,13 +35,13 @@ typedef struct | |||
35 | uint32_t timestamp; | 35 | uint32_t timestamp; |
36 | uint32_t last_pinged; | 36 | uint32_t last_pinged; |
37 | }Client_data; | 37 | }Client_data; |
38 | //maximum number of clients stored per friend. | 38 | /* maximum number of clients stored per friend. */ |
39 | #define MAX_FRIEND_CLIENTS 8 | 39 | #define MAX_FRIEND_CLIENTS 8 |
40 | typedef struct | 40 | typedef struct |
41 | { | 41 | { |
42 | uint8_t client_id[CLIENT_ID_SIZE]; | 42 | uint8_t client_id[CLIENT_ID_SIZE]; |
43 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 43 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
44 | uint32_t lastgetnode;//time at which the last get_nodes request was sent. | 44 | uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ |
45 | 45 | ||
46 | }Friend; | 46 | }Friend; |
47 | 47 | ||
@@ -60,12 +60,12 @@ typedef struct | |||
60 | 60 | ||
61 | }Pinged; | 61 | }Pinged; |
62 | 62 | ||
63 | //Our client id/public key | 63 | /* Our client id/public key */ |
64 | uint8_t self_public_key[CLIENT_ID_SIZE]; | 64 | uint8_t self_public_key[CLIENT_ID_SIZE]; |
65 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 65 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
66 | 66 | ||
67 | //TODO: Move these out of here and put them into the .c file. | 67 | /* TODO: Move these out of here and put them into the .c file. |
68 | //A list of the clients mathematically closest to ours. | 68 | A list of the clients mathematically closest to ours. */ |
69 | #define LCLIENT_LIST 32 | 69 | #define LCLIENT_LIST 32 |
70 | static Client_data close_clientlist[LCLIENT_LIST]; | 70 | static Client_data close_clientlist[LCLIENT_LIST]; |
71 | 71 | ||
@@ -74,7 +74,7 @@ static Client_data close_clientlist[LCLIENT_LIST]; | |||
74 | static Friend * friends_list; | 74 | static Friend * friends_list; |
75 | static uint16_t num_friends; | 75 | static uint16_t num_friends; |
76 | 76 | ||
77 | //The list of ip ports along with the ping_id of what we sent them and a timestamp | 77 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ |
78 | #define LPING_ARRAY 128 | 78 | #define LPING_ARRAY 128 |
79 | 79 | ||
80 | static Pinged pings[LPING_ARRAY]; | 80 | static Pinged pings[LPING_ARRAY]; |
@@ -84,11 +84,11 @@ static Pinged pings[LPING_ARRAY]; | |||
84 | static Pinged send_nodes[LSEND_NODES_ARRAY]; | 84 | static Pinged send_nodes[LSEND_NODES_ARRAY]; |
85 | 85 | ||
86 | 86 | ||
87 | //Compares client_id1 and client_id2 with client_id | 87 | /* Compares client_id1 and client_id2 with client_id |
88 | //return 0 if both are same distance | 88 | return 0 if both are same distance |
89 | //return 1 if client_id1 is closer. | 89 | return 1 if client_id1 is closer |
90 | //return 2 if client_id2 is closer. | 90 | return 2 if client_id2 is closer */ |
91 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)//tested | 91 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ |
92 | { | 92 | { |
93 | uint32_t i; | 93 | uint32_t i; |
94 | for(i = 0; i < CLIENT_ID_SIZE; ++i) | 94 | for(i = 0; i < CLIENT_ID_SIZE; ++i) |
@@ -108,11 +108,11 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)/ | |||
108 | 108 | ||
109 | } | 109 | } |
110 | 110 | ||
111 | //check if client with client_id is already in list of length length. | 111 | /* check if client with client_id is already in list of length length. |
112 | //if it is set it's corresponding timestamp to current time. | 112 | if it is set it's corresponding timestamp to current time. |
113 | //if the id is already in the list with a different ip_port, update it. | 113 | if the id is already in the list with a different ip_port, update it. |
114 | //return True(1) or False(0) | 114 | return True(1) or False(0) |
115 | //TODO: maybe optimize this. | 115 | TODO: maybe optimize this. */ |
116 | int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) | 116 | int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) |
117 | { | 117 | { |
118 | uint32_t i; | 118 | uint32_t i; |
@@ -122,7 +122,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
122 | { | 122 | { |
123 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) | 123 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) |
124 | { | 124 | { |
125 | //Refresh the client timestamp. | 125 | /* Refresh the client timestamp. */ |
126 | list[i].timestamp = temp_time; | 126 | list[i].timestamp = temp_time; |
127 | list[i].ip_port.ip.i = ip_port.ip.i; | 127 | list[i].ip_port.ip.i = ip_port.ip.i; |
128 | list[i].ip_port.port = ip_port.port; | 128 | list[i].ip_port.port = ip_port.port; |
@@ -133,8 +133,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
133 | 133 | ||
134 | } | 134 | } |
135 | 135 | ||
136 | //check if client with client_id is already in node format list of length length. | 136 | /* check if client with client_id is already in node format list of length length. |
137 | //return True(1) or False(0) | 137 | return True(1) or False(0) */ |
138 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | 138 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) |
139 | { | 139 | { |
140 | uint32_t i; | 140 | uint32_t i; |
@@ -152,15 +152,15 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | |||
152 | 152 | ||
153 | 153 | ||
154 | 154 | ||
155 | //the number of seconds for a non responsive node to become bad. | 155 | /* the number of seconds for a non responsive node to become bad. */ |
156 | #define BAD_NODE_TIMEOUT 130 | 156 | #define BAD_NODE_TIMEOUT 130 |
157 | //The max number of nodes to send with send nodes. | 157 | /* the max number of nodes to send with send nodes. */ |
158 | #define MAX_SENT_NODES 8 | 158 | #define MAX_SENT_NODES 8 |
159 | 159 | ||
160 | 160 | ||
161 | //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: |
162 | //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. |
163 | //TODO: Make this function much more efficient. | 163 | TODO: Make this function much more efficient. */ |
164 | int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | 164 | int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) |
165 | { | 165 | { |
166 | uint32_t i, j, k; | 166 | uint32_t i, j, k; |
@@ -170,7 +170,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
170 | { | 170 | { |
171 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && | 171 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && |
172 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) | 172 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) |
173 | //if node is good and not already in list. | 173 | /* if node is good and not already in list. */ |
174 | { | 174 | { |
175 | if(num_nodes < MAX_SENT_NODES) | 175 | if(num_nodes < MAX_SENT_NODES) |
176 | { | 176 | { |
@@ -196,7 +196,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
196 | { | 196 | { |
197 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && | 197 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && |
198 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) | 198 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) |
199 | //if node is good and not already in list. | 199 | /* if node is good and not already in list. */ |
200 | { | 200 | { |
201 | if(num_nodes < MAX_SENT_NODES) | 201 | if(num_nodes < MAX_SENT_NODES) |
202 | { | 202 | { |
@@ -223,16 +223,16 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
223 | 223 | ||
224 | 224 | ||
225 | 225 | ||
226 | //replace first bad (or empty) node with this one | 226 | /* replace first bad (or empty) node with this one |
227 | //return 0 if successful | 227 | return 0 if successful |
228 | //return 1 if not (list contains no bad nodes) | 228 | return 1 if not (list contains no bad nodes) */ |
229 | int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)//tested | 229 | int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */ |
230 | { | 230 | { |
231 | uint32_t i; | 231 | uint32_t i; |
232 | uint32_t temp_time = unix_time(); | 232 | uint32_t temp_time = unix_time(); |
233 | for(i = 0; i < length; ++i) | 233 | for(i = 0; i < length; ++i) |
234 | { | 234 | { |
235 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time)//if node is bad. | 235 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ |
236 | { | 236 | { |
237 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 237 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
238 | list[i].ip_port = ip_port; | 238 | list[i].ip_port = ip_port; |
@@ -244,7 +244,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
244 | 244 | ||
245 | } | 245 | } |
246 | 246 | ||
247 | //replace the first good node that is further to the comp_client_id than that of the client_id in the list | 247 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ |
248 | int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) | 248 | int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) |
249 | { | 249 | { |
250 | uint32_t i; | 250 | uint32_t i; |
@@ -264,18 +264,18 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
264 | 264 | ||
265 | } | 265 | } |
266 | 266 | ||
267 | //Attempt to add client with ip_port and client_id to the friends client list and close_clientlist | 267 | /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ |
268 | void addto_lists(IP_Port ip_port, uint8_t * client_id) | 268 | void addto_lists(IP_Port ip_port, uint8_t * client_id) |
269 | { | 269 | { |
270 | uint32_t i; | 270 | uint32_t i; |
271 | 271 | ||
272 | //NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. | 272 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ |
273 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 273 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) |
274 | { | 274 | { |
275 | 275 | ||
276 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 276 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) |
277 | { | 277 | { |
278 | //if we can't replace bad nodes we try replacing good ones | 278 | /* if we can't replace bad nodes we try replacing good ones */ |
279 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); | 279 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); |
280 | } | 280 | } |
281 | 281 | ||
@@ -287,7 +287,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
287 | 287 | ||
288 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | 288 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) |
289 | { | 289 | { |
290 | //if we can't replace bad nodes we try replacing good ones | 290 | /* if we can't replace bad nodes we try replacing good ones. */ |
291 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); | 291 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); |
292 | } | 292 | } |
293 | } | 293 | } |
@@ -295,13 +295,13 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
295 | } | 295 | } |
296 | 296 | ||
297 | 297 | ||
298 | //ping timeout in seconds | 298 | /* ping timeout in seconds */ |
299 | #define PING_TIMEOUT 5 | 299 | #define PING_TIMEOUT 5 |
300 | //check if we are currently pinging an ip_port and/or a ping_id | 300 | /* check if we are currently pinging an ip_port and/or a ping_id |
301 | //Variables with values of zero will not be checked. | 301 | variables with values of zero will not be checked. |
302 | //if we are already, return 1 | 302 | if we are already, return 1 |
303 | //else return 0 | 303 | else return 0 |
304 | //TODO: Maybe optimize this | 304 | TODO: optimize this */ |
305 | int is_pinging(IP_Port ip_port, uint64_t ping_id) | 305 | int is_pinging(IP_Port ip_port, uint64_t ping_id) |
306 | { | 306 | { |
307 | uint32_t i; | 307 | uint32_t i; |
@@ -341,7 +341,7 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
341 | } | 341 | } |
342 | 342 | ||
343 | 343 | ||
344 | //Same as last function but for get_node requests. | 344 | /* Same as last function but for get_node requests. */ |
345 | int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | 345 | int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) |
346 | { | 346 | { |
347 | uint32_t i; | 347 | uint32_t i; |
@@ -381,10 +381,10 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
381 | } | 381 | } |
382 | 382 | ||
383 | 383 | ||
384 | //Add a new ping request to the list of ping requests | 384 | /* Add a new ping request to the list of ping requests |
385 | //returns the ping_id to put in the ping request | 385 | returns the ping_id to put in the ping request |
386 | //returns 0 if problem. | 386 | returns 0 if problem. |
387 | //TODO: Maybe optimize this | 387 | TODO: optimize this */ |
388 | uint64_t add_pinging(IP_Port ip_port) | 388 | uint64_t add_pinging(IP_Port ip_port) |
389 | { | 389 | { |
390 | uint32_t i, j; | 390 | uint32_t i, j; |
@@ -408,7 +408,7 @@ uint64_t add_pinging(IP_Port ip_port) | |||
408 | 408 | ||
409 | } | 409 | } |
410 | 410 | ||
411 | //Same but for get node requests | 411 | /* Same but for get node requests */ |
412 | uint64_t add_gettingnodes(IP_Port ip_port) | 412 | uint64_t add_gettingnodes(IP_Port ip_port) |
413 | { | 413 | { |
414 | uint32_t i, j; | 414 | uint32_t i, j; |
@@ -434,11 +434,11 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
434 | 434 | ||
435 | 435 | ||
436 | 436 | ||
437 | //send a ping request | 437 | /* send a ping request |
438 | //Ping request only works if none has been sent to that ip/port in the last 5 seconds. | 438 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ |
439 | static int pingreq(IP_Port ip_port, uint8_t * public_key) | 439 | static int pingreq(IP_Port ip_port, uint8_t * public_key) |
440 | { | 440 | { |
441 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself | 441 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
442 | { | 442 | { |
443 | return 1; | 443 | return 1; |
444 | } | 444 | } |
@@ -474,10 +474,10 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
474 | } | 474 | } |
475 | 475 | ||
476 | 476 | ||
477 | //send a ping response | 477 | /* send a ping response */ |
478 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | 478 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) |
479 | { | 479 | { |
480 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself | 480 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
481 | { | 481 | { |
482 | return 1; | 482 | return 1; |
483 | } | 483 | } |
@@ -501,10 +501,10 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
501 | 501 | ||
502 | } | 502 | } |
503 | 503 | ||
504 | //send a getnodes request | 504 | /* send a getnodes request */ |
505 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 505 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) |
506 | { | 506 | { |
507 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself | 507 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
508 | { | 508 | { |
509 | return 1; | 509 | return 1; |
510 | } | 510 | } |
@@ -545,10 +545,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
545 | } | 545 | } |
546 | 546 | ||
547 | 547 | ||
548 | //send a send nodes response | 548 | /* send a send nodes response */ |
549 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) | 549 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) |
550 | { | 550 | { |
551 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself | 551 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
552 | { | 552 | { |
553 | return 1; | 553 | return 1; |
554 | } | 554 | } |
@@ -591,10 +591,9 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
591 | 591 | ||
592 | 592 | ||
593 | 593 | ||
594 | 594 | /* Packet handling functions | |
595 | //Packet handling functions | 595 | One to handle each types of packets we receive |
596 | //One to handle each types of packets we receive | 596 | return 0 if handled correctly, 1 if packet is bad. */ |
597 | //return 0 if handled correctly, 1 if packet is bad. | ||
598 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | 597 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) |
599 | { | 598 | { |
600 | uint64_t ping_id; | 599 | uint64_t ping_id; |
@@ -602,7 +601,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
602 | { | 601 | { |
603 | return 1; | 602 | return 1; |
604 | } | 603 | } |
605 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. | 604 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ |
606 | { | 605 | { |
607 | return 1; | 606 | return 1; |
608 | } | 607 | } |
@@ -620,7 +619,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
620 | 619 | ||
621 | pingres(source, packet + 1, ping_id); | 620 | pingres(source, packet + 1, ping_id); |
622 | 621 | ||
623 | pingreq(source, packet + 1);//TODO: make this smarter? | 622 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
624 | 623 | ||
625 | return 0; | 624 | return 0; |
626 | 625 | ||
@@ -633,7 +632,7 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
633 | { | 632 | { |
634 | return 1; | 633 | return 1; |
635 | } | 634 | } |
636 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. | 635 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ |
637 | { | 636 | { |
638 | return 1; | 637 | return 1; |
639 | } | 638 | } |
@@ -664,7 +663,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
664 | { | 663 | { |
665 | return 1; | 664 | return 1; |
666 | } | 665 | } |
667 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. | 666 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ |
668 | { | 667 | { |
669 | return 1; | 668 | return 1; |
670 | } | 669 | } |
@@ -684,7 +683,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
684 | memcpy(&ping_id, plain, sizeof(ping_id)); | 683 | memcpy(&ping_id, plain, sizeof(ping_id)); |
685 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); | 684 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); |
686 | 685 | ||
687 | pingreq(source, packet + 1);//TODO: make this smarter? | 686 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
688 | 687 | ||
689 | return 0; | 688 | return 0; |
690 | 689 | ||
@@ -737,9 +736,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
737 | 736 | ||
738 | } | 737 | } |
739 | 738 | ||
740 | //END of packet handling functions | 739 | /* END of packet handling functions */ |
741 | |||
742 | |||
743 | 740 | ||
744 | int DHT_addfriend(uint8_t * client_id) | 741 | int DHT_addfriend(uint8_t * client_id) |
745 | { | 742 | { |
@@ -766,15 +763,13 @@ int DHT_addfriend(uint8_t * client_id) | |||
766 | 763 | ||
767 | 764 | ||
768 | 765 | ||
769 | |||
770 | |||
771 | int DHT_delfriend(uint8_t * client_id) | 766 | int DHT_delfriend(uint8_t * client_id) |
772 | { | 767 | { |
773 | uint32_t i; | 768 | uint32_t i; |
774 | Friend * temp; | 769 | Friend * temp; |
775 | for(i = 0; i < num_friends; ++i) | 770 | for(i = 0; i < num_friends; ++i) |
776 | { | 771 | { |
777 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | 772 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ |
778 | { | 773 | { |
779 | --num_friends; | 774 | --num_friends; |
780 | if(num_friends != i) | 775 | if(num_friends != i) |
@@ -795,7 +790,7 @@ int DHT_delfriend(uint8_t * client_id) | |||
795 | 790 | ||
796 | 791 | ||
797 | 792 | ||
798 | //TODO: Optimize this. | 793 | /* TODO: Optimize this. */ |
799 | IP_Port DHT_getfriendip(uint8_t * client_id) | 794 | IP_Port DHT_getfriendip(uint8_t * client_id) |
800 | { | 795 | { |
801 | uint32_t i, j; | 796 | uint32_t i, j; |
@@ -803,7 +798,7 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
803 | uint32_t temp_time = unix_time(); | 798 | uint32_t temp_time = unix_time(); |
804 | for(i = 0; i < num_friends; ++i) | 799 | for(i = 0; i < num_friends; ++i) |
805 | { | 800 | { |
806 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | 801 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ |
807 | { | 802 | { |
808 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 803 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
809 | { | 804 | { |
@@ -821,9 +816,7 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
821 | empty.ip.i = 1; | 816 | empty.ip.i = 1; |
822 | return empty; | 817 | return empty; |
823 | 818 | ||
824 | } | 819 | } |
825 | |||
826 | |||
827 | 820 | ||
828 | 821 | ||
829 | 822 | ||
@@ -846,22 +839,22 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
846 | return 1; | 839 | return 1; |
847 | 840 | ||
848 | } | 841 | } |
849 | 842 | ||
850 | return 0; | 843 | return 0; |
851 | 844 | ||
852 | } | 845 | } |
853 | 846 | ||
854 | //The timeout after which a node is discarded completely. | 847 | /* The timeout after which a node is discarded completely. */ |
855 | #define Kill_NODE_TIMEOUT 300 | 848 | #define Kill_NODE_TIMEOUT 300 |
856 | 849 | ||
857 | //ping interval in seconds for each node in our lists. | 850 | /* ping interval in seconds for each node in our lists. */ |
858 | #define PING_INTERVAL 60 | 851 | #define PING_INTERVAL 60 |
859 | 852 | ||
860 | //ping interval in seconds for each random sending of a get nodes request. | 853 | /* ping interval in seconds for each random sending of a get nodes request. */ |
861 | #define GET_NODE_INTERVAL 10 | 854 | #define GET_NODE_INTERVAL 10 |
862 | 855 | ||
863 | //Ping each client in the "friends" list every 60 seconds. | 856 | /* Ping each client in the "friends" list every 60 seconds. |
864 | //Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. | 857 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ |
865 | 858 | ||
866 | 859 | ||
867 | void doDHTFriends() | 860 | void doDHTFriends() |
@@ -876,14 +869,14 @@ void doDHTFriends() | |||
876 | { | 869 | { |
877 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 870 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
878 | { | 871 | { |
879 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. | 872 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ |
880 | { | 873 | { |
881 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) | 874 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) |
882 | { | 875 | { |
883 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); | 876 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); |
884 | friends_list[i].client_list[j].last_pinged = temp_time; | 877 | friends_list[i].client_list[j].last_pinged = temp_time; |
885 | } | 878 | } |
886 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. | 879 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ |
887 | { | 880 | { |
888 | index[num_nodes] = j; | 881 | index[num_nodes] = j; |
889 | ++num_nodes; | 882 | ++num_nodes; |
@@ -903,9 +896,9 @@ void doDHTFriends() | |||
903 | 896 | ||
904 | static uint32_t close_lastgetnodes; | 897 | static uint32_t close_lastgetnodes; |
905 | 898 | ||
906 | //Ping each client in the close nodes list every 60 seconds. | 899 | /* Ping each client in the close nodes list every 60 seconds. |
907 | //Send a get nodes request every 20 seconds to a random good node in the list. | 900 | Send a get nodes request every 20 seconds to a random good node in the list. */ |
908 | void doClose()//tested | 901 | void doClose() /* tested */ |
909 | { | 902 | { |
910 | uint32_t i; | 903 | uint32_t i; |
911 | uint32_t temp_time = unix_time(); | 904 | uint32_t temp_time = unix_time(); |
@@ -915,14 +908,14 @@ void doClose()//tested | |||
915 | 908 | ||
916 | for(i = 0; i < LCLIENT_LIST; ++i) | 909 | for(i = 0; i < LCLIENT_LIST; ++i) |
917 | { | 910 | { |
918 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. | 911 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ |
919 | { | 912 | { |
920 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) | 913 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) |
921 | { | 914 | { |
922 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); | 915 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); |
923 | close_clientlist[i].last_pinged = temp_time; | 916 | close_clientlist[i].last_pinged = temp_time; |
924 | } | 917 | } |
925 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. | 918 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ |
926 | { | 919 | { |
927 | index[num_nodes] = i; | 920 | index[num_nodes] = i; |
928 | ++num_nodes; | 921 | ++num_nodes; |
@@ -951,31 +944,28 @@ void doDHT() | |||
951 | 944 | ||
952 | 945 | ||
953 | 946 | ||
954 | |||
955 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) | 947 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) |
956 | { | 948 | { |
957 | 949 | getnodes(ip_port, public_key, self_public_key); | |
958 | getnodes(ip_port, public_key, self_public_key); | ||
959 | |||
960 | } | 950 | } |
961 | 951 | ||
962 | 952 | ||
963 | //get the size of the DHT (for saving) | 953 | /* get the size of the DHT (for saving) */ |
964 | uint32_t DHT_size() | 954 | uint32_t DHT_size() |
965 | { | 955 | { |
966 | return sizeof(close_clientlist) + sizeof(Friend) * num_friends; | 956 | return sizeof(close_clientlist) + sizeof(Friend) * num_friends; |
967 | } | 957 | } |
968 | 958 | ||
969 | //save the DHT in data where data is an array of size DHT_size() | 959 | /* save the DHT in data where data is an array of size DHT_size() */ |
970 | void DHT_save(uint8_t * data) | 960 | void DHT_save(uint8_t * data) |
971 | { | 961 | { |
972 | memcpy(data, close_clientlist, sizeof(close_clientlist)); | 962 | memcpy(data, close_clientlist, sizeof(close_clientlist)); |
973 | memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends); | 963 | memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends); |
974 | } | 964 | } |
975 | 965 | ||
976 | //load the DHT from data of size size; | 966 | /* load the DHT from data of size size; |
977 | //return -1 if failure | 967 | return -1 if failure |
978 | //return 0 if success | 968 | return 0 if success */ |
979 | int DHT_load(uint8_t * data, uint32_t size) | 969 | int DHT_load(uint8_t * data, uint32_t size) |
980 | { | 970 | { |
981 | if(size < sizeof(close_clientlist)) | 971 | if(size < sizeof(close_clientlist)) |
@@ -987,7 +977,7 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
987 | return -1; | 977 | return -1; |
988 | } | 978 | } |
989 | uint32_t i, j; | 979 | uint32_t i, j; |
990 | //uint32_t temp_time = unix_time(); | 980 | /* uint32_t temp_time = unix_time(); */ |
991 | uint16_t temp; | 981 | uint16_t temp; |
992 | 982 | ||
993 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); | 983 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); |
@@ -1021,8 +1011,8 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1021 | return 0; | 1011 | return 0; |
1022 | } | 1012 | } |
1023 | 1013 | ||
1024 | //returns 0 if we are not connected to the DHT | 1014 | /* returns 0 if we are not connected to the DHT |
1025 | //returns 1 if we are | 1015 | returns 1 if we are */ |
1026 | int DHT_isconnected() | 1016 | int DHT_isconnected() |
1027 | { | 1017 | { |
1028 | uint32_t i; | 1018 | uint32_t i; |
@@ -28,66 +28,66 @@ | |||
28 | 28 | ||
29 | #include "net_crypto.h" | 29 | #include "net_crypto.h" |
30 | 30 | ||
31 | //Current time, unix format | 31 | /* Current time, unix format */ |
32 | #define unix_time() ((uint32_t)time(NULL)) | 32 | #define unix_time() ((uint32_t)time(NULL)) |
33 | 33 | ||
34 | //size of the client_id in bytes | 34 | /* size of the client_id in bytes */ |
35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
36 | 36 | ||
37 | 37 | ||
38 | 38 | ||
39 | //Add a new friend to the friends list | 39 | /* Add a new friend to the friends list |
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) */ |
43 | int DHT_addfriend(uint8_t * client_id); | 43 | int 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) */ |
49 | int DHT_delfriend(uint8_t * client_id); | 49 | int DHT_delfriend(uint8_t * client_id); |
50 | 50 | ||
51 | 51 | ||
52 | //Get ip of friend | 52 | /* Get ip of friend |
53 | //client_id must be CLIENT_ID_SIZE bytes long. | 53 | client_id must be CLIENT_ID_SIZE bytes long. |
54 | //ip must be 4 bytes long. | 54 | ip must be 4 bytes long. |
55 | //port must be 2 bytes long. | 55 | port must be 2 bytes long. |
56 | //returns ip if success | 56 | returns ip if success |
57 | //returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 57 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) |
58 | //returns ip of 1 if friend is not in list. | 58 | returns ip of 1 if friend is not in list. */ |
59 | IP_Port DHT_getfriendip(uint8_t * client_id); | 59 | IP_Port DHT_getfriendip(uint8_t * client_id); |
60 | 60 | ||
61 | 61 | ||
62 | //Run this function at least a couple times per second (It's the main loop) | 62 | /* Run this function at least a couple times per second (It's the main loop) */ |
63 | void doDHT(); | 63 | void doDHT(); |
64 | 64 | ||
65 | //if we receive a DHT packet we call this function so it can be handled. | 65 | /* if we receive a DHT packet we call this function so it can be handled. |
66 | //Return 0 if packet is handled correctly. | 66 | return 0 if packet is handled correctly. |
67 | //return 1 if it didn't handle the packet or if the packet was shit. | 67 | return 1 if it didn't handle the packet or if the packet was shit. */ |
68 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 68 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); |
69 | 69 | ||
70 | //Use this function to bootstrap the client | 70 | /* Use this function to bootstrap the client |
71 | //Sends a get nodes request to the given node with ip port and public_key | 71 | Sends a get nodes request to the given node with ip port and public_key */ |
72 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); | 72 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); |
73 | 73 | ||
74 | 74 | ||
75 | //SAVE/LOAD functions | 75 | /* SAVE/LOAD functions */ |
76 | 76 | ||
77 | //get the size of the DHT (for saving) | 77 | /* get the size of the DHT (for saving) */ |
78 | uint32_t DHT_size(); | 78 | uint32_t DHT_size(); |
79 | 79 | ||
80 | 80 | ||
81 | //save the DHT in data where data is an array of size DHT_size() | 81 | /* save the DHT in data where data is an array of size DHT_size() */ |
82 | void DHT_save(uint8_t * data); | 82 | void DHT_save(uint8_t * data); |
83 | 83 | ||
84 | //load the DHT from data of size size; | 84 | /* load the DHT from data of size size; |
85 | //return -1 if failure | 85 | return -1 if failure |
86 | //return 0 if success | 86 | return 0 if success */ |
87 | int DHT_load(uint8_t * data, uint32_t size); | 87 | int DHT_load(uint8_t * data, uint32_t size); |
88 | 88 | ||
89 | //returns 0 if we are not connected to the DHT | 89 | /* returns 0 if we are not connected to the DHT |
90 | //returns 1 if we are | 90 | returns 1 if we are */ |
91 | int DHT_isconnected(); | 91 | int DHT_isconnected(); |
92 | 92 | ||
93 | #endif | 93 | #endif |
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 427d764f..710bcc4c 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -21,29 +21,29 @@ | |||
21 | along with Tox. If not, see <http://www.gnu.org/licenses/>. | 21 | along with Tox. If not, see <http://www.gnu.org/licenses/>. |
22 | 22 | ||
23 | */ | 23 | */ |
24 | //TODO: clean this file a bit. | 24 | /* TODO: clean this file a bit. |
25 | //There are a couple of useless variables to get rid of. | 25 | There are a couple of useless variables to get rid of. */ |
26 | #include "Lossless_UDP.h" | 26 | #include "Lossless_UDP.h" |
27 | 27 | ||
28 | 28 | ||
29 | 29 | ||
30 | //maximum data packets in sent and receive queues. | 30 | /* maximum data packets in sent and receive queues. */ |
31 | #define MAX_QUEUE_NUM 16 | 31 | #define MAX_QUEUE_NUM 16 |
32 | 32 | ||
33 | //maximum length of the data in the data packets | 33 | /* maximum length of the data in the data packets */ |
34 | //#define MAX_DATA_SIZE 1024 //defined in Lossless_UDP.h | 34 | /* #define MAX_DATA_SIZE 1024 */ /* defined in Lossless_UDP.h */ |
35 | 35 | ||
36 | //maximum number of data packets in the buffer | 36 | /* maximum number of data packets in the buffer */ |
37 | #define BUFFER_PACKET_NUM (16-1) | 37 | #define BUFFER_PACKET_NUM (16-1) |
38 | 38 | ||
39 | //Lossless UDP connection timeout. | 39 | /* Lossless UDP connection timeout. |
40 | //timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT | 40 | timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */ |
41 | #define CONNEXION_TIMEOUT 5 | 41 | #define CONNEXION_TIMEOUT 5 |
42 | 42 | ||
43 | //initial amount of sync/hanshake packets to send per second. | 43 | /* initial amount of sync/hanshake packets to send per second. */ |
44 | #define SYNC_RATE 2 | 44 | #define SYNC_RATE 2 |
45 | 45 | ||
46 | //initial send rate of data. | 46 | /* initial send rate of data. */ |
47 | #define DATA_SYNC_RATE 30 | 47 | #define DATA_SYNC_RATE 30 |
48 | 48 | ||
49 | typedef struct | 49 | typedef struct |
@@ -55,37 +55,37 @@ typedef struct | |||
55 | typedef struct | 55 | typedef struct |
56 | { | 56 | { |
57 | IP_Port ip_port; | 57 | IP_Port ip_port; |
58 | uint8_t status;//0 if connection is dead, 1 if attempting handshake, | 58 | uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, |
59 | //2 if handshake is done (we start sending SYNC packets) | 59 | 2 if handshake is done (we start sending SYNC packets) |
60 | //3 if we are sending SYNC packets and can send data | 60 | 3 if we are sending SYNC packets and can send data |
61 | //4 if the connection has timed out. | 61 | 4 if the connection has timed out. */ |
62 | 62 | ||
63 | uint8_t inbound; //1 or 2 if connection was initiated by someone else, 0 if not. | 63 | uint8_t inbound; /* 1 or 2 if connection was initiated by someone else, 0 if not. |
64 | //2 if incoming_connection() has not returned it yet, 1 if it has. | 64 | 2 if incoming_connection() has not returned it yet, 1 if it has. */ |
65 | 65 | ||
66 | uint16_t SYNC_rate;//current SYNC packet send rate packets per second. | 66 | uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */ |
67 | uint16_t data_rate;//current data packet send rate packets per second. | 67 | uint16_t data_rate; /* current data packet send rate packets per second. */ |
68 | uint64_t last_SYNC; //time at which our last SYNC packet was sent. | 68 | uint64_t last_SYNC; /* time at which our last SYNC packet was sent. */ |
69 | uint64_t last_sent; //time at which our last data or handshake packet was sent. | 69 | uint64_t last_sent; /* time at which our last data or handshake packet was sent. */ |
70 | uint64_t last_recvSYNC; //time at which we last received a SYNC packet from the other | 70 | uint64_t last_recvSYNC; /* time at which we last received a SYNC packet from the other */ |
71 | uint64_t last_recvdata; //time at which we last received a DATA packet from the other | 71 | uint64_t last_recvdata; /* time at which we last received a DATA packet from the other */ |
72 | uint64_t killat; //time at which to kill the connection | 72 | uint64_t killat; /* time at which to kill the connection */ |
73 | Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer. | 73 | Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */ |
74 | Data recvbuffer[MAX_QUEUE_NUM];//packet receive buffer. | 74 | Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */ |
75 | uint32_t handshake_id1; | 75 | uint32_t handshake_id1; |
76 | uint32_t handshake_id2; | 76 | uint32_t handshake_id2; |
77 | uint32_t recv_packetnum; //number of data packets received (also used as handshake_id1) | 77 | uint32_t recv_packetnum; /* number of data packets received (also used as handshake_id1) */ |
78 | uint32_t orecv_packetnum; //number of packets received by the other peer | 78 | uint32_t orecv_packetnum; /* number of packets received by the other peer */ |
79 | uint32_t sent_packetnum; //number of data packets sent | 79 | uint32_t sent_packetnum; /* number of data packets sent */ |
80 | uint32_t osent_packetnum; //number of packets sent by the other peer. | 80 | uint32_t osent_packetnum; /* number of packets sent by the other peer. */ |
81 | uint32_t sendbuff_packetnum; //number of latest packet written onto the sendbuffer | 81 | uint32_t sendbuff_packetnum; /* number of latest packet written onto the sendbuffer */ |
82 | uint32_t successful_sent;//we know all packets before that number were successfully sent | 82 | uint32_t successful_sent; /* we know all packets before that number were successfully sent */ |
83 | uint32_t successful_read;//packet number of last packet read with the read_packet function | 83 | uint32_t successful_read; /* packet number of last packet read with the read_packet function */ |
84 | uint32_t req_packets[BUFFER_PACKET_NUM]; //list of currently requested packet numbers(by the other person) | 84 | uint32_t req_packets[BUFFER_PACKET_NUM]; /* list of currently requested packet numbers(by the other person) */ |
85 | uint16_t num_req_paquets; //total number of currently requested packets(by the other person) | 85 | uint16_t num_req_paquets; /* total number of currently requested packets(by the other person) */ |
86 | uint8_t recv_counter; | 86 | uint8_t recv_counter; |
87 | uint8_t send_counter; | 87 | uint8_t send_counter; |
88 | uint8_t timeout; //connection timeout in seconds. | 88 | uint8_t timeout; /* connection timeout in seconds. */ |
89 | }Connection; | 89 | }Connection; |
90 | 90 | ||
91 | 91 | ||
@@ -93,13 +93,13 @@ typedef struct | |||
93 | 93 | ||
94 | static Connection connections[MAX_CONNECTIONS]; | 94 | static Connection connections[MAX_CONNECTIONS]; |
95 | 95 | ||
96 | //static uint32_t numconnections; | 96 | /* static uint32_t numconnections; */ |
97 | 97 | ||
98 | //Functions | 98 | /* Functions */ |
99 | 99 | ||
100 | //get connection id from IP_Port | 100 | /* get connection id from IP_Port |
101 | //return -1 if there are no connections like we are looking for | 101 | return -1 if there are no connections like we are looking for |
102 | //return id if it found it | 102 | return id if it found it */ |
103 | int getconnection_id(IP_Port ip_port) | 103 | int getconnection_id(IP_Port ip_port) |
104 | { | 104 | { |
105 | uint32_t i; | 105 | uint32_t i; |
@@ -114,13 +114,13 @@ int getconnection_id(IP_Port ip_port) | |||
114 | return -1; | 114 | return -1; |
115 | } | 115 | } |
116 | 116 | ||
117 | //table of random numbers used below. | 117 | /* table of random numbers used below. */ |
118 | static uint32_t randtable[6][256]; | 118 | static uint32_t randtable[6][256]; |
119 | 119 | ||
120 | 120 | ||
121 | //generate a handshake_id which depends on the ip_port. | 121 | /* generate a handshake_id which depends on the ip_port. |
122 | //this function will always give one unique handshake_id per ip_port. | 122 | this function will always give one unique handshake_id per ip_port. |
123 | //TODO: make this better | 123 | TODO: make this better */ |
124 | uint32_t handshake_id(IP_Port source) | 124 | uint32_t handshake_id(IP_Port source) |
125 | { | 125 | { |
126 | uint32_t id = 0, i; | 126 | uint32_t id = 0, i; |
@@ -132,14 +132,14 @@ uint32_t handshake_id(IP_Port source) | |||
132 | } | 132 | } |
133 | id ^= randtable[i][((uint8_t *)&source)[i]]; | 133 | id ^= randtable[i][((uint8_t *)&source)[i]]; |
134 | } | 134 | } |
135 | if(id == 0)//id can't be zero | 135 | if(id == 0) /* id can't be zero */ |
136 | { | 136 | { |
137 | id = 1; | 137 | id = 1; |
138 | } | 138 | } |
139 | return id; | 139 | return id; |
140 | } | 140 | } |
141 | //change the hnshake id associated with that ip_port | 141 | /* change the hnshake id associated with that ip_port |
142 | //TODO: make this better | 142 | TODO: make this better */ |
143 | void change_handshake(IP_Port source) | 143 | void change_handshake(IP_Port source) |
144 | { | 144 | { |
145 | uint8_t rand = random_int() % 4; | 145 | uint8_t rand = random_int() % 4; |
@@ -147,10 +147,10 @@ void change_handshake(IP_Port source) | |||
147 | } | 147 | } |
148 | 148 | ||
149 | 149 | ||
150 | //initialize a new connection to ip_port | 150 | /* initialize a new connection to ip_port |
151 | //returns an integer corresponding to the connection id. | 151 | returns an integer corresponding to the connection id. |
152 | //return -1 if it could not initialize the connection. | 152 | return -1 if it could not initialize the connection. |
153 | //if there already was an existing connection to that ip_port return its number. | 153 | if there already was an existing connection to that ip_port return its number. */ |
154 | int new_connection(IP_Port ip_port) | 154 | int new_connection(IP_Port ip_port) |
155 | { | 155 | { |
156 | int connect = getconnection_id(ip_port); | 156 | int connect = getconnection_id(ip_port); |
@@ -177,7 +177,7 @@ int new_connection(IP_Port ip_port) | |||
177 | connections[i].last_sent = current_time(); | 177 | connections[i].last_sent = current_time(); |
178 | connections[i].killat = ~0; | 178 | connections[i].killat = ~0; |
179 | connections[i].send_counter = 0; | 179 | connections[i].send_counter = 0; |
180 | //add randomness to timeout to prevent connections getting stuck in a loop. | 180 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ |
181 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; | 181 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; |
182 | return i; | 182 | return i; |
183 | } | 183 | } |
@@ -185,9 +185,9 @@ int new_connection(IP_Port ip_port) | |||
185 | return -1; | 185 | return -1; |
186 | } | 186 | } |
187 | 187 | ||
188 | //initialize a new inbound connection from ip_port | 188 | /* initialize a new inbound connection from ip_port |
189 | //returns an integer corresponding to the connection id. | 189 | returns an integer corresponding to the connection id. |
190 | //return -1 if it could not initialize the connection. | 190 | return -1 if it could not initialize the connection. */ |
191 | int new_inconnection(IP_Port ip_port) | 191 | int new_inconnection(IP_Port ip_port) |
192 | { | 192 | { |
193 | if(getconnection_id(ip_port) != -1) | 193 | if(getconnection_id(ip_port) != -1) |
@@ -207,9 +207,9 @@ int new_inconnection(IP_Port ip_port) | |||
207 | connections[i].data_rate = DATA_SYNC_RATE; | 207 | connections[i].data_rate = DATA_SYNC_RATE; |
208 | connections[i].last_recvSYNC = current_time(); | 208 | connections[i].last_recvSYNC = current_time(); |
209 | connections[i].last_sent = current_time(); | 209 | connections[i].last_sent = current_time(); |
210 | //add randomness to timeout to prevent connections getting stuck in a loop. | 210 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ |
211 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; | 211 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; |
212 | //if this connection isn't handled within the timeout kill it. | 212 | /* if this connection isn't handled within the timeout kill it. */ |
213 | connections[i].killat = current_time() + 1000000UL*connections[i].timeout; | 213 | connections[i].killat = current_time() + 1000000UL*connections[i].timeout; |
214 | connections[i].send_counter = 127; | 214 | connections[i].send_counter = 127; |
215 | return i; | 215 | return i; |
@@ -218,8 +218,8 @@ int new_inconnection(IP_Port ip_port) | |||
218 | return -1; | 218 | return -1; |
219 | } | 219 | } |
220 | 220 | ||
221 | //returns an integer corresponding to the next connection in our incoming connection list | 221 | /* returns an integer corresponding to the next connection in our incoming connection list |
222 | //return -1 if there are no new incoming connections in the list. | 222 | return -1 if there are no new incoming connections in the list. */ |
223 | int incoming_connection() | 223 | int incoming_connection() |
224 | { | 224 | { |
225 | uint32_t i; | 225 | uint32_t i; |
@@ -234,8 +234,8 @@ int incoming_connection() | |||
234 | return -1; | 234 | return -1; |
235 | } | 235 | } |
236 | 236 | ||
237 | //return -1 if it could not kill the connection. | 237 | /* return -1 if it could not kill the connection. |
238 | //return 0 if killed successfully | 238 | return 0 if killed successfully */ |
239 | int kill_connection(int connection_id) | 239 | int kill_connection(int connection_id) |
240 | { | 240 | { |
241 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 241 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -250,9 +250,9 @@ int kill_connection(int connection_id) | |||
250 | return -1; | 250 | return -1; |
251 | } | 251 | } |
252 | 252 | ||
253 | //kill connection in seconds seconds. | 253 | /* kill connection in seconds seconds. |
254 | //return -1 if it can not kill the connection. | 254 | return -1 if it can not kill the connection. |
255 | //return 0 if it will kill it | 255 | return 0 if it will kill it */ |
256 | int kill_connection_in(int connection_id, uint32_t seconds) | 256 | int kill_connection_in(int connection_id, uint32_t seconds) |
257 | { | 257 | { |
258 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 258 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -266,12 +266,12 @@ int kill_connection_in(int connection_id, uint32_t seconds) | |||
266 | return -1; | 266 | return -1; |
267 | } | 267 | } |
268 | 268 | ||
269 | //check if connection is connected | 269 | /* check if connection is connected |
270 | //return 0 no. | 270 | return 0 no. |
271 | //return 1 if attempting handshake | 271 | return 1 if attempting handshake |
272 | //return 2 if handshake is done | 272 | return 2 if handshake is done |
273 | //return 3 if fully connected | 273 | return 3 if fully connected |
274 | //return 4 if timed out and waiting to be killed | 274 | return 4 if timed out and waiting to be killed */ |
275 | int is_connected(int connection_id) | 275 | int is_connected(int connection_id) |
276 | { | 276 | { |
277 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 277 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -281,7 +281,7 @@ int is_connected(int connection_id) | |||
281 | return 0; | 281 | return 0; |
282 | } | 282 | } |
283 | 283 | ||
284 | //returns the ip_port of the corresponding connection. | 284 | /* returns the ip_port of the corresponding connection. */ |
285 | IP_Port connection_ip(int connection_id) | 285 | IP_Port connection_ip(int connection_id) |
286 | { | 286 | { |
287 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 287 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -292,20 +292,20 @@ IP_Port connection_ip(int connection_id) | |||
292 | return zero; | 292 | return zero; |
293 | } | 293 | } |
294 | 294 | ||
295 | //returns the number of packets in the queue waiting to be successfully sent. | 295 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
296 | uint32_t sendqueue(int connection_id) | 296 | uint32_t sendqueue(int connection_id) |
297 | { | 297 | { |
298 | return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; | 298 | return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; |
299 | } | 299 | } |
300 | 300 | ||
301 | //returns the number of packets in the queue waiting to be successfully read with read_packet(...) | 301 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
302 | uint32_t recvqueue(int connection_id) | 302 | uint32_t recvqueue(int connection_id) |
303 | { | 303 | { |
304 | return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; | 304 | return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; |
305 | } | 305 | } |
306 | 306 | ||
307 | //returns the id of the next packet in the queue | 307 | /* returns the id of the next packet in the queue |
308 | //return -1 if no packet in queue | 308 | return -1 if no packet in queue */ |
309 | char id_packet(int connection_id) | 309 | char id_packet(int connection_id) |
310 | { | 310 | { |
311 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) | 311 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) |
@@ -314,8 +314,8 @@ char id_packet(int connection_id) | |||
314 | } | 314 | } |
315 | return -1; | 315 | return -1; |
316 | } | 316 | } |
317 | //return 0 if there is no received data in the buffer. | 317 | /* return 0 if there is no received data in the buffer. |
318 | //return length of received packet if successful | 318 | return length of received packet if successful */ |
319 | int read_packet(int connection_id, uint8_t * data) | 319 | int read_packet(int connection_id, uint8_t * data) |
320 | { | 320 | { |
321 | if(recvqueue(connection_id) != 0) | 321 | if(recvqueue(connection_id) != 0) |
@@ -330,8 +330,8 @@ int read_packet(int connection_id, uint8_t * data) | |||
330 | return 0; | 330 | return 0; |
331 | } | 331 | } |
332 | 332 | ||
333 | //return 0 if data could not be put in packet queue | 333 | /* return 0 if data could not be put in packet queue |
334 | //return 1 if data was put into the queue | 334 | return 1 if data was put into the queue */ |
335 | int write_packet(int connection_id, uint8_t * data, uint32_t length) | 335 | int write_packet(int connection_id, uint8_t * data, uint32_t length) |
336 | { | 336 | { |
337 | if(length > MAX_DATA_SIZE) | 337 | if(length > MAX_DATA_SIZE) |
@@ -356,13 +356,13 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length) | |||
356 | 356 | ||
357 | 357 | ||
358 | 358 | ||
359 | //put the packet numbers the we are missing in requested and return the number | 359 | /* put the packet numbers the we are missing in requested and return the number */ |
360 | uint32_t missing_packets(int connection_id, uint32_t * requested) | 360 | uint32_t missing_packets(int connection_id, uint32_t * requested) |
361 | { | 361 | { |
362 | uint32_t number = 0; | 362 | uint32_t number = 0; |
363 | uint32_t i; | 363 | uint32_t i; |
364 | uint32_t temp; | 364 | uint32_t temp; |
365 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1))//don't request packets if the buffer is full. | 365 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ |
366 | { | 366 | { |
367 | return 0; | 367 | return 0; |
368 | } | 368 | } |
@@ -383,9 +383,9 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) | |||
383 | 383 | ||
384 | } | 384 | } |
385 | 385 | ||
386 | //Packet sending functions | 386 | /* Packet sending functions |
387 | //One per packet type. | 387 | One per packet type. |
388 | //see docs/Lossless_UDP.txt for more information. | 388 | see docs/Lossless_UDP.txt for more information. */ |
389 | 389 | ||
390 | 390 | ||
391 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) | 391 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) |
@@ -444,7 +444,7 @@ int send_data_packet(uint32_t connection_id, uint32_t packet_num) | |||
444 | 1 + 4 + connections[connection_id].sendbuffer[index].size); | 444 | 1 + 4 + connections[connection_id].sendbuffer[index].size); |
445 | } | 445 | } |
446 | 446 | ||
447 | //sends 1 data packet | 447 | /* sends 1 data packet */ |
448 | int send_DATA(uint32_t connection_id) | 448 | int send_DATA(uint32_t connection_id) |
449 | { | 449 | { |
450 | int ret; | 450 | int ret; |
@@ -466,13 +466,13 @@ int send_DATA(uint32_t connection_id) | |||
466 | return 0; | 466 | return 0; |
467 | } | 467 | } |
468 | 468 | ||
469 | //END of packet sending functions | 469 | /* END of packet sending functions */ |
470 | 470 | ||
471 | 471 | ||
472 | 472 | ||
473 | //Packet handling functions | 473 | /* Packet handling functions |
474 | //One to handle each type of packets we receive | 474 | One to handle each type of packets we receive |
475 | //return 0 if handled correctly, 1 if packet is bad. | 475 | return 0 if handled correctly, 1 if packet is bad. */ |
476 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | 476 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) |
477 | { | 477 | { |
478 | if(length != (1 + 4 + 4)) | 478 | if(length != (1 + 4 + 4)) |
@@ -496,11 +496,11 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
496 | { | 496 | { |
497 | return 1; | 497 | return 1; |
498 | } | 498 | } |
499 | if(handshake_id2 == connections[connection].handshake_id1)//if handshake_id2 is what we sent previously as handshake_id1 | 499 | if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */ |
500 | { | 500 | { |
501 | connections[connection].status = 2; | 501 | connections[connection].status = 2; |
502 | //NOTE:is this necessary? | 502 | /* NOTE: is this necessary? |
503 | //connections[connection].handshake_id2 = handshake_id1; | 503 | connections[connection].handshake_id2 = handshake_id1; */ |
504 | connections[connection].orecv_packetnum = handshake_id2; | 504 | connections[connection].orecv_packetnum = handshake_id2; |
505 | connections[connection].osent_packetnum = handshake_id1; | 505 | connections[connection].osent_packetnum = handshake_id1; |
506 | connections[connection].recv_packetnum = handshake_id1; | 506 | connections[connection].recv_packetnum = handshake_id1; |
@@ -510,8 +510,8 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
510 | 510 | ||
511 | } | 511 | } |
512 | 512 | ||
513 | //returns 1 if sync packet is valid | 513 | /* returns 1 if sync packet is valid |
514 | //0 if not. | 514 | 0 if not. */ |
515 | int SYNC_valid(uint32_t length) | 515 | int SYNC_valid(uint32_t length) |
516 | { | 516 | { |
517 | if(length < 4 + 4 + 2) | 517 | if(length < 4 + 4 + 2) |
@@ -526,7 +526,7 @@ int SYNC_valid(uint32_t length) | |||
526 | return 1; | 526 | return 1; |
527 | } | 527 | } |
528 | 528 | ||
529 | //case 1: | 529 | /* case 1: */ |
530 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 530 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
531 | { | 531 | { |
532 | if(handshake_id(source) == recv_packetnum) | 532 | if(handshake_id(source) == recv_packetnum) |
@@ -548,11 +548,11 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu | |||
548 | return -1; | 548 | return -1; |
549 | } | 549 | } |
550 | 550 | ||
551 | //case 2: | 551 | /* case 2: */ |
552 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) | 552 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) |
553 | { | 553 | { |
554 | if(recv_packetnum == connections[connection_id].orecv_packetnum) | 554 | if(recv_packetnum == connections[connection_id].orecv_packetnum) |
555 | //&& sent_packetnum == connections[connection_id].osent_packetnum) | 555 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ |
556 | { | 556 | { |
557 | connections[connection_id].status = 3; | 557 | connections[connection_id].status = 3; |
558 | connections[connection_id].recv_counter = counter; | 558 | connections[connection_id].recv_counter = counter; |
@@ -562,17 +562,17 @@ int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
562 | } | 562 | } |
563 | return 1; | 563 | return 1; |
564 | } | 564 | } |
565 | //case 3: | 565 | /* case 3: */ |
566 | int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets, | 566 | int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets, |
567 | uint16_t number) | 567 | uint16_t number) |
568 | { | 568 | { |
569 | uint8_t comp_counter = (counter - connections[connection_id].recv_counter ); | 569 | uint8_t comp_counter = (counter - connections[connection_id].recv_counter ); |
570 | uint32_t i, temp; | 570 | uint32_t i, temp; |
571 | //uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent); | 571 | /* uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent); |
572 | //uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); | 572 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ |
573 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); | 573 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); |
574 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); | 574 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); |
575 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) //packet valid | 575 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) /* packet valid */ |
576 | { | 576 | { |
577 | connections[connection_id].orecv_packetnum = recv_packetnum; | 577 | connections[connection_id].orecv_packetnum = recv_packetnum; |
578 | connections[connection_id].osent_packetnum = sent_packetnum; | 578 | connections[connection_id].osent_packetnum = sent_packetnum; |
@@ -629,8 +629,8 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
629 | return 0; | 629 | return 0; |
630 | } | 630 | } |
631 | 631 | ||
632 | //add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. | 632 | /* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. |
633 | //return 1 if data was too big, 0 if not. | 633 | return 1 if data was too big, 0 if not. */ |
634 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) | 634 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) |
635 | { | 635 | { |
636 | if(size > MAX_DATA_SIZE) | 636 | if(size > MAX_DATA_SIZE) |
@@ -679,7 +679,7 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | |||
679 | return 1; | 679 | return 1; |
680 | } | 680 | } |
681 | 681 | ||
682 | if(connections[connection].status != 3)//Drop the data packet if connection is not connected. | 682 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ |
683 | { | 683 | { |
684 | return 1; | 684 | return 1; |
685 | } | 685 | } |
@@ -698,7 +698,7 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | |||
698 | 698 | ||
699 | } | 699 | } |
700 | 700 | ||
701 | //END of packet handling functions | 701 | /* END of packet handling functions */ |
702 | 702 | ||
703 | 703 | ||
704 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 704 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
@@ -723,8 +723,8 @@ int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
723 | 723 | ||
724 | } | 724 | } |
725 | 725 | ||
726 | //Send handshake requests | 726 | /* Send handshake requests |
727 | //handshake packets are sent at the same rate as SYNC packets | 727 | handshake packets are sent at the same rate as SYNC packets */ |
728 | void doNew() | 728 | void doNew() |
729 | { | 729 | { |
730 | uint32_t i; | 730 | uint32_t i; |
@@ -740,11 +740,11 @@ void doNew() | |||
740 | } | 740 | } |
741 | 741 | ||
742 | } | 742 | } |
743 | //kill all timed out connections | 743 | /* kill all timed out connections */ |
744 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && | 744 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && |
745 | connections[i].status != 4) | 745 | connections[i].status != 4) |
746 | { | 746 | { |
747 | //kill_connection(i); | 747 | /* kill_connection(i); */ |
748 | connections[i].status = 4; | 748 | connections[i].status = 4; |
749 | } | 749 | } |
750 | if(connections[i].status > 0 && connections[i].killat < temp_time) | 750 | if(connections[i].status > 0 && connections[i].killat < temp_time) |
@@ -792,8 +792,8 @@ void doData() | |||
792 | } | 792 | } |
793 | } | 793 | } |
794 | 794 | ||
795 | //TODO: flow control. | 795 | /* TODO: flow control. |
796 | //automatically adjusts send rates of packets for optimal transmission. | 796 | automatically adjusts send rates of packets for optimal transmission. */ |
797 | 797 | ||
798 | #define MAX_SYNC_RATE 10 | 798 | #define MAX_SYNC_RATE 10 |
799 | 799 | ||
@@ -824,8 +824,8 @@ void adjustRates() | |||
824 | } | 824 | } |
825 | } | 825 | } |
826 | } | 826 | } |
827 | //Call this function a couple times per second | 827 | /* Call this function a couple times per second |
828 | //It's the main loop. | 828 | It's the main loop. */ |
829 | void doLossless_UDP() | 829 | void doLossless_UDP() |
830 | { | 830 | { |
831 | doNew(); | 831 | doNew(); |
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index ad8c545c..8a71bd68 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h | |||
@@ -28,82 +28,82 @@ | |||
28 | #include "network.h" | 28 | #include "network.h" |
29 | 29 | ||
30 | 30 | ||
31 | //maximum length of the data in the data packets | 31 | /* maximum length of the data in the data packets */ |
32 | #define MAX_DATA_SIZE 1024 | 32 | #define MAX_DATA_SIZE 1024 |
33 | 33 | ||
34 | 34 | ||
35 | 35 | ||
36 | //Functions | 36 | /* Functions */ |
37 | 37 | ||
38 | //initialize a new connection to ip_port | 38 | /* initialize a new connection to ip_port |
39 | //returns an integer corresponding to the connection id. | 39 | returns an integer corresponding to the connection id. |
40 | //return -1 if it could not initialize the connection. | 40 | return -1 if it could not initialize the connection. |
41 | //if there already was an existing connection to that ip_port return its number. | 41 | if there already was an existing connection to that ip_port return its number. */ |
42 | int new_connection(IP_Port ip_port); | 42 | int new_connection(IP_Port ip_port); |
43 | 43 | ||
44 | //get connection id from IP_Port | 44 | /* get connection id from IP_Port |
45 | //return -1 if there are no connections like we are looking for | 45 | return -1 if there are no connections like we are looking for |
46 | //return id if it found it | 46 | return id if it found it */ |
47 | int getconnection_id(IP_Port ip_port); | 47 | int getconnection_id(IP_Port ip_port); |
48 | 48 | ||
49 | //returns an integer corresponding to the next connection in our imcoming connection list | 49 | /* returns an integer corresponding to the next connection in our imcoming connection list |
50 | //return -1 if there are no new incoming connections in the list. | 50 | return -1 if there are no new incoming connections in the list. */ |
51 | int incoming_connection(); | 51 | int incoming_connection(); |
52 | 52 | ||
53 | 53 | ||
54 | //return -1 if it could not kill the connection. | 54 | /* return -1 if it could not kill the connection. |
55 | //return 0 if killed successfully | 55 | return 0 if killed successfully */ |
56 | int kill_connection(int connection_id); | 56 | int kill_connection(int connection_id); |
57 | 57 | ||
58 | //kill connection in seconds seconds. | 58 | /* kill connection in seconds seconds. |
59 | //return -1 if it can not kill the connection. | 59 | return -1 if it can not kill the connection. |
60 | //return 0 if it will kill it | 60 | return 0 if it will kill it */ |
61 | int kill_connection_in(int connection_id, uint32_t seconds); | 61 | int kill_connection_in(int connection_id, uint32_t seconds); |
62 | 62 | ||
63 | //returns the ip_port of the corresponding connection. | 63 | /* returns the ip_port of the corresponding connection. |
64 | //return 0 if there is no such connection. | 64 | return 0 if there is no such connection. */ |
65 | IP_Port connection_ip(int connection_id); | 65 | IP_Port connection_ip(int connection_id); |
66 | 66 | ||
67 | //returns the id of the next packet in the queue | 67 | /* returns the id of the next packet in the queue |
68 | //return -1 if no packet in queue | 68 | return -1 if no packet in queue */ |
69 | char id_packet(int connection_id); | 69 | char id_packet(int connection_id); |
70 | 70 | ||
71 | //return 0 if there is no received data in the buffer. | 71 | /* return 0 if there is no received data in the buffer. |
72 | //return length of received packet if successful | 72 | return length of received packet if successful */ |
73 | int read_packet(int connection_id, uint8_t * data); | 73 | int read_packet(int connection_id, uint8_t * data); |
74 | 74 | ||
75 | 75 | ||
76 | //return 0 if data could not be put in packet queue | 76 | /* return 0 if data could not be put in packet queue |
77 | //return 1 if data was put into the queue | 77 | return 1 if data was put into the queue */ |
78 | int write_packet(int connection_id, uint8_t * data, uint32_t length); | 78 | int write_packet(int connection_id, uint8_t * data, uint32_t length); |
79 | 79 | ||
80 | 80 | ||
81 | 81 | ||
82 | //returns the number of packets in the queue waiting to be successfully sent. | 82 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
83 | uint32_t sendqueue(int connection_id); | 83 | uint32_t sendqueue(int connection_id); |
84 | 84 | ||
85 | 85 | ||
86 | //returns the number of packets in the queue waiting to be successfully read with read_packet(...) | 86 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
87 | uint32_t recvqueue(int connection_id); | 87 | uint32_t recvqueue(int connection_id); |
88 | 88 | ||
89 | 89 | ||
90 | //check if connection is connected | 90 | /* check if connection is connected |
91 | //return 0 no. | 91 | return 0 no. |
92 | //return 1 if attempting handshake | 92 | return 1 if attempting handshake |
93 | //return 2 if handshake is done | 93 | return 2 if handshake is done |
94 | //return 3 if fully connected | 94 | return 3 if fully connected |
95 | //return 4 if timed out and wating to be killed | 95 | return 4 if timed out and wating to be killed */ |
96 | int is_connected(int connection_id); | 96 | int is_connected(int connection_id); |
97 | 97 | ||
98 | 98 | ||
99 | //Call this function a couple times per second | 99 | /* Call this function a couple times per second |
100 | //It's the main loop. | 100 | It's the main loop. */ |
101 | void doLossless_UDP(); | 101 | void doLossless_UDP(); |
102 | 102 | ||
103 | 103 | ||
104 | //if we receive a Lossless_UDP packet we call this function so it can be handled. | 104 | /* if we receive a Lossless_UDP packet we call this function so it can be handled. |
105 | //Return 0 if packet is handled correctly. | 105 | return 0 if packet is handled correctly. |
106 | //return 1 if it didn't handle the packet or if the packet was shit. | 106 | return 1 if it didn't handle the packet or if the packet was shit. */ |
107 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 107 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); |
108 | 108 | ||
109 | #endif | 109 | #endif |
diff --git a/core/Messenger.c b/core/Messenger.c index bb581a45..25ce067d 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -29,15 +29,15 @@ typedef struct | |||
29 | { | 29 | { |
30 | uint8_t client_id[CLIENT_ID_SIZE]; | 30 | uint8_t client_id[CLIENT_ID_SIZE]; |
31 | int crypt_connection_id; | 31 | int crypt_connection_id; |
32 | int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend. | 32 | int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ |
33 | uint8_t status;//0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. | 33 | uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ |
34 | uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do | 34 | uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ |
35 | uint8_t name[MAX_NAME_LENGTH]; | 35 | uint8_t name[MAX_NAME_LENGTH]; |
36 | uint8_t name_sent;//0 if we didn't send our name to this friend 1 if we have. | 36 | uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ |
37 | uint8_t *userstatus; | 37 | uint8_t *userstatus; |
38 | uint16_t userstatus_length; | 38 | uint16_t userstatus_length; |
39 | uint8_t userstatus_sent; | 39 | uint8_t userstatus_sent; |
40 | uint16_t info_size; //length of the info | 40 | uint16_t info_size; /* length of the info */ |
41 | }Friend; | 41 | }Friend; |
42 | 42 | ||
43 | 43 | ||
@@ -54,13 +54,13 @@ static Friend friendlist[MAX_NUM_FRIENDS]; | |||
54 | 54 | ||
55 | static uint32_t numfriends; | 55 | static uint32_t numfriends; |
56 | 56 | ||
57 | //1 if we are online | 57 | /* 1 if we are online |
58 | //0 if we are offline | 58 | 0 if we are offline |
59 | //static uint8_t online; | 59 | static uint8_t online; */ |
60 | 60 | ||
61 | 61 | ||
62 | //return the friend id associated to that public key. | 62 | /* return the friend id associated to that public key. |
63 | //return -1 if no such friend | 63 | return -1 if no such friend */ |
64 | int getfriend_id(uint8_t * client_id) | 64 | int getfriend_id(uint8_t * client_id) |
65 | { | 65 | { |
66 | uint32_t i; | 66 | uint32_t i; |
@@ -78,10 +78,10 @@ int getfriend_id(uint8_t * client_id) | |||
78 | } | 78 | } |
79 | 79 | ||
80 | 80 | ||
81 | //copies the public key associated to that friend id into client_id buffer. | 81 | /* copies the public key associated to that friend id into client_id buffer. |
82 | //make sure that client_id is of size CLIENT_ID_SIZE. | 82 | make sure that client_id is of size CLIENT_ID_SIZE. |
83 | //returns 0 if success | 83 | return 0 if success |
84 | //return -1 if failure. | 84 | return -1 if failure. */ |
85 | int getclient_id(int friend_id, uint8_t * client_id) | 85 | int getclient_id(int friend_id, uint8_t * client_id) |
86 | { | 86 | { |
87 | if(friend_id >= numfriends || friend_id < 0) | 87 | if(friend_id >= numfriends || friend_id < 0) |
@@ -98,12 +98,12 @@ int getclient_id(int friend_id, uint8_t * client_id) | |||
98 | } | 98 | } |
99 | 99 | ||
100 | 100 | ||
101 | //add a friend | 101 | /* add a friend |
102 | //set the data that will be sent along with friend request | 102 | set the data that will be sent along with friend request |
103 | //client_id is the client id of the friend | 103 | client_id is the client id of the friend |
104 | //data is the data and length is the length | 104 | data is the data and length is the length |
105 | //returns the friend number if success | 105 | returns the friend number if success |
106 | //return -1 if failure. | 106 | return -1 if failure. */ |
107 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | 107 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) |
108 | { | 108 | { |
109 | if(length == 0 || length >= | 109 | if(length == 0 || length >= |
@@ -166,9 +166,9 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
166 | return -1; | 166 | return -1; |
167 | } | 167 | } |
168 | 168 | ||
169 | //remove a friend | 169 | /* remove a friend |
170 | //returns 0 if success | 170 | return 0 if success |
171 | //return -1 if failure. | 171 | return -1 if failure */ |
172 | int m_delfriend(int friendnumber) | 172 | int m_delfriend(int friendnumber) |
173 | { | 173 | { |
174 | if(friendnumber >= numfriends || friendnumber < 0) | 174 | if(friendnumber >= numfriends || friendnumber < 0) |
@@ -193,11 +193,11 @@ int m_delfriend(int friendnumber) | |||
193 | } | 193 | } |
194 | 194 | ||
195 | 195 | ||
196 | //return 4 if friend is online | 196 | /* return 4 if friend is online |
197 | //return 3 if friend is confirmed | 197 | return 3 if friend is confirmed |
198 | //return 2 if the friend request was sent | 198 | return 2 if the friend request was sent |
199 | //return 1 if the friend was added | 199 | return 1 if the friend was added |
200 | //return 0 if there is no friend with that number. | 200 | return 0 if there is no friend with that number */ |
201 | int m_friendstatus(int friendnumber) | 201 | int m_friendstatus(int friendnumber) |
202 | { | 202 | { |
203 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 203 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
@@ -208,9 +208,9 @@ int m_friendstatus(int friendnumber) | |||
208 | } | 208 | } |
209 | 209 | ||
210 | 210 | ||
211 | //send a text chat message to an online friend. | 211 | /* send a text chat message to an online friend |
212 | //returns 1 if packet was successfully put into the send queue | 212 | return 1 if packet was successfully put into the send queue |
213 | //return 0 if it was not. | 213 | return 0 if it was not */ |
214 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | 214 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) |
215 | { | 215 | { |
216 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 216 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
@@ -218,7 +218,7 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | |||
218 | return 0; | 218 | return 0; |
219 | } | 219 | } |
220 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 220 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
221 | //this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. | 221 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
222 | { | 222 | { |
223 | return 0; | 223 | return 0; |
224 | } | 224 | } |
@@ -228,7 +228,7 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | |||
228 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); | 228 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); |
229 | } | 229 | } |
230 | 230 | ||
231 | //send a name packet to friendnumber | 231 | /* send a name packet to friendnumber */ |
232 | static int m_sendname(int friendnumber, uint8_t * name) | 232 | static int m_sendname(int friendnumber, uint8_t * name) |
233 | { | 233 | { |
234 | uint8_t temp[MAX_NAME_LENGTH + 1]; | 234 | uint8_t temp[MAX_NAME_LENGTH + 1]; |
@@ -237,9 +237,9 @@ static int m_sendname(int friendnumber, uint8_t * name) | |||
237 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1); | 237 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1); |
238 | } | 238 | } |
239 | 239 | ||
240 | //set the name of a friend | 240 | /* set the name of a friend |
241 | //return 0 if success | 241 | return 0 if success |
242 | //return -1 if failure | 242 | return -1 if failure */ |
243 | 243 | ||
244 | static int setfriendname(int friendnumber, uint8_t * name) | 244 | static int setfriendname(int friendnumber, uint8_t * name) |
245 | { | 245 | { |
@@ -252,10 +252,10 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
252 | } | 252 | } |
253 | 253 | ||
254 | 254 | ||
255 | //Set our nickname | 255 | /* Set our nickname |
256 | //name must be a string of maximum MAX_NAME_LENGTH length. | 256 | name must be a string of maximum MAX_NAME_LENGTH length. |
257 | //return 0 if success | 257 | return 0 if success |
258 | //return -1 if failure | 258 | return -1 if failure */ |
259 | int setname(uint8_t * name, uint16_t length) | 259 | int setname(uint8_t * name, uint16_t length) |
260 | { | 260 | { |
261 | if(length > MAX_NAME_LENGTH) | 261 | if(length > MAX_NAME_LENGTH) |
@@ -271,11 +271,11 @@ int setname(uint8_t * name, uint16_t length) | |||
271 | return 0; | 271 | return 0; |
272 | } | 272 | } |
273 | 273 | ||
274 | //get name of friendnumber | 274 | /* get name of friendnumber |
275 | //put it in name | 275 | put it in name |
276 | //name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 276 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
277 | //return 0 if success | 277 | return 0 if success |
278 | //return -1 if failure | 278 | return -1 if failure */ |
279 | int getname(int friendnumber, uint8_t * name) | 279 | int getname(int friendnumber, uint8_t * name) |
280 | { | 280 | { |
281 | if(friendnumber >= numfriends || friendnumber < 0) | 281 | if(friendnumber >= numfriends || friendnumber < 0) |
@@ -306,8 +306,8 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
306 | return 0; | 306 | return 0; |
307 | } | 307 | } |
308 | 308 | ||
309 | // return the size of friendnumber's user status | 309 | /* return the size of friendnumber's user status |
310 | // guaranteed to be at most MAX_USERSTATUS_LENGTH | 310 | guaranteed to be at most MAX_USERSTATUS_LENGTH */ |
311 | int m_get_userstatus_size(int friendnumber) | 311 | int m_get_userstatus_size(int friendnumber) |
312 | { | 312 | { |
313 | if(friendnumber >= numfriends || friendnumber < 0) | 313 | if(friendnumber >= numfriends || friendnumber < 0) |
@@ -317,8 +317,8 @@ int m_get_userstatus_size(int friendnumber) | |||
317 | return friendlist[friendnumber].userstatus_length; | 317 | return friendlist[friendnumber].userstatus_length; |
318 | } | 318 | } |
319 | 319 | ||
320 | // copy the user status of friendnumber into buf, truncating if needed to maxlen | 320 | /* copy the user status of friendnumber into buf, truncating if needed to maxlen |
321 | // bytes, use m_get_userstatus_size to find out how much you need to allocate | 321 | bytes, use m_get_userstatus_size to find out how much you need to allocate */ |
322 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) | 322 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) |
323 | { | 323 | { |
324 | if(friendnumber >= numfriends || friendnumber < 0) | 324 | if(friendnumber >= numfriends || friendnumber < 0) |
@@ -357,7 +357,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le | |||
357 | static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 357 | static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
358 | static uint8_t friend_request_isset = 0; | 358 | static uint8_t friend_request_isset = 0; |
359 | 359 | ||
360 | //set the function that will be executed when a friend request is received. | 360 | /* set the function that will be executed when a friend request is received. */ |
361 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 361 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) |
362 | { | 362 | { |
363 | friend_request = function; | 363 | friend_request = function; |
@@ -368,7 +368,7 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | |||
368 | static void (*friend_message)(int, uint8_t *, uint16_t); | 368 | static void (*friend_message)(int, uint8_t *, uint16_t); |
369 | static uint8_t friend_message_isset = 0; | 369 | static uint8_t friend_message_isset = 0; |
370 | 370 | ||
371 | //set the function that will be executed when a message from a friend is received. | 371 | /* set the function that will be executed when a message from a friend is received. */ |
372 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) | 372 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) |
373 | { | 373 | { |
374 | friend_message = function; | 374 | friend_message = function; |
@@ -393,7 +393,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)) | |||
393 | } | 393 | } |
394 | 394 | ||
395 | #define PORT 33445 | 395 | #define PORT 33445 |
396 | //run this at startup | 396 | /* run this at startup */ |
397 | void initMessenger() | 397 | void initMessenger() |
398 | { | 398 | { |
399 | new_keys(); | 399 | new_keys(); |
@@ -405,7 +405,7 @@ void initMessenger() | |||
405 | } | 405 | } |
406 | 406 | ||
407 | static void doFriends() | 407 | static void doFriends() |
408 | {//TODO: add incoming connections and some other stuff. | 408 | {/* TODO: add incoming connections and some other stuff. */ |
409 | uint32_t i; | 409 | uint32_t i; |
410 | int len; | 410 | int len; |
411 | uint8_t temp[MAX_DATA_SIZE]; | 411 | uint8_t temp[MAX_DATA_SIZE]; |
@@ -415,7 +415,7 @@ static void doFriends() | |||
415 | { | 415 | { |
416 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 416 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
417 | int request = check_friendrequest(friendlist[i].friend_request_id); | 417 | int request = check_friendrequest(friendlist[i].friend_request_id); |
418 | //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); | 418 | /* printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); */ |
419 | if(friendip.ip.i > 1 && request == -1) | 419 | if(friendip.ip.i > 1 && request == -1) |
420 | { | 420 | { |
421 | friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, | 421 | friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, |
@@ -423,9 +423,9 @@ static void doFriends() | |||
423 | friendlist[i].status = 2; | 423 | friendlist[i].status = 2; |
424 | } | 424 | } |
425 | } | 425 | } |
426 | if(friendlist[i].status == 2 || friendlist[i].status == 3) //friend is not online | 426 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ |
427 | { | 427 | { |
428 | check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request | 428 | check_friendrequest(friendlist[i].friend_request_id); /* for now this is used to kill the friend request */ |
429 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 429 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
430 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) | 430 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) |
431 | { | 431 | { |
@@ -433,7 +433,7 @@ static void doFriends() | |||
433 | if (friendip.ip.i > 1) | 433 | if (friendip.ip.i > 1) |
434 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 434 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
435 | break; | 435 | break; |
436 | case 3: // Connection is established | 436 | case 3: /* Connection is established */ |
437 | friendlist[i].status = 4; | 437 | friendlist[i].status = 4; |
438 | break; | 438 | break; |
439 | case 4: | 439 | case 4: |
@@ -444,7 +444,7 @@ static void doFriends() | |||
444 | break; | 444 | break; |
445 | } | 445 | } |
446 | } | 446 | } |
447 | while(friendlist[i].status == 4) //friend is online | 447 | while(friendlist[i].status == 4) /* friend is online */ |
448 | { | 448 | { |
449 | if(friendlist[i].name_sent == 0) | 449 | if(friendlist[i].name_sent == 0) |
450 | { | 450 | { |
@@ -468,10 +468,10 @@ static void doFriends() | |||
468 | if (len != MAX_NAME_LENGTH + 1) break; | 468 | if (len != MAX_NAME_LENGTH + 1) break; |
469 | if(friend_namechange_isset) | 469 | if(friend_namechange_isset) |
470 | { | 470 | { |
471 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); // todo: use the actual length | 471 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ |
472 | } | 472 | } |
473 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); | 473 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); |
474 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present. | 474 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ |
475 | break; | 475 | break; |
476 | } | 476 | } |
477 | case PACKET_ID_USERSTATUS: { | 477 | case PACKET_ID_USERSTATUS: { |
@@ -496,7 +496,7 @@ static void doFriends() | |||
496 | } | 496 | } |
497 | else | 497 | else |
498 | { | 498 | { |
499 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it | 499 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */ |
500 | { | 500 | { |
501 | crypto_kill(friendlist[i].crypt_connection_id); | 501 | crypto_kill(friendlist[i].crypt_connection_id); |
502 | friendlist[i].crypt_connection_id = -1; | 502 | friendlist[i].crypt_connection_id = -1; |
@@ -545,7 +545,7 @@ static void doInbound() | |||
545 | } | 545 | } |
546 | } | 546 | } |
547 | 547 | ||
548 | //the main loop that needs to be run at least 200 times per second. | 548 | /* the main loop that needs to be run at least 200 times per second. */ |
549 | void doMessenger() | 549 | void doMessenger() |
550 | { | 550 | { |
551 | IP_Port ip_port; | 551 | IP_Port ip_port; |
@@ -554,18 +554,18 @@ void doMessenger() | |||
554 | while(receivepacket(&ip_port, data, &length) != -1) | 554 | while(receivepacket(&ip_port, data, &length) != -1) |
555 | { | 555 | { |
556 | #ifdef DEBUG | 556 | #ifdef DEBUG |
557 | //if(rand() % 3 != 1)//simulate packet loss | 557 | /* if(rand() % 3 != 1) //simulate packet loss */ |
558 | //{ | 558 | /* { */ |
559 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) | 559 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) |
560 | { | 560 | { |
561 | //if packet is discarded | 561 | /* if packet is discarded */ |
562 | printf("Received unhandled packet with length: %u\n", length); | 562 | printf("Received unhandled packet with length: %u\n", length); |
563 | } | 563 | } |
564 | else | 564 | else |
565 | { | 565 | { |
566 | printf("Received handled packet with length: %u\n", length); | 566 | printf("Received handled packet with length: %u\n", length); |
567 | } | 567 | } |
568 | //} | 568 | /* } */ |
569 | printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); | 569 | printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); |
570 | #else | 570 | #else |
571 | DHT_handlepacket(data, length, ip_port); | 571 | DHT_handlepacket(data, length, ip_port); |
@@ -581,14 +581,14 @@ void doMessenger() | |||
581 | doFriends(); | 581 | doFriends(); |
582 | } | 582 | } |
583 | 583 | ||
584 | //returns the size of the messenger data (for saving) | 584 | /* returns the size of the messenger data (for saving) */ |
585 | uint32_t Messenger_size() | 585 | uint32_t Messenger_size() |
586 | { | 586 | { |
587 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 587 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
588 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; | 588 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; |
589 | } | 589 | } |
590 | 590 | ||
591 | //save the messenger in data of size Messenger_size() | 591 | /* save the messenger in data of size Messenger_size() */ |
592 | void Messenger_save(uint8_t * data) | 592 | void Messenger_save(uint8_t * data) |
593 | { | 593 | { |
594 | save_keys(data); | 594 | save_keys(data); |
@@ -604,7 +604,7 @@ void Messenger_save(uint8_t * data) | |||
604 | memcpy(data, friendlist, sizeof(Friend) * numfriends); | 604 | memcpy(data, friendlist, sizeof(Friend) * numfriends); |
605 | } | 605 | } |
606 | 606 | ||
607 | //load the messenger from data of size length. | 607 | /* load the messenger from data of size length. */ |
608 | int Messenger_load(uint8_t * data, uint32_t length) | 608 | int Messenger_load(uint8_t * data, uint32_t length) |
609 | { | 609 | { |
610 | if(length == ~0) | 610 | if(length == ~0) |
@@ -651,7 +651,7 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
651 | { | 651 | { |
652 | int fnum = m_addfriend_norequest(temp[i].client_id); | 652 | int fnum = m_addfriend_norequest(temp[i].client_id); |
653 | setfriendname(fnum, temp[i].name); | 653 | setfriendname(fnum, temp[i].name); |
654 | //set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); | 654 | /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ |
655 | } | 655 | } |
656 | } | 656 | } |
657 | free(temp); | 657 | free(temp); |
diff --git a/core/Messenger.h b/core/Messenger.h index 3ba74d39..6fd98972 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -37,113 +37,113 @@ | |||
37 | #define PACKET_ID_USERSTATUS 49 | 37 | #define PACKET_ID_USERSTATUS 49 |
38 | #define PACKET_ID_MESSAGE 64 | 38 | #define PACKET_ID_MESSAGE 64 |
39 | 39 | ||
40 | // don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased | 40 | /* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased |
41 | // to an absurdly large number later | 41 | to an absurdly large number later */ |
42 | 42 | ||
43 | //add a friend | 43 | /* add a friend |
44 | //set the data that will be sent along with friend request | 44 | set the data that will be sent along with friend request |
45 | //client_id is the client id of the friend | 45 | client_id is the client id of the friend |
46 | //data is the data and length is the length | 46 | data is the data and length is the length |
47 | //returns the friend number if success | 47 | returns the friend number if success |
48 | //return -1 if failure. | 48 | return -1 if failure. */ |
49 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); | 49 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); |
50 | 50 | ||
51 | 51 | ||
52 | //add a friend without sending a friendrequest. | 52 | /* add a friend without sending a friendrequest. |
53 | //returns the friend number if success | 53 | returns the friend number if success |
54 | //return -1 if failure. | 54 | return -1 if failure. */ |
55 | int m_addfriend_norequest(uint8_t * client_id); | 55 | int m_addfriend_norequest(uint8_t * client_id); |
56 | 56 | ||
57 | //return the friend id associated to that client id. | 57 | /* return the friend id associated to that client id. |
58 | //return -1 if no such friend | 58 | return -1 if no such friend */ |
59 | int getfriend_id(uint8_t * client_id); | 59 | int getfriend_id(uint8_t * client_id); |
60 | 60 | ||
61 | //copies the public key associated to that friend id into client_id buffer. | 61 | /* copies the public key associated to that friend id into client_id buffer. |
62 | //make sure that client_id is of size CLIENT_ID_SIZE. | 62 | make sure that client_id is of size CLIENT_ID_SIZE. |
63 | //returns 0 if success | 63 | return 0 if success |
64 | //return -1 if failure. | 64 | return -1 if failure */ |
65 | int getclient_id(int friend_id, uint8_t * client_id); | 65 | int getclient_id(int friend_id, uint8_t * client_id); |
66 | 66 | ||
67 | //remove a friend | 67 | /* remove a friend */ |
68 | int m_delfriend(int friendnumber); | 68 | int m_delfriend(int friendnumber); |
69 | 69 | ||
70 | //return 4 if friend is online | 70 | /* return 4 if friend is online |
71 | //return 3 if friend is confirmed | 71 | return 3 if friend is confirmed |
72 | //return 2 if the friend request was sent | 72 | return 2 if the friend request was sent |
73 | //return 1 if the friend was added | 73 | return 1 if the friend was added |
74 | //return 0 if there is no friend with that number. | 74 | return 0 if there is no friend with that number */ |
75 | int m_friendstatus(int friendnumber); | 75 | int m_friendstatus(int friendnumber); |
76 | 76 | ||
77 | 77 | ||
78 | //send a text chat message to an online friend. | 78 | /* send a text chat message to an online friend |
79 | //returns 1 if packet was successfully put into the send queue | 79 | returns 1 if packet was successfully put into the send queue |
80 | //return 0 if it was not. | 80 | return 0 if it was not */ |
81 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); | 81 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); |
82 | 82 | ||
83 | //Set our nickname | 83 | /* Set our nickname |
84 | //name must be a string of maximum MAX_NAME_LENGTH length. | 84 | name must be a string of maximum MAX_NAME_LENGTH length. |
85 | //return 0 if success | 85 | return 0 if success |
86 | //return -1 if failure | 86 | return -1 if failure */ |
87 | int setname(uint8_t * name, uint16_t length); | 87 | int setname(uint8_t * name, uint16_t length); |
88 | 88 | ||
89 | 89 | ||
90 | //get name of friendnumber | 90 | /* get name of friendnumber |
91 | //put it in name | 91 | put it in name |
92 | //name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 92 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
93 | //return 0 if success | 93 | return 0 if success |
94 | //return -1 if failure | 94 | return -1 if failure */ |
95 | int getname(int friendnumber, uint8_t * name); | 95 | int getname(int friendnumber, uint8_t * name); |
96 | 96 | ||
97 | // set our user status | 97 | /* set our user status |
98 | // you are responsible for freeing status after | 98 | you are responsible for freeing status after |
99 | // returns 0 on success, -1 on failure | 99 | returns 0 on success, -1 on failure */ |
100 | int m_set_userstatus(uint8_t *status, uint16_t length); | 100 | int m_set_userstatus(uint8_t *status, uint16_t length); |
101 | 101 | ||
102 | // return the length of friendnumber's user status, | 102 | /* return the length of friendnumber's user status, |
103 | // including null | 103 | including null |
104 | // pass it into malloc | 104 | pass it into malloc */ |
105 | int m_get_userstatus_size(int friendnumber); | 105 | int m_get_userstatus_size(int friendnumber); |
106 | 106 | ||
107 | // copy friendnumber's userstatus into buf, truncating if size is over maxlen | 107 | /* copy friendnumber's userstatus into buf, truncating if size is over maxlen |
108 | // get the size you need to allocate from m_get_userstatus_size | 108 | get the size you need to allocate from m_get_userstatus_size */ |
109 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); | 109 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); |
110 | 110 | ||
111 | //set the function that will be executed when a friend request is received. | 111 | /* set the function that will be executed when a friend request is received. |
112 | //function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) | 112 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
113 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 113 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); |
114 | 114 | ||
115 | 115 | ||
116 | //set the function that will be executed when a message from a friend is received. | 116 | /* set the function that will be executed when a message from a friend is received. |
117 | //function format is: function(int friendnumber, uint8_t * message, uint32_t length) | 117 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ |
118 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); | 118 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); |
119 | 119 | ||
120 | // set the callback for name changes | 120 | /* set the callback for name changes |
121 | // function(int friendnumber, uint8_t *newname, uint16_t length) | 121 | function(int friendnumber, uint8_t *newname, uint16_t length) |
122 | // you are not responsible for freeing newname | 122 | you are not responsible for freeing newname */ |
123 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); | 123 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); |
124 | 124 | ||
125 | // set the callback for user status changes | 125 | /* set the callback for user status changes |
126 | // function(int friendnumber, uint8_t *newstatus, uint16_t length) | 126 | function(int friendnumber, uint8_t *newstatus, uint16_t length) |
127 | // you are not responsible for freeing newstatus | 127 | you are not responsible for freeing newstatus */ |
128 | void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); | 128 | void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); |
129 | 129 | ||
130 | //run this at startup | 130 | /* run this at startup */ |
131 | void initMessenger(); | 131 | void initMessenger(); |
132 | 132 | ||
133 | 133 | ||
134 | //the main loop that needs to be run at least 200 times per second. | 134 | /* the main loop that needs to be run at least 200 times per second */ |
135 | void doMessenger(); | 135 | void doMessenger(); |
136 | 136 | ||
137 | 137 | ||
138 | //SAVING AND LOADING FUNCTIONS: | 138 | /* SAVING AND LOADING FUNCTIONS: */ |
139 | 139 | ||
140 | //returns the size of the messenger data (for saving) | 140 | /* returns the size of the messenger data (for saving) */ |
141 | uint32_t Messenger_size(); | 141 | uint32_t Messenger_size(); |
142 | 142 | ||
143 | //save the messenger in data (must be allocated memory of size Messenger_size()) | 143 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ |
144 | void Messenger_save(uint8_t * data); | 144 | void Messenger_save(uint8_t * data); |
145 | 145 | ||
146 | //load the messenger from data of size length. | 146 | /* load the messenger from data of size length */ |
147 | int Messenger_load(uint8_t * data, uint32_t length); | 147 | int Messenger_load(uint8_t * data, uint32_t length); |
148 | 148 | ||
149 | #endif | 149 | #endif |