tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

dirclient.c (112064B)


      1 /* Copyright (c) 2001-2004, Roger Dingledine.
      2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
      3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
      4 /* See LICENSE for licensing information */
      5 
      6 /**
      7 * @file dirclient.c
      8 * @brief Download directory information
      9 **/
     10 
     11 #define DIRCLIENT_PRIVATE
     12 
     13 #include "core/or/or.h"
     14 
     15 #include "app/config/config.h"
     16 #include "core/mainloop/connection.h"
     17 #include "core/mainloop/mainloop.h"
     18 #include "core/or/connection_edge.h"
     19 #include "core/or/policies.h"
     20 #include "feature/client/bridges.h"
     21 #include "feature/client/entrynodes.h"
     22 #include "feature/control/control_events.h"
     23 #include "feature/dirauth/authmode.h"
     24 #include "feature/dirclient/dirclient.h"
     25 #include "feature/dirauth/dirvote.h"
     26 #include "feature/dirauth/shared_random.h"
     27 #include "feature/dircache/dirserv.h"
     28 #include "feature/dirclient/dirclient.h"
     29 #include "feature/dirclient/dirclient_modes.h"
     30 #include "feature/dirclient/dlstatus.h"
     31 #include "feature/dircommon/consdiff.h"
     32 #include "feature/dircommon/directory.h"
     33 #include "feature/dircommon/fp_pair.h"
     34 #include "feature/hs/hs_cache.h"
     35 #include "feature/hs/hs_client.h"
     36 #include "feature/hs/hs_control.h"
     37 #include "feature/nodelist/authcert.h"
     38 #include "feature/nodelist/describe.h"
     39 #include "feature/nodelist/dirlist.h"
     40 #include "feature/nodelist/microdesc.h"
     41 #include "feature/nodelist/networkstatus.h"
     42 #include "feature/nodelist/node_select.h"
     43 #include "feature/nodelist/nodelist.h"
     44 #include "feature/nodelist/routerinfo.h"
     45 #include "feature/nodelist/routerlist.h"
     46 #include "feature/nodelist/routerset.h"
     47 #include "feature/relay/relay_find_addr.h"
     48 #include "feature/relay/routermode.h"
     49 #include "feature/relay/selftest.h"
     50 #include "feature/rend/rendcommon.h"
     51 #include "feature/stats/predict_ports.h"
     52 
     53 #include "lib/cc/ctassert.h"
     54 #include "lib/compress/compress.h"
     55 #include "lib/crypt_ops/crypto_format.h"
     56 #include "lib/crypt_ops/crypto_util.h"
     57 #include "lib/encoding/confline.h"
     58 #include "lib/err/backtrace.h"
     59 
     60 #include "core/or/entry_connection_st.h"
     61 #include "feature/dircache/cached_dir_st.h"
     62 #include "feature/dirclient/dir_server_st.h"
     63 #include "feature/dircommon/dir_connection_st.h"
     64 #include "feature/nodelist/networkstatus_st.h"
     65 #include "feature/nodelist/node_st.h"
     66 #include "feature/nodelist/routerinfo_st.h"
     67 
     68 /** Maximum size, in bytes, for any directory object that we've downloaded. */
     69 #define MAX_DIR_DL_SIZE ((1<<24)-1) /* 16 MB - 1 */
     70 
     71 /** How far in the future do we allow a directory server to tell us it is
     72 * before deciding that one of us has the wrong time? */
     73 #define ALLOW_DIRECTORY_TIME_SKEW (30*60)
     74 
     75 static int body_is_plausible(const char *body, size_t body_len, int purpose);
     76 static void connection_dir_download_routerdesc_failed(dir_connection_t *conn);
     77 static void connection_dir_bridge_routerdesc_failed(dir_connection_t *conn);
     78 static void connection_dir_download_cert_failed(
     79                               dir_connection_t *conn, int status_code);
     80 static void connection_dir_retry_bridges(smartlist_t *descs);
     81 static void dir_routerdesc_download_failed(smartlist_t *failed,
     82                                           int status_code,
     83                                           int router_purpose,
     84                                           int was_extrainfo,
     85                                           int was_descriptor_digests);
     86 static void dir_microdesc_download_failed(smartlist_t *failed,
     87                                          int status_code,
     88                                          const char *dir_id);
     89 static void directory_send_command(dir_connection_t *conn,
     90                                   const int direct,
     91                                   const directory_request_t *req);
     92 static void connection_dir_close_consensus_fetches(
     93                   dir_connection_t *except_this_one, const char *resource);
     94 
     95 /** Return a string describing a given directory connection purpose. */
     96 STATIC const char *
     97 dir_conn_purpose_to_string(int purpose)
     98 {
     99  switch (purpose)
    100    {
    101    case DIR_PURPOSE_UPLOAD_DIR:
    102      return "server descriptor upload";
    103    case DIR_PURPOSE_UPLOAD_VOTE:
    104      return "consensus vote upload";
    105    case DIR_PURPOSE_UPLOAD_SIGNATURES:
    106      return "consensus signature upload";
    107    case DIR_PURPOSE_FETCH_SERVERDESC:
    108      return "server descriptor fetch";
    109    case DIR_PURPOSE_FETCH_EXTRAINFO:
    110      return "extra-info fetch";
    111    case DIR_PURPOSE_FETCH_CONSENSUS:
    112      return "consensus network-status fetch";
    113    case DIR_PURPOSE_FETCH_CERTIFICATE:
    114      return "authority cert fetch";
    115    case DIR_PURPOSE_FETCH_STATUS_VOTE:
    116      return "status vote fetch";
    117    case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
    118      return "consensus signature fetch";
    119    case DIR_PURPOSE_FETCH_HSDESC:
    120      return "hidden-service descriptor fetch";
    121    case DIR_PURPOSE_UPLOAD_HSDESC:
    122      return "hidden-service descriptor upload";
    123    case DIR_PURPOSE_FETCH_MICRODESC:
    124      return "microdescriptor fetch";
    125    }
    126 
    127  log_warn(LD_BUG, "Called with unknown purpose %d", purpose);
    128  return "(unknown)";
    129 }
    130 
    131 /** Return the requisite directory information types. */
    132 STATIC dirinfo_type_t
    133 dir_fetch_type(int dir_purpose, int router_purpose, const char *resource)
    134 {
    135  dirinfo_type_t type;
    136  switch (dir_purpose) {
    137    case DIR_PURPOSE_FETCH_EXTRAINFO:
    138      type = EXTRAINFO_DIRINFO;
    139      if (router_purpose == ROUTER_PURPOSE_BRIDGE)
    140        type |= BRIDGE_DIRINFO;
    141      else
    142        type |= V3_DIRINFO;
    143      break;
    144    case DIR_PURPOSE_FETCH_SERVERDESC:
    145      if (router_purpose == ROUTER_PURPOSE_BRIDGE)
    146        type = BRIDGE_DIRINFO;
    147      else
    148        type = V3_DIRINFO;
    149      break;
    150    case DIR_PURPOSE_FETCH_STATUS_VOTE:
    151    case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
    152    case DIR_PURPOSE_FETCH_CERTIFICATE:
    153      type = V3_DIRINFO;
    154      break;
    155    case DIR_PURPOSE_FETCH_CONSENSUS:
    156      type = V3_DIRINFO;
    157      if (resource && !strcmp(resource, "microdesc"))
    158        type |= MICRODESC_DIRINFO;
    159      break;
    160    case DIR_PURPOSE_FETCH_MICRODESC:
    161      type = MICRODESC_DIRINFO;
    162      break;
    163    default:
    164      log_warn(LD_BUG, "Unexpected purpose %d", (int)dir_purpose);
    165      type = NO_DIRINFO;
    166      break;
    167  }
    168  return type;
    169 }
    170 
    171 /** Return true iff <b>identity_digest</b> is the digest of a router which
    172 * says that it caches extrainfos.  (If <b>is_authority</b> we always
    173 * believe that to be true.) */
    174 int
    175 router_supports_extrainfo(const char *identity_digest, int is_authority)
    176 {
    177  const node_t *node = node_get_by_id(identity_digest);
    178 
    179  if (node && node->ri) {
    180    if (node->ri->caches_extra_info)
    181      return 1;
    182  }
    183  if (is_authority) {
    184    return 1;
    185  }
    186  return 0;
    187 }
    188 
    189 /** Return true iff any trusted directory authority has accepted our
    190 * server descriptor.
    191 *
    192 * We consider any authority sufficient because waiting for all of
    193 * them means it never happens while any authority is down; we don't
    194 * go for something more complex in the middle (like \>1/3 or \>1/2 or
    195 * \>=1/2) because that doesn't seem necessary yet.
    196 */
    197 int
    198 directories_have_accepted_server_descriptor(void)
    199 {
    200  const smartlist_t *servers = router_get_trusted_dir_servers();
    201  const or_options_t *options = get_options();
    202  SMARTLIST_FOREACH(servers, dir_server_t *, d, {
    203    if ((d->type & options->PublishServerDescriptor_) &&
    204        d->has_accepted_serverdesc) {
    205      return 1;
    206    }
    207  });
    208  return 0;
    209 }
    210 
    211 /** Start a connection to every suitable directory authority, using
    212 * connection purpose <b>dir_purpose</b> and uploading <b>payload</b>
    213 * (of length <b>payload_len</b>). The dir_purpose should be one of
    214 * 'DIR_PURPOSE_UPLOAD_{DIR|VOTE|SIGNATURES}'.
    215 *
    216 * <b>router_purpose</b> describes the type of descriptor we're
    217 * publishing, if we're publishing a descriptor -- e.g. general or bridge.
    218 *
    219 * <b>type</b> specifies what sort of dir authorities (V3,
    220 * BRIDGE, etc) we should upload to.
    221 *
    222 * If <b>extrainfo_len</b> is nonzero, the first <b>payload_len</b> bytes of
    223 * <b>payload</b> hold a router descriptor, and the next <b>extrainfo_len</b>
    224 * bytes of <b>payload</b> hold an extra-info document.  Upload the descriptor
    225 * to all authorities, and the extra-info document to all authorities that
    226 * support it.
    227 */
    228 void
    229 directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
    230                             dirinfo_type_t type,
    231                             const char *payload,
    232                             size_t payload_len, size_t extrainfo_len)
    233 {
    234  const or_options_t *options = get_options();
    235  dir_indirection_t indirection;
    236  const smartlist_t *dirservers = router_get_trusted_dir_servers();
    237  int found = 0;
    238  const int exclude_self = (dir_purpose == DIR_PURPOSE_UPLOAD_VOTE ||
    239                            dir_purpose == DIR_PURPOSE_UPLOAD_SIGNATURES);
    240  tor_assert(dirservers);
    241  /* This tries dirservers which we believe to be down, but ultimately, that's
    242   * harmless, and we may as well err on the side of getting things uploaded.
    243   */
    244  SMARTLIST_FOREACH_BEGIN(dirservers, dir_server_t *, ds) {
    245      const routerstatus_t *rs = router_get_consensus_status_by_id(ds->digest);
    246      if (!rs) {
    247        /* prefer to use the address in the consensus, but fall back to
    248         * the hard-coded trusted_dir_server address if we don't have a
    249         * consensus or this digest isn't in our consensus. */
    250        rs = &ds->fake_status;
    251      }
    252 
    253      size_t upload_len = payload_len;
    254 
    255      if ((type & ds->type) == 0)
    256        continue;
    257 
    258      if (exclude_self && router_digest_is_me(ds->digest)) {
    259        /* we don't upload to ourselves, but there's now at least
    260         * one authority of this type that has what we wanted to upload. */
    261        found = 1;
    262        continue;
    263      }
    264 
    265      if (options->StrictNodes &&
    266          routerset_contains_routerstatus(options->ExcludeNodes, rs, -1)) {
    267        log_warn(LD_DIR, "Wanted to contact authority '%s' for %s, but "
    268                 "it's in our ExcludedNodes list and StrictNodes is set. "
    269                 "Skipping.",
    270                 ds->nickname,
    271                 dir_conn_purpose_to_string(dir_purpose));
    272        continue;
    273      }
    274 
    275      found = 1; /* at least one authority of this type was listed */
    276      if (dir_purpose == DIR_PURPOSE_UPLOAD_DIR)
    277        ds->has_accepted_serverdesc = 0;
    278 
    279      if (extrainfo_len && router_supports_extrainfo(ds->digest, 1)) {
    280        upload_len += extrainfo_len;
    281        log_info(LD_DIR, "Uploading an extrainfo too (length %d)",
    282                 (int) extrainfo_len);
    283      }
    284      if (purpose_needs_anonymity(dir_purpose, router_purpose, NULL)) {
    285        indirection = DIRIND_ANONYMOUS;
    286      } else if (!reachable_addr_allows_rs(rs, FIREWALL_DIR_CONNECTION, 0)) {
    287        if (reachable_addr_allows_rs(rs, FIREWALL_OR_CONNECTION, 0))
    288          indirection = DIRIND_ONEHOP;
    289        else
    290          indirection = DIRIND_ANONYMOUS;
    291      } else {
    292        indirection = DIRIND_DIRECT_CONN;
    293      }
    294 
    295      directory_request_t *req = directory_request_new(dir_purpose);
    296      directory_request_set_routerstatus(req, rs);
    297      directory_request_set_router_purpose(req, router_purpose);
    298      directory_request_set_indirection(req, indirection);
    299      directory_request_set_payload(req, payload, upload_len);
    300      directory_initiate_request(req);
    301      directory_request_free(req);
    302  } SMARTLIST_FOREACH_END(ds);
    303  if (!found) {
    304    char *s = authdir_type_to_string(type);
    305    log_warn(LD_DIR, "Publishing server descriptor to directory authorities "
    306             "of type '%s', but no authorities of that type listed!", s);
    307    tor_free(s);
    308  }
    309 }
    310 
    311 /** Return true iff, according to the values in <b>options</b>, we should be
    312 * using directory guards for direct downloads of directory information. */
    313 STATIC int
    314 should_use_directory_guards(const or_options_t *options)
    315 {
    316  /* Public (non-bridge) servers never use directory guards. */
    317  if (public_server_mode(options))
    318    return 0;
    319  /* If guards are disabled, we can't use directory guards.
    320   */
    321  if (!options->UseEntryGuards)
    322    return 0;
    323  /* If we're configured to fetch directory info aggressively or of a
    324   * nonstandard type, don't use directory guards. */
    325  if (options->DownloadExtraInfo || options->FetchDirInfoEarly ||
    326      options->FetchDirInfoExtraEarly || options->FetchUselessDescriptors)
    327    return 0;
    328  return 1;
    329 }
    330 
    331 /** Pick an unconstrained directory server from among our guards, the latest
    332 * networkstatus, or the fallback dirservers, for use in downloading
    333 * information of type <b>type</b>, and return its routerstatus. */
    334 static const routerstatus_t *
    335 directory_pick_generic_dirserver(dirinfo_type_t type, int pds_flags,
    336                                 uint8_t dir_purpose,
    337                                 circuit_guard_state_t **guard_state_out)
    338 {
    339  const routerstatus_t *rs = NULL;
    340  const or_options_t *options = get_options();
    341 
    342  if (options->UseBridges)
    343    log_warn(LD_BUG, "Called when we have UseBridges set.");
    344 
    345  if (should_use_directory_guards(options)) {
    346    const node_t *node = guards_choose_dirguard(dir_purpose, guard_state_out);
    347    if (node)
    348      rs = node->rs;
    349  } else {
    350    /* anybody with a non-zero dirport will do */
    351    rs = router_pick_directory_server(type, pds_flags);
    352  }
    353  if (!rs) {
    354    log_info(LD_DIR, "No router found for %s; falling back to "
    355             "dirserver list.", dir_conn_purpose_to_string(dir_purpose));
    356    rs = router_pick_fallback_dirserver(type, pds_flags);
    357  }
    358 
    359  return rs;
    360 }
    361 
    362 /**
    363 * Set the extra fields in <b>req</b> that are used when requesting a
    364 * consensus of type <b>resource</b>.
    365 *
    366 * Right now, these fields are if-modified-since and x-or-diff-from-consensus.
    367 */
    368 static void
    369 dir_consensus_request_set_additional_headers(directory_request_t *req,
    370                                             const char *resource)
    371 {
    372  time_t if_modified_since = 0;
    373  uint8_t or_diff_from[DIGEST256_LEN];
    374  int or_diff_from_is_set = 0;
    375 
    376  /* DEFAULT_IF_MODIFIED_SINCE_DELAY is 1/20 of the default consensus
    377   * period of 1 hour.
    378   */
    379  const int DEFAULT_IF_MODIFIED_SINCE_DELAY = 180;
    380  const int32_t DEFAULT_TRY_DIFF_FOR_CONSENSUS_NEWER = 72;
    381  const int32_t MIN_TRY_DIFF_FOR_CONSENSUS_NEWER = 0;
    382  const int32_t MAX_TRY_DIFF_FOR_CONSENSUS_NEWER = 8192;
    383  const char TRY_DIFF_FOR_CONSENSUS_NEWER_NAME[] =
    384    "try-diff-for-consensus-newer-than";
    385 
    386  int flav = FLAV_NS;
    387  if (resource)
    388    flav = networkstatus_parse_flavor_name(resource);
    389 
    390  int32_t max_age_for_diff = 3600 *
    391    networkstatus_get_param(NULL,
    392                            TRY_DIFF_FOR_CONSENSUS_NEWER_NAME,
    393                            DEFAULT_TRY_DIFF_FOR_CONSENSUS_NEWER,
    394                            MIN_TRY_DIFF_FOR_CONSENSUS_NEWER,
    395                            MAX_TRY_DIFF_FOR_CONSENSUS_NEWER);
    396 
    397  if (flav != -1) {
    398    /* IF we have a parsed consensus of this type, we can do an
    399     * if-modified-time based on it. */
    400    networkstatus_t *v;
    401    v = networkstatus_get_latest_consensus_by_flavor(flav);
    402    if (v) {
    403      /* In networks with particularly short V3AuthVotingIntervals,
    404       * ask for the consensus if it's been modified since half the
    405       * V3AuthVotingInterval of the most recent consensus. */
    406      time_t ims_delay = DEFAULT_IF_MODIFIED_SINCE_DELAY;
    407      if (v->fresh_until > v->valid_after
    408          && ims_delay > (v->fresh_until - v->valid_after)/2) {
    409        ims_delay = (v->fresh_until - v->valid_after)/2;
    410      }
    411      if_modified_since = v->valid_after + ims_delay;
    412      if (v->valid_after >= approx_time() - max_age_for_diff) {
    413        memcpy(or_diff_from, v->digest_sha3_as_signed, DIGEST256_LEN);
    414        or_diff_from_is_set = 1;
    415      }
    416    }
    417  } else {
    418    /* Otherwise it might be a consensus we don't parse, but which we
    419     * do cache.  Look at the cached copy, perhaps. */
    420    cached_dir_t *cd = dirserv_get_consensus(resource);
    421    /* We have no method of determining the voting interval from an
    422     * unparsed consensus, so we use the default. */
    423    if (cd) {
    424      if_modified_since = cd->published + DEFAULT_IF_MODIFIED_SINCE_DELAY;
    425      if (cd->published >= approx_time() - max_age_for_diff) {
    426        memcpy(or_diff_from, cd->digest_sha3_as_signed, DIGEST256_LEN);
    427        or_diff_from_is_set = 1;
    428      }
    429    }
    430  }
    431 
    432  if (if_modified_since > 0)
    433    directory_request_set_if_modified_since(req, if_modified_since);
    434  if (or_diff_from_is_set) {
    435    char hex[HEX_DIGEST256_LEN + 1];
    436    base16_encode(hex, sizeof(hex),
    437                  (const char*)or_diff_from, sizeof(or_diff_from));
    438    directory_request_add_header(req, X_OR_DIFF_FROM_CONSENSUS_HEADER, hex);
    439  }
    440 }
    441 /** Start a connection to a random running directory server, using
    442 * connection purpose <b>dir_purpose</b>, intending to fetch descriptors
    443 * of purpose <b>router_purpose</b>, and requesting <b>resource</b>.
    444 * Use <b>pds_flags</b> as arguments to router_pick_directory_server()
    445 * or router_pick_trusteddirserver().
    446 */
    447 MOCK_IMPL(void,
    448 directory_get_from_dirserver,(
    449                            uint8_t dir_purpose,
    450                            uint8_t router_purpose,
    451                            const char *resource,
    452                            int pds_flags,
    453                            download_want_authority_t want_authority))
    454 {
    455  const routerstatus_t *rs = NULL;
    456  const or_options_t *options = get_options();
    457  int prefer_authority = (dirclient_fetches_from_authorities(options)
    458                          || want_authority == DL_WANT_AUTHORITY);
    459  int require_authority = 0;
    460  int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose,
    461                                            resource);
    462  dirinfo_type_t type = dir_fetch_type(dir_purpose, router_purpose, resource);
    463 
    464  if (type == NO_DIRINFO)
    465    return;
    466 
    467  if (!options->FetchServerDescriptors)
    468    return;
    469 
    470  circuit_guard_state_t *guard_state = NULL;
    471  if (!get_via_tor) {
    472    if (options->UseBridges && !(type & BRIDGE_DIRINFO)) {
    473      /* We want to ask a running bridge for which we have a descriptor.
    474       *
    475       * When we ask choose_random_entry() for a bridge, we specify what
    476       * sort of dir fetch we'll be doing, so it won't return a bridge
    477       * that can't answer our question.
    478       */
    479      const node_t *node = guards_choose_dirguard(dir_purpose, &guard_state);
    480      if (node && node->ri) {
    481        /* every bridge has a routerinfo. */
    482        routerinfo_t *ri = node->ri;
    483        /* clients always make OR connections to bridges */
    484        tor_addr_port_t or_ap;
    485        directory_request_t *req = directory_request_new(dir_purpose);
    486        /* we are willing to use a non-preferred address if we need to */
    487        reachable_addr_choose_from_node(node, FIREWALL_OR_CONNECTION, 0,
    488                                             &or_ap);
    489        directory_request_set_or_addr_port(req, &or_ap);
    490        directory_request_set_directory_id_digest(req,
    491                                            ri->cache_info.identity_digest);
    492        directory_request_set_router_purpose(req, router_purpose);
    493        directory_request_set_resource(req, resource);
    494        if (dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS)
    495          dir_consensus_request_set_additional_headers(req, resource);
    496        directory_request_set_guard_state(req, guard_state);
    497        directory_initiate_request(req);
    498        directory_request_free(req);
    499      } else {
    500        if (guard_state) {
    501          entry_guard_cancel(&guard_state);
    502        }
    503        log_notice(LD_DIR, "Ignoring directory request, since no bridge "
    504                           "nodes are available yet.");
    505      }
    506 
    507      return;
    508    } else {
    509      if (prefer_authority || (type & BRIDGE_DIRINFO)) {
    510        /* only ask authdirservers, and don't ask myself */
    511        rs = router_pick_trusteddirserver(type, pds_flags);
    512        if (rs == NULL && (pds_flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
    513                                        PDS_NO_EXISTING_MICRODESC_FETCH))) {
    514          /* We don't want to fetch from any authorities that we're currently
    515           * fetching server descriptors from, and we got no match.  Did we
    516           * get no match because all the authorities have connections
    517           * fetching server descriptors (in which case we should just
    518           * return,) or because all the authorities are down or on fire or
    519           * unreachable or something (in which case we should go on with
    520           * our fallback code)? */
    521          pds_flags &= ~(PDS_NO_EXISTING_SERVERDESC_FETCH|
    522                         PDS_NO_EXISTING_MICRODESC_FETCH);
    523          rs = router_pick_trusteddirserver(type, pds_flags);
    524          if (rs) {
    525            log_debug(LD_DIR, "Deferring serverdesc fetch: all authorities "
    526                      "are in use.");
    527            return;
    528          }
    529        }
    530        if (rs == NULL && require_authority) {
    531          log_info(LD_DIR, "No authorities were available for %s: will try "
    532                   "later.", dir_conn_purpose_to_string(dir_purpose));
    533          return;
    534        }
    535      }
    536      if (!rs && !(type & BRIDGE_DIRINFO)) {
    537        rs = directory_pick_generic_dirserver(type, pds_flags,
    538                                              dir_purpose,
    539                                              &guard_state);
    540        if (!rs)
    541          get_via_tor = 1; /* last resort: try routing it via Tor */
    542      }
    543    }
    544  }
    545 
    546  if (get_via_tor) {
    547    /* Never use fascistfirewall; we're going via Tor. */
    548    pds_flags |= PDS_IGNORE_FASCISTFIREWALL;
    549    rs = router_pick_directory_server(type, pds_flags);
    550  }
    551 
    552  /* If we have any hope of building an indirect conn, we know some router
    553   * descriptors.  If (rs==NULL), we can't build circuits anyway, so
    554   * there's no point in falling back to the authorities in this case. */
    555  if (rs) {
    556    const dir_indirection_t indirection =
    557      get_via_tor ? DIRIND_ANONYMOUS : DIRIND_ONEHOP;
    558    directory_request_t *req = directory_request_new(dir_purpose);
    559    directory_request_set_routerstatus(req, rs);
    560    directory_request_set_router_purpose(req, router_purpose);
    561    directory_request_set_indirection(req, indirection);
    562    directory_request_set_resource(req, resource);
    563    if (dir_purpose == DIR_PURPOSE_FETCH_CONSENSUS)
    564      dir_consensus_request_set_additional_headers(req, resource);
    565    if (guard_state)
    566      directory_request_set_guard_state(req, guard_state);
    567    directory_initiate_request(req);
    568    directory_request_free(req);
    569  } else {
    570    log_notice(LD_DIR,
    571               "While fetching directory info, "
    572               "no running dirservers known. Will try again later. "
    573               "(purpose %d)", dir_purpose);
    574    if (!purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
    575      /* remember we tried them all and failed. */
    576      directory_all_unreachable(time(NULL));
    577    }
    578  }
    579 }
    580 
    581 /** As directory_get_from_dirserver, but initiates a request to <i>every</i>
    582 * directory authority other than ourself.  Only for use by authorities when
    583 * searching for missing information while voting. */
    584 void
    585 directory_get_from_all_authorities(uint8_t dir_purpose,
    586                                   uint8_t router_purpose,
    587                                   const char *resource)
    588 {
    589  tor_assert(dir_purpose == DIR_PURPOSE_FETCH_STATUS_VOTE ||
    590             dir_purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES);
    591 
    592  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
    593                          dir_server_t *, ds) {
    594      if (router_digest_is_me(ds->digest))
    595        continue;
    596      if (!(ds->type & V3_DIRINFO))
    597        continue;
    598      const routerstatus_t *rs = router_get_consensus_status_by_id(ds->digest);
    599      if (!rs) {
    600        /* prefer to use the address in the consensus, but fall back to
    601         * the hard-coded trusted_dir_server address if we don't have a
    602         * consensus or this digest isn't in our consensus. */
    603        rs = &ds->fake_status;
    604      }
    605      directory_request_t *req = directory_request_new(dir_purpose);
    606      directory_request_set_routerstatus(req, rs);
    607      directory_request_set_router_purpose(req, router_purpose);
    608      directory_request_set_resource(req, resource);
    609      directory_initiate_request(req);
    610      directory_request_free(req);
    611  } SMARTLIST_FOREACH_END(ds);
    612 }
    613 
    614 /** Return true iff <b>ind</b> requires a multihop circuit. */
    615 static int
    616 dirind_is_anon(dir_indirection_t ind)
    617 {
    618  return ind == DIRIND_ANON_DIRPORT || ind == DIRIND_ANONYMOUS;
    619 }
    620 
    621 /* Choose reachable OR and Dir addresses and ports from status, copying them
    622 * into use_or_ap and use_dir_ap. If indirection is anonymous, then we're
    623 * connecting via another relay, so choose the primary IPv4 address and ports.
    624 *
    625 * status should have at least one reachable address, if we can't choose a
    626 * reachable address, warn and return -1. Otherwise, return 0.
    627 */
    628 static int
    629 directory_choose_address_routerstatus(const routerstatus_t *status,
    630                                      dir_indirection_t indirection,
    631                                      tor_addr_port_t *use_or_ap,
    632                                      tor_addr_port_t *use_dir_ap)
    633 {
    634  tor_assert(status != NULL);
    635  tor_assert(use_or_ap != NULL);
    636  tor_assert(use_dir_ap != NULL);
    637 
    638  const or_options_t *options = get_options();
    639  int have_or = 0, have_dir = 0;
    640 
    641  /* We expect status to have at least one reachable address if we're
    642   * connecting to it directly.
    643   *
    644   * Therefore, we can simply use the other address if the one we want isn't
    645   * allowed by the firewall.
    646   *
    647   * (When Tor uploads and downloads a hidden service descriptor, it uses
    648   * DIRIND_ANONYMOUS. Even Single Onion Servers (NYI) use DIRIND_ANONYMOUS,
    649   * to avoid HSDirs denying service by rejecting descriptors.)
    650   */
    651 
    652  /* Initialise the OR / Dir addresses */
    653  tor_addr_make_null(&use_or_ap->addr, AF_UNSPEC);
    654  use_or_ap->port = 0;
    655  tor_addr_make_null(&use_dir_ap->addr, AF_UNSPEC);
    656  use_dir_ap->port = 0;
    657 
    658  /* ORPort connections */
    659  if (indirection == DIRIND_ANONYMOUS) {
    660    if (!tor_addr_is_null(&status->ipv4_addr)) {
    661      /* Since we're going to build a 3-hop circuit and ask the 2nd relay
    662       * to extend to this address, always use the primary (IPv4) OR address */
    663      tor_addr_copy(&use_or_ap->addr, &status->ipv4_addr);
    664      use_or_ap->port = status->ipv4_orport;
    665      have_or = 1;
    666    }
    667  } else if (indirection == DIRIND_ONEHOP) {
    668    /* We use an IPv6 address if we have one and we prefer it.
    669     * Use the preferred address and port if they are reachable, otherwise,
    670     * use the alternate address and port (if any).
    671     */
    672    reachable_addr_choose_from_rs(status, FIREWALL_OR_CONNECTION, 0,
    673                                       use_or_ap);
    674    have_or = tor_addr_port_is_valid_ap(use_or_ap, 0);
    675  }
    676 
    677  /* DirPort connections
    678   * DIRIND_ONEHOP uses ORPort, but may fall back to the DirPort on relays */
    679  if (indirection == DIRIND_DIRECT_CONN ||
    680      indirection == DIRIND_ANON_DIRPORT ||
    681      (indirection == DIRIND_ONEHOP
    682       && !dirclient_must_use_begindir(options))) {
    683    reachable_addr_choose_from_rs(status, FIREWALL_DIR_CONNECTION, 0,
    684                                       use_dir_ap);
    685    have_dir = tor_addr_port_is_valid_ap(use_dir_ap, 0);
    686  }
    687 
    688  /* We rejected all addresses in the relay's status. This means we can't
    689   * connect to it. */
    690  if (!have_or && !have_dir) {
    691    char *ipv6_str = tor_addr_to_str_dup(&status->ipv6_addr);
    692    log_info(LD_BUG, "Rejected all OR and Dir addresses from %s when "
    693             "launching an outgoing directory connection to: IPv4 %s OR %d "
    694             "Dir %d IPv6 %s OR %d Dir %d", routerstatus_describe(status),
    695             fmt_addr(&status->ipv4_addr), status->ipv4_orport,
    696             status->ipv4_dirport, ipv6_str, status->ipv6_orport,
    697             status->ipv4_dirport);
    698    tor_free(ipv6_str);
    699    log_backtrace_once(LOG_INFO, LD_BUG, "Addresses came from");
    700    return -1;
    701  }
    702 
    703  return 0;
    704 }
    705 
    706 /** Called when we are unable to complete the client's request to a directory
    707 * server due to a network error: Mark the router as down and try again if
    708 * possible.
    709 */
    710 void
    711 connection_dir_client_request_failed(dir_connection_t *conn)
    712 {
    713  if (conn->guard_state) {
    714    /* We haven't seen a success on this guard state, so consider it to have
    715     * failed. */
    716    entry_guard_failed(&conn->guard_state);
    717  }
    718  if (!entry_list_is_constrained(get_options()))
    719    /* We must not set a directory to non-running for HS purposes else we end
    720     * up flagging nodes from the hashring has unusable. It doesn't have direct
    721     * effect on the HS subsystem because the nodes are selected regardless of
    722     * their status but still, we shouldn't flag them as non running.
    723     *
    724     * One example where this can go bad is if a tor instance gets added a lot
    725     * of ephemeral services and with a network with problem then many nodes in
    726     * the consenus ends up unusable.
    727     *
    728     * Furthermore, a service does close any pending directory connections
    729     * before uploading a descriptor and thus we can end up here in a natural
    730     * way since closing a pending directory connection leads to this code
    731     * path. */
    732    if (!DIR_PURPOSE_IS_HS(TO_CONN(conn)->purpose)) {
    733      router_set_status(conn->identity_digest, 0);
    734    }
    735  if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
    736             conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
    737    log_info(LD_DIR, "Giving up on serverdesc/extrainfo fetch from "
    738             "directory server at %s; retrying",
    739             connection_describe_peer(TO_CONN(conn)));
    740    if (conn->router_purpose == ROUTER_PURPOSE_BRIDGE)
    741      connection_dir_bridge_routerdesc_failed(conn);
    742    connection_dir_download_routerdesc_failed(conn);
    743  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
    744    if (conn->requested_resource)
    745      networkstatus_consensus_download_failed(0, conn->requested_resource);
    746  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
    747    log_info(LD_DIR, "Giving up on certificate fetch from directory server "
    748             "at %s; retrying",
    749             connection_describe_peer(TO_CONN(conn)));
    750    connection_dir_download_cert_failed(conn, 0);
    751  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
    752    log_info(LD_DIR, "Giving up downloading detached signatures from %s",
    753             connection_describe_peer(TO_CONN(conn)));
    754  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
    755    log_info(LD_DIR, "Giving up downloading votes from %s",
    756             connection_describe_peer(TO_CONN(conn)));
    757  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
    758    log_info(LD_DIR, "Giving up on downloading microdescriptors from "
    759             "directory server at %s; will retry",
    760             connection_describe_peer(TO_CONN(conn)));
    761    connection_dir_download_routerdesc_failed(conn);
    762  } else if (conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE ||
    763             conn->base_.purpose == DIR_PURPOSE_UPLOAD_SIGNATURES) {
    764    log_warn(LD_DIR, "Failed to post %s to %s.",
    765             dir_conn_purpose_to_string(conn->base_.purpose),
    766             connection_describe_peer(TO_CONN(conn)));
    767  }
    768 }
    769 
    770 /** Helper: Attempt to fetch directly the descriptors of each bridge
    771 * listed in <b>failed</b>.
    772 */
    773 static void
    774 connection_dir_retry_bridges(smartlist_t *descs)
    775 {
    776  char digest[DIGEST_LEN];
    777  SMARTLIST_FOREACH(descs, const char *, cp,
    778  {
    779    if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
    780      log_warn(LD_BUG, "Malformed fingerprint in list: %s",
    781              escaped(cp));
    782      continue;
    783    }
    784    retry_bridge_descriptor_fetch_directly(digest);
    785  });
    786 }
    787 
    788 /** Called when an attempt to download one or more router descriptors
    789 * or extra-info documents on connection <b>conn</b> failed.
    790 */
    791 static void
    792 connection_dir_download_routerdesc_failed(dir_connection_t *conn)
    793 {
    794  /* No need to increment the failure count for routerdescs, since
    795   * it's not their fault. */
    796 
    797  /* No need to relaunch descriptor downloads here: we already do it
    798   * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
    799  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
    800             conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
    801             conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
    802 
    803  (void) conn;
    804 }
    805 
    806 /** Called when an attempt to download a bridge's routerdesc from
    807 * one of the authorities failed due to a network error. If
    808 * possible attempt to download descriptors from the bridge directly.
    809 */
    810 static void
    811 connection_dir_bridge_routerdesc_failed(dir_connection_t *conn)
    812 {
    813  smartlist_t *which = NULL;
    814 
    815  /* Requests for bridge descriptors are in the form 'fp/', so ignore
    816     anything else. */
    817  if (!conn->requested_resource || strcmpstart(conn->requested_resource,"fp/"))
    818    return;
    819 
    820  which = smartlist_new();
    821  dir_split_resource_into_fingerprints(conn->requested_resource
    822                                        + strlen("fp/"),
    823                                       which, NULL, 0);
    824 
    825  tor_assert(conn->base_.purpose != DIR_PURPOSE_FETCH_EXTRAINFO);
    826  if (smartlist_len(which)) {
    827    connection_dir_retry_bridges(which);
    828    SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
    829  }
    830  smartlist_free(which);
    831 }
    832 
    833 /** Called when an attempt to fetch a certificate fails. */
    834 static void
    835 connection_dir_download_cert_failed(dir_connection_t *conn, int status)
    836 {
    837  const char *fp_pfx = "fp/";
    838  const char *fpsk_pfx = "fp-sk/";
    839  smartlist_t *failed;
    840  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE);
    841 
    842  if (!conn->requested_resource)
    843    return;
    844  failed = smartlist_new();
    845  /*
    846   * We have two cases download by fingerprint (resource starts
    847   * with "fp/") or download by fingerprint/signing key pair
    848   * (resource starts with "fp-sk/").
    849   */
    850  if (!strcmpstart(conn->requested_resource, fp_pfx)) {
    851    /* Download by fingerprint case */
    852    dir_split_resource_into_fingerprints(conn->requested_resource +
    853                                         strlen(fp_pfx),
    854                                         failed, NULL, DSR_HEX);
    855    SMARTLIST_FOREACH_BEGIN(failed, char *, cp) {
    856      /* Null signing key digest indicates download by fp only */
    857      authority_cert_dl_failed(cp, NULL, status);
    858      tor_free(cp);
    859    } SMARTLIST_FOREACH_END(cp);
    860  } else if (!strcmpstart(conn->requested_resource, fpsk_pfx)) {
    861    /* Download by (fp,sk) pairs */
    862    dir_split_resource_into_fingerprint_pairs(conn->requested_resource +
    863                                              strlen(fpsk_pfx), failed);
    864    SMARTLIST_FOREACH_BEGIN(failed, fp_pair_t *, cp) {
    865      authority_cert_dl_failed(cp->first, cp->second, status);
    866      tor_free(cp);
    867    } SMARTLIST_FOREACH_END(cp);
    868  } else {
    869    log_warn(LD_DIR,
    870             "Don't know what to do with failure for cert fetch %s",
    871             conn->requested_resource);
    872  }
    873 
    874  smartlist_free(failed);
    875 
    876  update_certificate_downloads(time(NULL));
    877 }
    878 
    879 /** Evaluate the situation and decide if we should use an encrypted
    880 * "begindir-style" connection for this directory request.
    881 * 0) If there is no DirPort, yes.
    882 * 1) If or_port is 0, or it's a direct conn and or_port is firewalled
    883 *    or we're a dir mirror, no.
    884 * 2) If we prefer to avoid begindir conns, and we're not fetching or
    885 *    publishing a bridge relay descriptor, no.
    886 * 3) Else yes.
    887 * If returning 0, return in *reason why we can't use begindir.
    888 * reason must not be NULL.
    889 */
    890 static int
    891 directory_command_should_use_begindir(const or_options_t *options,
    892                                      const directory_request_t *req,
    893                                      const char **reason)
    894 {
    895  const tor_addr_t *or_addr = &req->or_addr_port.addr;
    896  //const tor_addr_t *dir_addr = &req->dir_addr_port.addr;
    897  const int or_port = req->or_addr_port.port;
    898  const int dir_port = req->dir_addr_port.port;
    899 
    900  const dir_indirection_t indirection = req->indirection;
    901 
    902  tor_assert(reason);
    903  *reason = NULL;
    904 
    905  /* Reasons why we must use begindir */
    906  if (!dir_port) {
    907    *reason = "(using begindir - directory with no DirPort)";
    908    return 1; /* We don't know a DirPort -- must begindir. */
    909  }
    910  /* Reasons why we can't possibly use begindir */
    911  if (!or_port) {
    912    *reason = "directory with unknown ORPort";
    913    return 0; /* We don't know an ORPort -- no chance. */
    914  }
    915  if (indirection == DIRIND_DIRECT_CONN ||
    916      indirection == DIRIND_ANON_DIRPORT) {
    917    *reason = "DirPort connection";
    918    return 0;
    919  }
    920  if (indirection == DIRIND_ONEHOP) {
    921    /* We're firewalled and want a direct OR connection */
    922    if (!reachable_addr_allows_addr(or_addr, or_port,
    923                                              FIREWALL_OR_CONNECTION, 0, 0)) {
    924      *reason = "ORPort not reachable";
    925      return 0;
    926    }
    927  }
    928  /* Reasons why we want to avoid using begindir */
    929  if (indirection == DIRIND_ONEHOP) {
    930    if (!dirclient_must_use_begindir(options)) {
    931      *reason = "in relay mode";
    932      return 0;
    933    }
    934  }
    935  /* DIRIND_ONEHOP on a client, or DIRIND_ANONYMOUS
    936   */
    937  *reason = "(using begindir)";
    938  return 1;
    939 }
    940 
    941 /**
    942 * Create and return a new directory_request_t with purpose
    943 * <b>dir_purpose</b>.
    944 */
    945 directory_request_t *
    946 directory_request_new(uint8_t dir_purpose)
    947 {
    948  tor_assert(dir_purpose >= DIR_PURPOSE_MIN_);
    949  tor_assert(dir_purpose <= DIR_PURPOSE_MAX_);
    950  tor_assert(dir_purpose != DIR_PURPOSE_SERVER);
    951  tor_assert(dir_purpose != DIR_PURPOSE_HAS_FETCHED_HSDESC);
    952 
    953  directory_request_t *result = tor_malloc_zero(sizeof(*result));
    954  tor_addr_make_null(&result->or_addr_port.addr, AF_INET);
    955  result->or_addr_port.port = 0;
    956  tor_addr_make_null(&result->dir_addr_port.addr, AF_INET);
    957  result->dir_addr_port.port = 0;
    958  result->dir_purpose = dir_purpose;
    959  result->router_purpose = ROUTER_PURPOSE_GENERAL;
    960  result->indirection = DIRIND_ONEHOP;
    961  return result;
    962 }
    963 /**
    964 * Release all resources held by <b>req</b>.
    965 */
    966 void
    967 directory_request_free_(directory_request_t *req)
    968 {
    969  if (req == NULL)
    970    return;
    971  config_free_lines(req->additional_headers);
    972  tor_free(req);
    973 }
    974 /**
    975 * Set the address and OR port to use for this directory request.  If there is
    976 * no OR port, we'll have to connect over the dirport.  (If there are both,
    977 * the indirection setting determines which to use.)
    978 */
    979 void
    980 directory_request_set_or_addr_port(directory_request_t *req,
    981                                   const tor_addr_port_t *p)
    982 {
    983  memcpy(&req->or_addr_port, p, sizeof(*p));
    984 }
    985 /**
    986 * Set the address and dirport to use for this directory request.  If there
    987 * is no dirport, we'll have to connect over the OR port.  (If there are both,
    988 * the indirection setting determines which to use.)
    989 */
    990 void
    991 directory_request_set_dir_addr_port(directory_request_t *req,
    992                                    const tor_addr_port_t *p)
    993 {
    994  memcpy(&req->dir_addr_port, p, sizeof(*p));
    995 }
    996 /**
    997 * Set the RSA identity digest of the directory to use for this directory
    998 * request.
    999 */
   1000 void
   1001 directory_request_set_directory_id_digest(directory_request_t *req,
   1002                                          const char *digest)
   1003 {
   1004  memcpy(req->digest, digest, DIGEST_LEN);
   1005 }
   1006 /**
   1007 * Set the router purpose associated with uploaded and downloaded router
   1008 * descriptors and extrainfo documents in this directory request.  The purpose
   1009 * must be one of ROUTER_PURPOSE_GENERAL (the default) or
   1010 * ROUTER_PURPOSE_BRIDGE.
   1011 */
   1012 void
   1013 directory_request_set_router_purpose(directory_request_t *req,
   1014                                     uint8_t router_purpose)
   1015 {
   1016  tor_assert(router_purpose == ROUTER_PURPOSE_GENERAL ||
   1017             router_purpose == ROUTER_PURPOSE_BRIDGE);
   1018  // assert that it actually makes sense to set this purpose, given
   1019  // the dir_purpose.
   1020  req->router_purpose = router_purpose;
   1021 }
   1022 /**
   1023 * Set the indirection to be used for the directory request.  The indirection
   1024 * parameter configures whether to connect to a DirPort or ORPort, and whether
   1025 * to anonymize the connection.  DIRIND_ONEHOP (use ORPort, don't anonymize)
   1026 * is the default.  See dir_indirection_t for more information.
   1027 */
   1028 void
   1029 directory_request_set_indirection(directory_request_t *req,
   1030                                  dir_indirection_t indirection)
   1031 {
   1032  req->indirection = indirection;
   1033 }
   1034 
   1035 /**
   1036 * Set a pointer to the resource to request from a directory.  Different
   1037 * request types use resources to indicate different components of their URL.
   1038 * Note that only an alias to <b>resource</b> is stored, so the
   1039 * <b>resource</b> must outlive the request.
   1040 */
   1041 void
   1042 directory_request_set_resource(directory_request_t *req,
   1043                               const char *resource)
   1044 {
   1045  req->resource = resource;
   1046 }
   1047 /**
   1048 * Set a pointer to the payload to include with this directory request, along
   1049 * with its length.  Note that only an alias to <b>payload</b> is stored, so
   1050 * the <b>payload</b> must outlive the request.
   1051 */
   1052 void
   1053 directory_request_set_payload(directory_request_t *req,
   1054                              const char *payload,
   1055                              size_t payload_len)
   1056 {
   1057  tor_assert(DIR_PURPOSE_IS_UPLOAD(req->dir_purpose));
   1058 
   1059  req->payload = payload;
   1060  req->payload_len = payload_len;
   1061 }
   1062 /**
   1063 * Set an if-modified-since date to send along with the request.  The
   1064 * default is 0 (meaning, send no if-modified-since header).
   1065 */
   1066 void
   1067 directory_request_set_if_modified_since(directory_request_t *req,
   1068                                        time_t if_modified_since)
   1069 {
   1070  req->if_modified_since = if_modified_since;
   1071 }
   1072 
   1073 /** Include a header of name <b>key</b> with content <b>val</b> in the
   1074 * request. Neither may include newlines or other odd characters. Their
   1075 * ordering is not currently guaranteed.
   1076 *
   1077 * Note that, as elsewhere in this module, header keys include a trailing
   1078 * colon and space.
   1079 */
   1080 void
   1081 directory_request_add_header(directory_request_t *req,
   1082                             const char *key,
   1083                             const char *val)
   1084 {
   1085  config_line_prepend(&req->additional_headers, key, val);
   1086 }
   1087 /**
   1088 * Set an object containing HS connection identifier to be associated with
   1089 * this request. Note that only an alias to <b>ident</b> is stored, so the
   1090 * <b>ident</b> object must outlive the request.
   1091 */
   1092 void
   1093 directory_request_upload_set_hs_ident(directory_request_t *req,
   1094                                      const hs_ident_dir_conn_t *ident)
   1095 {
   1096  if (ident) {
   1097    tor_assert(req->dir_purpose == DIR_PURPOSE_UPLOAD_HSDESC);
   1098  }
   1099  req->hs_ident = ident;
   1100 }
   1101 /**
   1102 * Set an object containing HS connection identifier to be associated with
   1103 * this fetch request. Note that only an alias to <b>ident</b> is stored, so
   1104 * the <b>ident</b> object must outlive the request.
   1105 */
   1106 void
   1107 directory_request_fetch_set_hs_ident(directory_request_t *req,
   1108                                     const hs_ident_dir_conn_t *ident)
   1109 {
   1110  if (ident) {
   1111    tor_assert(req->dir_purpose == DIR_PURPOSE_FETCH_HSDESC);
   1112  }
   1113  req->hs_ident = ident;
   1114 }
   1115 /** Set a static circuit_guard_state_t object to affliate with the request in
   1116 * <b>req</b>.  This object will receive notification when the attempt to
   1117 * connect to the guard either succeeds or fails. */
   1118 void
   1119 directory_request_set_guard_state(directory_request_t *req,
   1120                                  circuit_guard_state_t *state)
   1121 {
   1122  req->guard_state = state;
   1123 }
   1124 
   1125 /**
   1126 * Internal: Return true if any information for contacting the directory in
   1127 * <b>req</b> has been set, other than by the routerstatus. */
   1128 static int
   1129 directory_request_dir_contact_info_specified(const directory_request_t *req)
   1130 {
   1131  /* We only check for ports here, since we don't use an addr unless the port
   1132   * is set */
   1133  return (req->or_addr_port.port ||
   1134          req->dir_addr_port.port ||
   1135          ! tor_digest_is_zero(req->digest));
   1136 }
   1137 
   1138 /**
   1139 * Set the routerstatus to use for the directory associated with this
   1140 * request.  If this option is set, then no other function to set the
   1141 * directory's address or identity should be called.
   1142 */
   1143 void
   1144 directory_request_set_routerstatus(directory_request_t *req,
   1145                                   const routerstatus_t *status)
   1146 {
   1147  req->routerstatus = status;
   1148 }
   1149 
   1150 /**
   1151 * Helper: update the addresses, ports, and identities in <b>req</b>
   1152 * from the routerstatus object in <b>req</b>.  Return 0 on success.
   1153 * On failure, warn and return -1.
   1154 */
   1155 static int
   1156 directory_request_set_dir_from_routerstatus(directory_request_t *req)
   1157 
   1158 {
   1159  const routerstatus_t *status = req->routerstatus;
   1160  if (BUG(status == NULL))
   1161    return -1;
   1162  const or_options_t *options = get_options();
   1163  const node_t *node;
   1164  tor_addr_port_t use_or_ap, use_dir_ap;
   1165  const int anonymized_connection = dirind_is_anon(req->indirection);
   1166 
   1167  tor_assert(status != NULL);
   1168 
   1169  node = node_get_by_id(status->identity_digest);
   1170 
   1171  /* XXX The below check is wrong: !node means it's not in the consensus,
   1172   * but we haven't checked if we have a descriptor for it -- and also,
   1173   * we only care about the descriptor if it's a begindir-style anonymized
   1174   * connection. */
   1175  if (!node && anonymized_connection) {
   1176    log_info(LD_DIR, "Not sending anonymized request to directory '%s'; we "
   1177             "don't have its router descriptor.",
   1178             routerstatus_describe(status));
   1179    return -1;
   1180  }
   1181 
   1182  if (options->ExcludeNodes && options->StrictNodes &&
   1183      routerset_contains_routerstatus(options->ExcludeNodes, status, -1)) {
   1184    log_warn(LD_DIR, "Wanted to contact directory mirror %s for %s, but "
   1185             "it's in our ExcludedNodes list and StrictNodes is set. "
   1186             "Skipping. This choice might make your Tor not work.",
   1187             routerstatus_describe(status),
   1188             dir_conn_purpose_to_string(req->dir_purpose));
   1189    return -1;
   1190  }
   1191 
   1192  /* At this point, if we are a client making a direct connection to a
   1193   * directory server, we have selected a server that has at least one address
   1194   * allowed by ClientUseIPv4/6 and Reachable{"",OR,Dir}Addresses. This
   1195   * selection uses the preference in ClientPreferIPv6{OR,Dir}Port, if
   1196   * possible. (If UseBridges is set, clients always use IPv6, and prefer it
   1197   * by default.)
   1198   *
   1199   * Now choose an address that we can use to connect to the directory server.
   1200   */
   1201  if (directory_choose_address_routerstatus(status,
   1202                                            req->indirection, &use_or_ap,
   1203                                            &use_dir_ap) < 0) {
   1204    return -1;
   1205  }
   1206 
   1207  /* One last thing: If we're talking to an authority, we might want to use
   1208   * a special HTTP port for it based on our purpose.
   1209   */
   1210  if (req->indirection == DIRIND_DIRECT_CONN && status->is_authority) {
   1211    const dir_server_t *ds = router_get_trusteddirserver_by_digest(
   1212                                            status->identity_digest);
   1213    if (ds) {
   1214      const tor_addr_port_t *v4 = NULL;
   1215      if (authdir_mode_v3(get_options())) {
   1216        // An authority connecting to another authority should always
   1217        // prefer the VOTING usage, if one is specifically configured.
   1218        v4 = trusted_dir_server_get_dirport_exact(
   1219                                    ds, AUTH_USAGE_VOTING, AF_INET);
   1220      }
   1221      if (! v4) {
   1222        // Everybody else should prefer a usage dependent on their
   1223        // the dir_purpose.
   1224        auth_dirport_usage_t usage =
   1225          auth_dirport_usage_for_purpose(req->dir_purpose);
   1226        v4 = trusted_dir_server_get_dirport(ds, usage, AF_INET);
   1227      }
   1228      tor_assert_nonfatal(v4);
   1229      if (v4) {
   1230        // XXXX We could, if we wanted, also select a v6 address.  But a v4
   1231        // address must exist here, and we as a relay are required to support
   1232        // ipv4.  So we just that.
   1233        tor_addr_port_copy(&use_dir_ap, v4);
   1234      }
   1235    }
   1236  }
   1237 
   1238  directory_request_set_or_addr_port(req, &use_or_ap);
   1239  directory_request_set_dir_addr_port(req, &use_dir_ap);
   1240  directory_request_set_directory_id_digest(req, status->identity_digest);
   1241  return 0;
   1242 }
   1243 
   1244 /**
   1245 * Launch the provided directory request, configured in <b>request</b>.
   1246 * After this function is called, you can free <b>request</b>.
   1247 */
   1248 MOCK_IMPL(void,
   1249 directory_initiate_request,(directory_request_t *request))
   1250 {
   1251  tor_assert(request);
   1252  if (request->routerstatus) {
   1253    tor_assert_nonfatal(
   1254               ! directory_request_dir_contact_info_specified(request));
   1255    if (directory_request_set_dir_from_routerstatus(request) < 0) {
   1256      return; // or here XXXX
   1257    }
   1258  }
   1259 
   1260  const tor_addr_port_t *or_addr_port = &request->or_addr_port;
   1261  const tor_addr_port_t *dir_addr_port = &request->dir_addr_port;
   1262  const char *digest = request->digest;
   1263  const uint8_t dir_purpose = request->dir_purpose;
   1264  const uint8_t router_purpose = request->router_purpose;
   1265  const dir_indirection_t indirection = request->indirection;
   1266  const char *resource = request->resource;
   1267  const hs_ident_dir_conn_t *hs_ident = request->hs_ident;
   1268  circuit_guard_state_t *guard_state = request->guard_state;
   1269 
   1270  tor_assert(or_addr_port->port || dir_addr_port->port);
   1271  tor_assert(digest);
   1272 
   1273  dir_connection_t *conn;
   1274  const or_options_t *options = get_options();
   1275  int socket_error = 0;
   1276  const char *begindir_reason = NULL;
   1277  /* Should the connection be to a relay's OR port (and inside that we will
   1278   * send our directory request)? */
   1279  const int use_begindir =
   1280    directory_command_should_use_begindir(options, request, &begindir_reason);
   1281 
   1282  /* Will the connection go via a three-hop Tor circuit? Note that this
   1283   * is separate from whether it will use_begindir. */
   1284  const int anonymized_connection = dirind_is_anon(indirection);
   1285 
   1286  /* What is the address we want to make the directory request to? If
   1287   * we're making a begindir request this is the ORPort of the relay
   1288   * we're contacting; if not a begindir request, this is its DirPort.
   1289   * Note that if anonymized_connection is true, we won't be initiating
   1290   * a connection directly to this address. */
   1291  tor_addr_t addr;
   1292  tor_addr_copy(&addr, &(use_begindir ? or_addr_port : dir_addr_port)->addr);
   1293  uint16_t port = (use_begindir ? or_addr_port : dir_addr_port)->port;
   1294 
   1295  log_debug(LD_DIR, "anonymized %d, use_begindir %d.",
   1296            anonymized_connection, use_begindir);
   1297 
   1298  log_debug(LD_DIR, "Initiating %s", dir_conn_purpose_to_string(dir_purpose));
   1299 
   1300  if (purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
   1301    tor_assert(anonymized_connection ||
   1302               hs_service_non_anonymous_mode_enabled(options));
   1303  }
   1304 
   1305  /* use encrypted begindir connections for everything except relays
   1306   * this provides better protection for directory fetches */
   1307  if (!use_begindir && dirclient_must_use_begindir(options)) {
   1308    log_warn(LD_BUG, "Client could not use begindir connection: %s",
   1309             begindir_reason ? begindir_reason : "(NULL)");
   1310    return;
   1311  }
   1312 
   1313  /* ensure that we don't make direct connections when a SOCKS server is
   1314   * configured. */
   1315  if (!anonymized_connection && !use_begindir && !options->HTTPProxy &&
   1316      (options->Socks4Proxy || options->Socks5Proxy)) {
   1317    log_warn(LD_DIR, "Cannot connect to a directory server through a "
   1318                     "SOCKS proxy!");
   1319    return;
   1320  }
   1321 
   1322  /* Make sure that the destination addr and port we picked is viable. */
   1323  if (!port || tor_addr_is_null(&addr)) {
   1324    log_warn(LD_DIR,
   1325             "Cannot make an outgoing %sconnection without a remote %sPort.",
   1326             use_begindir ? "begindir " : "",
   1327             use_begindir ? "OR" : "Dir");
   1328    log_backtrace_once(LOG_INFO, LD_BUG, "Address came from");
   1329    return;
   1330  }
   1331 
   1332  conn = dir_connection_new(tor_addr_family(&addr));
   1333 
   1334  /* set up conn so it's got all the data we need to remember */
   1335  tor_addr_copy(&conn->base_.addr, &addr);
   1336  conn->base_.port = port;
   1337  conn->base_.address = tor_addr_to_str_dup(&addr);
   1338  memcpy(conn->identity_digest, digest, DIGEST_LEN);
   1339 
   1340  conn->base_.purpose = dir_purpose;
   1341  conn->router_purpose = router_purpose;
   1342 
   1343  /* give it an initial state */
   1344  conn->base_.state = DIR_CONN_STATE_CONNECTING;
   1345 
   1346  /* decide whether we can learn our IP address from this conn */
   1347  /* XXXX This is a bad name for this field now. */
   1348  conn->dirconn_direct = !anonymized_connection;
   1349 
   1350  if (hs_ident) {
   1351    conn->hs_ident = hs_ident_dir_conn_dup(hs_ident);
   1352  }
   1353 
   1354  if (!anonymized_connection && !use_begindir) {
   1355    /* then we want to connect to dirport directly */
   1356 
   1357    if (options->HTTPProxy) {
   1358      tor_addr_copy(&addr, &options->HTTPProxyAddr);
   1359      port = options->HTTPProxyPort;
   1360    }
   1361 
   1362    // In this case we should not have picked a directory guard.
   1363    if (BUG(guard_state)) {
   1364      entry_guard_cancel(&guard_state);
   1365    }
   1366 
   1367    // XXXX This is the case where we replace.
   1368 
   1369    switch (connection_connect(TO_CONN(conn), conn->base_.address, &addr,
   1370                               port, &socket_error)) {
   1371      case -1:
   1372        connection_mark_for_close(TO_CONN(conn));
   1373        return;
   1374      case 1:
   1375        /* start flushing conn */
   1376        conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
   1377        FALLTHROUGH;
   1378      case 0:
   1379        /* queue the command on the outbuf */
   1380        directory_send_command(conn, 1, request);
   1381        connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT);
   1382        /* writable indicates finish, readable indicates broken link,
   1383           error indicates broken link in windowsland. */
   1384    }
   1385  } else {
   1386    /* We will use a Tor circuit (maybe 1-hop, maybe 3-hop, maybe with
   1387     * begindir, maybe not with begindir) */
   1388 
   1389    entry_connection_t *linked_conn;
   1390 
   1391    /* Anonymized tunneled connections can never share a circuit.
   1392     * One-hop directory connections can share circuits with each other
   1393     * but nothing else. */
   1394    int iso_flags = anonymized_connection ? ISO_STREAM : ISO_SESSIONGRP;
   1395 
   1396    /* If it's an anonymized connection, remember the fact that we
   1397     * wanted it for later: maybe we'll want it again soon. */
   1398    if (anonymized_connection && use_begindir)
   1399      rep_hist_note_used_internal(time(NULL), 0, 1);
   1400    else if (anonymized_connection && !use_begindir)
   1401      rep_hist_note_used_port(time(NULL), conn->base_.port);
   1402 
   1403    // In this case we should not have a directory guard; we'll
   1404    // get a regular guard later when we build the circuit.
   1405    if (BUG(anonymized_connection && guard_state)) {
   1406      entry_guard_cancel(&guard_state);
   1407    }
   1408 
   1409    conn->guard_state = guard_state;
   1410 
   1411    /* make an AP connection
   1412     * populate it and add it at the right state
   1413     * hook up both sides
   1414     */
   1415    linked_conn =
   1416      connection_ap_make_link(TO_CONN(conn),
   1417                              conn->base_.address, conn->base_.port,
   1418                              digest,
   1419                              SESSION_GROUP_DIRCONN, iso_flags,
   1420                              use_begindir, !anonymized_connection);
   1421    if (!linked_conn) {
   1422      log_warn(LD_NET,"Making tunnel to dirserver failed.");
   1423      connection_mark_for_close(TO_CONN(conn));
   1424      return;
   1425    }
   1426 
   1427    if (connection_add(TO_CONN(conn)) < 0) {
   1428      log_warn(LD_NET,"Unable to add connection for link to dirserver.");
   1429      connection_mark_for_close(TO_CONN(conn));
   1430      return;
   1431    }
   1432    conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
   1433    /* queue the command on the outbuf */
   1434    directory_send_command(conn, 0, request);
   1435 
   1436    connection_watch_events(TO_CONN(conn), READ_EVENT|WRITE_EVENT);
   1437    connection_start_reading(ENTRY_TO_CONN(linked_conn));
   1438  }
   1439 }
   1440 
   1441 /** Helper for sorting
   1442 *
   1443 * sort strings alphabetically
   1444 *
   1445 * XXXX we have a smartlist_sort_strings() function, right?
   1446 */
   1447 static int
   1448 compare_strs_(const void **a, const void **b)
   1449 {
   1450  const char *s1 = *a, *s2 = *b;
   1451  return strcmp(s1, s2);
   1452 }
   1453 
   1454 #define CONDITIONAL_CONSENSUS_FPR_LEN 3
   1455 CTASSERT(CONDITIONAL_CONSENSUS_FPR_LEN <= DIGEST_LEN);
   1456 
   1457 /** Return the URL we should use for a consensus download.
   1458 *
   1459 * Use the "conditional consensus downloading" feature described in
   1460 * dir-spec.txt, i.e.
   1461 * GET .../consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>
   1462 *
   1463 * If 'resource' is provided, it is the name of a consensus flavor to request.
   1464 */
   1465 static char *
   1466 directory_get_consensus_url(const char *resource)
   1467 {
   1468  char *url = NULL;
   1469  const char *hyphen, *flavor;
   1470  if (resource==NULL || strcmp(resource, "ns")==0) {
   1471    flavor = ""; /* Request ns consensuses as "", so older servers will work*/
   1472    hyphen = "";
   1473  } else {
   1474    flavor = resource;
   1475    hyphen = "-";
   1476  }
   1477 
   1478  {
   1479    char *authority_id_list;
   1480    smartlist_t *authority_digests = smartlist_new();
   1481 
   1482    SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
   1483                            dir_server_t *, ds) {
   1484        char *hex;
   1485        if (!(ds->type & V3_DIRINFO))
   1486          continue;
   1487 
   1488        hex = tor_malloc(2*CONDITIONAL_CONSENSUS_FPR_LEN+1);
   1489        base16_encode(hex, 2*CONDITIONAL_CONSENSUS_FPR_LEN+1,
   1490                      ds->v3_identity_digest, CONDITIONAL_CONSENSUS_FPR_LEN);
   1491        smartlist_add(authority_digests, hex);
   1492    } SMARTLIST_FOREACH_END(ds);
   1493    smartlist_sort(authority_digests, compare_strs_);
   1494    authority_id_list = smartlist_join_strings(authority_digests,
   1495                                               "+", 0, NULL);
   1496 
   1497    tor_asprintf(&url, "/tor/status-vote/current/consensus%s%s/%s.z",
   1498                 hyphen, flavor, authority_id_list);
   1499 
   1500    SMARTLIST_FOREACH(authority_digests, char *, cp, tor_free(cp));
   1501    smartlist_free(authority_digests);
   1502    tor_free(authority_id_list);
   1503  }
   1504  return url;
   1505 }
   1506 
   1507 /**
   1508 * Copies the ipv6 from source to destination, subject to buffer size limit
   1509 * size. If decorate is true, makes sure the copied address is decorated.
   1510 */
   1511 static void
   1512 copy_ipv6_address(char* destination, const char* source, size_t len,
   1513                  int decorate) {
   1514  tor_assert(destination);
   1515  tor_assert(source);
   1516 
   1517  if (decorate && source[0] != '[') {
   1518    tor_snprintf(destination, len, "[%s]", source);
   1519  } else {
   1520    strlcpy(destination, source, len);
   1521  }
   1522 }
   1523 
   1524 /** Queue an appropriate HTTP command for <b>request</b> on
   1525 * <b>conn</b>-\>outbuf.  If <b>direct</b> is true, we're making a
   1526 * non-anonymized connection to the dirport.
   1527 */
   1528 static void
   1529 directory_send_command(dir_connection_t *conn,
   1530                       const int direct,
   1531                       const directory_request_t *req)
   1532 {
   1533  tor_assert(req);
   1534  const int purpose = req->dir_purpose;
   1535  const char *resource = req->resource;
   1536  const char *payload = req->payload;
   1537  const size_t payload_len = req->payload_len;
   1538  const time_t if_modified_since = req->if_modified_since;
   1539  const int anonymized_connection = dirind_is_anon(req->indirection);
   1540 
   1541  char proxystring[256];
   1542  char hoststring[128];
   1543  /* NEEDS to be the same size hoststring.
   1544   Will be decorated with brackets around it if it is ipv6. */
   1545  char decorated_address[128];
   1546  smartlist_t *headers = smartlist_new();
   1547  char *url;
   1548  char *accept_encoding;
   1549  size_t url_len;
   1550  char request[8192];
   1551  size_t request_len, total_request_len = 0;
   1552  const char *httpcommand = NULL;
   1553 
   1554  tor_assert(conn);
   1555  tor_assert(conn->base_.type == CONN_TYPE_DIR);
   1556 
   1557  tor_free(conn->requested_resource);
   1558  if (resource)
   1559    conn->requested_resource = tor_strdup(resource);
   1560 
   1561  /* decorate the ip address if it is ipv6 */
   1562  if (strchr(conn->base_.address, ':')) {
   1563    copy_ipv6_address(decorated_address, conn->base_.address,
   1564                      sizeof(decorated_address), 1);
   1565  } else {
   1566    strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
   1567  }
   1568 
   1569  /* come up with a string for which Host: we want */
   1570  if (conn->base_.port == 80) {
   1571    strlcpy(hoststring, decorated_address, sizeof(hoststring));
   1572  } else {
   1573    tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
   1574                 decorated_address, conn->base_.port);
   1575  }
   1576 
   1577  /* Format if-modified-since */
   1578  if (if_modified_since) {
   1579    char b[RFC1123_TIME_LEN+1];
   1580    format_rfc1123_time(b, if_modified_since);
   1581    smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
   1582  }
   1583 
   1584  /* come up with some proxy lines, if we're using one. */
   1585  if (direct && get_options()->HTTPProxy) {
   1586    char *base64_authenticator=NULL;
   1587    const char *authenticator = get_options()->HTTPProxyAuthenticator;
   1588 
   1589    tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
   1590    if (authenticator) {
   1591      base64_authenticator = alloc_http_authenticator(authenticator);
   1592      if (!base64_authenticator)
   1593        log_warn(LD_BUG, "Encoding http authenticator failed");
   1594    }
   1595    if (base64_authenticator) {
   1596      smartlist_add_asprintf(headers,
   1597                   "Proxy-Authorization: Basic %s\r\n",
   1598                   base64_authenticator);
   1599      tor_free(base64_authenticator);
   1600    }
   1601  } else {
   1602    proxystring[0] = 0;
   1603  }
   1604 
   1605  if (! anonymized_connection) {
   1606    /* Add Accept-Encoding. */
   1607    accept_encoding = accept_encoding_header();
   1608    smartlist_add_asprintf(headers, "Accept-Encoding: %s\r\n",
   1609                           accept_encoding);
   1610    tor_free(accept_encoding);
   1611  }
   1612 
   1613  /* Add additional headers, if any */
   1614  {
   1615    config_line_t *h;
   1616    for (h = req->additional_headers; h; h = h->next) {
   1617      smartlist_add_asprintf(headers, "%s%s\r\n", h->key, h->value);
   1618    }
   1619  }
   1620 
   1621  switch (purpose) {
   1622    case DIR_PURPOSE_FETCH_CONSENSUS:
   1623      /* resource is optional.  If present, it's a flavor name */
   1624      tor_assert(!payload);
   1625      httpcommand = "GET";
   1626      url = directory_get_consensus_url(resource);
   1627      log_info(LD_DIR, "Downloading consensus from %s using %s",
   1628               hoststring, url);
   1629      break;
   1630    case DIR_PURPOSE_FETCH_CERTIFICATE:
   1631      tor_assert(resource);
   1632      tor_assert(!payload);
   1633      httpcommand = "GET";
   1634      tor_asprintf(&url, "/tor/keys/%s", resource);
   1635      break;
   1636    case DIR_PURPOSE_FETCH_STATUS_VOTE:
   1637      tor_assert(resource);
   1638      tor_assert(!payload);
   1639      httpcommand = "GET";
   1640      tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
   1641      break;
   1642    case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
   1643      tor_assert(!resource);
   1644      tor_assert(!payload);
   1645      httpcommand = "GET";
   1646      url = tor_strdup("/tor/status-vote/next/consensus-signatures.z");
   1647      break;
   1648    case DIR_PURPOSE_FETCH_SERVERDESC:
   1649      tor_assert(resource);
   1650      httpcommand = "GET";
   1651      tor_asprintf(&url, "/tor/server/%s", resource);
   1652      break;
   1653    case DIR_PURPOSE_FETCH_EXTRAINFO:
   1654      tor_assert(resource);
   1655      httpcommand = "GET";
   1656      tor_asprintf(&url, "/tor/extra/%s", resource);
   1657      break;
   1658    case DIR_PURPOSE_FETCH_MICRODESC:
   1659      tor_assert(resource);
   1660      httpcommand = "GET";
   1661      tor_asprintf(&url, "/tor/micro/%s", resource);
   1662      break;
   1663    case DIR_PURPOSE_UPLOAD_DIR: {
   1664      const char *why = router_get_descriptor_gen_reason();
   1665      tor_assert(!resource);
   1666      tor_assert(payload);
   1667      httpcommand = "POST";
   1668      url = tor_strdup("/tor/");
   1669      if (!why) {
   1670        why = "for no reason at all";
   1671      }
   1672      smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
   1673      break;
   1674    }
   1675    case DIR_PURPOSE_UPLOAD_VOTE:
   1676      tor_assert(!resource);
   1677      tor_assert(payload);
   1678      httpcommand = "POST";
   1679      url = tor_strdup("/tor/post/vote");
   1680      break;
   1681    case DIR_PURPOSE_UPLOAD_SIGNATURES:
   1682      tor_assert(!resource);
   1683      tor_assert(payload);
   1684      httpcommand = "POST";
   1685      url = tor_strdup("/tor/post/consensus-signature");
   1686      break;
   1687    case DIR_PURPOSE_FETCH_HSDESC:
   1688      tor_assert(resource);
   1689      tor_assert(strlen(resource) <= ED25519_BASE64_LEN);
   1690      tor_assert(!payload);
   1691      httpcommand = "GET";
   1692      tor_asprintf(&url, "/tor/hs/3/%s", resource);
   1693      break;
   1694    case DIR_PURPOSE_UPLOAD_HSDESC:
   1695      tor_assert(resource);
   1696      tor_assert(payload);
   1697      httpcommand = "POST";
   1698      tor_asprintf(&url, "/tor/hs/%s/publish", resource);
   1699      break;
   1700    default:
   1701      tor_assert(0);
   1702      return;
   1703  }
   1704 
   1705  /* warn in the non-tunneled case */
   1706  if (direct && (strlen(proxystring) + strlen(url) >= 4096)) {
   1707    log_warn(LD_BUG,
   1708             "Squid does not like URLs longer than 4095 bytes, and this "
   1709             "one is %d bytes long: %s%s",
   1710             (int)(strlen(proxystring) + strlen(url)), proxystring, url);
   1711  }
   1712 
   1713  tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
   1714 
   1715  request_len = strlen(request);
   1716  total_request_len += request_len;
   1717  connection_buf_add(request, request_len, TO_CONN(conn));
   1718 
   1719  url_len = strlen(url);
   1720  total_request_len += url_len;
   1721  connection_buf_add(url, url_len, TO_CONN(conn));
   1722  tor_free(url);
   1723 
   1724  if (!strcmp(httpcommand, "POST") || payload) {
   1725    smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
   1726                 payload ? (unsigned long)payload_len : 0);
   1727  }
   1728 
   1729  {
   1730    char *header = smartlist_join_strings(headers, "", 0, NULL);
   1731    tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
   1732                 hoststring, header);
   1733    tor_free(header);
   1734  }
   1735 
   1736  request_len = strlen(request);
   1737  total_request_len += request_len;
   1738  connection_buf_add(request, request_len, TO_CONN(conn));
   1739 
   1740  if (payload) {
   1741    /* then send the payload afterwards too */
   1742    connection_buf_add(payload, payload_len, TO_CONN(conn));
   1743    total_request_len += payload_len;
   1744  }
   1745 
   1746  SMARTLIST_FOREACH(headers, char *, h, tor_free(h));
   1747  smartlist_free(headers);
   1748 
   1749  log_debug(LD_DIR,
   1750            "Sent request to directory server %s "
   1751            "(purpose: %d, request size: %"TOR_PRIuSZ", "
   1752            "payload size: %"TOR_PRIuSZ")",
   1753            connection_describe_peer(TO_CONN(conn)),
   1754            conn->base_.purpose,
   1755            (total_request_len),
   1756            (payload ? payload_len : 0));
   1757 }
   1758 
   1759 /** Return true iff <b>body</b> doesn't start with a plausible router or
   1760 * network-status or microdescriptor opening.  This is a sign of possible
   1761 * compression. */
   1762 static int
   1763 body_is_plausible(const char *body, size_t len, int purpose)
   1764 {
   1765  int i;
   1766  if (len == 0)
   1767    return 1; /* empty bodies don't need decompression */
   1768  if (len < 32)
   1769    return 0;
   1770  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
   1771    return (!strcmpstart(body,"onion-key"));
   1772  }
   1773 
   1774  if (!strcmpstart(body,"router") ||
   1775      !strcmpstart(body,"network-status"))
   1776    return 1;
   1777  for (i=0;i<32;++i) {
   1778    if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
   1779      return 0;
   1780  }
   1781 
   1782  return 1;
   1783 }
   1784 
   1785 /** Called when we've just fetched a bunch of router descriptors in
   1786 * <b>body</b>.  The list <b>which</b>, if present, holds digests for
   1787 * descriptors we requested: descriptor digests if <b>descriptor_digests</b>
   1788 * is true, or identity digests otherwise.  Parse the descriptors, validate
   1789 * them, and annotate them as having purpose <b>purpose</b> and as having been
   1790 * downloaded from <b>source</b>.
   1791 *
   1792 * Return the number of routers actually added. */
   1793 static int
   1794 load_downloaded_routers(const char *body, smartlist_t *which,
   1795                        int descriptor_digests,
   1796                        int router_purpose,
   1797                        const char *source)
   1798 {
   1799  char buf[256];
   1800  char time_buf[ISO_TIME_LEN+1];
   1801  int added = 0;
   1802  int general = router_purpose == ROUTER_PURPOSE_GENERAL;
   1803  format_iso_time(time_buf, time(NULL));
   1804  tor_assert(source);
   1805 
   1806  if (tor_snprintf(buf, sizeof(buf),
   1807                   "@downloaded-at %s\n"
   1808                   "@source %s\n"
   1809                   "%s%s%s", time_buf, escaped(source),
   1810                   !general ? "@purpose " : "",
   1811                   !general ? router_purpose_to_string(router_purpose) : "",
   1812                   !general ? "\n" : "")<0)
   1813    return added;
   1814 
   1815  added = router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
   1816                                  descriptor_digests, buf);
   1817  if (added && general)
   1818    control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
   1819                           count_loading_descriptors_progress());
   1820  return added;
   1821 }
   1822 
   1823 static int handle_response_fetch_certificate(dir_connection_t *,
   1824                                             const response_handler_args_t *);
   1825 static int handle_response_fetch_status_vote(dir_connection_t *,
   1826                                             const response_handler_args_t *);
   1827 static int handle_response_fetch_detached_signatures(dir_connection_t *,
   1828                                             const response_handler_args_t *);
   1829 static int handle_response_fetch_desc(dir_connection_t *,
   1830                                             const response_handler_args_t *);
   1831 static int handle_response_upload_dir(dir_connection_t *,
   1832                                      const response_handler_args_t *);
   1833 static int handle_response_upload_vote(dir_connection_t *,
   1834                                       const response_handler_args_t *);
   1835 static int handle_response_upload_signatures(dir_connection_t *,
   1836                                             const response_handler_args_t *);
   1837 static int handle_response_upload_hsdesc(dir_connection_t *,
   1838                                         const response_handler_args_t *);
   1839 
   1840 static int
   1841 dir_client_decompress_response_body(char **bodyp, size_t *bodylenp,
   1842                                    dir_connection_t *conn,
   1843                                    compress_method_t compression,
   1844                                    int anonymized_connection)
   1845 {
   1846  int rv = 0;
   1847  const char *body = *bodyp;
   1848  size_t body_len = *bodylenp;
   1849  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
   1850                       conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
   1851                       conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
   1852 
   1853  int plausible = body_is_plausible(body, body_len, conn->base_.purpose);
   1854 
   1855  if (plausible && compression == NO_METHOD) {
   1856    return 0;
   1857  }
   1858 
   1859  int severity = LOG_DEBUG;
   1860  char *new_body = NULL;
   1861  size_t new_len = 0;
   1862  const char *description1, *description2;
   1863  int want_to_try_both = 0;
   1864  int tried_both = 0;
   1865  compress_method_t guessed = detect_compression_method(body, body_len);
   1866 
   1867  description1 = compression_method_get_human_name(compression);
   1868 
   1869  if (BUG(description1 == NULL))
   1870    description1 = compression_method_get_human_name(UNKNOWN_METHOD);
   1871 
   1872  if (guessed == UNKNOWN_METHOD && !plausible)
   1873    description2 = "confusing binary junk";
   1874  else
   1875    description2 = compression_method_get_human_name(guessed);
   1876 
   1877  /* Tell the user if we don't believe what we're told about compression.*/
   1878  want_to_try_both = (compression == UNKNOWN_METHOD ||
   1879                      guessed != compression);
   1880  if (want_to_try_both) {
   1881    severity = LOG_PROTOCOL_WARN;
   1882  }
   1883 
   1884  tor_log(severity, LD_HTTP,
   1885          "HTTP body from %s was labeled as %s, "
   1886          "%s it seems to be %s.%s",
   1887          connection_describe(TO_CONN(conn)),
   1888          description1,
   1889          guessed != compression?"but":"and",
   1890          description2,
   1891          (compression>0 && guessed>0 && want_to_try_both)?
   1892          "  Trying both.":"");
   1893 
   1894  /* Try declared compression first if we can.
   1895   * tor_compress_supports_method() also returns true for NO_METHOD.
   1896   * Ensure that the server is not sending us data compressed using a
   1897   * compression method that is not allowed for anonymous connections. */
   1898  if (anonymized_connection &&
   1899      ! allowed_anonymous_connection_compression_method(compression)) {
   1900    warn_disallowed_anonymous_compression_method(compression);
   1901    rv = -1;
   1902    goto done;
   1903  }
   1904 
   1905  if (tor_compress_supports_method(compression)) {
   1906    tor_uncompress(&new_body, &new_len, body, body_len, compression,
   1907                   !allow_partial, LOG_PROTOCOL_WARN);
   1908    if (new_body) {
   1909      /* We succeeded with the declared compression method. Great! */
   1910      rv = 0;
   1911      goto done;
   1912    }
   1913  }
   1914 
   1915  /* Okay, if that didn't work, and we think that it was compressed
   1916   * differently, try that. */
   1917  if (anonymized_connection &&
   1918      ! allowed_anonymous_connection_compression_method(guessed)) {
   1919    warn_disallowed_anonymous_compression_method(guessed);
   1920    rv = -1;
   1921    goto done;
   1922  }
   1923 
   1924  if (tor_compress_supports_method(guessed) &&
   1925      compression != guessed) {
   1926    tor_uncompress(&new_body, &new_len, body, body_len, guessed,
   1927                   !allow_partial, LOG_INFO);
   1928    tried_both = 1;
   1929  }
   1930  /* If we're pretty sure that we have a compressed directory, and
   1931   * we didn't manage to uncompress it, then warn and bail. */
   1932  if (!plausible && !new_body) {
   1933    static ratelim_t warning_limit = RATELIM_INIT(60 * 60);
   1934    log_fn_ratelim(&warning_limit, LOG_WARN, LD_HTTP,
   1935           "Unable to decompress HTTP body (tried %s%s%s, on %s).",
   1936           description1,
   1937           tried_both?" and ":"",
   1938           tried_both?description2:"",
   1939           connection_describe(TO_CONN(conn)));
   1940    rv = -1;
   1941    goto done;
   1942  }
   1943 
   1944 done:
   1945  if (new_body) {
   1946    if (rv == 0) {
   1947      /* success! */
   1948      tor_free(*bodyp);
   1949      *bodyp = new_body;
   1950      *bodylenp = new_len;
   1951    } else {
   1952      tor_free(new_body);
   1953    }
   1954  }
   1955 
   1956  return rv;
   1957 }
   1958 
   1959 /**
   1960 * Total number of bytes downloaded of each directory purpose, when
   1961 * bootstrapped, and when not bootstrapped.
   1962 *
   1963 * (For example, the number of bytes downloaded of purpose p while
   1964 * not fully bootstrapped is total_dl[p][false].)
   1965 **/
   1966 static uint64_t total_dl[DIR_PURPOSE_MAX_][2];
   1967 
   1968 /**
   1969 * Heartbeat: dump a summary of how many bytes of which purpose we've
   1970 * downloaded, when bootstrapping and when not bootstrapping.
   1971 **/
   1972 void
   1973 dirclient_dump_total_dls(void)
   1974 {
   1975  const or_options_t *options = get_options();
   1976  for (int bootstrapped = 0; bootstrapped < 2; ++bootstrapped) {
   1977    smartlist_t *lines = smartlist_new();
   1978    for (int i=0; i < DIR_PURPOSE_MAX_; ++i) {
   1979      uint64_t n = total_dl[i][bootstrapped];
   1980      if (n == 0)
   1981        continue;
   1982      if (options->SafeLogging_ != SAFELOG_SCRUB_NONE &&
   1983          purpose_needs_anonymity(i, ROUTER_PURPOSE_GENERAL, NULL))
   1984        continue;
   1985      smartlist_add_asprintf(lines, "%"PRIu64" (%s)",
   1986                             n, dir_conn_purpose_to_string(i));
   1987    }
   1988 
   1989    if (smartlist_len(lines) > 0) {
   1990      char *log_line = smartlist_join_strings(lines, "; ", 0, NULL);
   1991      log_notice(LD_NET, "While %sbootstrapping, fetched this many bytes: %s",
   1992                 bootstrapped?"not ":"", log_line);
   1993      tor_free(log_line);
   1994 
   1995      SMARTLIST_FOREACH(lines, char *, s, tor_free(s));
   1996    }
   1997    smartlist_free(lines);
   1998  }
   1999 }
   2000 
   2001 /** We are a client, and we've finished reading the server's
   2002 * response. Parse it and act appropriately.
   2003 *
   2004 * If we're still happy with using this directory server in the future, return
   2005 * 0. Otherwise return -1; and the caller should consider trying the request
   2006 * again.
   2007 *
   2008 * The caller will take care of marking the connection for close.
   2009 */
   2010 static int
   2011 connection_dir_client_reached_eof(dir_connection_t *conn)
   2012 {
   2013  char *body = NULL;
   2014  char *headers = NULL;
   2015  char *reason = NULL;
   2016  size_t body_len = 0;
   2017  int status_code;
   2018  time_t date_header = 0;
   2019  long apparent_skew;
   2020  compress_method_t compression;
   2021  int skewed = 0;
   2022  int rv;
   2023  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
   2024                       conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
   2025                       conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
   2026  size_t received_bytes;
   2027  const int anonymized_connection =
   2028    purpose_needs_anonymity(conn->base_.purpose,
   2029                            conn->router_purpose,
   2030                            conn->requested_resource);
   2031 
   2032  received_bytes = connection_get_inbuf_len(TO_CONN(conn));
   2033 
   2034  log_debug(LD_DIR, "Downloaded %"TOR_PRIuSZ" bytes on connection of purpose "
   2035             "%s; bootstrap %d%%",
   2036             received_bytes,
   2037             dir_conn_purpose_to_string(conn->base_.purpose),
   2038             control_get_bootstrap_percent());
   2039  {
   2040    bool bootstrapped = control_get_bootstrap_percent() == 100;
   2041    total_dl[conn->base_.purpose][bootstrapped] += received_bytes;
   2042  }
   2043 
   2044  switch (connection_fetch_from_buf_http(TO_CONN(conn),
   2045                              &headers, MAX_HEADERS_SIZE,
   2046                              &body, &body_len, MAX_DIR_DL_SIZE,
   2047                              allow_partial)) {
   2048    case -1: /* overflow */
   2049      log_warn(LD_PROTOCOL,
   2050               "'fetch' response too large (%s). Closing.",
   2051               connection_describe(TO_CONN(conn)));
   2052      return -1;
   2053    case 0:
   2054      log_info(LD_HTTP,
   2055               "'fetch' response not all here, but we're at eof. Closing.");
   2056      return -1;
   2057    /* case 1, fall through */
   2058  }
   2059 
   2060  if (parse_http_response(headers, &status_code, &date_header,
   2061                          &compression, &reason) < 0) {
   2062    log_warn(LD_HTTP,"Unparseable headers (%s). Closing.",
   2063             connection_describe(TO_CONN(conn)));
   2064    rv = -1;
   2065    goto done;
   2066  }
   2067  if (!reason) reason = tor_strdup("[no reason given]");
   2068 
   2069  tor_log(LOG_DEBUG, LD_DIR,
   2070            "Received response on %s: %d %s "
   2071            "(purpose: %d, response size: %"TOR_PRIuSZ
   2072 #ifdef MEASUREMENTS_21206
   2073            ", data cells received: %d, data cells sent: %d"
   2074 #endif
   2075            ", compression: %d)",
   2076            connection_describe(TO_CONN(conn)),
   2077            status_code,
   2078            escaped(reason), conn->base_.purpose,
   2079            (received_bytes),
   2080 #ifdef MEASUREMENTS_21206
   2081            conn->data_cells_received, conn->data_cells_sent,
   2082 #endif
   2083            compression);
   2084 
   2085  if (conn->guard_state) {
   2086    /* we count the connection as successful once we can read from it.  We do
   2087     * not, however, delay use of the circuit here, since it's just for a
   2088     * one-hop directory request. */
   2089    /* XXXXprop271 note that this will not do the right thing for other
   2090     * waiting circuits that would be triggered by this circuit becoming
   2091     * complete/usable. But that's ok, I think.
   2092     */
   2093    entry_guard_succeeded(&conn->guard_state);
   2094    circuit_guard_state_free(conn->guard_state);
   2095    conn->guard_state = NULL;
   2096  }
   2097 
   2098  /* now check if it's got any hints for us about our IP address. */
   2099  if (conn->dirconn_direct) {
   2100    char *guess = http_get_header(headers, X_ADDRESS_HEADER);
   2101    if (guess) {
   2102      tor_addr_t addr;
   2103      if (tor_addr_parse(&addr, guess) < 0) {
   2104        log_debug(LD_DIR, "Malformed X-Your-Address-Is header %s. Ignoring.",
   2105                  escaped(guess));
   2106      } else {
   2107        relay_address_new_suggestion(&addr, &TO_CONN(conn)->addr, NULL);
   2108      }
   2109      tor_free(guess);
   2110    }
   2111  }
   2112 
   2113  if (date_header > 0) {
   2114    /* The date header was written very soon after we sent our request,
   2115     * so compute the skew as the difference between sending the request
   2116     * and the date header.  (We used to check now-date_header, but that's
   2117     * inaccurate if we spend a lot of time downloading.)
   2118     */
   2119    apparent_skew = conn->base_.timestamp_last_write_allowed - date_header;
   2120    if (labs(apparent_skew)>ALLOW_DIRECTORY_TIME_SKEW) {
   2121      int trusted = router_digest_is_trusted_dir(conn->identity_digest);
   2122      clock_skew_warning(TO_CONN(conn), apparent_skew, trusted, LD_HTTP,
   2123                         "directory", "DIRSERV");
   2124      skewed = 1; /* don't check the recommended-versions line */
   2125    } else {
   2126      log_debug(LD_HTTP, "Time on received directory is within tolerance; "
   2127                "we are %ld seconds skewed.  (That's okay.)", apparent_skew);
   2128    }
   2129  }
   2130  (void) skewed; /* skewed isn't used yet. */
   2131 
   2132  if (status_code == 503) {
   2133    routerstatus_t *rs;
   2134    dir_server_t *ds;
   2135    const char *id_digest = conn->identity_digest;
   2136    log_info(LD_DIR,"Received http status code %d (%s) from server "
   2137             "%s. I'll try again soon.",
   2138             status_code, escaped(reason),
   2139             connection_describe_peer(TO_CONN(conn)));
   2140    time_t now = approx_time();
   2141    if ((rs = router_get_mutable_consensus_status_by_id(id_digest)))
   2142      rs->last_dir_503_at = now;
   2143    if ((ds = router_get_fallback_dirserver_by_digest(id_digest)))
   2144      ds->fake_status.last_dir_503_at = now;
   2145 
   2146    rv = -1;
   2147    goto done;
   2148  }
   2149 
   2150  if (dir_client_decompress_response_body(&body, &body_len,
   2151                             conn, compression, anonymized_connection) < 0) {
   2152    rv = -1;
   2153    goto done;
   2154  }
   2155 
   2156  response_handler_args_t args;
   2157  memset(&args, 0, sizeof(args));
   2158  args.status_code = status_code;
   2159  args.reason = reason;
   2160  args.body = body;
   2161  args.body_len = body_len;
   2162  args.headers = headers;
   2163 
   2164  switch (conn->base_.purpose) {
   2165    case DIR_PURPOSE_FETCH_CONSENSUS:
   2166      rv = handle_response_fetch_consensus(conn, &args);
   2167      break;
   2168    case DIR_PURPOSE_FETCH_CERTIFICATE:
   2169      rv = handle_response_fetch_certificate(conn, &args);
   2170      break;
   2171    case DIR_PURPOSE_FETCH_STATUS_VOTE:
   2172      rv = handle_response_fetch_status_vote(conn, &args);
   2173      break;
   2174    case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
   2175      rv = handle_response_fetch_detached_signatures(conn, &args);
   2176      break;
   2177    case DIR_PURPOSE_FETCH_SERVERDESC:
   2178    case DIR_PURPOSE_FETCH_EXTRAINFO:
   2179      rv = handle_response_fetch_desc(conn, &args);
   2180      break;
   2181    case DIR_PURPOSE_FETCH_MICRODESC:
   2182      rv = handle_response_fetch_microdesc(conn, &args);
   2183      break;
   2184    case DIR_PURPOSE_UPLOAD_DIR:
   2185      rv = handle_response_upload_dir(conn, &args);
   2186      break;
   2187    case DIR_PURPOSE_UPLOAD_SIGNATURES:
   2188      rv = handle_response_upload_signatures(conn, &args);
   2189      break;
   2190    case DIR_PURPOSE_UPLOAD_VOTE:
   2191      rv = handle_response_upload_vote(conn, &args);
   2192      break;
   2193    case DIR_PURPOSE_UPLOAD_HSDESC:
   2194      rv = handle_response_upload_hsdesc(conn, &args);
   2195      break;
   2196    case DIR_PURPOSE_FETCH_HSDESC:
   2197      rv = handle_response_fetch_hsdesc_v3(conn, &args);
   2198      break;
   2199    default:
   2200      tor_assert_nonfatal_unreached();
   2201      rv = -1;
   2202      break;
   2203  }
   2204 
   2205 done:
   2206  tor_free(body);
   2207  tor_free(headers);
   2208  tor_free(reason);
   2209  return rv;
   2210 }
   2211 
   2212 /**
   2213 * Handler function: processes a response to a request for a networkstatus
   2214 * consensus document by checking the consensus, storing it, and marking
   2215 * router requests as reachable.
   2216 **/
   2217 STATIC int
   2218 handle_response_fetch_consensus(dir_connection_t *conn,
   2219                                const response_handler_args_t *args)
   2220 {
   2221  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS);
   2222  const int status_code = args->status_code;
   2223  const char *body = args->body;
   2224  const size_t body_len = args->body_len;
   2225  const char *reason = args->reason;
   2226  const time_t now = approx_time();
   2227 
   2228  const char *consensus;
   2229  char *new_consensus = NULL;
   2230  const char *sourcename;
   2231 
   2232  int r;
   2233  const char *flavname = conn->requested_resource;
   2234  if (status_code != 200) {
   2235    int severity = (status_code == 304) ? LOG_INFO : LOG_WARN;
   2236    tor_log(severity, LD_DIR,
   2237            "Received http status code %d (%s) from server "
   2238            "%s while fetching consensus directory.",
   2239            status_code, escaped(reason),
   2240            connection_describe_peer(TO_CONN(conn)));
   2241    networkstatus_consensus_download_failed(status_code, flavname);
   2242    return -1;
   2243  }
   2244 
   2245  if (looks_like_a_consensus_diff(body, body_len)) {
   2246    /* First find our previous consensus. Maybe it's in ram, maybe not. */
   2247    cached_dir_t *cd = NULL;
   2248    const char *consensus_body = NULL;
   2249    size_t consensus_body_len;
   2250    tor_mmap_t *mapped_consensus = NULL;
   2251 
   2252    /* We prefer the mmap'd version over the cached_dir_t version,
   2253     * since that matches the logic we used when we picked a consensus
   2254     * back in dir_consensus_request_set_additional_headers. */
   2255    mapped_consensus = networkstatus_map_cached_consensus(flavname);
   2256    if (mapped_consensus) {
   2257      consensus_body = mapped_consensus->data;
   2258      consensus_body_len = mapped_consensus->size;
   2259    } else {
   2260      cd = dirserv_get_consensus(flavname);
   2261      if (cd) {
   2262        consensus_body = cd->dir;
   2263        consensus_body_len = cd->dir_len;
   2264      }
   2265    }
   2266    if (!consensus_body) {
   2267      log_warn(LD_DIR, "Received a consensus diff, but we can't find "
   2268               "any %s-flavored consensus in our current cache.",flavname);
   2269      tor_munmap_file(mapped_consensus);
   2270      networkstatus_consensus_download_failed(0, flavname);
   2271      // XXXX if this happens too much, see below
   2272      return -1;
   2273    }
   2274 
   2275    new_consensus = consensus_diff_apply(consensus_body, consensus_body_len,
   2276                                         body, body_len);
   2277    tor_munmap_file(mapped_consensus);
   2278    if (new_consensus == NULL) {
   2279      log_warn(LD_DIR, "Could not apply consensus diff received from server "
   2280               "%s", connection_describe_peer(TO_CONN(conn)));
   2281      // XXXX If this happens too many times, we should maybe not use
   2282      // XXXX this directory for diffs any more?
   2283      networkstatus_consensus_download_failed(0, flavname);
   2284      return -1;
   2285    }
   2286    log_info(LD_DIR, "Applied consensus diff (size %d) from server "
   2287             "%s, resulting in a new consensus document (size %d).",
   2288             (int)body_len, connection_describe_peer(TO_CONN(conn)),
   2289             (int)strlen(new_consensus));
   2290    consensus = new_consensus;
   2291    sourcename = "generated based on a diff";
   2292  } else {
   2293    log_info(LD_DIR,"Received consensus directory (body size %d) from server "
   2294             "%s", (int)body_len, connection_describe_peer(TO_CONN(conn)));
   2295    consensus = body;
   2296    sourcename = "downloaded";
   2297  }
   2298 
   2299  if ((r=networkstatus_set_current_consensus(consensus,
   2300                                             strlen(consensus),
   2301                                             flavname, 0,
   2302                                             conn->identity_digest))<0) {
   2303    log_fn(r<-1?LOG_WARN:LOG_INFO, LD_DIR,
   2304           "Unable to load %s consensus directory %s from "
   2305           "server %s. I'll try again soon.",
   2306           flavname, sourcename,
   2307           connection_describe_peer(TO_CONN(conn)));
   2308    networkstatus_consensus_download_failed(0, flavname);
   2309    tor_free(new_consensus);
   2310    return -1;
   2311  }
   2312 
   2313  /* If we launched other fetches for this consensus, cancel them. */
   2314  connection_dir_close_consensus_fetches(conn, flavname);
   2315 
   2316  /* update the list of routers and directory guards */
   2317  routers_update_all_from_networkstatus(now, 3);
   2318  update_microdescs_from_networkstatus(now);
   2319  directory_info_has_arrived(now, 0, 0);
   2320 
   2321  if (authdir_mode_v3(get_options())) {
   2322    sr_act_post_consensus(
   2323                     networkstatus_get_latest_consensus_by_flavor(FLAV_NS));
   2324  }
   2325  log_info(LD_DIR, "Successfully loaded consensus.");
   2326 
   2327  tor_free(new_consensus);
   2328  return 0;
   2329 }
   2330 
   2331 /**
   2332 * Handler function: processes a response to a request for one or more
   2333 * authority certificates
   2334 **/
   2335 static int
   2336 handle_response_fetch_certificate(dir_connection_t *conn,
   2337                                  const response_handler_args_t *args)
   2338 {
   2339  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE);
   2340  const int status_code = args->status_code;
   2341  const char *reason = args->reason;
   2342  const char *body = args->body;
   2343  const size_t body_len = args->body_len;
   2344 
   2345  if (status_code != 200) {
   2346    log_warn(LD_DIR,
   2347             "Received http status code %d (%s) from server "
   2348             "%s while fetching \"/tor/keys/%s\".",
   2349             status_code, escaped(reason),
   2350             connection_describe_peer(TO_CONN(conn)),
   2351             conn->requested_resource);
   2352    connection_dir_download_cert_failed(conn, status_code);
   2353    return -1;
   2354  }
   2355  log_info(LD_DIR,"Received authority certificates (body size %d) from "
   2356           "server %s",
   2357           (int)body_len, connection_describe_peer(TO_CONN(conn)));
   2358 
   2359  /*
   2360   * Tell trusted_dirs_load_certs_from_string() whether it was by fp
   2361   * or fp-sk pair.
   2362   */
   2363  int src_code = -1;
   2364  if (!strcmpstart(conn->requested_resource, "fp/")) {
   2365    src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST;
   2366  } else if (!strcmpstart(conn->requested_resource, "fp-sk/")) {
   2367    src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST;
   2368  }
   2369 
   2370  if (src_code != -1) {
   2371    if (trusted_dirs_load_certs_from_string(body, src_code, 1,
   2372                                            conn->identity_digest)<0) {
   2373      log_warn(LD_DIR, "Unable to parse fetched certificates");
   2374      /* if we fetched more than one and only some failed, the successful
   2375       * ones got flushed to disk so it's safe to call this on them */
   2376      connection_dir_download_cert_failed(conn, status_code);
   2377    } else {
   2378      time_t now = approx_time();
   2379      directory_info_has_arrived(now, 0, 0);
   2380      log_info(LD_DIR, "Successfully loaded certificates from fetch.");
   2381    }
   2382  } else {
   2383    log_warn(LD_DIR,
   2384             "Couldn't figure out what to do with fetched certificates for "
   2385             "unknown resource %s",
   2386             conn->requested_resource);
   2387    connection_dir_download_cert_failed(conn, status_code);
   2388  }
   2389  return 0;
   2390 }
   2391 
   2392 /**
   2393 * Handler function: processes a response to a request for an authority's
   2394 * current networkstatus vote.
   2395 **/
   2396 static int
   2397 handle_response_fetch_status_vote(dir_connection_t *conn,
   2398                                  const response_handler_args_t *args)
   2399 {
   2400  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE);
   2401  const int status_code = args->status_code;
   2402  const char *reason = args->reason;
   2403  const char *body = args->body;
   2404  const size_t body_len = args->body_len;
   2405 
   2406  const char *msg;
   2407  int st;
   2408  log_notice(LD_DIR,"Got votes (body size %d) from server %s",
   2409             (int)body_len, connection_describe_peer(TO_CONN(conn)));
   2410  if (status_code != 200) {
   2411    log_warn(LD_DIR,
   2412             "Received http status code %d (%s) from server "
   2413             "%s while fetching \"/tor/status-vote/next/%s.z\".",
   2414             status_code, escaped(reason),
   2415             connection_describe_peer(TO_CONN(conn)),
   2416             conn->requested_resource);
   2417    return -1;
   2418  }
   2419  dirvote_add_vote(body, 0, TO_CONN(conn)->address, &msg, &st);
   2420  if (st > 299) {
   2421    log_warn(LD_DIR, "Error adding retrieved vote: %s", msg);
   2422  } else {
   2423    log_info(LD_DIR, "Added vote(s) successfully [msg: %s]", msg);
   2424  }
   2425 
   2426  return 0;
   2427 }
   2428 
   2429 /**
   2430 * Handler function: processes a response to a request for the signatures
   2431 * that an authority knows about on a given consensus.
   2432 **/
   2433 static int
   2434 handle_response_fetch_detached_signatures(dir_connection_t *conn,
   2435                                          const response_handler_args_t *args)
   2436 {
   2437  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES);
   2438  const int status_code = args->status_code;
   2439  const char *reason = args->reason;
   2440  const char *body = args->body;
   2441  const size_t body_len = args->body_len;
   2442 
   2443  const char *msg = NULL;
   2444  log_info(LD_DIR,"Got detached signatures (body size %d) from server %s",
   2445           (int)body_len,
   2446           connection_describe_peer(TO_CONN(conn)));
   2447  if (status_code != 200) {
   2448    log_warn(LD_DIR,
   2449        "Received http status code %d (%s) from server %s while fetching "
   2450        "\"/tor/status-vote/next/consensus-signatures.z\".",
   2451        status_code, escaped(reason),
   2452        connection_describe_peer(TO_CONN(conn)));
   2453    return -1;
   2454  }
   2455  if (dirvote_add_signatures(body, conn->base_.address, &msg)<0) {
   2456    log_warn(LD_DIR, "Problem adding detached signatures from %s: %s",
   2457             connection_describe_peer(TO_CONN(conn)),
   2458             msg?msg:"???");
   2459  }
   2460 
   2461  return 0;
   2462 }
   2463 
   2464 /**
   2465 * Handler function: processes a response to a request for a group of server
   2466 * descriptors or an extrainfo documents.
   2467 **/
   2468 static int
   2469 handle_response_fetch_desc(dir_connection_t *conn,
   2470                           const response_handler_args_t *args)
   2471 {
   2472  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
   2473             conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO);
   2474  const int status_code = args->status_code;
   2475  const char *reason = args->reason;
   2476  const char *body = args->body;
   2477  const size_t body_len = args->body_len;
   2478 
   2479  int was_ei = conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
   2480  smartlist_t *which = NULL;
   2481  int n_asked_for = 0;
   2482  int descriptor_digests = conn->requested_resource &&
   2483    !strcmpstart(conn->requested_resource,"d/");
   2484  log_info(LD_DIR,"Received %s (body size %d) from server %s",
   2485           was_ei ? "extra server info" : "server info",
   2486           (int)body_len, connection_describe_peer(TO_CONN(conn)));
   2487  if (conn->requested_resource &&
   2488      (!strcmpstart(conn->requested_resource,"d/") ||
   2489       !strcmpstart(conn->requested_resource,"fp/"))) {
   2490    which = smartlist_new();
   2491    dir_split_resource_into_fingerprints(conn->requested_resource +
   2492                                         (descriptor_digests ? 2 : 3),
   2493                                         which, NULL, 0);
   2494    n_asked_for = smartlist_len(which);
   2495  }
   2496  if (status_code != 200) {
   2497    int dir_okay = status_code == 404 ||
   2498      (status_code == 400 && !strcmp(reason, "Servers unavailable.")) ||
   2499       status_code == 301;
   2500    /* 404 means that it didn't have them; no big deal.
   2501     * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead.
   2502     * 301 is considered as an error since Tor does not follow redirects,
   2503     * which means we failed to reach the server we wanted. */
   2504    log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
   2505           "Received http status code %d (%s) from server %s "
   2506           "while fetching \"/tor/server/%s\". I'll try again soon.",
   2507           status_code, escaped(reason),
   2508           connection_describe_peer(TO_CONN(conn)),
   2509           conn->requested_resource);
   2510    if (!which) {
   2511      connection_dir_download_routerdesc_failed(conn);
   2512    } else {
   2513      dir_routerdesc_download_failed(which, status_code,
   2514                                     conn->router_purpose,
   2515                                     was_ei, descriptor_digests);
   2516      SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
   2517      smartlist_free(which);
   2518    }
   2519    return dir_okay ? 0 : -1;
   2520  }
   2521  /* Learn the routers, assuming we requested by fingerprint or "all"
   2522   * or "authority".
   2523   *
   2524   * We use "authority" to fetch our own descriptor for
   2525   * testing, and to fetch bridge descriptors for bootstrapping. Ignore
   2526   * the output of "authority" requests unless we are using bridges,
   2527   * since otherwise they'll be the response from reachability tests,
   2528   * and we don't really want to add that to our routerlist. */
   2529  if (which || (conn->requested_resource &&
   2530                (!strcmpstart(conn->requested_resource, "all") ||
   2531                 (!strcmpstart(conn->requested_resource, "authority") &&
   2532                  get_options()->UseBridges)))) {
   2533    /* as we learn from them, we remove them from 'which' */
   2534    if (was_ei) {
   2535      router_load_extrainfo_from_string(body, NULL, SAVED_NOWHERE, which,
   2536                                        descriptor_digests);
   2537    } else {
   2538      //router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
   2539      //                       descriptor_digests, conn->router_purpose);
   2540      if (load_downloaded_routers(body, which, descriptor_digests,
   2541                                  conn->router_purpose,
   2542                                  conn->base_.address)) {
   2543        time_t now = approx_time();
   2544        directory_info_has_arrived(now, 0, 1);
   2545      }
   2546    }
   2547  }
   2548  if (which) { /* mark remaining ones as failed */
   2549    log_info(LD_DIR, "Received %d/%d %s requested from %s",
   2550             n_asked_for-smartlist_len(which), n_asked_for,
   2551             was_ei ? "extra-info documents" : "router descriptors",
   2552             connection_describe_peer(TO_CONN(conn)));
   2553    if (smartlist_len(which)) {
   2554      dir_routerdesc_download_failed(which, status_code,
   2555                                     conn->router_purpose,
   2556                                     was_ei, descriptor_digests);
   2557    }
   2558    SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
   2559    smartlist_free(which);
   2560  }
   2561 
   2562  return 0;
   2563 }
   2564 
   2565 /**
   2566 * Handler function: processes a response to a request for a group of
   2567 * microdescriptors
   2568 **/
   2569 STATIC int
   2570 handle_response_fetch_microdesc(dir_connection_t *conn,
   2571                                const response_handler_args_t *args)
   2572 {
   2573  tor_assert(conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
   2574  const int status_code = args->status_code;
   2575  const char *reason = args->reason;
   2576  const char *body = args->body;
   2577  const size_t body_len = args->body_len;
   2578 
   2579  smartlist_t *which = NULL;
   2580  log_info(LD_DIR,"Received answer to microdescriptor request (status %d, "
   2581           "body size %d) from server %s",
   2582           status_code, (int)body_len,
   2583           connection_describe_peer(TO_CONN(conn)));
   2584  tor_assert(conn->requested_resource &&
   2585             !strcmpstart(conn->requested_resource, "d/"));
   2586  tor_assert_nonfatal(!fast_mem_is_zero(conn->identity_digest, DIGEST_LEN));
   2587  which = smartlist_new();
   2588  dir_split_resource_into_fingerprints(conn->requested_resource+2,
   2589                                       which, NULL,
   2590                                       DSR_DIGEST256|DSR_BASE64);
   2591  if (status_code != 200) {
   2592    log_info(LD_DIR, "Received status code %d (%s) from server "
   2593             "%s while fetching \"/tor/micro/%s\".  I'll try again "
   2594             "soon.",
   2595             status_code, escaped(reason),
   2596             connection_describe_peer(TO_CONN(conn)),
   2597             conn->requested_resource);
   2598    dir_microdesc_download_failed(which, status_code, conn->identity_digest);
   2599    SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
   2600    smartlist_free(which);
   2601    return 0;
   2602  } else {
   2603    smartlist_t *mds;
   2604    time_t now = approx_time();
   2605    mds = microdescs_add_to_cache(get_microdesc_cache(),
   2606                                  body, body+body_len, SAVED_NOWHERE, 0,
   2607                                  now, which);
   2608    if (smartlist_len(which)) {
   2609      /* Mark remaining ones as failed. */
   2610      dir_microdesc_download_failed(which, status_code, conn->identity_digest);
   2611    }
   2612    if (mds && smartlist_len(mds)) {
   2613      control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
   2614                             count_loading_descriptors_progress());
   2615      directory_info_has_arrived(now, 0, 1);
   2616    }
   2617    SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
   2618    smartlist_free(which);
   2619    smartlist_free(mds);
   2620  }
   2621 
   2622  return 0;
   2623 }
   2624 
   2625 /**
   2626 * Handler function: processes a response to a POST request to upload our
   2627 * router descriptor.
   2628 **/
   2629 static int
   2630 handle_response_upload_dir(dir_connection_t *conn,
   2631                           const response_handler_args_t *args)
   2632 {
   2633  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_DIR);
   2634  const int status_code = args->status_code;
   2635  const char *reason = args->reason;
   2636  const char *headers = args->headers;
   2637 
   2638  switch (status_code) {
   2639  case 200: {
   2640    dir_server_t *ds =
   2641      router_get_trusteddirserver_by_digest(conn->identity_digest);
   2642    char *rejected_hdr = http_get_header(headers,
   2643                                         "X-Descriptor-Not-New: ");
   2644    if (rejected_hdr) {
   2645      if (!strcmp(rejected_hdr, "Yes")) {
   2646        log_info(LD_GENERAL,
   2647                 "Authority '%s' declined our descriptor (not new)",
   2648                 ds->nickname);
   2649        /* XXXX use this information; be sure to upload next one
   2650         * sooner. -NM */
   2651        /* XXXX++ On further thought, the task above implies that we're
   2652         * basing our regenerate-descriptor time on when we uploaded the
   2653         * last descriptor, not on the published time of the last
   2654         * descriptor.  If those are different, that's a bad thing to
   2655         * do. -NM */
   2656      }
   2657      tor_free(rejected_hdr);
   2658    }
   2659    log_info(LD_GENERAL,"eof (status 200) after uploading server "
   2660             "descriptor: finished.");
   2661    control_event_server_status(
   2662                   LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
   2663                   conn->base_.address, conn->base_.port);
   2664 
   2665    ds->has_accepted_serverdesc = 1;
   2666    if (directories_have_accepted_server_descriptor())
   2667      control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
   2668  }
   2669    break;
   2670  case 400:
   2671    log_warn(LD_GENERAL,"http status 400 (%s) response from "
   2672             "dirserver %s. Please correct.",
   2673             escaped(reason), connection_describe_peer(TO_CONN(conn)));
   2674    control_event_server_status(LOG_WARN,
   2675                    "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
   2676                    conn->base_.address, conn->base_.port, escaped(reason));
   2677    break;
   2678  default:
   2679    log_warn(LD_GENERAL,
   2680             "HTTP status %d (%s) was unexpected while uploading "
   2681             "descriptor to server %s'. Possibly the server is "
   2682             "misconfigured?",
   2683             status_code, escaped(reason),
   2684             connection_describe_peer(TO_CONN(conn)));
   2685    break;
   2686  }
   2687  /* return 0 in all cases, since we don't want to mark any
   2688   * dirservers down just because they don't like us. */
   2689 
   2690  return 0;
   2691 }
   2692 
   2693 /**
   2694 * Handler function: processes a response to POST request to upload our
   2695 * own networkstatus vote.
   2696 **/
   2697 static int
   2698 handle_response_upload_vote(dir_connection_t *conn,
   2699                            const response_handler_args_t *args)
   2700 {
   2701  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE);
   2702  const int status_code = args->status_code;
   2703  const char *reason = args->reason;
   2704 
   2705  switch (status_code) {
   2706  case 200: {
   2707    log_notice(LD_DIR,"Uploaded my vote to dirserver %s",
   2708               connection_describe_peer(TO_CONN(conn)));
   2709  }
   2710    break;
   2711  case 400:
   2712    log_warn(LD_DIR,"http status 400 (%s) response after uploading "
   2713             "vote to dirserver %s. Please correct.",
   2714             escaped(reason), connection_describe_peer(TO_CONN(conn)));
   2715    break;
   2716  default:
   2717    log_warn(LD_GENERAL,
   2718             "HTTP status %d (%s) was unexpected while uploading "
   2719             "vote to server %s.",
   2720             status_code, escaped(reason),
   2721             connection_describe_peer(TO_CONN(conn)));
   2722    break;
   2723  }
   2724  /* return 0 in all cases, since we don't want to mark any
   2725   * dirservers down just because they don't like us. */
   2726  return 0;
   2727 }
   2728 
   2729 /**
   2730 * Handler function: processes a response to POST request to upload our
   2731 * view of the signatures on the current consensus.
   2732 **/
   2733 static int
   2734 handle_response_upload_signatures(dir_connection_t *conn,
   2735                                  const response_handler_args_t *args)
   2736 {
   2737  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_SIGNATURES);
   2738  const int status_code = args->status_code;
   2739  const char *reason = args->reason;
   2740 
   2741  switch (status_code) {
   2742  case 200: {
   2743    log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s",
   2744               connection_describe_peer(TO_CONN(conn)));
   2745  }
   2746    break;
   2747  case 400:
   2748    log_warn(LD_DIR,"http status 400 (%s) response after uploading "
   2749             "signatures to dirserver %s. Please correct.",
   2750             escaped(reason), connection_describe_peer(TO_CONN(conn)));
   2751    break;
   2752  default:
   2753    log_warn(LD_GENERAL,
   2754             "HTTP status %d (%s) was unexpected while uploading "
   2755             "signatures to server %s.",
   2756             status_code, escaped(reason),
   2757             connection_describe_peer(TO_CONN(conn)));
   2758    break;
   2759  }
   2760  /* return 0 in all cases, since we don't want to mark any
   2761   * dirservers down just because they don't like us. */
   2762 
   2763  return 0;
   2764 }
   2765 
   2766 /**
   2767 * Handler function: processes a response to a request for a v3 hidden service
   2768 * descriptor.
   2769 **/
   2770 STATIC int
   2771 handle_response_fetch_hsdesc_v3(dir_connection_t *conn,
   2772                                const response_handler_args_t *args)
   2773 {
   2774  const int status_code = args->status_code;
   2775  const char *reason = args->reason;
   2776  const char *body = args->body;
   2777  const size_t body_len = args->body_len;
   2778 
   2779  tor_assert(conn->hs_ident);
   2780 
   2781  log_info(LD_REND,"Received v3 hsdesc (body size %d, status %d (%s))",
   2782           (int)body_len, status_code, escaped(reason));
   2783 
   2784  hs_client_dir_fetch_done(conn, reason, body, status_code);
   2785  return 0;
   2786 }
   2787 
   2788 /**
   2789 * Handler function: processes a response to a POST request to upload an
   2790 * hidden service descriptor.
   2791 **/
   2792 static int
   2793 handle_response_upload_hsdesc(dir_connection_t *conn,
   2794                              const response_handler_args_t *args)
   2795 {
   2796  const int status_code = args->status_code;
   2797  const char *reason = args->reason;
   2798 
   2799  tor_assert(conn);
   2800  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_HSDESC);
   2801 
   2802  log_info(LD_REND, "Uploaded hidden service descriptor (status %d "
   2803                    "(%s))",
   2804           status_code, escaped(reason));
   2805  /* For this directory response, it MUST have an hidden service identifier on
   2806   * this connection. */
   2807  tor_assert(conn->hs_ident);
   2808  switch (status_code) {
   2809  case 200:
   2810    log_info(LD_REND, "Uploading hidden service descriptor: "
   2811                      "finished with status 200 (%s)", escaped(reason));
   2812    hs_control_desc_event_uploaded(conn->hs_ident, conn->identity_digest);
   2813    break;
   2814  case 400:
   2815    log_fn(LOG_PROTOCOL_WARN, LD_REND,
   2816           "Uploading hidden service descriptor: http "
   2817           "status 400 (%s) response from dirserver "
   2818           "%s. Malformed hidden service descriptor?",
   2819           escaped(reason), connection_describe_peer(TO_CONN(conn)));
   2820    hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
   2821                                 "UPLOAD_REJECTED");
   2822    break;
   2823  default:
   2824    log_warn(LD_REND, "Uploading hidden service descriptor: http "
   2825                      "status %d (%s) response unexpected (server "
   2826                      "%s').",
   2827             status_code, escaped(reason),
   2828             connection_describe_peer(TO_CONN(conn)));
   2829    hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
   2830                                 "UNEXPECTED");
   2831    break;
   2832  }
   2833 
   2834  return 0;
   2835 }
   2836 
   2837 /** Called when a directory connection reaches EOF. */
   2838 int
   2839 connection_dir_reached_eof(dir_connection_t *conn)
   2840 {
   2841  int retval;
   2842  if (conn->base_.state != DIR_CONN_STATE_CLIENT_READING) {
   2843    log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
   2844             conn->base_.state);
   2845    connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
   2846    connection_mark_for_close(TO_CONN(conn));
   2847    return -1;
   2848  }
   2849 
   2850  retval = connection_dir_client_reached_eof(conn);
   2851  if (retval == 0) /* success */
   2852    conn->base_.state = DIR_CONN_STATE_CLIENT_FINISHED;
   2853  connection_mark_for_close(TO_CONN(conn));
   2854  return retval;
   2855 }
   2856 /** We are closing a dir connection: If <b>dir_conn</b> is a dir connection
   2857 *  that tried to fetch an HS descriptor, check if it successfully fetched it,
   2858 *  or if we need to try again. */
   2859 void
   2860 connection_dir_client_refetch_hsdesc_if_needed(dir_connection_t *dir_conn)
   2861 {
   2862  connection_t *conn = TO_CONN(dir_conn);
   2863 
   2864  /* Check for v3 rend desc fetch */
   2865  if (conn->purpose == DIR_PURPOSE_FETCH_HSDESC &&
   2866      dir_conn->hs_ident &&
   2867      !ed25519_public_key_is_zero(&dir_conn->hs_ident->identity_pk)) {
   2868    hs_client_refetch_hsdesc(&dir_conn->hs_ident->identity_pk);
   2869  }
   2870 }
   2871 
   2872 /** Array of compression methods to use (if supported) for requesting
   2873 * compressed data, ordered from best to worst. */
   2874 static compress_method_t client_meth_pref[] = {
   2875  LZMA_METHOD,
   2876  ZSTD_METHOD,
   2877  ZLIB_METHOD,
   2878  GZIP_METHOD,
   2879  NO_METHOD
   2880 };
   2881 
   2882 /** Array of allowed compression methods to use (if supported) when receiving a
   2883 * response from a request that was required to be anonymous. */
   2884 static compress_method_t client_meth_allowed_anonymous_compression[] = {
   2885  ZLIB_METHOD,
   2886  GZIP_METHOD,
   2887  NO_METHOD
   2888 };
   2889 
   2890 /** Return a newly allocated string containing a comma separated list of
   2891 * supported encodings. */
   2892 STATIC char *
   2893 accept_encoding_header(void)
   2894 {
   2895  smartlist_t *methods = smartlist_new();
   2896  char *header = NULL;
   2897  compress_method_t method;
   2898  unsigned i;
   2899 
   2900  for (i = 0; i < ARRAY_LENGTH(client_meth_pref); ++i) {
   2901    method = client_meth_pref[i];
   2902    if (tor_compress_supports_method(method))
   2903      smartlist_add(methods, (char *)compression_method_get_name(method));
   2904  }
   2905 
   2906  header = smartlist_join_strings(methods, ", ", 0, NULL);
   2907  smartlist_free(methods);
   2908 
   2909  return header;
   2910 }
   2911 
   2912 /** Check if the given compression method is allowed for a connection that is
   2913 * supposed to be anonymous. Returns 1 if the compression method is allowed,
   2914 * otherwise 0. */
   2915 STATIC int
   2916 allowed_anonymous_connection_compression_method(compress_method_t method)
   2917 {
   2918  unsigned u;
   2919 
   2920  for (u = 0; u < ARRAY_LENGTH(client_meth_allowed_anonymous_compression);
   2921       ++u) {
   2922    compress_method_t allowed_method =
   2923      client_meth_allowed_anonymous_compression[u];
   2924 
   2925    if (! tor_compress_supports_method(allowed_method))
   2926      continue;
   2927 
   2928    if (method == allowed_method)
   2929      return 1;
   2930  }
   2931 
   2932  return 0;
   2933 }
   2934 
   2935 /** Log a warning when a remote server has sent us a document using a
   2936 * compression method that is not allowed for anonymous directory requests. */
   2937 STATIC void
   2938 warn_disallowed_anonymous_compression_method(compress_method_t method)
   2939 {
   2940  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
   2941         "Received a %s HTTP response, which is not "
   2942         "allowed for anonymous directory requests.",
   2943         compression_method_get_human_name(method));
   2944 }
   2945 
   2946 /* We just got a new consensus! If there are other in-progress requests
   2947 * for this consensus flavor (for example because we launched several in
   2948 * parallel), cancel them.
   2949 *
   2950 * We do this check here (not just in
   2951 * connection_ap_handshake_attach_circuit()) to handle the edge case where
   2952 * a consensus fetch begins and ends before some other one tries to attach to
   2953 * a circuit, in which case the other one won't know that we're all happy now.
   2954 *
   2955 * Don't mark the conn that just gave us the consensus -- otherwise we
   2956 * would end up double-marking it when it cleans itself up.
   2957 */
   2958 static void
   2959 connection_dir_close_consensus_fetches(dir_connection_t *except_this_one,
   2960                                       const char *resource)
   2961 {
   2962  smartlist_t *conns_to_close =
   2963    connection_dir_list_by_purpose_and_resource(DIR_PURPOSE_FETCH_CONSENSUS,
   2964                                                resource);
   2965  SMARTLIST_FOREACH_BEGIN(conns_to_close, dir_connection_t *, d) {
   2966    if (d == except_this_one)
   2967      continue;
   2968    log_info(LD_DIR, "Closing consensus fetch (to %s) since one "
   2969             "has just arrived.", connection_describe_peer(TO_CONN(d)));
   2970    connection_mark_for_close(TO_CONN(d));
   2971  } SMARTLIST_FOREACH_END(d);
   2972  smartlist_free(conns_to_close);
   2973 }
   2974 /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
   2975 * fetches have failed (with uppercase fingerprints listed in <b>failed</b>,
   2976 * either as descriptor digests or as identity digests based on
   2977 * <b>was_descriptor_digests</b>).
   2978 */
   2979 static void
   2980 dir_routerdesc_download_failed(smartlist_t *failed, int status_code,
   2981                               int router_purpose,
   2982                               int was_extrainfo, int was_descriptor_digests)
   2983 {
   2984  char digest[DIGEST_LEN];
   2985  time_t now = time(NULL);
   2986  int server = dirclient_fetches_from_authorities(get_options());
   2987  if (!was_descriptor_digests) {
   2988    if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
   2989      tor_assert(!was_extrainfo);
   2990      connection_dir_retry_bridges(failed);
   2991    }
   2992    return; /* FFFF should implement for other-than-router-purpose someday */
   2993  }
   2994  SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
   2995    download_status_t *dls = NULL;
   2996    if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
   2997      log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
   2998      continue;
   2999    }
   3000    if (was_extrainfo) {
   3001      signed_descriptor_t *sd =
   3002        router_get_by_extrainfo_digest(digest);
   3003      if (sd)
   3004        dls = &sd->ei_dl_status;
   3005    } else {
   3006      dls = router_get_dl_status_by_descriptor_digest(digest);
   3007    }
   3008    if (!dls)
   3009      continue;
   3010    download_status_increment_failure(dls, status_code, cp, server, now);
   3011  } SMARTLIST_FOREACH_END(cp);
   3012 
   3013  /* No need to relaunch descriptor downloads here: we already do it
   3014   * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
   3015 }
   3016 
   3017 /** Called when a connection to download microdescriptors from relay with
   3018 * <b>dir_id</b> has failed in whole or in part. <b>failed</b> is a list
   3019 * of every microdesc digest we didn't get. <b>status_code</b> is the http
   3020 * status code we received. Reschedule the microdesc downloads as
   3021 * appropriate. */
   3022 static void
   3023 dir_microdesc_download_failed(smartlist_t *failed,
   3024                              int status_code, const char *dir_id)
   3025 {
   3026  networkstatus_t *consensus
   3027    = networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
   3028  routerstatus_t *rs;
   3029  download_status_t *dls;
   3030  time_t now = time(NULL);
   3031  int server = dirclient_fetches_from_authorities(get_options());
   3032 
   3033  if (! consensus)
   3034    return;
   3035 
   3036  /* We failed to fetch a microdescriptor from 'dir_id', note it down
   3037   * so that we don't try the same relay next time... */
   3038  microdesc_note_outdated_dirserver(dir_id);
   3039 
   3040  SMARTLIST_FOREACH_BEGIN(failed, const char *, d) {
   3041    rs = router_get_mutable_consensus_status_by_descriptor_digest(consensus,d);
   3042    if (!rs)
   3043      continue;
   3044    dls = &rs->dl_status;
   3045 
   3046    { /* Increment the failure count for this md fetch */
   3047      char buf[BASE64_DIGEST256_LEN+1];
   3048      digest256_to_base64(buf, d);
   3049      log_info(LD_DIR, "Failed to download md %s from %s",
   3050               buf, hex_str(dir_id, DIGEST_LEN));
   3051      download_status_increment_failure(dls, status_code, buf,
   3052                                        server, now);
   3053    }
   3054  } SMARTLIST_FOREACH_END(d);
   3055 }