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