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