summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Messenger.c19
-rw-r--r--toxcore/Messenger.h3
-rw-r--r--toxcore/TCP_connection.c13
-rw-r--r--toxcore/TCP_connection.h5
-rw-r--r--toxcore/net_crypto.c30
-rw-r--r--toxcore/net_crypto.h8
6 files changed, 59 insertions, 19 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index efb66961..fd17ab98 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -433,14 +433,19 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
433 return -1; 433 return -1;
434 434
435 if (m->friendlist[friendnumber].status == FRIEND_ONLINE) { 435 if (m->friendlist[friendnumber].status == FRIEND_ONLINE) {
436 uint8_t direct_connected = 0; 436 _Bool direct_connected = 0;
437 unsigned int num_online_relays = 0;
437 crypto_connection_status(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, 438 crypto_connection_status(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
438 m->friendlist[friendnumber].friendcon_id), &direct_connected); 439 m->friendlist[friendnumber].friendcon_id), &direct_connected, &num_online_relays);
439 440
440 if (direct_connected) { 441 if (direct_connected) {
441 return CONNECTION_UDP; 442 return CONNECTION_UDP;
442 } else { 443 } else {
443 return CONNECTION_TCP; 444 if (num_online_relays) {
445 return CONNECTION_TCP;
446 } else {
447 return CONNECTION_UNKNOWN;
448 }
444 } 449 }
445 } else { 450 } else {
446 return CONNECTION_NONE; 451 return CONNECTION_NONE;
@@ -851,6 +856,14 @@ static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber)
851 if (ret == -1) 856 if (ret == -1)
852 return; 857 return;
853 858
859 if (ret == CONNECTION_UNKNOWN) {
860 if (last_connection_udp_tcp == CONNECTION_UDP) {
861 return;
862 } else {
863 ret = CONNECTION_TCP;
864 }
865 }
866
854 if (last_connection_udp_tcp != ret) { 867 if (last_connection_udp_tcp != ret) {
855 if (m->friend_connectionstatuschange) 868 if (m->friend_connectionstatuschange)
856 m->friend_connectionstatuschange(m, friendnumber, ret, m->friend_connectionstatuschange_userdata); 869 m->friend_connectionstatuschange(m, friendnumber, ret, m->friend_connectionstatuschange_userdata);
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index c9573639..5221e639 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -109,7 +109,8 @@ enum {
109enum { 109enum {
110 CONNECTION_NONE, 110 CONNECTION_NONE,
111 CONNECTION_TCP, 111 CONNECTION_TCP,
112 CONNECTION_UDP 112 CONNECTION_UDP,
113 CONNECTION_UNKNOWN
113}; 114};
114 115
115/* USERSTATUS - 116/* USERSTATUS -
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 7b478611..fe39dc52 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -1124,6 +1124,19 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_
1124 } 1124 }
1125} 1125}
1126 1126
1127/* return number of online tcp relays tied to the connection on success.
1128 * return 0 on failure.
1129 */
1130unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int connections_number)
1131{
1132 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
1133
1134 if (!con_to)
1135 return 0;
1136
1137 return online_tcp_connection_from_conn(con_to);
1138}
1139
1127/* Copy a maximum of max_num TCP relays we are connected to to tcp_relays. 1140/* Copy a maximum of max_num TCP relays we are connected to to tcp_relays.
1128 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. 1141 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
1129 * 1142 *
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h
index 3f7b616d..29fbdee0 100644
--- a/toxcore/TCP_connection.h
+++ b/toxcore/TCP_connection.h
@@ -186,6 +186,11 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number);
186 */ 186 */
187int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number, _Bool status); 187int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number, _Bool status);
188 188
189/* return number of online tcp relays tied to the connection on success.
190 * return 0 on failure.
191 */
192unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int connections_number);
193
189/* Add a TCP relay tied to a connection. 194/* Add a TCP relay tied to a connection.
190 * 195 *
191 * NOTE: This can only be used during the tcp_oob_callback. 196 * NOTE: This can only be used during the tcp_oob_callback.
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index b6b1050a..ab0fb03a 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -404,8 +404,8 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
404 404
405 //TODO: on bad networks, direct connections might not last indefinitely. 405 //TODO: on bad networks, direct connections might not last indefinitely.
406 if (conn->ip_port.ip.family != 0) { 406 if (conn->ip_port.ip.family != 0) {
407 uint8_t direct_connected = 0; 407 _Bool direct_connected = 0;
408 crypto_connection_status(c, crypt_connection_id, &direct_connected); 408 crypto_connection_status(c, crypt_connection_id, &direct_connected, NULL);
409 409
410 if (direct_connected) { 410 if (direct_connected) {
411 if ((uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length) { 411 if ((uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length) {
@@ -1446,7 +1446,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
1446 conn->ip_port = source; 1446 conn->ip_port = source;
1447 } 1447 }
1448 1448
1449 conn->direct_lastrecv_time = current_time_monotonic(); 1449 conn->direct_lastrecv_time = unix_time();
1450 return 0; 1450 return 0;
1451 } else if (source.ip.family == TCP_FAMILY) { 1451 } else if (source.ip.family == TCP_FAMILY) {
1452 if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) 1452 if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0)
@@ -1646,7 +1646,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port)
1646 return -1; 1646 return -1;
1647 1647
1648 if (!ipport_equal(&ip_port, &conn->ip_port)) { 1648 if (!ipport_equal(&ip_port, &conn->ip_port)) {
1649 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > current_time_monotonic()) { 1649 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > unix_time()) {
1650 if (LAN_ip(ip_port.ip) == 0 && LAN_ip(conn->ip_port.ip) == 0 && conn->ip_port.port == ip_port.port) 1650 if (LAN_ip(ip_port.ip) == 0 && LAN_ip(conn->ip_port.ip) == 0 && conn->ip_port.port == ip_port.port)
1651 return -1; 1651 return -1;
1652 } 1652 }
@@ -1810,8 +1810,8 @@ static void do_tcp(Net_Crypto *c)
1810 return; 1810 return;
1811 1811
1812 if (conn->status == CRYPTO_CONN_ESTABLISHED) { 1812 if (conn->status == CRYPTO_CONN_ESTABLISHED) {
1813 uint8_t direct_connected = 0; 1813 _Bool direct_connected = 0;
1814 crypto_connection_status(c, i, &direct_connected); 1814 crypto_connection_status(c, i, &direct_connected, NULL);
1815 1815
1816 if (direct_connected) { 1816 if (direct_connected) {
1817 pthread_mutex_lock(&c->tcp_mutex); 1817 pthread_mutex_lock(&c->tcp_mutex);
@@ -1966,7 +1966,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
1966 return -1; 1966 return -1;
1967 1967
1968 pthread_mutex_lock(&conn->mutex); 1968 pthread_mutex_lock(&conn->mutex);
1969 conn->direct_lastrecv_time = current_time_monotonic(); 1969 conn->direct_lastrecv_time = unix_time();
1970 pthread_mutex_unlock(&conn->mutex); 1970 pthread_mutex_unlock(&conn->mutex);
1971 return 0; 1971 return 0;
1972} 1972}
@@ -2290,18 +2290,26 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
2290/* return one of CRYPTO_CONN_* values indicating the state of the connection. 2290/* return one of CRYPTO_CONN_* values indicating the state of the connection.
2291 * 2291 *
2292 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. 2292 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
2293 * sets online_tcp_relays to the number of connected tcp relays this connection has.
2293 */ 2294 */
2294unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected) 2295unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected,
2296 unsigned int *online_tcp_relays)
2295{ 2297{
2296 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2298 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2297 2299
2298 if (conn == 0) 2300 if (conn == 0)
2299 return CRYPTO_CONN_NO_CONNECTION; 2301 return CRYPTO_CONN_NO_CONNECTION;
2300 2302
2301 *direct_connected = 0; 2303 if (direct_connected) {
2304 *direct_connected = 0;
2302 2305
2303 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > current_time_monotonic()) 2306 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > unix_time())
2304 *direct_connected = 1; 2307 *direct_connected = 1;
2308 }
2309
2310 if (online_tcp_relays) {
2311 *online_tcp_relays = tcp_connection_to_online_tcp_relays(c->tcp_c, conn->connection_number_tcp);
2312 }
2305 2313
2306 return conn->status; 2314 return conn->status;
2307} 2315}
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index f6dd4702..9eb5e2d3 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -60,7 +60,7 @@
60#define MAX_NUM_SENDPACKET_TRIES 8 60#define MAX_NUM_SENDPACKET_TRIES 8
61 61
62/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */ 62/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */
63#define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 2) 63#define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 1000)
64 64
65#define PACKET_ID_PADDING 0 /* Denotes padding */ 65#define PACKET_ID_PADDING 0 /* Denotes padding */
66#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */ 66#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */
@@ -360,13 +360,13 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u
360 */ 360 */
361int crypto_kill(Net_Crypto *c, int crypt_connection_id); 361int crypto_kill(Net_Crypto *c, int crypt_connection_id);
362 362
363
364/* return one of CRYPTO_CONN_* values indicating the state of the connection. 363/* return one of CRYPTO_CONN_* values indicating the state of the connection.
365 * 364 *
366 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. 365 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
366 * sets online_tcp_relays to the number of connected tcp relays this connection has.
367 */ 367 */
368unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected); 368unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected,
369 369 unsigned int *online_tcp_relays);
370 370
371/* Generate our public and private keys. 371/* Generate our public and private keys.
372 * Only call this function the first time the program starts. 372 * Only call this function the first time the program starts.