diff options
-rw-r--r-- | src/or/dirserv.c | 16 | ||||
-rw-r--r-- | src/or/dirvote.c | 83 | ||||
-rw-r--r-- | src/or/or.h | 15 | ||||
-rw-r--r-- | src/or/routerlist.c | 19 |
4 files changed, 127 insertions, 6 deletions
diff --git a/src/or/dirserv.c b/src/or/dirserv.c index 74fae7c346..ae4c932189 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -1752,6 +1752,7 @@ generate_networkstatus_vote_obj(crypto_pk_env_t *private_key, time_t now = time(NULL); time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH; networkstatus_voter_info_t *voter = NULL; + vote_timing_t timing; /* check that everything is deallocated XXXX020 */ @@ -1818,12 +1819,15 @@ generate_networkstatus_vote_obj(crypto_pk_env_t *private_key, tor_assert(v3_out); memset(v3_out, 0, sizeof(networkstatus_vote_t)); v3_out->is_vote = 1; - v3_out->published = time(NULL); - v3_out->valid_after = time(NULL); /* XXXX020 not right. */ - v3_out->fresh_until = time(NULL); /* XXXX020 not right. */ - v3_out->valid_until = time(NULL); /* XXXX020 not right. */ - v3_out->vote_seconds = 600; /* XXXX020 not right. */ - v3_out->dist_seconds = 600; /* XXXX020 not right. */ + dirvote_get_preferred_voting_intervals(&timing); + v3_out->published = now; + v3_out->valid_after = + dirvote_get_start_of_next_interval(now, timing.vote_interval); + v3_out->fresh_until = v3_out->valid_after + timing.vote_interval; + v3_out->valid_until = v3_out->valid_after + + (timing.vote_interval * timing.n_intervals_valid); + v3_out->vote_seconds = timing.vote_delay; + v3_out->dist_seconds = timing.dist_delay; v3_out->client_versions = client_versions; v3_out->server_versions = server_versions; diff --git a/src/or/dirvote.c b/src/or/dirvote.c index 722eaaa23e..5810505ff0 100644 --- a/src/or/dirvote.c +++ b/src/or/dirvote.c @@ -10,8 +10,13 @@ const char dirvote_c_id[] = /** * \file dirvote.c + * \brief Functions to compute directory consensus, and schedule voting. **/ +/* ===== + * Voting and consensus generation + * ===== */ + /** Clear all storage held in <b>ns</b>. */ void networkstatus_vote_free(networkstatus_vote_t *ns) @@ -747,6 +752,10 @@ networkstatus_check_consensus_signature(networkstatus_vote_t *consensus) return 0; } +/* ===== + * Certificate functions + * ===== */ + /** Free storage held in <b>cert</b>. */ void authority_cert_free(authority_cert_t *cert) @@ -783,3 +792,77 @@ authority_cert_dup(authority_cert_t *cert) return out; } +/* ===== + * Vote scheduling + * ===== */ + +/** DOCDOC */ +void +dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out) +{ + tor_assert(timing_out); + + /* XXXX020 make these configurable. */ + timing_out->vote_interval = 3600; + timing_out->n_intervals_valid = 3; + timing_out->vote_delay = 300; + timing_out->dist_delay = 300; +} + +/** DOCDOC */ +time_t +dirvote_get_start_of_next_interval(time_t now, int interval) +{ + struct tm tm; + time_t midnight_today; + time_t midnight_tomorrow; + time_t next; + + tor_gmtime_r(&now, &tm); + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + + midnight_today = tor_timegm(&tm); + midnight_tomorrow = midnight_today + (24*60*60); + + next = midnight_today + ((now-midnight_today)/interval + 1)*interval; + + if (next > midnight_tomorrow) + next = midnight_tomorrow; + + return next; +} + +/** DOCDOC */ +static struct { + time_t voting_starts; + time_t voting_ends; + time_t interval_starts; +} voting_schedule; + +/** DOCDOC */ +void +dirvote_recalculate_timing(time_t now) +{ + int interval, vote_delay, dist_delay; + time_t start; + networkstatus_vote_t *consensus = networkstatus_get_latest_consensus(); + + if (consensus) { + /* XXXX020 sanity-check these somewhere! */ + interval = consensus->fresh_until - consensus->valid_after; + vote_delay = consensus->vote_seconds; + vote_delay = consensus->dist_seconds; + } else { + /* XXXX020 is this correct according the the spec? */ + interval = 3600; + vote_delay = dist_delay = 300; + } + + start = voting_schedule.interval_starts = + dirvote_get_start_of_next_interval(now,interval); + voting_schedule.voting_ends = start - vote_delay; + voting_schedule.voting_starts = start - vote_delay - dist_delay; +} + diff --git a/src/or/or.h b/src/or/or.h index 1e3400ac84..664e31464b 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -2782,6 +2782,17 @@ int networkstatus_check_consensus_signature(networkstatus_vote_t *consensus); void authority_cert_free(authority_cert_t *cert); authority_cert_t *authority_cert_dup(authority_cert_t *cert); +/** DOCDOC */ +typedef struct vote_timing_t { + int vote_interval; + int n_intervals_valid; + int vote_delay; + int dist_delay; +} vote_timing_t; +void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out); +time_t dirvote_get_start_of_next_interval(time_t now, int interval); +void dirvote_recalculate_timing(time_t now); + #ifdef DIRVOTE_PRIVATE int networkstatus_check_voter_signature(networkstatus_vote_t *consensus, networkstatus_voter_info_t *voter, @@ -3349,6 +3360,10 @@ local_routerstatus_t *router_get_combined_status_by_digest(const char *digest); local_routerstatus_t *router_get_combined_status_by_descriptor_digest( const char *digest); +/* for consensuses. */ +networkstatus_vote_t *networkstatus_get_latest_consensus(void); +networkstatus_vote_t *networkstatus_get_live_consensus(time_t now); + //routerstatus_t *routerstatus_get_by_hexdigest(const char *hexdigest); int should_delay_dir_fetches(or_options_t *options); void update_networkstatus_downloads(time_t now); diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 42ba117a7b..7230f03c4d 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -65,6 +65,9 @@ static routerlist_t *routerlist = NULL; * about. This list is kept sorted by published_on. */ static smartlist_t *networkstatus_list = NULL; +/** DOCDOC */ +static networkstatus_vote_t *current_consensus = NULL; + /** Global list of local_routerstatus_t for each router, known or unknown. * Kept sorted by digest. */ static smartlist_t *routerstatus_list = NULL; @@ -3726,6 +3729,22 @@ networkstatus_get_by_digest(const char *digest) return NULL; } +/** DOCDOC */ +networkstatus_vote_t * +networkstatus_get_latest_consensus(void) +{ + return current_consensus; +} + +/** DOCDOC */ +networkstatus_vote_t * +networkstatus_get_live_consensus(time_t now) +{ + /* XXXX020 check for liveness */ + (void)now; + return current_consensus; +} + /** We believe networkstatuses more recent than this when they tell us that * our server is broken, invalid, obsolete, etc. */ #define SELF_OPINION_INTERVAL (90*60) |