summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c25
-rw-r--r--toxcore/net_crypto.c24
-rw-r--r--toxcore/net_crypto.h10
3 files changed, 32 insertions, 27 deletions
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)
885 885
886 int crypt_id = m->friendlist[friendnumber].crypt_connection_id; 886 int crypt_id = m->friendlist[friendnumber].crypt_connection_id;
887 887
888 if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED) 888 uint8_t direct_connected;
889
890 if (crypto_connection_status(m->net_crypto, crypt_id, &direct_connected) != CRYPTO_CONN_ESTABLISHED)
891 return zero;
892
893 if (direct_connected == 0)
889 return zero; 894 return zero;
890 895
891 return m->net_crypto->crypto_connections[crypt_id].ip_port; 896 return m->net_crypto->crypto_connections[crypt_id].ip_port;
@@ -2201,10 +2206,15 @@ void do_friends(Messenger *m)
2201 set_conection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key); 2206 set_conection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key);
2202 } 2207 }
2203 2208
2204 IP_Port friendip; 2209 uint8_t direct_connected;
2210 unsigned int status = crypto_connection_status(m->net_crypto, m->friendlist[i].crypt_connection_id, &direct_connected);
2205 2211
2206 if (onion_getfriendip(m->onion_c, m->friendlist[i].onion_friendnum, &friendip) == 1) { 2212 if (direct_connected == 0 || status == CRYPTO_CONN_COOKIE_REQUESTING) {
2207 set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, friendip); 2213 IP_Port friendip;
2214
2215 if (onion_getfriendip(m->onion_c, m->friendlist[i].onion_friendnum, &friendip) == 1) {
2216 set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, friendip);
2217 }
2208 } 2218 }
2209 } 2219 }
2210 2220
@@ -2233,13 +2243,6 @@ void do_friends(Messenger *m)
2233 send_ping(m, i); 2243 send_ping(m, i);
2234 } 2244 }
2235 2245
2236 if (is_cryptoconnected(m->net_crypto,
2237 m->friendlist[i].crypt_connection_id) == CRYPTO_CONN_TIMED_OUT) { /* If the connection timed out, kill it. */
2238 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
2239 m->friendlist[i].crypt_connection_id = -1;
2240 set_friend_status(m, i, FRIEND_CONFIRMED);
2241 }
2242
2243 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { 2246 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
2244 /* If we stopped receiving ping packets, kill it. */ 2247 /* If we stopped receiving ping packets, kill it. */
2245 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); 2248 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)
1309 if (create_send_handshake(c, crypt_connection_id, n_c->cookie) != 0) 1309 if (create_send_handshake(c, crypt_connection_id, n_c->cookie) != 0)
1310 return -1; 1310 return -1;
1311 1311
1312 send_temp_packet(c, crypt_connection_id);
1313 conn->status = CRYPTO_CONN_NOT_CONFIRMED; 1312 conn->status = CRYPTO_CONN_NOT_CONFIRMED;
1314 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 1313 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
1315 crypto_connection_add_source(c, crypt_connection_id, n_c->source); 1314 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)
1651 return wipe_crypto_connection(c, crypt_connection_id); 1650 return wipe_crypto_connection(c, crypt_connection_id);
1652} 1651}
1653 1652
1654/* return 0 if no connection. 1653/* return one of CRYPTO_CONN_* values indicating the state of the connection.
1655 * return 1 we have sent a handshake. 1654 *
1656 * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). 1655 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
1657 * return 3 if the connection is established.
1658 * return 4 if the connection is timed out and waiting to be killed.
1659 */ 1656 */
1660int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 1657unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected)
1661{ 1658{
1662 if ((unsigned int)crypt_connection_id < c->crypto_connections_length) 1659 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1663 return c->crypto_connections[crypt_connection_id].status; 1660
1661 if (conn == 0)
1662 return CRYPTO_CONN_NO_CONNECTION;
1663
1664 *direct_connected = 0;
1665
1666 if ((CRYPTO_SEND_PACKET_INTERVAL * MAX_NUM_SENDPACKET_TRIES + conn->direct_lastrecv_time) > current_time_monotonic())
1667 *direct_connected = 1;
1664 1668
1665 return CRYPTO_CONN_NO_CONNECTION; 1669 return conn->status;
1666} 1670}
1667 1671
1668void new_keys(Net_Crypto *c) 1672void 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,
235int crypto_kill(Net_Crypto *c, int crypt_connection_id); 235int crypto_kill(Net_Crypto *c, int crypt_connection_id);
236 236
237 237
238/* return 0 if no connection. 238/* return one of CRYPTO_CONN_* values indicating the state of the connection.
239 * return 1 we have sent a handshake 239 *
240 * return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet). 240 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
241 * return 3 if the connection is established.
242 * return 4 if the connection is timed out and waiting to be killed.
243 */ 241 */
244int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); 242unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected);
245 243
246 244
247/* Generate our public and private keys. 245/* Generate our public and private keys.