aboutsummaryrefslogtreecommitdiff
path: root/src/or/dirvote.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/or/dirvote.c')
-rw-r--r--src/or/dirvote.c188
1 files changed, 121 insertions, 67 deletions
diff --git a/src/or/dirvote.c b/src/or/dirvote.c
index 144859ae0..ef1e506f4 100644
--- a/src/or/dirvote.c
+++ b/src/or/dirvote.c
@@ -53,29 +53,6 @@ static int dirvote_compute_consensuses(void);
static int dirvote_publish_consensus(void);
static char *make_consensus_method_list(int low, int high, const char *sep);
-/** The highest consensus method that we currently support. */
-#define MAX_SUPPORTED_CONSENSUS_METHOD 13
-
-/** Lowest consensus method that contains a 'directory-footer' marker */
-#define MIN_METHOD_FOR_FOOTER 9
-
-/** Lowest consensus method that contains bandwidth weights */
-#define MIN_METHOD_FOR_BW_WEIGHTS 9
-
-/** Lowest consensus method that contains consensus params */
-#define MIN_METHOD_FOR_PARAMS 7
-
-/** Lowest consensus method that generates microdescriptors */
-#define MIN_METHOD_FOR_MICRODESC 8
-
-/** Lowest consensus method that ensures a majority of authorities voted
- * for a param. */
-#define MIN_METHOD_FOR_MAJORITY_PARAMS 12
-
-/** Lowest consensus method where microdesc consensuses omit any entry
- * with no microdesc. */
-#define MIN_METHOD_FOR_MANDATORY_MICRODESC 13
-
/* =====
* Voting
* =====*/
@@ -346,7 +323,7 @@ typedef struct dir_src_ent_t {
/** Helper for sorting networkstatus_t votes (not consensuses) by the
* hash of their voters' identity digests. */
static int
-_compare_votes_by_authority_id(const void **_a, const void **_b)
+compare_votes_by_authority_id_(const void **_a, const void **_b)
{
const networkstatus_t *a = *_a, *b = *_b;
return fast_memcmp(get_voter(a)->identity_digest,
@@ -357,7 +334,7 @@ _compare_votes_by_authority_id(const void **_a, const void **_b)
* their identity digests, and return -1, 0, or 1 depending on their
* ordering */
static int
-_compare_dir_src_ents_by_authority_id(const void **_a, const void **_b)
+compare_dir_src_ents_by_authority_id_(const void **_a, const void **_b)
{
const dir_src_ent_t *a = *_a, *b = *_b;
const networkstatus_voter_info_t *a_v = get_voter(a->v),
@@ -424,12 +401,27 @@ compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
/** Helper for sorting routerlists based on compare_vote_rs. */
static int
-_compare_vote_rs(const void **_a, const void **_b)
+compare_vote_rs_(const void **_a, const void **_b)
{
const vote_routerstatus_t *a = *_a, *b = *_b;
return compare_vote_rs(a,b);
}
+/** Helper for sorting OR ports. */
+static int
+compare_orports_(const void **_a, const void **_b)
+{
+ const tor_addr_port_t *a = *_a, *b = *_b;
+ int r;
+
+ if ((r = tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
+ return r;
+ if ((r = (((int) b->port) - ((int) a->port))))
+ return r;
+
+ return 0;
+}
+
/** Given a list of vote_routerstatus_t, all for the same router identity,
* return whichever is most frequent, breaking ties in favor of more
* recently published vote_routerstatus_t and in case of ties there,
@@ -437,17 +429,18 @@ _compare_vote_rs(const void **_a, const void **_b)
*/
static vote_routerstatus_t *
compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
- char *microdesc_digest256_out)
+ char *microdesc_digest256_out,
+ tor_addr_port_t *best_alt_orport_out)
{
vote_routerstatus_t *most = NULL, *cur = NULL;
int most_n = 0, cur_n = 0;
time_t most_published = 0;
- /* _compare_vote_rs() sorts the items by identity digest (all the same),
+ /* compare_vote_rs_() sorts the items by identity digest (all the same),
* then by SD digest. That way, if we have a tie that the published_on
* date cannot tie, we use the descriptor with the smaller digest.
*/
- smartlist_sort(votes, _compare_vote_rs);
+ smartlist_sort(votes, compare_vote_rs_);
SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
if (cur && !compare_vote_rs(cur, rs)) {
++cur_n;
@@ -473,6 +466,38 @@ compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
tor_assert(most);
+ /* If we're producing "a" lines, vote on potential alternative (sets
+ * of) OR port(s) in the winning routerstatuses.
+ *
+ * XXX prop186 There's at most one alternative OR port (_the_ IPv6
+ * port) for now. */
+ if (consensus_method >= MIN_METHOD_FOR_A_LINES && best_alt_orport_out) {
+ smartlist_t *alt_orports = smartlist_new();
+ const tor_addr_port_t *most_alt_orport = NULL;
+
+ SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
+ if (compare_vote_rs(most, rs) == 0 &&
+ !tor_addr_is_null(&rs->status.ipv6_addr)
+ && rs->status.ipv6_orport) {
+ smartlist_add(alt_orports, tor_addr_port_new(&rs->status.ipv6_addr,
+ rs->status.ipv6_orport));
+ }
+ } SMARTLIST_FOREACH_END(rs);
+
+ smartlist_sort(alt_orports, compare_orports_);
+ most_alt_orport = smartlist_get_most_frequent(alt_orports,
+ compare_orports_);
+ if (most_alt_orport) {
+ memcpy(best_alt_orport_out, most_alt_orport, sizeof(tor_addr_port_t));
+ log_debug(LD_DIR, "\"a\" line winner for %s is %s",
+ most->status.nickname,
+ fmt_addrport(&most_alt_orport->addr, most_alt_orport->port));
+ }
+
+ SMARTLIST_FOREACH(alt_orports, tor_addr_port_t *, ap, tor_free(ap));
+ smartlist_free(alt_orports);
+ }
+
if (consensus_method >= MIN_METHOD_FOR_MICRODESC &&
microdesc_digest256_out) {
smartlist_t *digests = smartlist_new();
@@ -518,7 +543,7 @@ hash_list_members(char *digest_out, size_t len_out,
* positive integers. (Non-integers are treated as prior to all integers, and
* compared lexically.) */
static int
-_cmp_int_strings(const void **_a, const void **_b)
+cmp_int_strings_(const void **_a, const void **_b)
{
const char *a = *_a, *b = *_b;
int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
@@ -549,13 +574,13 @@ compute_consensus_method(smartlist_t *votes)
{
tor_assert(vote->supported_methods);
smartlist_add_all(tmp, vote->supported_methods);
- smartlist_sort(tmp, _cmp_int_strings);
- smartlist_uniq(tmp, _cmp_int_strings, NULL);
+ smartlist_sort(tmp, cmp_int_strings_);
+ smartlist_uniq(tmp, cmp_int_strings_, NULL);
smartlist_add_all(all_methods, tmp);
smartlist_clear(tmp);
});
- smartlist_sort(all_methods, _cmp_int_strings);
+ smartlist_sort(all_methods, cmp_int_strings_);
get_frequent_members(acceptable_methods, all_methods, min);
n_ok = smartlist_len(acceptable_methods);
if (n_ok) {
@@ -1504,7 +1529,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
}
/* Sort the votes. */
- smartlist_sort(votes, _compare_votes_by_authority_id);
+ smartlist_sort(votes, compare_votes_by_authority_id_);
/* Add the authority sections. */
{
smartlist_t *dir_sources = smartlist_new();
@@ -1523,7 +1548,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
smartlist_add(dir_sources, e_legacy);
}
} SMARTLIST_FOREACH_END(v);
- smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
+ smartlist_sort(dir_sources, compare_dir_src_ents_by_authority_id_);
SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
char fingerprint[HEX_DIGEST_LEN+1];
@@ -1685,6 +1710,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
int n_listing = 0;
int i;
char microdesc_digest[DIGEST256_LEN];
+ tor_addr_port_t alt_orport = {TOR_ADDR_NULL, 0};
/* Of the next-to-be-considered digest in each voter, which is first? */
SMARTLIST_FOREACH(votes, networkstatus_t *, v, {
@@ -1754,7 +1780,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
* routerinfo and its contents are. */
memset(microdesc_digest, 0, sizeof(microdesc_digest));
rs = compute_routerstatus_consensus(matching_descs, consensus_method,
- microdesc_digest);
+ microdesc_digest, &alt_orport);
/* Copy bits of that into rs_out. */
memset(&rs_out, 0, sizeof(rs_out));
tor_assert(fast_memeq(lowest_id, rs->status.identity_digest,DIGEST_LEN));
@@ -1765,6 +1791,10 @@ networkstatus_compute_consensus(smartlist_t *votes,
rs_out.published_on = rs->status.published_on;
rs_out.dir_port = rs->status.dir_port;
rs_out.or_port = rs->status.or_port;
+ if (consensus_method >= MIN_METHOD_FOR_A_LINES) {
+ tor_addr_copy(&rs_out.ipv6_addr, &alt_orport.addr);
+ rs_out.ipv6_orport = alt_orport.port;
+ }
rs_out.has_bandwidth = 0;
rs_out.has_exitsummary = 0;
@@ -1862,7 +1892,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
* listed that descriptor will have the same summary. If not then
* something is fishy and we'll use the most common one (breaking
* ties in favor of lexicographically larger one (only because it
- * lets me reuse more existing code.
+ * lets me reuse more existing code)).
*
* The other case that can happen is that no authority that voted
* for that descriptor has an exit policy summary. That's
@@ -2193,7 +2223,7 @@ networkstatus_add_detached_signatures(networkstatus_t *target,
{
digests_t *digests = strmap_get(sigs->digests, flavor);
int n_matches = 0;
- digest_algorithm_t alg;
+ int alg;
if (!digests) {
*msg_out = "No digests for given consensus flavor";
return -1;
@@ -2457,7 +2487,7 @@ ns_detached_signatures_free(ns_detached_signatures_t *s)
smartlist_free(sigs);
} STRMAP_FOREACH_END;
strmap_free(s->signatures, NULL);
- strmap_free(s->digests, _tor_free);
+ strmap_free(s->digests, tor_free_);
}
tor_free(s);
@@ -2757,7 +2787,7 @@ dirvote_fetch_missing_votes(void)
char *resource;
SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
- trusted_dir_server_t *, ds) {
+ dir_server_t *, ds) {
if (!(ds->type & V3_DIRINFO))
continue;
if (!dirvote_get_vote(ds->v3_identity_digest,
@@ -2875,7 +2905,7 @@ list_v3_auth_ids(void)
smartlist_t *known_v3_keys = smartlist_new();
char *keys;
SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
- trusted_dir_server_t *, ds,
+ dir_server_t *, ds,
if ((ds->type & V3_DIRINFO) &&
!tor_digest_is_zero(ds->v3_identity_digest))
smartlist_add(known_v3_keys,
@@ -2896,7 +2926,7 @@ dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
{
networkstatus_t *vote;
networkstatus_voter_info_t *vi;
- trusted_dir_server_t *ds;
+ dir_server_t *ds;
pending_vote_t *pending_vote = NULL;
const char *end_of_vote = NULL;
int any_failed = 0;
@@ -3441,7 +3471,7 @@ dirvote_free_all(void)
const char *
dirvote_get_pending_consensus(consensus_flavor_t flav)
{
- tor_assert(((int)flav) >= 0 && flav < N_CONSENSUS_FLAVORS);
+ tor_assert(((int)flav) >= 0 && (int)flav < N_CONSENSUS_FLAVORS);
return pending_consensuses[flav].body;
}
@@ -3504,15 +3534,11 @@ dirvote_get_vote(const char *fp, int flags)
return NULL;
}
-/** Construct and return a new microdescriptor from a routerinfo <b>ri</b>.
- *
- * XXX Right now, there is only one way to generate microdescriptors from
- * router descriptors. This may change in future consensus methods. If so,
- * we'll need an internal way to remember which method we used, and ask for a
- * particular method.
+/** Construct and return a new microdescriptor from a routerinfo <b>ri</b>
+ * according to <b>consensus_method</b>.
**/
microdesc_t *
-dirvote_create_microdescriptor(const routerinfo_t *ri)
+dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
{
microdesc_t *result = NULL;
char *key = NULL, *summary = NULL, *family = NULL;
@@ -3522,18 +3548,43 @@ dirvote_create_microdescriptor(const routerinfo_t *ri)
if (crypto_pk_write_public_key_to_string(ri->onion_pkey, &key, &keylen)<0)
goto done;
- summary = policy_summarize(ri->exit_policy);
+ summary = policy_summarize(ri->exit_policy, AF_INET);
if (ri->declared_family)
family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
smartlist_add_asprintf(chunks, "onion-key\n%s", key);
+ if (consensus_method >= MIN_METHOD_FOR_NTOR_KEY &&
+ ri->onion_curve25519_pkey) {
+ char kbuf[128];
+ base64_encode(kbuf, sizeof(kbuf),
+ (const char*)ri->onion_curve25519_pkey->public_key,
+ CURVE25519_PUBKEY_LEN);
+ smartlist_add_asprintf(chunks, "ntor-onion-key %s", kbuf);
+ }
+
+ if (consensus_method >= MIN_METHOD_FOR_A_LINES &&
+ !tor_addr_is_null(&ri->ipv6_addr) && ri->ipv6_orport)
+ smartlist_add_asprintf(chunks, "a %s\n",
+ fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
+
if (family)
smartlist_add_asprintf(chunks, "family %s\n", family);
if (summary && strcmp(summary, "reject 1-65535"))
smartlist_add_asprintf(chunks, "p %s\n", summary);
+ if (consensus_method >= MIN_METHOD_FOR_P6_LINES &&
+ ri->ipv6_exit_policy) {
+ /* XXXX024 This doesn't match proposal 208, which says these should
+ * be taken unchanged from the routerinfo. That's bogosity, IMO:
+ * the proposal should have said to do this instead.*/
+ char *p6 = write_short_policy(ri->ipv6_exit_policy);
+ if (p6 && strcmp(p6, "reject 1-65535"))
+ smartlist_add_asprintf(chunks, "p6 %s\n", p6);
+ tor_free(p6);
+ }
+
output = smartlist_join_strings(chunks, "", 0, NULL);
{
@@ -3561,33 +3612,36 @@ dirvote_create_microdescriptor(const routerinfo_t *ri)
return result;
}
-/** Cached space-separated string to hold */
-static char *microdesc_consensus_methods = NULL;
-
/** Format the appropriate vote line to describe the microdescriptor <b>md</b>
* in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
* in <b>out</b>. Return -1 on failure and the number of characters written
* on success. */
ssize_t
-dirvote_format_microdesc_vote_line(char *out, size_t out_len,
- const microdesc_t *md)
+dirvote_format_microdesc_vote_line(char *out_buf, size_t out_buf_len,
+ const microdesc_t *md,
+ int consensus_method_low,
+ int consensus_method_high)
{
+ ssize_t ret = -1;
char d64[BASE64_DIGEST256_LEN+1];
- if (!microdesc_consensus_methods) {
- microdesc_consensus_methods =
- make_consensus_method_list(MIN_METHOD_FOR_MICRODESC,
- MAX_SUPPORTED_CONSENSUS_METHOD,
- ",");
- tor_assert(microdesc_consensus_methods);
- }
+ char *microdesc_consensus_methods =
+ make_consensus_method_list(consensus_method_low,
+ consensus_method_high,
+ ",");
+ tor_assert(microdesc_consensus_methods);
+
if (digest256_to_base64(d64, md->digest)<0)
- return -1;
+ goto out;
- if (tor_snprintf(out, out_len, "m %s sha256=%s\n",
+ if (tor_snprintf(out_buf, out_buf_len, "m %s sha256=%s\n",
microdesc_consensus_methods, d64)<0)
- return -1;
+ goto out;
- return strlen(out);
+ ret = strlen(out_buf);
+
+ out:
+ tor_free(microdesc_consensus_methods);
+ return ret;
}
/** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with