Skip to content

Commit

Permalink
Exposed and tested disconnect notification TCP packets.
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed May 16, 2014
1 parent 1e7164f commit a514167
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ START_TEST(test_client)
do_TCP_connection(conn);
do_TCP_connection(conn2);
ck_assert_msg(data_callback_good == 1, "data callback not called");
status_callback_good = 0;
send_disconnect_request(conn2, 0);
c_sleep(50);
do_TCP_server(tcp_s);
c_sleep(50);
do_TCP_connection(conn);
do_TCP_connection(conn2);
ck_assert_msg(status_callback_good == 1, "status callback not called");
ck_assert_msg(status_callback_status == 1, "wrong status");
}
END_TEST

Expand Down
15 changes: 15 additions & 0 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ static int send_ping_response(TCP_Client_Connection *con, uint64_t ping_id)
return write_packet_TCP_secure_connection(con, packet, sizeof(packet));
}

/* return 1 on success.
* return 0 if could not send packet.
* return -1 on failure (connection must be killed).
*/
int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id)
{
if (con_id >= NUM_CLIENT_CONNECTIONS)
return -1;

con->connections[con_id].status = 0;
con->connections[con_id].number = 0;
return send_disconnect_notification(con, con_id + NUM_RESERVED_PORTS);
}

/* return 1 on success.
* return 0 if could not send packet.
* return -1 on failure (connection must be killed).
Expand Down Expand Up @@ -564,6 +578,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection)

if (sizeof(data) == len) {
if (handle_handshake(TCP_connection, data) == 0) {
TCP_connection->kill_at = ~0;
TCP_connection->status = TCP_CLIENT_CONFIRMED;
} else {
TCP_connection->kill_at = 0;
Expand Down
6 changes: 6 additions & 0 deletions toxcore/TCP_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ void routing_response_handler(TCP_Client_Connection *con, int (*response_callbac
void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint32_t number,
uint8_t connection_id, uint8_t status), void *object);

/* return 1 on success.
* return 0 if could not send packet.
* return -1 on failure (connection must be killed).
*/
int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id);

/* Set the number that will be used as an argument in the callbacks related to con_id.
*
* When not set by this function, the number is ~0.
Expand Down

0 comments on commit a514167

Please sign in to comment.