summaryrefslogtreecommitdiff
path: root/toxcore/friend_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/friend_connection.c
parent54066f338f185f2fbd6694d9a4877f42cbfa21c8 (diff)
Use per-instance `Mono_Time` for Messenger and onion.
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r--toxcore/friend_connection.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 4d245871..f8b0ccf1 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -74,6 +74,7 @@ typedef struct Friend_Conn {
74 74
75 75
76struct Friend_Connections { 76struct Friend_Connections {
77 const Mono_Time *mono_time;
77 Net_Crypto *net_crypto; 78 Net_Crypto *net_crypto;
78 DHT *dht; 79 DHT *dht;
79 Onion_Client *onion_c; 80 Onion_Client *onion_c;
@@ -298,7 +299,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
298 ++length; 299 ++length;
299 300
300 if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) { 301 if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) {
301 friend_con->share_relays_lastsent = unix_time(); 302 friend_con->share_relays_lastsent = mono_time_get(fr_c->mono_time);
302 return 1; 303 return 1;
303 } 304 }
304 305
@@ -339,7 +340,7 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
339 340
340 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, 1); 341 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, 1);
341 friend_con->dht_ip_port = ip_port; 342 friend_con->dht_ip_port = ip_port;
342 friend_con->dht_ip_port_lastrecv = unix_time(); 343 friend_con->dht_ip_port_lastrecv = mono_time_get(fr_c->mono_time);
343 344
344 if (friend_con->hosting_tcp_relay) { 345 if (friend_con->hosting_tcp_relay) {
345 friend_add_tcp_relay(fr_c, number, ip_port, friend_con->dht_temp_pk); 346 friend_add_tcp_relay(fr_c, number, ip_port, friend_con->dht_temp_pk);
@@ -355,7 +356,7 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint
355 return; 356 return;
356 } 357 }
357 358
358 friend_con->dht_pk_lastrecv = unix_time(); 359 friend_con->dht_pk_lastrecv = mono_time_get(fr_c->mono_time);
359 360
360 if (friend_con->dht_lock) { 361 if (friend_con->dht_lock) {
361 if (dht_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) { 362 if (dht_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) {
@@ -384,13 +385,13 @@ static int handle_status(void *object, int number, uint8_t status, void *userdat
384 if (status) { /* Went online. */ 385 if (status) { /* Went online. */
385 status_changed = 1; 386 status_changed = 1;
386 friend_con->status = FRIENDCONN_STATUS_CONNECTED; 387 friend_con->status = FRIENDCONN_STATUS_CONNECTED;
387 friend_con->ping_lastrecv = unix_time(); 388 friend_con->ping_lastrecv = mono_time_get(fr_c->mono_time);
388 friend_con->share_relays_lastsent = 0; 389 friend_con->share_relays_lastsent = 0;
389 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); 390 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
390 } else { /* Went offline. */ 391 } else { /* Went offline. */
391 if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { 392 if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) {
392 status_changed = 1; 393 status_changed = 1;
393 friend_con->dht_pk_lastrecv = unix_time(); 394 friend_con->dht_pk_lastrecv = mono_time_get(fr_c->mono_time);
394 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); 395 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
395 } 396 }
396 397
@@ -463,7 +464,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
463 } 464 }
464 465
465 if (data[0] == PACKET_ID_ALIVE) { 466 if (data[0] == PACKET_ID_ALIVE) {
466 friend_con->ping_lastrecv = unix_time(); 467 friend_con->ping_lastrecv = mono_time_get(fr_c->mono_time);
467 return 0; 468 return 0;
468 } 469 }
469 470
@@ -558,7 +559,7 @@ static int handle_new_connections(void *object, New_Connection *n_c)
558 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); 559 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0);
559 } else { 560 } else {
560 friend_con->dht_ip_port = n_c->source; 561 friend_con->dht_ip_port = n_c->source;
561 friend_con->dht_ip_port_lastrecv = unix_time(); 562 friend_con->dht_ip_port_lastrecv = mono_time_get(fr_c->mono_time);
562 } 563 }
563 564
564 if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { 565 if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) {
@@ -613,7 +614,7 @@ static int send_ping(const Friend_Connections *fr_c, int friendcon_id)
613 const int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); 614 const int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0);
614 615
615 if (ret != -1) { 616 if (ret != -1) {
616 friend_con->ping_lastsent = unix_time(); 617 friend_con->ping_lastsent = mono_time_get(fr_c->mono_time);
617 return 0; 618 return 0;
618 } 619 }
619 620
@@ -850,7 +851,8 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
850} 851}
851 852
852/* Create new friend_connections instance. */ 853/* Create new friend_connections instance. */
853Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled) 854Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Client *onion_c,
855 bool local_discovery_enabled)
854{ 856{
855 if (onion_c == nullptr) { 857 if (onion_c == nullptr) {
856 return nullptr; 858 return nullptr;
@@ -862,6 +864,7 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
862 return nullptr; 864 return nullptr;
863 } 865 }
864 866
867 temp->mono_time = mono_time;
865 temp->dht = onion_get_dht(onion_c); 868 temp->dht = onion_get_dht(onion_c);
866 temp->net_crypto = onion_get_net_crypto(onion_c); 869 temp->net_crypto = onion_get_net_crypto(onion_c);
867 temp->onion_c = onion_c; 870 temp->onion_c = onion_c;
@@ -881,7 +884,7 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
881/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 884/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
882static void lan_discovery(Friend_Connections *fr_c) 885static void lan_discovery(Friend_Connections *fr_c)
883{ 886{
884 if (fr_c->last_lan_discovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 887 if (fr_c->last_lan_discovery + LAN_DISCOVERY_INTERVAL < mono_time_get(fr_c->mono_time)) {
885 const uint16_t first = fr_c->next_lan_port; 888 const uint16_t first = fr_c->next_lan_port;
886 uint16_t last = first + PORTS_PER_DISCOVERY; 889 uint16_t last = first + PORTS_PER_DISCOVERY;
887 last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; 890 last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last;
@@ -896,14 +899,14 @@ static void lan_discovery(Friend_Connections *fr_c)
896 899
897 // Don't include default port in port range 900 // Don't include default port in port range
898 fr_c->next_lan_port = last != TOX_PORTRANGE_TO ? last : TOX_PORTRANGE_FROM + 1; 901 fr_c->next_lan_port = last != TOX_PORTRANGE_TO ? last : TOX_PORTRANGE_FROM + 1;
899 fr_c->last_lan_discovery = unix_time(); 902 fr_c->last_lan_discovery = mono_time_get(fr_c->mono_time);
900 } 903 }
901} 904}
902 905
903/* main friend_connections loop. */ 906/* main friend_connections loop. */
904void do_friend_connections(Friend_Connections *fr_c, void *userdata) 907void do_friend_connections(Friend_Connections *fr_c, void *userdata)
905{ 908{
906 const uint64_t temp_time = unix_time(); 909 const uint64_t temp_time = mono_time_get(fr_c->mono_time);
907 910
908 for (uint32_t i = 0; i < fr_c->num_cons; ++i) { 911 for (uint32_t i = 0; i < fr_c->num_cons; ++i) {
909 Friend_Conn *const friend_con = get_conn(fr_c, i); 912 Friend_Conn *const friend_con = get_conn(fr_c, i);