summaryrefslogtreecommitdiff
path: root/core/Lossless_UDP.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-27 08:26:00 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-27 08:26:00 -0400
commit1a6446266c9727e5c95e18d5e44157fd5a60900f (patch)
tree6dafbf52b7952f68a95ac9317871e923db6cc565 /core/Lossless_UDP.c
parent45b5ef0b79239167d9bc8fe52b25b36b684d2687 (diff)
parent241aca98bdc8106221ee7d7dbcea9f2fa17f24bc (diff)
Merge branch 'master' of https://github.com/KostyaKow/ProjectTox-Core into KostyaKow-master
Conflicts: core/DHT.c
Diffstat (limited to 'core/Lossless_UDP.c')
-rw-r--r--core/Lossless_UDP.c174
1 files changed, 88 insertions, 86 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 43f61b5b..f9d20b2f 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -44,14 +44,12 @@ timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION
44/* initial send rate of data. */ 44/* initial send rate of data. */
45#define DATA_SYNC_RATE 30 45#define DATA_SYNC_RATE 30
46 46
47typedef struct 47typedef struct {
48{
49 uint8_t data[MAX_DATA_SIZE]; 48 uint8_t data[MAX_DATA_SIZE];
50 uint16_t size; 49 uint16_t size;
51}Data; 50}Data;
52 51
53typedef struct 52typedef struct {
54{
55 IP_Port ip_port; 53 IP_Port ip_port;
56 uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, 54 uint8_t status; /* 0 if connection is dead, 1 if attempting handshake,
57 2 if handshake is done (we start sending SYNC packets) 55 2 if handshake is done (we start sending SYNC packets)
@@ -97,13 +95,13 @@ static Connection connections[MAX_CONNECTIONS];
97/* get connection id from IP_Port 95/* get connection id from IP_Port
98 return -1 if there are no connections like we are looking for 96 return -1 if there are no connections like we are looking for
99 return id if it found it */ 97 return id if it found it */
100int getconnection_id(IP_Port ip_port) 98int getconnection_id(IP_Port ip_port) {
101{
102 uint32_t i; 99 uint32_t i;
103 for(i = 0; i < MAX_CONNECTIONS; ++i) 100 for (i = 0; i < MAX_CONNECTIONS; ++i) {
104 if(connections[i].ip_port.ip.i == ip_port.ip.i && 101 if (connections[i].ip_port.ip.i == ip_port.ip.i &&
105 connections[i].ip_port.port == ip_port.port && connections[i].status > 0) 102 connections[i].ip_port.port == ip_port.port && connections[i].status > 0)
106 return i; 103 return i;
104 }
107 return -1; 105 return -1;
108} 106}
109 107
@@ -116,12 +114,12 @@ static uint32_t randtable[6][256];
116uint32_t handshake_id(IP_Port source) 114uint32_t handshake_id(IP_Port source)
117{ 115{
118 uint32_t id = 0, i; 116 uint32_t id = 0, i;
119 for(i = 0; i < 6; ++i) { 117 for (i = 0; i < 6; ++i) {
120 if(randtable[i][((uint8_t *)&source)[i]] == 0) 118 if(randtable[i][((uint8_t *)&source)[i]] == 0)
121 randtable[i][((uint8_t *)&source)[i]] = random_int(); 119 randtable[i][((uint8_t *)&source)[i]] = random_int();
122 id ^= randtable[i][((uint8_t *)&source)[i]]; 120 id ^= randtable[i][((uint8_t *)&source)[i]];
123 } 121 }
124 if(id == 0) /* id can't be zero */ 122 if (id == 0) /* id can't be zero */
125 id = 1; 123 id = 1;
126 return id; 124 return id;
127} 125}
@@ -141,10 +139,10 @@ void change_handshake(IP_Port source)
141int new_connection(IP_Port ip_port) 139int new_connection(IP_Port ip_port)
142{ 140{
143 int connect = getconnection_id(ip_port); 141 int connect = getconnection_id(ip_port);
144 if(connect != -1) 142 if (connect != -1)
145 return connect; 143 return connect;
146 uint32_t i; 144 uint32_t i;
147 for(i = 0; i < MAX_CONNECTIONS; ++i) { 145 for (i = 0; i < MAX_CONNECTIONS; ++i) {
148 if(connections[i].status == 0) { 146 if(connections[i].status == 0) {
149 memset(&connections[i], 0, sizeof(Connection)); 147 memset(&connections[i], 0, sizeof(Connection));
150 connections[i].ip_port = ip_port; 148 connections[i].ip_port = ip_port;
@@ -173,11 +171,11 @@ int new_connection(IP_Port ip_port)
173 return -1 if it could not initialize the connection. */ 171 return -1 if it could not initialize the connection. */
174int new_inconnection(IP_Port ip_port) 172int new_inconnection(IP_Port ip_port)
175{ 173{
176 if(getconnection_id(ip_port) != -1) 174 if (getconnection_id(ip_port) != -1)
177 return -1; 175 return -1;
178 uint32_t i; 176 uint32_t i;
179 for(i = 0; i < MAX_CONNECTIONS; ++i) { 177 for (i = 0; i < MAX_CONNECTIONS; ++i) {
180 if(connections[i].status == 0) { 178 if (connections[i].status == 0) {
181 memset(&connections[i], 0, sizeof(Connection)); 179 memset(&connections[i], 0, sizeof(Connection));
182 connections[i].ip_port = ip_port; 180 connections[i].ip_port = ip_port;
183 connections[i].status = 2; 181 connections[i].status = 2;
@@ -202,11 +200,12 @@ int new_inconnection(IP_Port ip_port)
202int incoming_connection() 200int incoming_connection()
203{ 201{
204 uint32_t i; 202 uint32_t i;
205 for(i = 0; i < MAX_CONNECTIONS; ++i) 203 for (i = 0; i < MAX_CONNECTIONS; ++i) {
206 if(connections[i].inbound == 2) { 204 if (connections[i].inbound == 2) {
207 connections[i].inbound = 1; 205 connections[i].inbound = 1;
208 return i; 206 return i;
209 } 207 }
208 }
210 return -1; 209 return -1;
211} 210}
212 211
@@ -214,12 +213,13 @@ int incoming_connection()
214 return 0 if killed successfully */ 213 return 0 if killed successfully */
215int kill_connection(int connection_id) 214int kill_connection(int connection_id)
216{ 215{
217 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 216 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
218 if(connections[connection_id].status > 0) { 217 if (connections[connection_id].status > 0) {
219 connections[connection_id].status = 0; 218 connections[connection_id].status = 0;
220 change_handshake(connections[connection_id].ip_port); 219 change_handshake(connections[connection_id].ip_port);
221 return 0; 220 return 0;
222 } 221 }
222 }
223 return -1; 223 return -1;
224} 224}
225 225
@@ -228,11 +228,12 @@ int kill_connection(int connection_id)
228 return 0 if it will kill it */ 228 return 0 if it will kill it */
229int kill_connection_in(int connection_id, uint32_t seconds) 229int kill_connection_in(int connection_id, uint32_t seconds)
230{ 230{
231 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 231 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
232 if(connections[connection_id].status > 0) { 232 if (connections[connection_id].status > 0) {
233 connections[connection_id].killat = current_time() + 1000000UL*seconds; 233 connections[connection_id].killat = current_time() + 1000000UL*seconds;
234 return 0; 234 return 0;
235 } 235 }
236 }
236 return -1; 237 return -1;
237} 238}
238 239
@@ -244,7 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds)
244 return 4 if timed out and waiting to be killed */ 245 return 4 if timed out and waiting to be killed */
245int is_connected(int connection_id) 246int is_connected(int connection_id)
246{ 247{
247 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 248 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
248 return connections[connection_id].status; 249 return connections[connection_id].status;
249 return 0; 250 return 0;
250} 251}
@@ -252,7 +253,7 @@ int is_connected(int connection_id)
252/* returns the ip_port of the corresponding connection. */ 253/* returns the ip_port of the corresponding connection. */
253IP_Port connection_ip(int connection_id) 254IP_Port connection_ip(int connection_id)
254{ 255{
255 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 256 if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
256 return connections[connection_id].ip_port; 257 return connections[connection_id].ip_port;
257 IP_Port zero = {{{0}}, 0}; 258 IP_Port zero = {{{0}}, 0};
258 return zero; 259 return zero;
@@ -274,7 +275,7 @@ uint32_t recvqueue(int connection_id)
274 return -1 if no packet in queue */ 275 return -1 if no packet in queue */
275char id_packet(int connection_id) 276char id_packet(int connection_id)
276{ 277{
277 if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) 278 if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
278 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; 279 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
279 return -1; 280 return -1;
280} 281}
@@ -283,7 +284,7 @@ char id_packet(int connection_id)
283 return length of received packet if successful */ 284 return length of received packet if successful */
284int read_packet(int connection_id, uint8_t * data) 285int read_packet(int connection_id, uint8_t * data)
285{ 286{
286 if(recvqueue(connection_id) != 0) { 287 if (recvqueue(connection_id) != 0) {
287 uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; 288 uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM;
288 uint16_t size = connections[connection_id].recvbuffer[index].size; 289 uint16_t size = connections[connection_id].recvbuffer[index].size;
289 memcpy(data, connections[connection_id].recvbuffer[index].data, size); 290 memcpy(data, connections[connection_id].recvbuffer[index].data, size);
@@ -298,11 +299,11 @@ int read_packet(int connection_id, uint8_t * data)
298 return 1 if data was put into the queue */ 299 return 1 if data was put into the queue */
299int write_packet(int connection_id, uint8_t * data, uint32_t length) 300int write_packet(int connection_id, uint8_t * data, uint32_t length)
300{ 301{
301 if(length > MAX_DATA_SIZE) 302 if (length > MAX_DATA_SIZE)
302 return 0; 303 return 0;
303 if(length == 0) 304 if (length == 0)
304 return 0; 305 return 0;
305 if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { 306 if (sendqueue(connection_id) < BUFFER_PACKET_NUM) {
306 uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; 307 uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
307 memcpy(connections[connection_id].sendbuffer[index].data, data, length); 308 memcpy(connections[connection_id].sendbuffer[index].data, data, length);
308 connections[connection_id].sendbuffer[index].size = length; 309 connections[connection_id].sendbuffer[index].size = length;
@@ -318,14 +319,15 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
318 uint32_t number = 0; 319 uint32_t number = 0;
319 uint32_t i; 320 uint32_t i;
320 uint32_t temp; 321 uint32_t temp;
321 if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ 322 if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
322 return 0; 323 return 0;
323 for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) 324 for (i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++) {
324 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { 325 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
325 temp = htonl(i); 326 temp = htonl(i);
326 memcpy(requested + number, &temp, 4); 327 memcpy(requested + number, &temp, 4);
327 ++number; 328 ++number;
328 } 329 }
330 }
329 if(number == 0) 331 if(number == 0)
330 connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; 332 connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum;
331 return number; 333 return number;
@@ -393,14 +395,14 @@ int send_DATA(uint32_t connection_id)
393{ 395{
394 int ret; 396 int ret;
395 uint32_t buffer[BUFFER_PACKET_NUM]; 397 uint32_t buffer[BUFFER_PACKET_NUM];
396 if(connections[connection_id].num_req_paquets > 0) { 398 if (connections[connection_id].num_req_paquets > 0) {
397 ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); 399 ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
398 connections[connection_id].num_req_paquets--; 400 connections[connection_id].num_req_paquets--;
399 memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); 401 memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4);
400 memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); 402 memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
401 return ret; 403 return ret;
402 } 404 }
403 if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { 405 if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
404 ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); 406 ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
405 connections[connection_id].sent_packetnum++; 407 connections[connection_id].sent_packetnum++;
406 return ret; 408 return ret;
@@ -415,7 +417,7 @@ int send_DATA(uint32_t connection_id)
415 return 0 if handled correctly, 1 if packet is bad. */ 417 return 0 if handled correctly, 1 if packet is bad. */
416int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) 418int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
417{ 419{
418 if(length != (1 + 4 + 4)) 420 if (length != (1 + 4 + 4))
419 return 1; 421 return 1;
420 uint32_t temp; 422 uint32_t temp;
421 uint32_t handshake_id1, handshake_id2; 423 uint32_t handshake_id1, handshake_id2;
@@ -425,13 +427,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
425 memcpy(&temp, packet + 5, 4); 427 memcpy(&temp, packet + 5, 4);
426 handshake_id2 = ntohl(temp); 428 handshake_id2 = ntohl(temp);
427 429
428 if(handshake_id2 == 0) { 430 if (handshake_id2 == 0) {
429 send_handshake(source, handshake_id(source), handshake_id1); 431 send_handshake(source, handshake_id(source), handshake_id1);
430 return 0; 432 return 0;
431 } 433 }
432 if(is_connected(connection) != 1) 434 if (is_connected(connection) != 1)
433 return 1; 435 return 1;
434 if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ 436 if (handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
435 connections[connection].status = 2; 437 connections[connection].status = 2;
436 /* NOTE: is this necessary? 438 /* NOTE: is this necessary?
437 connections[connection].handshake_id2 = handshake_id1; */ 439 connections[connection].handshake_id2 = handshake_id1; */
@@ -448,10 +450,10 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
448 0 if not. */ 450 0 if not. */
449int SYNC_valid(uint32_t length) 451int SYNC_valid(uint32_t length)
450{ 452{
451 if(length < 4 + 4 + 2) 453 if (length < 4 + 4 + 2)
452 return 0; 454 return 0;
453 if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || 455 if (length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
454 ((length - 4 - 4 - 2) % 4) != 0) 456 ((length - 4 - 4 - 2) % 4) != 0)
455 return 0; 457 return 0;
456 return 1; 458 return 1;
457} 459}
@@ -459,9 +461,9 @@ int SYNC_valid(uint32_t length)
459/* case 1: */ 461/* case 1: */
460int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 462int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
461{ 463{
462 if(handshake_id(source) == recv_packetnum) { 464 if (handshake_id(source) == recv_packetnum) {
463 int x = new_inconnection(source); 465 int x = new_inconnection(source);
464 if(x != -1) { 466 if (x != -1) {
465 connections[x].orecv_packetnum = recv_packetnum; 467 connections[x].orecv_packetnum = recv_packetnum;
466 connections[x].sent_packetnum = recv_packetnum; 468 connections[x].sent_packetnum = recv_packetnum;
467 connections[x].sendbuff_packetnum = recv_packetnum; 469 connections[x].sendbuff_packetnum = recv_packetnum;
@@ -479,7 +481,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
479/* case 2: */ 481/* case 2: */
480int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) 482int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
481{ 483{
482 if(recv_packetnum == connections[connection_id].orecv_packetnum) { 484 if (recv_packetnum == connections[connection_id].orecv_packetnum) {
483 /* && sent_packetnum == connections[connection_id].osent_packetnum) */ 485 /* && sent_packetnum == connections[connection_id].osent_packetnum) */
484 connections[connection_id].status = 3; 486 connections[connection_id].status = 3;
485 connections[connection_id].recv_counter = counter; 487 connections[connection_id].recv_counter = counter;
@@ -499,14 +501,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
499 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ 501 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
500 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); 502 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
501 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); 503 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
502 if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ 504 if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
503 connections[connection_id].orecv_packetnum = recv_packetnum; 505 connections[connection_id].orecv_packetnum = recv_packetnum;
504 connections[connection_id].osent_packetnum = sent_packetnum; 506 connections[connection_id].osent_packetnum = sent_packetnum;
505 connections[connection_id].successful_sent = recv_packetnum; 507 connections[connection_id].successful_sent = recv_packetnum;
506 connections[connection_id].last_recvSYNC = current_time(); 508 connections[connection_id].last_recvSYNC = current_time();
507 connections[connection_id].recv_counter = counter; 509 connections[connection_id].recv_counter = counter;
508 ++connections[connection_id].send_counter; 510 ++connections[connection_id].send_counter;
509 for(i = 0; i < number; ++i) { 511 for (i = 0; i < number; ++i) {
510 temp = ntohl(req_packets[i]); 512 temp = ntohl(req_packets[i]);
511 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); 513 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
512 } 514 }
@@ -516,10 +518,10 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
516 return 1; 518 return 1;
517} 519}
518 520
519int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) 521int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source)
520{ 522{
521 523
522 if(!SYNC_valid(length)) 524 if (!SYNC_valid(length))
523 return 1; 525 return 1;
524 int connection = getconnection_id(source); 526 int connection = getconnection_id(source);
525 uint8_t counter; 527 uint8_t counter;
@@ -533,39 +535,39 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
533 recv_packetnum = ntohl(temp); 535 recv_packetnum = ntohl(temp);
534 memcpy(&temp,packet + 6, 4); 536 memcpy(&temp,packet + 6, 4);
535 sent_packetnum = ntohl(temp); 537 sent_packetnum = ntohl(temp);
536 if(number != 0) 538 if (number != 0)
537 memcpy(req_packets, packet + 10, 4 * number); 539 memcpy(req_packets, packet + 10, 4 * number);
538 if(connection == -1) 540 if (connection == -1)
539 return handle_SYNC1(source, recv_packetnum, sent_packetnum); 541 return handle_SYNC1(source, recv_packetnum, sent_packetnum);
540 if(connections[connection].status == 2) 542 if (connections[connection].status == 2)
541 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); 543 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum);
542 if(connections[connection].status == 3) 544 if (connections[connection].status == 3)
543 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); 545 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number);
544 return 0; 546 return 0;
545} 547}
546 548
547/* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. 549/* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value.
548 return 1 if data was too big, 0 if not. */ 550 return 1 if data was too big, 0 if not. */
549int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) 551int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
550{ 552{
551 if(size > MAX_DATA_SIZE) 553 if (size > MAX_DATA_SIZE)
552 return 1; 554 return 1;
553 555
554 uint32_t i; 556 uint32_t i;
555 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; 557 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
556 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; 558 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
557 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { 559 for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
558 if(i == data_num) { 560 if (i == data_num) {
559 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); 561 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
560 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; 562 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
561 connections[connection_id].last_recvdata = current_time(); 563 connections[connection_id].last_recvdata = current_time();
562 if(sent_packet < BUFFER_PACKET_NUM) 564 if (sent_packet < BUFFER_PACKET_NUM)
563 connections[connection_id].osent_packetnum = data_num; 565 connections[connection_id].osent_packetnum = data_num;
564 break; 566 break;
565 } 567 }
566 } 568 }
567 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { 569 for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
568 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) 570 if (connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
569 connections[connection_id].recv_packetnum = i; 571 connections[connection_id].recv_packetnum = i;
570 else 572 else
571 break; 573 break;
@@ -574,17 +576,17 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size
574 return 0; 576 return 0;
575} 577}
576 578
577int handle_data(uint8_t * packet, uint32_t length, IP_Port source) 579int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
578{ 580{
579 int connection = getconnection_id(source); 581 int connection = getconnection_id(source);
580 582
581 if(connection == -1) 583 if (connection == -1)
582 return 1; 584 return 1;
583 585
584 if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ 586 if (connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
585 return 1; 587 return 1;
586 588
587 if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) 589 if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
588 return 1; 590 return 1;
589 uint32_t temp; 591 uint32_t temp;
590 uint32_t number; 592 uint32_t number;
@@ -597,9 +599,9 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
597 599
598/* END of packet handling functions */ 600/* END of packet handling functions */
599 601
600int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 602int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
601{ 603{
602 switch (packet[0]) { 604 switch (packet[0]) { //TODO: check if no break statement is correct???
603 case 16: 605 case 16:
604 return handle_handshake(packet, length, source); 606 return handle_handshake(packet, length, source);
605 607
@@ -622,19 +624,19 @@ void doNew()
622{ 624{
623 uint32_t i; 625 uint32_t i;
624 uint64_t temp_time = current_time(); 626 uint64_t temp_time = current_time();
625 for(i = 0; i < MAX_CONNECTIONS; ++i) { 627 for (i = 0; i < MAX_CONNECTIONS; ++i) {
626 if(connections[i].status == 1) 628 if (connections[i].status == 1)
627 if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { 629 if ((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
628 send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); 630 send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0);
629 connections[i].last_sent = temp_time; 631 connections[i].last_sent = temp_time;
630 } 632 }
631 633
632 /* kill all timed out connections */ 634 /* kill all timed out connections */
633 if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && 635 if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
634 connections[i].status != 4) 636 connections[i].status != 4)
635 /* kill_connection(i); */ 637 /* kill_connection(i); */
636 connections[i].status = 4; 638 connections[i].status = 4;
637 if(connections[i].status > 0 && connections[i].killat < temp_time) 639 if (connections[i].status > 0 && connections[i].killat < temp_time)
638 kill_connection(i); 640 kill_connection(i);
639 } 641 }
640} 642}
@@ -643,9 +645,9 @@ void doSYNC()
643{ 645{
644 uint32_t i; 646 uint32_t i;
645 uint64_t temp_time = current_time(); 647 uint64_t temp_time = current_time();
646 for(i = 0; i < MAX_CONNECTIONS; ++i) { 648 for (i = 0; i < MAX_CONNECTIONS; ++i) {
647 if(connections[i].status == 2 || connections[i].status == 3) 649 if (connections[i].status == 2 || connections[i].status == 3)
648 if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { 650 if ((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
649 send_SYNC(i); 651 send_SYNC(i);
650 connections[i].last_SYNC = temp_time; 652 connections[i].last_SYNC = temp_time;
651 } 653 }
@@ -657,10 +659,10 @@ void doData()
657 uint32_t i; 659 uint32_t i;
658 uint64_t j; 660 uint64_t j;
659 uint64_t temp_time = current_time(); 661 uint64_t temp_time = current_time();
660 for(i = 0; i < MAX_CONNECTIONS; ++i) 662 for (i = 0; i < MAX_CONNECTIONS; ++i)
661 if(connections[i].status == 3 && sendqueue(i) != 0) 663 if (connections[i].status == 3 && sendqueue(i) != 0)
662 if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { 664 if ((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
663 for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) 665 for (j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
664 send_DATA(i); 666 send_DATA(i);
665 connections[i].last_sent = temp_time; 667 connections[i].last_sent = temp_time;
666 } 668 }
@@ -675,15 +677,15 @@ void adjustRates()
675{ 677{
676 uint32_t i; 678 uint32_t i;
677 uint64_t temp_time = current_time(); 679 uint64_t temp_time = current_time();
678 for(i = 0; i < MAX_CONNECTIONS; ++i) { 680 for (i = 0; i < MAX_CONNECTIONS; ++i) {
679 if(connections[i].status == 1 || connections[i].status == 2) 681 if (connections[i].status == 1 || connections[i].status == 2)
680 connections[i].SYNC_rate = MAX_SYNC_RATE; 682 connections[i].SYNC_rate = MAX_SYNC_RATE;
681 if(connections[i].status == 3) { 683 if (connections[i].status == 3) {
682 if(sendqueue(i) != 0) { 684 if (sendqueue(i) != 0) {
683 connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; 685 connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
684 connections[i].SYNC_rate = MAX_SYNC_RATE; 686 connections[i].SYNC_rate = MAX_SYNC_RATE;
685 } 687 }
686 else if(connections[i].last_recvdata + 1000000UL > temp_time) 688 else if (connections[i].last_recvdata + 1000000UL > temp_time)
687 connections[i].SYNC_rate = MAX_SYNC_RATE; 689 connections[i].SYNC_rate = MAX_SYNC_RATE;
688 else 690 else
689 connections[i].SYNC_rate = SYNC_RATE; 691 connections[i].SYNC_rate = SYNC_RATE;