Pings and onion packets implemented in TCP_Client.c

Astyled one test and added a couple lines to another.
This commit is contained in:
irungentoo 2014-04-12 22:00:46 -04:00
parent 881e95671a
commit 268172ec41
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 100 additions and 11 deletions

View File

@ -320,6 +320,18 @@ START_TEST(test_client)
do_TCP_connection(conn);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
c_sleep(500);
do_TCP_connection(conn);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
c_sleep(500);
do_TCP_connection(conn);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
do_TCP_server(tcp_s);
c_sleep(50);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
}
END_TEST

View File

@ -296,8 +296,8 @@ START_TEST(test_AV)
toxav_recv_video(status_control.Alice.av, &video_storage);
if ( video_storage ) {
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/
}
@ -351,8 +351,8 @@ START_TEST(test_AV)
toxav_recv_video(status_control.Alice.av, &video_storage);
if ( video_storage ) {
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/
}
@ -370,8 +370,8 @@ START_TEST(test_AV)
toxav_recv_video(status_control.Bob.av, &video_storage);
if ( video_storage ) {
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Alice is invalid");*/
}

View File

@ -219,7 +219,7 @@ static int send_ping_response(TCP_Client_Connection *con, uint64_t ping_id)
* return 0 if could not send packet.
* return -1 on failure (connection must be killed).
*/
static int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t length)
int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t length)
{
uint8_t packet[1 + length];
packet[0] = TCP_PACKET_ONION_REQUEST;
@ -227,6 +227,13 @@ static int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_
return write_packet_TCP_secure_connection(con, packet, sizeof(packet));
}
void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data,
uint16_t length), void *object)
{
con->onion_callback = onion_callback;
con->onion_callback_object = object;
}
/* Create new TCP connection to ip_port/public_key
*/
TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, uint8_t *public_key, uint8_t *self_public_key,
@ -282,6 +289,18 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_
return -1;
switch (data[0]) {
case TCP_PACKET_ROUTING_RESPONSE: {
return 0;
}
case TCP_PACKET_CONNECTION_NOTIFICATION: {
return 0;
}
case TCP_PACKET_DISCONNECT_NOTIFICATION: {
return 0;
}
case TCP_PACKET_PING: {
if (length != 1 + sizeof(uint64_t))
return -1;
@ -292,8 +311,35 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_
return 0;
}
default: {
case TCP_PACKET_PONG: {
if (length != 1 + sizeof(uint64_t))
return -1;
uint64_t ping_id;
memcpy(&ping_id, data + 1, sizeof(uint64_t));
if (ping_id) {
if (ping_id == conn->ping_id) {
conn->ping_id = 0;
}
return 0;
} else {
return -1;
}
}
case TCP_PACKET_ONION_RESPONSE: {
conn->onion_callback(conn->onion_callback_object, data + 1, length - 1);
return 0;
}
default: {
if (data[0] < NUM_RESERVED_PORTS)
return -1;
uint8_t con_id = data[0] - NUM_RESERVED_PORTS;
//TODO
}
}
@ -306,6 +352,25 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
uint8_t packet[MAX_PACKET_SIZE];
int len;
if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
uint64_t ping_id = random_64b();
if (!ping_id)
++ping_id;
int ret = send_ping_request(conn, ping_id);
if (ret == 1) {
conn->last_pinged = unix_time();
conn->ping_id = ping_id;
}
}
if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {
conn->status = TCP_CLIENT_DISCONNECTED;
return 0;
}
while ((len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key,
conn->recv_nonce, packet, sizeof(packet)))) {
if (len == -1) {

View File

@ -55,6 +55,14 @@ typedef struct {
uint64_t last_pinged;
uint64_t ping_id;
struct {
uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
} connections[NUM_CLIENT_CONNECTIONS];
int (*onion_callback)(void *object, uint8_t *data, uint16_t length);
void *onion_callback_object;
} TCP_Client_Connection;
/* Create new TCP connection to ip_port/public_key
@ -70,9 +78,13 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection);
*/
void kill_TCP_connection(TCP_Client_Connection *TCP_connection);
int get_TCP_connection_status(TCP_Client_Connection *TCP_connection);
int read_TCP_connection(TCP_Client_Connection *TCP_connection, uint8_t *data);
/* return 1 on success.
* return 0 if could not send packet.
* return -1 on failure (connection must be killed).
*/
int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t length);
void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data,
uint16_t length), void *object);
#endif