summaryrefslogtreecommitdiff
path: root/toxcore/TCP_connection.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-01 23:02:13 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-16 21:01:43 +0000
commitd6d305feeb76735ee4b4e14c6bca737a5482bc19 (patch)
tree99005c635a452245006b3b5de44f1dd80da9f77f /toxcore/TCP_connection.c
parent54066f338f185f2fbd6694d9a4877f42cbfa21c8 (diff)
Use per-instance `Mono_Time` for Messenger and onion.
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r--toxcore/TCP_connection.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 6763328d..0c388a39 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -36,6 +36,7 @@
36 36
37 37
38struct TCP_Connections { 38struct TCP_Connections {
39 Mono_Time *mono_time;
39 DHT *dht; 40 DHT *dht;
40 41
41 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 42 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -794,8 +795,8 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
794 uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE]; 795 uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
795 memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); 796 memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
796 kill_TCP_connection(tcp_con->connection); 797 kill_TCP_connection(tcp_con->connection);
797 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, 798 tcp_con->connection = new_TCP_connection(tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key,
798 &tcp_c->proxy_info); 799 tcp_c->self_secret_key, &tcp_c->proxy_info);
799 800
800 if (!tcp_con->connection) { 801 if (!tcp_con->connection) {
801 kill_tcp_relay_connection(tcp_c, tcp_connections_number); 802 kill_tcp_relay_connection(tcp_c, tcp_connections_number);
@@ -884,7 +885,7 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti
884 return -1; 885 return -1;
885 } 886 }
886 887
887 tcp_con->connection = new_TCP_connection(tcp_con->ip_port, tcp_con->relay_pk, tcp_c->self_public_key, 888 tcp_con->connection = new_TCP_connection(tcp_c->mono_time, tcp_con->ip_port, tcp_con->relay_pk, tcp_c->self_public_key,
888 tcp_c->self_secret_key, &tcp_c->proxy_info); 889 tcp_c->self_secret_key, &tcp_c->proxy_info);
889 890
890 if (!tcp_con->connection) { 891 if (!tcp_con->connection) {
@@ -1122,7 +1123,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe
1122 1123
1123 /* If this connection isn't used by any connection, we don't need to wait for them to come online. */ 1124 /* If this connection isn't used by any connection, we don't need to wait for them to come online. */
1124 if (sent) { 1125 if (sent) {
1125 tcp_con->connected_time = unix_time(); 1126 tcp_con->connected_time = mono_time_get(tcp_c->mono_time);
1126 } else { 1127 } else {
1127 tcp_con->connected_time = 0; 1128 tcp_con->connected_time = 0;
1128 } 1129 }
@@ -1155,8 +1156,8 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const
1155 1156
1156 TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; 1157 TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number];
1157 1158
1158 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, 1159 tcp_con->connection = new_TCP_connection(tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key,
1159 &tcp_c->proxy_info); 1160 tcp_c->self_secret_key, &tcp_c->proxy_info);
1160 1161
1161 if (!tcp_con->connection) { 1162 if (!tcp_con->connection) {
1162 return -1; 1163 return -1;
@@ -1216,7 +1217,7 @@ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_numb
1216 1217
1217 if (tcp_con->status == TCP_CONN_CONNECTED) { 1218 if (tcp_con->status == TCP_CONN_CONNECTED) {
1218 if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) { 1219 if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) {
1219 tcp_con->connected_time = unix_time(); 1220 tcp_con->connected_time = mono_time_get(tcp_c->mono_time);
1220 } 1221 }
1221 } 1222 }
1222 1223
@@ -1391,7 +1392,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
1391 * 1392 *
1392 * Returns NULL on failure. 1393 * Returns NULL on failure.
1393 */ 1394 */
1394TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info) 1395TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret_key, TCP_Proxy_Info *proxy_info)
1395{ 1396{
1396 if (secret_key == nullptr) { 1397 if (secret_key == nullptr) {
1397 return nullptr; 1398 return nullptr;
@@ -1403,6 +1404,8 @@ TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *
1403 return nullptr; 1404 return nullptr;
1404 } 1405 }
1405 1406
1407 temp->mono_time = mono_time;
1408
1406 memcpy(temp->self_secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); 1409 memcpy(temp->self_secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE);
1407 crypto_derive_public_key(temp->self_public_key, temp->self_secret_key); 1410 crypto_derive_public_key(temp->self_public_key, temp->self_secret_key);
1408 temp->proxy_info = *proxy_info; 1411 temp->proxy_info = *proxy_info;
@@ -1419,7 +1422,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
1419 1422
1420 if (tcp_con) { 1423 if (tcp_con) {
1421 if (tcp_con->status != TCP_CONN_SLEEPING) { 1424 if (tcp_con->status != TCP_CONN_SLEEPING) {
1422 do_TCP_connection(tcp_con->connection, userdata); 1425 do_TCP_connection(tcp_c->mono_time, tcp_con->connection, userdata);
1423 1426
1424 /* callbacks can change TCP connection address. */ 1427 /* callbacks can change TCP connection address. */
1425 tcp_con = get_tcp_connection(tcp_c, i); 1428 tcp_con = get_tcp_connection(tcp_c, i);
@@ -1443,7 +1446,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
1443 1446
1444 if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion && tcp_con->lock_count 1447 if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion && tcp_con->lock_count
1445 && tcp_con->lock_count == tcp_con->sleep_count 1448 && tcp_con->lock_count == tcp_con->sleep_count
1446 && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { 1449 && mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) {
1447 sleep_tcp_relay_connection(tcp_c, i); 1450 sleep_tcp_relay_connection(tcp_c, i);
1448 } 1451 }
1449 } 1452 }
@@ -1471,7 +1474,8 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1471 1474
1472 if (tcp_con) { 1475 if (tcp_con) {
1473 if (tcp_con->status == TCP_CONN_CONNECTED) { 1476 if (tcp_con->status == TCP_CONN_CONNECTED) {
1474 if (!tcp_con->onion && !tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { 1477 if (!tcp_con->onion && !tcp_con->lock_count
1478 && mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) {
1475 to_kill[num_kill] = i; 1479 to_kill[num_kill] = i;
1476 ++num_kill; 1480 ++num_kill;
1477 } 1481 }