diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index ced1e845..267670f9 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -885,7 +885,12 @@ static IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber) int crypt_id = m->friendlist[friendnumber].crypt_connection_id; - if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED) + uint8_t direct_connected; + + if (crypto_connection_status(m->net_crypto, crypt_id, &direct_connected) != CRYPTO_CONN_ESTABLISHED) + return zero; + + if (direct_connected == 0) return zero; return m->net_crypto->crypto_connections[crypt_id].ip_port; @@ -2201,10 +2206,15 @@ void do_friends(Messenger *m) set_conection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key); } - IP_Port friendip; + uint8_t direct_connected; + unsigned int status = crypto_connection_status(m->net_crypto, m->friendlist[i].crypt_connection_id, &direct_connected); - if (onion_getfriendip(m->onion_c, m->friendlist[i].onion_friendnum, &friendip) == 1) { - set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, friendip); + if (direct_connected == 0 || status == CRYPTO_CONN_COOKIE_REQUESTING) { + IP_Port friendip; + + if (onion_getfriendip(m->onion_c, m->friendlist[i].onion_friendnum, &friendip) == 1) { + set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, friendip); + } } } @@ -2233,13 +2243,6 @@ void do_friends(Messenger *m) send_ping(m, i); } - if (is_cryptoconnected(m->net_crypto, - m->friendlist[i].crypt_connection_id) == CRYPTO_CONN_TIMED_OUT) { /* If the connection timed out, kill it. */ - crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); - m->friendlist[i].crypt_connection_id = -1; - set_friend_status(m, i, FRIEND_CONFIRMED); - } - if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { /* If we stopped receiving ping packets, kill it. */ crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index a5149a7c..685ce653 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1309,7 +1309,6 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c) if (create_send_handshake(c, crypt_connection_id, n_c->cookie) != 0) return -1; - send_temp_packet(c, crypt_connection_id); conn->status = CRYPTO_CONN_NOT_CONFIRMED; conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; crypto_connection_add_source(c, crypt_connection_id, n_c->source); @@ -1651,18 +1650,23 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) return wipe_crypto_connection(c, crypt_connection_id); } -/* return 0 if no connection. - * return 1 we have sent a handshake. - * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). - * return 3 if the connection is established. - * return 4 if the connection is timed out and waiting to be killed. +/* return one of CRYPTO_CONN_* values indicating the state of the connection. + * + * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. */ -int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) +unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected) { - if ((unsigned int)crypt_connection_id < c->crypto_connections_length) - return c->crypto_connections[crypt_connection_id].status; + Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - return CRYPTO_CONN_NO_CONNECTION; + if (conn == 0) + return CRYPTO_CONN_NO_CONNECTION; + + *direct_connected = 0; + + if ((CRYPTO_SEND_PACKET_INTERVAL * MAX_NUM_SENDPACKET_TRIES + conn->direct_lastrecv_time) > current_time_monotonic()) + *direct_connected = 1; + + return conn->status; } void new_keys(Net_Crypto *c) diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 7d616be6..a010c4d2 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -235,13 +235,11 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, int crypto_kill(Net_Crypto *c, int crypt_connection_id); -/* return 0 if no connection. - * return 1 we have sent a handshake - * return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet). - * return 3 if the connection is established. - * return 4 if the connection is timed out and waiting to be killed. +/* return one of CRYPTO_CONN_* values indicating the state of the connection. + * + * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. */ -int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); +unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected); /* Generate our public and private keys.