summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/net_crypto.c16
-rw-r--r--toxcore/net_crypto.h3
-rw-r--r--toxcore/network.c46
-rw-r--r--toxcore/network.h4
-rw-r--r--toxcore/util.c8
5 files changed, 57 insertions, 20 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 9425afba..d4922380 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -379,7 +379,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat
379 return -1; 379 return -1;
380 380
381 increment_nonce(conn->sent_nonce); 381 increment_nonce(conn->sent_nonce);
382 conn->last_data_packet_sent = current_time(); //TODO remove this. 382 conn->last_data_packet_sent = current_time_monotonic(); //TODO remove this.
383 return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); 383 return send_packet_to(c, crypt_connection_id, packet, sizeof(packet));
384} 384}
385 385
@@ -505,7 +505,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
505 if (send_packet_to(c, crypt_connection_id, conn->temp_packet, conn->temp_packet_length) != 0) 505 if (send_packet_to(c, crypt_connection_id, conn->temp_packet, conn->temp_packet_length) != 0)
506 return -1; 506 return -1;
507 507
508 conn->temp_packet_sent_time = current_time(); 508 conn->temp_packet_sent_time = current_time_monotonic();
509 ++conn->temp_packet_num_sent; 509 ++conn->temp_packet_num_sent;
510 return 0; 510 return 0;
511} 511}
@@ -738,7 +738,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
738 738
739 if (source.ip.family == AF_INET || source.ip.family == AF_INET6) { 739 if (source.ip.family == AF_INET || source.ip.family == AF_INET6) {
740 conn->ip_port = source; 740 conn->ip_port = source;
741 conn->direct_lastrecv_time = current_time(); 741 conn->direct_lastrecv_time = current_time_monotonic();
742 return 0; 742 return 0;
743 } 743 }
744 744
@@ -1032,14 +1032,14 @@ static int udp_handle_packet(void *object, IP_Port source, uint8_t *packet, uint
1032 if (conn == 0) 1032 if (conn == 0)
1033 return -1; 1033 return -1;
1034 1034
1035 conn->direct_lastrecv_time = current_time(); 1035 conn->direct_lastrecv_time = current_time_monotonic();
1036 return 0; 1036 return 0;
1037} 1037}
1038 1038
1039static void send_crypto_packets(Net_Crypto *c) 1039static void send_crypto_packets(Net_Crypto *c)
1040{ 1040{
1041 uint32_t i; 1041 uint32_t i;
1042 uint64_t temp_time = current_time(); 1042 uint64_t temp_time = current_time_monotonic();
1043 1043
1044 for (i = 0; i < c->crypto_connections_length; ++i) { 1044 for (i = 0; i < c->crypto_connections_length; ++i) {
1045 Crypto_Connection *conn = get_crypto_connection(c, i); 1045 Crypto_Connection *conn = get_crypto_connection(c, i);
@@ -1047,12 +1047,12 @@ static void send_crypto_packets(Net_Crypto *c)
1047 if (conn == 0) 1047 if (conn == 0)
1048 return; 1048 return;
1049 1049
1050 if ((CRYPTO_SEND_PACKET_INTERVAL * 1000ULL) + conn->temp_packet_sent_time < temp_time) { 1050 if (CRYPTO_SEND_PACKET_INTERVAL + conn->temp_packet_sent_time < temp_time) {
1051 send_temp_packet(c, i); 1051 send_temp_packet(c, i);
1052 } 1052 }
1053 1053
1054 if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED 1054 if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED
1055 && (500ULL * 1000ULL) + conn->last_data_packet_sent < temp_time) {//TODO remove this. 1055 && (500ULL + conn->last_data_packet_sent) < temp_time) {//TODO remove this.
1056 uint8_t data[4] = {}; 1056 uint8_t data[4] = {};
1057 send_data_packet(c, i, data, 4); 1057 send_data_packet(c, i, data, 4);
1058 } 1058 }
@@ -1159,7 +1159,7 @@ Net_Crypto *new_net_crypto(DHT *dht)
1159static void kill_timedout(Net_Crypto *c) 1159static void kill_timedout(Net_Crypto *c)
1160{ 1160{
1161 uint32_t i; 1161 uint32_t i;
1162 uint64_t temp_time = current_time(); 1162 //uint64_t temp_time = current_time_monotonic();
1163 1163
1164 for (i = 0; i < c->crypto_connections_length; ++i) { 1164 for (i = 0; i < c->crypto_connections_length; ++i) {
1165 Crypto_Connection *conn = get_crypto_connection(c, i); 1165 Crypto_Connection *conn = get_crypto_connection(c, i);
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 5d4ff35b..0389f283 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -72,7 +72,8 @@ typedef struct {
72 uint32_t temp_packet_num_sent; 72 uint32_t temp_packet_num_sent;
73 73
74 IP_Port ip_port; /* The ip and port to contact this guy directly.*/ 74 IP_Port ip_port; /* The ip and port to contact this guy directly.*/
75 uint64_t direct_lastrecv_time; /* The Time at which we last receive a direct packet. */ 75 uint64_t direct_lastrecv_time; /* The Time at which we last received a direct packet in ms. */
76
76 77
77 int (*connection_status_callback)(void *object, int id, uint8_t status); 78 int (*connection_status_callback)(void *object, int id, uint8_t status);
78 void *connection_status_callback_object; 79 void *connection_status_callback_object;
diff --git a/toxcore/network.c b/toxcore/network.c
index 28925180..69874189 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -183,8 +183,9 @@ int set_socket_dualstack(sock_t sock)
183 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0); 183 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0);
184} 184}
185 185
186
186/* return current UNIX time in microseconds (us). */ 187/* return current UNIX time in microseconds (us). */
187uint64_t current_time(void) 188static uint64_t current_time_actual(void)
188{ 189{
189 uint64_t time; 190 uint64_t time;
190#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 191#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@@ -205,6 +206,37 @@ uint64_t current_time(void)
205} 206}
206 207
207 208
209#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
210static uint64_t last_monotime;
211static uint64_t add_monotime;
212#endif
213
214/* return current monotonic time in milliseconds (ms). */
215uint64_t current_time_monotonic(void)
216{
217 uint64_t time;
218#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
219 time = (uint64_t)GetTickCount() + add_monotime;
220
221 if (time < last_monotime) { /* Prevent time from ever decreasing because of 32 bit wrap. */
222 uint32_t add = ~0;
223 add_monotime += add;
224 time += add;
225 }
226
227 last_monotime = time;
228#else
229 struct timespec monotime;
230#if defined(__linux__)
231 clock_gettime(CLOCK_MONOTONIC_RAW, &monotime);
232#else
233 clock_gettime(CLOCK_MONOTONIC, &monotime);
234#endif
235 time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL);
236#endif
237 return time;
238}
239
208#ifdef LOGGING 240#ifdef LOGGING
209static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); 241static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res);
210#endif 242#endif
@@ -273,7 +305,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
273 if ((res >= 0) && ((uint32_t)res == length)) 305 if ((res >= 0) && ((uint32_t)res == length))
274 net->send_fail_eagain = 0; 306 net->send_fail_eagain = 0;
275 else if ((res < 0) && (errno == EWOULDBLOCK)) 307 else if ((res < 0) && (errno == EWOULDBLOCK))
276 net->send_fail_eagain = current_time(); 308 net->send_fail_eagain = current_time_monotonic();
277 309
278 return res; 310 return res;
279} 311}
@@ -421,13 +453,13 @@ int networking_wait_execute(uint8_t *data, long seconds, long microseconds)
421 * that code) 453 * that code)
422 */ 454 */
423 if (s->send_fail_eagain != 0) { 455 if (s->send_fail_eagain != 0) {
424 // current_time(): microseconds 456 // current_time(): milliseconds
425 uint64_t now = current_time(); 457 uint64_t now = current_time_monotonic();
426 458
427 /* s->sendqueue_length: might be used to guess how long we keep checking */ 459 /* s->sendqueue_length: might be used to guess how long we keep checking */
428 /* for now, threshold is hardcoded to 250ms, too long for a really really 460 /* for now, threshold is hardcoded to 250ms, too long for a really really
429 * fast link, but too short for a sloooooow link... */ 461 * fast link, but too short for a sloooooow link... */
430 if (now - s->send_fail_eagain < 250000) { 462 if (now - s->send_fail_eagain < 250) {
431 writefds_add = 1; 463 writefds_add = 1;
432 } 464 }
433 } 465 }
@@ -522,9 +554,9 @@ int networking_at_startup(void)
522 return -1; 554 return -1;
523 555
524#else 556#else
525 srandom((uint32_t)current_time()); 557 srandom((uint32_t)current_time_actual());
526#endif 558#endif
527 srand((uint32_t)current_time()); 559 srand((uint32_t)current_time_actual());
528 at_startup_ran = 1; 560 at_startup_ran = 1;
529 return 0; 561 return 0;
530} 562}
diff --git a/toxcore/network.h b/toxcore/network.h
index 4a893cff..e268df74 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -322,8 +322,8 @@ int set_socket_nosigpipe(sock_t sock);
322 */ 322 */
323int set_socket_dualstack(sock_t sock); 323int set_socket_dualstack(sock_t sock);
324 324
325/* return current UNIX time in microseconds (us). */ 325/* return current monotonic time in milliseconds (ms). */
326uint64_t current_time(void); 326uint64_t current_time_monotonic(void);
327 327
328/* Basic network functions: */ 328/* Basic network functions: */
329 329
diff --git a/toxcore/util.c b/toxcore/util.c
index 07db57f0..58f0336c 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -36,10 +36,14 @@
36 36
37/* don't call into system billions of times for no reason */ 37/* don't call into system billions of times for no reason */
38static uint64_t unix_time_value; 38static uint64_t unix_time_value;
39static uint64_t unix_base_time_value;
39 40
40void unix_time_update() 41void unix_time_update()
41{ 42{
42 unix_time_value = (uint64_t)time(NULL); 43 if (unix_base_time_value == 0)
44 unix_base_time_value = ((uint64_t)time(NULL) - (current_time_monotonic() / 1000ULL));
45
46 unix_time_value = (current_time_monotonic() / 1000ULL) + unix_base_time_value;
43} 47}
44 48
45uint64_t unix_time() 49uint64_t unix_time()
@@ -49,7 +53,7 @@ uint64_t unix_time()
49 53
50int is_timeout(uint64_t timestamp, uint64_t timeout) 54int is_timeout(uint64_t timestamp, uint64_t timeout)
51{ 55{
52 return timestamp + timeout <= unix_time_value; 56 return timestamp + timeout <= unix_time();
53} 57}
54 58
55 59