r17337@aud-055: nickm | 2008-07-24 10:17:43 +0200

Refactor the is_vote field of networkstatus_t to add a third possibility ("opinion") in addition to vote and opinion.  First part of implementing proposal 147.


svn:r16166
This commit is contained in:
Nick Mathewson 2008-07-24 09:22:27 +00:00
parent ea95ce25b6
commit dff1ef7d06
6 changed files with 74 additions and 51 deletions

View File

@ -2230,7 +2230,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_env_t *private_key,
v3_out = tor_malloc_zero(sizeof(networkstatus_t));
v3_out->is_vote = 1;
v3_out->type = NS_TYPE_VOTE;
dirvote_get_preferred_voting_intervals(&timing);
v3_out->published = now;
{

View File

@ -50,6 +50,7 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
networkstatus_voter_info_t *voter;
tor_assert(private_signing_key);
tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
voter = smartlist_get(v3_ns->voters, 0);
@ -104,7 +105,7 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
tor_assert(cert);
tor_snprintf(status, len,
"network-status-version 3\n"
"vote-status vote\n"
"vote-status %s\n"
"consensus-methods 1 2 3 4\n"
"published %s\n"
"valid-after %s\n"
@ -115,6 +116,7 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
"known-flags %s\n"
"dir-source %s %s %s %s %d %d\n"
"contact %s\n",
v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
published, va, fu, vu,
v3_ns->vote_seconds, v3_ns->dist_seconds,
version_lines,
@ -182,9 +184,11 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
{
networkstatus_t *v;
if (!(v = networkstatus_parse_vote_from_string(status, NULL, 1))) {
log_err(LD_BUG,"Generated a networkstatus vote we couldn't parse: "
"<<%s>>", status);
if (!(v = networkstatus_parse_vote_from_string(status, NULL,
v3_ns->type))) {
log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
"<<%s>>",
v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
goto err;
}
networkstatus_vote_free(v);
@ -209,7 +213,7 @@ static networkstatus_voter_info_t *
get_voter(const networkstatus_t *vote)
{
tor_assert(vote);
tor_assert(vote->is_vote);
tor_assert(vote->type == NS_TYPE_VOTE);
tor_assert(vote->voters);
tor_assert(smartlist_len(vote->voters) == 1);
return smartlist_get(vote->voters, 0);
@ -504,7 +508,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
int j;
SMARTLIST_FOREACH(votes, networkstatus_t *, v,
{
tor_assert(v->is_vote);
tor_assert(v->type == NS_TYPE_VOTE);
va_times[v_sl_idx] = v->valid_after;
fu_times[v_sl_idx] = v->fresh_until;
vu_times[v_sl_idx] = v->valid_until;
@ -1000,7 +1004,8 @@ networkstatus_compute_consensus(smartlist_t *votes,
{
networkstatus_t *c;
if (!(c = networkstatus_parse_vote_from_string(result, NULL, 0))) {
if (!(c = networkstatus_parse_vote_from_string(result, NULL,
NS_TYPE_CONSENSUS))) {
log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
"parse.");
tor_free(result);
@ -1028,7 +1033,7 @@ networkstatus_add_detached_signatures(networkstatus_t *target,
int r = 0;
tor_assert(sigs);
tor_assert(target);
tor_assert(!target->is_vote);
tor_assert(target->type == NS_TYPE_CONSENSUS);
/* Do the times seem right? */
if (target->valid_after != sigs->valid_after) {
@ -1117,7 +1122,7 @@ networkstatus_get_detached_signatures(networkstatus_t *consensus)
char *result = NULL;
int n_sigs = 0;
tor_assert(consensus);
tor_assert(! consensus->is_vote);
tor_assert(consensus->type == NS_TYPE_CONSENSUS);
elements = smartlist_create();
@ -1598,7 +1603,8 @@ dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
*msg_out = NULL;
again:
vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote, 1);
vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote,
NS_TYPE_VOTE);
if (!end_of_vote)
end_of_vote = vote_body + strlen(vote_body);
if (!vote) {
@ -1783,7 +1789,8 @@ dirvote_compute_consensus(void)
log_warn(LD_DIR, "Couldn't generate a consensus at all!");
goto err;
}
consensus = networkstatus_parse_vote_from_string(consensus_body, NULL, 0);
consensus = networkstatus_parse_vote_from_string(consensus_body, NULL,
NS_TYPE_CONSENSUS);
if (!consensus) {
log_warn(LD_DIR, "Couldn't parse consensus we generated!");
goto err;
@ -1904,7 +1911,8 @@ dirvote_add_signatures_to_pending_consensus(
ns_detached_signatures_t *sigs =
networkstatus_parse_detached_signatures(new_detached, NULL);
networkstatus_t *v = networkstatus_parse_vote_from_string(
pending_consensus_body, NULL, 0);
pending_consensus_body, NULL,
NS_TYPE_CONSENSUS);
tor_assert(sigs);
ns_detached_signatures_free(sigs);
tor_assert(v);

View File

@ -290,7 +290,7 @@ networkstatus_vote_free(networkstatus_t *ns)
authority_cert_free(ns->cert);
if (ns->routerstatus_list) {
if (ns->is_vote) {
if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
{
tor_free(rs->version);
@ -382,7 +382,7 @@ networkstatus_check_consensus_signature(networkstatus_t *consensus,
smartlist_t *missing_authorities = smartlist_create();
int severity;
tor_assert(! consensus->is_vote);
tor_assert(consensus->type == NS_TYPE_CONSENSUS);
SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
{
@ -1366,7 +1366,7 @@ networkstatus_set_current_consensus(const char *consensus, unsigned flags)
const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
/* Make sure it's parseable. */
c = networkstatus_parse_vote_from_string(consensus, NULL, 0);
c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
if (!c) {
log_warn(LD_DIR, "Unable to parse networkstatus consensus");
result = -2;

View File

@ -1492,10 +1492,19 @@ typedef struct networkstatus_voter_info_t {
* as good. */
} networkstatus_voter_info_t;
/** Enumerates the possible seriousness values of a networkstatus document. */
typedef enum {
NS_TYPE_VOTE,
NS_TYPE_CONSENSUS,
NS_TYPE_OPINION,
} networkstatus_type_t;
/** A common structure to hold a v3 network status vote, or a v3 network
* status consensus. */
typedef struct networkstatus_t {
int is_vote; /**< True if this is a vote; false if it is a consensus. */
// int is_vote;
networkstatus_type_t type; /**< Vote, consensus, or opinion? */
time_t published; /**< Vote only: Tiem when vote was written. */
time_t valid_after; /**< Time after which this vote or consensus applies. */
time_t fresh_until; /**< Time before which this is the most recent vote or
@ -4207,8 +4216,8 @@ void dump_distinct_digest_count(int severity);
networkstatus_v2_t *networkstatus_v2_parse_from_string(const char *s);
networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
const char **eos_out,
int is_vote);
const char **eos_out,
networkstatus_type_t ns_type);
ns_detached_signatures_t *networkstatus_parse_detached_signatures(
const char *s, const char *eos);

View File

@ -2058,12 +2058,11 @@ networkstatus_v2_parse_from_string(const char *s)
return ns;
}
/** Parse a v3 networkstatus vote (if <b>is_vote</b> is true) or a v3
* networkstatus consensus (if <b>is_vote</b> is false) from <b>s</b>, and
* return the result. Return NULL on failure. */
/** Parse a v3 networkstatus vote, opinion, or consensus (depending on
* ns_type), from <b>s</b>, and return the result. Return NULL on failure. */
networkstatus_t *
networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
int is_vote)
networkstatus_type_t ns_type)
{
smartlist_t *tokens = smartlist_create();
smartlist_t *rs_tokens = NULL, *footer_tokens = NULL;
@ -2085,16 +2084,17 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
area = memarea_new(8192);
end_of_header = find_start_of_next_routerstatus(s);
if (tokenize_string(area, s, end_of_header, tokens,
is_vote ? networkstatus_token_table :
networkstatus_consensus_token_table, 0)) {
log_warn(LD_DIR, "Error tokenizing network-status vote header.");
(ns_type == NS_TYPE_CONSENSUS) ?
networkstatus_consensus_token_table :
networkstatus_token_table, 0)) {
log_warn(LD_DIR, "Error tokenizing network-status vote header");
goto err;
}
ns = tor_malloc_zero(sizeof(networkstatus_t));
memcpy(ns->networkstatus_digest, ns_digest, DIGEST_LEN);
if (is_vote) {
if (ns_type != NS_TYPE_CONSENSUS) {
const char *end_of_cert = NULL;
if (!(cert = strstr(s, "\ndir-key-certificate-version")))
goto err;
@ -2108,20 +2108,22 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
tor_assert(tok);
tor_assert(tok->n_args != 0);
if (!strcmp(tok->args[0], "vote")) {
ns->is_vote = 1;
ns->type = NS_TYPE_VOTE;
} else if (!strcmp(tok->args[0], "consensus")) {
ns->is_vote = 0;
ns->type = NS_TYPE_CONSENSUS;
} else if (!strcmp(tok->args[0], "opinion")) {
ns->type = NS_TYPE_OPINION;
} else {
log_warn(LD_DIR, "Unrecognized vote status %s in network-status",
escaped(tok->args[0]));
goto err;
}
if (!bool_eq(ns->is_vote, is_vote)) {
if (ns_type != ns->type) {
log_warn(LD_DIR, "Got the wrong kind of v3 networkstatus.");
goto err;
}
if (ns->is_vote) {
if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
tok = find_first_by_keyword(tokens, K_PUBLISHED);
if (parse_iso_time(tok->args[0], &ns->published))
goto err;
@ -2218,7 +2220,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
if (voter)
smartlist_add(ns->voters, voter);
voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
if (is_vote)
if (ns->type != NS_TYPE_CONSENSUS)
memcpy(voter->vote_digest, ns_digest, DIGEST_LEN);
voter->nickname = tor_strdup(tok->args[0]);
@ -2229,8 +2231,9 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
"network-status vote.", escaped(tok->args[1]));
goto err;
}
if (is_vote && memcmp(ns->cert->cache_info.identity_digest,
voter->identity_digest, DIGEST_LEN)) {
if (ns->type != NS_TYPE_CONSENSUS &&
memcmp(ns->cert->cache_info.identity_digest,
voter->identity_digest, DIGEST_LEN)) {
log_warn(LD_DIR,"Mismatch between identities in certificate and vote");
goto err;
}
@ -2256,7 +2259,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
}
voter->contact = tor_strdup(tok->args[0]);
} else if (tok->tp == K_VOTE_DIGEST) {
tor_assert(!is_vote);
tor_assert(ns->type == NS_TYPE_CONSENSUS);
tor_assert(tok->n_args >= 1);
if (!voter || ! tor_digest_is_zero(voter->vote_digest)) {
log_warn(LD_DIR, "vote-digest element is out of place.");
@ -2278,12 +2281,12 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
if (smartlist_len(ns->voters) == 0) {
log_warn(LD_DIR, "Missing dir-source elements in a vote networkstatus.");
goto err;
} else if (is_vote && smartlist_len(ns->voters) != 1) {
} else if (ns->type != NS_TYPE_CONSENSUS && smartlist_len(ns->voters) != 1) {
log_warn(LD_DIR, "Too many dir-source elements in a vote networkstatus.");
goto err;
}
if (is_vote &&
if (ns->type != NS_TYPE_CONSENSUS &&
(tok = find_first_by_keyword(tokens, K_LEGACY_DIR_KEY))) {
int bad = 1;
if (strlen(tok->args[0]) == HEX_DIGEST_LEN) {
@ -2307,7 +2310,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
ns->routerstatus_list = smartlist_create();
while (!strcmpstart(s, "r ")) {
if (is_vote) {
if (ns->type != NS_TYPE_CONSENSUS) {
vote_routerstatus_t *rs = tor_malloc_zero(sizeof(vote_routerstatus_t));
if (routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens, ns,
rs, 0))
@ -2326,7 +2329,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
}
for (i = 1; i < smartlist_len(ns->routerstatus_list); ++i) {
routerstatus_t *rs1, *rs2;
if (is_vote) {
if (ns->type != NS_TYPE_CONSENSUS) {
vote_routerstatus_t *a = smartlist_get(ns->routerstatus_list, i-1);
vote_routerstatus_t *b = smartlist_get(ns->routerstatus_list, i);
rs1 = &a->status; rs2 = &b->status;
@ -2389,7 +2392,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
goto err;
}
if (is_vote) {
if (ns->type != NS_TYPE_CONSENSUS) {
if (memcmp(declared_identity, ns->cert->cache_info.identity_digest,
DIGEST_LEN)) {
log_warn(LD_DIR, "Digest mismatch between declared and actual on "
@ -2398,7 +2401,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
}
}
if (is_vote) {
if (ns->type != NS_TYPE_CONSENSUS) {
if (check_signature_token(ns_digest, tok, ns->cert->signing_key, 0,
"network-status vote"))
goto err;

View File

@ -2845,7 +2845,7 @@ test_v3_networkstatus(void)
* Set up a vote; generate it; try to parse it.
*/
vote = tor_malloc_zero(sizeof(networkstatus_t));
vote->is_vote = 1;
vote->type = NS_TYPE_VOTE;
vote->published = now;
vote->valid_after = now+1000;
vote->fresh_until = now+2000;
@ -2931,11 +2931,11 @@ test_v3_networkstatus(void)
/* dump the vote and try to parse it. */
v1_text = format_networkstatus_vote(sign_skey_1, vote);
test_assert(v1_text);
v1 = networkstatus_parse_vote_from_string(v1_text, NULL, 1);
v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
test_assert(v1);
/* Make sure the parsed thing was right. */
test_eq(v1->is_vote, 1);
test_eq(v1->type, NS_TYPE_VOTE);
test_eq(v1->published, vote->published);
test_eq(v1->valid_after, vote->valid_after);
test_eq(v1->fresh_until, vote->fresh_until);
@ -3015,7 +3015,7 @@ test_v3_networkstatus(void)
/* generate and parse. */
v2_text = format_networkstatus_vote(sign_skey_2, vote);
test_assert(v2_text);
v2 = networkstatus_parse_vote_from_string(v2_text, NULL, 1);
v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
test_assert(v2);
/* Check that flags come out right.*/
cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
@ -3054,7 +3054,7 @@ test_v3_networkstatus(void)
v3_text = format_networkstatus_vote(sign_skey_3, vote);
test_assert(v3_text);
v3 = networkstatus_parse_vote_from_string(v3_text, NULL, 1);
v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
test_assert(v3);
/* Compute a consensus as voter 3. */
@ -3067,13 +3067,14 @@ test_v3_networkstatus(void)
"AAAAAAAAAAAAAAAAAAAA",
sign_skey_leg1);
test_assert(consensus_text);
con = networkstatus_parse_vote_from_string(consensus_text, NULL, 0);
con = networkstatus_parse_vote_from_string(consensus_text, NULL,
NS_TYPE_CONSENSUS);
test_assert(con);
//log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
// v1_text, v2_text, v3_text);
/* Check consensus contents. */
test_assert(!con->is_vote);
test_assert(con->type == NS_TYPE_CONSENSUS);
test_eq(con->published, 0); /* this field only appears in votes. */
test_eq(con->valid_after, now+1000);
test_eq(con->fresh_until, now+2003); /* median */
@ -3177,8 +3178,10 @@ test_v3_networkstatus(void)
sign_skey_1, NULL,NULL);
test_assert(consensus_text2);
test_assert(consensus_text3);
con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL, 0);
con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL, 0);
con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
NS_TYPE_CONSENSUS);
con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
NS_TYPE_CONSENSUS);
test_assert(con2);
test_assert(con3);
@ -4072,7 +4075,7 @@ main(int c, char**v)
crypto_seed_rng(1);
if (1) {
if (0) {
bench_aes();
return 0;
}