Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

tlyu / tor / 2361

18 Dec 2018 - 19:57 coverage decreased (-0.01%) to 60.744%
2361

push

travis-ci

739ebe3feeaa3fc6a86160d22121e5c2?size=18&default=identicontlyu
The big bootstrap phase redefinition

43657 of 71870 relevant lines covered (60.74%)

44782.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.94
/src/feature/dirauth/dirvote.c
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"
14
#include "feature/dirauth/dircollate.h"
15
#include "feature/dirauth/dsigs_parse.h"
16
#include "feature/dirauth/guardfraction.h"
17
#include "feature/dirauth/recommend_pkg.h"
18
#include "feature/dirauth/voteflags.h"
19
#include "feature/dircache/dirserv.h"
20
#include "feature/dirclient/dirclient.h"
21
#include "feature/dircommon/directory.h"
22
#include "feature/dirparse/microdesc_parse.h"
23
#include "feature/dirparse/ns_parse.h"
24
#include "feature/dirparse/parsecommon.h"
25
#include "feature/dirparse/signing.h"
26
#include "feature/nodelist/authcert.h"
27
#include "feature/nodelist/dirlist.h"
28
#include "feature/nodelist/fmt_routerstatus.h"
29
#include "feature/nodelist/microdesc.h"
30
#include "feature/nodelist/networkstatus.h"
31
#include "feature/nodelist/nodefamily.h"
32
#include "feature/nodelist/nodelist.h"
33
#include "feature/nodelist/routerlist.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"
39
#include "feature/dircommon/voting_schedule.h"
40

41
#include "feature/dirauth/dirvote.h"
42
#include "feature/dirauth/authmode.h"
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"
62
#include "lib/crypt_ops/crypto_format.h"
63

64
/**
65
 * \file dirvote.c
66
 * \brief Functions to compute directory consensus, and schedule voting.
67
 *
68
 * This module is the center of the consensus-voting based directory
69
 * authority system.  With this system, a set of authorities first
70
 * publish vote based on their opinions of the network, and then compute
71
 * a consensus from those votes.  Each authority signs the consensus,
72
 * and clients trust the consensus if enough known authorities have
73
 * signed it.
74
 *
75
 * The code in this module is only invoked on directory authorities.  It's
76
 * responsible for:
77
 *
78
 * <ul>
79
 *   <li>Generating this authority's vote networkstatus, based on the
80
 *       authority's view of the network as represented in dirserv.c
81
 *   <li>Formatting the vote networkstatus objects.
82
 *   <li>Generating the microdescriptors that correspond to our own
83
 *       vote.
84
 *   <li>Sending votes to all the other authorities.
85
 *   <li>Trying to fetch missing votes from other authorities.
86
 *   <li>Computing the consensus from a set of votes, as well as
87
 *       a "detached signature" object for other authorities to fetch.
88
 *   <li>Collecting other authorities' signatures on the same consensus,
89
 *       until there are enough.
90
 *   <li>Publishing the consensus to the reset of the directory system.
91
 *   <li>Scheduling all of the above operations.
92
 * </ul>
93
 *
94
 * The main entry points are in dirvote_act(), which handles scheduled
95
 * actions; and dirvote_add_vote() and dirvote_add_signatures(), which
96
 * handle uploaded and downloaded votes and signatures.
97
 *
98
 * (See dir-spec.txt from torspec.git for a complete specification of
99
 * the directory protocol and voting algorithms.)
100
 **/
101

102
/** A consensus that we have built and are appending signatures to.  Once it's
103
 * time to publish it, it will become an active consensus if it accumulates
104
 * enough signatures. */
105
typedef struct pending_consensus_t {
106
  /** The body of the consensus that we're currently building.  Once we
107
   * have it built, it goes into dirserv.c */
108
  char *body;
109
  /** The parsed in-progress consensus document. */
110
  networkstatus_t *consensus;
111
} pending_consensus_t;
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);
118
static int dirvote_add_signatures_to_pending_consensus(
119
                       pending_consensus_t *pc,
120
                       ns_detached_signatures_t *sigs,
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

136
/** Allocate and return a new authority_cert_t with the same contents as
137
 * <b>cert</b>. */
138
STATIC authority_cert_t *
139
authority_cert_dup(authority_cert_t *cert)
27×
140
{
141
  authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
27×
142
  tor_assert(cert);
27×
143

144
  memcpy(out, cert, sizeof(authority_cert_t));
145
  /* Now copy pointed-to things. */
146
  out->cache_info.signed_descriptor_body =
27×
147
    tor_strndup(cert->cache_info.signed_descriptor_body,
27×
148
                cert->cache_info.signed_descriptor_len);
149
  out->cache_info.saved_location = SAVED_NOWHERE;
27×
150
  out->identity_key = crypto_pk_dup_key(cert->identity_key);
27×
151
  out->signing_key = crypto_pk_dup_key(cert->signing_key);
27×
152

153
  return out;
27×
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)
162×
164
{
165
  if (opt_value) {
162×
166
    char *result = NULL;
36×
167
    tor_asprintf(&result, "%s %s\n", keyword, opt_value);
36×
168
    return result;
36×
169
  } else {
170
    return tor_strdup("");
126×
171
  }
172
}
173

174
/** Format the recommended/required-relay-client protocols lines for a vote in
175
 * a newly allocated string, and return that string. */
176
static char *
177
format_protocols_lines_for_vote(const networkstatus_t *v3_ns)
27×
178
{
179
  char *recommended_relay_protocols_line = NULL;
27×
180
  char *recommended_client_protocols_line = NULL;
27×
181
  char *required_relay_protocols_line = NULL;
27×
182
  char *required_client_protocols_line = NULL;
27×
183

184
  recommended_relay_protocols_line =
27×
185
    format_line_if_present("recommended-relay-protocols",
27×
186
                           v3_ns->recommended_relay_protocols);
27×
187
  recommended_client_protocols_line =
27×
188
    format_line_if_present("recommended-client-protocols",
27×
189
                           v3_ns->recommended_client_protocols);
27×
190
  required_relay_protocols_line =
27×
191
    format_line_if_present("required-relay-protocols",
27×
192
                           v3_ns->required_relay_protocols);
27×
193
  required_client_protocols_line =
27×
194
    format_line_if_present("required-client-protocols",
27×
195
                           v3_ns->required_client_protocols);
27×
196

197
  char *result = NULL;
27×
198
  tor_asprintf(&result, "%s%s%s%s",
27×
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);
27×
205
  tor_free(recommended_client_protocols_line);
27×
206
  tor_free(required_relay_protocols_line);
27×
207
  tor_free(required_client_protocols_line);
27×
208

209
  return result;
27×
210
}
211

212
/** Return a new string containing the string representation of the vote in
213
 * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
214
 * For v3 authorities. */
215
STATIC char *
216
format_networkstatus_vote(crypto_pk_t *private_signing_key,
27×
217
                          networkstatus_t *v3_ns)
218
{
219
  smartlist_t *chunks = smartlist_new();
27×
220
  char *packages = NULL;
27×
221
  char fingerprint[FINGERPRINT_LEN+1];
222
  char digest[DIGEST_LEN];
223
  uint32_t addr;
224
  char *protocols_lines = NULL;
27×
225
  char *client_versions_line = NULL, *server_versions_line = NULL;
27×
226
  char *shared_random_vote_str = NULL;
27×
227
  networkstatus_voter_info_t *voter;
228
  char *status = NULL;
27×
229

230
  tor_assert(private_signing_key);
27×
231
  tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
27×
232

233
  voter = smartlist_get(v3_ns->voters, 0);
27×
234

235
  addr = voter->addr;
27×
236

237
  base16_encode(fingerprint, sizeof(fingerprint),
27×
238
                v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
27×
239

240
  client_versions_line = format_line_if_present("client-versions",
27×
241
                                                v3_ns->client_versions);
27×
242
  server_versions_line = format_line_if_present("server-versions",
27×
243
                                                v3_ns->server_versions);
27×
244
  protocols_lines = format_protocols_lines_for_vote(v3_ns);
27×
245

246
  if (v3_ns->package_lines) {
27×
247
    smartlist_t *tmp = smartlist_new();
!
248
    SMARTLIST_FOREACH(v3_ns->package_lines, const char *, p,
!
249
                      if (validate_recommended_package_line(p))
250
                        smartlist_add_asprintf(tmp, "package %s\n", p));
251
    smartlist_sort_strings(tmp);
!
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("");
27×
257
  }
258

259
    /* Get shared random commitments/reveals line(s). */
260
  shared_random_vote_str = sr_get_string_for_vote();
27×
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);
27×
268
    /* XXXX Abstraction violation: should be pulling a field out of v3_ns.*/
269
    char *flag_thresholds = dirserv_get_flag_thresholds_line();
27×
270
    char *params;
271
    char *bw_headers_line = NULL;
27×
272
    authority_cert_t *cert = v3_ns->cert;
27×
273
    char *methods =
27×
274
      make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD,
27×
275
                                 MAX_SUPPORTED_CONSENSUS_METHOD, " ");
276
    format_iso_time(published, v3_ns->published);
27×
277
    format_iso_time(va, v3_ns->valid_after);
27×
278
    format_iso_time(fu, v3_ns->fresh_until);
27×
279
    format_iso_time(vu, v3_ns->valid_until);
27×
280

281
    if (v3_ns->net_params)
27×
282
      params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
27×
283
    else
284
      params = tor_strdup("");
!
285
    tor_assert(cert);
27×
286

287
    /* v3_ns->bw_file_headers is only set when V3BandwidthsFile is
288
     * configured */
289
    if (v3_ns->bw_file_headers) {
27×
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)
!
293
                > MAX_BW_FILE_HEADER_COUNT_IN_VOTE)) {
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,
108×
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",
27×
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,
54×
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);
27×
351
    tor_free(flags);
27×
352
    tor_free(flag_thresholds);
27×
353
    tor_free(methods);
27×
354
    tor_free(shared_random_vote_str);
27×
355
    tor_free(bw_headers_line);
27×
356

357
    if (!tor_digest_is_zero(voter->legacy_id_digest)) {
27×
358
      char fpbuf[HEX_DIGEST_LEN+1];
359
      base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
9×
360
      smartlist_add_asprintf(chunks, "legacy-dir-key %s\n", fpbuf);
9×
361
    }
362

363
    smartlist_add(chunks, tor_strndup(cert->cache_info.signed_descriptor_body,
27×
364
                                      cert->cache_info.signed_descriptor_len));
365
  }
366

367
  SMARTLIST_FOREACH_BEGIN(v3_ns->routerstatus_list, vote_routerstatus_t *,
135×
368
                          vrs) {
369
    char *rsf;
370
    vote_microdesc_hash_t *h;
371
    rsf = routerstatus_format_entry(&vrs->status,
108×
372
                                    vrs->version, vrs->protocols,
108×
373
                                    NS_V3_VOTE,
374
                                    ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD,
375
                                    vrs);
376
    if (rsf)
108×
377
      smartlist_add(chunks, rsf);
108×
378

379
    for (h = vrs->microdesc; h; h = h->next) {
216×
380
      smartlist_add_strdup(chunks, h->microdesc_hash_line);
108×
381
    }
382
  } SMARTLIST_FOREACH_END(vrs);
108×
383

384
  smartlist_add_strdup(chunks, "directory-footer\n");
27×
385

386
  /* The digest includes everything up through the space after
387
   * directory-signature.  (Yuck.) */
388
  crypto_digest_smartlist(digest, DIGEST_LEN, chunks,
27×
389
                          "directory-signature ", DIGEST_SHA1);
390

391
  {
392
    char signing_key_fingerprint[FINGERPRINT_LEN+1];
393
    if (crypto_pk_get_fingerprint(private_signing_key,
27×
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,
27×
400
                           signing_key_fingerprint);
401
  }
402

403
  {
404
    char *sig = router_get_dirobj_signature(digest, DIGEST_LEN,
27×
405
                                            private_signing_key);
406
    if (!sig) {
27×
407
      log_warn(LD_BUG, "Unable to sign networkstatus vote.");
!
408
      goto err;
!
409
    }
410
    smartlist_add(chunks, sig);
27×
411
  }
412

413
  status = smartlist_join_strings(chunks, "", 0, NULL);
27×
414

415
  {
416
    networkstatus_t *v;
417
    if (!(v = networkstatus_parse_vote_from_string(status, strlen(status),
27×
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);
27×
426
  }
427

428
  goto done;
27×
429

430
 err:
431
  tor_free(status);
!
432
 done:
433
  tor_free(client_versions_line);
27×
434
  tor_free(server_versions_line);
27×
435
  tor_free(protocols_lines);
27×
436
  tor_free(packages);
27×
437

438
  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
27×
439
  smartlist_free(chunks);
27×
440
  return status;
27×
441
}
442

443
/** Set *<b>timing_out</b> to the intervals at which we would like to vote.
444
 * Note that these aren't the intervals we'll use to vote; they're the ones
445
 * that we'll vote to use. */
446
static void
447
dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
!
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

463
/** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
464
 * the digest algorithm <b>alg</b>, decode it and copy it into
465
 * <b>digest256_out</b> and return 0.  Otherwise return -1. */
466
static int
467
vote_routerstatus_find_microdesc_hash(char *digest256_out,
258×
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);
258×
479
  mlen = strlen(mstr);
258×
480
  tor_snprintf(dstr, sizeof(dstr), " %s=",
258×
481
               crypto_digest_algorithm_get_name(alg));
482

483
  for (h = vrs->microdesc; h; h = h->next) {
258×
484
    const char *cp = h->microdesc_hash_line;
258×
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");
1,146×
490
      if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
1,146×
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);
258×
496
        if (!cp)
258×
497
          return -1;
498
        cp += strlen(dstr);
258×
499
        strlcpy(buf, cp, sizeof(buf));
258×
500
        return digest256_from_base64(digest256_out, buf);
258×
501
      }
502
      if (num_len == 0 || cp[num_len] != ',')
888×
503
        break;
504
      cp += num_len + 1;
888×
505
    }
506
  }
507
  return -1;
508
}
509

510
/** Given a vote <b>vote</b> (not a consensus!), return its associated
511
 * networkstatus_voter_info_t. */
512
static networkstatus_voter_info_t *
513
get_voter(const networkstatus_t *vote)
630×
514
{
515
  tor_assert(vote);
630×
516
  tor_assert(vote->type == NS_TYPE_VOTE);
630×
517
  tor_assert(vote->voters);
630×
518
  tor_assert(smartlist_len(vote->voters) == 1);
630×
519
  return smartlist_get(vote->voters, 0);
630×
520
}
521

522
/** Temporary structure used in constructing a list of dir-source entries
523
 * for a consensus.  One of these is generated for every vote, and one more
524
 * for every legacy key in each vote. */
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

531
/** Helper for sorting networkstatus_t votes (not consensuses) by the
532
 * hash of their voters' identity digests. */
533
static int
534
compare_votes_by_authority_id_(const void **_a, const void **_b)
60×
535
{
536
  const networkstatus_t *a = *_a, *b = *_b;
60×
537
  return fast_memcmp(get_voter(a)->identity_digest,
60×
538
                get_voter(b)->identity_digest, DIGEST_LEN);
539
}
540

541
/** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
542
 * their identity digests, and return -1, 0, or 1 depending on their
543
 * ordering */
544
static int
545
compare_dir_src_ents_by_authority_id_(const void **_a, const void **_b)
120×
546
{
547
  const dir_src_ent_t *a = *_a, *b = *_b;
120×
548
  const networkstatus_voter_info_t *a_v = get_voter(a->v),
120×
549
    *b_v = get_voter(b->v);
120×
550
  const char *a_id, *b_id;
551
  a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
120×
552
  b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
120×
553

554
  return fast_memcmp(a_id, b_id, DIGEST_LEN);
120×
555
}
556

557
/** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
558
 * that occurs more than <b>min</b> times. */
559
static void
560
get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
72×
561
{
562
  char *cur = NULL;
72×
563
  int count = 0;
72×
564
  SMARTLIST_FOREACH_BEGIN(in, char *, cp) {
636×
565
    if (cur && !strcmp(cp, cur)) {
564×
566
      ++count;
288×
567
    } else {
568
      if (count > min)
276×
569
        smartlist_add(out, cur);
132×
570
      cur = cp;
276×
571
      count = 1;
276×
572
    }
573
  } SMARTLIST_FOREACH_END(cp);
564×
574
  if (count > min)
72×
575
    smartlist_add(out, cur);
48×
576
}
72×
577

578
/** Given a sorted list of strings <b>lst</b>, return the member that appears
579
 * most.  Break ties in favor of later-occurring members. */
580
#define get_most_frequent_member(lst)           \
581
  smartlist_get_most_frequent_string(lst)
582

583
/** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
584
 * that come from the same routerinfo, with the same derived elements.
585
 */
586
static int
587
compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
876×
588
{
589
  int r;
590
  tor_assert(a);
876×
591
  tor_assert(b);
876×
592

593
  if ((r = fast_memcmp(a->status.identity_digest, b->status.identity_digest,
876×
594
                  DIGEST_LEN)))
595
    return r;
596
  if ((r = fast_memcmp(a->status.descriptor_digest,
876×
597
                       b->status.descriptor_digest,
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);
852×
616

617
  if ((r = strcmp(b->status.nickname, a->status.nickname)))
852×
618
    return r;
619

620
  CMP_FIELD(unsigned, int, addr);
852×
621
  CMP_FIELD(unsigned, int, or_port);
852×
622
  CMP_FIELD(unsigned, int, dir_port);
852×
623

624
  return 0;
852×
625
}
626

627
/** Helper for sorting routerlists based on compare_vote_rs. */
628
static int
629
compare_vote_rs_(const void **_a, const void **_b)
174×
630
{
631
  const vote_routerstatus_t *a = *_a, *b = *_b;
174×
632
  return compare_vote_rs(a,b);
174×
633
}
634

635
/** Helper for sorting OR ports. */
636
static int
637
compare_orports_(const void **_a, const void **_b)
84×
638
{
639
  const tor_addr_port_t *a = *_a, *b = *_b;
84×
640
  int r;
641

642
  if ((r = tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
84×
643
    return r;
644
  if ((r = (((int) b->port) - ((int) a->port))))
84×
645
    return r;
!
646

647
  return 0;
648
}
649

650
/** Given a list of vote_routerstatus_t, all for the same router identity,
651
 * return whichever is most frequent, breaking ties in favor of more
652
 * recently published vote_routerstatus_t and in case of ties there,
653
 * in favor of smaller descriptor digest.
654
 */
655
static vote_routerstatus_t *
656
compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
90×
657
                               char *microdesc_digest256_out,
658
                               tor_addr_port_t *best_alt_orport_out)
659
{
660
  vote_routerstatus_t *most = NULL, *cur = NULL;
90×
661
  int most_n = 0, cur_n = 0;
90×
662
  time_t most_published = 0;
90×
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
   */
668
  smartlist_sort(votes, compare_vote_rs_);
90×
669
  SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
354×
670
    if (cur && !compare_vote_rs(cur, rs)) {
264×
671
      ++cur_n;
168×
672
    } else {
673
      if (cur && (cur_n > most_n ||
96×
674
                  (cur_n == most_n &&
!
675
                   cur->status.published_on > most_published))) {
!
676
        most = cur;
6×
677
        most_n = cur_n;
6×
678
        most_published = cur->status.published_on;
6×
679
      }
680
      cur_n = 1;
96×
681
      cur = rs;
96×
682
    }
683
  } SMARTLIST_FOREACH_END(rs);
264×
684

685
  if (cur_n > most_n ||
96×
686
      (cur && cur_n == most_n && cur->status.published_on > most_published)) {
6×
687
    most = cur;
84×
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);
90×
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) {
90×
700
    smartlist_t *alt_orports = smartlist_new();
90×
701
    const tor_addr_port_t *most_alt_orport = NULL;
90×
702

703
    SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
354×
704
      tor_assert(rs);
264×
705
      if (compare_vote_rs(most, rs) == 0 &&
522×
706
          !tor_addr_is_null(&rs->status.ipv6_addr)
258×
707
          && rs->status.ipv6_orport) {
66×
708
        smartlist_add(alt_orports, tor_addr_port_new(&rs->status.ipv6_addr,
66×
709
                                                     rs->status.ipv6_orport));
710
      }
711
    } SMARTLIST_FOREACH_END(rs);
264×
712

713
    smartlist_sort(alt_orports, compare_orports_);
90×
714
    most_alt_orport = smartlist_get_most_frequent(alt_orports,
90×
715
                                                  compare_orports_);
716
    if (most_alt_orport) {
90×
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",
24×
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));
90×
724
    smartlist_free(alt_orports);
90×
725
  }
726

727
  if (microdesc_digest256_out) {
90×
728
    smartlist_t *digests = smartlist_new();
90×
729
    const uint8_t *best_microdesc_digest;
730
    SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
354×
731
        char d[DIGEST256_LEN];
732
        if (compare_vote_rs(rs, most))
264×
733
          continue;
6×
734
        if (!vote_routerstatus_find_microdesc_hash(d, rs, consensus_method,
258×
735
                                                   DIGEST_SHA256))
736
          smartlist_add(digests, tor_memdup(d, sizeof(d)));
258×
737
    } SMARTLIST_FOREACH_END(rs);
258×
738
    smartlist_sort_digests256(digests);
90×
739
    best_microdesc_digest = smartlist_get_most_frequent_digest256(digests);
90×
740
    if (best_microdesc_digest)
90×
741
      memcpy(microdesc_digest256_out, best_microdesc_digest, DIGEST256_LEN);
742
    SMARTLIST_FOREACH(digests, char *, cp, tor_free(cp));
90×
743
    smartlist_free(digests);
90×
744
  }
745

746
  return most;
90×
747
}
748

749
/** Sorting helper: compare two strings based on their values as base-ten
750
 * positive integers. (Non-integers are treated as prior to all integers, and
751
 * compared lexically.) */
752
static int
753
cmp_int_strings_(const void **_a, const void **_b)
1,416×
754
{
755
  const char *a = *_a, *b = *_b;
1,416×
756
  int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
1,416×
757
  int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
1,416×
758
  if (ai<bi) {
1,416×
759
    return -1;
760
  } else if (ai==bi) {
588×
761
    if (ai == 0) /* Parsing failed. */
264×
762
      return strcmp(a, b);
!
763
    return 0;
764
  } else {
765
    return 1;
766
  }
767
}
768

769
/** Given a list of networkstatus_t votes, determine and return the number of
770
 * the highest consensus method that is supported by 2/3 of the voters. */
771
static int
772
compute_consensus_method(smartlist_t *votes)
24×
773
{
774
  smartlist_t *all_methods = smartlist_new();
24×
775
  smartlist_t *acceptable_methods = smartlist_new();
24×
776
  smartlist_t *tmp = smartlist_new();
24×
777
  int min = (smartlist_len(votes) * 2) / 3;
24×
778
  int n_ok;
779
  int result;
780
  SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
48×
781
  {
782
    tor_assert(vote->supported_methods);
783
    smartlist_add_all(tmp, vote->supported_methods);
784
    smartlist_sort(tmp, cmp_int_strings_);
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_);
24×
791
  get_frequent_members(acceptable_methods, all_methods, min);
48×
792
  n_ok = smartlist_len(acceptable_methods);
24×
793
  if (n_ok) {
24×
794
    const char *best = smartlist_get(acceptable_methods, n_ok-1);
24×
795
    result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
24×
796
  } else {
797
    result = 1;
798
  }
799
  smartlist_free(tmp);
24×
800
  smartlist_free(all_methods);
24×
801
  smartlist_free(acceptable_methods);
24×
802
  return result;
24×
803
}
804

805
/** Return true iff <b>method</b> is a consensus method that we support. */
806
static int
807
consensus_method_is_supported(int method)
579×
808
{
809
  return (method >= MIN_SUPPORTED_CONSENSUS_METHOD) &&
579×
810
    (method <= MAX_SUPPORTED_CONSENSUS_METHOD);
811
}
812

813
/** Return a newly allocated string holding the numbers between low and high
814
 * (inclusive) that are supported consensus methods. */
815
STATIC char *
816
make_consensus_method_list(int low, int high, const char *separator)
111×
817
{
818
  char *list;
819

820
  int i;
821
  smartlist_t *lst;
822
  lst = smartlist_new();
111×
823
  for (i = low; i <= high; ++i) {
666×
824
    if (!consensus_method_is_supported(i))
555×
825
      continue;
!
826
    smartlist_add_asprintf(lst, "%d", i);
555×
827
  }
828
  list = smartlist_join_strings(lst, separator, 0, NULL);
111×
829
  tor_assert(list);
111×
830
  SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
111×
831
  smartlist_free(lst);
111×
832
  return list;
111×
833
}
834

835
/** Helper: given <b>lst</b>, a list of version strings such that every
836
 * version appears once for every versioning voter who recommends it, return a
837
 * newly allocated string holding the resulting client-versions or
838
 * server-versions list. May change contents of <b>lst</b> */
839
static char *
840
compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
96×
841
{
842
  int min = n_versioning / 2;
48×
843
  smartlist_t *good = smartlist_new();
48×
844
  char *result;
845
  SMARTLIST_FOREACH_BEGIN(lst, const char *, v) {
288×
846
    if (strchr(v, ' ')) {
240×
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);
240×
853
  sort_version_list(lst, 0);
48×
854
  get_frequent_members(good, lst, min);
96×
855
  result = smartlist_join_strings(good, ",", 0, NULL);
48×
856
  smartlist_free(good);
48×
857
  return result;
48×
858
}
859

860
/** Given a list of K=V values, return the int32_t value corresponding to
861
 * KEYWORD=, or default_val if no such value exists, or if the value is
862
 * corrupt.
863
 */
864
STATIC int32_t
865
dirvote_get_intermediate_param_value(const smartlist_t *param_list,
32×
866
                                     const char *keyword,
867
                                     int32_t default_val)
868
{
869
  unsigned int n_found = 0;
32×
870
  int32_t value = default_val;
32×
871

872
  SMARTLIST_FOREACH_BEGIN(param_list, const char *, k_v_pair) {
146×
873
    if (!strcmpstart(k_v_pair, keyword) && k_v_pair[strlen(keyword)] == '=') {
117×
874
      const char *integer_str = &k_v_pair[strlen(keyword)+1];
7×
875
      int ok;
876
      value = (int32_t)
7×
877
        tor_parse_long(integer_str, 10, INT32_MIN, INT32_MAX, &ok, NULL);
7×
878
      if (BUG(! ok))
7×
879
        return default_val;
3×
880
      ++n_found;
4×
881
    }
882
  } SMARTLIST_FOREACH_END(k_v_pair);
114×
883

884
  if (n_found == 1) {
29×
885
    return value;
886
  } else {
887
    tor_assert_nonfatal(n_found == 0);
27×
888
    return default_val;
27×
889
  }
890
}
891

892
/** Minimum number of directory authorities voting for a parameter to
893
 * include it in the consensus, if consensus method 12 or later is to be
894
 * used. See proposal 178 for details. */
895
#define MIN_VOTES_FOR_PARAM 3
896

897
/** Helper: given a list of valid networkstatus_t, return a new smartlist
898
 * containing the contents of the consensus network parameter set.
899
 */
900
STATIC smartlist_t *
901
dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
36×
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);
36×
911
  smartlist_t *output;
912
  smartlist_t *param_list = smartlist_new();
36×
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));
36×
921

922
  SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
141×
923
    if (!v->net_params)
105×
924
      continue;
!
925
    smartlist_add_all(param_list, v->net_params);
105×
926
  } SMARTLIST_FOREACH_END(v);
105×
927

928
  if (smartlist_len(param_list) == 0) {
36×
929
    tor_free(vals);
!
930
    return param_list;
!
931
  }
932

933
  smartlist_sort_strings(param_list);
36×
934
  i = 0;
36×
935
  cur_param = smartlist_get(param_list, 0);
36×
936
  eq = strchr(cur_param, '=');
36×
937
  tor_assert(eq);
36×
938
  cur_param_len = (int)(eq+1 - cur_param);
36×
939

940
  output = smartlist_new();
36×
941

942
  SMARTLIST_FOREACH_BEGIN(param_list, const char *, param) {
331×
943
    /* resolve spurious clang shallow analysis null pointer errors */
944
    tor_assert(param);
295×
945

946
    const char *next_param;
947
    int ok=0;
295×
948
    eq = strchr(param, '=');
295×
949
    tor_assert(i<n_votes); /* Make sure we prevented vote-stuffing. */
295×
950
    vals[i++] = (int32_t)
590×
951
      tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
295×
952
    tor_assert(ok); /* Already checked these when parsing. */
295×
953

954
    if (param_sl_idx+1 == smartlist_len(param_list))
295×
955
      next_param = NULL;
956
    else
957
      next_param = smartlist_get(param_list, param_sl_idx+1);
259×
958

959
    if (!next_param || strncmp(next_param, param, cur_param_len)) {
295×
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 ||
140×
964
          i >= MIN_VOTES_FOR_PARAM) {
965
        int32_t median = median_int32(vals, i);
88×
966
        char *out_string = tor_malloc(64+cur_param_len);
88×
967
        memcpy(out_string, param, cur_param_len);
88×
968
        tor_snprintf(out_string+cur_param_len,64, "%ld", (long)median);
88×
969
        smartlist_add(output, out_string);
88×
970
      }
971

972
      i = 0;
140×
973
      if (next_param) {
140×
974
        eq = strchr(next_param, '=');
104×
975
        cur_param_len = (int)(eq+1 - next_param);
104×
976
      }
977
    }
978
  } SMARTLIST_FOREACH_END(param);
295×
979

980
  smartlist_free(param_list);
36×
981
  tor_free(vals);
36×
982
  return output;
36×
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

1004
/**
1005
 * Verify that any weightings satisfy the balanced formulas.
1006
 */
1007
static bw_weights_error_t
1008
networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg,
47×
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;
47×
1014

1015
  // Wed + Wmd + Wgd == 1
1016
  if (!CHECK_EQ(Wed + Wmd + Wgd, scale, margin)) {
47×
1017
    berr = BW_WEIGHTS_SUMD_ERROR;
1018
    goto out;
1019
  }
1020

1021
  // Wmg + Wgg == 1
1022
  if (!CHECK_EQ(Wmg + Wgg, scale, margin)) {
47×
1023
    berr = BW_WEIGHTS_SUMG_ERROR;
1024
    goto out;
1025
  }
1026

1027
  // Wme + Wee == 1
1028
  if (!CHECK_EQ(Wme + Wee, scale, margin)) {
47×
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)) {
47×
1035
    berr = BW_WEIGHTS_RANGE_ERROR;
1036
    goto out;
1037
  }
1038

1039
  if (do_balance) {
33×
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)) {
33×
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,
32×
1048
                (margin*T)/3)) {
1049
      berr = BW_WEIGHTS_BALANCE_MID_ERROR;
!
1050
      goto out;
!
1051
    }
1052
  }
1053

1054
 out:
1055
  if (berr) {
47×
1056
    log_info(LD_DIR,
15×
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;
47×
1069
}
1070

1071
/**
1072
 * This function computes the bandwidth weights for consensus method 10.
1073
 *
1074
 * It returns true if weights could be computed, false otherwise.
1075
 */
1076
int
1077
networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
39×
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;
39×
1082
  int64_t Wgg = -1, Wgd = -1;
39×
1083
  int64_t Wmg = -1, Wme = -1, Wmd = -1;
39×
1084
  int64_t Wed = -1, Wee = -1;
39×
1085
  const char *casename;
1086

1087
  if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
39×
1088
    log_warn(LD_DIR, "Consensus with empty bandwidth: "
1×
1089
                     "G=%"PRId64" M=%"PRId64" E=%"PRId64
1090
                     " D=%"PRId64" T=%"PRId64,
1091
             (G), (M), (E),
1092
             (D), (T));
1093
    return 0;
1×
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
38×
1108
    /* Case 1: Neither are scarce.  */
1109
    casename = "Case 1 (Wgd=Wmd=Wed)";
1×
1110
    Wgd = weight_scale/3;
1×
1111
    Wed = weight_scale/3;
1×
1112
    Wmd = weight_scale/3;
1×
1113
    Wee = (weight_scale*(E+G+M))/(3*E);
1×
1114
    Wme = weight_scale - Wee;
1×
1115
    Wmg = (weight_scale*(2*G-E-M))/(3*G);
1×
1116
    Wgg = weight_scale - Wmg;
1×
1117

1118
    berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
1×
1119
                                       weight_scale, G, M, E, D, T, 10, 1);
1120

1121
    if (berr) {
1×
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
37×
1135
    int64_t R = MIN(E, G);
31×
1136
    int64_t S = MAX(E, G);
31×
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
31×
1143
      Wgg = weight_scale;
2×
1144
      Wee = weight_scale;
2×
1145
      Wmg = 0;
2×
1146
      Wme = 0;
2×
1147
      Wmd = 0;
2×
1148
      if (E < G) {
2×
1149
        casename = "Case 2a (E scarce)";
1150
        Wed = weight_scale;
1151
        Wgd = 0;
1152
      } else { /* E >= G */
1153
        casename = "Case 2a (G scarce)";
1×
1154
        Wed = 0;
1×
1155
        Wgd = weight_scale;
1×
1156
      }
1157
    } else { // Subcase b: R+D >= S
1158
      casename = "Case 2b1 (Wgg=weight_scale, Wmd=Wgd)";
29×
1159
      Wee = (weight_scale*(E - G + M))/E;
29×
1160
      Wed = (weight_scale*(D - 2*E + 4*G - 2*M))/(3*D);
29×
1161
      Wme = (weight_scale*(G-M))/E;
29×
1162
      Wmg = 0;
29×
1163
      Wgg = weight_scale;
29×
1164
      Wmd = (weight_scale - Wed)/2;
29×
1165
      Wgd = (weight_scale - Wed)/2;
29×
1166

1167
      berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
29×
1168
                                       weight_scale, G, M, E, D, T, 10, 1);
1169

1170
      if (berr) {
29×
1171
        casename = "Case 2b2 (Wgg=weight_scale, Wee=weight_scale)";
14×
1172
        Wgg = weight_scale;
14×
1173
        Wee = weight_scale;
14×
1174
        Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
14×
1175
        Wmd = (weight_scale*(D - 2*M + G + E))/(3*D);
14×
1176
        Wme = 0;
14×
1177
        Wmg = 0;
14×
1178

1179
        if (Wmd < 0) { // Can happen if M > T/3
14×
1180
          casename = "Case 2b3 (Wmd=0)";
1×
1181
          Wmd = 0;
1×
1182
          log_warn(LD_DIR,
1×
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;
14×
1188
        berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
14×
1189
                  Wed, weight_scale, G, M, E, D, T, 10, 1);
1190
      }
1191
      if (berr != BW_WEIGHTS_NO_ERROR &&
58×
1192
              berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
29×
1193
        log_warn(LD_DIR,
1×
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;
1×
1204
      }
1205
    }
1206
  } else { // if (E < T/3 || G < T/3) {
1207
    int64_t S = MIN(E, G);
6×
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)) {
6×
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
6×
1218
      if (G < E) {
3×
1219
        casename = "Case 3a (G scarce)";
1×
1220
        Wgg = Wgd = weight_scale;
1×
1221
        Wmd = Wed = Wmg = 0;
1×
1222
        // Minor subcase, if E is more scarce than M,
1223
        // keep its bandwidth in place.
1224
        if (E < M) Wme = 0;
1×
1225
        else Wme = (weight_scale*(E-M))/(2*E);
1×
1226
        Wee = weight_scale-Wme;
1×
1227
      } else { // G >= E
1228
        casename = "Case 3a (E scarce)";
2×
1229
        Wee = Wed = weight_scale;
2×
1230
        Wmd = Wgd = Wme = 0;
2×
1231
        // Minor subcase, if G is more scarce than M,
1232
        // keep its bandwidth in place.
1233
        if (G < M) Wmg = 0;
2×
1234
        else Wmg = (weight_scale*(G-M))/(2*G);
2×
1235
        Wgg = weight_scale-Wmg;
2×
1236
      }
1237
    } else { // Subcase b: S+D >= T/3
1238
      // D != 0 because S+D >= T/3
1239
      if (G < E) {
3×
1240
        casename = "Case 3bg (G scarce, Wgg=weight_scale, Wmd == Wed)";
1×
1241
        Wgg = weight_scale;
1×
1242
        Wgd = (weight_scale*(D - 2*G + E + M))/(3*D);
1×
1243
        Wmg = 0;
1×
1244
        Wee = (weight_scale*(E+M))/(2*E);
1×
1245
        Wme = weight_scale - Wee;
1×
1246
        Wmd = (weight_scale - Wgd)/2;
1×
1247
        Wed = (weight_scale - Wgd)/2;
1×
1248

1249
        berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
1×
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)";
2×
1253
        Wee = weight_scale;
2×
1254
        Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
2×
1255
        Wme = 0;
2×
1256
        Wgg = (weight_scale*(G+M))/(2*G);
2×
1257
        Wmg = weight_scale - Wgg;
2×
1258
        Wmd = (weight_scale - Wed)/2;
2×
1259
        Wgd = (weight_scale - Wed)/2;
2×
1260

1261
        berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
2×
1262
                      Wed, weight_scale, G, M, E, D, T, 10, 1);
1263
      }
1264
      if (berr) {
3×
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);
37×
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,
37×
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: "
37×
1306
             "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
1307
             " T=%"PRId64,
1308
             casename,
1309
             (G), (M), (E),
1310
             (D), (T));
1311
  return 1;
37×
1312
}
1313

1314
/** Update total bandwidth weights (G/M/E/D/T) with the bandwidth of
1315
 *  the router in <b>rs</b>. */
1316
static void
1317
update_total_bandwidth_weights(const routerstatus_t *rs,
78×
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;
78×
1323
  int guardfraction_bandwidth = 0;
78×
1324

1325
  if (!rs->has_bandwidth) {
78×
1326
    log_info(LD_BUG, "Missing consensus bandwidth for router %s",
!
1327
             rs->nickname);
1328
    return;
78×
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) {
78×
1353
    guardfraction_bandwidth_t guardfraction_bw;
1354

1355
    tor_assert(is_guard);
!
1356

1357
    guard_get_guardfraction_bandwidth(&guardfraction_bw,
!
1358
                                      rs->bandwidth_kb,
1359
                                      rs->guardfraction_percentage);
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;
78×
1372
  if (is_exit && is_guard) {
78×
1373

1374
    *D += default_bandwidth;
42×
1375
    if (rs->has_guardfraction) {
42×
1376
      *E += guardfraction_bandwidth;
!
1377
    }
1378

1379
  } else if (is_exit) {
36×
1380

1381
    *E += default_bandwidth;
!
1382

1383
  } else if (is_guard) {
36×
1384

1385
    *G += default_bandwidth;
!
1386
    if (rs->has_guardfraction) {
!
1387
      *M += guardfraction_bandwidth;
!
1388
    }
1389

1390
  } else {
1391

1392
    *M += default_bandwidth;
36×
1393
  }
1394
}
1395

1396
/** Considering the different recommended/required protocols sets as a
1397
 * 4-element array, return the element from <b>vote</b> for that protocol
1398
 * set.
1399
 */
1400
static const char *
1401
get_nth_protocol_set_vote(int n, const networkstatus_t *vote)
288×
1402
{
1403
  switch (n) {
288×
1404
    case 0: return vote->recommended_client_protocols;
72×
1405
    case 1: return vote->recommended_relay_protocols;
72×
1406
    case 2: return vote->required_client_protocols;
72×
1407
    case 3: return vote->required_relay_protocols;
72×
1408
    default:
1409
      tor_assert_unreached();
!
1410
      return NULL;
1411
  }
1412
}
1413

1414
/** Considering the different recommended/required protocols sets as a
1415
 * 4-element array, return a newly allocated string for the consensus value
1416
 * for the n'th set.
1417
 */
1418
static char *
1419
compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes)
96×
1420
{
1421
  const char *keyword;
1422
  smartlist_t *proto_votes = smartlist_new();
96×
1423
  int threshold;
1424
  switch (n) {
96×
1425
    case 0:
1426
      keyword = "recommended-client-protocols";
24×
1427
      threshold = CEIL_DIV(n_voters, 2);
24×
1428
      break;
24×
1429
    case 1:
1430
      keyword = "recommended-relay-protocols";
24×
1431
      threshold = CEIL_DIV(n_voters, 2);
24×
1432
      break;
24×
1433
    case 2:
1434
      keyword = "required-client-protocols";
24×
1435
      threshold = CEIL_DIV(n_voters * 2, 3);
24×
1436
      break;
24×
1437
    case 3:
1438
      keyword = "required-relay-protocols";
24×
1439
      threshold = CEIL_DIV(n_voters * 2, 3);
24×
1440
      break;
24×
1441
    default:
1442
      tor_assert_unreached();
!
1443
      return NULL;
1444
  }
1445

1446
  SMARTLIST_FOREACH_BEGIN(votes, const networkstatus_t *, ns) {
384×
1447
    const char *v = get_nth_protocol_set_vote(n, ns);
288×
1448
    if (v)
288×
1449
      smartlist_add(proto_votes, (void*)v);
!
1450
  } SMARTLIST_FOREACH_END(ns);
288×
1451

1452
  char *protocols = protover_compute_vote(proto_votes, threshold);
96×
1453
  smartlist_free(proto_votes);
96×
1454

1455
  char *result = NULL;
96×
1456
  tor_asprintf(&result, "%s %s\n", keyword, protocols);
96×
1457
  tor_free(protocols);
96×
1458

1459
  return result;
96×
1460
}
1461

1462
/** Given a list of vote networkstatus_t in <b>votes</b>, our public
1463
 * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
1464
 * and the number of <b>total_authorities</b> that we believe exist in our
1465
 * voting quorum, generate the text of a new v3 consensus or microdescriptor
1466
 * consensus (depending on <b>flavor</b>), and return the value in a newly
1467
 * allocated string.
1468
 *
1469
 * Note: this function DOES NOT check whether the votes are from
1470
 * recognized authorities.   (dirvote_add_vote does that.)
1471
 *
1472
 * <strong>WATCH OUT</strong>: You need to think before you change the
1473
 * behavior of this function, or of the functions it calls! If some
1474
 * authorities compute the consensus with a different algorithm than
1475
 * others, they will not reach the same result, and they will not all
1476
 * sign the same thing!  If you really need to change the algorithm
1477
 * here, you should allocate a new "consensus_method" for the new
1478
 * behavior, and make the new behavior conditional on a new-enough
1479
 * consensus_method.
1480
 **/
1481
STATIC char *
1482
networkstatus_compute_consensus(smartlist_t *votes,
24×
1483
                                int total_authorities,
1484
                                crypto_pk_t *identity_key,
1485
                                crypto_pk_t *signing_key,
1486
                                const char *legacy_id_key_digest,
1487
                                crypto_pk_t *legacy_signing_key,
1488
                                consensus_flavor_t flavor)
1489
{
1490
  smartlist_t *chunks;
1491
  char *result = NULL;
24×
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;
24×
1496
  smartlist_t *flags;
1497
  const char *flavor_name;
1498
  uint32_t max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
24×
1499
  int64_t G, M, E, D, T; /* For bandwidth weights */
1500
  const routerstatus_format_type_t rs_format =
24×
1501
    flavor == FLAV_NS ? NS_V3_CONSENSUS : NS_V3_CONSENSUS_MICRODESC;
1502
  char *params = NULL;
24×
1503
  char *packages = NULL;
24×
1504
  int added_weights = 0;
24×
1505
  dircollator_t *collator = NULL;
24×
1506
  smartlist_t *param_list = NULL;
24×
1507

1508
  tor_assert(flavor == FLAV_NS || flavor == FLAV_MICRODESC);
24×
1509
  tor_assert(total_authorities >= smartlist_len(votes));
24×
1510
  tor_assert(total_authorities > 0);
24×
1511

1512
  flavor_name = networkstatus_get_flavor_name(flavor);
24×
1513

1514
  if (!smartlist_len(votes)) {
24×
1515
    log_warn(LD_DIR, "Can't compute a consensus from no votes.");
!
1516
    return NULL;
!
1517
  }
1518
  flags = smartlist_new();
24×
1519

1520
  consensus_method = compute_consensus_method(votes);
24×
1521
  if (consensus_method_is_supported(consensus_method)) {
24×
1522
    log_info(LD_DIR, "Generating consensus using method %d.",
24×
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) {
24×
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;
24×
1535
    T = 4;
24×
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);
24×
1545
    time_t *va_times = tor_calloc(n_votes, sizeof(time_t));
24×
1546
    time_t *fu_times = tor_calloc(n_votes, sizeof(time_t));
24×
1547
    time_t *vu_times = tor_calloc(n_votes, sizeof(time_t));
24×
1548
    int *votesec_list = tor_calloc(n_votes, sizeof(int));
24×
1549
    int *distsec_list = tor_calloc(n_votes, sizeof(int));
24×
1550
    int n_versioning_clients = 0, n_versioning_servers = 0;
24×
1551
    smartlist_t *combined_client_versions = smartlist_new();
24×
1552
    smartlist_t *combined_server_versions = smartlist_new();
24×
1553

1554
    SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
96×
1555
      tor_assert(v->type == NS_TYPE_VOTE);
72×
1556
      va_times[v_sl_idx] = v->valid_after;
72×
1557
      fu_times[v_sl_idx] = v->fresh_until;
72×
1558
      vu_times[v_sl_idx] = v->valid_until;
72×
1559
      votesec_list[v_sl_idx] = v->vote_seconds;
72×
1560
      distsec_list[v_sl_idx] = v->dist_seconds;
72×
1561
      if (v->client_versions) {
72×
1562
        smartlist_t *cv = smartlist_new();
48×
1563
        ++n_versioning_clients;
48×
1564
        smartlist_split_string(cv, v->client_versions, ",",
48×
1565
                               SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1566
        sort_version_list(cv, 1);
48×
1567
        smartlist_add_all(combined_client_versions, cv);
48×
1568
        smartlist_free(cv); /* elements get freed later. */
48×
1569
      }
1570
      if (v->server_versions) {
72×
1571
        smartlist_t *sv = smartlist_new();
48×
1572
        ++n_versioning_servers;
48×
1573
        smartlist_split_string(sv, v->server_versions, ",",
48×
1574
                               SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1575
        sort_version_list(sv, 1);
48×
1576
        smartlist_add_all(combined_server_versions, sv);
48×
1577
        smartlist_free(sv); /* elements get freed later. */
48×
1578
      }
1579
      SMARTLIST_FOREACH(v->known_flags, const char *, cp,
72×
1580
                        smartlist_add_strdup(flags, cp));
1581
    } SMARTLIST_FOREACH_END(v);
72×
1582
    valid_after = median_time(va_times, n_votes);
24×
1583
    fresh_until = median_time(fu_times, n_votes);
24×
1584
    valid_until = median_time(vu_times, n_votes);
24×
1585
    vote_seconds = median_int(votesec_list, n_votes);
24×
1586
    dist_seconds = median_int(distsec_list, n_votes);
24×
1587

1588
    tor_assert(valid_after +
24×
1589
               (get_options()->TestingTorNetwork ?
1590
                MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= fresh_until);
1591
    tor_assert(fresh_until +
24×
1592
               (get_options()->TestingTorNetwork ?
1593
                MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= valid_until);
1594
    tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
24×
1595
    tor_assert(dist_seconds >= MIN_DIST_SECONDS);
24×
1596

1597
    server_versions = compute_consensus_versions_list(combined_server_versions,
24×
1598
                                                      n_versioning_servers);
1599
    client_versions = compute_consensus_versions_list(combined_client_versions,
24×
1600
                                                      n_versioning_clients);
1601
    packages = compute_consensus_package_lines(votes);
24×
1602

1603
    SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
24×
1604
    SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
24×
1605
    smartlist_free(combined_server_versions);
24×
1606
    smartlist_free(combined_client_versions);
24×
1607

1608
    smartlist_add_strdup(flags, "NoEdConsensus");
24×
1609

1610
    smartlist_sort_strings(flags);
24×
1611
    smartlist_uniq_strings(flags);
24×
1612

1613
    tor_free(va_times);
24×
1614
    tor_free(fu_times);
24×
1615
    tor_free(vu_times);
24×
1616
    tor_free(votesec_list);
24×
1617
    tor_free(distsec_list);
24×
1618
  }
1619

1620
  chunks = smartlist_new();
24×
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);
24×
1627
    format_iso_time(fu_buf, fresh_until);
24×
1628
    format_iso_time(vu_buf, valid_until);
24×
1629
    flaglist = smartlist_join_strings(flags, " ", 0, NULL);
24×
1630

1631
    smartlist_add_asprintf(chunks, "network-status-version 3%s%s\n"
24×
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",
24×
1637
                           consensus_method);
1638

1639
    smartlist_add_asprintf(chunks,
24×
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);
24×
1655
  }
1656

1657
  {
1658
    int num_dirauth = get_n_authorities(V3_DIRINFO);
24×
1659
    int idx;
1660
    for (idx = 0; idx < 4; ++idx) {
120×
1661
      char *proto_line = compute_nth_protocol_set(idx, num_dirauth, votes);
96×
1662
      if (BUG(!proto_line))
96×
1663
        continue;
!
1664
      smartlist_add(chunks, proto_line);
96×
1665
    }
1666
  }
1667

1668
  param_list = dirvote_compute_params(votes, consensus_method,
24×
1669
                                      total_authorities);
1670
  if (smartlist_len(param_list)) {
24×
1671
    params = smartlist_join_strings(param_list, " ", 0, NULL);
24×
1672
    smartlist_add_strdup(chunks, "params ");
24×
1673
    smartlist_add(chunks, params);
24×
1674
    smartlist_add_strdup(chunks, "\n");
24×
1675
  }
1676

1677
  {
1678
    int num_dirauth = get_n_authorities(V3_DIRINFO);
24×
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 =
24×
1683
      dirvote_get_intermediate_param_value(param_list,
24×
1684
                                           "AuthDirNumSRVAgreements",
1685
                                           (num_dirauth * 2) / 3);
24×
1686
    /* Add the shared random value. */
1687
    char *srv_lines = sr_get_string_for_consensus(votes, num_srv_agreements);
24×
1688
    if (srv_lines != NULL) {
24×
1689
      smartlist_add(chunks, srv_lines);
!
1690
    }
1691
  }
1692

1693
  /* Sort the votes. */
1694
  smartlist_sort(votes, compare_votes_by_authority_id_);
24×
1695
  /* Add the authority sections. */
1696
  {
1697
    smartlist_t *dir_sources = smartlist_new();
24×
1698
    SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
96×
1699
      dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
72×
1700
      e->v = v;
72×
1701
      e->digest = get_voter(v)->identity_digest;
72×
1702
      e->is_legacy = 0;
72×
1703
      smartlist_add(dir_sources, e);
72×
1704
      if (!tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
72×
1705
        dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
24×
1706
        e_legacy->v = v;
24×
1707
        e_legacy->digest = get_voter(v)->legacy_id_digest;
24×
1708
        e_legacy->is_legacy = 1;
24×
1709
        smartlist_add(dir_sources, e_legacy);
24×
1710
      }
1711
    } SMARTLIST_FOREACH_END(v);
72×
1712
    smartlist_sort(dir_sources, compare_dir_src_ents_by_authority_id_);
24×
1713

1714
    SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
120×
1715
      char fingerprint[HEX_DIGEST_LEN+1];
1716
      char votedigest[HEX_DIGEST_LEN+1];
1717
      networkstatus_t *v = e->v;
96×
1718
      networkstatus_voter_info_t *voter = get_voter(v);
96×
1719

1720
      base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
96×
1721
      base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
96×
1722
                    DIGEST_LEN);
1723

1724
      smartlist_add_asprintf(chunks,
288×
1725
                   "dir-source %s%s %s %s %s %d %d\n",
1726
                   voter->nickname, e->is_legacy ? "-legacy" : "",
96×
1727
                   fingerprint, voter->address, fmt_addr32(voter->addr),
1728
                   voter->dir_port,
96×
1729
                   voter->or_port);
96×
1730
      if (! e->is_legacy) {
96×
1731
        smartlist_add_asprintf(chunks,
72×
1732
                     "contact %s\n"
1733
                     "vote-digest %s\n",
1734
                     voter->contact,
1735
                     votedigest);
1736
      }
1737
    } SMARTLIST_FOREACH_END(e);
96×
1738
    SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
24×
1739
    smartlist_free(dir_sources);
24×
1740
  }
1741

1742
  {
1743
    char *max_unmeasured_param = NULL;
24×
1744
    /* XXXX Extract this code into a common function.  Or don't!  see #19011 */
1745
    if (params) {
24×
1746
      if (strcmpstart(params, "maxunmeasuredbw=") == 0)
24×
1747
        max_unmeasured_param = params;
1748
      else
1749
        max_unmeasured_param = strstr(params, " maxunmeasuredbw=");
24×
1750
    }
1751
    if (max_unmeasured_param) {
24×
1752
      int ok = 0;
6×
1753
      char *eq = strchr(max_unmeasured_param, '=');
6×
1754
      if (eq) {
6×
1755
        max_unmeasured_bw_kb = (uint32_t)
6×
1756
          tor_parse_ulong(eq+1, 10, 1, UINT32_MAX, &ok, NULL);
6×
1757
        if (!ok) {
6×
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();
24×
1773
    smartlist_t *chosen_flags = smartlist_new();
24×
1774
    smartlist_t *versions = smartlist_new();
24×
1775
    smartlist_t *protocols = smartlist_new();
24×
1776
    smartlist_t *exitsummaries = smartlist_new();
24×
1777
    uint32_t *bandwidths_kb = tor_calloc(smartlist_len(votes),
24×
1778
                                         sizeof(uint32_t));
1779
    uint32_t *measured_bws_kb = tor_calloc(smartlist_len(votes),
24×
1780
                                           sizeof(uint32_t));
1781
    uint32_t *measured_guardfraction = tor_calloc(smartlist_len(votes),
24×
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();
24×
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));
24×
1804
    n_voter_flags = tor_calloc(smartlist_len(votes), sizeof(int));
24×
1805
    n_flag_voters = tor_calloc(smartlist_len(flags), sizeof(int));
24×
1806
    flag_map = tor_calloc(smartlist_len(votes), sizeof(int *));
24×
1807
    named_flag = tor_calloc(smartlist_len(votes), sizeof(int));
24×
1808
    unnamed_flag = tor_calloc(smartlist_len(votes), sizeof(int));
24×
1809
    for (i = 0; i < smartlist_len(votes); ++i)
96×
1810
      unnamed_flag[i] = named_flag[i] = -1;
72×
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.*/
1818
    SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
144×
1819
      flag_map[v_sl_idx] = tor_calloc(smartlist_len(v->known_flags),
72×
1820
                                      sizeof(int));
1821
      if (smartlist_len(v->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
72×
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) {
696×
1826
        int p = smartlist_string_pos(flags, fl);
624×
1827
        tor_assert(p >= 0);
624×
1828
        flag_map[v_sl_idx][fl_sl_idx] = p;
624×
1829
        ++n_flag_voters[p];
624×
1830
        if (!strcmp(fl, "Named"))
624×
1831
          named_flag[v_sl_idx] = fl_sl_idx;
!
1832
        if (!strcmp(fl, "Unnamed"))
624×
1833
          unnamed_flag[v_sl_idx] = fl_sl_idx;
!
1834
      } SMARTLIST_FOREACH_END(fl);
624×
1835
      n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
72×
1836
      size[v_sl_idx] = smartlist_len(v->routerstatus_list);
72×
1837
    } SMARTLIST_FOREACH_END(v);
72×
1838

1839
    /* Named and Unnamed get treated specially */
1840
    {
1841
      SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
96×
1842
        uint64_t nf;
1843
        if (named_flag[v_sl_idx]<0)
72×
1844
          continue;
72×
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

1866
      SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
96×
1867
        uint64_t uf;
1868
        if (unnamed_flag[v_sl_idx]<0)
72×
1869
          continue;
72×
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;
24×
1893
    SMARTLIST_FOREACH(votes, const networkstatus_t *, v,
24×
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);
24×
1901
    SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
96×
1902
      dircollator_add_vote(collator, v);
72×
1903
    } SMARTLIST_FOREACH_END(v);
72×
1904

1905
    dircollator_collate(collator, consensus_method);
24×
1906

1907
    /* Now go through all the votes */
1908
    flag_counts = tor_calloc(smartlist_len(flags), sizeof(int));
24×
1909
    const int num_routers = dircollator_n_routers(collator);
24×
1910
    for (i = 0; i < num_routers; ++i) {
114×
1911
      vote_routerstatus_t **vrs_lst =
90×
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;
90×
1917
      const char *chosen_version;
1918
      const char *chosen_protocol_list;
1919
      const char *chosen_name = NULL;
90×
1920
      int exitsummary_disagreement = 0;
90×
1921
      int is_named = 0, is_unnamed = 0, is_running = 0, is_valid = 0;
90×
1922
      int is_guard = 0, is_exit = 0, is_bad_exit = 0;
90×
1923
      int naming_conflict = 0;
90×
1924
      int n_listing = 0;
90×
1925
      char microdesc_digest[DIGEST256_LEN];
1926
      tor_addr_port_t alt_orport = {TOR_ADDR_NULL, 0};
90×
1927

1928
      memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
90×
1929
      smartlist_clear(matching_descs);
90×
1930
      smartlist_clear(chosen_flags);
90×
1931
      smartlist_clear(versions);
90×
1932
      smartlist_clear(protocols);
90×
1933
      num_bandwidths = 0;
90×
1934
      num_mbws = 0;
90×
1935
      num_guardfraction_inputs = 0;
90×
1936
      int ed_consensus = 0;
90×
1937
      const uint8_t *ed_consensus_val = NULL;
90×
1938

1939
      /* Okay, go through all the entries for this digest. */
1940
      for (int voter_idx = 0; voter_idx < smartlist_len(votes); ++voter_idx) {
360×
1941
        if (vrs_lst[voter_idx] == NULL)
270×
1942
          continue; /* This voter had nothing to say about this entry. */
6×
1943
        rs = vrs_lst[voter_idx];
264×
1944
        ++n_listing;
264×
1945

1946
        current_rsa_id = rs->status.identity_digest;
264×
1947

1948
        smartlist_add(matching_descs, rs);
264×
1949
        if (rs->version && rs->version[0])
264×
1950
          smartlist_add(versions, rs->version);
264×
1951

1952
        if (rs->protocols) {
264×
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) {
2,292×
1960
          if (rs->flags & (UINT64_C(1) << flag))
2,292×
1961
            ++flag_counts[flag_map[voter_idx][flag]];
1,134×
1962
        }
1963
        if (named_flag[voter_idx] >= 0 &&
264×
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) {
264×
1975
          measured_guardfraction[num_guardfraction_inputs++] =
!
1976
            rs->status.guardfraction_percentage;
!
1977
        }
1978

1979
        /* count bandwidths */
1980
        if (rs->has_measured_bw)
264×
1981
          measured_bws_kb[num_mbws++] = rs->measured_bw_kb;
72×
1982

1983
        if (rs->status.has_bandwidth)
264×
1984
          bandwidths_kb[num_bandwidths++] = rs->status.bandwidth_kb;
264×
1985

1986
        /* Count number for which ed25519 is canonical. */
1987
        if (rs->ed25519_reflects_consensus) {
264×
1988
          ++ed_consensus;
264×
1989
          if (ed_consensus_val) {
264×
1990
            tor_assert(fast_memeq(ed_consensus_val, rs->ed25519_id,
174×
1991
                                  ED25519_PUBKEY_LEN));
1992
          } else {
1993
            ed_consensus_val = rs->ed25519_id;
90×
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)
90×
2001
        continue;
12×
2002

2003
      if (ed_consensus > 0) {
90×
2004
        if (ed_consensus <= total_authorities / 2) {
90×
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);
90×
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,
90×
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,
90×
2022
                            rs->status.identity_digest,DIGEST_LEN));
2023
      memcpy(rs_out.identity_digest, current_rsa_id, DIGEST_LEN);
2024
      memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
90×
2025
             DIGEST_LEN);
2026
      rs_out.addr = rs->status.addr;
90×
2027
      rs_out.published_on = rs->status.published_on;
90×
2028
      rs_out.dir_port = rs->status.dir_port;
90×
2029
      rs_out.or_port = rs->status.or_port;
90×
2030
      tor_addr_copy(&rs_out.ipv6_addr, &alt_orport.addr);
90×
2031
      rs_out.ipv6_orport = alt_orport.port;
90×
2032
      rs_out.has_bandwidth = 0;
90×
2033
      rs_out.has_exitsummary = 0;
90×
2034

2035
      if (chosen_name && !naming_conflict) {
90×
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));
90×
2039
      }
2040

2041
      {
2042
        const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
90×
2043
        if (!d) {
90×
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. */
90×
2054
      SMARTLIST_FOREACH_BEGIN(flags, const char *, fl) {
1,080×
2055
        if (!strcmp(fl, "Named")) {
990×
2056
          if (is_named)
!
2057
            smartlist_add(chosen_flags, (char*)fl);
!
2058
        } else if (!strcmp(fl, "Unnamed")) {
990×
2059
          if (is_unnamed)
!
2060
            smartlist_add(chosen_flags, (char*)fl);
!
2061
        } else if (!strcmp(fl, "NoEdConsensus")) {
990×
2062
          if (ed_consensus <= total_authorities/2)
90×
2063
            smartlist_add(chosen_flags, (char*)fl);
!
2064
        } else {
2065
          if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
900×
2066
            smartlist_add(chosen_flags, (char*)fl);
384×
2067
            if (!strcmp(fl, "Exit"))
384×
2068
              is_exit = 1;
2069
            else if (!strcmp(fl, "Guard"))
342×
2070
              is_guard = 1;
2071
            else if (!strcmp(fl, "Running"))
300×
2072
              is_running = 1;
2073
            else if (!strcmp(fl, "BadExit"))
222×
2074
              is_bad_exit = 1;
2075
            else if (!strcmp(fl, "Valid"))
222×
2076
              is_valid = 1;
78×
2077
          }
2078
        }
2079
      } SMARTLIST_FOREACH_END(fl);
990×
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)
90×
2084
        continue;
12×
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)
78×
2089
        continue;
!
2090

2091
      /* Pick the version. */
2092
      if (smartlist_len(versions)) {
78×
2093
        sort_version_list(versions, 0);
78×
2094
        chosen_version = get_most_frequent_member(versions);
78×
2095
      } else {
2096
        chosen_version = NULL;
2097
      }
2098

2099
      /* Pick the protocol list */
2100
      if (smartlist_len(protocols)) {
78×
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) {
78×
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) {
78×
2119
        rs_out.has_bandwidth = 1;
24×
2120
        rs_out.bw_is_unmeasured = 0;
24×
2121
        rs_out.bandwidth_kb = median_uint32(measured_bws_kb, num_mbws);
24×
2122
      } else if (num_bandwidths > 0) {
54×
2123
        rs_out.has_bandwidth = 1;
54×
2124
        rs_out.bw_is_unmeasured = 1;
54×
2125
        rs_out.bandwidth_kb = median_uint32(bandwidths_kb, num_bandwidths);
54×
2126
        if (n_authorities_measuring_bandwidth > 2) {
54×
2127
          /* Cap non-measured bandwidths. */
2128
          if (rs_out.bandwidth_kb > max_unmeasured_bw_kb) {
24×
2129
            rs_out.bandwidth_kb = max_unmeasured_bw_kb;
12×
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;
78×
2136

2137
      /* Update total bandwidth weights with the bandwidths of this router. */
2138
      {
2139
        update_total_bandwidth_weights(&rs_out,
78×
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;
78×
2162
        smartlist_clear(exitsummaries);
78×
2163
        SMARTLIST_FOREACH_BEGIN(matching_descs, vote_routerstatus_t *, vsr) {
306×
2164
          /* Check if the vote where this status comes from had the
2165
           * proper descriptor */
2166
          tor_assert(fast_memeq(rs_out.identity_digest,
228×
2167
                             vsr->status.identity_digest,
2168
                             DIGEST_LEN));
2169
          if (vsr->status.has_exitsummary &&
456×
2170
               fast_memeq(rs_out.descriptor_digest,
228×
2171
                       vsr->status.descriptor_digest,
2172
                       DIGEST_LEN)) {
2173
            tor_assert(vsr->status.exitsummary);
222×
2174
            smartlist_add(exitsummaries, vsr->status.exitsummary);
222×
2175
            if (!chosen_exitsummary) {
222×
2176
              chosen_exitsummary = vsr->status.exitsummary;
78×
2177
            } else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
144×
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);
228×
2184

2185
        if (exitsummary_disagreement) {
78×
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) {
78×
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) {
78×
2222
          rs_out.has_exitsummary = 1;
78×
2223
          /* yea, discards the const */
2224
          rs_out.exitsummary = (char *)chosen_exitsummary;
78×
2225
        }
2226
      }
2227

2228
      if (flavor == FLAV_MICRODESC &&
126×
2229
          tor_digest256_is_zero(microdesc_digest)) {
48×
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,
78×
2239
                                        rs_format, consensus_method, NULL);
2240
        if (buf)
78×
2241
          smartlist_add(chunks, buf);
78×
2242
      }
2243
      /*     Now an m line, if applicable. */
2244
      if (flavor == FLAV_MICRODESC &&
126×
2245
          !tor_digest256_is_zero(microdesc_digest)) {
48×
2246
        char m[BASE64_DIGEST256_LEN+1];
2247
        digest256_to_base64(m, microdesc_digest);
48×
2248
        smartlist_add_asprintf(chunks, "m %s\n", m);
48×
2249
      }
2250
      /*     Next line is all flags.  The "\n" is missing. */
2251
      smartlist_add(chunks,
78×
2252
                    smartlist_join_strings(chosen_flags, " ", 0, NULL));
78×
2253
      /*     Now the version line. */
2254
      if (chosen_version) {
78×
2255
        smartlist_add_strdup(chunks, "\nv ");
78×
2256
        smartlist_add_strdup(chunks, chosen_version);
78×
2257
      }
2258
      smartlist_add_strdup(chunks, "\n");
78×
2259
      if (chosen_protocol_list &&
156×
2260
          consensus_method >= MIN_METHOD_FOR_RS_PROTOCOLS) {
78×
2261
        smartlist_add_asprintf(chunks, "pr %s\n", chosen_protocol_list);
!
2262
      }
2263
      /*     Now the weight line. */
2264
      if (rs_out.has_bandwidth) {
78×
2265
        char *guardfraction_str = NULL;
78×
2266
        int unmeasured = rs_out.bw_is_unmeasured;
78×
2267

2268
        /* If we have guardfraction info, include it in the 'w' line. */
2269
        if (rs_out.has_guardfraction) {
78×
2270
          tor_asprintf(&guardfraction_str,
!
2271
                       " GuardFraction=%u", rs_out.guardfraction_percentage);
2272
        }
2273
        smartlist_add_asprintf(chunks, "w Bandwidth=%d%s%s\n",
78×
2274
                               rs_out.bandwidth_kb,
2275
                               unmeasured?" Unmeasured=1":"",
2276
                               guardfraction_str ? guardfraction_str : "");
78×
2277

2278
        tor_free(guardfraction_str);
78×
2279
      }
2280

2281
      /*     Now the exitpolicy summary line. */
2282
      if (rs_out.has_exitsummary && flavor == FLAV_NS) {
78×
2283
        smartlist_add_asprintf(chunks, "p %s\n", rs_out.exitsummary);
30×
2284
      }
2285

2286
      /* And the loop is over and we move on to the next router */
2287
    }
2288

2289
    tor_free(size);
24×
2290
    tor_free(n_voter_flags);
24×
2291
    tor_free(n_flag_voters);
24×
2292
    for (i = 0; i < smartlist_len(votes); ++i)
96×
2293
      tor_free(flag_map[i]);
72×
2294
    tor_free(flag_map);
24×
2295
    tor_free(flag_counts);
24×
2296
    tor_free(named_flag);
24×
2297
    tor_free(unnamed_flag);
24×
2298
    strmap_free(name_to_id_map, NULL);
24×
2299
    smartlist_free(matching_descs);
24×
2300
    smartlist_free(chosen_flags);
24×
2301
    smartlist_free(versions);
24×
2302
    smartlist_free(protocols);
24×
2303
    smartlist_free(exitsummaries);
24×
2304
    tor_free(bandwidths_kb);
24×
2305
    tor_free(measured_bws_kb);
24×
2306
    tor_free(measured_guardfraction);
24×
2307
  }
2308

2309
  /* Mark the directory footer region */
2310
  smartlist_add_strdup(chunks, "directory-footer\n");
24×
2311

2312
  {
2313
    int64_t weight_scale = BW_WEIGHT_SCALE;
24×
2314
    char *bw_weight_param = NULL;
24×
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) {
24×
2321
      if (strcmpstart(params, "bwweightscale=") == 0)
24×
2322
        bw_weight_param = params;
2323
      else
2324
        bw_weight_param = strstr(params, " bwweightscale=");
24×
2325
    }
2326

2327
    if (bw_weight_param) {
24×
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,
24×
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 =
24×
2355
      flavor == FLAV_NS ? DIGEST_SHA1 : DIGEST_SHA256;
24×
2356
    size_t digest_len =
24×
2357
      flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
2358
    const char *algname = crypto_digest_algorithm_get_name(digest_alg);
24×
2359
    char *signature;
2360

2361
    smartlist_add_strdup(chunks, "directory-signature ");
24×
2362

2363
    /* Compute the hash of the chunks. */
2364
    crypto_digest_smartlist(digest, digest_len, chunks, "", digest_alg);
24×
2365

2366
    /* Get the fingerprints */
2367
    crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
24×
2368
    crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
24×
2369

2370
    /* add the junk that will go at the end of the line. */
2371
    if (flavor == FLAV_NS) {
24×
2372
      smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
9×
2373
                   signing_key_fingerprint);
2374
    } else {
2375
      smartlist_add_asprintf(chunks, "%s %s %s\n",
15×
2376
                   algname, fingerprint,
2377
                   signing_key_fingerprint);
2378
    }
2379
    /* And the signature. */
2380
    if (!(signature = router_get_dirobj_signature(digest, digest_len,
24×
2381
                                                  signing_key))) {
2382
      log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
!
2383
      goto done;
!
2384
    }
2385
    smartlist_add(chunks, signature);
24×
2386

2387
    if (legacy_id_key_digest && legacy_signing_key) {
24×
2388
      smartlist_add_strdup(chunks, "directory-signature ");
12×
2389
      base16_encode(fingerprint, sizeof(fingerprint),
12×
2390
                    legacy_id_key_digest, DIGEST_LEN);
2391
      crypto_pk_get_fingerprint(legacy_signing_key,
12×
2392
                                signing_key_fingerprint, 0);
2393
      if (flavor == FLAV_NS) {
12×
2394
        smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
3×
2395
                     signing_key_fingerprint);
2396
      } else {
2397
        smartlist_add_asprintf(chunks, "%s %s %s\n",
9×
2398
                     algname, fingerprint,
2399
                     signing_key_fingerprint);
2400
      }
2401

2402
      if (!(signature = router_get_dirobj_signature(digest, digest_len,
12×
2403
                                                    legacy_signing_key))) {
2404
        log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
!
2405
        goto done;
!
2406
      }
2407
      smartlist_add(chunks, signature);
12×
2408
    }
2409
  }
2410

2411
  result = smartlist_join_strings(chunks, "", 0, NULL);
24×
2412

2413
  {
2414
    networkstatus_t *c;
2415
    if (!(c = networkstatus_parse_vote_from_string(result, strlen(result),
24×
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) {
24×
2425
      networkstatus_verify_bw_weights(c, consensus_method);
24×
2426
    }
2427
    networkstatus_vote_free(c);
24×
2428
  }
2429

2430
 done:
2431

2432
  dircollator_free(collator);
24×
2433
  tor_free(client_versions);
24×
2434
  tor_free(server_versions);
24×
2435
  tor_free(packages);
24×
2436
  SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
24×
2437
  smartlist_free(flags);
24×
2438
  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
24×
2439
  smartlist_free(chunks);
24×
2440
  SMARTLIST_FOREACH(param_list, char *, cp, tor_free(cp));
24×
2441
  smartlist_free(param_list);
24×
2442

2443
  return result;
24×
2444
}
2445

2446
/** Given a list of networkstatus_t for each vote, return a newly allocated
2447
 * string containing the "package" lines for the vote. */
2448
STATIC char *
2449
compute_consensus_package_lines(smartlist_t *votes)
25×
2450
{
2451
  const int n_votes = smartlist_len(votes);
25×
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();
25×
2458

2459
  SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
103×
2460
    if (! v->package_lines)
78×
2461
      continue;
!
2462
    SMARTLIST_FOREACH_BEGIN(v->package_lines, const char *, line) {
130×
2463
      if (! validate_recommended_package_line(line))
52×
2464
        continue;
5×
2465

2466
      /* Skip 'cp' to the second space in the line. */
2467
      const char *cp = strchr(line, ' ');
47×
2468
      if (!cp) continue;
47×
2469
      ++cp;
47×
2470
      cp = strchr(cp, ' ');
47×
2471
      if (!cp) continue;
47×
2472

2473
      char *key = tor_strndup(line, cp - line);
47×
2474

2475
      const char **status = strmap_get(package_status, key);
47×
2476
      if (!status) {
47×
2477
        status = tor_calloc(n_votes, sizeof(const char *));
9×
2478
        strmap_set(package_status, key, status);
9×
2479
      }
2480
      status[v_sl_idx] = line; /* overwrite old value */
47×
2481
      tor_free(key);
47×
2482
    } SMARTLIST_FOREACH_END(line);
47×
2483
  } SMARTLIST_FOREACH_END(v);
2484

2485
  smartlist_t *entries = smartlist_new(); /* temporary */
25×
2486
  smartlist_t *result_list = smartlist_new(); /* output */
25×
2487
  STRMAP_FOREACH(package_status, key, const char **, values) {
34×
2488
    int i, count=-1;
9×
2489
    for (i = 0; i < n_votes; ++i) {
63×
2490
      if (values[i])
54×
2491
        smartlist_add(entries, (void*) values[i]);
37×
2492
    }
2493
    smartlist_sort_strings(entries);
9×
2494
    int n_voting_for_entry = smartlist_len(entries);
9×
2495
    const char *most_frequent =
9×
2496
      smartlist_get_most_frequent_string_(entries, &count);
2497

2498
    if (n_voting_for_entry >= 3 && count > n_voting_for_entry / 2) {
9×
2499
      smartlist_add_asprintf(result_list, "package %s\n", most_frequent);
5×
2500
    }
2501

2502
    smartlist_clear(entries);
9×
2503

2504
  } STRMAP_FOREACH_END;
2505

2506
  smartlist_sort_strings(result_list);
25×
2507

2508
  char *result = smartlist_join_strings(result_list, "", 0, NULL);
25×
2509

2510
  SMARTLIST_FOREACH(result_list, char *, cp, tor_free(cp));
25×
2511
  smartlist_free(result_list);
25×
2512
  smartlist_free(entries);
25×
2513
  strmap_free(package_status, tor_free_);
25×
2514

2515
  return result;
25×
2516
}
2517

2518
/** Given a consensus vote <b>target</b> and a set of detached signatures in
2519
 * <b>sigs</b> that correspond to the same consensus, check whether there are
2520
 * any new signatures in <b>src_voter_list</b> that should be added to
2521
 * <b>target</b>. (A signature should be added if we have no signature for that
2522
 * voter in <b>target</b> yet, or if we have no verifiable signature and the
2523
 * new signature is verifiable.)  Return the number of signatures added or
2524
 * changed, or -1 if the document signed by <b>sigs</b> isn't the same
2525
 * document as <b>target</b>. */
2526
STATIC int
2527
networkstatus_add_detached_signatures(networkstatus_t *target,
12×
2528
                                      ns_detached_signatures_t *sigs,
2529
                                      const char *source,
2530
                                      int severity,
2531
                                      const char **msg_out)
2532
{
2533
  int r = 0;
12×
2534
  const char *flavor;
2535
  smartlist_t *siglist;
2536
  tor_assert(sigs);
12×
2537
  tor_assert(target);
12×
2538
  tor_assert(target->type == NS_TYPE_CONSENSUS);
12×
2539

2540
  flavor = networkstatus_get_flavor_name(target->flavor);
12×
2541

2542
  /* Do the times seem right? */
2543
  if (target->valid_after != sigs->valid_after) {
12×
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) {
12×
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) {
12×
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);
12×
2559
  if (!siglist) {
12×
2560
    *msg_out = "No signatures for given consensus flavor";
!
2561
    return -1;
!
2562
  }
2563

2564
  /** Make sure all the digests we know match, and at least one matches. */
2565
  {
2566
    common_digests_t *digests = strmap_get(sigs->digests, flavor);
12×
2567
    int n_matches = 0;
12×
2568
    int alg;
2569
    if (!digests) {
12×
2570
      *msg_out = "No digests for given consensus flavor";
!
2571
      return -1;
!
2572
    }
2573
    for (alg = DIGEST_SHA1; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
24×
2574
      if (!tor_mem_is_zero(digests->d[alg], DIGEST256_LEN)) {
24×
2575
        if (fast_memeq(target->digests.d[alg], digests->d[alg],
12×
2576
                       DIGEST256_LEN)) {
2577
          ++n_matches;
12×
2578
        } else {
2579
          *msg_out = "Mismatched digest.";
!
2580
          return -1;
!
2581
        }
2582
      }
2583
    }
2584
    if (!n_matches) {
12×
2585
      *msg_out = "No recognized digests for given consensus flavor";
!
2586
    }
2587
  }
2588

2589
  /* For each voter in src... */
2590
  SMARTLIST_FOREACH_BEGIN(siglist, document_signature_t *, sig) {
27×
2591
    char voter_identity[HEX_DIGEST_LEN+1];
2592
    networkstatus_voter_info_t *target_voter =
15×
2593
      networkstatus_get_voter_by_id(target, sig->identity_digest);
15×
2594
    authority_cert_t *cert = NULL;
15×
2595
    const char *algorithm;
2596
    document_signature_t *old_sig = NULL;
15×
2597

2598
    algorithm = crypto_digest_algorithm_get_name(sig->alg);
15×
2599

2600
    base16_encode(voter_identity, sizeof(voter_identity),
15×
2601
                  sig->identity_digest, DIGEST_LEN);
2602
    log_info(LD_DIR, "Looking at signature from %s using %s", voter_identity,
15×
2603
             algorithm);
2604
    /* If the target doesn't know about this voter, then forget it. */
2605
    if (!target_voter) {
15×
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);
15×
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) {
15×
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) {
15×
2622
      cert = authority_cert_get_by_digests(sig->identity_digest,
15×
2623
                                           sig->signing_key_digest);
15×
2624
      if (cert) {
15×
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) {
15×
2634
      log_info(LD_DIR, "Adding signature from %s with %s", voter_identity,
12×
2635
               algorithm);
2636
      tor_log(severity, LD_DIR, "Added a signature for %s from %s.",
12×
2637
          target_voter->nickname, source);
2638
      ++r;
12×
2639
      if (old_sig) {
12×
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));
12×
2644
    } else {
2645
      log_info(LD_DIR, "Not adding signature from %s", voter_identity);
3×
2646
    }
2647
  } SMARTLIST_FOREACH_END(sig);
15×
2648

2649
  return r;
2650
}
2651

2652
/** Return a newly allocated string containing all the signatures on
2653
 * <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
2654
 * then the signatures will be put in a detached signatures document, so
2655
 * prefix any non-NS-flavored signatures with "additional-signature" rather
2656
 * than "directory-signature". */
2657
static char *
2658
networkstatus_format_signatures(networkstatus_t *consensus,
18×
2659
                                int for_detached_signatures)
2660
{
2661
  smartlist_t *elements;
2662
  char buf[4096];
2663
  char *result = NULL;
18×
2664
  int n_sigs = 0;
18×
2665
  const consensus_flavor_t flavor = consensus->flavor;
18×
2666
  const char *flavor_name = networkstatus_get_flavor_name(flavor);
18×
2667
  const char *keyword;
2668

2669
  if (for_detached_signatures && flavor != FLAV_NS)
18×
2670
    keyword = "additional-signature";
2671
  else
2672
    keyword = "directory-signature";
9×
2673

2674
  elements = smartlist_new();
18×
2675

2676
  SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *, v) {
90×
2677
    SMARTLIST_FOREACH_BEGIN(v->sigs, document_signature_t *, sig) {
96×
2678
      char sk[HEX_DIGEST_LEN+1];
2679
      char id[HEX_DIGEST_LEN+1];
2680
      if (!sig->signature || sig->bad_signature)
24×
2681
        continue;
!
2682
      ++n_sigs;
24×
2683
      base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
24×
2684
      base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
24×
2685
      if (flavor == FLAV_NS) {
24×
2686
        smartlist_add_asprintf(elements,
12×
2687
                     "%s %s %s\n-----BEGIN SIGNATURE-----\n",
2688
                     keyword, id, sk);
2689
      } else {
2690
        const char *digest_name =
12×
2691
          crypto_digest_algorithm_get_name(sig->alg);
12×
2692
        smartlist_add_asprintf(elements,
12×
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,
24×
2700
                    BASE64_ENCODE_MULTILINE);
2701
      strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
24×
2702
      smartlist_add_strdup(elements, buf);
24×
2703
    } SMARTLIST_FOREACH_END(sig);
24×
2704
  } SMARTLIST_FOREACH_END(v);
72×
2705

2706
  result = smartlist_join_strings(elements, "", 0, NULL);
18×
2707
  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
18×
2708
  smartlist_free(elements);
18×
2709
  if (!n_sigs)
18×
2710
    tor_free(result);
!
2711
  return result;
18×
2712
}
2713

2714
/** Return a newly allocated string holding the detached-signatures document
2715
 * corresponding to the signatures on <b>consensuses</b>, which must contain
2716
 * exactly one FLAV_NS consensus, and no more than one consensus for each
2717
 * other flavor. */
2718
STATIC char *
2719
networkstatus_get_detached_signatures(smartlist_t *consensuses)
9×
2720
{
2721
  smartlist_t *elements;
2722
  char *result = NULL, *sigs = NULL;
9×
2723
  networkstatus_t *consensus_ns = NULL;
18×
2724
  tor_assert(consensuses);
9×
2725

2726
  SMARTLIST_FOREACH(consensuses, networkstatus_t *, ns, {
9×
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) {
9×
2733
    log_warn(LD_BUG, "No NS consensus given.");
!
2734
    return NULL;
!
2735
  }
2736

2737
  elements = smartlist_new();
9×
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),
9×
2745
                  consensus_ns->digests.d[DIGEST_SHA1], DIGEST_LEN);
9×
2746
    format_iso_time(va_buf, consensus_ns->valid_after);
9×
2747
    format_iso_time(fu_buf, consensus_ns->fresh_until);
9×
2748
    format_iso_time(vu_buf, consensus_ns->valid_until);
9×
2749

2750
    smartlist_add_asprintf(elements,
9×
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) {
27×
2759
    const char *flavor_name = networkstatus_get_flavor_name(ns->flavor);
18×
2760
    int alg;
2761
    if (ns->flavor == FLAV_NS)
18×
2762
      continue;
9×
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) {
9×
2767
      char d[HEX_DIGEST256_LEN+1];
2768
      const char *alg_name =
9×
2769
        crypto_digest_algorithm_get_name(alg);
9×
2770
      if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
9×
2771
        continue;
!
2772
      base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN);
9×
2773
      smartlist_add_asprintf(elements, "additional-digest %s %s %s\n",
9×
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) {
27×
2780
    char *sigs_on_this_consensus;
2781
    if (ns->flavor == FLAV_NS)
18×
2782
      continue;
9×
2783
    sigs_on_this_consensus = networkstatus_format_signatures(ns, 1);
9×
2784
    if (!sigs_on_this_consensus) {
9×
2785
      log_warn(LD_DIR, "Couldn't format signatures");
!
2786
      goto err;
!
2787
    }
2788
    smartlist_add(elements, sigs_on_this_consensus);
9×
2789
  } SMARTLIST_FOREACH_END(ns);
9×
2790

2791
  /* Now add the FLAV_NS consensus signatrures. */
2792
  sigs = networkstatus_format_signatures(consensus_ns, 1);
9×
2793
  if (!sigs)
9×
2794
    goto err;
2795
  smartlist_add(elements, sigs);
9×
2796

2797
  result = smartlist_join_strings(elements, "", 0, NULL);
9×
2798
 err:
2799
  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
9×
2800
  smartlist_free(elements);
9×
2801
  return result;
9×
2802
}
2803

2804
/** Return a newly allocated string holding a detached-signatures document for
2805
 * all of the in-progress consensuses in the <b>n_flavors</b>-element array at
2806
 * <b>pending</b>. */
2807
static char *
2808
get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
!
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

2823
/**
2824
 * Entry point: Take whatever voting actions are pending as of <b>now</b>.
2825
 *
2826
 * Return the time at which the next action should be taken.
2827
 */
2828
time_t
2829
dirvote_act(const or_options_t *options, time_t now)
1×
2830
{
2831
  if (!authdir_mode_v3(options))
1×
2832
    return TIME_MAX;
2833
  tor_assert_nonfatal(voting_schedule.voting_starts);
1×
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) {
1×
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.",
2844
               keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
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) {
1×
2859
    log_notice(LD_DIR, "Time to vote.");
1×
2860
    dirvote_perform_vote();
1×
2861
    voting_schedule.have_voted = 1;
1×
2862
  } ENDIF
2863
  IF_TIME_FOR_NEXT_ACTION(fetch_missing_votes, have_fetched_missing_votes) {
1×
2864
    log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
1×
2865
    dirvote_fetch_missing_votes();
1×
2866
    voting_schedule.have_fetched_missing_votes = 1;
1×
2867
  } ENDIF
2868
  IF_TIME_FOR_NEXT_ACTION(voting_ends, have_built_consensus) {
1×
2869
    log_notice(LD_DIR, "Time to compute a consensus.");
1×
2870
    dirvote_compute_consensuses();
1×
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;
1×
2874
  } ENDIF
2875
  IF_TIME_FOR_NEXT_ACTION(fetch_missing_signatures,
1×
2876
                          have_fetched_missing_signatures) {
2877
    log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
1×
2878
    dirvote_fetch_missing_signatures();
1×
2879
    voting_schedule.have_fetched_missing_signatures = 1;
1×
2880
  } ENDIF
2881
  IF_TIME_FOR_NEXT_ACTION(interval_starts,
1×
2882
                          have_published_consensus) {
2883
    log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
1×
2884
    dirvote_publish_consensus();
1×
2885
    dirvote_clear_votes(0);
1×
2886
    voting_schedule.have_published_consensus = 1;
1×
2887
    /* Update our shared random state with the consensus just published. */
2888
    sr_act_post_consensus(
2×
2889
                networkstatus_get_latest_consensus_by_flavor(FLAV_NS));
1×
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);
1×
2893
    return voting_schedule.voting_starts;
1×
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

2903
/** A vote networkstatus_t and its unparsed body: held around so we can
2904
 * use it to generate a consensus (at voting_ends) and so we can serve it to
2905
 * other authorities that might want it. */
2906
typedef struct pending_vote_t {
2907
  cached_dir_t *vote_body;
2908
  networkstatus_t *vote;
2909
} pending_vote_t;
2910

2911
/** List of pending_vote_t for the current vote.  Before we've used them to
2912
 * build a consensus, the votes go here. */
2913
static smartlist_t *pending_vote_list = NULL;
2914
/** List of pending_vote_t for the previous vote.  After we've used them to
2915
 * build a consensus, the votes go here for the next period. */
2916
static smartlist_t *previous_vote_list = NULL;
2917

2918
/* DOCDOC pending_consensuses */
2919
static pending_consensus_t pending_consensuses[N_CONSENSUS_FLAVORS];
2920

2921
/** The detached signatures for the consensus that we're currently
2922
 * building. */
2923
static char *pending_consensus_signatures = NULL;
2924

2925
/** List of ns_detached_signatures_t: hold signatures that get posted to us
2926
 * before we have generated the consensus on our own. */
2927
static smartlist_t *pending_consensus_signature_list = NULL;
2928

2929
/** Generate a networkstatus vote and post it to all the v3 authorities.
2930
 * (V3 Authority only) */
2931
static int
2932
dirvote_perform_vote(void)
1×
2933
{
2934
  crypto_pk_t *key = get_my_v3_authority_signing_key();
1×
2935
  authority_cert_t *cert = get_my_v3_authority_cert();
1×
2936
  networkstatus_t *ns;
2937
  char *contents;
2938
  pending_vote_t *pending_vote;
2939
  time_t now = time(NULL);
1×
2940

2941
  int status;
2942
  const char *msg = "";
1×
2943

2944
  if (!cert || !key) {
1×
2945
    log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
1×
2946
    return -1;
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

2967
  directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
!
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

2976
/** Send an HTTP request to every other v3 authority, for the votes of every
2977
 * authority for which we haven't received a vote yet in this period. (V3
2978
 * authority only) */
2979
static void
2980
dirvote_fetch_missing_votes(void)
1×
2981
{
2982
  smartlist_t *missing_fps = smartlist_new();
1×
2983
  char *resource;
2984

2985
  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
2×
2986
                          dir_server_t *, ds) {
2987
      if (!(ds->type & V3_DIRINFO))
1×
2988
        continue;
!
2989
      if (!dirvote_get_vote(ds->v3_identity_digest,
1×
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)) {
1×
2999
    smartlist_free(missing_fps);
1×
3000
    return;
1×
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);
!
3010
  directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
!
3011
                                     0, resource);
3012
  tor_free(resource);
!
3013
  SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
!
3014
  smartlist_free(missing_fps);
!
3015
}
3016

3017
/** Send a request to every other authority for its detached signatures,
3018
 * unless we have signatures from all other v3 authorities already. */
3019
static void
3020
dirvote_fetch_missing_signatures(void)
1×
3021
{
3022
  int need_any = 0;
1×
3023
  int i;
3024
  for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
3×
3025
    networkstatus_t *consensus = pending_consensuses[i].consensus;
2×
3026
    if (!consensus ||
2×
3027
        networkstatus_check_consensus_signature(consensus, -1) == 1) {
!
3028
      /* We have no consensus, or we have one that's signed by everybody. */
3029
      continue;
2×
3030
    }
3031
    need_any = 1;
3032
  }
3033
  if (!need_any)
1×
3034
    return;
1×
3035

3036
  directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
!
3037
                                     0, NULL);
3038
}
3039

3040
/** Release all storage held by pending consensuses (those waiting for
3041
 * signatures). */
3042
static void
3043
dirvote_clear_pending_consensuses(void)
63×
3044
{
3045
  int i;
3046
  for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
189×
3047
    pending_consensus_t *pc = &pending_consensuses[i];
126×
3048
    tor_free(pc->body);
126×
3049

3050
    networkstatus_vote_free(pc->consensus);
126×
3051
    pc->consensus = NULL;
3052
  }
3053
}
63×
3054

3055
/** Drop all currently pending votes, consensus, and detached signatures. */
3056
static void
3057
dirvote_clear_votes(int all_votes)
32×
3058
{
3059
  if (!previous_vote_list)
32×
3060
    previous_vote_list = smartlist_new();
31×
3061
  if (!pending_vote_list)
32×
3062
    pending_vote_list = smartlist_new();
28×
3063

3064
  /* All "previous" votes are now junk. */
3065
  SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
32×
3066
      cached_dir_decref(v->vote_body);
3067
      v->vote_body = NULL;
3068
      networkstatus_vote_free(v->vote);
3069
      tor_free(v);
3070
    });
3071
  smartlist_clear(previous_vote_list);
32×
3072

3073
  if (all_votes) {
32×
3074
    /* If we're dumping all the votes, we delete the pending ones. */
3075
    SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
31×
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". */
3083
    smartlist_add_all(previous_vote_list, pending_vote_list);
1×
3084
  }
3085
  smartlist_clear(pending_vote_list);
32×
3086

3087
  if (pending_consensus_signature_list) {
32×
3088
    SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
!
3089
                      tor_free(cp));
3090
    smartlist_clear(pending_consensus_signature_list);
!
3091
  }
3092
  tor_free(pending_consensus_signatures);
32×
3093
  dirvote_clear_pending_consensuses();
32×
3094
}
32×
3095

3096
/** Return a newly allocated string containing the hex-encoded v3 authority
3097
    identity digest of every recognized v3 authority. */
3098
static char *
3099
list_v3_auth_ids(void)
!
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

3115
/** Called when we have received a networkstatus vote in <b>vote_body</b>.
3116
 * Parse and validate it, and on success store it as a pending vote (which we
3117
 * then return).  Return NULL on failure.  Sets *<b>msg_out</b> and
3118
 * *<b>status_out</b> to an HTTP response and status code.  (V3 authority
3119
 * only) */
3120
pending_vote_t *
3121
dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
3×
3122
{
3123
  networkstatus_t *vote;
3124
  networkstatus_voter_info_t *vi;
3125
  dir_server_t *ds;
3126
  pending_vote_t *pending_vote = NULL;
3×
3127
  const char *end_of_vote = NULL;
3×
3128
  int any_failed = 0;
3×
3129
  tor_assert(vote_body);
3×
3130
  tor_assert(msg_out);
3×
3131
  tor_assert(status_out);
3×
3132

3133
  if (!pending_vote_list)
3×
3134
    pending_vote_list = smartlist_new();
3×
3135
  *status_out = 0;
3×
3136
  *msg_out = NULL;
3×
3137

3138
 again:
3139
  vote = networkstatus_parse_vote_from_string(vote_body, strlen(vote_body),
3×
3140
                                              &end_of_vote,
3141
                                              NS_TYPE_VOTE);
3142
  if (!end_of_vote)
3×
3143
    end_of_vote = vote_body + strlen(vote_body);
!
3144
  if (!vote) {
3×
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);
3×
3151
  vi = get_voter(vote);
3×
3152
  {
3153
    int any_sig_good = 0;
3×
3154
    SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
3×
3155
                      if (sig->good_signature)
3156
                        any_sig_good = 1);
3157
    tor_assert(any_sig_good);
3×
3158
  }
3159
  ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
3×
3160
  if (!ds) {
3×
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);
3×
3172
  if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
3×
3173
                                     vote->cert->signing_key_digest)) {
3×
3174
    /* Hey, it's a new cert! */
3175
    trusted_dirs_load_certs_from_string(
1×
3176
                               vote->cert->cache_info.signed_descriptor_body,
1×
3177
                               TRUSTED_DIRS_CERTS_SRC_FROM_VOTE, 1 /*flush*/,
3178
                               NULL);
3179
    if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
1×
3180
                                       vote->cert->signing_key_digest)) {
1×
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) {
3×
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);
3×
3198

3199
  /* Now see whether we already have a vote from this authority. */
3200
  SMARTLIST_FOREACH_BEGIN(pending_vote_list, pending_vote_t *, v) {
3×
3201
      if (fast_memeq(v->vote->cert->cache_info.identity_digest,
!
3202
                   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,
3×
3239
                             vote->cert->identity_key);
3×
3240

3241
  pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
3×
3242
  pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
3×
3243
                                                       end_of_vote-vote_body),
3244
                                           vote->published);
3245
  pending_vote->vote = vote;
3×
3246
  smartlist_add(pending_vote_list, pending_vote);
3×
3247

3248
  if (!strcmpstart(end_of_vote, "network-status-version ")) {
3×
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)
3×
3273
    *status_out = 200;
3×
3274
  if (!*msg_out) {
3×
3275
    if (!any_failed && !pending_vote) {
3×
3276
      *msg_out = "Duplicate discarded";
!
3277
    } else {
3278
      *msg_out = "ok";
3×
3279
    }
3280
  }
3281

3282
  return any_failed ? NULL : pending_vote;
3×
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)
1×
3288
{
3289
  smartlist_t *votestrings = smartlist_new();
1×
3290
  char *votefile = NULL;
1×
3291

3292
  SMARTLIST_FOREACH(pending_votes, pending_vote_t *, v,
1×
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");
1×
3301
  write_chunks_to_file(votefile, votestrings, 0, 0);
1×
3302
  log_debug(LD_DIR, "Wrote votes to disk (%s)!", votefile);
1×
3303

3304
  tor_free(votefile);
1×
3305
  SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
1×
3306
  smartlist_free(votestrings);
1×
3307
}
1×
3308

3309
/** Try to compute a v3 networkstatus consensus from the currently pending
3310
 * votes.  Return 0 on success, -1 on failure.  Store the consensus in
3311
 * pending_consensus: it won't be ready to be published until we have
3312
 * everybody else's signatures collected too. (V3 Authority only) */
3313
static int
3314
dirvote_compute_consensuses(void)
1×
3315
{
3316
  /* Have we got enough votes to try? */
3317
  int n_votes, n_voters, n_vote_running = 0;
1×
3318
  smartlist_t *votes = NULL;
1×
3319
  char *consensus_body = NULL, *signatures = NULL;
1×
3320
  networkstatus_t *consensus = NULL;
1×
3321
  authority_cert_t *my_cert;
3322
  pending_consensus_t pending[N_CONSENSUS_FLAVORS];
3323
  int flav;
3324

3325
  memset(pending, 0, sizeof(pending));
3326

3327
  if (!pending_vote_list)
1×
3328
    pending_vote_list = smartlist_new();
!
3329

3330
  /* Write votes to disk */
3331
  write_v3_votes_to_disk(pending_vote_list);
1×
3332

3333
  /* Setup votes smartlist */
3334
  votes = smartlist_new();
1×
3335
  SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
1×
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);
1×
3342
  n_votes = smartlist_len(pending_vote_list);
1×
3343
  if (n_votes <= n_voters/2) {
1×
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
  }
3348
  tor_assert(pending_vote_list);
1×
3349
  SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
1×
3350
    if (smartlist_contains_string(v->vote->known_flags, "Running"))
3351
      n_vote_running++;
3352
  });
3353
  if (!n_vote_running) {
1×
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())) {
1×
3363
    log_warn(LD_DIR, "Can't generate consensus without a certificate.");
1×
3364
    goto err;
1×
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) {
!
3373
      authority_cert_t *cert = get_my_v3_legacy_cert();
!
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. */
3410
      networkstatus_check_consensus_signature(consensus, -1);
!
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

3424
  signatures = get_detached_signatures_from_pending_consensuses(
!
3425
       pending, N_CONSENSUS_FLAVORS);
3426

3427
  if (!signatures) {
!
3428
    log_warn(LD_DIR, "Couldn't extract signatures.");
!
3429
    goto err;
!
3430
  }
3431

3432
  dirvote_clear_pending_consensuses();
!
3433
  memcpy(pending_consensuses, pending, sizeof(pending));
3434

3435
  tor_free(pending_consensus_signatures);
!
3436
  pending_consensus_signatures = signatures;
!
3437

3438
  if (pending_consensus_signature_list) {
!
3439
    int n_sigs = 0;
!
3440
    /* we may have gotten signatures for this consensus before we built
3441
     * it ourself.  Add them now. */
3442
    SMARTLIST_FOREACH_BEGIN(pending_consensus_signature_list, char *, sig) {
!
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);
3457
    smartlist_clear(pending_consensus_signature_list);
!
3458
  }
3459

3460
  log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
!
3461

3462
  directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
!
3463
                               ROUTER_PURPOSE_GENERAL,
3464
                               V3_DIRINFO,
3465
                               pending_consensus_signatures,
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);
1×
3473
  tor_free(consensus_body);
1×
3474
  tor_free(signatures);
1×
3475
  networkstatus_vote_free(consensus);
1×
3476

3477
  return -1;
1×
3478
}
3479

3480
/** Helper: we just got the <b>detached_signatures_body</b> sent to us as
3481
 * signatures on the currently pending consensus.  Add them to <b>pc</b>
3482
 * as appropriate.  Return the number of signatures added. (?) */
3483
static int
3484
dirvote_add_signatures_to_pending_consensus(
!
3485
                       pending_consensus_t *pc,
3486
                       ns_detached_signatures_t *sigs,
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);
!
3497
  tor_assert(pending_consensus_signatures);
!
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
  }
3507
  r = networkstatus_add_detached_signatures(pc->consensus, sigs,
!
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 =
!
3519
      networkstatus_format_signatures(pc->consensus, 0);
!
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
    {
3538
      networkstatus_t *v = networkstatus_parse_vote_from_string(
!
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;
!
3567
  ns_detached_signatures_t *sigs;
3568
  tor_assert(detached_signatures_body);
!
3569
  tor_assert(msg_out);
!
3570
  tor_assert(pending_consensus_signatures);
!
3571

3572
  if (!(sigs = networkstatus_parse_detached_signatures(
!
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 =
!
3599
      get_detached_signatures_from_pending_consensuses(
3600
                      pending_consensuses, N_CONSENSUS_FLAVORS);
3601
    if (new_detached) {
!
3602
      tor_free(pending_consensus_signatures);
!
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

3619
/** Helper: we just got the <b>detached_signatures_body</b> sent to us as
3620
 * signatures on the currently pending consensus.  Add them to the pending
3621
 * consensus (if we have one); otherwise queue them until we have a
3622
 * consensus.  Return negative on failure, nonnegative on success. */
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);
3636
    if (!pending_consensus_signature_list)
!
3637
      pending_consensus_signature_list = smartlist_new();
!
3638
    smartlist_add_strdup(pending_consensus_signature_list,
!
3639
                  detached_signatures_body);
3640
    *msg = "Signature queued";
!
3641
    return 0;
!
3642
  }
3643
}
3644

3645
/** Replace the consensus that we're currently serving with the one that we've
3646
 * been building. (V3 Authority only) */
3647
static int
3648
dirvote_publish_consensus(void)
1×
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) {
3×
3654
    pending_consensus_t *pending = &pending_consensuses[i];
2×
3655
    const char *name;
3656
    name = networkstatus_get_flavor_name(i);
2×
3657
    tor_assert(name);
2×
3658
    if (!pending->consensus ||
2×
3659
      networkstatus_check_consensus_signature(pending->consensus, 1)<0) {
!
3660
      log_warn(LD_DIR, "Not enough info to publish pending %s consensus",name);
2×
3661
      continue;
2×
3662
    }
3663

3664
    if (networkstatus_set_current_consensus(pending->body,
!
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;
1×
3673
}
3674

3675
/** Release all static storage held in dirvote.c */
3676
void
3677
dirvote_free_all(void)
31×
3678
{
3679
  dirvote_clear_votes(1);
31×
3680
  /* now empty as a result of dirvote_clear_votes(). */
3681
  smartlist_free(pending_vote_list);
31×
3682
  pending_vote_list = NULL;
3683
  smartlist_free(previous_vote_list);
31×
3684
  previous_vote_list = NULL;
3685

3686
  dirvote_clear_pending_consensuses();
31×
3687
  tor_free(pending_consensus_signatures);
31×
3688
  if (pending_consensus_signature_list) {
31×
3689
    /* now empty as a result of dirvote_clear_votes(). */
3690
    smartlist_free(pending_consensus_signature_list);
!
3691
    pending_consensus_signature_list = NULL;
3692
  }
3693
}
31×
3694

3695
/* ====
3696
 * Access to pending items.
3697
 * ==== */
3698

3699
/** Return the body of the consensus that we're currently trying to build. */
3700
MOCK_IMPL(const char *,
1×
3701
dirvote_get_pending_consensus, (consensus_flavor_t flav))
3702
{
3703
  tor_assert(((int)flav) >= 0 && (int)flav < N_CONSENSUS_FLAVORS);
1×
3704
  return pending_consensuses[flav].body;
1×
3705
}
3706

3707
/** Return the signatures that we know for the consensus that we're currently
3708
 * trying to build. */
3709
MOCK_IMPL(const char *,
1×
3710
dirvote_get_pending_detached_signatures, (void))
3711
{
3712
  return pending_consensus_signatures;
1×
3713
}
3714

3715
/** Return a given vote specified by <b>fp</b>.  If <b>by_id</b>, return the
3716
 * vote for the authority with the v3 authority identity key digest <b>fp</b>;
3717
 * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>.  If
3718
 * <b>fp</b> is NULL, return our own vote.  If <b>include_previous</b> is
3719
 * false, do not consider any votes for a consensus that's already been built.
3720
 * If <b>include_pending</b> is false, do not consider any votes for the
3721
 * consensus that's in progress.  May return NULL if we have no vote for the
3722
 * authority in question. */
3723
const cached_dir_t *
3724
dirvote_get_vote(const char *fp, int flags)
11×
3725
{
3726
  int by_id = flags & DGV_BY_ID;
11×
3727
  const int include_pending = flags & DGV_INCLUDE_PENDING;
11×
3728
  const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
11×
3729

3730
  if (!pending_vote_list && !previous_vote_list)
11×
3731
    return NULL;
3732
  if (fp == NULL) {
5×
3733
    authority_cert_t *c = get_my_v3_authority_cert();
2×
3734
    if (c) {
2×
3735
      fp = c->cache_info.identity_digest;
2×
3736
      by_id = 1;
2×
3737
    } else
3738
      return NULL;
3739
  }
3740
  if (by_id) {
5×
3741
    if (pending_vote_list && include_pending) {
3×
3742
      SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
2×
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) {
1×
3747
      SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
1×
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) {
2×
3753
      SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
2×
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) {
!
3758
      SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
!
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

3766
/** Construct and return a new microdescriptor from a routerinfo <b>ri</b>
3767
 * according to <b>consensus_method</b>.
3768
 **/
3769
STATIC microdesc_t *
3770
dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
5×
3771
{
3772
  microdesc_t *result = NULL;
5×
3773
  char *key = NULL, *summary = NULL, *family = NULL;
5×
3774
  size_t keylen;
3775
  smartlist_t *chunks = smartlist_new();
5×
3776
  char *output = NULL;
5×
3777
  crypto_pk_t *rsa_pubkey = router_get_rsa_onion_pkey(ri->onion_pkey,
5×
3778
                                                      ri->onion_pkey_len);
3779

3780
  if (crypto_pk_write_public_key_to_string(rsa_pubkey, &key, &keylen)<0)
5×
3781
    goto done;
3782
  summary = policy_summarize(ri->exit_policy, AF_INET);
5×
3783
  if (ri->declared_family)
5×
3784
    family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
2×
3785

3786
  smartlist_add_asprintf(chunks, "onion-key\n%s", key);
5×
3787

3788
  if (ri->onion_curve25519_pkey) {
5×
3789
    char kbuf[128];
3790
    base64_encode(kbuf, sizeof(kbuf),
5×
3791
                  (const char*)ri->onion_curve25519_pkey->public_key,
5×
3792
                  CURVE25519_PUBKEY_LEN, BASE64_ENCODE_MULTILINE);
3793
    smartlist_add_asprintf(chunks, "ntor-onion-key %s", kbuf);
5×
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 &&
8×
3799
      !tor_addr_is_null(&ri->ipv6_addr) && ri->ipv6_orport)
3×
3800
    smartlist_add_asprintf(chunks, "a %s\n",
!
3801
                           fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
3802

3803
  if (family) {
5×
3804
    if (consensus_method < MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS) {
2×
3805
      smartlist_add_asprintf(chunks, "family %s\n", family);
1×
3806
    } else {
3807
      const uint8_t *id = (const uint8_t *)ri->cache_info.identity_digest;
1×
3808
      char *canonical_family = nodefamily_canonicalize(family, id, 0);
1×
3809
      smartlist_add_asprintf(chunks, "family %s\n", canonical_family);
1×
3810
      tor_free(canonical_family);
1×
3811
    }
3812
  }
3813

3814
  if (summary && strcmp(summary, "reject 1-65535"))
5×
3815
    smartlist_add_asprintf(chunks, "p %s\n", summary);
2×
3816

3817
  if (ri->ipv6_exit_policy) {
5×
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 &&
5×
3831
        ri->cache_info.signing_key_cert->signing_key_included) {
3832
      keytype = "ed25519";
3×
3833
      ed25519_public_to_base64(idbuf,
3×
3834
                               &ri->cache_info.signing_key_cert->signing_key);
3×
3835
    } else {
3836
      keytype = "rsa1024";
2×
3837
      digest_to_base64(idbuf, ri->cache_info.identity_digest);
2×
3838
    }
3839
    smartlist_add_asprintf(chunks, "id %s %s\n", keytype, idbuf);
5×
3840
  }
3841

3842
  output = smartlist_join_strings(chunks, "", 0, NULL);
5×
3843

3844
  {
3845
    smartlist_t *lst = microdescs_parse_from_string(output,
5×
3846
                                                    output+strlen(output), 0,
5×
3847
                                                    SAVED_NOWHERE, NULL);
3848
    if (smartlist_len(lst) != 1) {
5×
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);
5×
3855
    smartlist_free(lst);
5×
3856
  }
3857

3858
 done:
3859
  crypto_pk_free(rsa_pubkey);
5×
3860
  tor_free(output);
5×
3861
  tor_free(key);
5×
3862
  tor_free(summary);
5×
3863
  tor_free(family);
5×
3864
  if (chunks) {
5×
3865
    SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
5×
3866
    smartlist_free(chunks);
5×
3867
  }
3868
  return result;
5×
3869
}
3870

3871
/** Format the appropriate vote line to describe the microdescriptor <b>md</b>
3872
 * in a consensus vote document.  Write it into the <b>out_len</b>-byte buffer
3873
 * in <b>out</b>.  Return -1 on failure and the number of characters written
3874
 * on success. */
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

3903
/** Array of start and end of consensus methods used for supported
3904
    microdescriptor formats. */
3905
static const struct consensus_method_range_t {
3906
  int low;
3907
  int high;
3908
} microdesc_consensus_methods[] = {
3909
  {MIN_SUPPORTED_CONSENSUS_METHOD, MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC - 1},
3910
  {MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC,
3911
   MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS - 1},
3912
  {MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS,
3913
   MAX_SUPPORTED_CONSENSUS_METHOD},
3914
  {-1, -1}
3915
};
3916

3917
/** Helper type used when generating the microdescriptor lines in a directory
3918
 * vote. */
3919
typedef struct microdesc_vote_line_t {
3920
  int low;
3921
  int high;
3922
  microdesc_t *md;
3923
  struct microdesc_vote_line_t *next;
3924
} microdesc_vote_line_t;
3925

3926
/** Generate and return a linked list of all the lines that should appear to
3927
 * describe a router's microdescriptor versions in a directory vote.
3928
 * Add the generated microdescriptors to <b>microdescriptors_out</b>. */
3929
vote_microdesc_hash_t *
3930
dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now,
!
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) {
!
3943
      microdesc_vote_line_t *e =
!
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];
3969
    vote_microdesc_hash_t *h;
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

3986
/** Parse and extract all SR commits from <b>tokens</b> and place them in
3987
 *  <b>ns</b>. */
3988
static void
3989
extract_shared_random_commits(networkstatus_t *ns, const smartlist_t *tokens)
54×
3990
{
3991
  smartlist_t *chunks = NULL;
54×
3992

3993
  tor_assert(ns);
54×
3994
  tor_assert(tokens);
54×
3995
  /* Commits are only present in a vote. */
3996
  tor_assert(ns->type == NS_TYPE_VOTE);
54×
3997

3998
  ns->sr_info.commits = smartlist_new();
54×
3999

4000
  smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
54×
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) {
54×
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);
54×
4037
  smartlist_free(commits);
54×
4038
}
54×
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)
57×
4046
{
4047
  /* Does this authority participates in the SR protocol? */
4048
  directory_token_t *tok = find_opt_by_keyword(tokens, K_SR_FLAG);
57×
4049
  if (tok) {
57×
4050
    ns->sr_info.participate = 1;
54×
4051
    /* Get the SR commitments and reveals from the vote. */
4052
    extract_shared_random_commits(ns, tokens);
54×
4053
  }
4054
}
57×
4055

4056
/* For the given vote, free the shared random commits if any. */
4057
void
4058
dirvote_clear_commits(networkstatus_t *ns)
54×
4059
{
4060
  tor_assert(ns->type == NS_TYPE_VOTE);
54×
4061

4062
  if (ns->sr_info.commits) {
54×
4063
    SMARTLIST_FOREACH(ns->sr_info.commits, sr_commit_t *, c,
54×
4064
                      sr_commit_free(c));
4065
    smartlist_free(ns->sr_info.commits);
54×
4066
  }
4067
}
54×
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,
16×
4074
                               smartlist_t *dir_items)
4075
{
4076
  int current;
4077

4078
  url += strlen("/tor/status-vote/");
16×
4079
  current = !strcmpstart(url, "current/");
16×
4080
  url = strchr(url, '/');
16×
4081
  tor_assert(url);
16×
4082
  ++url;
16×
4083
  if (!strcmp(url, "consensus")) {
16×
4084
    const char *item;
4085
    tor_assert(!current); /* we handle current consensus specially above,
3×
4086
                           * since it wants to be spooled. */
4087
    if ((item = dirvote_get_pending_consensus(FLAV_NS)))
3×
4088
      smartlist_add(items, (char*)item);
2×
4089
  } else if (!current && !strcmp(url, "consensus-signatures")) {
13×
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()))
3×
4095
      smartlist_add(items, (char*)item);
2×
4096
  } else if (!strcmp(url, "authority")) {
10×
4097
    const cached_dir_t *d;
4098
    int flags = DGV_BY_ID |
4×
4099
      (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
4100
    if ((d=dirvote_get_vote(NULL, flags)))
4×
4101
      smartlist_add(dir_items, (cached_dir_t*)d);
2×
4102
  } else {
4103
    const cached_dir_t *d;
4104
    smartlist_t *fps = smartlist_new();
6×
4105
    int flags;
4106
    if (!strcmpstart(url, "d/")) {
6×
4107
      url += 2;
4×
4108
      flags = DGV_INCLUDE_PENDING | DGV_INCLUDE_PREVIOUS;
4×
4109
    } else {
4110
      flags = DGV_BY_ID |
2×
4111
        (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
4112
    }
4113
    dir_split_resource_into_fingerprints(url, fps, NULL,
6×
4114
                                         DSR_HEX|DSR_SORT_UNIQ);
4115
    SMARTLIST_FOREACH(fps, char *, fp, {
6×
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);
6×
4121
  }
4122
}
16×
4123

4124
/** Get the best estimate of a router's bandwidth for dirauth purposes,
4125
 * preferring measured to advertised values if available. */
4126
static uint32_t
4127
dirserv_get_bandwidth_for_router_kb(const routerinfo_t *ri)
!
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
     */
4143
    if (dirserv_query_measured_bw_cache_kb(ri->cache_info.identity_digest,
!
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

4156
/** Helper for sorting: compares two routerinfos first by address, and then by
4157
 * descending order of "usefulness".  (An authority is more useful than a
4158
 * non-authority; a running router is more useful than a non-running router;
4159
 * and a router with more bandwidth is more useful than one with less.)
4160
 **/
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

4216
/** Given a list of routerinfo_t in <b>routers</b>, return a new digestmap_t
4217
 * whose keys are the identity digests of those routers that we're going to
4218
 * exclude for Sybil-like appearance. */
4219
static digestmap_t *
4220
get_possible_sybil_list(const smartlist_t *routers)
!
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);
!
4233
  smartlist_sort(routers_by_ip, compare_routerinfo_by_ip_and_bw_);
!
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

4251
/** Given a platform string as in a routerinfo_t (possibly null), return a
4252
 * newly allocated version string for a networkstatus document, or NULL if the
4253
 * platform doesn't give a Tor version. */
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

4271
/** Given a (possibly empty) list of config_line_t, each line of which contains
4272
 * a list of comma-separated version numbers surrounded by optional space,
4273
 * allocate and return a new string containing the version numbers, in order,
4274
 * separated by commas.  Used to generate Recommended(Client|Server)?Versions
4275
 */
4276
char *
4277
format_recommended_version_list(const config_line_t *ln, int warn)
35×
4278
{
4279
  smartlist_t *versions;
4280
  char *result;
4281
  versions = smartlist_new();
35×
4282
  for ( ; ln; ln = ln->next) {
48×
4283
    smartlist_split_string(versions, ln->value, ",",
13×
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();
35×
4290
  SMARTLIST_FOREACH_BEGIN(versions, char *, v) {
60×
4291
    if (strchr(v, ' ')) {
25×
4292
      if (warn)
1×
4293
        log_warn(LD_DIRSERV, "Unexpected space in versions list member %s. "
1×
4294
                 "(These are supposed to be comma-separated; I'll pretend you "
4295
                 "used commas instead.)", escaped(v));
4296
      SMARTLIST_DEL_CURRENT(versions, v);
1×
4297
      smartlist_split_string(more_versions, v, NULL,
1×
4298
                             SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
4299
      tor_free(v);
1×
4300
    }
4301
  } SMARTLIST_FOREACH_END(v);
25×
4302
  smartlist_add_all(versions, more_versions);
35×
4303
  smartlist_free(more_versions);
35×
4304

4305
  /* Check to make sure everything looks like a version. */
4306
  if (warn) {
35×
4307
    SMARTLIST_FOREACH_BEGIN(versions, const char *, v) {
61×
4308
      tor_version_t ver;
4309
      if (tor_version_parse(v, &ver) < 0) {
26×
4310
        log_warn(LD_DIRSERV, "Recommended version %s does not look valid. "
3×
4311
                 " (I'll include it anyway, since you told me to.)",
4312
                 escaped(v));
4313
      }
4314
    } SMARTLIST_FOREACH_END(v);
26×
4315
  }
4316

4317
  sort_version_list(versions, 1);
35×
4318
  result = smartlist_join_strings(versions,",",0,NULL);
35×
4319
  SMARTLIST_FOREACH(versions,char *,s,tor_free(s));
35×
4320
  smartlist_free(versions);
35×
4321
  return result;
35×
4322
}
4323

4324
/** If there are entries in <b>routers</b> with exactly the same ed25519 keys,
4325
 * remove the older one.  If they are exactly the same age, remove the one
4326
 * with the greater descriptor digest. May alter the order of the list. */
4327
static void
4328
routers_make_ed_keys_unique(smartlist_t *routers)
!
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

4368
/** Routerstatus <b>rs</b> is part of a group of routers that are on
4369
 * too narrow an IP-space. Clear out its flags since we don't want it be used
4370
 * because of its Sybil-like appearance.
4371
 *
4372
 * Leave its BadExit flag alone though, since if we think it's a bad exit,
4373
 * we want to vote that way in case all the other authorities are voting
4374
 * Running and Exit.
4375
 */
4376
static void
4377
clear_status_flags_on_sybil(routerstatus_t *rs)
!
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

4387
/** Space-separated list of all the flags that we will always vote on. */
4388
const char DIRVOTE_UNIVERSAL_FLAGS[] =
4389
  "Authority "
4390
  "Exit "
4391
  "Fast "
4392
  "Guard "
4393
  "HSDir "
4394
  "Stable "
4395
  "StaleDesc "
4396
  "V2Dir "
4397
  "Valid";
4398
/** Space-separated list of all flags that we may or may not vote on,
4399
 * depending on our configuration. */
4400
const char DIRVOTE_OPTIONAL_FLAGS[] =
4401
  "BadExit "
4402
  "Running";
4403

4404
/** Return a new networkstatus_t* containing our current opinion. (For v3
4405
 * authorities) */
4406
networkstatus_t *
4407
dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
!
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;
!
4419
  routerlist_t *rl = router_get_routerlist();
!
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
     */
4471
    if (dirserv_get_measured_bw_cache_size() > 0) {
!
4472
      dirserv_clear_measured_bw_cache();
!
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);
!
4492
  } DIGESTMAP_FOREACH_END;
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

4498
  dirserv_compute_performance_thresholds(omit_as_sybil);
!
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))
!
4529
        clear_status_flags_on_sybil(rs);
!
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(
!
4539
                                protover_compute_for_old_tor(vrs->version));
4540
      }
4541
      vrs->microdesc = dirvote_format_all_microdesc_vote_lines(ri, now,
!
4542
                                                            microdescriptors);
4543

4544
      smartlist_add(routerstatuses, vrs);
!
4545
    }
4546
  } SMARTLIST_FOREACH_END(ri);
4547

4548
  {
4549
    smartlist_t *added =
!
4550
      microdescs_add_list_to_cache(get_microdesc_cache(),
!
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) {
!
4561
    dirserv_read_guardfraction_file(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();
!
4569
    dirserv_read_measured_bandwidths(options->V3BandwidthsFile,
!
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
     */
4576
    if (dirserv_get_measured_bw_cache_size() > 0) {
!
4577
      dirserv_clear_measured_bw_cache();
!
4578
    }
4579
  }
4580

4581
  v3_out = tor_malloc_zero(sizeof(networkstatus_t));
!
4582

4583
  v3_out->type = NS_TYPE_VOTE;
!
4584
  dirvote_get_preferred_voting_intervals(&timing);
!
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 =
!
4597
      voting_schedule_get_start_of_next_interval(now,
!
4598
                                   (int)last_consensus_interval,
4599
                                   options->TestingV3AuthVotingStartOffset);
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();
!
4651
  smartlist_split_string(v3_out->known_flags,
!
4652
                         DIRVOTE_UNIVERSAL_FLAGS,
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");
!
4658
  smartlist_sort_strings(v3_out->known_flags);
!
4659

4660
  if (options->ConsensusParams) {
!
4661
    v3_out->net_params = smartlist_new();
!
4662
    smartlist_split_string(v3_out->net_params,
!
4663
                           options->ConsensusParams, NULL, 0, 0);
!
4664
    smartlist_sort_strings(v3_out->net_params);
!
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) {
!
4678
    authority_cert_t *c = get_my_v3_legacy_cert();
!
4679
    if (c) {
!
4680
      if (crypto_pk_get_digest(c->identity_key, voter->legacy_id_digest)) {
!
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
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2021 Coveralls, Inc