tor  0.4.1.0-alpha-dev
dirvote.c
Go to the documentation of this file.
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 #define DIRVOTE_PRIVATE
7 #include "core/or/or.h"
8 #include "app/config/config.h"
9 #include "core/or/policies.h"
10 #include "core/or/protover.h"
11 #include "core/or/tor_version_st.h"
12 #include "core/or/versions.h"
13 #include "feature/dirauth/bwauth.h"
34 #include "feature/relay/router.h"
35 #include "feature/relay/routerkeys.h"
36 #include "feature/stats/rephist.h"
37 #include "feature/client/entrynodes.h" /* needed for guardfraction methods */
38 #include "feature/nodelist/torcert.h"
40 
43 #include "feature/dirauth/shared_random_state.h"
44 
45 #include "feature/nodelist/authority_cert_st.h"
46 #include "feature/dircache/cached_dir_st.h"
47 #include "feature/dirclient/dir_server_st.h"
48 #include "feature/nodelist/document_signature_st.h"
49 #include "feature/nodelist/microdesc_st.h"
50 #include "feature/nodelist/networkstatus_st.h"
51 #include "feature/nodelist/networkstatus_voter_info_st.h"
52 #include "feature/nodelist/node_st.h"
53 #include "feature/dirauth/ns_detached_signatures_st.h"
54 #include "feature/nodelist/routerinfo_st.h"
55 #include "feature/nodelist/routerlist_st.h"
56 #include "feature/dirauth/vote_microdesc_hash_st.h"
57 #include "feature/nodelist/vote_routerstatus_st.h"
58 #include "feature/dircommon/vote_timing_st.h"
59 
60 #include "lib/container/order.h"
61 #include "lib/encoding/confline.h"
63 
64 /* Algorithm to use for the bandwidth file digest. */
65 #define DIGEST_ALG_BW_FILE DIGEST_SHA256
66 
108 typedef struct pending_consensus_t {
111  char *body;
115 
116 /* DOCDOC dirvote_add_signatures_to_all_pending_consensuses */
117 static int dirvote_add_signatures_to_all_pending_consensuses(
118  const char *detached_signatures_body,
119  const char *source,
120  const char **msg_out);
124  const char *source,
125  int severity,
126  const char **msg_out);
127 static char *list_v3_auth_ids(void);
128 static void dirvote_fetch_missing_votes(void);
129 static void dirvote_fetch_missing_signatures(void);
130 static int dirvote_perform_vote(void);
131 static void dirvote_clear_votes(int all_votes);
132 static int dirvote_compute_consensuses(void);
133 static int dirvote_publish_consensus(void);
134 
135 /* =====
136  * Certificate functions
137  * ===== */
138 
141 STATIC authority_cert_t *
143 {
144  authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
145  tor_assert(cert);
146 
147  memcpy(out, cert, sizeof(authority_cert_t));
148  /* Now copy pointed-to things. */
150  tor_strndup(cert->cache_info.signed_descriptor_body,
155 
156  return out;
157 }
158 
159 /* =====
160  * Voting
161  * =====*/
162 
163 /* If <b>opt_value</b> is non-NULL, return "keyword opt_value\n" in a new
164  * string. Otherwise return a new empty string. */
165 static char *
166 format_line_if_present(const char *keyword, const char *opt_value)
167 {
168  if (opt_value) {
169  char *result = NULL;
170  tor_asprintf(&result, "%s %s\n", keyword, opt_value);
171  return result;
172  } else {
173  return tor_strdup("");
174  }
175 }
176 
179 static char *
181 {
182  char *recommended_relay_protocols_line = NULL;
183  char *recommended_client_protocols_line = NULL;
184  char *required_relay_protocols_line = NULL;
185  char *required_client_protocols_line = NULL;
186 
187  recommended_relay_protocols_line =
188  format_line_if_present("recommended-relay-protocols",
190  recommended_client_protocols_line =
191  format_line_if_present("recommended-client-protocols",
192  v3_ns->recommended_client_protocols);
193  required_relay_protocols_line =
194  format_line_if_present("required-relay-protocols",
195  v3_ns->required_relay_protocols);
196  required_client_protocols_line =
197  format_line_if_present("required-client-protocols",
198  v3_ns->required_client_protocols);
199 
200  char *result = NULL;
201  tor_asprintf(&result, "%s%s%s%s",
202  recommended_relay_protocols_line,
203  recommended_client_protocols_line,
204  required_relay_protocols_line,
205  required_client_protocols_line);
206 
207  tor_free(recommended_relay_protocols_line);
208  tor_free(recommended_client_protocols_line);
209  tor_free(required_relay_protocols_line);
210  tor_free(required_client_protocols_line);
211 
212  return result;
213 }
214 
218 STATIC char *
220  networkstatus_t *v3_ns)
221 {
222  smartlist_t *chunks = smartlist_new();
223  char *packages = NULL;
224  char fingerprint[FINGERPRINT_LEN+1];
225  char digest[DIGEST_LEN];
226  uint32_t addr;
227  char *protocols_lines = NULL;
228  char *client_versions_line = NULL, *server_versions_line = NULL;
229  char *shared_random_vote_str = NULL;
231  char *status = NULL;
232 
233  tor_assert(private_signing_key);
234  tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
235 
236  voter = smartlist_get(v3_ns->voters, 0);
237 
238  addr = voter->addr;
239 
240  base16_encode(fingerprint, sizeof(fingerprint),
242 
243  client_versions_line = format_line_if_present("client-versions",
244  v3_ns->client_versions);
245  server_versions_line = format_line_if_present("server-versions",
246  v3_ns->server_versions);
247  protocols_lines = format_protocols_lines_for_vote(v3_ns);
248 
249  if (v3_ns->package_lines) {
250  smartlist_t *tmp = smartlist_new();
251  SMARTLIST_FOREACH(v3_ns->package_lines, const char *, p,
253  smartlist_add_asprintf(tmp, "package %s\n", p));
255  packages = smartlist_join_strings(tmp, "", 0, NULL);
256  SMARTLIST_FOREACH(tmp, char *, cp, tor_free(cp));
257  smartlist_free(tmp);
258  } else {
259  packages = tor_strdup("");
260  }
261 
262  /* Get shared random commitments/reveals line(s). */
263  shared_random_vote_str = sr_get_string_for_vote();
264 
265  {
266  char published[ISO_TIME_LEN+1];
267  char va[ISO_TIME_LEN+1];
268  char fu[ISO_TIME_LEN+1];
269  char vu[ISO_TIME_LEN+1];
270  char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
271  /* XXXX Abstraction violation: should be pulling a field out of v3_ns.*/
272  char *flag_thresholds = dirserv_get_flag_thresholds_line();
273  char *params;
274  char *bw_headers_line = NULL;
275  char *bw_file_digest = NULL;
276  authority_cert_t *cert = v3_ns->cert;
277  char *methods =
280  format_iso_time(published, v3_ns->published);
281  format_iso_time(va, v3_ns->valid_after);
282  format_iso_time(fu, v3_ns->fresh_until);
283  format_iso_time(vu, v3_ns->valid_until);
284 
285  if (v3_ns->net_params)
286  params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
287  else
288  params = tor_strdup("");
289  tor_assert(cert);
290 
291  /* v3_ns->bw_file_headers is only set when V3BandwidthsFile is
292  * configured */
293  if (v3_ns->bw_file_headers) {
294  char *bw_file_headers = NULL;
295  /* If there are too many headers, leave the header string NULL */
296  if (! BUG(smartlist_len(v3_ns->bw_file_headers)
298  bw_file_headers = smartlist_join_strings(v3_ns->bw_file_headers, " ",
299  0, NULL);
300  if (BUG(strlen(bw_file_headers) > MAX_BW_FILE_HEADERS_LINE_LEN)) {
301  /* Free and set to NULL, because the line was too long */
302  tor_free(bw_file_headers);
303  }
304  }
305  if (!bw_file_headers) {
306  /* If parsing failed, add a bandwidth header line with no entries */
307  bw_file_headers = tor_strdup("");
308  }
309  /* At this point, the line will always be present */
310  bw_headers_line = format_line_if_present("bandwidth-file-headers",
311  bw_file_headers);
312  tor_free(bw_file_headers);
313  }
314 
315  /* Create bandwidth-file-digest if applicable.
316  * v3_ns->b64_digest_bw_file will contain the digest when V3BandwidthsFile
317  * is configured and the bandwidth file could be read, even if it was not
318  * parseable.
319  */
320  if (!tor_digest256_is_zero((const char *)v3_ns->bw_file_digest256)) {
321  /* Encode the digest. */
322  char b64_digest_bw_file[BASE64_DIGEST256_LEN+1] = {0};
323  if (digest256_to_base64(b64_digest_bw_file,
324  (const char *)v3_ns->bw_file_digest256)>0) {
325  /* "bandwidth-file-digest" 1*(SP algorithm "=" digest) NL */
326  char *digest_algo_b64_digest_bw_file = NULL;
327  tor_asprintf(&digest_algo_b64_digest_bw_file, "%s=%s",
328  crypto_digest_algorithm_get_name(DIGEST_ALG_BW_FILE),
329  b64_digest_bw_file);
330  /* No need for tor_strdup(""), format_line_if_present does it. */
331  bw_file_digest = format_line_if_present(
332  "bandwidth-file-digest", digest_algo_b64_digest_bw_file);
333  tor_free(digest_algo_b64_digest_bw_file);
334  }
335  }
336 
337  smartlist_add_asprintf(chunks,
338  "network-status-version 3\n"
339  "vote-status %s\n"
340  "consensus-methods %s\n"
341  "published %s\n"
342  "valid-after %s\n"
343  "fresh-until %s\n"
344  "valid-until %s\n"
345  "voting-delay %d %d\n"
346  "%s%s" /* versions */
347  "%s" /* protocols */
348  "%s" /* packages */
349  "known-flags %s\n"
350  "flag-thresholds %s\n"
351  "params %s\n"
352  "dir-source %s %s %s %s %d %d\n"
353  "contact %s\n"
354  "%s" /* shared randomness information */
355  "%s" /* bandwidth file headers */
356  "%s" /* bandwidth file */
357  ,
358  v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
359  methods,
360  published, va, fu, vu,
361  v3_ns->vote_seconds, v3_ns->dist_seconds,
362  client_versions_line,
363  server_versions_line,
364  protocols_lines,
365  packages,
366  flags,
367  flag_thresholds,
368  params,
369  voter->nickname, fingerprint, voter->address,
370  fmt_addr32(addr), voter->dir_port, voter->or_port,
371  voter->contact,
372  shared_random_vote_str ?
373  shared_random_vote_str : "",
374  bw_headers_line ?
375  bw_headers_line : "",
376  bw_file_digest ? bw_file_digest: "");
377 
378  tor_free(params);
379  tor_free(flags);
380  tor_free(flag_thresholds);
381  tor_free(methods);
382  tor_free(shared_random_vote_str);
383  tor_free(bw_headers_line);
384  tor_free(bw_file_digest);
385 
386  if (!tor_digest_is_zero(voter->legacy_id_digest)) {
387  char fpbuf[HEX_DIGEST_LEN+1];
388  base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
389  smartlist_add_asprintf(chunks, "legacy-dir-key %s\n", fpbuf);
390  }
391 
392  smartlist_add(chunks, tor_strndup(cert->cache_info.signed_descriptor_body,
394  }
395 
397  vrs) {
398  char *rsf;
400  rsf = routerstatus_format_entry(&vrs->status,
401  vrs->version, vrs->protocols,
402  NS_V3_VOTE,
403  ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD,
404  vrs);
405  if (rsf)
406  smartlist_add(chunks, rsf);
407 
408  for (h = vrs->microdesc; h; h = h->next) {
410  }
411  } SMARTLIST_FOREACH_END(vrs);
412 
413  smartlist_add_strdup(chunks, "directory-footer\n");
414 
415  /* The digest includes everything up through the space after
416  * directory-signature. (Yuck.) */
417  crypto_digest_smartlist(digest, DIGEST_LEN, chunks,
418  "directory-signature ", DIGEST_SHA1);
419 
420  {
421  char signing_key_fingerprint[FINGERPRINT_LEN+1];
422  if (crypto_pk_get_fingerprint(private_signing_key,
423  signing_key_fingerprint, 0)<0) {
424  log_warn(LD_BUG, "Unable to get fingerprint for signing key");
425  goto err;
426  }
427 
428  smartlist_add_asprintf(chunks, "directory-signature %s %s\n", fingerprint,
429  signing_key_fingerprint);
430  }
431 
432  {
433  char *sig = router_get_dirobj_signature(digest, DIGEST_LEN,
434  private_signing_key);
435  if (!sig) {
436  log_warn(LD_BUG, "Unable to sign networkstatus vote.");
437  goto err;
438  }
439  smartlist_add(chunks, sig);
440  }
441 
442  status = smartlist_join_strings(chunks, "", 0, NULL);
443 
444  {
445  networkstatus_t *v;
446  if (!(v = networkstatus_parse_vote_from_string(status, strlen(status),
447  NULL,
448  v3_ns->type))) {
449  log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
450  "<<%s>>",
451  v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
452  goto err;
453  }
454  networkstatus_vote_free(v);
455  }
456 
457  goto done;
458 
459  err:
460  tor_free(status);
461  done:
462  tor_free(client_versions_line);
463  tor_free(server_versions_line);
464  tor_free(protocols_lines);
465  tor_free(packages);
466 
467  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
468  smartlist_free(chunks);
469  return status;
470 }
471 
475 static void
477 {
478  const or_options_t *options = get_options();
479 
480  tor_assert(timing_out);
481 
482  timing_out->vote_interval = options->V3AuthVotingInterval;
483  timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
484  timing_out->vote_delay = options->V3AuthVoteDelay;
485  timing_out->dist_delay = options->V3AuthDistDelay;
486 }
487 
488 /* =====
489  * Consensus generation
490  * ===== */
491 
495 static int
497  const vote_routerstatus_t *vrs,
498  int method,
499  digest_algorithm_t alg)
500 {
501  /* XXXX only returns the sha256 method. */
502  const vote_microdesc_hash_t *h;
503  char mstr[64];
504  size_t mlen;
505  char dstr[64];
506 
507  tor_snprintf(mstr, sizeof(mstr), "%d", method);
508  mlen = strlen(mstr);
509  tor_snprintf(dstr, sizeof(dstr), " %s=",
511 
512  for (h = vrs->microdesc; h; h = h->next) {
513  const char *cp = h->microdesc_hash_line;
514  size_t num_len;
515  /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
516  * the first part. */
517  while (1) {
518  num_len = strspn(cp, "1234567890");
519  if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
520  /* This is the line. */
521  char buf[BASE64_DIGEST256_LEN+1];
522  /* XXXX ignores extraneous stuff if the digest is too long. This
523  * seems harmless enough, right? */
524  cp = strstr(cp, dstr);
525  if (!cp)
526  return -1;
527  cp += strlen(dstr);
528  strlcpy(buf, cp, sizeof(buf));
529  return digest256_from_base64(digest256_out, buf);
530  }
531  if (num_len == 0 || cp[num_len] != ',')
532  break;
533  cp += num_len + 1;
534  }
535  }
536  return -1;
537 }
538 
543 {
544  tor_assert(vote);
545  tor_assert(vote->type == NS_TYPE_VOTE);
546  tor_assert(vote->voters);
547  tor_assert(smartlist_len(vote->voters) == 1);
548  return smartlist_get(vote->voters, 0);
549 }
550 
554 typedef struct dir_src_ent_t {
555  networkstatus_t *v;
556  const char *digest;
557  int is_legacy;
558 } dir_src_ent_t;
559 
562 static int
563 compare_votes_by_authority_id_(const void **_a, const void **_b)
564 {
565  const networkstatus_t *a = *_a, *b = *_b;
566  return fast_memcmp(get_voter(a)->identity_digest,
567  get_voter(b)->identity_digest, DIGEST_LEN);
568 }
569 
573 static int
574 compare_dir_src_ents_by_authority_id_(const void **_a, const void **_b)
575 {
576  const dir_src_ent_t *a = *_a, *b = *_b;
577  const networkstatus_voter_info_t *a_v = get_voter(a->v),
578  *b_v = get_voter(b->v);
579  const char *a_id, *b_id;
580  a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
581  b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
582 
583  return fast_memcmp(a_id, b_id, DIGEST_LEN);
584 }
585 
588 static void
590 {
591  char *cur = NULL;
592  int count = 0;
593  SMARTLIST_FOREACH_BEGIN(in, char *, cp) {
594  if (cur && !strcmp(cp, cur)) {
595  ++count;
596  } else {
597  if (count > min)
598  smartlist_add(out, cur);
599  cur = cp;
600  count = 1;
601  }
602  } SMARTLIST_FOREACH_END(cp);
603  if (count > min)
604  smartlist_add(out, cur);
605 }
606 
609 #define get_most_frequent_member(lst) \
610  smartlist_get_most_frequent_string(lst)
611 
615 static int
617 {
618  int r;
619  tor_assert(a);
620  tor_assert(b);
621 
623  DIGEST_LEN)))
624  return r;
627  DIGEST_LEN)))
628  return r;
629  /* If we actually reached this point, then the identities and
630  * the descriptor digests matched, so somebody is making SHA1 collisions.
631  */
632 #define CMP_FIELD(utype, itype, field) do { \
633  utype aval = (utype) (itype) a->status.field; \
634  utype bval = (utype) (itype) b->status.field; \
635  utype u = bval - aval; \
636  itype r2 = (itype) u; \
637  if (r2 < 0) { \
638  return -1; \
639  } else if (r2 > 0) { \
640  return 1; \
641  } \
642  } while (0)
643 
644  CMP_FIELD(uint64_t, int64_t, published_on);
645 
646  if ((r = strcmp(b->status.nickname, a->status.nickname)))
647  return r;
648 
649  CMP_FIELD(unsigned, int, addr);
650  CMP_FIELD(unsigned, int, or_port);
651  CMP_FIELD(unsigned, int, dir_port);
652 
653  return 0;
654 }
655 
657 static int
658 compare_vote_rs_(const void **_a, const void **_b)
659 {
660  const vote_routerstatus_t *a = *_a, *b = *_b;
661  return compare_vote_rs(a,b);
662 }
663 
665 static int
666 compare_orports_(const void **_a, const void **_b)
667 {
668  const tor_addr_port_t *a = *_a, *b = *_b;
669  int r;
670 
671  if ((r = tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
672  return r;
673  if ((r = (((int) b->port) - ((int) a->port))))
674  return r;
675 
676  return 0;
677 }
678 
684 static vote_routerstatus_t *
685 compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
686  char *microdesc_digest256_out,
687  tor_addr_port_t *best_alt_orport_out)
688 {
689  vote_routerstatus_t *most = NULL, *cur = NULL;
690  int most_n = 0, cur_n = 0;
691  time_t most_published = 0;
692 
693  /* compare_vote_rs_() sorts the items by identity digest (all the same),
694  * then by SD digest. That way, if we have a tie that the published_on
695  * date cannot break, we use the descriptor with the smaller digest.
696  */
699  if (cur && !compare_vote_rs(cur, rs)) {
700  ++cur_n;
701  } else {
702  if (cur && (cur_n > most_n ||
703  (cur_n == most_n &&
704  cur->status.published_on > most_published))) {
705  most = cur;
706  most_n = cur_n;
707  most_published = cur->status.published_on;
708  }
709  cur_n = 1;
710  cur = rs;
711  }
712  } SMARTLIST_FOREACH_END(rs);
713 
714  if (cur_n > most_n ||
715  (cur && cur_n == most_n && cur->status.published_on > most_published)) {
716  most = cur;
717  // most_n = cur_n; // unused after this point.
718  // most_published = cur->status.published_on; // unused after this point.
719  }
720 
721  tor_assert(most);
722 
723  /* Vote on potential alternative (sets of) OR port(s) in the winning
724  * routerstatuses.
725  *
726  * XXX prop186 There's at most one alternative OR port (_the_ IPv6
727  * port) for now. */
728  if (best_alt_orport_out) {
729  smartlist_t *alt_orports = smartlist_new();
730  const tor_addr_port_t *most_alt_orport = NULL;
731 
733  tor_assert(rs);
734  if (compare_vote_rs(most, rs) == 0 &&
735  !tor_addr_is_null(&rs->status.ipv6_addr)
736  && rs->status.ipv6_orport) {
737  smartlist_add(alt_orports, tor_addr_port_new(&rs->status.ipv6_addr,
738  rs->status.ipv6_orport));
739  }
740  } SMARTLIST_FOREACH_END(rs);
741 
742  smartlist_sort(alt_orports, compare_orports_);
743  most_alt_orport = smartlist_get_most_frequent(alt_orports,
745  if (most_alt_orport) {
746  memcpy(best_alt_orport_out, most_alt_orport, sizeof(tor_addr_port_t));
747  log_debug(LD_DIR, "\"a\" line winner for %s is %s",
748  most->status.nickname,
749  fmt_addrport(&most_alt_orport->addr, most_alt_orport->port));
750  }
751 
752  SMARTLIST_FOREACH(alt_orports, tor_addr_port_t *, ap, tor_free(ap));
753  smartlist_free(alt_orports);
754  }
755 
756  if (microdesc_digest256_out) {
757  smartlist_t *digests = smartlist_new();
758  const uint8_t *best_microdesc_digest;
760  char d[DIGEST256_LEN];
761  if (compare_vote_rs(rs, most))
762  continue;
763  if (!vote_routerstatus_find_microdesc_hash(d, rs, consensus_method,
764  DIGEST_SHA256))
765  smartlist_add(digests, tor_memdup(d, sizeof(d)));
766  } SMARTLIST_FOREACH_END(rs);
767  smartlist_sort_digests256(digests);
768  best_microdesc_digest = smartlist_get_most_frequent_digest256(digests);
769  if (best_microdesc_digest)
770  memcpy(microdesc_digest256_out, best_microdesc_digest, DIGEST256_LEN);
771  SMARTLIST_FOREACH(digests, char *, cp, tor_free(cp));
772  smartlist_free(digests);
773  }
774 
775  return most;
776 }
777 
781 static int
782 cmp_int_strings_(const void **_a, const void **_b)
783 {
784  const char *a = *_a, *b = *_b;
785  int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
786  int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
787  if (ai<bi) {
788  return -1;
789  } else if (ai==bi) {
790  if (ai == 0) /* Parsing failed. */
791  return strcmp(a, b);
792  return 0;
793  } else {
794  return 1;
795  }
796 }
797 
800 static int
802 {
803  smartlist_t *all_methods = smartlist_new();
804  smartlist_t *acceptable_methods = smartlist_new();
805  smartlist_t *tmp = smartlist_new();
806  int min = (smartlist_len(votes) * 2) / 3;
807  int n_ok;
808  int result;
809  SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
810  {
811  tor_assert(vote->supported_methods);
812  smartlist_add_all(tmp, vote->supported_methods);
814  smartlist_uniq(tmp, cmp_int_strings_, NULL);
815  smartlist_add_all(all_methods, tmp);
816  smartlist_clear(tmp);
817  });
818 
819  smartlist_sort(all_methods, cmp_int_strings_);
820  get_frequent_members(acceptable_methods, all_methods, min);
821  n_ok = smartlist_len(acceptable_methods);
822  if (n_ok) {
823  const char *best = smartlist_get(acceptable_methods, n_ok-1);
824  result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
825  } else {
826  result = 1;
827  }
828  smartlist_free(tmp);
829  smartlist_free(all_methods);
830  smartlist_free(acceptable_methods);
831  return result;
832 }
833 
835 static int
837 {
838  return (method >= MIN_SUPPORTED_CONSENSUS_METHOD) &&
839  (method <= MAX_SUPPORTED_CONSENSUS_METHOD);
840 }
841 
844 STATIC char *
845 make_consensus_method_list(int low, int high, const char *separator)
846 {
847  char *list;
848 
849  int i;
850  smartlist_t *lst;
851  lst = smartlist_new();
852  for (i = low; i <= high; ++i) {
854  continue;
855  smartlist_add_asprintf(lst, "%d", i);
856  }
857  list = smartlist_join_strings(lst, separator, 0, NULL);
858  tor_assert(list);
859  SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
860  smartlist_free(lst);
861  return list;
862 }
863 
868 static char *
870 {
871  int min = n_versioning / 2;
872  smartlist_t *good = smartlist_new();
873  char *result;
874  SMARTLIST_FOREACH_BEGIN(lst, const char *, v) {
875  if (strchr(v, ' ')) {
876  log_warn(LD_DIR, "At least one authority has voted for a version %s "
877  "that contains a space. This probably wasn't intentional, and "
878  "is likely to cause trouble. Please tell them to stop it.",
879  escaped(v));
880  }
881  } SMARTLIST_FOREACH_END(v);
882  sort_version_list(lst, 0);
883  get_frequent_members(good, lst, min);
884  result = smartlist_join_strings(good, ",", 0, NULL);
885  smartlist_free(good);
886  return result;
887 }
888 
893 STATIC int32_t
895  const char *keyword,
896  int32_t default_val)
897 {
898  unsigned int n_found = 0;
899  int32_t value = default_val;
900 
901  SMARTLIST_FOREACH_BEGIN(param_list, const char *, k_v_pair) {
902  if (!strcmpstart(k_v_pair, keyword) && k_v_pair[strlen(keyword)] == '=') {
903  const char *integer_str = &k_v_pair[strlen(keyword)+1];
904  int ok;
905  value = (int32_t)
906  tor_parse_long(integer_str, 10, INT32_MIN, INT32_MAX, &ok, NULL);
907  if (BUG(! ok))
908  return default_val;
909  ++n_found;
910  }
911  } SMARTLIST_FOREACH_END(k_v_pair);
912 
913  if (n_found == 1) {
914  return value;
915  } else {
916  tor_assert_nonfatal(n_found == 0);
917  return default_val;
918  }
919 }
920 
924 #define MIN_VOTES_FOR_PARAM 3
925 
929 STATIC smartlist_t *
930 dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
931 {
932  int i;
933  int32_t *vals;
934 
935  int cur_param_len;
936  const char *cur_param;
937  const char *eq;
938 
939  const int n_votes = smartlist_len(votes);
940  smartlist_t *output;
941  smartlist_t *param_list = smartlist_new();
942  (void) method;
943 
944  /* We require that the parameter lists in the votes are well-formed: that
945  is, that their keywords are unique and sorted, and that their values are
946  between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
947  the parsing code. */
948 
949  vals = tor_calloc(n_votes, sizeof(int));
950 
952  if (!v->net_params)
953  continue;
954  smartlist_add_all(param_list, v->net_params);
955  } SMARTLIST_FOREACH_END(v);
956 
957  if (smartlist_len(param_list) == 0) {
958  tor_free(vals);
959  return param_list;
960  }
961 
962  smartlist_sort_strings(param_list);
963  i = 0;
964  cur_param = smartlist_get(param_list, 0);
965  eq = strchr(cur_param, '=');
966  tor_assert(eq);
967  cur_param_len = (int)(eq+1 - cur_param);
968 
969  output = smartlist_new();
970 
971  SMARTLIST_FOREACH_BEGIN(param_list, const char *, param) {
972  /* resolve spurious clang shallow analysis null pointer errors */
973  tor_assert(param);
974 
975  const char *next_param;
976  int ok=0;
977  eq = strchr(param, '=');
978  tor_assert(i<n_votes); /* Make sure we prevented vote-stuffing. */
979  vals[i++] = (int32_t)
980  tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
981  tor_assert(ok); /* Already checked these when parsing. */
982 
983  if (param_sl_idx+1 == smartlist_len(param_list))
984  next_param = NULL;
985  else
986  next_param = smartlist_get(param_list, param_sl_idx+1);
987 
988  if (!next_param || strncmp(next_param, param, cur_param_len)) {
989  /* We've reached the end of a series. */
990  /* Make sure enough authorities voted on this param, unless the
991  * the consensus method we use is too old for that. */
992  if (i > total_authorities/2 ||
993  i >= MIN_VOTES_FOR_PARAM) {
994  int32_t median = median_int32(vals, i);
995  char *out_string = tor_malloc(64+cur_param_len);
996  memcpy(out_string, param, cur_param_len);
997  tor_snprintf(out_string+cur_param_len,64, "%ld", (long)median);
998  smartlist_add(output, out_string);
999  }
1000 
1001  i = 0;
1002  if (next_param) {
1003  eq = strchr(next_param, '=');
1004  cur_param_len = (int)(eq+1 - next_param);
1005  }
1006  }
1007  } SMARTLIST_FOREACH_END(param);
1008 
1009  smartlist_free(param_list);
1010  tor_free(vals);
1011  return output;
1012 }
1013 
1014 #define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
1015  ((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
1016  (c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
1017  (e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
1018  (g) >= 0 && (g) <= (mx))
1019 
1020 #define CHECK_EQ(a, b, margin) \
1021  ((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
1022 
1023 typedef enum {
1024  BW_WEIGHTS_NO_ERROR = 0,
1025  BW_WEIGHTS_RANGE_ERROR = 1,
1026  BW_WEIGHTS_SUMG_ERROR = 2,
1027  BW_WEIGHTS_SUME_ERROR = 3,
1028  BW_WEIGHTS_SUMD_ERROR = 4,
1029  BW_WEIGHTS_BALANCE_MID_ERROR = 5,
1030  BW_WEIGHTS_BALANCE_EG_ERROR = 6
1031 } bw_weights_error_t;
1032 
1036 static bw_weights_error_t
1037 networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg,
1038  int64_t Wme, int64_t Wmd, int64_t Wee,
1039  int64_t Wed, int64_t scale, int64_t G,
1040  int64_t M, int64_t E, int64_t D, int64_t T,
1041  int64_t margin, int do_balance) {
1042  bw_weights_error_t berr = BW_WEIGHTS_NO_ERROR;
1043 
1044  // Wed + Wmd + Wgd == 1
1045  if (!CHECK_EQ(Wed + Wmd + Wgd, scale, margin)) {
1046  berr = BW_WEIGHTS_SUMD_ERROR;
1047  goto out;
1048  }
1049 
1050  // Wmg + Wgg == 1
1051  if (!CHECK_EQ(Wmg + Wgg, scale, margin)) {
1052  berr = BW_WEIGHTS_SUMG_ERROR;
1053  goto out;
1054  }
1055 
1056  // Wme + Wee == 1
1057  if (!CHECK_EQ(Wme + Wee, scale, margin)) {
1058  berr = BW_WEIGHTS_SUME_ERROR;
1059  goto out;
1060  }
1061 
1062  // Verify weights within range 0->1
1063  if (!RANGE_CHECK(Wgg, Wgd, Wmg, Wme, Wmd, Wed, Wee, scale)) {
1064  berr = BW_WEIGHTS_RANGE_ERROR;
1065  goto out;
1066  }
1067 
1068  if (do_balance) {
1069  // Wgg*G + Wgd*D == Wee*E + Wed*D, already scaled
1070  if (!CHECK_EQ(Wgg*G + Wgd*D, Wee*E + Wed*D, (margin*T)/3)) {
1071  berr = BW_WEIGHTS_BALANCE_EG_ERROR;
1072  goto out;
1073  }
1074 
1075  // Wgg*G + Wgd*D == M*scale + Wmd*D + Wme*E + Wmg*G, already scaled
1076  if (!CHECK_EQ(Wgg*G + Wgd*D, M*scale + Wmd*D + Wme*E + Wmg*G,
1077  (margin*T)/3)) {
1078  berr = BW_WEIGHTS_BALANCE_MID_ERROR;
1079  goto out;
1080  }
1081  }
1082 
1083  out:
1084  if (berr) {
1085  log_info(LD_DIR,
1086  "Bw weight mismatch %d. G=%"PRId64" M=%"PRId64
1087  " E=%"PRId64" D=%"PRId64" T=%"PRId64
1088  " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
1089  " Wgd=%d Wgg=%d Wme=%d Wmg=%d",
1090  berr,
1091  (G), (M), (E),
1092  (D), (T),
1093  (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
1094  (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg);
1095  }
1096 
1097  return berr;
1098 }
1099 
1105 int
1107  int64_t M, int64_t E, int64_t D,
1108  int64_t T, int64_t weight_scale)
1109 {
1110  bw_weights_error_t berr = 0;
1111  int64_t Wgg = -1, Wgd = -1;
1112  int64_t Wmg = -1, Wme = -1, Wmd = -1;
1113  int64_t Wed = -1, Wee = -1;
1114  const char *casename;
1115 
1116  if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
1117  log_warn(LD_DIR, "Consensus with empty bandwidth: "
1118  "G=%"PRId64" M=%"PRId64" E=%"PRId64
1119  " D=%"PRId64" T=%"PRId64,
1120  (G), (M), (E),
1121  (D), (T));
1122  return 0;
1123  }
1124 
1125  /*
1126  * Computed from cases in 3.8.3 of dir-spec.txt
1127  *
1128  * 1. Neither are scarce
1129  * 2. Both Guard and Exit are scarce
1130  * a. R+D <= S
1131  * b. R+D > S
1132  * 3. One of Guard or Exit is scarce
1133  * a. S+D < T/3
1134  * b. S+D >= T/3
1135  */
1136  if (3*E >= T && 3*G >= T) { // E >= T/3 && G >= T/3
1137  /* Case 1: Neither are scarce. */
1138  casename = "Case 1 (Wgd=Wmd=Wed)";
1139  Wgd = weight_scale/3;
1140  Wed = weight_scale/3;
1141  Wmd = weight_scale/3;
1142  Wee = (weight_scale*(E+G+M))/(3*E);
1143  Wme = weight_scale - Wee;
1144  Wmg = (weight_scale*(2*G-E-M))/(3*G);
1145  Wgg = weight_scale - Wmg;
1146 
1147  berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
1148  weight_scale, G, M, E, D, T, 10, 1);
1149 
1150  if (berr) {
1151  log_warn(LD_DIR,
1152  "Bw Weights error %d for %s v10. G=%"PRId64" M=%"PRId64
1153  " E=%"PRId64" D=%"PRId64" T=%"PRId64
1154  " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
1155  " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
1156  berr, casename,
1157  (G), (M), (E),
1158  (D), (T),
1159  (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
1160  (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
1161  return 0;
1162  }
1163  } else if (3*E < T && 3*G < T) { // E < T/3 && G < T/3
1164  int64_t R = MIN(E, G);
1165  int64_t S = MAX(E, G);
1166  /*
1167  * Case 2: Both Guards and Exits are scarce
1168  * Balance D between E and G, depending upon
1169  * D capacity and scarcity.
1170  */
1171  if (R+D < S) { // Subcase a
1172  Wgg = weight_scale;
1173  Wee = weight_scale;
1174  Wmg = 0;
1175  Wme = 0;
1176  Wmd = 0;
1177  if (E < G) {
1178  casename = "Case 2a (E scarce)";
1179  Wed = weight_scale;
1180  Wgd = 0;
1181  } else { /* E >= G */
1182  casename = "Case 2a (G scarce)";
1183  Wed = 0;
1184  Wgd = weight_scale;
1185  }
1186  } else { // Subcase b: R+D >= S
1187  casename = "Case 2b1 (Wgg=weight_scale, Wmd=Wgd)";
1188  Wee = (weight_scale*(E - G + M))/E;
1189  Wed = (weight_scale*(D - 2*E + 4*G - 2*M))/(3*D);
1190  Wme = (weight_scale*(G-M))/E;
1191  Wmg = 0;
1192  Wgg = weight_scale;
1193  Wmd = (weight_scale - Wed)/2;
1194  Wgd = (weight_scale - Wed)/2;
1195 
1196  berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
1197  weight_scale, G, M, E, D, T, 10, 1);
1198 
1199  if (berr) {
1200  casename = "Case 2b2 (Wgg=weight_scale, Wee=weight_scale)";
1201  Wgg = weight_scale;
1202  Wee = weight_scale;
1203  Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
1204  Wmd = (weight_scale*(D - 2*M + G + E))/(3*D);
1205  Wme = 0;
1206  Wmg = 0;
1207 
1208  if (Wmd < 0) { // Can happen if M > T/3
1209  casename = "Case 2b3 (Wmd=0)";
1210  Wmd = 0;
1211  log_warn(LD_DIR,
1212  "Too much Middle bandwidth on the network to calculate "
1213  "balanced bandwidth-weights. Consider increasing the "
1214  "number of Guard nodes by lowering the requirements.");
1215  }
1216  Wgd = weight_scale - Wed - Wmd;
1217  berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
1218  Wed, weight_scale, G, M, E, D, T, 10, 1);
1219  }
1220  if (berr != BW_WEIGHTS_NO_ERROR &&
1221  berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
1222  log_warn(LD_DIR,
1223  "Bw Weights error %d for %s v10. G=%"PRId64" M=%"PRId64
1224  " E=%"PRId64" D=%"PRId64" T=%"PRId64
1225  " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
1226  " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
1227  berr, casename,
1228  (G), (M), (E),
1229  (D), (T),
1230  (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
1231  (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
1232  return 0;
1233  }
1234  }
1235  } else { // if (E < T/3 || G < T/3) {
1236  int64_t S = MIN(E, G);
1237  // Case 3: Exactly one of Guard or Exit is scarce
1238  if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
1239  log_warn(LD_BUG,
1240  "Bw-Weights Case 3 v10 but with G=%"PRId64" M="
1241  "%"PRId64" E=%"PRId64" D=%"PRId64" T=%"PRId64,
1242  (G), (M), (E),
1243  (D), (T));
1244  }
1245 
1246  if (3*(S+D) < T) { // Subcase a: S+D < T/3
1247  if (G < E) {
1248  casename = "Case 3a (G scarce)";
1249  Wgg = Wgd = weight_scale;
1250  Wmd = Wed = Wmg = 0;
1251  // Minor subcase, if E is more scarce than M,
1252  // keep its bandwidth in place.
1253  if (E < M) Wme = 0;
1254  else Wme = (weight_scale*(E-M))/(2*E);
1255  Wee = weight_scale-Wme;
1256  } else { // G >= E
1257  casename = "Case 3a (E scarce)";
1258  Wee = Wed = weight_scale;
1259  Wmd = Wgd = Wme = 0;
1260  // Minor subcase, if G is more scarce than M,
1261  // keep its bandwidth in place.
1262  if (G < M) Wmg = 0;
1263  else Wmg = (weight_scale*(G-M))/(2*G);
1264  Wgg = weight_scale-Wmg;
1265  }
1266  } else { // Subcase b: S+D >= T/3
1267  // D != 0 because S+D >= T/3
1268  if (G < E) {
1269  casename = "Case 3bg (G scarce, Wgg=weight_scale, Wmd == Wed)";
1270  Wgg = weight_scale;
1271  Wgd = (weight_scale*(D - 2*G + E + M))/(3*D);
1272  Wmg = 0;
1273  Wee = (weight_scale*(E+M))/(2*E);
1274  Wme = weight_scale - Wee;
1275  Wmd = (weight_scale - Wgd)/2;
1276  Wed = (weight_scale - Wgd)/2;
1277 
1278  berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
1279  Wed, weight_scale, G, M, E, D, T, 10, 1);
1280  } else { // G >= E
1281  casename = "Case 3be (E scarce, Wee=weight_scale, Wmd == Wgd)";
1282  Wee = weight_scale;
1283  Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
1284  Wme = 0;
1285  Wgg = (weight_scale*(G+M))/(2*G);
1286  Wmg = weight_scale - Wgg;
1287  Wmd = (weight_scale - Wed)/2;
1288  Wgd = (weight_scale - Wed)/2;
1289 
1290  berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
1291  Wed, weight_scale, G, M, E, D, T, 10, 1);
1292  }
1293  if (berr) {
1294  log_warn(LD_DIR,
1295  "Bw Weights error %d for %s v10. G=%"PRId64" M=%"PRId64
1296  " E=%"PRId64" D=%"PRId64" T=%"PRId64
1297  " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
1298  " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
1299  berr, casename,
1300  (G), (M), (E),
1301  (D), (T),
1302  (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
1303  (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
1304  return 0;
1305  }
1306  }
1307  }
1308 
1309  /* We cast down the weights to 32 bit ints on the assumption that
1310  * weight_scale is ~= 10000. We need to ensure a rogue authority
1311  * doesn't break this assumption to rig our weights */
1312  tor_assert(0 < weight_scale && weight_scale <= INT32_MAX);
1313 
1314  /*
1315  * Provide Wgm=Wgg, Wmm=weight_scale, Wem=Wee, Weg=Wed. May later determine
1316  * that middle nodes need different bandwidth weights for dirport traffic,
1317  * or that weird exit policies need special weight, or that bridges
1318  * need special weight.
1319  *
1320  * NOTE: This list is sorted.
1321  */
1322  smartlist_add_asprintf(chunks,
1323  "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
1324  "Wdb=%d "
1325  "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
1326  "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
1327  "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
1328  (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
1329  (int)weight_scale,
1330  (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
1331  (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
1332  (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
1333 
1334  log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: "
1335  "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
1336  " T=%"PRId64,
1337  casename,
1338  (G), (M), (E),
1339  (D), (T));
1340  return 1;
1341 }
1342 
1345 static void
1347  int is_exit, int is_guard,
1348  int64_t *G, int64_t *M, int64_t *E, int64_t *D,
1349  int64_t *T)
1350 {
1351  int default_bandwidth = rs->bandwidth_kb;
1352  int guardfraction_bandwidth = 0;
1353 
1354  if (!rs->has_bandwidth) {
1355  log_info(LD_BUG, "Missing consensus bandwidth for router %s",
1356  rs->nickname);
1357  return;
1358  }
1359 
1360  /* If this routerstatus represents a guard that we have
1361  * guardfraction information on, use it to calculate its actual
1362  * bandwidth. From proposal236:
1363  *
1364  * Similarly, when calculating the bandwidth-weights line as in
1365  * section 3.8.3 of dir-spec.txt, directory authorities should treat N
1366  * as if fraction F of its bandwidth has the guard flag and (1-F) does
1367  * not. So when computing the totals G,M,E,D, each relay N with guard
1368  * visibility fraction F and bandwidth B should be added as follows:
1369  *
1370  * G' = G + F*B, if N does not have the exit flag
1371  * M' = M + (1-F)*B, if N does not have the exit flag
1372  *
1373  * or
1374  *
1375  * D' = D + F*B, if N has the exit flag
1376  * E' = E + (1-F)*B, if N has the exit flag
1377  *
1378  * In this block of code, we prepare the bandwidth values by setting
1379  * the default_bandwidth to F*B and guardfraction_bandwidth to (1-F)*B.
1380  */
1381  if (rs->has_guardfraction) {
1382  guardfraction_bandwidth_t guardfraction_bw;
1383 
1384  tor_assert(is_guard);
1385 
1386  guard_get_guardfraction_bandwidth(&guardfraction_bw,
1387  rs->bandwidth_kb,
1389 
1390  default_bandwidth = guardfraction_bw.guard_bw;
1391  guardfraction_bandwidth = guardfraction_bw.non_guard_bw;
1392  }
1393 
1394  /* Now calculate the total bandwidth weights with or without
1395  * guardfraction. Depending on the flags of the relay, add its
1396  * bandwidth to the appropriate weight pool. If it's a guard and
1397  * guardfraction is enabled, add its bandwidth to both pools as
1398  * indicated by the previous comment.
1399  */
1400  *T += default_bandwidth;
1401  if (is_exit && is_guard) {
1402 
1403  *D += default_bandwidth;
1404  if (rs->has_guardfraction) {
1405  *E += guardfraction_bandwidth;
1406  }
1407 
1408  } else if (is_exit) {
1409 
1410  *E += default_bandwidth;
1411 
1412  } else if (is_guard) {
1413 
1414  *G += default_bandwidth;
1415  if (rs->has_guardfraction) {
1416  *M += guardfraction_bandwidth;
1417  }
1418 
1419  } else {
1420 
1421  *M += default_bandwidth;
1422  }
1423 }
1424 
1429 static const char *
1431 {
1432  switch (n) {
1433  case 0: return vote->recommended_client_protocols;
1434  case 1: return vote->recommended_relay_protocols;
1435  case 2: return vote->required_client_protocols;
1436  case 3: return vote->required_relay_protocols;
1437  default:
1438  tor_assert_unreached();
1439  return NULL;
1440  }
1441 }
1442 
1447 static char *
1448 compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes)
1449 {
1450  const char *keyword;
1451  smartlist_t *proto_votes = smartlist_new();
1452  int threshold;
1453  switch (n) {
1454  case 0:
1455  keyword = "recommended-client-protocols";
1456  threshold = CEIL_DIV(n_voters, 2);
1457  break;
1458  case 1:
1459  keyword = "recommended-relay-protocols";
1460  threshold = CEIL_DIV(n_voters, 2);
1461  break;
1462  case 2:
1463  keyword = "required-client-protocols";
1464  threshold = CEIL_DIV(n_voters * 2, 3);
1465  break;
1466  case 3:
1467  keyword = "required-relay-protocols";
1468  threshold = CEIL_DIV(n_voters * 2, 3);
1469  break;
1470  default:
1471  tor_assert_unreached();
1472  return NULL;
1473  }
1474 
1475  SMARTLIST_FOREACH_BEGIN(votes, const networkstatus_t *, ns) {
1476  const char *v = get_nth_protocol_set_vote(n, ns);
1477  if (v)
1478  smartlist_add(proto_votes, (void*)v);
1479  } SMARTLIST_FOREACH_END(ns);
1480 
1481  char *protocols = protover_compute_vote(proto_votes, threshold);
1482  smartlist_free(proto_votes);
1483 
1484  char *result = NULL;
1485  tor_asprintf(&result, "%s %s\n", keyword, protocols);
1486  tor_free(protocols);
1487 
1488  return result;
1489 }
1490 
1510 STATIC char *
1512  int total_authorities,
1513  crypto_pk_t *identity_key,
1514  crypto_pk_t *signing_key,
1515  const char *legacy_id_key_digest,
1517  consensus_flavor_t flavor)
1518 {
1519  smartlist_t *chunks;
1520  char *result = NULL;
1521  int consensus_method;
1522  time_t valid_after, fresh_until, valid_until;
1523  int vote_seconds, dist_seconds;
1524  char *client_versions = NULL, *server_versions = NULL;
1525  smartlist_t *flags;
1526  const char *flavor_name;
1527  uint32_t max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
1528  int64_t G, M, E, D, T; /* For bandwidth weights */
1529  const routerstatus_format_type_t rs_format =
1530  flavor == FLAV_NS ? NS_V3_CONSENSUS : NS_V3_CONSENSUS_MICRODESC;
1531  char *params = NULL;
1532  char *packages = NULL;
1533  int added_weights = 0;
1534  dircollator_t *collator = NULL;
1535  smartlist_t *param_list = NULL;
1536 
1537  tor_assert(flavor == FLAV_NS || flavor == FLAV_MICRODESC);
1538  tor_assert(total_authorities >= smartlist_len(votes));
1539  tor_assert(total_authorities > 0);
1540 
1541  flavor_name = networkstatus_get_flavor_name(flavor);
1542 
1543  if (!smartlist_len(votes)) {
1544  log_warn(LD_DIR, "Can't compute a consensus from no votes.");
1545  return NULL;
1546  }
1547  flags = smartlist_new();
1548 
1549  consensus_method = compute_consensus_method(votes);
1550  if (consensus_method_is_supported(consensus_method)) {
1551  log_info(LD_DIR, "Generating consensus using method %d.",
1552  consensus_method);
1553  } else {
1554  log_warn(LD_DIR, "The other authorities will use consensus method %d, "
1555  "which I don't support. Maybe I should upgrade!",
1556  consensus_method);
1557  consensus_method = MAX_SUPPORTED_CONSENSUS_METHOD;
1558  }
1559 
1560  if (consensus_method >= MIN_METHOD_FOR_INIT_BW_WEIGHTS_ONE) {
1561  /* It's smarter to initialize these weights to 1, so that later on,
1562  * we can't accidentally divide by zero. */
1563  G = M = E = D = 1;
1564  T = 4;
1565  } else {
1566  /* ...but originally, they were set to zero. */
1567  G = M = E = D = T = 0;
1568  }
1569 
1570  /* Compute medians of time-related things, and figure out how many
1571  * routers we might need to talk about. */
1572  {
1573  int n_votes = smartlist_len(votes);
1574  time_t *va_times = tor_calloc(n_votes, sizeof(time_t));
1575  time_t *fu_times = tor_calloc(n_votes, sizeof(time_t));
1576  time_t *vu_times = tor_calloc(n_votes, sizeof(time_t));
1577  int *votesec_list = tor_calloc(n_votes, sizeof(int));
1578  int *distsec_list = tor_calloc(n_votes, sizeof(int));
1579  int n_versioning_clients = 0, n_versioning_servers = 0;
1580  smartlist_t *combined_client_versions = smartlist_new();
1581  smartlist_t *combined_server_versions = smartlist_new();
1582 
1584  tor_assert(v->type == NS_TYPE_VOTE);
1585  va_times[v_sl_idx] = v->valid_after;
1586  fu_times[v_sl_idx] = v->fresh_until;
1587  vu_times[v_sl_idx] = v->valid_until;
1588  votesec_list[v_sl_idx] = v->vote_seconds;
1589  distsec_list[v_sl_idx] = v->dist_seconds;
1590  if (v->client_versions) {
1591  smartlist_t *cv = smartlist_new();
1592  ++n_versioning_clients;
1593  smartlist_split_string(cv, v->client_versions, ",",
1594  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1595  sort_version_list(cv, 1);
1596  smartlist_add_all(combined_client_versions, cv);
1597  smartlist_free(cv); /* elements get freed later. */
1598  }
1599  if (v->server_versions) {
1600  smartlist_t *sv = smartlist_new();
1601  ++n_versioning_servers;
1602  smartlist_split_string(sv, v->server_versions, ",",
1603  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1604  sort_version_list(sv, 1);
1605  smartlist_add_all(combined_server_versions, sv);
1606  smartlist_free(sv); /* elements get freed later. */
1607  }
1608  SMARTLIST_FOREACH(v->known_flags, const char *, cp,
1609  smartlist_add_strdup(flags, cp));
1610  } SMARTLIST_FOREACH_END(v);
1611  valid_after = median_time(va_times, n_votes);
1612  fresh_until = median_time(fu_times, n_votes);
1613  valid_until = median_time(vu_times, n_votes);
1614  vote_seconds = median_int(votesec_list, n_votes);
1615  dist_seconds = median_int(distsec_list, n_votes);
1616 
1617  tor_assert(valid_after +
1618  (get_options()->TestingTorNetwork ?
1619  MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= fresh_until);
1620  tor_assert(fresh_until +
1621  (get_options()->TestingTorNetwork ?
1622  MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= valid_until);
1623  tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
1624  tor_assert(dist_seconds >= MIN_DIST_SECONDS);
1625 
1626  server_versions = compute_consensus_versions_list(combined_server_versions,
1627  n_versioning_servers);
1628  client_versions = compute_consensus_versions_list(combined_client_versions,
1629  n_versioning_clients);
1630  packages = compute_consensus_package_lines(votes);
1631 
1632  SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
1633  SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
1634  smartlist_free(combined_server_versions);
1635  smartlist_free(combined_client_versions);
1636 
1637  smartlist_add_strdup(flags, "NoEdConsensus");
1638 
1639  smartlist_sort_strings(flags);
1640  smartlist_uniq_strings(flags);
1641 
1642  tor_free(va_times);
1643  tor_free(fu_times);
1644  tor_free(vu_times);
1645  tor_free(votesec_list);
1646  tor_free(distsec_list);
1647  }
1648 
1649  chunks = smartlist_new();
1650 
1651  {
1652  char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
1653  vu_buf[ISO_TIME_LEN+1];
1654  char *flaglist;
1655  format_iso_time(va_buf, valid_after);
1656  format_iso_time(fu_buf, fresh_until);
1657  format_iso_time(vu_buf, valid_until);
1658  flaglist = smartlist_join_strings(flags, " ", 0, NULL);
1659 
1660  smartlist_add_asprintf(chunks, "network-status-version 3%s%s\n"
1661  "vote-status consensus\n",
1662  flavor == FLAV_NS ? "" : " ",
1663  flavor == FLAV_NS ? "" : flavor_name);
1664 
1665  smartlist_add_asprintf(chunks, "consensus-method %d\n",
1666  consensus_method);
1667 
1668  smartlist_add_asprintf(chunks,
1669  "valid-after %s\n"
1670  "fresh-until %s\n"
1671  "valid-until %s\n"
1672  "voting-delay %d %d\n"
1673  "client-versions %s\n"
1674  "server-versions %s\n"
1675  "%s" /* packages */
1676  "known-flags %s\n",
1677  va_buf, fu_buf, vu_buf,
1678  vote_seconds, dist_seconds,
1679  client_versions, server_versions,
1680  packages,
1681  flaglist);
1682 
1683  tor_free(flaglist);
1684  }
1685 
1686  {
1687  int num_dirauth = get_n_authorities(V3_DIRINFO);
1688  int idx;
1689  for (idx = 0; idx < 4; ++idx) {
1690  char *proto_line = compute_nth_protocol_set(idx, num_dirauth, votes);
1691  if (BUG(!proto_line))
1692  continue;
1693  smartlist_add(chunks, proto_line);
1694  }
1695  }
1696 
1697  param_list = dirvote_compute_params(votes, consensus_method,
1698  total_authorities);
1699  if (smartlist_len(param_list)) {
1700  params = smartlist_join_strings(param_list, " ", 0, NULL);
1701  smartlist_add_strdup(chunks, "params ");
1702  smartlist_add(chunks, params);
1703  smartlist_add_strdup(chunks, "\n");
1704  }
1705 
1706  {
1707  int num_dirauth = get_n_authorities(V3_DIRINFO);
1708  /* Default value of this is 2/3 of the total number of authorities. For
1709  * instance, if we have 9 dirauth, the default value is 6. The following
1710  * calculation will round it down. */
1711  int32_t num_srv_agreements =
1713  "AuthDirNumSRVAgreements",
1714  (num_dirauth * 2) / 3);
1715  /* Add the shared random value. */
1716  char *srv_lines = sr_get_string_for_consensus(votes, num_srv_agreements);
1717  if (srv_lines != NULL) {
1718  smartlist_add(chunks, srv_lines);
1719  }
1720  }
1721 
1722  /* Sort the votes. */
1724  /* Add the authority sections. */
1725  {
1726  smartlist_t *dir_sources = smartlist_new();
1728  dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
1729  e->v = v;
1730  e->digest = get_voter(v)->identity_digest;
1731  e->is_legacy = 0;
1732  smartlist_add(dir_sources, e);
1733  if (!tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
1734  dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
1735  e_legacy->v = v;
1736  e_legacy->digest = get_voter(v)->legacy_id_digest;
1737  e_legacy->is_legacy = 1;
1738  smartlist_add(dir_sources, e_legacy);
1739  }
1740  } SMARTLIST_FOREACH_END(v);
1742 
1743  SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
1744  char fingerprint[HEX_DIGEST_LEN+1];
1745  char votedigest[HEX_DIGEST_LEN+1];
1746  networkstatus_t *v = e->v;
1748 
1749  base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
1750  base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
1751  DIGEST_LEN);
1752 
1753  smartlist_add_asprintf(chunks,
1754  "dir-source %s%s %s %s %s %d %d\n",
1755  voter->nickname, e->is_legacy ? "-legacy" : "",
1756  fingerprint, voter->address, fmt_addr32(voter->addr),
1757  voter->dir_port,
1758  voter->or_port);
1759  if (! e->is_legacy) {
1760  smartlist_add_asprintf(chunks,
1761  "contact %s\n"
1762  "vote-digest %s\n",
1763  voter->contact,
1764  votedigest);
1765  }
1766  } SMARTLIST_FOREACH_END(e);
1767  SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
1768  smartlist_free(dir_sources);
1769  }
1770 
1771  {
1772  char *max_unmeasured_param = NULL;
1773  /* XXXX Extract this code into a common function. Or don't! see #19011 */
1774  if (params) {
1775  if (strcmpstart(params, "maxunmeasuredbw=") == 0)
1776  max_unmeasured_param = params;
1777  else
1778  max_unmeasured_param = strstr(params, " maxunmeasuredbw=");
1779  }
1780  if (max_unmeasured_param) {
1781  int ok = 0;
1782  char *eq = strchr(max_unmeasured_param, '=');
1783  if (eq) {
1784  max_unmeasured_bw_kb = (uint32_t)
1785  tor_parse_ulong(eq+1, 10, 1, UINT32_MAX, &ok, NULL);
1786  if (!ok) {
1787  log_warn(LD_DIR, "Bad element '%s' in max unmeasured bw param",
1788  escaped(max_unmeasured_param));
1789  max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
1790  }
1791  }
1792  }
1793  }
1794 
1795  /* Add the actual router entries. */
1796  {
1797  int *size; /* size[j] is the number of routerstatuses in votes[j]. */
1798  int *flag_counts; /* The number of voters that list flag[j] for the
1799  * currently considered router. */
1800  int i;
1801  smartlist_t *matching_descs = smartlist_new();
1802  smartlist_t *chosen_flags = smartlist_new();
1803  smartlist_t *versions = smartlist_new();
1804  smartlist_t *protocols = smartlist_new();
1805  smartlist_t *exitsummaries = smartlist_new();
1806  uint32_t *bandwidths_kb = tor_calloc(smartlist_len(votes),
1807  sizeof(uint32_t));
1808  uint32_t *measured_bws_kb = tor_calloc(smartlist_len(votes),
1809  sizeof(uint32_t));
1810  uint32_t *measured_guardfraction = tor_calloc(smartlist_len(votes),
1811  sizeof(uint32_t));
1812  int num_bandwidths;
1813  int num_mbws;
1814  int num_guardfraction_inputs;
1815 
1816  int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
1817  * votes[j] knows about. */
1818  int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
1819  * about flags[f]. */
1820  int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
1821  * is the same flag as votes[j]->known_flags[b]. */
1822  int *named_flag; /* Index of the flag "Named" for votes[j] */
1823  int *unnamed_flag; /* Index of the flag "Unnamed" for votes[j] */
1824  int n_authorities_measuring_bandwidth;
1825 
1826  strmap_t *name_to_id_map = strmap_new();
1827  char conflict[DIGEST_LEN];
1828  char unknown[DIGEST_LEN];
1829  memset(conflict, 0, sizeof(conflict));
1830  memset(unknown, 0xff, sizeof(conflict));
1831 
1832  size = tor_calloc(smartlist_len(votes), sizeof(int));
1833  n_voter_flags = tor_calloc(smartlist_len(votes), sizeof(int));
1834  n_flag_voters = tor_calloc(smartlist_len(flags), sizeof(int));
1835  flag_map = tor_calloc(smartlist_len(votes), sizeof(int *));
1836  named_flag = tor_calloc(smartlist_len(votes), sizeof(int));
1837  unnamed_flag = tor_calloc(smartlist_len(votes), sizeof(int));
1838  for (i = 0; i < smartlist_len(votes); ++i)
1839  unnamed_flag[i] = named_flag[i] = -1;
1840 
1841  /* Build the flag indexes. Note that no vote can have more than 64 members
1842  * for known_flags, so no value will be greater than 63, so it's safe to
1843  * do UINT64_C(1) << index on these values. But note also that
1844  * named_flag and unnamed_flag are initialized to -1, so we need to check
1845  * that they're actually set before doing UINT64_C(1) << index with
1846  * them.*/
1848  flag_map[v_sl_idx] = tor_calloc(smartlist_len(v->known_flags),
1849  sizeof(int));
1850  if (smartlist_len(v->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
1851  log_warn(LD_BUG, "Somehow, a vote has %d entries in known_flags",
1852  smartlist_len(v->known_flags));
1853  }
1854  SMARTLIST_FOREACH_BEGIN(v->known_flags, const char *, fl) {
1855  int p = smartlist_string_pos(flags, fl);
1856  tor_assert(p >= 0);
1857  flag_map[v_sl_idx][fl_sl_idx] = p;
1858  ++n_flag_voters[p];
1859  if (!strcmp(fl, "Named"))
1860  named_flag[v_sl_idx] = fl_sl_idx;
1861  if (!strcmp(fl, "Unnamed"))
1862  unnamed_flag[v_sl_idx] = fl_sl_idx;
1863  } SMARTLIST_FOREACH_END(fl);
1864  n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
1865  size[v_sl_idx] = smartlist_len(v->routerstatus_list);
1866  } SMARTLIST_FOREACH_END(v);
1867 
1868  /* Named and Unnamed get treated specially */
1869  {
1871  uint64_t nf;
1872  if (named_flag[v_sl_idx]<0)
1873  continue;
1874  nf = UINT64_C(1) << named_flag[v_sl_idx];
1875  SMARTLIST_FOREACH_BEGIN(v->routerstatus_list,
1876  vote_routerstatus_t *, rs) {
1877 
1878  if ((rs->flags & nf) != 0) {
1879  const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
1880  if (!d) {
1881  /* We have no name officially mapped to this digest. */
1882  strmap_set_lc(name_to_id_map, rs->status.nickname,
1883  rs->status.identity_digest);
1884  } else if (d != conflict &&
1885  fast_memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
1886  /* Authorities disagree about this nickname. */
1887  strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
1888  } else {
1889  /* It's already a conflict, or it's already this ID. */
1890  }
1891  }
1892  } SMARTLIST_FOREACH_END(rs);
1893  } SMARTLIST_FOREACH_END(v);
1894 
1896  uint64_t uf;
1897  if (unnamed_flag[v_sl_idx]<0)
1898  continue;
1899  uf = UINT64_C(1) << unnamed_flag[v_sl_idx];
1900  SMARTLIST_FOREACH_BEGIN(v->routerstatus_list,
1901  vote_routerstatus_t *, rs) {
1902  if ((rs->flags & uf) != 0) {
1903  const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
1904  if (d == conflict || d == unknown) {
1905  /* Leave it alone; we know what it is. */
1906  } else if (!d) {
1907  /* We have no name officially mapped to this digest. */
1908  strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
1909  } else if (fast_memeq(d, rs->status.identity_digest, DIGEST_LEN)) {
1910  /* Authorities disagree about this nickname. */
1911  strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
1912  } else {
1913  /* It's mapped to a different name. */
1914  }
1915  }
1916  } SMARTLIST_FOREACH_END(rs);
1917  } SMARTLIST_FOREACH_END(v);
1918  }
1919 
1920  /* We need to know how many votes measure bandwidth. */
1921  n_authorities_measuring_bandwidth = 0;
1922  SMARTLIST_FOREACH(votes, const networkstatus_t *, v,
1923  if (v->has_measured_bws) {
1924  ++n_authorities_measuring_bandwidth;
1925  }
1926  );
1927 
1928  /* Populate the collator */
1929  collator = dircollator_new(smartlist_len(votes), total_authorities);
1931  dircollator_add_vote(collator, v);
1932  } SMARTLIST_FOREACH_END(v);
1933 
1934  dircollator_collate(collator, consensus_method);
1935 
1936  /* Now go through all the votes */
1937  flag_counts = tor_calloc(smartlist_len(flags), sizeof(int));
1938  const int num_routers = dircollator_n_routers(collator);
1939  for (i = 0; i < num_routers; ++i) {
1940  vote_routerstatus_t **vrs_lst =
1941  dircollator_get_votes_for_router(collator, i);
1942 
1943  vote_routerstatus_t *rs;
1944  routerstatus_t rs_out;
1945  const char *current_rsa_id = NULL;
1946  const char *chosen_version;
1947  const char *chosen_protocol_list;
1948  const char *chosen_name = NULL;
1949  int exitsummary_disagreement = 0;
1950  int is_named = 0, is_unnamed = 0, is_running = 0, is_valid = 0;
1951  int is_guard = 0, is_exit = 0, is_bad_exit = 0;
1952  int naming_conflict = 0;
1953  int n_listing = 0;
1954  char microdesc_digest[DIGEST256_LEN];
1955  tor_addr_port_t alt_orport = {TOR_ADDR_NULL, 0};
1956 
1957  memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
1958  smartlist_clear(matching_descs);
1959  smartlist_clear(chosen_flags);
1960  smartlist_clear(versions);
1961  smartlist_clear(protocols);
1962  num_bandwidths = 0;
1963  num_mbws = 0;
1964  num_guardfraction_inputs = 0;
1965  int ed_consensus = 0;
1966  const uint8_t *ed_consensus_val = NULL;
1967 
1968  /* Okay, go through all the entries for this digest. */
1969  for (int voter_idx = 0; voter_idx < smartlist_len(votes); ++voter_idx) {
1970  if (vrs_lst[voter_idx] == NULL)
1971  continue; /* This voter had nothing to say about this entry. */
1972  rs = vrs_lst[voter_idx];
1973  ++n_listing;
1974 
1975  current_rsa_id = rs->status.identity_digest;
1976 
1977  smartlist_add(matching_descs, rs);
1978  if (rs->version && rs->version[0])
1979  smartlist_add(versions, rs->version);
1980 
1981  if (rs->protocols) {
1982  /* We include this one even if it's empty: voting for an
1983  * empty protocol list actually is meaningful. */
1984  smartlist_add(protocols, rs->protocols);
1985  }
1986 
1987  /* Tally up all the flags. */
1988  for (int flag = 0; flag < n_voter_flags[voter_idx]; ++flag) {
1989  if (rs->flags & (UINT64_C(1) << flag))
1990  ++flag_counts[flag_map[voter_idx][flag]];
1991  }
1992  if (named_flag[voter_idx] >= 0 &&
1993  (rs->flags & (UINT64_C(1) << named_flag[voter_idx]))) {
1994  if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
1995  log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
1996  chosen_name, rs->status.nickname);
1997  naming_conflict = 1;
1998  }
1999  chosen_name = rs->status.nickname;
2000  }
2001 
2002  /* Count guardfraction votes and note down the values. */
2003  if (rs->status.has_guardfraction) {
2004  measured_guardfraction[num_guardfraction_inputs++] =
2006  }
2007 
2008  /* count bandwidths */
2009  if (rs->has_measured_bw)
2010  measured_bws_kb[num_mbws++] = rs->measured_bw_kb;
2011 
2012  if (rs->status.has_bandwidth)
2013  bandwidths_kb[num_bandwidths++] = rs->status.bandwidth_kb;
2014 
2015  /* Count number for which ed25519 is canonical. */
2016  if (rs->ed25519_reflects_consensus) {
2017  ++ed_consensus;
2018  if (ed_consensus_val) {
2019  tor_assert(fast_memeq(ed_consensus_val, rs->ed25519_id,
2020  ED25519_PUBKEY_LEN));
2021  } else {
2022  ed_consensus_val = rs->ed25519_id;
2023  }
2024  }
2025  }
2026 
2027  /* We don't include this router at all unless more than half of
2028  * the authorities we believe in list it. */
2029  if (n_listing <= total_authorities/2)
2030  continue;
2031 
2032  if (ed_consensus > 0) {
2033  if (ed_consensus <= total_authorities / 2) {
2034  log_warn(LD_BUG, "Not enough entries had ed_consensus set; how "
2035  "can we have a consensus of %d?", ed_consensus);
2036  }
2037  }
2038 
2039  /* The clangalyzer can't figure out that this will never be NULL
2040  * if n_listing is at least 1 */
2041  tor_assert(current_rsa_id);
2042 
2043  /* Figure out the most popular opinion of what the most recent
2044  * routerinfo and its contents are. */
2045  memset(microdesc_digest, 0, sizeof(microdesc_digest));
2046  rs = compute_routerstatus_consensus(matching_descs, consensus_method,
2047  microdesc_digest, &alt_orport);
2048  /* Copy bits of that into rs_out. */
2049  memset(&rs_out, 0, sizeof(rs_out));
2050  tor_assert(fast_memeq(current_rsa_id,
2052  memcpy(rs_out.identity_digest, current_rsa_id, DIGEST_LEN);
2053  memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
2054  DIGEST_LEN);
2055  rs_out.addr = rs->status.addr;
2056  rs_out.published_on = rs->status.published_on;
2057  rs_out.dir_port = rs->status.dir_port;
2058  rs_out.or_port = rs->status.or_port;
2059  tor_addr_copy(&rs_out.ipv6_addr, &alt_orport.addr);
2060  rs_out.ipv6_orport = alt_orport.port;
2061  rs_out.has_bandwidth = 0;
2062  rs_out.has_exitsummary = 0;
2063 
2064  if (chosen_name && !naming_conflict) {
2065  strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
2066  } else {
2067  strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
2068  }
2069 
2070  {
2071  const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
2072  if (!d) {
2073  is_named = is_unnamed = 0;
2074  } else if (fast_memeq(d, current_rsa_id, DIGEST_LEN)) {
2075  is_named = 1; is_unnamed = 0;
2076  } else {
2077  is_named = 0; is_unnamed = 1;
2078  }
2079  }
2080 
2081  /* Set the flags. */
2082  smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
2083  SMARTLIST_FOREACH_BEGIN(flags, const char *, fl) {
2084  if (!strcmp(fl, "Named")) {
2085  if (is_named)
2086  smartlist_add(chosen_flags, (char*)fl);
2087  } else if (!strcmp(fl, "Unnamed")) {
2088  if (is_unnamed)
2089  smartlist_add(chosen_flags, (char*)fl);
2090  } else if (!strcmp(fl, "NoEdConsensus")) {
2091  if (ed_consensus <= total_authorities/2)
2092  smartlist_add(chosen_flags, (char*)fl);
2093  } else {
2094  if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
2095  smartlist_add(chosen_flags, (char*)fl);
2096  if (!strcmp(fl, "Exit"))
2097  is_exit = 1;
2098  else if (!strcmp(fl, "Guard"))
2099  is_guard = 1;
2100  else if (!strcmp(fl, "Running"))
2101  is_running = 1;
2102  else if (!strcmp(fl, "BadExit"))
2103  is_bad_exit = 1;
2104  else if (!strcmp(fl, "Valid"))
2105  is_valid = 1;
2106  }
2107  }
2108  } SMARTLIST_FOREACH_END(fl);
2109 
2110  /* Starting with consensus method 4 we do not list servers
2111  * that are not running in a consensus. See Proposal 138 */
2112  if (!is_running)
2113  continue;
2114 
2115  /* Starting with consensus method 24, we don't list servers
2116  * that are not valid in a consensus. See Proposal 272 */
2117  if (!is_valid)
2118  continue;
2119 
2120  /* Pick the version. */
2121  if (smartlist_len(versions)) {
2122  sort_version_list(versions, 0);
2123  chosen_version = get_most_frequent_member(versions);
2124  } else {
2125  chosen_version = NULL;
2126  }
2127 
2128  /* Pick the protocol list */
2129  if (smartlist_len(protocols)) {
2130  smartlist_sort_strings(protocols);
2131  chosen_protocol_list = get_most_frequent_member(protocols);
2132  } else {
2133  chosen_protocol_list = NULL;
2134  }
2135 
2136  /* If it's a guard and we have enough guardfraction votes,
2137  calculate its consensus guardfraction value. */
2138  if (is_guard && num_guardfraction_inputs > 2) {
2139  rs_out.has_guardfraction = 1;
2140  rs_out.guardfraction_percentage = median_uint32(measured_guardfraction,
2141  num_guardfraction_inputs);
2142  /* final value should be an integer percentage! */
2143  tor_assert(rs_out.guardfraction_percentage <= 100);
2144  }
2145 
2146  /* Pick a bandwidth */
2147  if (num_mbws > 2) {
2148  rs_out.has_bandwidth = 1;
2149  rs_out.bw_is_unmeasured = 0;
2150  rs_out.bandwidth_kb = median_uint32(measured_bws_kb, num_mbws);
2151  } else if (num_bandwidths > 0) {
2152  rs_out.has_bandwidth = 1;
2153  rs_out.bw_is_unmeasured = 1;
2154  rs_out.bandwidth_kb = median_uint32(bandwidths_kb, num_bandwidths);
2155  if (n_authorities_measuring_bandwidth > 2) {
2156  /* Cap non-measured bandwidths. */
2157  if (rs_out.bandwidth_kb > max_unmeasured_bw_kb) {
2158  rs_out.bandwidth_kb = max_unmeasured_bw_kb;
2159  }
2160  }
2161  }
2162 
2163  /* Fix bug 2203: Do not count BadExit nodes as Exits for bw weights */
2164  is_exit = is_exit && !is_bad_exit;
2165 
2166  /* Update total bandwidth weights with the bandwidths of this router. */
2167  {
2169  is_exit, is_guard,
2170  &G, &M, &E, &D, &T);
2171  }
2172 
2173  /* Ok, we already picked a descriptor digest we want to list
2174  * previously. Now we want to use the exit policy summary from
2175  * that descriptor. If everybody plays nice all the voters who
2176  * listed that descriptor will have the same summary. If not then
2177  * something is fishy and we'll use the most common one (breaking
2178  * ties in favor of lexicographically larger one (only because it
2179  * lets me reuse more existing code)).
2180  *
2181  * The other case that can happen is that no authority that voted
2182  * for that descriptor has an exit policy summary. That's
2183  * probably quite unlikely but can happen. In that case we use
2184  * the policy that was most often listed in votes, again breaking
2185  * ties like in the previous case.
2186  */
2187  {
2188  /* Okay, go through all the votes for this router. We prepared
2189  * that list previously */
2190  const char *chosen_exitsummary = NULL;
2191  smartlist_clear(exitsummaries);
2192  SMARTLIST_FOREACH_BEGIN(matching_descs, vote_routerstatus_t *, vsr) {
2193  /* Check if the vote where this status comes from had the
2194  * proper descriptor */
2195  tor_assert(fast_memeq(rs_out.identity_digest,
2196  vsr->status.identity_digest,
2197  DIGEST_LEN));
2198  if (vsr->status.has_exitsummary &&
2199  fast_memeq(rs_out.descriptor_digest,
2200  vsr->status.descriptor_digest,
2201  DIGEST_LEN)) {
2202  tor_assert(vsr->status.exitsummary);
2203  smartlist_add(exitsummaries, vsr->status.exitsummary);
2204  if (!chosen_exitsummary) {
2205  chosen_exitsummary = vsr->status.exitsummary;
2206  } else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
2207  /* Great. There's disagreement among the voters. That
2208  * really shouldn't be */
2209  exitsummary_disagreement = 1;
2210  }
2211  }
2212  } SMARTLIST_FOREACH_END(vsr);
2213 
2214  if (exitsummary_disagreement) {
2215  char id[HEX_DIGEST_LEN+1];
2216  char dd[HEX_DIGEST_LEN+1];
2217  base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
2218  base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
2219  log_warn(LD_DIR, "The voters disagreed on the exit policy summary "
2220  " for router %s with descriptor %s. This really shouldn't"
2221  " have happened.", id, dd);
2222 
2223  smartlist_sort_strings(exitsummaries);
2224  chosen_exitsummary = get_most_frequent_member(exitsummaries);
2225  } else if (!chosen_exitsummary) {
2226  char id[HEX_DIGEST_LEN+1];
2227  char dd[HEX_DIGEST_LEN+1];
2228  base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
2229  base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
2230  log_warn(LD_DIR, "Not one of the voters that made us select"
2231  "descriptor %s for router %s had an exit policy"
2232  "summary", dd, id);
2233 
2234  /* Ok, none of those voting for the digest we chose had an
2235  * exit policy for us. Well, that kinda sucks.
2236  */
2237  smartlist_clear(exitsummaries);
2238  SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
2239  if (vsr->status.has_exitsummary)
2240  smartlist_add(exitsummaries, vsr->status.exitsummary);
2241  });
2242  smartlist_sort_strings(exitsummaries);
2243  chosen_exitsummary = get_most_frequent_member(exitsummaries);
2244 
2245  if (!chosen_exitsummary)
2246  log_warn(LD_DIR, "Wow, not one of the voters had an exit "
2247  "policy summary for %s. Wow.", id);
2248  }
2249 
2250  if (chosen_exitsummary) {
2251  rs_out.has_exitsummary = 1;
2252  /* yea, discards the const */
2253  rs_out.exitsummary = (char *)chosen_exitsummary;
2254  }
2255  }
2256 
2257  if (flavor == FLAV_MICRODESC &&
2258  tor_digest256_is_zero(microdesc_digest)) {
2259  /* With no microdescriptor digest, we omit the entry entirely. */
2260  continue;
2261  }
2262 
2263  {
2264  char *buf;
2265  /* Okay!! Now we can write the descriptor... */
2266  /* First line goes into "buf". */
2267  buf = routerstatus_format_entry(&rs_out, NULL, NULL,
2268  rs_format, consensus_method, NULL);
2269  if (buf)
2270  smartlist_add(chunks, buf);
2271  }
2272  /* Now an m line, if applicable. */
2273  if (flavor == FLAV_MICRODESC &&
2274  !tor_digest256_is_zero(microdesc_digest)) {
2275  char m[BASE64_DIGEST256_LEN+1];
2276  digest256_to_base64(m, microdesc_digest);
2277  smartlist_add_asprintf(chunks, "m %s\n", m);
2278  }
2279  /* Next line is all flags. The "\n" is missing. */
2280  smartlist_add(chunks,
2281  smartlist_join_strings(chosen_flags, " ", 0, NULL));
2282  /* Now the version line. */
2283  if (chosen_version) {
2284  smartlist_add_strdup(chunks, "\nv ");
2285  smartlist_add_strdup(chunks, chosen_version);
2286  }
2287  smartlist_add_strdup(chunks, "\n");
2288  if (chosen_protocol_list &&
2289  consensus_method >= MIN_METHOD_FOR_RS_PROTOCOLS) {
2290  smartlist_add_asprintf(chunks, "pr %s\n", chosen_protocol_list);
2291  }
2292  /* Now the weight line. */
2293  if (rs_out.has_bandwidth) {
2294  char *guardfraction_str = NULL;
2295  int unmeasured = rs_out.bw_is_unmeasured;
2296 
2297  /* If we have guardfraction info, include it in the 'w' line. */
2298  if (rs_out.has_guardfraction) {
2299  tor_asprintf(&guardfraction_str,
2300  " GuardFraction=%u", rs_out.guardfraction_percentage);
2301  }
2302  smartlist_add_asprintf(chunks, "w Bandwidth=%d%s%s\n",
2303  rs_out.bandwidth_kb,
2304  unmeasured?" Unmeasured=1":"",
2305  guardfraction_str ? guardfraction_str : "");
2306 
2307  tor_free(guardfraction_str);
2308  }
2309 
2310  /* Now the exitpolicy summary line. */
2311  if (rs_out.has_exitsummary && flavor == FLAV_NS) {
2312  smartlist_add_asprintf(chunks, "p %s\n", rs_out.exitsummary);
2313  }
2314 
2315  /* And the loop is over and we move on to the next router */
2316  }
2317 
2318  tor_free(size);
2319  tor_free(n_voter_flags);
2320  tor_free(n_flag_voters);
2321  for (i = 0; i < smartlist_len(votes); ++i)
2322  tor_free(flag_map[i]);
2323  tor_free(flag_map);
2324  tor_free(flag_counts);
2325  tor_free(named_flag);
2326  tor_free(unnamed_flag);
2327  strmap_free(name_to_id_map, NULL);
2328  smartlist_free(matching_descs);
2329  smartlist_free(chosen_flags);
2330  smartlist_free(versions);
2331  smartlist_free(protocols);
2332  smartlist_free(exitsummaries);
2333  tor_free(bandwidths_kb);
2334  tor_free(measured_bws_kb);
2335  tor_free(measured_guardfraction);
2336  }
2337 
2338  /* Mark the directory footer region */
2339  smartlist_add_strdup(chunks, "directory-footer\n");
2340 
2341  {
2342  int64_t weight_scale = BW_WEIGHT_SCALE;
2343  char *bw_weight_param = NULL;
2344 
2345  // Parse params, extract BW_WEIGHT_SCALE if present
2346  // DO NOT use consensus_param_bw_weight_scale() in this code!
2347  // The consensus is not formed yet!
2348  /* XXXX Extract this code into a common function. Or not: #19011. */
2349  if (params) {
2350  if (strcmpstart(params, "bwweightscale=") == 0)
2351  bw_weight_param = params;
2352  else
2353  bw_weight_param = strstr(params, " bwweightscale=");
2354  }
2355 
2356  if (bw_weight_param) {
2357  int ok=0;
2358  char *eq = strchr(bw_weight_param, '=');
2359  if (eq) {
2360  weight_scale = tor_parse_long(eq+1, 10, 1, INT32_MAX, &ok,
2361  NULL);
2362  if (!ok) {
2363  log_warn(LD_DIR, "Bad element '%s' in bw weight param",
2364  escaped(bw_weight_param));
2365  weight_scale = BW_WEIGHT_SCALE;
2366  }
2367  } else {
2368  log_warn(LD_DIR, "Bad element '%s' in bw weight param",
2369  escaped(bw_weight_param));
2370  weight_scale = BW_WEIGHT_SCALE;
2371  }
2372  }
2373 
2374  added_weights = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D,
2375  T, weight_scale);
2376  }
2377 
2378  /* Add a signature. */
2379  {
2380  char digest[DIGEST256_LEN];
2381  char fingerprint[HEX_DIGEST_LEN+1];
2382  char signing_key_fingerprint[HEX_DIGEST_LEN+1];
2383  digest_algorithm_t digest_alg =
2384  flavor == FLAV_NS ? DIGEST_SHA1 : DIGEST_SHA256;
2385  size_t digest_len =
2386  flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
2387  const char *algname = crypto_digest_algorithm_get_name(digest_alg);
2388  char *signature;
2389 
2390  smartlist_add_strdup(chunks, "directory-signature ");
2391 
2392  /* Compute the hash of the chunks. */
2393  crypto_digest_smartlist(digest, digest_len, chunks, "", digest_alg);
2394 
2395  /* Get the fingerprints */
2396  crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
2397  crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
2398 
2399  /* add the junk that will go at the end of the line. */
2400  if (flavor == FLAV_NS) {
2401  smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
2402  signing_key_fingerprint);
2403  } else {
2404  smartlist_add_asprintf(chunks, "%s %s %s\n",
2405  algname, fingerprint,
2406  signing_key_fingerprint);
2407  }
2408  /* And the signature. */
2409  if (!(signature = router_get_dirobj_signature(digest, digest_len,
2410  signing_key))) {
2411  log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
2412  goto done;
2413  }
2414  smartlist_add(chunks, signature);
2415 
2416  if (legacy_id_key_digest && legacy_signing_key) {
2417  smartlist_add_strdup(chunks, "directory-signature ");
2418  base16_encode(fingerprint, sizeof(fingerprint),
2419  legacy_id_key_digest, DIGEST_LEN);
2421  signing_key_fingerprint, 0);
2422  if (flavor == FLAV_NS) {
2423  smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
2424  signing_key_fingerprint);
2425  } else {
2426  smartlist_add_asprintf(chunks, "%s %s %s\n",
2427  algname, fingerprint,
2428  signing_key_fingerprint);
2429  }
2430 
2431  if (!(signature = router_get_dirobj_signature(digest, digest_len,
2432  legacy_signing_key))) {
2433  log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
2434  goto done;
2435  }
2436  smartlist_add(chunks, signature);
2437  }
2438  }
2439 
2440  result = smartlist_join_strings(chunks, "", 0, NULL);
2441 
2442  {
2443  networkstatus_t *c;
2444  if (!(c = networkstatus_parse_vote_from_string(result, strlen(result),
2445  NULL,
2446  NS_TYPE_CONSENSUS))) {
2447  log_err(LD_BUG, "Generated a networkstatus consensus we couldn't "
2448  "parse.");
2449  tor_free(result);
2450  goto done;
2451  }
2452  // Verify balancing parameters
2453  if (added_weights) {
2454  networkstatus_verify_bw_weights(c, consensus_method);
2455  }
2456  networkstatus_vote_free(c);
2457  }
2458 
2459  done:
2460 
2461  dircollator_free(collator);
2462  tor_free(client_versions);
2463  tor_free(server_versions);
2464  tor_free(packages);
2465  SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
2466  smartlist_free(flags);
2467  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
2468  smartlist_free(chunks);
2469  SMARTLIST_FOREACH(param_list, char *, cp, tor_free(cp));
2470  smartlist_free(param_list);
2471 
2472  return result;
2473 }
2474 
2477 STATIC char *
2479 {
2480  const int n_votes = smartlist_len(votes);
2481 
2482  /* This will be a map from "packagename version" strings to arrays
2483  * of const char *, with the i'th member of the array corresponding to the
2484  * package line from the i'th vote.
2485  */
2486  strmap_t *package_status = strmap_new();
2487 
2489  if (! v->package_lines)
2490  continue;
2491  SMARTLIST_FOREACH_BEGIN(v->package_lines, const char *, line) {
2493  continue;
2494 
2495  /* Skip 'cp' to the second space in the line. */
2496  const char *cp = strchr(line, ' ');
2497  if (!cp) continue;
2498  ++cp;
2499  cp = strchr(cp, ' ');
2500  if (!cp) continue;
2501 
2502  char *key = tor_strndup(line, cp - line);
2503 
2504  const char **status = strmap_get(package_status, key);
2505  if (!status) {
2506  status = tor_calloc(n_votes, sizeof(const char *));
2507  strmap_set(package_status, key, status);
2508  }
2509  status[v_sl_idx] = line; /* overwrite old value */
2510  tor_free(key);
2511  } SMARTLIST_FOREACH_END(line);
2512  } SMARTLIST_FOREACH_END(v);
2513 
2514  smartlist_t *entries = smartlist_new(); /* temporary */
2515  smartlist_t *result_list = smartlist_new(); /* output */
2516  STRMAP_FOREACH(package_status, key, const char **, values) {
2517  int i, count=-1;
2518  for (i = 0; i < n_votes; ++i) {
2519  if (values[i])
2520  smartlist_add(entries, (void*) values[i]);
2521  }
2522  smartlist_sort_strings(entries);
2523  int n_voting_for_entry = smartlist_len(entries);
2524  const char *most_frequent =
2525  smartlist_get_most_frequent_string_(entries, &count);
2526 
2527  if (n_voting_for_entry >= 3 && count > n_voting_for_entry / 2) {
2528  smartlist_add_asprintf(result_list, "package %s\n", most_frequent);
2529  }
2530 
2531  smartlist_clear(entries);
2532 
2533  } STRMAP_FOREACH_END;
2534 
2535  smartlist_sort_strings(result_list);
2536 
2537  char *result = smartlist_join_strings(result_list, "", 0, NULL);
2538 
2539  SMARTLIST_FOREACH(result_list, char *, cp, tor_free(cp));
2540  smartlist_free(result_list);
2541  smartlist_free(entries);
2542  strmap_free(package_status, tor_free_);
2543 
2544  return result;
2545 }
2546 
2555 STATIC int
2558  const char *source,
2559  int severity,
2560  const char **msg_out)
2561 {
2562  int r = 0;
2563  const char *flavor;
2564  smartlist_t *siglist;
2565  tor_assert(sigs);
2566  tor_assert(target);
2567  tor_assert(target->type == NS_TYPE_CONSENSUS);
2568 
2569  flavor = networkstatus_get_flavor_name(target->flavor);
2570 
2571  /* Do the times seem right? */
2572  if (target->valid_after != sigs->valid_after) {
2573  *msg_out = "Valid-After times do not match "
2574  "when adding detached signatures to consensus";
2575  return -1;
2576  }
2577  if (target->fresh_until != sigs->fresh_until) {
2578  *msg_out = "Fresh-until times do not match "
2579  "when adding detached signatures to consensus";
2580  return -1;
2581  }
2582  if (target->valid_until != sigs->valid_until) {
2583  *msg_out = "Valid-until times do not match "
2584  "when adding detached signatures to consensus";
2585  return -1;
2586  }
2587  siglist = strmap_get(sigs->signatures, flavor);
2588  if (!siglist) {
2589  *msg_out = "No signatures for given consensus flavor";
2590  return -1;
2591  }
2592 
2594  {
2595  common_digests_t *digests = strmap_get(sigs->digests, flavor);
2596  int n_matches = 0;
2597  int alg;
2598  if (!digests) {
2599  *msg_out = "No digests for given consensus flavor";
2600  return -1;
2601  }
2602  for (alg = DIGEST_SHA1; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
2603  if (!tor_mem_is_zero(digests->d[alg], DIGEST256_LEN)) {
2604  if (fast_memeq(target->digests.d[alg], digests->d[alg],
2605  DIGEST256_LEN)) {
2606  ++n_matches;
2607  } else {
2608  *msg_out = "Mismatched digest.";
2609  return -1;
2610  }
2611  }
2612  }
2613  if (!n_matches) {
2614  *msg_out = "No recognized digests for given consensus flavor";
2615  }
2616  }
2617 
2618  /* For each voter in src... */
2620  char voter_identity[HEX_DIGEST_LEN+1];
2621  networkstatus_voter_info_t *target_voter =
2622  networkstatus_get_voter_by_id(target, sig->identity_digest);
2623  authority_cert_t *cert = NULL;
2624  const char *algorithm;
2625  document_signature_t *old_sig = NULL;
2626 
2627  algorithm = crypto_digest_algorithm_get_name(sig->alg);
2628 
2629  base16_encode(voter_identity, sizeof(voter_identity),
2630  sig->identity_digest, DIGEST_LEN);
2631  log_info(LD_DIR, "Looking at signature from %s using %s", voter_identity,
2632  algorithm);
2633  /* If the target doesn't know about this voter, then forget it. */
2634  if (!target_voter) {
2635  log_info(LD_DIR, "We do not know any voter with ID %s", voter_identity);
2636  continue;
2637  }
2638 
2639  old_sig = networkstatus_get_voter_sig_by_alg(target_voter, sig->alg);
2640 
2641  /* If the target already has a good signature from this voter, then skip
2642  * this one. */
2643  if (old_sig && old_sig->good_signature) {
2644  log_info(LD_DIR, "We already have a good signature from %s using %s",
2645  voter_identity, algorithm);
2646  continue;
2647  }
2648 
2649  /* Try checking the signature if we haven't already. */
2650  if (!sig->good_signature && !sig->bad_signature) {
2651  cert = authority_cert_get_by_digests(sig->identity_digest,
2652  sig->signing_key_digest);
2653  if (cert) {
2654  /* Not checking the return value here, since we are going to look
2655  * at the status of sig->good_signature in a moment. */
2656  (void) networkstatus_check_document_signature(target, sig, cert);
2657  }
2658  }
2659 
2660  /* If this signature is good, or we don't have any signature yet,
2661  * then maybe add it. */
2662  if (sig->good_signature || !old_sig || old_sig->bad_signature) {
2663  log_info(LD_DIR, "Adding signature from %s with %s", voter_identity,
2664  algorithm);
2665  tor_log(severity, LD_DIR, "Added a signature for %s from %s.",
2666  target_voter->nickname, source);
2667  ++r;
2668  if (old_sig) {
2669  smartlist_remove(target_voter->sigs, old_sig);
2670  document_signature_free(old_sig);
2671  }
2672  smartlist_add(target_voter->sigs, document_signature_dup(sig));
2673  } else {
2674  log_info(LD_DIR, "Not adding signature from %s", voter_identity);
2675  }
2676  } SMARTLIST_FOREACH_END(sig);
2677 
2678  return r;
2679 }
2680 
2686 static char *
2688  int for_detached_signatures)
2689 {
2690  smartlist_t *elements;
2691  char buf[4096];
2692  char *result = NULL;
2693  int n_sigs = 0;
2694  const consensus_flavor_t flavor = consensus->flavor;
2695  const char *flavor_name = networkstatus_get_flavor_name(flavor);
2696  const char *keyword;
2697 
2698  if (for_detached_signatures && flavor != FLAV_NS)
2699  keyword = "additional-signature";
2700  else
2701  keyword = "directory-signature";
2702 
2703  elements = smartlist_new();
2704 
2707  char sk[HEX_DIGEST_LEN+1];
2708  char id[HEX_DIGEST_LEN+1];
2709  if (!sig->signature || sig->bad_signature)
2710  continue;
2711  ++n_sigs;
2712  base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
2713  base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
2714  if (flavor == FLAV_NS) {
2715  smartlist_add_asprintf(elements,
2716  "%s %s %s\n-----BEGIN SIGNATURE-----\n",
2717  keyword, id, sk);
2718  } else {
2719  const char *digest_name =
2721  smartlist_add_asprintf(elements,
2722  "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
2723  keyword,
2724  for_detached_signatures ? " " : "",
2725  for_detached_signatures ? flavor_name : "",
2726  digest_name, id, sk);
2727  }
2728  base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len,
2729  BASE64_ENCODE_MULTILINE);
2730  strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
2731  smartlist_add_strdup(elements, buf);
2732  } SMARTLIST_FOREACH_END(sig);
2733  } SMARTLIST_FOREACH_END(v);
2734 
2735  result = smartlist_join_strings(elements, "", 0, NULL);
2736  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
2737  smartlist_free(elements);
2738  if (!n_sigs)
2739  tor_free(result);
2740  return result;
2741 }
2742 
2747 STATIC char *
2749 {
2750  smartlist_t *elements;
2751  char *result = NULL, *sigs = NULL;
2752  networkstatus_t *consensus_ns = NULL;
2753  tor_assert(consensuses);
2754 
2755  SMARTLIST_FOREACH(consensuses, networkstatus_t *, ns, {
2756  tor_assert(ns);
2757  tor_assert(ns->type == NS_TYPE_CONSENSUS);
2758  if (ns && ns->flavor == FLAV_NS)
2759  consensus_ns = ns;
2760  });
2761  if (!consensus_ns) {
2762  log_warn(LD_BUG, "No NS consensus given.");
2763  return NULL;
2764  }
2765 
2766  elements = smartlist_new();
2767 
2768  {
2769  char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
2770  vu_buf[ISO_TIME_LEN+1];
2771  char d[HEX_DIGEST_LEN+1];
2772 
2773  base16_encode(d, sizeof(d),
2774  consensus_ns->digests.d[DIGEST_SHA1], DIGEST_LEN);
2775  format_iso_time(va_buf, consensus_ns->valid_after);
2776  format_iso_time(fu_buf, consensus_ns->fresh_until);
2777  format_iso_time(vu_buf, consensus_ns->valid_until);
2778 
2779  smartlist_add_asprintf(elements,
2780  "consensus-digest %s\n"
2781  "valid-after %s\n"
2782  "fresh-until %s\n"
2783  "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
2784  }
2785 
2786  /* Get all the digests for the non-FLAV_NS consensuses */
2787  SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
2788  const char *flavor_name = networkstatus_get_flavor_name(ns->flavor);
2789  int alg;
2790  if (ns->flavor == FLAV_NS)
2791  continue;
2792 
2793  /* start with SHA256; we don't include SHA1 for anything but the basic
2794  * consensus. */
2795  for (alg = DIGEST_SHA256; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
2796  char d[HEX_DIGEST256_LEN+1];
2797  const char *alg_name =
2799  if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
2800  continue;
2801  base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN);
2802  smartlist_add_asprintf(elements, "additional-digest %s %s %s\n",
2803  flavor_name, alg_name, d);
2804  }
2805  } SMARTLIST_FOREACH_END(ns);
2806 
2807  /* Now get all the sigs for non-FLAV_NS consensuses */
2808  SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
2809  char *sigs_on_this_consensus;
2810  if (ns->flavor == FLAV_NS)
2811  continue;
2812  sigs_on_this_consensus = networkstatus_format_signatures(ns, 1);
2813  if (!sigs_on_this_consensus) {
2814  log_warn(LD_DIR, "Couldn't format signatures");
2815  goto err;
2816  }
2817  smartlist_add(elements, sigs_on_this_consensus);
2818  } SMARTLIST_FOREACH_END(ns);
2819 
2820  /* Now add the FLAV_NS consensus signatrures. */
2821  sigs = networkstatus_format_signatures(consensus_ns, 1);
2822  if (!sigs)
2823  goto err;
2824  smartlist_add(elements, sigs);
2825 
2826  result = smartlist_join_strings(elements, "", 0, NULL);
2827  err:
2828  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
2829  smartlist_free(elements);
2830  return result;
2831 }
2832 
2836 static char *
2838  int n_flavors)
2839 {
2840  int flav;
2841  char *signatures;
2842  smartlist_t *c = smartlist_new();
2843  for (flav = 0; flav < n_flavors; ++flav) {
2844  if (pending[flav].consensus)
2845  smartlist_add(c, pending[flav].consensus);
2846  }
2847  signatures = networkstatus_get_detached_signatures(c);
2848  smartlist_free(c);
2849  return signatures;
2850 }
2851 
2857 time_t
2858 dirvote_act(const or_options_t *options, time_t now)
2859 {
2860  if (!authdir_mode_v3(options))
2861  return TIME_MAX;
2862  tor_assert_nonfatal(voting_schedule.voting_starts);
2863  /* If we haven't initialized this object through this codeflow, we need to
2864  * recalculate the timings to match our vote. The reason to do that is if we
2865  * have a voting schedule initialized 1 minute ago, the voting timings might
2866  * not be aligned to what we should expect with "now". This is especially
2867  * true for TestingTorNetwork using smaller timings. */
2868  if (voting_schedule.created_on_demand) {
2869  char *keys = list_v3_auth_ids();
2870  authority_cert_t *c = get_my_v3_authority_cert();
2871  log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
2872  "Mine is %s.",
2874  tor_free(keys);
2875  voting_schedule_recalculate_timing(options, now);
2876  }
2877 
2878 #define IF_TIME_FOR_NEXT_ACTION(when_field, done_field) \
2879  if (! voting_schedule.done_field) { \
2880  if (voting_schedule.when_field > now) { \
2881  return voting_schedule.when_field; \
2882  } else {
2883 #define ENDIF \
2884  } \
2885  }
2886 
2887  IF_TIME_FOR_NEXT_ACTION(voting_starts, have_voted) {
2888  log_notice(LD_DIR, "Time to vote.");
2890  voting_schedule.have_voted = 1;
2891  } ENDIF
2892  IF_TIME_FOR_NEXT_ACTION(fetch_missing_votes, have_fetched_missing_votes) {
2893  log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
2895  voting_schedule.have_fetched_missing_votes = 1;
2896  } ENDIF
2897  IF_TIME_FOR_NEXT_ACTION(voting_ends, have_built_consensus) {
2898  log_notice(LD_DIR, "Time to compute a consensus.");
2900  /* XXXX We will want to try again later if we haven't got enough
2901  * votes yet. Implement this if it turns out to ever happen. */
2902  voting_schedule.have_built_consensus = 1;
2903  } ENDIF
2904  IF_TIME_FOR_NEXT_ACTION(fetch_missing_signatures,
2905  have_fetched_missing_signatures) {
2906  log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
2908  voting_schedule.have_fetched_missing_signatures = 1;
2909  } ENDIF
2910  IF_TIME_FOR_NEXT_ACTION(interval_starts,
2911  have_published_consensus) {
2912  log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
2915  voting_schedule.have_published_consensus = 1;
2916  /* Update our shared random state with the consensus just published. */
2917  sr_act_post_consensus(
2918  networkstatus_get_latest_consensus_by_flavor(FLAV_NS));
2919  /* XXXX We will want to try again later if we haven't got enough
2920  * signatures yet. Implement this if it turns out to ever happen. */
2921  voting_schedule_recalculate_timing(options, now);
2922  return voting_schedule.voting_starts;
2923  } ENDIF
2924 
2925  tor_assert_nonfatal_unreached();
2926  return now + 1;
2927 
2928 #undef ENDIF
2929 #undef IF_TIME_FOR_NEXT_ACTION
2930 }
2931 
2935 typedef struct pending_vote_t {
2936  cached_dir_t *vote_body;
2937  networkstatus_t *vote;
2938 } pending_vote_t;
2939 
2946 
2947 /* DOCDOC pending_consensuses */
2948 static pending_consensus_t pending_consensuses[N_CONSENSUS_FLAVORS];
2949 
2952 static char *pending_consensus_signatures = NULL;
2953 
2957 
2960 static int
2962 {
2964  authority_cert_t *cert = get_my_v3_authority_cert();
2965  networkstatus_t *ns;
2966  char *contents;
2967  pending_vote_t *pending_vote;
2968  time_t now = time(NULL);
2969 
2970  int status;
2971  const char *msg = "";
2972 
2973  if (!cert || !key) {
2974  log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
2975  return -1;
2976  } else if (cert->expires < now) {
2977  log_warn(LD_NET, "Can't generate v3 vote with expired certificate");
2978  return -1;
2979  }
2980  if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
2981  return -1;
2982 
2983  contents = format_networkstatus_vote(key, ns);
2984  networkstatus_vote_free(ns);
2985  if (!contents)
2986  return -1;
2987 
2988  pending_vote = dirvote_add_vote(contents, &msg, &status);
2989  tor_free(contents);
2990  if (!pending_vote) {
2991  log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
2992  msg);
2993  return -1;
2994  }
2995 
2997  ROUTER_PURPOSE_GENERAL,
2998  V3_DIRINFO,
2999  pending_vote->vote_body->dir,
3000  pending_vote->vote_body->dir_len, 0);
3001  log_notice(LD_DIR, "Vote posted.");
3002  return 0;
3003 }
3004 
3008 static void
3010 {
3011  smartlist_t *missing_fps = smartlist_new();
3012  char *resource;
3013 
3014  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
3015  dir_server_t *, ds) {
3016  if (!(ds->type & V3_DIRINFO))
3017  continue;
3018  if (!dirvote_get_vote(ds->v3_identity_digest,
3019  DGV_BY_ID|DGV_INCLUDE_PENDING)) {
3020  char *cp = tor_malloc(HEX_DIGEST_LEN+1);
3021  base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
3022  DIGEST_LEN);
3023  smartlist_add(missing_fps, cp);
3024  }
3025  } SMARTLIST_FOREACH_END(ds);
3026 
3027  if (!smartlist_len(missing_fps)) {
3028  smartlist_free(missing_fps);
3029  return;
3030  }
3031  {
3032  char *tmp = smartlist_join_strings(missing_fps, " ", 0, NULL);
3033  log_notice(LOG_NOTICE, "We're missing votes from %d authorities (%s). "
3034  "Asking every other authority for a copy.",
3035  smartlist_len(missing_fps), tmp);
3036  tor_free(tmp);
3037  }
3038  resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
3040  0, resource);
3041  tor_free(resource);
3042  SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
3043  smartlist_free(missing_fps);
3044 }
3045 
3048 static void
3050 {
3051  int need_any = 0;
3052  int i;
3053  for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
3054  networkstatus_t *consensus = pending_consensuses[i].consensus;
3055  if (!consensus ||
3056  networkstatus_check_consensus_signature(consensus, -1) == 1) {
3057  /* We have no consensus, or we have one that's signed by everybody. */
3058  continue;
3059  }
3060  need_any = 1;
3061  }
3062  if (!need_any)
3063  return;
3064 
3066  0, NULL);
3067 }
3068 
3071 static void
3073 {
3074  int i;
3075  for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
3076  pending_consensus_t *pc = &pending_consensuses[i];
3077  tor_free(pc->body);
3078 
3079  networkstatus_vote_free(pc->consensus);
3080  pc->consensus = NULL;
3081  }
3082 }
3083 
3085 static void
3086 dirvote_clear_votes(int all_votes)
3087 {
3088  if (!previous_vote_list)
3089  previous_vote_list = smartlist_new();
3090  if (!pending_vote_list)
3091  pending_vote_list = smartlist_new();
3092 
3093  /* All "previous" votes are now junk. */
3095  cached_dir_decref(v->vote_body);
3096  v->vote_body = NULL;
3097  networkstatus_vote_free(v->vote);
3098  tor_free(v);
3099  });
3101 
3102  if (all_votes) {
3103  /* If we're dumping all the votes, we delete the pending ones. */
3105  cached_dir_decref(v->vote_body);
3106  v->vote_body = NULL;
3107  networkstatus_vote_free(v->vote);
3108  tor_free(v);
3109  });
3110  } else {
3111  /* Otherwise, we move them into "previous". */
3113  }
3115 
3118  tor_free(cp));
3120  }
3123 }
3124 
3127 static char *
3129 {
3130  smartlist_t *known_v3_keys = smartlist_new();
3131  char *keys;
3132  SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
3133  dir_server_t *, ds,
3134  if ((ds->type & V3_DIRINFO) &&
3135  !tor_digest_is_zero(ds->v3_identity_digest))
3136  smartlist_add(known_v3_keys,
3137  tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
3138  keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
3139  SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
3140  smartlist_free(known_v3_keys);
3141  return keys;
3142 }
3143 
3150 dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
3151 {
3152  networkstatus_t *vote;
3154  dir_server_t *ds;
3155  pending_vote_t *pending_vote = NULL;
3156  const char *end_of_vote = NULL;
3157  int any_failed = 0;
3158  tor_assert(vote_body);
3159  tor_assert(msg_out);
3160  tor_assert(status_out);
3161 
3162  if (!pending_vote_list)
3163  pending_vote_list = smartlist_new();
3164  *status_out = 0;
3165  *msg_out = NULL;
3166 
3167  again:
3168  vote = networkstatus_parse_vote_from_string(vote_body, strlen(vote_body),
3169  &end_of_vote,
3170  NS_TYPE_VOTE);
3171  if (!end_of_vote)
3172  end_of_vote = vote_body + strlen(vote_body);
3173  if (!vote) {
3174  log_warn(LD_DIR, "Couldn't parse vote: length was %d",
3175  (int)strlen(vote_body));
3176  *msg_out = "Unable to parse vote";
3177  goto err;
3178  }
3179  tor_assert(smartlist_len(vote->voters) == 1);
3180  vi = get_voter(vote);
3181  {
3182  int any_sig_good = 0;
3184  if (sig->good_signature)
3185  any_sig_good = 1);
3186  tor_assert(any_sig_good);
3187  }
3188  ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
3189  if (!ds) {
3190  char *keys = list_v3_auth_ids();
3191  log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
3192  "with authority key ID %s. "
3193  "This key ID is not recognized. Known v3 key IDs are: %s",
3194  vi->nickname, vi->address,
3195  hex_str(vi->identity_digest, DIGEST_LEN), keys);
3196  tor_free(keys);
3197  *msg_out = "Vote not from a recognized v3 authority";
3198  goto err;
3199  }
3200  tor_assert(vote->cert);
3202  vote->cert->signing_key_digest)) {
3203  /* Hey, it's a new cert! */
3206  TRUSTED_DIRS_CERTS_SRC_FROM_VOTE, 1 /*flush*/,
3207  NULL);
3209  vote->cert->signing_key_digest)) {
3210  log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
3211  }
3212  }
3213 
3214  /* Is it for the right period? */
3215  if (vote->valid_after != voting_schedule.interval_starts) {
3216  char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
3217  format_iso_time(tbuf1, vote->valid_after);
3218  format_iso_time(tbuf2, voting_schedule.interval_starts);
3219  log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
3220  "we were expecting %s", vi->address, tbuf1, tbuf2);
3221  *msg_out = "Bad valid-after time";
3222  goto err;
3223  }
3224 
3225  /* Fetch any new router descriptors we just learned about */
3226  update_consensus_router_descriptor_downloads(time(NULL), 1, vote);
3227 
3228  /* Now see whether we already have a vote from this authority. */
3230  if (fast_memeq(v->vote->cert->cache_info.identity_digest,
3232  DIGEST_LEN)) {
3233  networkstatus_voter_info_t *vi_old = get_voter(v->vote);
3234  if (fast_memeq(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
3235  /* Ah, it's the same vote. Not a problem. */
3236  log_info(LD_DIR, "Discarding a vote we already have (from %s).",
3237  vi->address);
3238  if (*status_out < 200)
3239  *status_out = 200;
3240  goto discard;
3241  } else if (v->vote->published < vote->published) {
3242  log_notice(LD_DIR, "Replacing an older pending vote from this "
3243  "directory (%s)", vi->address);
3244  cached_dir_decref(v->vote_body);
3245  networkstatus_vote_free(v->vote);
3246  v->vote_body = new_cached_dir(tor_strndup(vote_body,
3247  end_of_vote-vote_body),
3248  vote->published);
3249  v->vote = vote;
3250  if (end_of_vote &&
3251  !strcmpstart(end_of_vote, "network-status-version"))
3252  goto again;
3253 
3254  if (*status_out < 200)
3255  *status_out = 200;
3256  if (!*msg_out)
3257  *msg_out = "OK";
3258  return v;
3259  } else {
3260  *msg_out = "Already have a newer pending vote";
3261  goto err;
3262  }
3263  }
3264  } SMARTLIST_FOREACH_END(v);
3265 
3266  /* This a valid vote, update our shared random state. */
3267  sr_handle_received_commits(vote->sr_info.commits,
3268  vote->cert->identity_key);
3269 
3270  pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
3271  pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
3272  end_of_vote-vote_body),
3273  vote->published);
3274  pending_vote->vote = vote;
3275  smartlist_add(pending_vote_list, pending_vote);
3276 
3277  if (!strcmpstart(end_of_vote, "network-status-version ")) {
3278  vote_body = end_of_vote;
3279  goto again;
3280  }
3281 
3282  goto done;
3283 
3284  err:
3285  any_failed = 1;
3286  if (!*msg_out)
3287  *msg_out = "Error adding vote";
3288  if (*status_out < 400)
3289  *status_out = 400;
3290 
3291  discard:
3292  networkstatus_vote_free(vote);
3293 
3294  if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
3295  vote_body = end_of_vote;
3296  goto again;
3297  }
3298 
3299  done:
3300 
3301  if (*status_out < 200)
3302  *status_out = 200;
3303  if (!*msg_out) {
3304  if (!any_failed && !pending_vote) {
3305  *msg_out = "Duplicate discarded";
3306  } else {
3307  *msg_out = "ok";
3308  }
3309  }
3310 
3311  return any_failed ? NULL : pending_vote;
3312 }
3313 
3314 /* Write the votes in <b>pending_vote_list</b> to disk. */
3315 static void
3316 write_v3_votes_to_disk(const smartlist_t *pending_votes)
3317 {
3318  smartlist_t *votestrings = smartlist_new();
3319  char *votefile = NULL;
3320 
3321  SMARTLIST_FOREACH(pending_votes, pending_vote_t *, v,
3322  {
3323  sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
3324  c->bytes = v->vote_body->dir;
3325  c->len = v->vote_body->dir_len;
3326  smartlist_add(votestrings, c); /* collect strings to write to disk */
3327  });
3328 
3329  votefile = get_datadir_fname("v3-status-votes");
3330  write_chunks_to_file(votefile, votestrings, 0, 0);
3331  log_debug(LD_DIR, "Wrote votes to disk (%s)!", votefile);
3332 
3333  tor_free(votefile);
3334  SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
3335  smartlist_free(votestrings);
3336 }
3337 
3342 static int
3344 {
3345  /* Have we got enough votes to try? */
3346  int n_votes, n_voters, n_vote_running = 0;
3347  smartlist_t *votes = NULL;
3348  char *consensus_body = NULL, *signatures = NULL;
3349  networkstatus_t *consensus = NULL;
3350  authority_cert_t *my_cert;
3352  int flav;
3353 
3354  memset(pending, 0, sizeof(pending));
3355 
3356  if (!pending_vote_list)
3357  pending_vote_list = smartlist_new();
3358 
3359  /* Write votes to disk */
3360  write_v3_votes_to_disk(pending_vote_list);
3361 
3362  /* Setup votes smartlist */
3363  votes = smartlist_new();
3365  {
3366  smartlist_add(votes, v->vote); /* collect votes to compute consensus */
3367  });
3368 
3369  /* See if consensus managed to achieve majority */
3370  n_voters = get_n_authorities(V3_DIRINFO);
3371  n_votes = smartlist_len(pending_vote_list);
3372  if (n_votes <= n_voters/2) {
3373  log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
3374  "%d of %d", n_votes, n_voters/2+1);
3375  goto err;
3376  }
3379  if (smartlist_contains_string(v->vote->known_flags, "Running"))
3380  n_vote_running++;
3381  });
3382  if (!n_vote_running) {
3383  /* See task 1066. */
3384  log_warn(LD_DIR, "Nobody has voted on the Running flag. Generating "
3385  "and publishing a consensus without Running nodes "
3386  "would make many clients stop working. Not "
3387  "generating a consensus!");
3388  goto err;
3389  }
3390 
3391  if (!(my_cert = get_my_v3_authority_cert())) {
3392  log_warn(LD_DIR, "Can't generate consensus without a certificate.");
3393  goto err;
3394  }
3395 
3396  {
3397  char legacy_dbuf[DIGEST_LEN];
3398  crypto_pk_t *legacy_sign=NULL;
3399  char *legacy_id_digest = NULL;
3400  int n_generated = 0;
3401  if (get_options()->V3AuthUseLegacyKey) {
3403  legacy_sign = get_my_v3_legacy_signing_key();
3404  if (cert) {
3405  if (crypto_pk_get_digest(cert->identity_key, legacy_dbuf)) {
3406  log_warn(LD_BUG,
3407  "Unable to compute digest of legacy v3 identity key");
3408  } else {
3409  legacy_id_digest = legacy_dbuf;
3410  }
3411  }
3412  }
3413 
3414  for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
3415  const char *flavor_name = networkstatus_get_flavor_name(flav);
3416  consensus_body = networkstatus_compute_consensus(
3417  votes, n_voters,
3418  my_cert->identity_key,
3419  get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign,
3420  flav);
3421 
3422  if (!consensus_body) {
3423  log_warn(LD_DIR, "Couldn't generate a %s consensus at all!",
3424  flavor_name);
3425  continue;
3426  }
3427  consensus = networkstatus_parse_vote_from_string(consensus_body,
3428  strlen(consensus_body),
3429  NULL,
3430  NS_TYPE_CONSENSUS);
3431  if (!consensus) {
3432  log_warn(LD_DIR, "Couldn't parse %s consensus we generated!",
3433  flavor_name);
3434  tor_free(consensus_body);
3435  continue;
3436  }
3437 
3438  /* 'Check' our own signature, to mark it valid. */
3440 
3441  pending[flav].body = consensus_body;
3442  pending[flav].consensus = consensus;
3443  n_generated++;
3444  consensus_body = NULL;
3445  consensus = NULL;
3446  }
3447  if (!n_generated) {
3448  log_warn(LD_DIR, "Couldn't generate any consensus flavors at all.");
3449  goto err;
3450  }
3451  }
3452 
3454  pending, N_CONSENSUS_FLAVORS);
3455 
3456  if (!signatures) {
3457  log_warn(LD_DIR, "Couldn't extract signatures.");
3458  goto err;
3459  }
3460 
3462  memcpy(pending_consensuses, pending, sizeof(pending));
3463 
3465  pending_consensus_signatures = signatures;
3466 
3468  int n_sigs = 0;
3469  /* we may have gotten signatures for this consensus before we built
3470  * it ourself. Add them now. */
3472  const char *msg = NULL;
3473  int r = dirvote_add_signatures_to_all_pending_consensuses(sig,
3474  "pending", &msg);
3475  if (r >= 0)
3476  n_sigs += r;
3477  else
3478  log_warn(LD_DIR,
3479  "Could not add queued signature to new consensus: %s",
3480  msg);
3481  tor_free(sig);
3482  } SMARTLIST_FOREACH_END(sig);
3483  if (n_sigs)
3484  log_notice(LD_DIR, "Added %d pending signatures while building "
3485  "consensus.", n_sigs);
3487  }
3488 
3489  log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
3490 
3492  ROUTER_PURPOSE_GENERAL,
3493  V3_DIRINFO,
3495  strlen(pending_consensus_signatures), 0);
3496  log_notice(LD_DIR, "Signature(s) posted.");
3497 
3498  smartlist_free(votes);
3499  return 0;
3500  err:
3501  smartlist_free(votes);
3502  tor_free(consensus_body);
3503  tor_free(signatures);
3504  networkstatus_vote_free(consensus);
3505 
3506  return -1;
3507 }
3508 
3512 static int
3514  pending_consensus_t *pc,
3516  const char *source,
3517  int severity,
3518  const char **msg_out)
3519 {
3520  const char *flavor_name;
3521  int r = -1;
3522 
3523  /* Only call if we have a pending consensus right now. */
3524  tor_assert(pc->consensus);
3525  tor_assert(pc->body);
3527 
3528  flavor_name = networkstatus_get_flavor_name(pc->consensus->flavor);
3529  *msg_out = NULL;
3530 
3531  {
3532  smartlist_t *sig_list = strmap_get(sigs->signatures, flavor_name);
3533  log_info(LD_DIR, "Have %d signatures for adding to %s consensus.",
3534  sig_list ? smartlist_len(sig_list) : 0, flavor_name);
3535  }
3537  source, severity, msg_out);
3538  if (r >= 0) {
3539  log_info(LD_DIR,"Added %d signatures to consensus.", r);
3540  } else {
3541  log_fn(LOG_PROTOCOL_WARN, LD_DIR,
3542  "Unable to add signatures to consensus: %s",
3543  *msg_out ? *msg_out : "(unknown)");
3544  }
3545 
3546  if (r >= 1) {
3547  char *new_signatures =
3549  char *dst, *dst_end;
3550  size_t new_consensus_len;
3551  if (!new_signatures) {
3552  *msg_out = "No signatures to add";
3553  goto err;
3554  }
3555  new_consensus_len =
3556  strlen(pc->body) + strlen(new_signatures) + 1;
3557  pc->body = tor_realloc(pc->body, new_consensus_len);
3558  dst_end = pc->body + new_consensus_len;
3559  dst = strstr(pc->body, "directory-signature ");
3560  tor_assert(dst);
3561  strlcpy(dst, new_signatures, dst_end-dst);
3562 
3563  /* We remove this block once it has failed to crash for a while. But
3564  * unless it shows up in profiles, we're probably better leaving it in,
3565  * just in case we break detached signature processing at some point. */
3566  {
3568  pc->body, strlen(pc->body), NULL,
3569  NS_TYPE_CONSENSUS);
3570  tor_assert(v);
3571  networkstatus_vote_free(v);
3572  }
3573  *msg_out = "Signatures added";
3574  tor_free(new_signatures);
3575  } else if (r == 0) {
3576  *msg_out = "Signatures ignored";
3577  } else {
3578  goto err;
3579  }
3580 
3581  goto done;
3582  err:
3583  if (!*msg_out)
3584  *msg_out = "Unrecognized error while adding detached signatures.";
3585  done:
3586  return r;
3587 }
3588 
3589 static int
3590 dirvote_add_signatures_to_all_pending_consensuses(
3591  const char *detached_signatures_body,
3592  const char *source,
3593  const char **msg_out)
3594 {
3595  int r=0, i, n_added = 0, errors = 0;
3597  tor_assert(detached_signatures_body);
3598  tor_assert(msg_out);
3600 
3602  detached_signatures_body, NULL))) {
3603  *msg_out = "Couldn't parse detached signatures.";
3604  goto err;
3605  }
3606 
3607  for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
3608  int res;
3609  int severity = i == FLAV_NS ? LOG_NOTICE : LOG_INFO;
3610  pending_consensus_t *pc = &pending_consensuses[i];
3611  if (!pc->consensus)
3612  continue;
3613  res = dirvote_add_signatures_to_pending_consensus(pc, sigs, source,
3614  severity, msg_out);
3615  if (res < 0)
3616  errors++;
3617  else
3618  n_added += res;
3619  }
3620 
3621  if (errors && !n_added) {
3622  r = -1;
3623  goto err;
3624  }
3625 
3626  if (n_added && pending_consensuses[FLAV_NS].consensus) {
3627  char *new_detached =
3629  pending_consensuses, N_CONSENSUS_FLAVORS);
3630  if (new_detached) {
3632  pending_consensus_signatures = new_detached;
3633  }
3634  }
3635 
3636  r = n_added;
3637  goto done;
3638  err:
3639  if (!*msg_out)
3640  *msg_out = "Unrecognized error while adding detached signatures.";
3641  done:
3642  ns_detached_signatures_free(sigs);
3643  /* XXXX NM Check how return is used. We can now have an error *and*
3644  signatures added. */
3645  return r;
3646 }
3647 
3652 int
3653 dirvote_add_signatures(const char *detached_signatures_body,
3654  const char *source,
3655  const char **msg)
3656 {
3657  if (pending_consensuses[FLAV_NS].consensus) {
3658  log_notice(LD_DIR, "Got a signature from %s. "
3659  "Adding it to the pending consensus.", source);
3660  return dirvote_add_signatures_to_all_pending_consensuses(
3661  detached_signatures_body, source, msg);
3662  } else {
3663  log_notice(LD_DIR, "Got a signature from %s. "
3664  "Queuing it for the next consensus.", source);
3666  pending_consensus_signature_list = smartlist_new();
3668  detached_signatures_body);
3669  *msg = "Signature queued";
3670  return 0;
3671  }
3672 }
3673 
3676 static int
3678 {
3679  int i;
3680 
3681  /* Now remember all the other consensuses as if we were a directory cache. */
3682  for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
3683  pending_consensus_t *pending = &pending_consensuses[i];
3684  const char *name;
3686  tor_assert(name);
3687  if (!pending->consensus ||
3689  log_warn(LD_DIR, "Not enough info to publish pending %s consensus",name);
3690  continue;
3691  }
3692 
3694  strlen(pending->body),
3695  name, 0, NULL))
3696  log_warn(LD_DIR, "Error publishing %s consensus", name);
3697  else
3698  log_notice(LD_DIR, "Published %s consensus", name);
3699  }
3700 
3701  return 0;
3702 }
3703 
3705 void
3707 {
3709  /* now empty as a result of dirvote_clear_votes(). */
3710  smartlist_free(pending_vote_list);
3711  pending_vote_list = NULL;
3712  smartlist_free(previous_vote_list);
3713  previous_vote_list = NULL;
3714 
3718  /* now empty as a result of dirvote_clear_votes(). */
3719  smartlist_free(pending_consensus_signature_list);
3721  }
3722 }
3723 
3724 /* ====
3725  * Access to pending items.
3726  * ==== */
3727 
3729 MOCK_IMPL(const char *,
3730 dirvote_get_pending_consensus, (consensus_flavor_t flav))
3731 {
3732  tor_assert(((int)flav) >= 0 && (int)flav < N_CONSENSUS_FLAVORS);
3733  return pending_consensuses[flav].body;
3734 }
3735 
3738 MOCK_IMPL(const char *,
3739 dirvote_get_pending_detached_signatures, (void))
3740 {
3742 }
3743 
3752 const cached_dir_t *
3753 dirvote_get_vote(const char *fp, int flags)
3754 {
3755  int by_id = flags & DGV_BY_ID;
3756  const int include_pending = flags & DGV_INCLUDE_PENDING;
3757  const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
3758 
3760  return NULL;
3761  if (fp == NULL) {
3762  authority_cert_t *c = get_my_v3_authority_cert();
3763  if (c) {
3764  fp = c->cache_info.identity_digest;
3765  by_id = 1;
3766  } else
3767  return NULL;
3768  }
3769  if (by_id) {
3770  if (pending_vote_list && include_pending) {
3772  if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
3773  return pv->vote_body);
3774  }
3775  if (previous_vote_list && include_previous) {
3777  if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
3778  return pv->vote_body);
3779  }
3780  } else {
3781  if (pending_vote_list && include_pending) {
3783  if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
3784  return pv->vote_body);
3785  }
3786  if (previous_vote_list && include_previous) {
3788  if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
3789  return pv->vote_body);
3790  }
3791  }
3792  return NULL;
3793 }
3794 
3798 STATIC microdesc_t *
3799 dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
3800 {
3801  microdesc_t *result = NULL;
3802  char *key = NULL, *summary = NULL, *family = NULL;
3803  size_t keylen;
3804  smartlist_t *chunks = smartlist_new();
3805  char *output = NULL;
3806  crypto_pk_t *rsa_pubkey = router_get_rsa_onion_pkey(ri->onion_pkey,
3807  ri->onion_pkey_len);
3808 
3809  if (crypto_pk_write_public_key_to_string(rsa_pubkey, &key, &keylen)<0)
3810  goto done;
3811  summary = policy_summarize(ri->exit_policy, AF_INET);
3812  if (ri->declared_family)
3813  family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
3814 
3815  smartlist_add_asprintf(chunks, "onion-key\n%s", key);
3816 
3817  if (ri->onion_curve25519_pkey) {
3818  char kbuf[128];
3819  base64_encode(kbuf, sizeof(kbuf),
3820  (const char*)ri->onion_curve25519_pkey->public_key,
3821  CURVE25519_PUBKEY_LEN, BASE64_ENCODE_MULTILINE);
3822  smartlist_add_asprintf(chunks, "ntor-onion-key %s", kbuf);
3823  }
3824 
3825  /* We originally put a lines in the micrdescriptors, but then we worked out
3826  * that we needed them in the microdesc consensus. See #20916. */
3827  if (consensus_method < MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC &&
3828  !tor_addr_is_null(&ri->ipv6_addr) && ri->ipv6_orport)
3829  smartlist_add_asprintf(chunks, "a %s\n",
3830  fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
3831 
3832  if (family) {
3833  if (consensus_method < MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS) {
3834  smartlist_add_asprintf(chunks, "family %s\n", family);
3835  } else {
3836  const uint8_t *id = (const uint8_t *)ri->cache_info.identity_digest;
3837  char *canonical_family = nodefamily_canonicalize(family, id, 0);
3838  smartlist_add_asprintf(chunks, "family %s\n", canonical_family);
3839  tor_free(canonical_family);
3840  }
3841  }
3842 
3843  if (summary && strcmp(summary, "reject 1-65535"))
3844  smartlist_add_asprintf(chunks, "p %s\n", summary);
3845 
3846  if (ri->ipv6_exit_policy) {
3847  /* XXXX+++ This doesn't match proposal 208, which says these should
3848  * be taken unchanged from the routerinfo. That's bogosity, IMO:
3849  * the proposal should have said to do this instead.*/
3850  char *p6 = write_short_policy(ri->ipv6_exit_policy);
3851  if (p6 && strcmp(p6, "reject 1-65535"))
3852  smartlist_add_asprintf(chunks, "p6 %s\n", p6);
3853  tor_free(p6);
3854  }
3855 
3856  {
3857  char idbuf[ED25519_BASE64_LEN+1];
3858  const char *keytype;
3859  if (ri->cache_info.signing_key_cert &&
3860  ri->cache_info.signing_key_cert->signing_key_included) {
3861  keytype = "ed25519";
3863  &ri->cache_info.signing_key_cert->signing_key);
3864  } else {
3865  keytype = "rsa1024";
3866  digest_to_base64(idbuf, ri->cache_info.identity_digest);
3867  }
3868  smartlist_add_asprintf(chunks, "id %s %s\n", keytype, idbuf);
3869  }
3870 
3871  output = smartlist_join_strings(chunks, "", 0, NULL);
3872 
3873  {
3875  output+strlen(output), 0,
3876  SAVED_NOWHERE, NULL);
3877  if (smartlist_len(lst) != 1) {
3878  log_warn(LD_DIR, "We generated a microdescriptor we couldn't parse.");
3879  SMARTLIST_FOREACH(lst, microdesc_t *, md, microdesc_free(md));
3880  smartlist_free(lst);
3881  goto done;
3882  }
3883  result = smartlist_get(lst, 0);
3884  smartlist_free(lst);
3885  }
3886 
3887  done:
3888  crypto_pk_free(rsa_pubkey);
3889  tor_free(output);
3890  tor_free(key);
3891  tor_free(summary);
3892  tor_free(family);
3893  if (chunks) {
3894  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
3895  smartlist_free(chunks);
3896  }
3897  return result;
3898 }
3899 
3904 static ssize_t
3905 dirvote_format_microdesc_vote_line(char *out_buf, size_t out_buf_len,
3906  const microdesc_t *md,
3907  int consensus_method_low,
3908  int consensus_method_high)
3909 {
3910  ssize_t ret = -1;
3911  char d64[BASE64_DIGEST256_LEN+1];
3912  char *microdesc_consensus_methods =
3913  make_consensus_method_list(consensus_method_low,
3914  consensus_method_high,
3915  ",");
3916  tor_assert(microdesc_consensus_methods);
3917 
3918  if (digest256_to_base64(d64, md->digest)<0)
3919  goto out;
3920 
3921  if (tor_snprintf(out_buf, out_buf_len, "m %s sha256=%s\n",
3922  microdesc_consensus_methods, d64)<0)
3923  goto out;
3924 
3925  ret = strlen(out_buf);
3926 
3927  out:
3928  tor_free(microdesc_consensus_methods);
3929  return ret;
3930 }
3931 
3934 static const struct consensus_method_range_t {
3935  int low;
3936  int high;
3937 } microdesc_consensus_methods[] = {
3943  {-1, -1}
3944 };
3945 
3948 typedef struct microdesc_vote_line_t {
3949  int low;
3950  int high;
3951  microdesc_t *md;
3952  struct microdesc_vote_line_t *next;
3954 
3960  smartlist_t *microdescriptors_out)
3961 {
3962  const struct consensus_method_range_t *cmr;
3963  microdesc_vote_line_t *entries = NULL, *ep;
3964  vote_microdesc_hash_t *result = NULL;
3965 
3966  /* Generate the microdescriptors. */
3967  for (cmr = microdesc_consensus_methods;
3968  cmr->low != -1 && cmr->high != -1;
3969  cmr++) {
3970  microdesc_t *md = dirvote_create_microdescriptor(ri, cmr->low);
3971  if (md) {
3973  tor_malloc_zero(sizeof(microdesc_vote_line_t));
3974  e->md = md;
3975  e->low = cmr->low;
3976  e->high = cmr->high;
3977  e->next = entries;
3978  entries = e;
3979  }
3980  }
3981 
3982  /* Compress adjacent identical ones */
3983  for (ep = entries; ep; ep = ep->next) {
3984  while (ep->next &&
3985  fast_memeq(ep->md->digest, ep->next->md->digest, DIGEST256_LEN) &&
3986  ep->low == ep->next->high + 1) {
3987  microdesc_vote_line_t *next = ep->next;
3988  ep->low = next->low;
3989  microdesc_free(next->md);
3990  ep->next = next->next;
3991  tor_free(next);
3992  }
3993  }
3994 
3995  /* Format them into vote_microdesc_hash_t, and add to microdescriptors_out.*/
3996  while ((ep = entries)) {
3997  char buf[128];
3999  if (dirvote_format_microdesc_vote_line(buf, sizeof(buf), ep->md,
4000  ep->low, ep->high) >= 0) {
4001  h = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
4002  h->microdesc_hash_line = tor_strdup(buf);
4003  h->next = result;
4004  result = h;
4005  ep->md->last_listed = now;
4006  smartlist_add(microdescriptors_out, ep->md);
4007  }
4008  entries = ep->next;
4009  tor_free(ep);
4010  }
4011 
4012  return result;
4013 }
4014 
4017 static void
4019 {
4020  smartlist_t *chunks = NULL;
4021 
4022  tor_assert(ns);
4023  tor_assert(tokens);
4024  /* Commits are only present in a vote. */
4025  tor_assert(ns->type == NS_TYPE_VOTE);
4026 
4027  ns->sr_info.commits = smartlist_new();
4028 
4029  smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
4030  /* It's normal that a vote might contain no commits even if it participates
4031  * in the SR protocol. Don't treat it as an error. */
4032  if (commits == NULL) {
4033  goto end;
4034  }
4035 
4036  /* Parse the commit. We do NO validation of number of arguments or ordering
4037  * for forward compatibility, it's the parse commit job to inform us if it's
4038  * supported or not. */
4039  chunks = smartlist_new();
4040  SMARTLIST_FOREACH_BEGIN(commits, directory_token_t *, tok) {
4041  /* Extract all arguments and put them in the chunks list. */
4042  for (int i = 0; i < tok->n_args; i++) {
4043  smartlist_add(chunks, tok->args[i]);
4044  }
4045  sr_commit_t *commit = sr_parse_commit(chunks);
4046  smartlist_clear(chunks);
4047  if (commit == NULL) {
4048  /* Get voter identity so we can warn that this dirauth vote contains
4049  * commit we can't parse. */
4050  networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
4051  tor_assert(voter);
4052  log_warn(LD_DIR, "SR: Unable to parse commit %s from vote of voter %s.",
4053  escaped(tok->object_body),
4054  hex_str(voter->identity_digest,
4055  sizeof(voter->identity_digest)));
4056  /* Commitment couldn't be parsed. Continue onto the next commit because
4057  * this one could be unsupported for instance. */
4058  continue;
4059  }
4060  /* Add newly created commit object to the vote. */
4061  smartlist_add(ns->sr_info.commits, commit);
4062  } SMARTLIST_FOREACH_END(tok);
4063 
4064  end:
4065  smartlist_free(chunks);
4066  smartlist_free(commits);
4067 }
4068 
4069 /* Using the given directory tokens in tokens, parse the shared random commits
4070  * and put them in the given vote document ns.
4071  *
4072  * This also sets the SR participation flag if present in the vote. */
4073 void
4074 dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens)
4075 {
4076  /* Does this authority participates in the SR protocol? */
4077  directory_token_t *tok = find_opt_by_keyword(tokens, K_SR_FLAG);
4078  if (tok) {
4079  ns->sr_info.participate = 1;
4080  /* Get the SR commitments and reveals from the vote. */
4081  extract_shared_random_commits(ns, tokens);
4082  }
4083 }
4084 
4085 /* For the given vote, free the shared random commits if any. */
4086 void
4087 dirvote_clear_commits(networkstatus_t *ns)
4088 {
4089  tor_assert(ns->type == NS_TYPE_VOTE);
4090 
4091  if (ns->sr_info.commits) {
4092  SMARTLIST_FOREACH(ns->sr_info.commits, sr_commit_t *, c,
4093  sr_commit_free(c));
4094  smartlist_free(ns->sr_info.commits);
4095  }
4096 }
4097 
4098 /* The given url is the /tor/status-vote GET directory request. Populates the
4099  * items list with strings that we can compress on the fly and dir_items with
4100  * cached_dir_t objects that have a precompressed deflated version. */
4101 void
4102 dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items,
4103  smartlist_t *dir_items)
4104 {
4105  int current;
4106 
4107  url += strlen("/tor/status-vote/");
4108  current = !strcmpstart(url, "current/");
4109  url = strchr(url, '/');
4110  tor_assert(url);
4111  ++url;
4112  if (!strcmp(url, "consensus")) {
4113  const char *item;
4114  tor_assert(!current); /* we handle current consensus specially above,
4115  * since it wants to be spooled. */
4116  if ((item = dirvote_get_pending_consensus(FLAV_NS)))
4117  smartlist_add(items, (char*)item);
4118  } else if (!current && !strcmp(url, "consensus-signatures")) {
4119  /* XXXX the spec says that we should implement
4120  * current/consensus-signatures too. It doesn't seem to be needed,
4121  * though. */
4122  const char *item;
4123  if ((item=dirvote_get_pending_detached_signatures()))
4124  smartlist_add(items, (char*)item);
4125  } else if (!strcmp(url, "authority")) {
4126  const cached_dir_t *d;
4127  int flags = DGV_BY_ID |
4128  (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
4129  if ((d=dirvote_get_vote(NULL, flags)))
4130  smartlist_add(dir_items, (cached_dir_t*)d);
4131  } else {
4132  const cached_dir_t *d;
4133  smartlist_t *fps = smartlist_new();
4134  int flags;
4135  if (!strcmpstart(url, "d/")) {
4136  url += 2;
4137  flags = DGV_INCLUDE_PENDING | DGV_INCLUDE_PREVIOUS;
4138  } else {
4139  flags = DGV_BY_ID |
4140  (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
4141  }
4142  dir_split_resource_into_fingerprints(url, fps, NULL,
4143  DSR_HEX|DSR_SORT_UNIQ);
4144  SMARTLIST_FOREACH(fps, char *, fp, {
4145  if ((d = dirvote_get_vote(fp, flags)))
4146  smartlist_add(dir_items, (cached_dir_t*)d);
4147  tor_free(fp);
4148  });
4149  smartlist_free(fps);
4150  }
4151 }
4152 
4155 static uint32_t
4157 {
4158  uint32_t bw_kb = 0;
4159  /*
4160  * Yeah, measured bandwidths in measured_bw_line_t are (implicitly
4161  * signed) longs and the ones router_get_advertised_bandwidth() returns
4162  * are uint32_t.
4163  */
4164  long mbw_kb = 0;
4165 
4166  if (ri) {
4167  /*
4168  * * First try to see if we have a measured bandwidth; don't bother with
4169  * as_of_out here, on the theory that a stale measured bandwidth is still
4170  * better to trust than an advertised one.
4171  */
4173  &mbw_kb, NULL)) {
4174  /* Got one! */
4175  bw_kb = (uint32_t)mbw_kb;
4176  } else {
4177  /* If not, fall back to advertised */
4178  bw_kb = router_get_advertised_bandwidth(ri) / 1000;
4179  }
4180  }
4181 
4182  return bw_kb;
4183 }
4184 
4190 static int
4191 compare_routerinfo_by_ip_and_bw_(const void **a, const void **b)
4192 {
4193  routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
4194  int first_is_auth, second_is_auth;
4195  uint32_t bw_kb_first, bw_kb_second;
4196  const node_t *node_first, *node_second;
4197  int first_is_running, second_is_running;
4198 
4199  /* we return -1 if first should appear before second... that is,
4200  * if first is a better router. */
4201  if (first->addr < second->addr)
4202  return -1;
4203  else if (first->addr > second->addr)
4204  return 1;
4205 
4206  /* Potentially, this next bit could cause k n lg n memeq calls. But in
4207  * reality, we will almost never get here, since addresses will usually be
4208  * different. */
4209 
4210  first_is_auth =
4211  router_digest_is_trusted_dir(first->cache_info.identity_digest);
4212  second_is_auth =
4213  router_digest_is_trusted_dir(second->cache_info.identity_digest);
4214 
4215  if (first_is_auth && !second_is_auth)
4216  return -1;
4217  else if (!first_is_auth && second_is_auth)
4218  return 1;
4219 
4220  node_first = node_get_by_id(first->cache_info.identity_digest);
4221  node_second = node_get_by_id(second->cache_info.identity_digest);
4222  first_is_running = node_first && node_first->is_running;
4223  second_is_running = node_second && node_second->is_running;
4224 
4225  if (first_is_running && !second_is_running)
4226  return -1;
4227  else if (!first_is_running && second_is_running)
4228  return 1;
4229 
4230  bw_kb_first = dirserv_get_bandwidth_for_router_kb(first);
4231  bw_kb_second = dirserv_get_bandwidth_for_router_kb(second);
4232 
4233  if (bw_kb_first > bw_kb_second)
4234  return -1;
4235  else if (bw_kb_first < bw_kb_second)
4236  return 1;
4237 
4238  /* They're equal! Compare by identity digest, so there's a
4239  * deterministic order and we avoid flapping. */
4240  return fast_memcmp(first->cache_info.identity_digest,
4241  second->cache_info.identity_digest,
4242  DIGEST_LEN);
4243 }
4244 
4248 static digestmap_t *
4250 {
4251  const or_options_t *options = get_options();
4252  digestmap_t *omit_as_sybil;
4253  smartlist_t *routers_by_ip = smartlist_new();
4254  uint32_t last_addr;
4255  int addr_count;
4256  /* Allow at most this number of Tor servers on a single IP address, ... */
4257  int max_with_same_addr = options->AuthDirMaxServersPerAddr;
4258  if (max_with_same_addr <= 0)
4259  max_with_same_addr = INT_MAX;
4260 
4261  smartlist_add_all(routers_by_ip, routers);
4263  omit_as_sybil = digestmap_new();
4264 
4265  last_addr = 0;
4266  addr_count = 0;
4267  SMARTLIST_FOREACH_BEGIN(routers_by_ip, routerinfo_t *, ri) {
4268  if (last_addr != ri->addr) {
4269  last_addr = ri->addr;
4270  addr_count = 1;
4271  } else if (++addr_count > max_with_same_addr) {
4272  digestmap_set(omit_as_sybil, ri->cache_info.identity_digest, ri);
4273  }
4274  } SMARTLIST_FOREACH_END(ri);
4275 
4276  smartlist_free(routers_by_ip);
4277  return omit_as_sybil;
4278 }
4279 
4283 static char *
4284 version_from_platform(const char *platform)
4285 {
4286  if (platform && !strcmpstart(platform, "Tor ")) {
4287  const char *eos = find_whitespace(platform+4);
4288  if (eos && !strcmpstart(eos, " (r")) {
4289  /* XXXX Unify this logic with the other version extraction
4290  * logic in routerparse.c. */
4291  eos = find_whitespace(eos+1);
4292  }
4293  if (eos) {
4294  return tor_strndup(platform, eos-platform);
4295  }
4296  }
4297  return NULL;
4298 }
4299 
4305 char *
4307 {
4308  smartlist_t *versions;
4309  char *result;
4310  versions = smartlist_new();
4311  for ( ; ln; ln = ln->next) {
4312  smartlist_split_string(versions, ln->value, ",",
4313  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
4314  }
4315 
4316  /* Handle the case where a dirauth operator has accidentally made some
4317  * versions space-separated instead of comma-separated. */
4318  smartlist_t *more_versions = smartlist_new();
4319  SMARTLIST_FOREACH_BEGIN(versions, char *, v) {
4320  if (strchr(v, ' ')) {
4321  if (warn)
4322  log_warn(LD_DIRSERV, "Unexpected space in versions list member %s. "
4323  "(These are supposed to be comma-separated; I'll pretend you "
4324  "used commas instead.)", escaped(v));
4325  SMARTLIST_DEL_CURRENT(versions, v);
4326  smartlist_split_string(more_versions, v, NULL,
4327  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
4328  tor_free(v);
4329  }
4330  } SMARTLIST_FOREACH_END(v);
4331  smartlist_add_all(versions, more_versions);
4332  smartlist_free(more_versions);
4333 
4334  /* Check to make sure everything looks like a version. */
4335  if (warn) {
4336  SMARTLIST_FOREACH_BEGIN(versions, const char *, v) {
4337  tor_version_t ver;
4338  if (tor_version_parse(v, &ver) < 0) {
4339  log_warn(LD_DIRSERV, "Recommended version %s does not look valid. "
4340  " (I'll include it anyway, since you told me to.)",
4341  escaped(v));
4342  }
4343  } SMARTLIST_FOREACH_END(v);
4344  }
4345 
4346  sort_version_list(versions, 1);
4347  result = smartlist_join_strings(versions,",",0,NULL);
4348  SMARTLIST_FOREACH(versions,char *,s,tor_free(s));
4349  smartlist_free(versions);
4350  return result;
4351 }
4352 
4356 static void
4358 {
4359  routerinfo_t *ri2;
4360  digest256map_t *by_ed_key = digest256map_new();
4361 
4362  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
4363  ri->omit_from_vote = 0;
4364  if (ri->cache_info.signing_key_cert == NULL)
4365  continue; /* No ed key */
4366  const uint8_t *pk = ri->cache_info.signing_key_cert->signing_key.pubkey;
4367  if ((ri2 = digest256map_get(by_ed_key, pk))) {
4368  /* Duplicate; must omit one. Set the omit_from_vote flag in whichever
4369  * one has the earlier published_on. */
4370  const time_t ri_pub = ri->cache_info.published_on;
4371  const time_t ri2_pub = ri2->cache_info.published_on;
4372  if (ri2_pub < ri_pub ||
4373  (ri2_pub == ri_pub &&
4374  fast_memcmp(ri->cache_info.signed_descriptor_digest,
4375  ri2->cache_info.signed_descriptor_digest,DIGEST_LEN)<0)) {
4376  digest256map_set(by_ed_key, pk, ri);
4377  ri2->omit_from_vote = 1;
4378  } else {
4379  ri->omit_from_vote = 1;
4380  }
4381  } else {
4382  /* Add to map */
4383  digest256map_set(by_ed_key, pk, ri);
4384  }
4385  } SMARTLIST_FOREACH_END(ri);
4386 
4387  digest256map_free(by_ed_key, NULL);
4388 
4389  /* Now remove every router where the omit_from_vote flag got set. */
4390  SMARTLIST_FOREACH_BEGIN(routers, const routerinfo_t *, ri) {
4391  if (ri->omit_from_vote) {
4392  SMARTLIST_DEL_CURRENT(routers, ri);
4393  }
4394  } SMARTLIST_FOREACH_END(ri);
4395 }
4396 
4405 static void
4407 {
4408  rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
4409  rs->is_flagged_running = rs->is_named = rs->is_valid =
4410  rs->is_hs_dir = rs->is_v2_dir = rs->is_possible_guard = 0;
4411  /* FFFF we might want some mechanism to check later on if we
4412  * missed zeroing any flags: it's easy to add a new flag but
4413  * forget to add it to this clause. */
4414 }
4415 
4418  "Authority "
4419  "Exit "
4420  "Fast "
4421  "Guard "
4422  "HSDir "
4423  "Stable "
4424  "StaleDesc "
4425  "V2Dir "
4426  "Valid";
4430  "BadExit "
4431  "Running";
4432 
4437  authority_cert_t *cert)
4438 {
4439  const or_options_t *options = get_options();
4440  networkstatus_t *v3_out = NULL;
4441  uint32_t addr;
4442  char *hostname = NULL, *client_versions = NULL, *server_versions = NULL;
4443  const char *contact;
4444  smartlist_t *routers, *routerstatuses;
4445  char identity_digest[DIGEST_LEN];
4446  char signing_key_digest[DIGEST_LEN];
4447  int listbadexits = options->AuthDirListBadExits;
4449  time_t now = time(NULL);
4450  time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
4451  networkstatus_voter_info_t *voter = NULL;
4452  vote_timing_t timing;
4453  digestmap_t *omit_as_sybil = NULL;
4454  const int vote_on_reachability = running_long_enough_to_decide_unreachable();
4455  smartlist_t *microdescriptors = NULL;
4456  smartlist_t *bw_file_headers = NULL;
4457  uint8_t bw_file_digest256[DIGEST256_LEN] = {0};
4458 
4459  tor_assert(private_key);
4460  tor_assert(cert);
4461 
4462  if (crypto_pk_get_digest(private_key, signing_key_digest)<0) {
4463  log_err(LD_BUG, "Error computing signing key digest");
4464  return NULL;
4465  }
4466  if (crypto_pk_get_digest(cert->identity_key, identity_digest)<0) {
4467  log_err(LD_BUG, "Error computing identity key digest");
4468  return NULL;
4469  }
4470  if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname)<0) {
4471  log_warn(LD_NET, "Couldn't resolve my hostname");
4472  return NULL;
4473  }
4474  if (!hostname || !strchr(hostname, '.')) {
4475  tor_free(hostname);
4476  hostname = tor_dup_ip(addr);
4477  }
4478 
4479  if (options->VersioningAuthoritativeDir) {
4480  client_versions =
4481  format_recommended_version_list(options->RecommendedClientVersions, 0);
4482  server_versions =
4483  format_recommended_version_list(options->RecommendedServerVersions, 0);
4484  }
4485 
4486  contact = get_options()->ContactInfo;
4487  if (!contact)
4488  contact = "(none)";
4489 
4490  /*
4491  * Do this so dirserv_compute_performance_thresholds() and
4492  * set_routerstatus_from_routerinfo() see up-to-date bandwidth info.
4493  */
4494  if (options->V3BandwidthsFile) {
4496  NULL);
4497  } else {
4498  /*
4499  * No bandwidths file; clear the measured bandwidth cache in case we had
4500  * one last time around.
4501  */
4504  }
4505  }
4506 
4507  /* precompute this part, since we need it to decide what "stable"
4508  * means. */
4509  SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
4510  dirserv_set_router_is_running(ri, now);
4511  });
4512 
4513  routers = smartlist_new();
4514  smartlist_add_all(routers, rl->routers);
4515  routers_make_ed_keys_unique(routers);
4516  /* After this point, don't use rl->routers; use 'routers' instead. */
4517  routers_sort_by_identity(routers);
4518  omit_as_sybil = get_possible_sybil_list(routers);
4519 
4520  DIGESTMAP_FOREACH(omit_as_sybil, sybil_id, void *, ignore) {
4521  (void) ignore;
4522  rep_hist_make_router_pessimal(sybil_id, now);
4524 
4525  /* Count how many have measured bandwidths so we know how to assign flags;
4526  * this must come before dirserv_compute_performance_thresholds() */
4527  dirserv_count_measured_bws(routers);
4528 
4530 
4531  routerstatuses = smartlist_new();
4532  microdescriptors = smartlist_new();
4533 
4534  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
4535  /* If it has a protover list and contains a protocol name greater than
4536  * MAX_PROTOCOL_NAME_LENGTH, skip it. */
4537  if (ri->protocol_list &&
4538  protover_contains_long_protocol_names(ri->protocol_list)) {
4539  continue;
4540  }
4541  if (ri->cache_info.published_on >= cutoff) {
4542  routerstatus_t *rs;
4543  vote_routerstatus_t *vrs;
4544  node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
4545  if (!node)
4546  continue;
4547 
4548  vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
4549  rs = &vrs->status;
4550  set_routerstatus_from_routerinfo(rs, node, ri, now,
4551  listbadexits);
4552 
4553  if (ri->cache_info.signing_key_cert) {
4554  memcpy(vrs->ed25519_id,
4555  ri->cache_info.signing_key_cert->signing_key.pubkey,
4556  ED25519_PUBKEY_LEN);
4557  }
4558 
4559  if (digestmap_get(omit_as_sybil, ri->cache_info.identity_digest))
4561 
4562  if (!vote_on_reachability)
4563  rs->is_flagged_running = 0;
4564 
4565  vrs->version = version_from_platform(ri->platform);
4566  if (ri->protocol_list) {
4567  vrs->protocols = tor_strdup(ri->protocol_list);
4568  } else {
4569  vrs->protocols = tor_strdup(
4571  }
4573  microdescriptors);
4574 
4575  smartlist_add(routerstatuses, vrs);
4576  }
4577  } SMARTLIST_FOREACH_END(ri);
4578 
4579  {
4580  smartlist_t *added =
4582  microdescriptors, SAVED_NOWHERE, 0);
4583  smartlist_free(added);
4584  smartlist_free(microdescriptors);
4585  }
4586 
4587  smartlist_free(routers);
4588  digestmap_free(omit_as_sybil, NULL);
4589 
4590  /* Apply guardfraction information to routerstatuses. */
4591  if (options->GuardfractionFile) {
4593  routerstatuses);
4594  }
4595 
4596  /* This pass through applies the measured bw lines to the routerstatuses */
4597  if (options->V3BandwidthsFile) {
4598  /* Only set bw_file_headers when V3BandwidthsFile is configured */
4599  bw_file_headers = smartlist_new();
4601  routerstatuses, bw_file_headers,
4602  bw_file_digest256);
4603 
4604  } else {
4605  /*
4606  * No bandwidths file; clear the measured bandwidth cache in case we had
4607  * one last time around.
4608  */
4611  }
4612  }
4613 
4614  v3_out = tor_malloc_zero(sizeof(networkstatus_t));
4615 
4616  v3_out->type = NS_TYPE_VOTE;
4618  v3_out->published = now;
4619  {
4620  char tbuf[ISO_TIME_LEN+1];
4621  networkstatus_t *current_consensus =
4622  networkstatus_get_live_consensus(now);
4623  long last_consensus_interval; /* only used to pick a valid_after */
4624  if (current_consensus)
4625  last_consensus_interval = current_consensus->fresh_until -
4626  current_consensus->valid_after;
4627  else
4628  last_consensus_interval = options->TestingV3AuthInitialVotingInterval;
4629  v3_out->valid_after =
4631  (int)last_consensus_interval,
4633  format_iso_time(tbuf, v3_out->valid_after);
4634  log_notice(LD_DIR,"Choosing valid-after time in vote as %s: "
4635  "consensus_set=%d, last_interval=%d",
4636  tbuf, current_consensus?1:0, (int)last_consensus_interval);
4637  }
4638  v3_out->fresh_until = v3_out->valid_after + timing.vote_interval;
4639  v3_out->valid_until = v3_out->valid_after +
4640  (timing.vote_interval * timing.n_intervals_valid);
4641  v3_out->vote_seconds = timing.vote_delay;
4642  v3_out->dist_seconds = timing.dist_delay;
4643  tor_assert(v3_out->vote_seconds > 0);
4644  tor_assert(v3_out->dist_seconds > 0);
4645  tor_assert(timing.n_intervals_valid > 0);
4646 
4647  v3_out->client_versions = client_versions;
4648  v3_out->server_versions = server_versions;
4649 
4650  /* These are hardwired, to avoid disaster. */
4651  v3_out->recommended_relay_protocols =
4652  tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
4653  "Link=4 Microdesc=1-2 Relay=2");
4654  v3_out->recommended_client_protocols =
4655  tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
4656  "Link=4 Microdesc=1-2 Relay=2");
4657  v3_out->required_client_protocols =
4658  tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
4659  "Link=4 Microdesc=1-2 Relay=2");
4660  v3_out->required_relay_protocols =
4661  tor_strdup("Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
4662  "Link=3-4 Microdesc=1 Relay=1-2");
4663 
4664  /* We are not allowed to vote to require anything we don't have. */
4665  tor_assert(protover_all_supported(v3_out->required_relay_protocols, NULL));
4666  tor_assert(protover_all_supported(v3_out->required_client_protocols, NULL));
4667 
4668  /* We should not recommend anything we don't have. */
4669  tor_assert_nonfatal(protover_all_supported(
4670  v3_out->recommended_relay_protocols, NULL));
4671  tor_assert_nonfatal(protover_all_supported(
4672  v3_out->recommended_client_protocols, NULL));
4673 
4674  v3_out->package_lines = smartlist_new();
4675  {
4676  config_line_t *cl;
4677  for (cl = get_options()->RecommendedPackages; cl; cl = cl->next) {
4678  if (validate_recommended_package_line(cl->value))
4679  smartlist_add_strdup(v3_out->package_lines, cl->value);
4680  }
4681  }
4682 
4683  v3_out->known_flags = smartlist_new();
4686  0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
4687  if (vote_on_reachability)
4688  smartlist_add_strdup(v3_out->known_flags, "Running");
4689  if (listbadexits)
4690  smartlist_add_strdup(v3_out->known_flags, "BadExit");
4692 
4693  if (options->ConsensusParams) {
4694  v3_out->net_params = smartlist_new();
4696  options->ConsensusParams, NULL, 0, 0);
4698  }
4699  v3_out->bw_file_headers = bw_file_headers;
4700  memcpy(v3_out->bw_file_digest256, bw_file_digest256, DIGEST256_LEN);
4701 
4702  voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
4703  voter->nickname = tor_strdup(options->Nickname);
4704  memcpy(voter->identity_digest, identity_digest, DIGEST_LEN);
4705  voter->sigs = smartlist_new();
4706  voter->address = hostname;
4707  voter->addr = addr;
4708  voter->dir_port = router_get_advertised_dir_port(options, 0);
4709  voter->or_port = router_get_advertised_or_port(options);
4710  voter->contact = tor_strdup(contact);
4711  if (options->V3AuthUseLegacyKey) {
4713  if (c) {
4715  log_warn(LD_BUG, "Unable to compute digest of legacy v3 identity key");
4716  memset(voter->legacy_id_digest, 0, DIGEST_LEN);
4717  }
4718  }
4719  }
4720 
4721  v3_out->voters = smartlist_new();
4722  smartlist_add(v3_out->voters, voter);
4723  v3_out->cert = authority_cert_dup(cert);
4724  v3_out->routerstatus_list = routerstatuses;
4725  /* Note: networkstatus_digest is unset; it won't get set until we actually
4726  * format the vote. */
4727 
4728  return v3_out;
4729 }
static int compute_consensus_method(smartlist_t *votes)
Definition: dirvote.c:801
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: routerinfo_st.h:40
Header file for dirserv.c.
STATIC char * networkstatus_get_detached_signatures(smartlist_t *consensuses)
Definition: dirvote.c:2748
networkstatus_type_t type
networkstatus_voter_info_t * networkstatus_get_voter_by_id(networkstatus_t *vote, const char *identity)
const char * smartlist_get_most_frequent_string_(smartlist_t *sl, int *count_out)
Definition: smartlist.c:566
int digest256_to_base64(char *d64, const char *digest)
const cached_dir_t * dirvote_get_vote(const char *fp, int flags)
Definition: dirvote.c:3753
Header file for dirclient.c.
int dirserv_get_measured_bw_cache_size(void)
Definition: bwauth.c:164
unsigned int is_fast
document_signature_t * networkstatus_get_voter_sig_by_alg(const networkstatus_voter_info_t *voter, digest_algorithm_t alg)
Header for confline.c.
unsigned int is_valid
dircollator_t * dircollator_new(int n_votes, int n_authorities)
Definition: dircollate.c:149
STATIC char * compute_consensus_package_lines(smartlist_t *votes)
Definition: dirvote.c:2478
#define MIN_METHOD_FOR_RS_PROTOCOLS
Definition: dirvote.h:68
void * strmap_get_lc(const strmap_t *map, const char *key)
Definition: map.c:393
smartlist_t * find_all_by_keyword(const smartlist_t *s, directory_keyword k)
Definition: parsecommon.c:461
Format routerstatus entries for controller, vote, or consensus.
char * policy_summarize(smartlist_t *policy, sa_family_t family)
Definition: policies.c:2604
Header file for circuitbuild.c.
struct dir_src_ent_t dir_src_ent_t
Header file for signing.c.
Definition: node_st.h:28
Header file for voteflags.c.
void directory_get_from_all_authorities(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
Definition: dirclient.c:579
#define MIN_METHOD_FOR_INIT_BW_WEIGHTS_ONE
Definition: dirvote.h:72
saved_location_t saved_location
unsigned int is_hs_dir
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
networkstatus_sr_info_t sr_info
char * router_get_dirobj_signature(const char *digest, size_t digest_len, const crypto_pk_t *private_key)
Definition: signing.c:22
time_t dirvote_act(const or_options_t *options, time_t now)
Definition: dirvote.c:2858
uint16_t router_get_advertised_or_port(const or_options_t *options)
Definition: router.c:1427
char * V3BandwidthsFile
#define BW_WEIGHT_SCALE
Definition: or.h:1008
void crypto_digest_smartlist(char *digest_out, size_t len_out, const smartlist_t *lst, const char *append, digest_algorithm_t alg)
Header file for guardfraction.c.
Header file for nodefamily.c.
#define MIN_VOTE_INTERVAL
Definition: dirvote.h:37
networkstatus_t * networkstatus_parse_vote_from_string(const char *s, size_t len, const char **eos_out, enum networkstatus_type_t ns_type)
Definition: ns_parse.c:1060
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int tor_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
void dirserv_count_measured_bws(const smartlist_t *routers)
Definition: bwauth.c:37
static void dirvote_clear_votes(int all_votes)
Definition: dirvote.c:3086
uint8_t bw_file_digest256[DIGEST256_LEN]
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: parse_int.c:75
static const char * get_nth_protocol_set_vote(int n, const networkstatus_t *vote)
Definition: dirvote.c:1430
#define DIR_PURPOSE_FETCH_STATUS_VOTE
Definition: directory.h:50
int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
Definition: address.c:953
int VersioningAuthoritativeDir
char identity_digest[DIGEST_LEN]
#define LOG_INFO
Definition: log.h:42
int get_n_authorities(dirinfo_type_t type)
Definition: dirlist.c:55
Header file for nodelist.c.
const uint8_t * smartlist_get_most_frequent_digest256(smartlist_t *sl)
Definition: smartlist.c:854
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:631
static void dirvote_fetch_missing_votes(void)
Definition: dirvote.c:3009
Header file for directory.c.
static char * pending_consensus_signatures
Definition: dirvote.c:2952
int AuthDirListBadExits
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_sort_digests256(smartlist_t *sl)
Definition: smartlist.c:846
int ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
int digest256_from_base64(char *digest, const char *d64)
static int compare_orports_(const void **_a, const void **_b)
Definition: dirvote.c:666
void voting_schedule_recalculate_timing(const or_options_t *options, time_t now)
Header file for config.c.
struct pending_consensus_t pending_consensus_t
Header file for authcert.c.
static networkstatus_voter_info_t * get_voter(const networkstatus_t *vote)
Definition: dirvote.c:542
static smartlist_t * previous_vote_list
Definition: dirvote.c:2945
smartlist_t * net_params
int crypto_pk_write_public_key_to_string(crypto_pk_t *env, char **dest, size_t *len)
Definition: crypto_rsa.c:466
#define HEX_DIGEST256_LEN
Definition: crypto_digest.h:37
int networkstatus_check_document_signature(const networkstatus_t *consensus, document_signature_t *sig, const authority_cert_t *cert)
const char * find_whitespace(const char *s)
Definition: util_string.c:347
uint32_t bandwidth_kb
static char * compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
Definition: dirvote.c:869
ed25519_public_key_t signing_key
Definition: torcert.h:28
Header file for microdesc.c.
void smartlist_uniq_strings(smartlist_t *sl)
Definition: smartlist.c:574
void routers_sort_by_identity(smartlist_t *routers)
Definition: routerlist.c:3207
char signed_descriptor_digest[DIGEST_LEN]
int digest_to_base64(char *d64, const char *digest)
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:209
networkstatus_t * dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key, authority_cert_t *cert)
Definition: dirvote.c:4436
void directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose, dirinfo_type_t type, const char *payload, size_t payload_len, size_t extrainfo_len)
Definition: dirclient.c:228
#define tor_free(p)
Definition: malloc.h:52
#define MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS
Definition: dirvote.h:86
common_digests_t digests
static char * format_protocols_lines_for_vote(const networkstatus_t *v3_ns)
Definition: dirvote.c:180
static bw_weights_error_t networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg, int64_t Wme, int64_t Wmd, int64_t Wee, int64_t Wed, int64_t scale, int64_t G, int64_t M, int64_t E, int64_t D, int64_t T, int64_t margin, int do_balance)
Definition: dirvote.c:1037
int smartlist_string_pos(const smartlist_t *sl, const char *element)
Definition: smartlist.c:106
#define DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
Definition: directory.h:53
char * routerstatus_format_entry(const routerstatus_t *rs, const char *version, const char *protocols, routerstatus_format_type_t format, int consensus_method, const vote_routerstatus_t *vrs)
#define LOG_NOTICE
Definition: log.h:47
struct tor_cert_st * signing_key_cert
#define SMARTLIST_DEL_CURRENT(sl, var)
int AuthDirMaxServersPerAddr
Header file for microdesc_parse.c.
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
void * strmap_set_lc(strmap_t *map, const char *key, void *val)
Definition: map.c:379
pending_vote_t * dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
Definition: dirvote.c:3150
void dirserv_clear_measured_bw_cache(void)
Definition: bwauth.c:101
smartlist_t * declared_family
Definition: routerinfo_st.h:62
#define DIR_PURPOSE_UPLOAD_SIGNATURES
Definition: directory.h:47
unsigned int has_guardfraction
Header file for versions.c.
MOCK_IMPL(const char *, dirvote_get_pending_consensus,(consensus_flavor_t flav))
Definition: dirvote.c:3729
int tor_version_parse(const char *s, tor_version_t *out)
Definition: versions.c:206
signed_descriptor_t cache_info
static vote_routerstatus_t * compute_routerstatus_consensus(smartlist_t *votes, int consensus_method, char *microdesc_digest256_out, tor_addr_port_t *best_alt_orport_out)
Definition: dirvote.c:685
unsigned int is_authority
crypto_pk_t * identity_key
unsigned signing_key_included
Definition: torcert.h:40
int validate_recommended_package_line(const char *line)
Definition: recommend_pkg.c:38
time_t voting_schedule_get_start_of_next_interval(time_t now, int interval, int offset)
int trusted_dirs_load_certs_from_string(const char *contents, int source, int flush, const char *source_dir)
Definition: authcert.c:373
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags)
Definition: binascii.c:215
int V3AuthNIntervalsValid
Header file for directory authority mode.
#define MAX_BW_FILE_HEADER_COUNT_IN_VOTE
Definition: bwauth.h:16
int dirserv_read_measured_bandwidths(const char *from_file, smartlist_t *routerstatuses, smartlist_t *bw_file_headers, uint8_t *digest_out)
Definition: bwauth.c:207
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Header file for policies.c.
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)
Definition: map.h:153
unsigned int is_named
STATIC char * make_consensus_method_list(int low, int high, const char *separator)
Definition: dirvote.c:845
void update_consensus_router_descriptor_downloads(time_t now, int is_vote, networkstatus_t *consensus)
Definition: routerlist.c:2526
void smartlist_uniq(smartlist_t *sl, int(*compare)(const void **a, const void **b), void(*free_fn)(void *a))
Definition: smartlist.c:390
static void dirvote_clear_pending_consensuses(void)
Definition: dirvote.c:3072
tor_assert(buffer)
unsigned int is_running
Definition: node_st.h:57
static int compare_vote_rs_(const void **_a, const void **_b)
Definition: dirvote.c:658
Header file for recommend_pkg.c.
void cached_dir_decref(cached_dir_t *d)
Definition: dirserv.c:186
#define LD_CIRC
Definition: log.h:79
Code to parse and validate detached-signature objects.
smartlist_t * microdescs_parse_from_string(const char *s, const char *eos, int allow_annotations, saved_location_t where, smartlist_t *invalid_digests_out)
#define DIR_PURPOSE_UPLOAD_VOTE
Definition: directory.h:45
Header for crypto_format.c.
static int dirvote_publish_consensus(void)
Definition: dirvote.c:3677
struct vote_microdesc_hash_t * next
crypto_pk_t * get_my_v3_legacy_signing_key(void)
Definition: router.c:449
int networkstatus_verify_bw_weights(networkstatus_t *ns, int)
Definition: ns_parse.c:592
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1168
STATIC authority_cert_t * authority_cert_dup(authority_cert_t *cert)
Definition: dirvote.c:142
static char * version_from_platform(const char *platform)
Definition: dirvote.c:4284
smartlist_t * exit_policy
Definition: routerinfo_st.h:56
#define DIGEST_LEN
Definition: digest_sizes.h:20
static int cmp_int_strings_(const void **_a, const void **_b)
Definition: dirvote.c:782
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
int V3AuthVotingInterval
struct microdesc_vote_line_t microdesc_vote_line_t
Master header file for Tor-specific functionality.
static ssize_t 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)
Definition: dirvote.c:3905
char * Nickname
Definition: or_options_st.h:68
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
#define LD_DIRSERV
Definition: log.h:87
int networkstatus_check_consensus_signature(networkstatus_t *consensus, int warn)
Header file for bwauth.c.
size_t onion_pkey_len
Definition: routerinfo_st.h:36
uint16_t router_get_advertised_dir_port(const or_options_t *options, uint16_t dirport)
Definition: router.c:1457
void dircollator_collate(dircollator_t *dc, int consensus_method)
Definition: dircollate.c:211
static char * get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending, int n_flavors)
Definition: dirvote.c:2837
static digestmap_t * get_possible_sybil_list(const smartlist_t *routers)
Definition: dirvote.c:4249
int V3AuthUseLegacyKey
Header file for rephist.c.
void smartlist_remove(smartlist_t *sl, const void *element)
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1180
char signing_key_digest[DIGEST_LEN]
crypto_pk_t * signing_key
tor_addr_t ipv6_addr
#define LOG_WARN
Definition: log.h:50
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:769
char nickname[MAX_NICKNAME_LEN+1]
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:476
#define get_most_frequent_member(lst)
Definition: dirvote.c:609
void dirvote_free_all(void)
Definition: dirvote.c:3706
static void extract_shared_random_commits(networkstatus_t *ns, const smartlist_t *tokens)
Definition: dirvote.c:4018
char * nodefamily_canonicalize(const char *s, const uint8_t *rsa_id_self, unsigned flags)
Definition: nodefamily.c:111
#define MIN_VOTE_SECONDS
Definition: dirvote.h:27
void tor_free_(void *mem)
Definition: malloc.c:227
STATIC int networkstatus_add_detached_signatures(networkstatus_t *target, ns_detached_signatures_t *sigs, const char *source, int severity, const char **msg_out)
Definition: dirvote.c:2556
unsigned int is_exit
crypto_pk_t * crypto_pk_dup_key(crypto_pk_t *orig)
#define MIN_SUPPORTED_CONSENSUS_METHOD
Definition: dirvote.h:57
smartlist_t * known_flags
Header file for voting_schedule.c.
void dircollator_add_vote(dircollator_t *dc, networkstatus_t *v)
Definition: dircollate.c:194
#define MAX_SUPPORTED_CONSENSUS_METHOD
Definition: dirvote.h:60
char * onion_pkey
Definition: routerinfo_st.h:34
directory_token_t * find_opt_by_keyword(const smartlist_t *s, directory_keyword keyword)
Definition: parsecommon.c:450
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
void dirserv_compute_performance_thresholds(digestmap_t *omit_as_sybil)
Definition: voteflags.c:235
int resolve_my_address(int warn_severity, const or_options_t *options, uint32_t *addr_out, const char **method_out, char **hostname_out)
Definition: config.c:2741
Header for order.c.
Headers and type declarations for protover.c.
smartlist_t * voters
static int compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
Definition: dirvote.c:616
static int consensus_method_is_supported(int method)
Definition: dirvote.c:836
#define DIGESTMAP_FOREACH_END
Definition: map.h:167
bool protover_contains_long_protocol_names(const char *s)
Definition: protover.c:304
smartlist_t * routerstatus_list
unsigned int has_bandwidth
#define MAX_BW_FILE_HEADERS_LINE_LEN
Definition: dirvote.h:99
char descriptor_digest[DIGEST256_LEN]
void sort_version_list(smartlist_t *versions, int remove_duplicates)
Definition: versions.c:391
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
struct short_policy_t * ipv6_exit_policy
Definition: routerinfo_st.h:60
#define MIN_VOTES_FOR_PARAM
Definition: dirvote.c:924
unsigned int is_possible_guard
#define LD_DIR
Definition: log.h:85
static int vote_routerstatus_find_microdesc_hash(char *digest256_out, const vote_routerstatus_t *vrs, int method, digest_algorithm_t alg)
Definition: dirvote.c:496
#define DEFAULT_MAX_UNMEASURED_BW_KB
Definition: dirvote.h:91
consensus_flavor_t flavor
authority_cert_t * authority_cert_get_by_digests(const char *id_digest, const char *sk_digest)
Definition: authcert.c:650
Header file for parsecommon.c.
STATIC microdesc_t * dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
Definition: dirvote.c:3799
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
cached_dir_t * new_cached_dir(char *s, time_t published)
Definition: dirserv.c:197
char identity_digest[DIGEST_LEN]
static int compare_dir_src_ents_by_authority_id_(const void **_a, const void **_b)
Definition: dirvote.c:574
int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
Definition: crypto_rsa.c:229
uint8_t ed25519_id[ED25519_PUBKEY_LEN]
Header file for ns_parse.c.
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
#define ROUTER_MAX_AGE_TO_PUBLISH
Definition: or.h:162
int dirserv_query_measured_bw_cache_kb(const char *node_id, long *bw_kb_out, time_t *as_of_out)
Definition: bwauth.c:136
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
size_t dir_len
Definition: cached_dir_st.h:15
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
microdesc_cache_t * get_microdesc_cache(void)
Definition: microdesc.c:245
static void clear_status_flags_on_sybil(routerstatus_t *rs)
Definition: dirvote.c:4406
Header file for dircollate.c.
Header file for dirvote.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
static char * networkstatus_format_signatures(networkstatus_t *consensus, int for_detached_signatures)
Definition: dirvote.c:2687
Header file for router.c.
vote_routerstatus_t ** dircollator_get_votes_for_router(dircollator_t *dc, int idx)
Definition: dircollate.c:320
tor_addr_port_t * tor_addr_port_new(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1937
int networkstatus_set_current_consensus(const char *consensus, size_t consensus_len, const char *flavor, unsigned flags, const char *source_dir)
const char * escaped(const char *s)
Definition: escape.c:126
unsigned int has_exitsummary
routerstatus_format_type_t
#define FINGERPRINT_LEN
Definition: crypto_rsa.h:34
static char * compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes)
Definition: dirvote.c:1448
networkstatus_t * consensus
Definition: dirvote.c:113
consensus_flavor_t
Definition: or.h:863
unsigned int is_stable
#define log_fn(severity, domain, args,...)
Definition: log.h:256
static void update_total_bandwidth_weights(const routerstatus_t *rs, int is_exit, int is_guard, int64_t *G, int64_t *M, int64_t *E, int64_t *D, int64_t *T)
Definition: dirvote.c:1346
crypto_pk_t * get_my_v3_authority_signing_key(void)
Definition: router.c:432
int dir_split_resource_into_fingerprints(const char *resource, smartlist_t *fp_out, int *compressed_out, int flags)
Definition: directory.c:544
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
authority_cert_t * get_my_v3_legacy_cert(void)
Definition: router.c:441
STATIC int32_t dirvote_get_intermediate_param_value(const smartlist_t *param_list, const char *keyword, int32_t default_val)
Definition: dirvote.c:894
static int compare_votes_by_authority_id_(const void **_a, const void **_b)
Definition: dirvote.c:563
int protover_all_supported(const char *s, char **missing_out)
Definition: protover.c:746
const char * protover_compute_for_old_tor(const char *version)
C_RUST_COUPLED: src/rust/protover/protover.rs compute_for_old_tor
Definition: protover.c:899
void rep_hist_make_router_pessimal(const char *id, time_t when)
Definition: rephist.c:368
Definition: or.h:890
static uint32_t dirserv_get_bandwidth_for_router_kb(const routerinfo_t *ri)
Definition: dirvote.c:4156
vote_microdesc_hash_t * microdesc
#define fast_memcmp(a, b, c)
Definition: di_ops.h:26
ns_detached_signatures_t * networkstatus_parse_detached_signatures(const char *s, const char *eos)
Definition: dsigs_parse.c:67
static int compare_routerinfo_by_ip_and_bw_(const void **a, const void **b)
Definition: dirvote.c:4191
char digest[DIGEST256_LEN]
Definition: microdesc_st.h:53
STATIC char * format_networkstatus_vote(crypto_pk_t *private_signing_key, networkstatus_t *v3_ns)
Definition: dirvote.c:219
#define MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC
Definition: dirvote.h:80
smartlist_t * package_lines
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
int networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G, int64_t M, int64_t E, int64_t D, int64_t T, int64_t weight_scale)
Definition: dirvote.c:1106
char * write_short_policy(const short_policy_t *policy)
Definition: policies.c:2817
Header file for dirlist.c.
static smartlist_t * pending_consensus_signature_list
Definition: dirvote.c:2956
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: parse_int.c:56
const char DIRVOTE_UNIVERSAL_FLAGS[]
Definition: dirvote.c:4417
#define N_CONSENSUS_FLAVORS
Definition: or.h:869
uint32_t addr
Definition: routerinfo_st.h:19
unsigned int omit_from_vote
Definition: routerinfo_st.h:87
int dirserv_read_guardfraction_file(const char *fname, smartlist_t *vote_routerstatuses)
#define MAX(a, b)
Definition: cmp.h:22
uint16_t ipv6_orport
#define MIN_DIST_SECONDS
Definition: dirvote.h:32
static void dirvote_fetch_missing_signatures(void)
Definition: dirvote.c:3049
unsigned int bw_is_unmeasured
static int dirvote_compute_consensuses(void)
Definition: dirvote.c:3343
int tor_digest256_is_zero(const char *digest)
Definition: util_string.c:106
unsigned int is_flagged_running
#define LD_NET
Definition: log.h:63
static int dirvote_add_signatures_to_pending_consensus(pending_consensus_t *pc, ns_detached_signatures_t *sigs, const char *source, int severity, const char **msg_out)
Definition: dirvote.c:3513
vote_microdesc_hash_t * dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now, smartlist_t *microdescriptors_out)
Definition: dirvote.c:3959
STATIC char * networkstatus_compute_consensus(smartlist_t *votes, int total_authorities, crypto_pk_t *identity_key, crypto_pk_t *signing_key, const char *legacy_id_key_digest, crypto_pk_t *legacy_signing_key, consensus_flavor_t flavor)
Definition: dirvote.c:1511
static smartlist_t * pending_vote_list
Definition: dirvote.c:2942
struct pending_vote_t pending_vote_t
char * GuardfractionFile
static void get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
Definition: dirvote.c:589
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:27
char * recommended_relay_protocols
int dirvote_add_signatures(const char *detached_signatures_body, const char *source, const char **msg)
Definition: dirvote.c:3653
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:903
STATIC smartlist_t * dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
Definition: dirvote.c:930
smartlist_t * microdescs_add_list_to_cache(microdesc_cache_t *cache, smartlist_t *descriptors, saved_location_t where, int no_save)
Definition: microdesc.c:377
int TestingV3AuthVotingStartOffset
char * dirserv_get_flag_thresholds_line(void)
Definition: voteflags.c:426
document_signature_t * document_signature_dup(const document_signature_t *sig)
const char * networkstatus_get_flavor_name(consensus_flavor_t flav)
char * ConsensusParams
static void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
Definition: dirvote.c:476
void set_routerstatus_from_routerinfo(routerstatus_t *rs, node_t *node, const routerinfo_t *ri, time_t now, int listbadexits)
Definition: voteflags.c:556
void smartlist_clear(smartlist_t *sl)
static void routers_make_ed_keys_unique(smartlist_t *routers)
Definition: dirvote.c:4357
static crypto_pk_t * legacy_signing_key
Definition: router.c:126
uint32_t guardfraction_percentage
#define MIN_VOTE_INTERVAL_TESTING
Definition: dirvote.h:46
uint32_t router_get_advertised_bandwidth(const routerinfo_t *router)
Definition: routerlist.c:557
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
int dircollator_n_routers(dircollator_t *dc)
Definition: dircollate.c:305
smartlist_t * bw_file_headers
char * protover_compute_vote(const smartlist_t *list_of_proto_strings, int threshold)
Definition: protover.c:669
static char * list_v3_auth_ids(void)
Definition: dirvote.c:3128
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:809
struct authority_cert_t * cert
char * format_recommended_version_list(const config_line_t *ln, int warn)
Definition: dirvote.c:4306
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:83
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20
smartlist_t * routers
Definition: routerlist_st.h:27
Header file for routerlist.c.
const char DIRVOTE_OPTIONAL_FLAGS[]
Definition: dirvote.c:4429
static int dirvote_perform_vote(void)
Definition: dirvote.c:2961
int TestingV3AuthInitialVotingInterval
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
unsigned int ed25519_reflects_consensus
char * tor_dup_ip(uint32_t addr)
Definition: address.c:1890
#define T(s, t, a, o)
Definition: parsecommon.h:246