From 83c6e9dd357a64cb60ebeb33dd96d85256820366 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 22 Sep 2013 11:08:23 -0400 Subject: Fixed the connection bug and cleaned up some stuff. --- testing/Lossless_UDP_testserver.c | 2 +- toxcore/Lossless_UDP.c | 28 ++++++++++++++-- toxcore/Lossless_UDP.h | 13 ++++++-- toxcore/Messenger.c | 4 +-- toxcore/net_crypto.c | 69 ++++++++++----------------------------- toxcore/net_crypto.h | 5 --- 6 files changed, 55 insertions(+), 66 deletions(-) diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 8deace82..5a89b00e 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -185,7 +185,7 @@ int main(int argc, char *argv[]) while (1) { networking_poll(ludp->net); do_lossless_udp(ludp); - connection = incoming_connection(ludp); + connection = incoming_connection(ludp, 0); if (connection != -1) { if (is_connected(ludp, connection) == 2) { diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 25d38b8d..feae6767 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -303,13 +303,13 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) } /* - * return an integer corresponding to the next connection in our incoming connection list. + * return an integer corresponding to the next connection in our incoming connection list with at least numpackets in the recieve queue. * return -1 if there are no new incoming connections in the list. */ -int incoming_connection(Lossless_UDP *ludp) +int incoming_connection(Lossless_UDP *ludp, uint32_t numpackets) { tox_array_for_each(&ludp->connections, Connection, tmp) { - if (tmp->inbound == 2) { + if (tmp->inbound == 2 && tmp->recv_packetnum - tmp->successful_read >= numpackets) { tmp->inbound = 1; return tmp_i; } @@ -392,6 +392,27 @@ int is_connected(Lossless_UDP *ludp, int connection_id) return 0; } +/* Check if connection is confirmed. + * + * returns 1 if yes. + * returns 0 if no/failure. + */ +int connection_confirmed(Lossless_UDP *ludp, int connection_id) +{ + if ((unsigned int)connection_id >= ludp->connections.len) + return 0; + + Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); + + if (connection->status == 0) + return 0; + + if (connection->confirmed == 1) + return 1; + + return 0; +} + /* Confirm an incoming connection. * Also disables the auto kill timeout on incomming connections. * @@ -410,6 +431,7 @@ int confirm_connection(Lossless_UDP *ludp, int connection_id) connection->killat = ~0; connection->confirmed = 1; + connection->inbound = 0; return 0; } diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h index 9b5c2406..2ef6f4d9 100644 --- a/toxcore/Lossless_UDP.h +++ b/toxcore/Lossless_UDP.h @@ -155,10 +155,11 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port); */ int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); -/* return an integer corresponding to the next connection in our imcoming connection list. +/* + * return an integer corresponding to the next connection in our incoming connection list with at least numpackets in the recieve queue. * return -1 if there are no new incoming connections in the list. */ -int incoming_connection(Lossless_UDP *ludp); +int incoming_connection(Lossless_UDP *ludp, uint32_t numpackets); /* return -1 if it could not kill the connection. * return 0 if killed successfully. @@ -173,6 +174,14 @@ int kill_connection(Lossless_UDP *ludp, int connection_id); */ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); + +/* Check if connection is confirmed. + * + * returns 1 if yes. + * returns 0 if no. + */ +int connection_confirmed(Lossless_UDP *ludp, int connection_id); + /* Confirm an incoming connection. * Also disables the auto kill timeout on incomming connections. * diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index d595c67d..43db2a9d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1232,7 +1232,7 @@ void doInbound(Messenger *m) if (friend_id != -1) { if (m_get_friend_connectionstatus(m, friend_id) == 1) { - crypto_kill(m->net_crypto, inconnection); + kill_connection(m->net_crypto->lossless_udp, inconnection); return; } @@ -1242,8 +1242,6 @@ void doInbound(Messenger *m) set_friend_status(m, friend_id, FRIEND_CONFIRMED); } - - crypto_kill(m->net_crypto, inconnection); } } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 41c8c45c..2da2ba3b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -503,27 +503,28 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) */ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) { - uint32_t i; + uint32_t i, j; + + while (1) { + int incoming_con = incoming_connection(c->lossless_udp, 1); - for (i = 0; i < MAX_INCOMING; ++i) { - if (c->incoming_connections[i] != -1) { - if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4 - || is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) { - kill_connection(c->lossless_udp, c->incoming_connections[i]); - c->incoming_connections[i] = -1; + if (incoming_con != -1) { + if (is_connected(c->lossless_udp, incoming_con) == 4 + || is_connected(c->lossless_udp, incoming_con) == 0) { + kill_connection(c->lossless_udp, incoming_con); continue; } - if (id_packet(c->lossless_udp, c->incoming_connections[i]) == 2) { + if (id_packet(c->lossless_udp, incoming_con) == 2) { uint8_t temp_data[MAX_DATA_SIZE]; - uint16_t len = read_packet_silent(c->lossless_udp, c->incoming_connections[i], temp_data); + uint16_t len = read_packet_silent(c->lossless_udp, incoming_con, temp_data); if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { - int connection_id = c->incoming_connections[i]; - c->incoming_connections[i] = -1; /* Remove this connection from the incoming connection list. */ - return connection_id; + return incoming_con; } } + } else { + break; } } @@ -579,6 +580,10 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, * return -1; * } */ + + /* Connection is accepted. */ + confirm_connection(c->lossless_udp, connection_id); + if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 || c->crypto_connections == NULL) return -1; @@ -659,41 +664,6 @@ void load_keys(Net_Crypto *c, uint8_t *keys) memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); } -/* Adds an incoming connection to the incoming_connection list. - * TODO: Optimize this. - * - * returns 0 if successful - * returns 1 if failure. - */ -static int new_incoming(Net_Crypto *c, int id) -{ - uint32_t i; - - for (i = 0; i < MAX_INCOMING; ++i) { - if (c->incoming_connections[i] == -1) { - c->incoming_connections[i] = id; - return 0; - } - } - - return 1; -} - -/* Handle all new incoming connections. - * TODO: Optimize this. - */ -static void handle_incomings(Net_Crypto *c) -{ - int income; - - while (1) { - income = incoming_connection(c->lossless_udp); - - if (income == -1 || new_incoming(c, income) ) - break; - } -} - /* Handle received packets for not yet established crypto connections. */ static void receive_crypto(Net_Crypto *c) { @@ -748,9 +718,6 @@ static void receive_crypto(Net_Crypto *c) c->crypto_connections[i].sessionsecret_key, c->crypto_connections[i].shared_key); c->crypto_connections[i].status = CONN_ESTABLISHED; - - /* Connection is accepted. */ - confirm_connection(c->lossless_udp, c->crypto_connections[i].number); } else { /* This should not happen, kill the connection if it does. */ crypto_kill(c, i); @@ -785,7 +752,6 @@ Net_Crypto *new_net_crypto(Networking_Core *net) return NULL; } - memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); return temp; } @@ -810,7 +776,6 @@ static void kill_timedout(Net_Crypto *c) void do_net_crypto(Net_Crypto *c) { do_lossless_udp(c->lossless_udp); - handle_incomings(c); kill_timedout(c); receive_crypto(c); } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 55c1e3e3..f8eeb424 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -26,8 +26,6 @@ #include "Lossless_UDP.h" -#define MAX_INCOMING 64 - #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ @@ -66,9 +64,6 @@ typedef struct { uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; - /* keeps track of the connection numbers for friends request so we can check later if they were sent. */ - int incoming_connections[MAX_INCOMING]; - Cryptopacket_Handles cryptopackethandlers[256]; } Net_Crypto; -- cgit v1.2.3