diff options
author | George Kadianakis <desnacked@riseup.net> | 2017-02-13 15:31:34 +0200 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2017-04-18 11:03:15 -0400 |
commit | e1a59ade9557cc2a7b0345397148dcfd2e16d35c (patch) | |
tree | 9bc5f37119a4e64a8d8400ea91d62dad2643c818 /src/or/hs_common.c | |
parent | b081a7ed21ae729f6e195715e130edaca3e0b7fe (diff) | |
download | tor-e1a59ade9557cc2a7b0345397148dcfd2e16d35c.tar.gz tor-e1a59ade9557cc2a7b0345397148dcfd2e16d35c.zip |
prop224: Add time period functions and unittests
This will be used by the build blinded key functions.
Signed-off-by: David Goulet <dgoulet@torproject.org>
Diffstat (limited to 'src/or/hs_common.c')
-rw-r--r-- | src/or/hs_common.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/or/hs_common.c b/src/or/hs_common.c index 4af3081502..42508126f8 100644 --- a/src/or/hs_common.c +++ b/src/or/hs_common.c @@ -9,6 +9,8 @@ * protocol. **/ +#define HS_COMMON_PRIVATE + #include "or.h" #include "config.h" @@ -50,6 +52,46 @@ hs_check_service_private_dir(const char *username, const char *path, return 0; } +/** Get the default HS time period length in minutes from the consensus. */ +STATIC uint64_t +get_time_period_length(void) +{ + int32_t time_period_length = networkstatus_get_param(NULL, "hsdir-interval", + HS_TIME_PERIOD_LENGTH_DEFAULT, + HS_TIME_PERIOD_LENGTH_MIN, + HS_TIME_PERIOD_LENGTH_MAX); + /* Make sure it's a positive value. */ + tor_assert(time_period_length >= 0); + /* uint64_t will always be able to contain a int32_t */ + return (uint64_t) time_period_length; +} + +/** Get the HS time period number at time <b>now</b> */ +STATIC uint64_t +get_time_period_num(time_t now) +{ + uint64_t time_period_num; + uint64_t time_period_length = get_time_period_length(); + uint64_t minutes_since_epoch = now / 60; + + /* Now subtract half a day to fit the prop224 time period schedule (see + * section [TIME-PERIODS]). */ + tor_assert(minutes_since_epoch > HS_TIME_PERIOD_ROTATION_OFFSET); + minutes_since_epoch -= HS_TIME_PERIOD_ROTATION_OFFSET; + + /* Calculate the time period */ + time_period_num = minutes_since_epoch / time_period_length; + return time_period_num; +} + +/** Get the number of the _upcoming_ HS time period, given that the current + * time is <b>now</b>. */ +uint64_t +hs_get_next_time_period_num(time_t now) +{ + return get_time_period_num(now) + 1; +} + /* Create a new rend_data_t for a specific given <b>version</b>. * Return a pointer to the newly allocated data structure. */ static rend_data_t * |