Commit e5dd46be authored by juga's avatar juga
Browse files

Add the Bandwidth List file headers to votes

* add bwlist_headers argument to dirserv_read_measured_bandwidth
  in order to store all the headers found when parsing the file
* add bwlist_headers to networkstatus_t in order to store the
  the headers found by the previous function
* include the bandwidth headers as string in vote documents
* add test to check that dirserv_read_measured_bandwidth generates
  the bwlist_headers
parent 8505522e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3560,7 +3560,7 @@ options_validate(or_options_t *old_options, or_options_t *options,
             "(Bridge/V3)AuthoritativeDir is set.");
    /* If we have a v3bandwidthsfile and it's broken, complain on startup */
    if (options->V3BandwidthsFile && !old_options) {
      dirserv_read_measured_bandwidths(options->V3BandwidthsFile, NULL);
      dirserv_read_measured_bandwidths(options->V3BandwidthsFile, NULL, NULL);
    }
    /* same for guardfraction file */
    if (options->GuardfractionFile && !old_options) {
+12 −6
Original line number Diff line number Diff line
@@ -254,6 +254,8 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
    /* XXXX Abstraction violation: should be pulling a field out of v3_ns.*/
    char *flag_thresholds = dirserv_get_flag_thresholds_line();
    char *params;
    char *bwlist_headers = smartlist_join_strings(v3_ns->bwlist_headers,
                                                  " ", 0, NULL);
    authority_cert_t *cert = v3_ns->cert;
    char *methods =
      make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD,
@@ -267,7 +269,6 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
      params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
    else
      params = tor_strdup("");

    tor_assert(cert);
    smartlist_add_asprintf(chunks,
                 "network-status-version 3\n"
@@ -286,7 +287,8 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
                 "params %s\n"
                 "dir-source %s %s %s %s %d %d\n"
                 "contact %s\n"
                 "%s", /* shared randomness information */
                 "%s" /* shared randomness information */
                 "bandwidth-file %s\n", /* bandwidth file headers */
                 v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
                 methods,
                 published, va, fu, vu,
@@ -302,13 +304,15 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
                 fmt_addr32(addr), voter->dir_port, voter->or_port,
                 voter->contact,
                 shared_random_vote_str ?
                           shared_random_vote_str : "");
                           shared_random_vote_str : "",
                 bwlist_headers);

    tor_free(params);
    tor_free(flags);
    tor_free(flag_thresholds);
    tor_free(methods);
    tor_free(shared_random_vote_str);
    tor_free(bwlist_headers);

    if (!tor_digest_is_zero(voter->legacy_id_digest)) {
      char fpbuf[HEX_DIGEST_LEN+1];
@@ -4291,7 +4295,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
  uint32_t addr;
  char *hostname = NULL, *client_versions = NULL, *server_versions = NULL;
  const char *contact;
  smartlist_t *routers, *routerstatuses;
  smartlist_t *routers, *routerstatuses, *bwlist_headers;
  char identity_digest[DIGEST_LEN];
  char signing_key_digest[DIGEST_LEN];
  int listbadexits = options->AuthDirListBadExits;
@@ -4338,7 +4342,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
   * set_routerstatus_from_routerinfo() see up-to-date bandwidth info.
   */
  if (options->V3BandwidthsFile) {
    dirserv_read_measured_bandwidths(options->V3BandwidthsFile, NULL);
    dirserv_read_measured_bandwidths(options->V3BandwidthsFile, NULL, NULL);
  } else {
    /*
     * No bandwidths file; clear the measured bandwidth cache in case we had
@@ -4375,6 +4379,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,

  routerstatuses = smartlist_new();
  microdescriptors = smartlist_new();
  bwlist_headers = smartlist_new();

  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
    /* If it has a protover list and contains a protocol name greater than
@@ -4441,7 +4446,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
  /* This pass through applies the measured bw lines to the routerstatuses */
  if (options->V3BandwidthsFile) {
    dirserv_read_measured_bandwidths(options->V3BandwidthsFile,
                                     routerstatuses);
                                     routerstatuses, bwlist_headers);
  } else {
    /*
     * No bandwidths file; clear the measured bandwidth cache in case we had
@@ -4537,6 +4542,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
                           options->ConsensusParams, NULL, 0, 0);
    smartlist_sort_strings(v3_out->net_params);
  }
  v3_out->bwlist_headers = bwlist_headers;

  voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  voter->nickname = tor_strdup(options->Nickname);
+12 −3
Original line number Diff line number Diff line
@@ -2599,12 +2599,14 @@ measured_bw_line_apply(measured_bw_line_t *parsed_line,
}

/**
 * Read the measured bandwidth file and apply it to the list of
 * vote_routerstatus_t. Returns -1 on error, 0 otherwise.
 * Read the measured bandwidth list file, apply it to the list of
 * vote_routerstatus_t and store all the headers in <b>bwlist_headers</b>.
 * Returns -1 on error, 0 otherwise.
 */
int
dirserv_read_measured_bandwidths(const char *from_file,
                                 smartlist_t *routerstatuses)
                                 smartlist_t *routerstatuses,
                                 smartlist_t *bwlist_headers)
{
  FILE *fp = tor_fopen_cloexec(from_file, "r");
  int applied_lines = 0;
@@ -2654,6 +2656,8 @@ dirserv_read_measured_bandwidths(const char *from_file,
    goto err;
  }

  smartlist_add_asprintf(bwlist_headers, "timestamp=%ld", file_time);

  if (routerstatuses)
    smartlist_sort(routerstatuses, compare_vote_routerstatus_entries);

@@ -2669,6 +2673,11 @@ dirserv_read_measured_bandwidths(const char *from_file,
        dirserv_cache_measured_bw(&parsed_line, file_time);
        if (measured_bw_line_apply(&parsed_line, routerstatuses) > 0)
          applied_lines++;
      } else {
        if (strcmp(line, "====\n") != 0) {
          line[strlen(line)-1] = '\0';
          smartlist_add_strdup(bwlist_headers, line);
        };
      }
    }
  }
+2 −1
Original line number Diff line number Diff line
@@ -215,7 +215,8 @@ dirserv_read_guardfraction_file_from_str(const char *guardfraction_file_str,
#endif /* defined(DIRSERV_PRIVATE) */

int dirserv_read_measured_bandwidths(const char *from_file,
                                     smartlist_t *routerstatuses);
                                     smartlist_t *routerstatuses,
                                     smartlist_t *bwlist_headers);

int dirserv_read_guardfraction_file(const char *fname,
                                 smartlist_t *vote_routerstatuses);
+40 −3
Original line number Diff line number Diff line
@@ -1595,18 +1595,55 @@ test_dir_measured_bw_kb(void *arg)
static void
test_dir_dirserv_read_measured_bandwidths_empty(void *arg)
{
  char *fname=NULL;
  (void)arg;
  char *content = NULL;
  time_t timestamp = time(NULL);
  char *fname = tor_strdup(get_fname("V3BandwidthsFile"));
  smartlist_t *bwlist_headers = smartlist_new();
  char *bwlist_headers_str = NULL;
  char *out_bwlist_headers_str = NULL;

  fname = tor_strdup(get_fname("V3BandwidthsFile"));
  /* Test an empty file */
  write_str_to_file(fname, "", 0);
  setup_capture_of_logs(LOG_WARN);
  tt_int_op(-1, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL));
  tt_int_op(-1, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL, NULL));
  expect_log_msg("Empty bandwidth file\n");

  /* Test v1.1.0 headers */
  const char *v110_header_lines=
    "version=1.1.0\n"
    "software=sbws\n"
    "software_version=0.1.0\n"
    "generator_started=2018-05-08T16:13:25\n"
    "earliest_bandwidth=2018-05-08T16:13:26\n"
    "====\n";

  /* And test bwlist_headers generation for dirvote.c */
  tor_asprintf(&content, "%ld\n%s", timestamp, v110_header_lines);
  write_str_to_file(fname, content, 0);
  tor_free(content);
  tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
                                                       bwlist_headers));

  /* The bwlist_headers str that should get generated by the previous
   * v110v110_header_lines */
  const char *headers_str = "version=1.1.0 software=sbws "
                            "software_version=0.1.0 "
                            "generator_started=2018-05-08T16:13:25 "
                            "earliest_bandwidth=2018-05-08T16:13:26";
  tor_asprintf(&bwlist_headers_str, "timestamp=%ld %s", timestamp,
               headers_str);
  /* Compare the strings */
  out_bwlist_headers_str = smartlist_join_strings(bwlist_headers, " ",
                                                        0, NULL);
  tt_str_op(bwlist_headers_str, OP_EQ, out_bwlist_headers_str);

 done:
  tor_free(fname);
  tor_free(bwlist_headers_str);
  tor_free(out_bwlist_headers_str);
  SMARTLIST_FOREACH(bwlist_headers, char *, cp, tor_free(cp));
  smartlist_free(bwlist_headers);
  teardown_capture_of_logs();
}