diff options
author | irungentoo <irungentoo@gmail.com> | 2014-05-16 12:43:53 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-05-16 12:43:53 -0400 |
commit | cda3cedd37fd66d23362e326562ec666e51dc6fd (patch) | |
tree | aa302c886d799d09e70a08eebf94397bfcac6074 /toxcore | |
parent | 297281dea5166aa2dd12fd98e9148562645d4725 (diff) |
TCP_server fix.
Clear the connections array when connection is killed.
Don't allow duplicate entries in connections array.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/TCP_server.c | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index ec8dbf1a..8be22593 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -350,6 +350,8 @@ static void kill_TCP_connection(TCP_Secure_Connection *con) | |||
350 | memset(con, 0, sizeof(TCP_Secure_Connection)); | 350 | memset(con, 0, sizeof(TCP_Secure_Connection)); |
351 | } | 351 | } |
352 | 352 | ||
353 | static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number); | ||
354 | |||
353 | /* Kill an accepted TCP_Secure_Connection | 355 | /* Kill an accepted TCP_Secure_Connection |
354 | * | 356 | * |
355 | * return -1 on failure. | 357 | * return -1 on failure. |
@@ -360,6 +362,12 @@ static int kill_accepted(TCP_Server *TCP_server, int index) | |||
360 | if ((uint32_t)index >= TCP_server->size_accepted_connections) | 362 | if ((uint32_t)index >= TCP_server->size_accepted_connections) |
361 | return -1; | 363 | return -1; |
362 | 364 | ||
365 | uint32_t i; | ||
366 | |||
367 | for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { | ||
368 | rm_connection_index(TCP_server, &TCP_server->accepted_connection_array[index], i); | ||
369 | } | ||
370 | |||
363 | sock_t sock = TCP_server->accepted_connection_array[index].sock; | 371 | sock_t sock = TCP_server->accepted_connection_array[index].sock; |
364 | 372 | ||
365 | if (del_accepted(TCP_server, index) != 0) | 373 | if (del_accepted(TCP_server, index) != 0) |
@@ -482,9 +490,16 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, uint8 | |||
482 | } | 490 | } |
483 | 491 | ||
484 | for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { | 492 | for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { |
485 | if (con->connections[i].status == 0) { | 493 | if (con->connections[i].status != 0) { |
494 | if (memcmp(public_key, con->connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { | ||
495 | if (send_routing_response(con, i + NUM_RESERVED_PORTS, public_key) == -1) { | ||
496 | return -1; | ||
497 | } else { | ||
498 | return 0; | ||
499 | } | ||
500 | } | ||
501 | } else if (index == (uint32_t)~0) { | ||
486 | index = i; | 502 | index = i; |
487 | break; | ||
488 | } | 503 | } |
489 | } | 504 | } |
490 | 505 | ||
@@ -560,7 +575,12 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, uint8_t | |||
560 | return 0; | 575 | return 0; |
561 | } | 576 | } |
562 | 577 | ||
563 | static int disconnect_conection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) | 578 | /* Remove connection with con_number from the connections array of con. |
579 | * | ||
580 | * return -1 on failure. | ||
581 | * return 0 on success. | ||
582 | */ | ||
583 | static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) | ||
564 | { | 584 | { |
565 | if (con_number >= NUM_CLIENT_CONNECTIONS) | 585 | if (con_number >= NUM_CLIENT_CONNECTIONS) |
566 | return -1; | 586 | return -1; |
@@ -584,14 +604,21 @@ static int disconnect_conection_index(TCP_Server *TCP_server, TCP_Secure_Connect | |||
584 | con->connections[con_number].index = 0; | 604 | con->connections[con_number].index = 0; |
585 | con->connections[con_number].other_id = 0; | 605 | con->connections[con_number].other_id = 0; |
586 | con->connections[con_number].status = 0; | 606 | con->connections[con_number].status = 0; |
587 | //TODO: return values? | ||
588 | send_disconnect_notification(con, con_number); | ||
589 | return 0; | 607 | return 0; |
590 | } else { | 608 | } else { |
591 | return -1; | 609 | return -1; |
592 | } | 610 | } |
593 | } | 611 | } |
594 | 612 | ||
613 | static int disconnect_conection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) | ||
614 | { | ||
615 | if (rm_connection_index(TCP_server, con, con_number) != 0) | ||
616 | return -1; | ||
617 | |||
618 | send_disconnect_notification(con, con_number); | ||
619 | return 0; | ||
620 | } | ||
621 | |||
595 | static int handle_onion_recv_1(void *object, IP_Port dest, uint8_t *data, uint16_t length) | 622 | static int handle_onion_recv_1(void *object, IP_Port dest, uint8_t *data, uint16_t length) |
596 | { | 623 | { |
597 | TCP_Server *TCP_server = object; | 624 | TCP_Server *TCP_server = object; |