commit 926fc93be5b6afab1a604ecd7c79aa6e8ae8a676 Author: George Kadianakis desnacked@riseup.net Date: Fri Dec 7 16:35:23 2018 +0200
Concentrate all TOR_USEC_PER_SEC definitions in a single header file.
Co-authored-by: Mike Perry mikeperry-git@torproject.org --- src/core/or/circuitpadding.c | 5 +---- src/feature/hibernate/hibernate.c | 3 +-- src/lib/defs/include.am | 1 + src/lib/defs/time.h | 23 +++++++++++++++++++++++ src/lib/time/.may_include | 1 + src/lib/time/tvdiff.c | 3 +-- src/test/test_circuitpadding.c | 39 ++++++++++++++++++--------------------- src/test/test_util.c | 2 +- 8 files changed, 47 insertions(+), 30 deletions(-)
diff --git a/src/core/or/circuitpadding.c b/src/core/or/circuitpadding.c index 5210265ff..9f8713f62 100644 --- a/src/core/or/circuitpadding.c +++ b/src/core/or/circuitpadding.c @@ -17,6 +17,7 @@ #include "core/or/channel.h"
#include "lib/time/compat_time.h" +#include "lib/defs/time.h" #include "lib/crypt_ops/crypto_rand.h"
#include "core/or/crypt_path_st.h" @@ -31,10 +32,6 @@
#include "app/config/config.h"
-/* XXX: This is a dup of the constant in ./src/lib/time/tvdiff.c. - * Should/Do we have a header for time constants like this? */ -#define TOR_USEC_PER_SEC (1000000) - static inline circpad_purpose_mask_t circpad_circ_purpose_to_mask(uint8_t circ_purpose); static inline circpad_circuit_state_t circpad_circuit_state( diff --git a/src/feature/hibernate/hibernate.c b/src/feature/hibernate/hibernate.c index feeb3d92e..f10a45f4a 100644 --- a/src/feature/hibernate/hibernate.c +++ b/src/feature/hibernate/hibernate.c @@ -37,6 +37,7 @@ hibernating, phase 2: #include "core/or/connection_or.h" #include "feature/control/control.h" #include "lib/crypt_ops/crypto_rand.h" +#include "lib/defs/time.h" #include "feature/hibernate/hibernate.h" #include "core/mainloop/mainloop.h" #include "feature/relay/router.h" @@ -832,8 +833,6 @@ hibernate_soft_limit_reached(void) return get_accounting_bytes() >= soft_limit; }
-#define TOR_USEC_PER_SEC (1000000) - /** Called when we get a SIGINT, or when bandwidth soft limit is * reached. Puts us into "loose hibernation": we don't accept new * connections, but we continue handling old ones. */ diff --git a/src/lib/defs/include.am b/src/lib/defs/include.am index 48ee7f29f..6a7f9114e 100644 --- a/src/lib/defs/include.am +++ b/src/lib/defs/include.am @@ -2,4 +2,5 @@ noinst_HEADERS += \ src/lib/defs/dh_sizes.h \ src/lib/defs/digest_sizes.h \ + src/lib/defs/time.h \ src/lib/defs/x25519_sizes.h diff --git a/src/lib/defs/time.h b/src/lib/defs/time.h new file mode 100644 index 000000000..762b23fea --- /dev/null +++ b/src/lib/defs/time.h @@ -0,0 +1,23 @@ +/* Copyright (c) 2001, Matej Pfajfar. + * Copyright (c) 2001-2004, Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#ifndef TOR_TIME_DEFS_H +#define TOR_TIME_DEFS_H + +/** + * \file time.h + * + * \brief Definitions for timing-related constants. + **/ + +/** How many microseconds per second */ +#define TOR_USEC_PER_SEC (1000000) +/** How many nanoseconds per microsecond */ +#define TOR_NSEC_PER_USEC (1000) +/* How many nanoseconds per millisecond */ +#define TOR_NSEC_PER_MSEC (1000*1000) + +#endif diff --git a/src/lib/time/.may_include b/src/lib/time/.may_include index 40a18805a..ae01431b6 100644 --- a/src/lib/time/.may_include +++ b/src/lib/time/.may_include @@ -7,6 +7,7 @@ lib/log/*.h lib/subsys/*.h lib/time/*.h lib/wallclock/*.h +lib/defs/time.h
# For load_windows_system_lib. lib/fs/winlib.h \ No newline at end of file diff --git a/src/lib/time/tvdiff.c b/src/lib/time/tvdiff.c index bc8a1166e..9dfb63c26 100644 --- a/src/lib/time/tvdiff.c +++ b/src/lib/time/tvdiff.c @@ -11,6 +11,7 @@ #include "lib/time/tvdiff.h"
#include "lib/cc/compat_compiler.h" +#include "lib/defs/time.h" #include "lib/log/log.h"
#ifdef _WIN32 @@ -20,8 +21,6 @@ #include <sys/time.h> #endif
-#define TOR_USEC_PER_SEC 1000000 - /** Return the difference between start->tv_sec and end->tv_sec. * Returns INT64_MAX on overflow and underflow. */ diff --git a/src/test/test_circuitpadding.c b/src/test/test_circuitpadding.c index 78f93f7b2..5693c98e4 100644 --- a/src/test/test_circuitpadding.c +++ b/src/test/test_circuitpadding.c @@ -12,6 +12,7 @@ #include <event.h> #include "lib/evloop/compat_libevent.h" #include "lib/time/compat_time.h" +#include "lib/defs/time.h" #include "core/or/relay.h" #include "core/or/circuitlist.h" #include "core/or/circuitbuild.h" @@ -32,10 +33,6 @@
extern smartlist_t *connection_array;
-#define USEC_PER_SEC (1000000) -#define NSEC_PER_USEC (1000) -#define NSEC_PER_MSEC (1000*1000) - circid_t get_unique_circ_id_by_chan(channel_t *chan); void helper_create_basic_machine(void); static void helper_create_conditional_machines(void); @@ -69,7 +66,7 @@ static circpad_machine_t circ_client_machine; static void timers_advance_and_run(int64_t msec_update) { - curr_mocked_time += msec_update*NSEC_PER_MSEC; + curr_mocked_time += msec_update*TOR_NSEC_PER_MSEC; monotime_coarse_set_mock_time_nsec(curr_mocked_time); monotime_set_mock_time_nsec(curr_mocked_time); timers_run_pending(); @@ -304,9 +301,9 @@ test_circuitpadding_rtt(void *arg)
monotime_init(); monotime_enable_test_mocking(); - monotime_set_mock_time_nsec(1*NSEC_PER_USEC); - monotime_coarse_set_mock_time_nsec(1*NSEC_PER_USEC); - curr_mocked_time = 1*NSEC_PER_USEC; + monotime_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + monotime_coarse_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + curr_mocked_time = 1*TOR_NSEC_PER_USEC;
timers_initialize(); circpad_machines_init(); @@ -994,9 +991,9 @@ test_circuitpadding_tokens(void *arg)
monotime_init(); monotime_enable_test_mocking(); - monotime_set_mock_time_nsec(1*NSEC_PER_USEC); - monotime_coarse_set_mock_time_nsec(1*NSEC_PER_USEC); - curr_mocked_time = 1*NSEC_PER_USEC; + monotime_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + monotime_coarse_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + curr_mocked_time = 1*TOR_NSEC_PER_USEC;
timers_initialize();
@@ -1441,9 +1438,9 @@ test_circuitpadding_negotiation(void *arg)
monotime_init(); monotime_enable_test_mocking(); - monotime_set_mock_time_nsec(1*NSEC_PER_USEC); - monotime_coarse_set_mock_time_nsec(1*NSEC_PER_USEC); - curr_mocked_time = 1*NSEC_PER_USEC; + monotime_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + monotime_coarse_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + curr_mocked_time = 1*TOR_NSEC_PER_USEC;
timers_initialize(); circpad_machines_init(); @@ -1562,7 +1559,7 @@ simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay, circpad_cell_event_nonpadding_received((circuit_t*)mid_relay);
// Advance time a tiny bit so we can calculate an RTT - curr_mocked_time += 10 * NSEC_PER_MSEC; + curr_mocked_time += 10 * TOR_NSEC_PER_MSEC; monotime_coarse_set_mock_time_nsec(curr_mocked_time); monotime_set_mock_time_nsec(curr_mocked_time);
@@ -1715,9 +1712,9 @@ test_circuitpadding_conditions(void *arg)
monotime_init(); monotime_enable_test_mocking(); - monotime_set_mock_time_nsec(1*NSEC_PER_USEC); - monotime_coarse_set_mock_time_nsec(1*NSEC_PER_USEC); - curr_mocked_time = 1*NSEC_PER_USEC; + monotime_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + monotime_coarse_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + curr_mocked_time = 1*TOR_NSEC_PER_USEC;
timers_initialize(); helper_create_conditional_machines(); @@ -1835,9 +1832,9 @@ test_circuitpadding_circuitsetup_machine(void *arg)
monotime_init(); monotime_enable_test_mocking(); - monotime_set_mock_time_nsec(1*NSEC_PER_USEC); - monotime_coarse_set_mock_time_nsec(1*NSEC_PER_USEC); - curr_mocked_time = 1*NSEC_PER_USEC; + monotime_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + monotime_coarse_set_mock_time_nsec(1*TOR_NSEC_PER_USEC); + curr_mocked_time = 1*TOR_NSEC_PER_USEC;
timers_initialize(); circpad_machines_init(); diff --git a/src/test/test_util.c b/src/test/test_util.c index b983cbb0b..bf64cff7e 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -19,6 +19,7 @@ #include "feature/client/transports.h" #include "lib/crypt_ops/crypto_format.h" #include "lib/crypt_ops/crypto_rand.h" +#include "lib/defs/time.h" #include "test/test.h" #include "lib/memarea/memarea.h" #include "lib/process/waitpid.h" @@ -404,7 +405,6 @@ test_util_time(void *arg)
/* Assume tv_usec is an unsigned integer until proven otherwise */ #define TV_USEC_MAX UINT_MAX -#define TOR_USEC_PER_SEC 1000000
/* Overflows in the result type */
tor-commits@lists.torproject.org