diff options
-rw-r--r-- | testing/misc_tools.h | 4 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.c | 161 |
2 files changed, 77 insertions, 88 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index 705f4aab..2d8707ed 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -166,7 +166,7 @@ static inline void tox_array_pop(tox_array *arr, uint32_t num) | |||
166 | 166 | ||
167 | 167 | ||
168 | #define tox_array_for_each(arr, type, tmp_name) \ | 168 | #define tox_array_for_each(arr, type, tmp_name) \ |
169 | type tmp_name; uint32_t tmp_name ## _i = 0; \ | 169 | type *tmp_name; uint32_t tmp_name ## _i = 0; \ |
170 | for (; tmp_name ## _i != (arr)->len; tmp_name = tox_array_get(arr, ++ tmp_name ## _i, type)) | 170 | for (; tmp_name ## _i != (arr)->len; tmp_name = &tox_array_get(arr, ++ tmp_name ## _i, type)) |
171 | 171 | ||
172 | #endif // MISC_TOOLS_H | 172 | #endif // MISC_TOOLS_H |
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 73df0e80..d6f01349 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c | |||
@@ -39,9 +39,9 @@ | |||
39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) | 39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) |
40 | { | 40 | { |
41 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 41 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
42 | if (tmp.ip_port.ip.i == ip_port.ip.i && | 42 | if (tmp->ip_port.ip.i == ip_port.ip.i && |
43 | tmp.ip_port.port == ip_port.port && | 43 | tmp->ip_port.port == ip_port.port && |
44 | tmp.status > 0) | 44 | tmp->status > 0) |
45 | { | 45 | { |
46 | return tmp_i; | 46 | return tmp_i; |
47 | } | 47 | } |
@@ -87,20 +87,19 @@ static void change_handshake(Lossless_UDP *ludp, IP_Port source) | |||
87 | 87 | ||
88 | /* | 88 | /* |
89 | * Initialize a new connection to ip_port | 89 | * Initialize a new connection to ip_port |
90 | * Returns an integer corresponding to the connection idt | 90 | * Returns an integer corresponding to the connection id. |
91 | * Return -1 if it could not initialize the connectiont | 91 | * Return -1 if it could not initialize the connectiont |
92 | * If there already was an existing connection to that ip_port return its number. | 92 | * If there already was an existing connection to that ip_port return its number. |
93 | */ | 93 | */ |
94 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | 94 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port) |
95 | { | 95 | { |
96 | int connect = getconnection_id(ludp, ip_port); | 96 | int connection_id = getconnection_id(ludp, ip_port); |
97 | 97 | ||
98 | if (connect != -1) | 98 | if (connection_id != -1) |
99 | return connect; | 99 | return connection_id; |
100 | 100 | ||
101 | int connection_id = -1; | ||
102 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 101 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
103 | if (tmp.status == 0) { | 102 | if (tmp->status == 0) { |
104 | connection_id = tmp_i; | 103 | connection_id = tmp_i; |
105 | break; | 104 | break; |
106 | } | 105 | } |
@@ -145,11 +144,11 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | |||
145 | static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | 144 | static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) |
146 | { | 145 | { |
147 | if (getconnection_id(ludp, ip_port) != -1) | 146 | if (getconnection_id(ludp, ip_port) != -1) |
148 | return -1; | 147 | return -1; /* TODO: return existing connection instead? */ |
149 | 148 | ||
150 | int connection_id = -1; | 149 | int connection_id = -1; |
151 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 150 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
152 | if (tmp.status == 0) { | 151 | if (tmp->status == 0) { |
153 | connection_id = tmp_i; | 152 | connection_id = tmp_i; |
154 | break; | 153 | break; |
155 | } | 154 | } |
@@ -191,12 +190,11 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | |||
191 | int incoming_connection(Lossless_UDP *ludp) | 190 | int incoming_connection(Lossless_UDP *ludp) |
192 | { | 191 | { |
193 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 192 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
194 | if (tmp.inbound == 2) { | 193 | if (tmp->inbound == 2) { |
195 | tmp.inbound = 1; | 194 | tmp->inbound = 1; |
196 | return tmp_i; | 195 | return tmp_i; |
197 | } | 196 | } |
198 | } | 197 | } |
199 | |||
200 | return -1; | 198 | return -1; |
201 | } | 199 | } |
202 | 200 | ||
@@ -206,9 +204,8 @@ int incoming_connection(Lossless_UDP *ludp) | |||
206 | */ | 204 | */ |
207 | int kill_connection(Lossless_UDP *ludp, int connection_id) | 205 | int kill_connection(Lossless_UDP *ludp, int connection_id) |
208 | { | 206 | { |
209 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
210 | |||
211 | if (connection_id >= 0 && connection_id < ludp->connections.len) { | 207 | if (connection_id >= 0 && connection_id < ludp->connections.len) { |
208 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
212 | if (connection->status > 0) { | 209 | if (connection->status > 0) { |
213 | connection->status = 0; | 210 | connection->status = 0; |
214 | change_handshake(ludp, connection->ip_port); | 211 | change_handshake(ludp, connection->ip_port); |
@@ -228,8 +225,9 @@ int kill_connection(Lossless_UDP *ludp, int connection_id) | |||
228 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) | 225 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) |
229 | { | 226 | { |
230 | if (connection_id >= 0 && connection_id < ludp->connections.len) { | 227 | if (connection_id >= 0 && connection_id < ludp->connections.len) { |
231 | if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) { | 228 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
232 | tox_array_get(&ludp->connections, connection_id, Connection).killat = current_time() + 1000000UL * seconds; | 229 | if (connection->status > 0) { |
230 | connection->killat = current_time() + 1000000UL * seconds; | ||
233 | return 0; | 231 | return 0; |
234 | } | 232 | } |
235 | } | 233 | } |
@@ -269,8 +267,8 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) | |||
269 | if (connection_id < 0 || connection_id >= ludp->connections.len) | 267 | if (connection_id < 0 || connection_id >= ludp->connections.len) |
270 | return 0; | 268 | return 0; |
271 | 269 | ||
272 | return tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum - | 270 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
273 | tox_array_get(&ludp->connections, connection_id, Connection).successful_sent; | 271 | return connection->sendbuff_packetnum - connection->successful_sent; |
274 | } | 272 | } |
275 | 273 | ||
276 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 274 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
@@ -279,19 +277,20 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) | |||
279 | if (connection_id < 0 || connection_id >= ludp->connections.len) | 277 | if (connection_id < 0 || connection_id >= ludp->connections.len) |
280 | return 0; | 278 | return 0; |
281 | 279 | ||
282 | return tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum - tox_array_get(&ludp->connections, connection_id, Connection).successful_read; | 280 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
281 | return connection->recv_packetnum - connection->successful_read; | ||
283 | } | 282 | } |
284 | 283 | ||
285 | /* returns the id of the next packet in the queue | 284 | /* returns the id of the next packet in the queue |
286 | return -1 if no packet in queue */ | 285 | return -1 if no packet in queue */ |
287 | char id_packet(Lossless_UDP *ludp, int connection_id) | 286 | char id_packet(Lossless_UDP *ludp, int connection_id) |
288 | { | 287 | { |
289 | if (connection_id < 0 || connection_id >= ludp->connections.len) | 288 | if (connection_id < 0 || connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0) |
290 | return -1; | 289 | return -1; |
291 | 290 | ||
292 | if (recvqueue(ludp, connection_id) != 0 && tox_array_get(&ludp->connections, connection_id, Connection).status != 0) | 291 | Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
293 | return tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[tox_array_get(&ludp->connections, connection_id, Connection).successful_read % | 292 | if (connection->status != 0) |
294 | MAX_QUEUE_NUM].data[0]; | 293 | return connection->recvbuffer[connection->successful_read % MAX_QUEUE_NUM].data[0]; |
295 | 294 | ||
296 | return -1; | 295 | return -1; |
297 | } | 296 | } |
@@ -300,17 +299,17 @@ char id_packet(Lossless_UDP *ludp, int connection_id) | |||
300 | return length of received packet if successful */ | 299 | return length of received packet if successful */ |
301 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) | 300 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) |
302 | { | 301 | { |
303 | if (recvqueue(ludp, connection_id) != 0) { | 302 | if (recvqueue(ludp, connection_id) == 0) |
304 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | 303 | return 0; |
305 | uint16_t index = connection->successful_read % MAX_QUEUE_NUM; | 304 | |
306 | uint16_t size = connection->recvbuffer[index].size; | 305 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
307 | memcpy(data, connection->recvbuffer[index].data, size); | 306 | uint16_t index = connection->successful_read % MAX_QUEUE_NUM; |
308 | ++connection->successful_read; | 307 | uint16_t size = connection->recvbuffer[index].size; |
309 | connection->recvbuffer[index].size = 0; | 308 | memcpy(data, connection->recvbuffer[index].data, size); |
310 | return size; | 309 | ++connection->successful_read; |
311 | } | 310 | connection->recvbuffer[index].size = 0; |
311 | return size; | ||
312 | 312 | ||
313 | return 0; | ||
314 | } | 313 | } |
315 | 314 | ||
316 | /* | 315 | /* |
@@ -319,32 +318,28 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) | |||
319 | */ | 318 | */ |
320 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) | 319 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) |
321 | { | 320 | { |
322 | if (length > MAX_DATA_SIZE || length == 0) | 321 | if (length > MAX_DATA_SIZE || length == 0 || sendqueue(ludp, connection_id) >= BUFFER_PACKET_NUM) |
323 | return 0; | 322 | return 0; |
324 | 323 | ||
325 | if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) { | ||
326 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | 324 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
327 | uint32_t index = connection->sendbuff_packetnum % MAX_QUEUE_NUM; | 325 | uint32_t index = connection->sendbuff_packetnum % MAX_QUEUE_NUM; |
328 | memcpy(connection->sendbuffer[index].data, data, length); | 326 | memcpy(connection->sendbuffer[index].data, data, length); |
329 | connection->sendbuffer[index].size = length; | 327 | connection->sendbuffer[index].size = length; |
330 | connection->sendbuff_packetnum++; | 328 | connection->sendbuff_packetnum++; |
331 | return 1; | 329 | return 1; |
332 | } | ||
333 | |||
334 | return 0; | ||
335 | } | 330 | } |
336 | 331 | ||
337 | /* put the packet numbers the we are missing in requested and return the number */ | 332 | /* put the packet numbers the we are missing in requested and return the number */ |
338 | uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) | 333 | uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) |
339 | { | 334 | { |
340 | uint32_t number = 0; | ||
341 | uint32_t i; | ||
342 | uint32_t temp; | ||
343 | |||
344 | /* don't request packets if the buffer is full. */ | 335 | /* don't request packets if the buffer is full. */ |
345 | if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) | 336 | if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) |
346 | return 0; | 337 | return 0; |
347 | 338 | ||
339 | uint32_t number = 0; | ||
340 | uint32_t i; | ||
341 | uint32_t temp; | ||
342 | |||
348 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | 343 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
349 | 344 | ||
350 | for (i = connection->recv_packetnum; | 345 | for (i = connection->recv_packetnum; |
@@ -387,7 +382,6 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak | |||
387 | static int send_SYNC(Lossless_UDP *ludp, int connection_id) | 382 | static int send_SYNC(Lossless_UDP *ludp, int connection_id) |
388 | { | 383 | { |
389 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | 384 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
390 | |||
391 | uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; | 385 | uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; |
392 | uint16_t index = 0; | 386 | uint16_t index = 0; |
393 | 387 | ||
@@ -470,15 +464,14 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3 | |||
470 | 464 | ||
471 | uint32_t temp; | 465 | uint32_t temp; |
472 | uint32_t handshake_id1, handshake_id2; | 466 | uint32_t handshake_id1, handshake_id2; |
473 | |||
474 | int connection_id = getconnection_id(ludp, source); | 467 | int connection_id = getconnection_id(ludp, source); |
475 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
476 | 468 | ||
477 | memcpy(&temp, packet + 1, 4); | 469 | memcpy(&temp, packet + 1, 4); |
478 | handshake_id1 = ntohl(temp); | 470 | handshake_id1 = ntohl(temp); |
479 | memcpy(&temp, packet + 5, 4); | 471 | memcpy(&temp, packet + 5, 4); |
480 | handshake_id2 = ntohl(temp); | 472 | handshake_id2 = ntohl(temp); |
481 | 473 | ||
474 | |||
482 | if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) { | 475 | if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) { |
483 | send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1); | 476 | send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1); |
484 | return 0; | 477 | return 0; |
@@ -487,6 +480,7 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3 | |||
487 | if (is_connected(ludp, connection_id) != 1) | 480 | if (is_connected(ludp, connection_id) != 1) |
488 | return 1; | 481 | return 1; |
489 | 482 | ||
483 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
490 | /* if handshake_id2 is what we sent previously as handshake_id1 */ | 484 | /* if handshake_id2 is what we sent previously as handshake_id1 */ |
491 | if (handshake_id2 == connection->handshake_id1) { | 485 | if (handshake_id2 == connection->handshake_id1) { |
492 | connection->status = 2; | 486 | connection->status = 2; |
@@ -519,9 +513,9 @@ static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packet | |||
519 | { | 513 | { |
520 | if (handshake_id(ludp, source) == recv_packetnum) { | 514 | if (handshake_id(ludp, source) == recv_packetnum) { |
521 | int connection_id = new_inconnection(ludp, source); | 515 | int connection_id = new_inconnection(ludp, source); |
522 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
523 | 516 | ||
524 | if (connection_id != -1) { | 517 | if (connection_id != -1) { |
518 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
525 | connection->orecv_packetnum = recv_packetnum; | 519 | connection->orecv_packetnum = recv_packetnum; |
526 | connection->sent_packetnum = recv_packetnum; | 520 | connection->sent_packetnum = recv_packetnum; |
527 | connection->sendbuff_packetnum = recv_packetnum; | 521 | connection->sendbuff_packetnum = recv_packetnum; |
@@ -545,7 +539,7 @@ static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, | |||
545 | 539 | ||
546 | if (recv_packetnum == connection->orecv_packetnum) { | 540 | if (recv_packetnum == connection->orecv_packetnum) { |
547 | /* && sent_packetnum == connection->osent_packetnum) */ | 541 | /* && sent_packetnum == connection->osent_packetnum) */ |
548 | connection->status = 3; | 542 | connection->status = 3; |
549 | connection->recv_counter = counter; | 543 | connection->recv_counter = counter; |
550 | ++connection->send_counter; | 544 | ++connection->send_counter; |
551 | send_SYNC(ludp, connection_id); | 545 | send_SYNC(ludp, connection_id); |
@@ -571,9 +565,9 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, | |||
571 | 565 | ||
572 | /* packet valid */ | 566 | /* packet valid */ |
573 | if (comp_1 <= BUFFER_PACKET_NUM && | 567 | if (comp_1 <= BUFFER_PACKET_NUM && |
574 | comp_2 <= BUFFER_PACKET_NUM && | 568 | comp_2 <= BUFFER_PACKET_NUM && |
575 | comp_counter < 10 && comp_counter != 0) { | 569 | comp_counter < 10 && comp_counter != 0) |
576 | 570 | { | |
577 | connection->orecv_packetnum = recv_packetnum; | 571 | connection->orecv_packetnum = recv_packetnum; |
578 | connection->osent_packetnum = sent_packetnum; | 572 | connection->osent_packetnum = sent_packetnum; |
579 | connection->successful_sent = recv_packetnum; | 573 | connection->successful_sent = recv_packetnum; |
@@ -610,22 +604,22 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
610 | memcpy(&counter, packet + 1, 1); | 604 | memcpy(&counter, packet + 1, 1); |
611 | memcpy(&temp, packet + 2, 4); | 605 | memcpy(&temp, packet + 2, 4); |
612 | recv_packetnum = ntohl(temp); | 606 | recv_packetnum = ntohl(temp); |
613 | memcpy(&temp, packet + 6, 4); | 607 | memcpy(&temp, packet + 6, 4); |
614 | sent_packetnum = ntohl(temp); | 608 | sent_packetnum = ntohl(temp); |
615 | 609 | ||
616 | if (number != 0) | 610 | if (number != 0) |
617 | memcpy(req_packets, packet + 10, 4 * number); | 611 | memcpy(req_packets, packet + 10, 4 * number); |
618 | 612 | ||
619 | int connection_id = getconnection_id(ludp, source); | 613 | int connection_id = getconnection_id(ludp, source); |
620 | if (connection_id == -1) | 614 | if (connection_id == -1) |
621 | return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum); | 615 | return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum); |
622 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | 616 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); |
623 | 617 | ||
624 | if (connection->status == 2) | 618 | if (connection->status == 2) |
625 | return handle_SYNC2(ludp, connection_id, counter, | 619 | return handle_SYNC2(ludp, connection_id, counter, |
626 | recv_packetnum, sent_packetnum); | 620 | recv_packetnum, sent_packetnum); |
627 | 621 | ||
628 | if (connection->status == 3) | 622 | if (connection->status == 3) |
629 | return handle_SYNC3(ludp, connection_id, counter, recv_packetnum, | 623 | return handle_SYNC3(ludp, connection_id, counter, recv_packetnum, |
630 | sent_packetnum, req_packets, number); | 624 | sent_packetnum, req_packets, number); |
631 | 625 | ||
@@ -638,11 +632,10 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
638 | */ | 632 | */ |
639 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) | 633 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) |
640 | { | 634 | { |
641 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
642 | |||
643 | if (size > MAX_DATA_SIZE) | 635 | if (size > MAX_DATA_SIZE) |
644 | return 1; | 636 | return 1; |
645 | 637 | ||
638 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
646 | uint32_t i; | 639 | uint32_t i; |
647 | uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM; | 640 | uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM; |
648 | uint32_t sent_packet = data_num - connection->osent_packetnum; | 641 | uint32_t sent_packet = data_num - connection->osent_packetnum; |
@@ -654,9 +647,8 @@ static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, ui | |||
654 | connection->recvbuffer[i % MAX_QUEUE_NUM].size = size; | 647 | connection->recvbuffer[i % MAX_QUEUE_NUM].size = size; |
655 | connection->last_recvdata = current_time(); | 648 | connection->last_recvdata = current_time(); |
656 | 649 | ||
657 | if (sent_packet < BUFFER_PACKET_NUM) { | 650 | if (sent_packet < BUFFER_PACKET_NUM) |
658 | connection->osent_packetnum = data_num; | 651 | connection->osent_packetnum = data_num; |
659 | } | ||
660 | 652 | ||
661 | break; | 653 | break; |
662 | } | 654 | } |
@@ -676,12 +668,9 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
676 | { | 668 | { |
677 | Lossless_UDP *ludp = object; | 669 | Lossless_UDP *ludp = object; |
678 | int connection_id = getconnection_id(ludp, source); | 670 | int connection_id = getconnection_id(ludp, source); |
679 | if (connection_id == -1) | ||
680 | return 1; | ||
681 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
682 | 671 | ||
683 | /* Drop the data packet if connection is not connected. */ | 672 | /* Drop the data packet if connection is not connected. */ |
684 | if (connection->status != 3) | 673 | if (connection_id == -1 || tox_array_get(&ludp->connections, connection_id, Connection).status != 3) |
685 | return 1; | 674 | return 1; |
686 | 675 | ||
687 | if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) | 676 | if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) |
@@ -728,19 +717,19 @@ static void do_new(Lossless_UDP *ludp) | |||
728 | uint64_t temp_time = current_time(); | 717 | uint64_t temp_time = current_time(); |
729 | 718 | ||
730 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 719 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
731 | if (tmp.status == 1) | 720 | if (tmp->status == 1) |
732 | if ((tmp.last_sent + (1000000UL / tmp.SYNC_rate)) <= temp_time) { | 721 | if ((tmp->last_sent + (1000000UL / tmp->SYNC_rate)) <= temp_time) { |
733 | send_handshake(ludp, tmp.ip_port, tmp.handshake_id1, 0); | 722 | send_handshake(ludp, tmp->ip_port, tmp->handshake_id1, 0); |
734 | tmp.last_sent = temp_time; | 723 | tmp->last_sent = temp_time; |
735 | } | 724 | } |
736 | 725 | ||
737 | /* kill all timed out connections */ | 726 | /* kill all timed out connections */ |
738 | if (tmp.status > 0 && (tmp.last_recvSYNC + tmp.timeout * 1000000UL) < temp_time && tmp.status != 4) { | 727 | if (tmp->status > 0 && (tmp->last_recvSYNC + tmp->timeout * 1000000UL) < temp_time && tmp->status != 4) { |
739 | tmp.status = 4; | 728 | tmp->status = 4; |
740 | /* kill_connection(i); */ | 729 | /* kill_connection(i); */ |
741 | } | 730 | } |
742 | 731 | ||
743 | if (tmp.status > 0 && tmp.killat < temp_time) | 732 | if (tmp->status > 0 && tmp->killat < temp_time) |
744 | kill_connection(ludp, i); | 733 | kill_connection(ludp, i); |
745 | } | 734 | } |
746 | } | 735 | } |
@@ -751,10 +740,10 @@ static void do_SYNC(Lossless_UDP *ludp) | |||
751 | uint64_t temp_time = current_time(); | 740 | uint64_t temp_time = current_time(); |
752 | 741 | ||
753 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 742 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
754 | if (tmp.status == 2 || tmp.status == 3) | 743 | if (tmp->status == 2 || tmp->status == 3) |
755 | if ((tmp.last_SYNC + (1000000UL / tmp.SYNC_rate)) <= temp_time) { | 744 | if ((tmp->last_SYNC + (1000000UL / tmp->SYNC_rate)) <= temp_time) { |
756 | send_SYNC(ludp, i); | 745 | send_SYNC(ludp, i); |
757 | tmp.last_SYNC = temp_time; | 746 | tmp->last_SYNC = temp_time; |
758 | } | 747 | } |
759 | } | 748 | } |
760 | } | 749 | } |
@@ -766,13 +755,13 @@ static void do_data(Lossless_UDP *ludp) | |||
766 | uint64_t temp_time = current_time(); | 755 | uint64_t temp_time = current_time(); |
767 | 756 | ||
768 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 757 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
769 | if (tmp.status == 3 && sendqueue(ludp, i) != 0 && | 758 | if (tmp->status == 3 && sendqueue(ludp, i) != 0 && |
770 | (tmp.last_sent + (1000000UL / tmp.data_rate)) <= temp_time) | 759 | (tmp->last_sent + (1000000UL / tmp->data_rate)) <= temp_time) |
771 | { | 760 | { |
772 | for (j = tmp.last_sent; j < temp_time; j += (1000000UL / tmp.data_rate)) | 761 | for (j = tmp->last_sent; j < temp_time; j += (1000000UL / tmp->data_rate)) |
773 | send_DATA(ludp, i); | 762 | send_DATA(ludp, i); |
774 | 763 | ||
775 | tmp.last_sent = temp_time; | 764 | tmp->last_sent = temp_time; |
776 | 765 | ||
777 | } | 766 | } |
778 | } | 767 | } |
@@ -791,17 +780,17 @@ static void adjust_rates(Lossless_UDP *ludp) | |||
791 | uint64_t temp_time = current_time(); | 780 | uint64_t temp_time = current_time(); |
792 | 781 | ||
793 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 782 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
794 | if (tmp.status == 1 || tmp.status == 2) | 783 | if (tmp->status == 1 || tmp->status == 2) |
795 | tmp.SYNC_rate = MAX_SYNC_RATE; | 784 | tmp->SYNC_rate = MAX_SYNC_RATE; |
796 | 785 | ||
797 | if (tmp.status == 3) { | 786 | if (tmp->status == 3) { |
798 | if (sendqueue(ludp, i) != 0) { | 787 | if (sendqueue(ludp, i) != 0) { |
799 | tmp.data_rate = (BUFFER_PACKET_NUM - tmp.num_req_paquets) * MAX_SYNC_RATE; | 788 | tmp->data_rate = (BUFFER_PACKET_NUM - tmp->num_req_paquets) * MAX_SYNC_RATE; |
800 | tmp.SYNC_rate = MAX_SYNC_RATE; | 789 | tmp->SYNC_rate = MAX_SYNC_RATE; |
801 | } else if (tmp.last_recvdata + 1000000UL > temp_time) | 790 | } else if (tmp->last_recvdata + 1000000UL > temp_time) |
802 | tmp.SYNC_rate = MAX_SYNC_RATE; | 791 | tmp->SYNC_rate = MAX_SYNC_RATE; |
803 | else | 792 | else |
804 | tmp.SYNC_rate = SYNC_RATE; | 793 | tmp->SYNC_rate = SYNC_RATE; |
805 | } | 794 | } |
806 | } | 795 | } |
807 | } | 796 | } |