diff options
author | Nick Mathewson <nickm@torproject.org> | 2020-02-24 10:04:01 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2020-02-24 10:04:01 -0500 |
commit | b7ba558f56da643857884761f6a52262c7aa51b8 (patch) | |
tree | e6e09605b3c98de2228ea0e49076f399257e717e /src/feature/nodelist | |
parent | e1cf10ceb7b742b5028ff4f5cfbf2b2e67572c06 (diff) | |
download | tor-b7ba558f56da643857884761f6a52262c7aa51b8.tar.gz tor-b7ba558f56da643857884761f6a52262c7aa51b8.zip |
Move one voting schedule fn into networkstatus.c
The 'voting_schdule_get_start_of_next_interval' function isn't
actually dirauth-specific.
Diffstat (limited to 'src/feature/nodelist')
-rw-r--r-- | src/feature/nodelist/networkstatus.c | 44 | ||||
-rw-r--r-- | src/feature/nodelist/networkstatus.h | 3 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/feature/nodelist/networkstatus.c b/src/feature/nodelist/networkstatus.c index d7864fa38a..6755de1a81 100644 --- a/src/feature/nodelist/networkstatus.c +++ b/src/feature/nodelist/networkstatus.c @@ -2765,3 +2765,47 @@ networkstatus_free_all(void) } } } + +/** Return the start of the next interval of size <b>interval</b> (in + * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always + * starts a fresh interval, and if the last interval of a day would be + * truncated to less than half its size, it is rolled into the + * previous interval. */ +time_t +voting_schedule_get_start_of_next_interval(time_t now, int interval, + int offset) +{ + struct tm tm; + time_t midnight_today=0; + time_t midnight_tomorrow; + time_t next; + + tor_gmtime_r(&now, &tm); + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + + if (tor_timegm(&tm, &midnight_today) < 0) { + // LCOV_EXCL_START + log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight."); + // LCOV_EXCL_STOP + } + midnight_tomorrow = midnight_today + (24*60*60); + + next = midnight_today + ((now-midnight_today)/interval + 1)*interval; + + /* Intervals never cross midnight. */ + if (next > midnight_tomorrow) + next = midnight_tomorrow; + + /* If the interval would only last half as long as it's supposed to, then + * skip over to the next day. */ + if (next + interval/2 > midnight_tomorrow) + next = midnight_tomorrow; + + next += offset; + if (next - interval > now) + next -= interval; + + return next; +} diff --git a/src/feature/nodelist/networkstatus.h b/src/feature/nodelist/networkstatus.h index 5e8c8a9e57..c376bdd371 100644 --- a/src/feature/nodelist/networkstatus.h +++ b/src/feature/nodelist/networkstatus.h @@ -153,6 +153,9 @@ void vote_routerstatus_free_(vote_routerstatus_t *rs); void set_routerstatus_from_routerinfo(routerstatus_t *rs, const node_t *node, const routerinfo_t *ri); +time_t voting_schedule_get_start_of_next_interval(time_t now, + int interval, + int offset); #ifdef NETWORKSTATUS_PRIVATE #ifdef TOR_UNIT_TESTS |