Tor  0.4.5.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 (!reachable_addr_allows_dir_server(ds,
288  FIREWALL_DIR_CONNECTION,
289  0)) {
290  if (reachable_addr_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  reachable_addr_choose_from_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 (!tor_addr_is_null(&status->ipv4_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_copy(&use_or_ap->addr, &status->ipv4_addr);
661  use_or_ap->port = status->ipv4_orport;
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  reachable_addr_choose_from_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  reachable_addr_choose_from_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  char *ipv6_str = tor_addr_to_str_dup(&status->ipv6_addr);
690  log_info(LD_BUG, "Rejected all OR and Dir addresses from %s when "
691  "launching an outgoing directory connection to: IPv4 %s OR %d "
692  "Dir %d IPv6 %s OR %d Dir %d", routerstatus_describe(status),
693  fmt_addr(&status->ipv4_addr), status->ipv4_orport,
694  status->ipv4_dirport, ipv6_str, status->ipv6_orport,
695  status->ipv4_dirport);
696  tor_free(ipv6_str);
697  if (!logged_backtrace) {
698  log_backtrace(LOG_INFO, LD_BUG, "Addresses came from");
699  logged_backtrace = 1;
700  }
701  return -1;
702  }
703 
704  return 0;
705 }
706 
707 /** Return true iff <b>conn</b> is the client side of a directory connection
708  * we launched to ourself in order to determine the reachability of our
709  * dir_port. */
710 static int
712 {
713  if (conn->requested_resource &&
714  !strcmpstart(conn->requested_resource,"authority")) {
716  if (me &&
718  tor_addr_eq(&TO_CONN(conn)->addr, &me->ipv4_addr) &&
719  me->ipv4_dirport == conn->base_.port)
720  return 1;
721  }
722  return 0;
723 }
724 
725 /** Called when we are unable to complete the client's request to a directory
726  * server due to a network error: Mark the router as down and try again if
727  * possible.
728  */
729 void
731 {
732  if (conn->guard_state) {
733  /* We haven't seen a success on this guard state, so consider it to have
734  * failed. */
736  }
738  return; /* this was a test fetch. don't retry. */
739  }
741  router_set_status(conn->identity_digest, 0); /* don't try this one again */
742  if (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
743  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO) {
744  log_info(LD_DIR, "Giving up on serverdesc/extrainfo fetch from "
745  "directory server at %s; retrying",
750  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
751  if (conn->requested_resource)
753  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_CERTIFICATE) {
754  log_info(LD_DIR, "Giving up on certificate fetch from directory server "
755  "at %s; retrying",
758  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_DETACHED_SIGNATURES) {
759  log_info(LD_DIR, "Giving up downloading detached signatures from %s",
761  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_STATUS_VOTE) {
762  log_info(LD_DIR, "Giving up downloading votes from %s",
764  } else if (conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC) {
765  log_info(LD_DIR, "Giving up on downloading microdescriptors from "
766  "directory server at %s; will retry",
769  }
770 }
771 
772 /** Helper: Attempt to fetch directly the descriptors of each bridge
773  * listed in <b>failed</b>.
774  */
775 static void
777 {
778  char digest[DIGEST_LEN];
779  SMARTLIST_FOREACH(descs, const char *, cp,
780  {
781  if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
782  log_warn(LD_BUG, "Malformed fingerprint in list: %s",
783  escaped(cp));
784  continue;
785  }
787  });
788 }
789 
790 /** Called when an attempt to download one or more router descriptors
791  * or extra-info documents on connection <b>conn</b> failed.
792  */
793 static void
795 {
796  /* No need to increment the failure count for routerdescs, since
797  * it's not their fault. */
798 
799  /* No need to relaunch descriptor downloads here: we already do it
800  * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
802  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
803  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
804 
805  (void) conn;
806 }
807 
808 /** Called when an attempt to download a bridge's routerdesc from
809  * one of the authorities failed due to a network error. If
810  * possible attempt to download descriptors from the bridge directly.
811  */
812 static void
814 {
815  smartlist_t *which = NULL;
816 
817  /* Requests for bridge descriptors are in the form 'fp/', so ignore
818  anything else. */
819  if (!conn->requested_resource || strcmpstart(conn->requested_resource,"fp/"))
820  return;
821 
822  which = smartlist_new();
824  + strlen("fp/"),
825  which, NULL, 0);
826 
828  if (smartlist_len(which)) {
830  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
831  }
832  smartlist_free(which);
833 }
834 
835 /** Called when an attempt to fetch a certificate fails. */
836 static void
838 {
839  const char *fp_pfx = "fp/";
840  const char *fpsk_pfx = "fp-sk/";
841  smartlist_t *failed;
843 
844  if (!conn->requested_resource)
845  return;
846  failed = smartlist_new();
847  /*
848  * We have two cases download by fingerprint (resource starts
849  * with "fp/") or download by fingerprint/signing key pair
850  * (resource starts with "fp-sk/").
851  */
852  if (!strcmpstart(conn->requested_resource, fp_pfx)) {
853  /* Download by fingerprint case */
855  strlen(fp_pfx),
856  failed, NULL, DSR_HEX);
857  SMARTLIST_FOREACH_BEGIN(failed, char *, cp) {
858  /* Null signing key digest indicates download by fp only */
859  authority_cert_dl_failed(cp, NULL, status);
860  tor_free(cp);
861  } SMARTLIST_FOREACH_END(cp);
862  } else if (!strcmpstart(conn->requested_resource, fpsk_pfx)) {
863  /* Download by (fp,sk) pairs */
865  strlen(fpsk_pfx), failed);
866  SMARTLIST_FOREACH_BEGIN(failed, fp_pair_t *, cp) {
867  authority_cert_dl_failed(cp->first, cp->second, status);
868  tor_free(cp);
869  } SMARTLIST_FOREACH_END(cp);
870  } else {
871  log_warn(LD_DIR,
872  "Don't know what to do with failure for cert fetch %s",
873  conn->requested_resource);
874  }
875 
876  smartlist_free(failed);
877 
878  update_certificate_downloads(time(NULL));
879 }
880 
881 /** Evaluate the situation and decide if we should use an encrypted
882  * "begindir-style" connection for this directory request.
883  * 0) If there is no DirPort, yes.
884  * 1) If or_port is 0, or it's a direct conn and or_port is firewalled
885  * or we're a dir mirror, no.
886  * 2) If we prefer to avoid begindir conns, and we're not fetching or
887  * publishing a bridge relay descriptor, no.
888  * 3) Else yes.
889  * If returning 0, return in *reason why we can't use begindir.
890  * reason must not be NULL.
891  */
892 static int
894  const directory_request_t *req,
895  const char **reason)
896 {
897  const tor_addr_t *or_addr = &req->or_addr_port.addr;
898  //const tor_addr_t *dir_addr = &req->dir_addr_port.addr;
899  const int or_port = req->or_addr_port.port;
900  const int dir_port = req->dir_addr_port.port;
901 
902  const dir_indirection_t indirection = req->indirection;
903 
904  tor_assert(reason);
905  *reason = NULL;
906 
907  /* Reasons why we must use begindir */
908  if (!dir_port) {
909  *reason = "(using begindir - directory with no DirPort)";
910  return 1; /* We don't know a DirPort -- must begindir. */
911  }
912  /* Reasons why we can't possibly use begindir */
913  if (!or_port) {
914  *reason = "directory with unknown ORPort";
915  return 0; /* We don't know an ORPort -- no chance. */
916  }
917  if (indirection == DIRIND_DIRECT_CONN ||
918  indirection == DIRIND_ANON_DIRPORT) {
919  *reason = "DirPort connection";
920  return 0;
921  }
922  if (indirection == DIRIND_ONEHOP) {
923  /* We're firewalled and want a direct OR connection */
924  if (!reachable_addr_allows_addr(or_addr, or_port,
925  FIREWALL_OR_CONNECTION, 0, 0)) {
926  *reason = "ORPort not reachable";
927  return 0;
928  }
929  }
930  /* Reasons why we want to avoid using begindir */
931  if (indirection == DIRIND_ONEHOP) {
932  if (!dirclient_must_use_begindir(options)) {
933  *reason = "in relay mode";
934  return 0;
935  }
936  }
937  /* DIRIND_ONEHOP on a client, or DIRIND_ANONYMOUS
938  */
939  *reason = "(using begindir)";
940  return 1;
941 }
942 
943 /**
944  * Create and return a new directory_request_t with purpose
945  * <b>dir_purpose</b>.
946  */
948 directory_request_new(uint8_t dir_purpose)
949 {
950  tor_assert(dir_purpose >= DIR_PURPOSE_MIN_);
951  tor_assert(dir_purpose <= DIR_PURPOSE_MAX_);
952  tor_assert(dir_purpose != DIR_PURPOSE_SERVER);
955 
956  directory_request_t *result = tor_malloc_zero(sizeof(*result));
957  tor_addr_make_null(&result->or_addr_port.addr, AF_INET);
958  result->or_addr_port.port = 0;
959  tor_addr_make_null(&result->dir_addr_port.addr, AF_INET);
960  result->dir_addr_port.port = 0;
961  result->dir_purpose = dir_purpose;
962  result->router_purpose = ROUTER_PURPOSE_GENERAL;
963  result->indirection = DIRIND_ONEHOP;
964  return result;
965 }
966 /**
967  * Release all resources held by <b>req</b>.
968  */
969 void
971 {
972  if (req == NULL)
973  return;
974  config_free_lines(req->additional_headers);
975  tor_free(req);
976 }
977 /**
978  * Set the address and OR port to use for this directory request. If there is
979  * no OR port, we'll have to connect over the dirport. (If there are both,
980  * the indirection setting determines which to use.)
981  */
982 void
984  const tor_addr_port_t *p)
985 {
986  memcpy(&req->or_addr_port, p, sizeof(*p));
987 }
988 /**
989  * Set the address and dirport to use for this directory request. If there
990  * is no dirport, we'll have to connect over the OR port. (If there are both,
991  * the indirection setting determines which to use.)
992  */
993 void
995  const tor_addr_port_t *p)
996 {
997  memcpy(&req->dir_addr_port, p, sizeof(*p));
998 }
999 /**
1000  * Set the RSA identity digest of the directory to use for this directory
1001  * request.
1002  */
1003 void
1005  const char *digest)
1006 {
1007  memcpy(req->digest, digest, DIGEST_LEN);
1008 }
1009 /**
1010  * Set the router purpose associated with uploaded and downloaded router
1011  * descriptors and extrainfo documents in this directory request. The purpose
1012  * must be one of ROUTER_PURPOSE_GENERAL (the default) or
1013  * ROUTER_PURPOSE_BRIDGE.
1014  */
1015 void
1017  uint8_t router_purpose)
1018 {
1019  tor_assert(router_purpose == ROUTER_PURPOSE_GENERAL ||
1020  router_purpose == ROUTER_PURPOSE_BRIDGE);
1021  // assert that it actually makes sense to set this purpose, given
1022  // the dir_purpose.
1023  req->router_purpose = router_purpose;
1024 }
1025 /**
1026  * Set the indirection to be used for the directory request. The indirection
1027  * parameter configures whether to connect to a DirPort or ORPort, and whether
1028  * to anonymize the connection. DIRIND_ONEHOP (use ORPort, don't anonymize)
1029  * is the default. See dir_indirection_t for more information.
1030  */
1031 void
1033  dir_indirection_t indirection)
1034 {
1035  req->indirection = indirection;
1036 }
1037 
1038 /**
1039  * Set a pointer to the resource to request from a directory. Different
1040  * request types use resources to indicate different components of their URL.
1041  * Note that only an alias to <b>resource</b> is stored, so the
1042  * <b>resource</b> must outlive the request.
1043  */
1044 void
1046  const char *resource)
1047 {
1048  req->resource = resource;
1049 }
1050 /**
1051  * Set a pointer to the payload to include with this directory request, along
1052  * with its length. Note that only an alias to <b>payload</b> is stored, so
1053  * the <b>payload</b> must outlive the request.
1054  */
1055 void
1057  const char *payload,
1058  size_t payload_len)
1059 {
1060  tor_assert(DIR_PURPOSE_IS_UPLOAD(req->dir_purpose));
1061 
1062  req->payload = payload;
1063  req->payload_len = payload_len;
1064 }
1065 /**
1066  * Set an if-modified-since date to send along with the request. The
1067  * default is 0 (meaning, send no if-modified-since header).
1068  */
1069 void
1071  time_t if_modified_since)
1072 {
1073  req->if_modified_since = if_modified_since;
1074 }
1075 
1076 /** Include a header of name <b>key</b> with content <b>val</b> in the
1077  * request. Neither may include newlines or other odd characters. Their
1078  * ordering is not currently guaranteed.
1079  *
1080  * Note that, as elsewhere in this module, header keys include a trailing
1081  * colon and space.
1082  */
1083 void
1085  const char *key,
1086  const char *val)
1087 {
1088  config_line_prepend(&req->additional_headers, key, val);
1089 }
1090 /**
1091  * Set an object containing HS data to be associated with this request. Note
1092  * that only an alias to <b>query</b> is stored, so the <b>query</b> object
1093  * must outlive the request.
1094  */
1095 void
1097  const rend_data_t *query)
1098 {
1099  if (query) {
1100  tor_assert(req->dir_purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 ||
1101  req->dir_purpose == DIR_PURPOSE_UPLOAD_RENDDESC_V2);
1102  }
1103  req->rend_query = query;
1104 }
1105 /**
1106  * Set an object containing HS connection identifier to be associated with
1107  * this request. Note that only an alias to <b>ident</b> is stored, so the
1108  * <b>ident</b> object must outlive the request.
1109  */
1110 void
1112  const hs_ident_dir_conn_t *ident)
1113 {
1114  if (ident) {
1115  tor_assert(req->dir_purpose == DIR_PURPOSE_UPLOAD_HSDESC);
1116  }
1117  req->hs_ident = ident;
1118 }
1119 /**
1120  * Set an object containing HS connection identifier to be associated with
1121  * this fetch request. Note that only an alias to <b>ident</b> is stored, so
1122  * the <b>ident</b> object must outlive the request.
1123  */
1124 void
1126  const hs_ident_dir_conn_t *ident)
1127 {
1128  if (ident) {
1129  tor_assert(req->dir_purpose == DIR_PURPOSE_FETCH_HSDESC);
1130  }
1131  req->hs_ident = ident;
1132 }
1133 /** Set a static circuit_guard_state_t object to affliate with the request in
1134  * <b>req</b>. This object will receive notification when the attempt to
1135  * connect to the guard either succeeds or fails. */
1136 void
1138  circuit_guard_state_t *state)
1139 {
1140  req->guard_state = state;
1141 }
1142 
1143 /**
1144  * Internal: Return true if any information for contacting the directory in
1145  * <b>req</b> has been set, other than by the routerstatus. */
1146 static int
1148 {
1149  /* We only check for ports here, since we don't use an addr unless the port
1150  * is set */
1151  return (req->or_addr_port.port ||
1152  req->dir_addr_port.port ||
1153  ! tor_digest_is_zero(req->digest));
1154 }
1155 
1156 /**
1157  * Set the routerstatus to use for the directory associated with this
1158  * request. If this option is set, then no other function to set the
1159  * directory's address or identity should be called.
1160  */
1161 void
1163  const routerstatus_t *status)
1164 {
1165  req->routerstatus = status;
1166 }
1167 /**
1168  * Helper: update the addresses, ports, and identities in <b>req</b>
1169  * from the routerstatus object in <b>req</b>. Return 0 on success.
1170  * On failure, warn and return -1.
1171  */
1172 static int
1174 
1175 {
1176  const routerstatus_t *status = req->routerstatus;
1177  if (BUG(status == NULL))
1178  return -1;
1179  const or_options_t *options = get_options();
1180  const node_t *node;
1181  tor_addr_port_t use_or_ap, use_dir_ap;
1182  const int anonymized_connection = dirind_is_anon(req->indirection);
1183 
1184  tor_assert(status != NULL);
1185 
1186  node = node_get_by_id(status->identity_digest);
1187 
1188  /* XXX The below check is wrong: !node means it's not in the consensus,
1189  * but we haven't checked if we have a descriptor for it -- and also,
1190  * we only care about the descriptor if it's a begindir-style anonymized
1191  * connection. */
1192  if (!node && anonymized_connection) {
1193  log_info(LD_DIR, "Not sending anonymized request to directory '%s'; we "
1194  "don't have its router descriptor.",
1195  routerstatus_describe(status));
1196  return -1;
1197  }
1198 
1199  if (options->ExcludeNodes && options->StrictNodes &&
1200  routerset_contains_routerstatus(options->ExcludeNodes, status, -1)) {
1201  log_warn(LD_DIR, "Wanted to contact directory mirror %s for %s, but "
1202  "it's in our ExcludedNodes list and StrictNodes is set. "
1203  "Skipping. This choice might make your Tor not work.",
1204  routerstatus_describe(status),
1205  dir_conn_purpose_to_string(req->dir_purpose));
1206  return -1;
1207  }
1208 
1209  /* At this point, if we are a client making a direct connection to a
1210  * directory server, we have selected a server that has at least one address
1211  * allowed by ClientUseIPv4/6 and Reachable{"",OR,Dir}Addresses. This
1212  * selection uses the preference in ClientPreferIPv6{OR,Dir}Port, if
1213  * possible. (If UseBridges is set, clients always use IPv6, and prefer it
1214  * by default.)
1215  *
1216  * Now choose an address that we can use to connect to the directory server.
1217  */
1218  if (directory_choose_address_routerstatus(status,
1219  req->indirection, &use_or_ap,
1220  &use_dir_ap) < 0) {
1221  return -1;
1222  }
1223 
1224  directory_request_set_or_addr_port(req, &use_or_ap);
1225  directory_request_set_dir_addr_port(req, &use_dir_ap);
1226  directory_request_set_directory_id_digest(req, status->identity_digest);
1227  return 0;
1228 }
1229 
1230 /**
1231  * Launch the provided directory request, configured in <b>request</b>.
1232  * After this function is called, you can free <b>request</b>.
1233  */
1234 MOCK_IMPL(void,
1236 {
1237  tor_assert(request);
1238  if (request->routerstatus) {
1239  tor_assert_nonfatal(
1242  return;
1243  }
1244  }
1245 
1246  const tor_addr_port_t *or_addr_port = &request->or_addr_port;
1247  const tor_addr_port_t *dir_addr_port = &request->dir_addr_port;
1248  const char *digest = request->digest;
1249  const uint8_t dir_purpose = request->dir_purpose;
1250  const uint8_t router_purpose = request->router_purpose;
1251  const dir_indirection_t indirection = request->indirection;
1252  const char *resource = request->resource;
1253  const rend_data_t *rend_query = request->rend_query;
1254  const hs_ident_dir_conn_t *hs_ident = request->hs_ident;
1255  circuit_guard_state_t *guard_state = request->guard_state;
1256 
1257  tor_assert(or_addr_port->port || dir_addr_port->port);
1258  tor_assert(digest);
1259 
1260  dir_connection_t *conn;
1261  const or_options_t *options = get_options();
1262  int socket_error = 0;
1263  const char *begindir_reason = NULL;
1264  /* Should the connection be to a relay's OR port (and inside that we will
1265  * send our directory request)? */
1266  const int use_begindir =
1267  directory_command_should_use_begindir(options, request, &begindir_reason);
1268 
1269  /* Will the connection go via a three-hop Tor circuit? Note that this
1270  * is separate from whether it will use_begindir. */
1271  const int anonymized_connection = dirind_is_anon(indirection);
1272 
1273  /* What is the address we want to make the directory request to? If
1274  * we're making a begindir request this is the ORPort of the relay
1275  * we're contacting; if not a begindir request, this is its DirPort.
1276  * Note that if anonymized_connection is true, we won't be initiating
1277  * a connection directly to this address. */
1278  tor_addr_t addr;
1279  tor_addr_copy(&addr, &(use_begindir ? or_addr_port : dir_addr_port)->addr);
1280  uint16_t port = (use_begindir ? or_addr_port : dir_addr_port)->port;
1281 
1282  log_debug(LD_DIR, "anonymized %d, use_begindir %d.",
1283  anonymized_connection, use_begindir);
1284 
1285  log_debug(LD_DIR, "Initiating %s", dir_conn_purpose_to_string(dir_purpose));
1286 
1287  if (purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
1288  tor_assert(anonymized_connection ||
1289  rend_non_anonymous_mode_enabled(options));
1290  }
1291 
1292  /* use encrypted begindir connections for everything except relays
1293  * this provides better protection for directory fetches */
1294  if (!use_begindir && dirclient_must_use_begindir(options)) {
1295  log_warn(LD_BUG, "Client could not use begindir connection: %s",
1296  begindir_reason ? begindir_reason : "(NULL)");
1297  return;
1298  }
1299 
1300  /* ensure that we don't make direct connections when a SOCKS server is
1301  * configured. */
1302  if (!anonymized_connection && !use_begindir && !options->HTTPProxy &&
1303  (options->Socks4Proxy || options->Socks5Proxy)) {
1304  log_warn(LD_DIR, "Cannot connect to a directory server through a "
1305  "SOCKS proxy!");
1306  return;
1307  }
1308 
1309  /* Make sure that the destination addr and port we picked is viable. */
1310  if (!port || tor_addr_is_null(&addr)) {
1311  static int logged_backtrace = 0;
1312  log_warn(LD_DIR,
1313  "Cannot make an outgoing %sconnection without a remote %sPort.",
1314  use_begindir ? "begindir " : "",
1315  use_begindir ? "OR" : "Dir");
1316  if (!logged_backtrace) {
1317  log_backtrace(LOG_INFO, LD_BUG, "Address came from");
1318  logged_backtrace = 1;
1319  }
1320  return;
1321  }
1322 
1323  conn = dir_connection_new(tor_addr_family(&addr));
1324 
1325  /* set up conn so it's got all the data we need to remember */
1326  tor_addr_copy(&conn->base_.addr, &addr);
1327  conn->base_.port = port;
1328  conn->base_.address = tor_addr_to_str_dup(&addr);
1329  memcpy(conn->identity_digest, digest, DIGEST_LEN);
1330 
1331  conn->base_.purpose = dir_purpose;
1332  conn->router_purpose = router_purpose;
1333 
1334  /* give it an initial state */
1335  conn->base_.state = DIR_CONN_STATE_CONNECTING;
1336 
1337  /* decide whether we can learn our IP address from this conn */
1338  /* XXXX This is a bad name for this field now. */
1339  conn->dirconn_direct = !anonymized_connection;
1340 
1341  /* copy rendezvous data, if any */
1342  if (rend_query) {
1343  /* We can't have both v2 and v3+ identifier. */
1344  tor_assert_nonfatal(!hs_ident);
1345  conn->rend_data = rend_data_dup(rend_query);
1346  }
1347  if (hs_ident) {
1348  /* We can't have both v2 and v3+ identifier. */
1349  tor_assert_nonfatal(!rend_query);
1350  conn->hs_ident = hs_ident_dir_conn_dup(hs_ident);
1351  }
1352 
1353  if (!anonymized_connection && !use_begindir) {
1354  /* then we want to connect to dirport directly */
1355 
1356  if (options->HTTPProxy) {
1357  tor_addr_copy(&addr, &options->HTTPProxyAddr);
1358  port = options->HTTPProxyPort;
1359  }
1360 
1361  // In this case we should not have picked a directory guard.
1362  if (BUG(guard_state)) {
1363  entry_guard_cancel(&guard_state);
1364  }
1365 
1366  switch (connection_connect(TO_CONN(conn), conn->base_.address, &addr,
1367  port, &socket_error)) {
1368  case -1:
1369  connection_mark_for_close(TO_CONN(conn));
1370  return;
1371  case 1:
1372  /* start flushing conn */
1373  conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
1374  FALLTHROUGH;
1375  case 0:
1376  /* queue the command on the outbuf */
1377  directory_send_command(conn, 1, request);
1379  /* writable indicates finish, readable indicates broken link,
1380  error indicates broken link in windowsland. */
1381  }
1382  } else {
1383  /* We will use a Tor circuit (maybe 1-hop, maybe 3-hop, maybe with
1384  * begindir, maybe not with begindir) */
1385 
1386  entry_connection_t *linked_conn;
1387 
1388  /* Anonymized tunneled connections can never share a circuit.
1389  * One-hop directory connections can share circuits with each other
1390  * but nothing else. */
1391  int iso_flags = anonymized_connection ? ISO_STREAM : ISO_SESSIONGRP;
1392 
1393  /* If it's an anonymized connection, remember the fact that we
1394  * wanted it for later: maybe we'll want it again soon. */
1395  if (anonymized_connection && use_begindir)
1396  rep_hist_note_used_internal(time(NULL), 0, 1);
1397  else if (anonymized_connection && !use_begindir)
1398  rep_hist_note_used_port(time(NULL), conn->base_.port);
1399 
1400  // In this case we should not have a directory guard; we'll
1401  // get a regular guard later when we build the circuit.
1402  if (BUG(anonymized_connection && guard_state)) {
1403  entry_guard_cancel(&guard_state);
1404  }
1405 
1406  conn->guard_state = guard_state;
1407 
1408  /* make an AP connection
1409  * populate it and add it at the right state
1410  * hook up both sides
1411  */
1412  linked_conn =
1414  conn->base_.address, conn->base_.port,
1415  digest,
1416  SESSION_GROUP_DIRCONN, iso_flags,
1417  use_begindir, !anonymized_connection);
1418  if (!linked_conn) {
1419  log_warn(LD_NET,"Making tunnel to dirserver failed.");
1420  connection_mark_for_close(TO_CONN(conn));
1421  return;
1422  }
1423 
1424  if (connection_add(TO_CONN(conn)) < 0) {
1425  log_warn(LD_NET,"Unable to add connection for link to dirserver.");
1426  connection_mark_for_close(TO_CONN(conn));
1427  return;
1428  }
1429  conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
1430  /* queue the command on the outbuf */
1431  directory_send_command(conn, 0, request);
1432 
1435  }
1436 }
1437 
1438 /** Helper for sorting
1439  *
1440  * sort strings alphabetically
1441  *
1442  * XXXX we have a smartlist_sort_strings() function, right?
1443  */
1444 static int
1445 compare_strs_(const void **a, const void **b)
1446 {
1447  const char *s1 = *a, *s2 = *b;
1448  return strcmp(s1, s2);
1449 }
1450 
1451 #define CONDITIONAL_CONSENSUS_FPR_LEN 3
1452 CTASSERT(CONDITIONAL_CONSENSUS_FPR_LEN <= DIGEST_LEN);
1453 
1454 /** Return the URL we should use for a consensus download.
1455  *
1456  * Use the "conditional consensus downloading" feature described in
1457  * dir-spec.txt, i.e.
1458  * GET .../consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>
1459  *
1460  * If 'resource' is provided, it is the name of a consensus flavor to request.
1461  */
1462 static char *
1463 directory_get_consensus_url(const char *resource)
1464 {
1465  char *url = NULL;
1466  const char *hyphen, *flavor;
1467  if (resource==NULL || strcmp(resource, "ns")==0) {
1468  flavor = ""; /* Request ns consensuses as "", so older servers will work*/
1469  hyphen = "";
1470  } else {
1471  flavor = resource;
1472  hyphen = "-";
1473  }
1474 
1475  {
1476  char *authority_id_list;
1477  smartlist_t *authority_digests = smartlist_new();
1478 
1479  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
1480  dir_server_t *, ds) {
1481  char *hex;
1482  if (!(ds->type & V3_DIRINFO))
1483  continue;
1484 
1485  hex = tor_malloc(2*CONDITIONAL_CONSENSUS_FPR_LEN+1);
1486  base16_encode(hex, 2*CONDITIONAL_CONSENSUS_FPR_LEN+1,
1487  ds->v3_identity_digest, CONDITIONAL_CONSENSUS_FPR_LEN);
1488  smartlist_add(authority_digests, hex);
1489  } SMARTLIST_FOREACH_END(ds);
1490  smartlist_sort(authority_digests, compare_strs_);
1491  authority_id_list = smartlist_join_strings(authority_digests,
1492  "+", 0, NULL);
1493 
1494  tor_asprintf(&url, "/tor/status-vote/current/consensus%s%s/%s.z",
1495  hyphen, flavor, authority_id_list);
1496 
1497  SMARTLIST_FOREACH(authority_digests, char *, cp, tor_free(cp));
1498  smartlist_free(authority_digests);
1499  tor_free(authority_id_list);
1500  }
1501  return url;
1502 }
1503 
1504 /**
1505  * Copies the ipv6 from source to destination, subject to buffer size limit
1506  * size. If decorate is true, makes sure the copied address is decorated.
1507  */
1508 static void
1509 copy_ipv6_address(char* destination, const char* source, size_t len,
1510  int decorate) {
1511  tor_assert(destination);
1512  tor_assert(source);
1513 
1514  if (decorate && source[0] != '[') {
1515  tor_snprintf(destination, len, "[%s]", source);
1516  } else {
1517  strlcpy(destination, source, len);
1518  }
1519 }
1520 
1521 /** Queue an appropriate HTTP command for <b>request</b> on
1522  * <b>conn</b>->outbuf. If <b>direct</b> is true, we're making a
1523  * non-anonymized connection to the dirport.
1524  */
1525 static void
1527  const int direct,
1528  const directory_request_t *req)
1529 {
1530  tor_assert(req);
1531  const int purpose = req->dir_purpose;
1532  const char *resource = req->resource;
1533  const char *payload = req->payload;
1534  const size_t payload_len = req->payload_len;
1535  const time_t if_modified_since = req->if_modified_since;
1536  const int anonymized_connection = dirind_is_anon(req->indirection);
1537 
1538  char proxystring[256];
1539  char hoststring[128];
1540  /* NEEDS to be the same size hoststring.
1541  Will be decorated with brackets around it if it is ipv6. */
1542  char decorated_address[128];
1543  smartlist_t *headers = smartlist_new();
1544  char *url;
1545  char *accept_encoding;
1546  size_t url_len;
1547  char request[8192];
1548  size_t request_len, total_request_len = 0;
1549  const char *httpcommand = NULL;
1550 
1551  tor_assert(conn);
1552  tor_assert(conn->base_.type == CONN_TYPE_DIR);
1553 
1555  if (resource)
1556  conn->requested_resource = tor_strdup(resource);
1557 
1558  /* decorate the ip address if it is ipv6 */
1559  if (strchr(conn->base_.address, ':')) {
1560  copy_ipv6_address(decorated_address, conn->base_.address,
1561  sizeof(decorated_address), 1);
1562  } else {
1563  strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
1564  }
1565 
1566  /* come up with a string for which Host: we want */
1567  if (conn->base_.port == 80) {
1568  strlcpy(hoststring, decorated_address, sizeof(hoststring));
1569  } else {
1570  tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
1571  decorated_address, conn->base_.port);
1572  }
1573 
1574  /* Format if-modified-since */
1575  if (if_modified_since) {
1576  char b[RFC1123_TIME_LEN+1];
1577  format_rfc1123_time(b, if_modified_since);
1578  smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
1579  }
1580 
1581  /* come up with some proxy lines, if we're using one. */
1582  if (direct && get_options()->HTTPProxy) {
1583  char *base64_authenticator=NULL;
1584  const char *authenticator = get_options()->HTTPProxyAuthenticator;
1585 
1586  tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
1587  if (authenticator) {
1588  base64_authenticator = alloc_http_authenticator(authenticator);
1589  if (!base64_authenticator)
1590  log_warn(LD_BUG, "Encoding http authenticator failed");
1591  }
1592  if (base64_authenticator) {
1593  smartlist_add_asprintf(headers,
1594  "Proxy-Authorization: Basic %s\r\n",
1595  base64_authenticator);
1596  tor_free(base64_authenticator);
1597  }
1598  } else {
1599  proxystring[0] = 0;
1600  }
1601 
1602  if (! anonymized_connection) {
1603  /* Add Accept-Encoding. */
1604  accept_encoding = accept_encoding_header();
1605  smartlist_add_asprintf(headers, "Accept-Encoding: %s\r\n",
1606  accept_encoding);
1607  tor_free(accept_encoding);
1608  }
1609 
1610  /* Add additional headers, if any */
1611  {
1612  config_line_t *h;
1613  for (h = req->additional_headers; h; h = h->next) {
1614  smartlist_add_asprintf(headers, "%s%s\r\n", h->key, h->value);
1615  }
1616  }
1617 
1618  switch (purpose) {
1620  /* resource is optional. If present, it's a flavor name */
1621  tor_assert(!payload);
1622  httpcommand = "GET";
1623  url = directory_get_consensus_url(resource);
1624  log_info(LD_DIR, "Downloading consensus from %s using %s",
1625  hoststring, url);
1626  break;
1628  tor_assert(resource);
1629  tor_assert(!payload);
1630  httpcommand = "GET";
1631  tor_asprintf(&url, "/tor/keys/%s", resource);
1632  break;
1634  tor_assert(resource);
1635  tor_assert(!payload);
1636  httpcommand = "GET";
1637  tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
1638  break;
1640  tor_assert(!resource);
1641  tor_assert(!payload);
1642  httpcommand = "GET";
1643  url = tor_strdup("/tor/status-vote/next/consensus-signatures.z");
1644  break;
1646  tor_assert(resource);
1647  httpcommand = "GET";
1648  tor_asprintf(&url, "/tor/server/%s", resource);
1649  break;
1651  tor_assert(resource);
1652  httpcommand = "GET";
1653  tor_asprintf(&url, "/tor/extra/%s", resource);
1654  break;
1656  tor_assert(resource);
1657  httpcommand = "GET";
1658  tor_asprintf(&url, "/tor/micro/%s", resource);
1659  break;
1660  case DIR_PURPOSE_UPLOAD_DIR: {
1661  const char *why = router_get_descriptor_gen_reason();
1662  tor_assert(!resource);
1663  tor_assert(payload);
1664  httpcommand = "POST";
1665  url = tor_strdup("/tor/");
1666  if (!why) {
1667  why = "for no reason at all";
1668  }
1669  smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
1670  break;
1671  }
1673  tor_assert(!resource);
1674  tor_assert(payload);
1675  httpcommand = "POST";
1676  url = tor_strdup("/tor/post/vote");
1677  break;
1679  tor_assert(!resource);
1680  tor_assert(payload);
1681  httpcommand = "POST";
1682  url = tor_strdup("/tor/post/consensus-signature");
1683  break;
1685  tor_assert(resource);
1686  tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32);
1687  tor_assert(!payload);
1688  httpcommand = "GET";
1689  tor_asprintf(&url, "/tor/rendezvous2/%s", resource);
1690  break;
1692  tor_assert(resource);
1693  tor_assert(strlen(resource) <= ED25519_BASE64_LEN);
1694  tor_assert(!payload);
1695  httpcommand = "GET";
1696  tor_asprintf(&url, "/tor/hs/3/%s", resource);
1697  break;
1699  tor_assert(!resource);
1700  tor_assert(payload);
1701  httpcommand = "POST";
1702  url = tor_strdup("/tor/rendezvous2/publish");
1703  break;
1705  tor_assert(resource);
1706  tor_assert(payload);
1707  httpcommand = "POST";
1708  tor_asprintf(&url, "/tor/hs/%s/publish", resource);
1709  break;
1710  default:
1711  tor_assert(0);
1712  return;
1713  }
1714 
1715  /* warn in the non-tunneled case */
1716  if (direct && (strlen(proxystring) + strlen(url) >= 4096)) {
1717  log_warn(LD_BUG,
1718  "Squid does not like URLs longer than 4095 bytes, and this "
1719  "one is %d bytes long: %s%s",
1720  (int)(strlen(proxystring) + strlen(url)), proxystring, url);
1721  }
1722 
1723  tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
1724 
1725  request_len = strlen(request);
1726  total_request_len += request_len;
1727  connection_buf_add(request, request_len, TO_CONN(conn));
1728 
1729  url_len = strlen(url);
1730  total_request_len += url_len;
1731  connection_buf_add(url, url_len, TO_CONN(conn));
1732  tor_free(url);
1733 
1734  if (!strcmp(httpcommand, "POST") || payload) {
1735  smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
1736  payload ? (unsigned long)payload_len : 0);
1737  }
1738 
1739  {
1740  char *header = smartlist_join_strings(headers, "", 0, NULL);
1741  tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
1742  hoststring, header);
1743  tor_free(header);
1744  }
1745 
1746  request_len = strlen(request);
1747  total_request_len += request_len;
1748  connection_buf_add(request, request_len, TO_CONN(conn));
1749 
1750  if (payload) {
1751  /* then send the payload afterwards too */
1752  connection_buf_add(payload, payload_len, TO_CONN(conn));
1753  total_request_len += payload_len;
1754  }
1755 
1756  SMARTLIST_FOREACH(headers, char *, h, tor_free(h));
1757  smartlist_free(headers);
1758 
1759  log_debug(LD_DIR,
1760  "Sent request to directory server %s "
1761  "(purpose: %d, request size: %"TOR_PRIuSZ", "
1762  "payload size: %"TOR_PRIuSZ")",
1764  conn->base_.purpose,
1765  (total_request_len),
1766  (payload ? payload_len : 0));
1767 }
1768 
1769 /** Return true iff <b>body</b> doesn't start with a plausible router or
1770  * network-status or microdescriptor opening. This is a sign of possible
1771  * compression. */
1772 static int
1773 body_is_plausible(const char *body, size_t len, int purpose)
1774 {
1775  int i;
1776  if (len == 0)
1777  return 1; /* empty bodies don't need decompression */
1778  if (len < 32)
1779  return 0;
1780  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
1781  return (!strcmpstart(body,"onion-key"));
1782  }
1783 
1784  if (!strcmpstart(body,"router") ||
1785  !strcmpstart(body,"network-status"))
1786  return 1;
1787  for (i=0;i<32;++i) {
1788  if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
1789  return 0;
1790  }
1791 
1792  return 1;
1793 }
1794 
1795 /** Called when we've just fetched a bunch of router descriptors in
1796  * <b>body</b>. The list <b>which</b>, if present, holds digests for
1797  * descriptors we requested: descriptor digests if <b>descriptor_digests</b>
1798  * is true, or identity digests otherwise. Parse the descriptors, validate
1799  * them, and annotate them as having purpose <b>purpose</b> and as having been
1800  * downloaded from <b>source</b>.
1801  *
1802  * Return the number of routers actually added. */
1803 static int
1804 load_downloaded_routers(const char *body, smartlist_t *which,
1805  int descriptor_digests,
1806  int router_purpose,
1807  const char *source)
1808 {
1809  char buf[256];
1810  char time_buf[ISO_TIME_LEN+1];
1811  int added = 0;
1812  int general = router_purpose == ROUTER_PURPOSE_GENERAL;
1813  format_iso_time(time_buf, time(NULL));
1814  tor_assert(source);
1815 
1816  if (tor_snprintf(buf, sizeof(buf),
1817  "@downloaded-at %s\n"
1818  "@source %s\n"
1819  "%s%s%s", time_buf, escaped(source),
1820  !general ? "@purpose " : "",
1821  !general ? router_purpose_to_string(router_purpose) : "",
1822  !general ? "\n" : "")<0)
1823  return added;
1824 
1825  added = router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
1826  descriptor_digests, buf);
1827  if (added && general)
1828  control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
1830  return added;
1831 }
1832 
1834  const response_handler_args_t *);
1836  const response_handler_args_t *);
1838  const response_handler_args_t *);
1840  const response_handler_args_t *);
1842  const response_handler_args_t *);
1844  const response_handler_args_t *);
1846  const response_handler_args_t *);
1848  const response_handler_args_t *);
1850  const response_handler_args_t *);
1852  const response_handler_args_t *);
1853 
1854 static int
1855 dir_client_decompress_response_body(char **bodyp, size_t *bodylenp,
1856  dir_connection_t *conn,
1857  compress_method_t compression,
1858  int anonymized_connection)
1859 {
1860  int rv = 0;
1861  const char *body = *bodyp;
1862  size_t body_len = *bodylenp;
1863  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
1864  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
1865  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
1866 
1867  int plausible = body_is_plausible(body, body_len, conn->base_.purpose);
1868 
1869  if (plausible && compression == NO_METHOD) {
1870  return 0;
1871  }
1872 
1873  int severity = LOG_DEBUG;
1874  char *new_body = NULL;
1875  size_t new_len = 0;
1876  const char *description1, *description2;
1877  int want_to_try_both = 0;
1878  int tried_both = 0;
1879  compress_method_t guessed = detect_compression_method(body, body_len);
1880 
1881  description1 = compression_method_get_human_name(compression);
1882 
1883  if (BUG(description1 == NULL))
1884  description1 = compression_method_get_human_name(UNKNOWN_METHOD);
1885 
1886  if (guessed == UNKNOWN_METHOD && !plausible)
1887  description2 = "confusing binary junk";
1888  else
1889  description2 = compression_method_get_human_name(guessed);
1890 
1891  /* Tell the user if we don't believe what we're told about compression.*/
1892  want_to_try_both = (compression == UNKNOWN_METHOD ||
1893  guessed != compression);
1894  if (want_to_try_both) {
1895  severity = LOG_PROTOCOL_WARN;
1896  }
1897 
1898  tor_log(severity, LD_HTTP,
1899  "HTTP body from %s was labeled as %s, "
1900  "%s it seems to be %s.%s",
1902  description1,
1903  guessed != compression?"but":"and",
1904  description2,
1905  (compression>0 && guessed>0 && want_to_try_both)?
1906  " Trying both.":"");
1907 
1908  /* Try declared compression first if we can.
1909  * tor_compress_supports_method() also returns true for NO_METHOD.
1910  * Ensure that the server is not sending us data compressed using a
1911  * compression method that is not allowed for anonymous connections. */
1912  if (anonymized_connection &&
1915  rv = -1;
1916  goto done;
1917  }
1918 
1919  if (tor_compress_supports_method(compression)) {
1920  tor_uncompress(&new_body, &new_len, body, body_len, compression,
1921  !allow_partial, LOG_PROTOCOL_WARN);
1922  if (new_body) {
1923  /* We succeeded with the declared compression method. Great! */
1924  rv = 0;
1925  goto done;
1926  }
1927  }
1928 
1929  /* Okay, if that didn't work, and we think that it was compressed
1930  * differently, try that. */
1931  if (anonymized_connection &&
1934  rv = -1;
1935  goto done;
1936  }
1937 
1938  if (tor_compress_supports_method(guessed) &&
1939  compression != guessed) {
1940  tor_uncompress(&new_body, &new_len, body, body_len, guessed,
1941  !allow_partial, LOG_INFO);
1942  tried_both = 1;
1943  }
1944  /* If we're pretty sure that we have a compressed directory, and
1945  * we didn't manage to uncompress it, then warn and bail. */
1946  if (!plausible && !new_body) {
1947  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
1948  "Unable to decompress HTTP body (tried %s%s%s, on %s).",
1949  description1,
1950  tried_both?" and ":"",
1951  tried_both?description2:"",
1952  connection_describe(TO_CONN(conn)));
1953  rv = -1;
1954  goto done;
1955  }
1956 
1957  done:
1958  if (new_body) {
1959  if (rv == 0) {
1960  /* success! */
1961  tor_free(*bodyp);
1962  *bodyp = new_body;
1963  *bodylenp = new_len;
1964  } else {
1965  tor_free(new_body);
1966  }
1967  }
1968 
1969  return rv;
1970 }
1971 
1972 /**
1973  * Total number of bytes downloaded of each directory purpose, when
1974  * bootstrapped, and when not bootstrapped.
1975  *
1976  * (For example, the number of bytes downloaded of purpose p while
1977  * not fully bootstrapped is total_dl[p][false].)
1978  **/
1979 static uint64_t total_dl[DIR_PURPOSE_MAX_][2];
1980 
1981 /**
1982  * Heartbeat: dump a summary of how many bytes of which purpose we've
1983  * downloaded, when bootstrapping and when not bootstrapping.
1984  **/
1985 void
1987 {
1988  const or_options_t *options = get_options();
1989  for (int bootstrapped = 0; bootstrapped < 2; ++bootstrapped) {
1990  bool first_time = true;
1991  for (int i=0; i < DIR_PURPOSE_MAX_; ++i) {
1992  uint64_t n = total_dl[i][bootstrapped];
1993  if (n == 0)
1994  continue;
1995  if (options->SafeLogging_ != SAFELOG_SCRUB_NONE &&
1997  continue;
1998  if (first_time) {
1999  log_notice(LD_NET,
2000  "While %sbootstrapping, fetched this many bytes: ",
2001  bootstrapped?"not ":"");
2002  first_time = false;
2003  }
2004  log_notice(LD_NET, " %"PRIu64" (%s)",
2006  }
2007  }
2008 }
2009 
2010 /** We are a client, and we've finished reading the server's
2011  * response. Parse it and act appropriately.
2012  *
2013  * If we're still happy with using this directory server in the future, return
2014  * 0. Otherwise return -1; and the caller should consider trying the request
2015  * again.
2016  *
2017  * The caller will take care of marking the connection for close.
2018  */
2019 static int
2021 {
2022  char *body = NULL;
2023  char *headers = NULL;
2024  char *reason = NULL;
2025  size_t body_len = 0;
2026  int status_code;
2027  time_t date_header = 0;
2028  long apparent_skew;
2029  compress_method_t compression;
2030  int skewed = 0;
2031  int rv;
2032  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
2033  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
2034  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
2035  size_t received_bytes;
2036  const int anonymized_connection =
2037  purpose_needs_anonymity(conn->base_.purpose,
2038  conn->router_purpose,
2039  conn->requested_resource);
2040 
2041  received_bytes = connection_get_inbuf_len(TO_CONN(conn));
2042 
2043  log_debug(LD_DIR, "Downloaded %"TOR_PRIuSZ" bytes on connection of purpose "
2044  "%s; bootstrap %d%%",
2045  received_bytes,
2046  dir_conn_purpose_to_string(conn->base_.purpose),
2047  control_get_bootstrap_percent());
2048  {
2049  bool bootstrapped = control_get_bootstrap_percent() == 100;
2050  total_dl[conn->base_.purpose][bootstrapped] += received_bytes;
2051  }
2052 
2054  &headers, MAX_HEADERS_SIZE,
2055  &body, &body_len, MAX_DIR_DL_SIZE,
2056  allow_partial)) {
2057  case -1: /* overflow */
2058  log_warn(LD_PROTOCOL,
2059  "'fetch' response too large (%s). Closing.",
2060  connection_describe(TO_CONN(conn)));
2061  return -1;
2062  case 0:
2063  log_info(LD_HTTP,
2064  "'fetch' response not all here, but we're at eof. Closing.");
2065  return -1;
2066  /* case 1, fall through */
2067  }
2068 
2069  if (parse_http_response(headers, &status_code, &date_header,
2070  &compression, &reason) < 0) {
2071  log_warn(LD_HTTP,"Unparseable headers (%s). Closing.",
2072  connection_describe(TO_CONN(conn)));
2073  rv = -1;
2074  goto done;
2075  }
2076  if (!reason) reason = tor_strdup("[no reason given]");
2077 
2079  "Received response on %s: %d %s "
2080  "(purpose: %d, response size: %"TOR_PRIuSZ
2081 #ifdef MEASUREMENTS_21206
2082  ", data cells received: %d, data cells sent: %d"
2083 #endif
2084  ", compression: %d)",
2086  status_code,
2087  escaped(reason), conn->base_.purpose,
2088  (received_bytes),
2089 #ifdef MEASUREMENTS_21206
2090  conn->data_cells_received, conn->data_cells_sent,
2091 #endif
2092  compression);
2093 
2094  if (conn->guard_state) {
2095  /* we count the connection as successful once we can read from it. We do
2096  * not, however, delay use of the circuit here, since it's just for a
2097  * one-hop directory request. */
2098  /* XXXXprop271 note that this will not do the right thing for other
2099  * waiting circuits that would be triggered by this circuit becoming
2100  * complete/usable. But that's ok, I think.
2101  */
2103  circuit_guard_state_free(conn->guard_state);
2104  conn->guard_state = NULL;
2105  }
2106 
2107  /* now check if it's got any hints for us about our IP address. */
2108  if (conn->dirconn_direct) {
2109  char *guess = http_get_header(headers, X_ADDRESS_HEADER);
2110  if (guess) {
2111  tor_addr_t addr;
2112  if (tor_addr_parse(&addr, guess) < 0) {
2113  log_debug(LD_DIR, "Malformed X-Your-Address-Is header %s. Ignoring.",
2114  escaped(guess));
2115  } else {
2116  relay_address_new_suggestion(&addr, &TO_CONN(conn)->addr, NULL);
2117  }
2118  tor_free(guess);
2119  }
2120  }
2121 
2122  if (date_header > 0) {
2123  /* The date header was written very soon after we sent our request,
2124  * so compute the skew as the difference between sending the request
2125  * and the date header. (We used to check now-date_header, but that's
2126  * inaccurate if we spend a lot of time downloading.)
2127  */
2128  apparent_skew = conn->base_.timestamp_last_write_allowed - date_header;
2129  if (labs(apparent_skew)>ALLOW_DIRECTORY_TIME_SKEW) {
2130  int trusted = router_digest_is_trusted_dir(conn->identity_digest);
2131  clock_skew_warning(TO_CONN(conn), apparent_skew, trusted, LD_HTTP,
2132  "directory", "DIRSERV");
2133  skewed = 1; /* don't check the recommended-versions line */
2134  } else {
2135  log_debug(LD_HTTP, "Time on received directory is within tolerance; "
2136  "we are %ld seconds skewed. (That's okay.)", apparent_skew);
2137  }
2138  }
2139  (void) skewed; /* skewed isn't used yet. */
2140 
2141  if (status_code == 503) {
2142  routerstatus_t *rs;
2143  dir_server_t *ds;
2144  const char *id_digest = conn->identity_digest;
2145  log_info(LD_DIR,"Received http status code %d (%s) from server "
2146  "%s. I'll try again soon.",
2147  status_code, escaped(reason),
2149  time_t now = approx_time();
2150  if ((rs = router_get_mutable_consensus_status_by_id(id_digest)))
2151  rs->last_dir_503_at = now;
2152  if ((ds = router_get_fallback_dirserver_by_digest(id_digest)))
2153  ds->fake_status.last_dir_503_at = now;
2154 
2155  rv = -1;
2156  goto done;
2157  }
2158 
2159  if (dir_client_decompress_response_body(&body, &body_len,
2160  conn, compression, anonymized_connection) < 0) {
2161  rv = -1;
2162  goto done;
2163  }
2164 
2165  response_handler_args_t args;
2166  memset(&args, 0, sizeof(args));
2167  args.status_code = status_code;
2168  args.reason = reason;
2169  args.body = body;
2170  args.body_len = body_len;
2171  args.headers = headers;
2172 
2173  switch (conn->base_.purpose) {
2175  rv = handle_response_fetch_consensus(conn, &args);
2176  break;
2178  rv = handle_response_fetch_certificate(conn, &args);
2179  break;
2181  rv = handle_response_fetch_status_vote(conn, &args);
2182  break;
2185  break;
2188  rv = handle_response_fetch_desc(conn, &args);
2189  break;
2191  rv = handle_response_fetch_microdesc(conn, &args);
2192  break;
2194  rv = handle_response_fetch_renddesc_v2(conn, &args);
2195  break;
2197  rv = handle_response_upload_dir(conn, &args);
2198  break;
2200  rv = handle_response_upload_signatures(conn, &args);
2201  break;
2203  rv = handle_response_upload_vote(conn, &args);
2204  break;
2206  rv = handle_response_upload_renddesc_v2(conn, &args);
2207  break;
2209  rv = handle_response_upload_hsdesc(conn, &args);
2210  break;
2212  rv = handle_response_fetch_hsdesc_v3(conn, &args);
2213  break;
2214  default:
2216  rv = -1;
2217  break;
2218  }
2219 
2220  done:
2221  tor_free(body);
2222  tor_free(headers);
2223  tor_free(reason);
2224  return rv;
2225 }
2226 
2227 /**
2228  * Handler function: processes a response to a request for a networkstatus
2229  * consensus document by checking the consensus, storing it, and marking
2230  * router requests as reachable.
2231  **/
2232 STATIC int
2234  const response_handler_args_t *args)
2235 {
2237  const int status_code = args->status_code;
2238  const char *body = args->body;
2239  const size_t body_len = args->body_len;
2240  const char *reason = args->reason;
2241  const time_t now = approx_time();
2242 
2243  const char *consensus;
2244  char *new_consensus = NULL;
2245  const char *sourcename;
2246 
2247  int r;
2248  const char *flavname = conn->requested_resource;
2249  if (status_code != 200) {
2250  int severity = (status_code == 304) ? LOG_INFO : LOG_WARN;
2251  tor_log(severity, LD_DIR,
2252  "Received http status code %d (%s) from server "
2253  "%s while fetching consensus directory.",
2254  status_code, escaped(reason),
2256  networkstatus_consensus_download_failed(status_code, flavname);
2257  return -1;
2258  }
2259 
2260  if (looks_like_a_consensus_diff(body, body_len)) {
2261  /* First find our previous consensus. Maybe it's in ram, maybe not. */
2262  cached_dir_t *cd = dirserv_get_consensus(flavname);
2263  const char *consensus_body = NULL;
2264  size_t consensus_body_len;
2265  tor_mmap_t *mapped_consensus = NULL;
2266  if (cd) {
2267  consensus_body = cd->dir;
2268  consensus_body_len = cd->dir_len;
2269  } else {
2270  mapped_consensus = networkstatus_map_cached_consensus(flavname);
2271  if (mapped_consensus) {
2272  consensus_body = mapped_consensus->data;
2273  consensus_body_len = mapped_consensus->size;
2274  }
2275  }
2276  if (!consensus_body) {
2277  log_warn(LD_DIR, "Received a consensus diff, but we can't find "
2278  "any %s-flavored consensus in our current cache.",flavname);
2279  tor_munmap_file(mapped_consensus);
2281  // XXXX if this happens too much, see below
2282  return -1;
2283  }
2284 
2285  new_consensus = consensus_diff_apply(consensus_body, consensus_body_len,
2286  body, body_len);
2287  tor_munmap_file(mapped_consensus);
2288  if (new_consensus == NULL) {
2289  log_warn(LD_DIR, "Could not apply consensus diff received from server "
2290  "%s", connection_describe_peer(TO_CONN(conn)));
2291  // XXXX If this happens too many times, we should maybe not use
2292  // XXXX this directory for diffs any more?
2294  return -1;
2295  }
2296  log_info(LD_DIR, "Applied consensus diff (size %d) from server "
2297  "%s, resulting in a new consensus document (size %d).",
2298  (int)body_len, connection_describe_peer(TO_CONN(conn)),
2299  (int)strlen(new_consensus));
2300  consensus = new_consensus;
2301  sourcename = "generated based on a diff";
2302  } else {
2303  log_info(LD_DIR,"Received consensus directory (body size %d) from server "
2304  "%s", (int)body_len, connection_describe_peer(TO_CONN(conn)));
2305  consensus = body;
2306  sourcename = "downloaded";
2307  }
2308 
2309  if ((r=networkstatus_set_current_consensus(consensus,
2310  strlen(consensus),
2311  flavname, 0,
2312  conn->identity_digest))<0) {
2314  "Unable to load %s consensus directory %s from "
2315  "server %s. I'll try again soon.",
2316  flavname, sourcename,
2319  tor_free(new_consensus);
2320  return -1;
2321  }
2322 
2323  /* If we launched other fetches for this consensus, cancel them. */
2324  connection_dir_close_consensus_fetches(conn, flavname);
2325 
2326  /* update the list of routers and directory guards */
2329  directory_info_has_arrived(now, 0, 0);
2330 
2331  if (authdir_mode_v3(get_options())) {
2334  }
2335  log_info(LD_DIR, "Successfully loaded consensus.");
2336 
2337  tor_free(new_consensus);
2338  return 0;
2339 }
2340 
2341 /**
2342  * Handler function: processes a response to a request for one or more
2343  * authority certificates
2344  **/
2345 static int
2347  const response_handler_args_t *args)
2348 {
2350  const int status_code = args->status_code;
2351  const char *reason = args->reason;
2352  const char *body = args->body;
2353  const size_t body_len = args->body_len;
2354 
2355  if (status_code != 200) {
2356  log_warn(LD_DIR,
2357  "Received http status code %d (%s) from server "
2358  "%s while fetching \"/tor/keys/%s\".",
2359  status_code, escaped(reason),
2361  conn->requested_resource);
2362  connection_dir_download_cert_failed(conn, status_code);
2363  return -1;
2364  }
2365  log_info(LD_DIR,"Received authority certificates (body size %d) from "
2366  "server %s",
2367  (int)body_len, connection_describe_peer(TO_CONN(conn)));
2368 
2369  /*
2370  * Tell trusted_dirs_load_certs_from_string() whether it was by fp
2371  * or fp-sk pair.
2372  */
2373  int src_code = -1;
2374  if (!strcmpstart(conn->requested_resource, "fp/")) {
2375  src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST;
2376  } else if (!strcmpstart(conn->requested_resource, "fp-sk/")) {
2377  src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST;
2378  }
2379 
2380  if (src_code != -1) {
2381  if (trusted_dirs_load_certs_from_string(body, src_code, 1,
2382  conn->identity_digest)<0) {
2383  log_warn(LD_DIR, "Unable to parse fetched certificates");
2384  /* if we fetched more than one and only some failed, the successful
2385  * ones got flushed to disk so it's safe to call this on them */
2386  connection_dir_download_cert_failed(conn, status_code);
2387  } else {
2388  time_t now = approx_time();
2389  directory_info_has_arrived(now, 0, 0);
2390  log_info(LD_DIR, "Successfully loaded certificates from fetch.");
2391  }
2392  } else {
2393  log_warn(LD_DIR,
2394  "Couldn't figure out what to do with fetched certificates for "
2395  "unknown resource %s",
2396  conn->requested_resource);
2397  connection_dir_download_cert_failed(conn, status_code);
2398  }
2399  return 0;
2400 }
2401 
2402 /**
2403  * Handler function: processes a response to a request for an authority's
2404  * current networkstatus vote.
2405  **/
2406 static int
2408  const response_handler_args_t *args)
2409 {
2411  const int status_code = args->status_code;
2412  const char *reason = args->reason;
2413  const char *body = args->body;
2414  const size_t body_len = args->body_len;
2415 
2416  const char *msg;
2417  int st;
2418  log_info(LD_DIR,"Got votes (body size %d) from server %s",
2419  (int)body_len, connection_describe_peer(TO_CONN(conn)));
2420  if (status_code != 200) {
2421  log_warn(LD_DIR,
2422  "Received http status code %d (%s) from server "
2423  "%s while fetching \"/tor/status-vote/next/%s.z\".",
2424  status_code, escaped(reason),
2426  conn->requested_resource);
2427  return -1;
2428  }
2429  dirvote_add_vote(body, 0, &msg, &st);
2430  if (st > 299) {
2431  log_warn(LD_DIR, "Error adding retrieved vote: %s", msg);
2432  } else {
2433  log_info(LD_DIR, "Added vote(s) successfully [msg: %s]", msg);
2434  }
2435 
2436  return 0;
2437 }
2438 
2439 /**
2440  * Handler function: processes a response to a request for the signatures
2441  * that an authority knows about on a given consensus.
2442  **/
2443 static int
2445  const response_handler_args_t *args)
2446 {
2448  const int status_code = args->status_code;
2449  const char *reason = args->reason;
2450  const char *body = args->body;
2451  const size_t body_len = args->body_len;
2452 
2453  const char *msg = NULL;
2454  log_info(LD_DIR,"Got detached signatures (body size %d) from server %s",
2455  (int)body_len,
2457  if (status_code != 200) {
2458  log_warn(LD_DIR,
2459  "Received http status code %d (%s) from server %s while fetching "
2460  "\"/tor/status-vote/next/consensus-signatures.z\".",
2461  status_code, escaped(reason),
2463  return -1;
2464  }
2465  if (dirvote_add_signatures(body, conn->base_.address, &msg)<0) {
2466  log_warn(LD_DIR, "Problem adding detached signatures from %s: %s",
2468  msg?msg:"???");
2469  }
2470 
2471  return 0;
2472 }
2473 
2474 /**
2475  * Handler function: processes a response to a request for a group of server
2476  * descriptors or an extrainfo documents.
2477  **/
2478 static int
2480  const response_handler_args_t *args)
2481 {
2483  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO);
2484  const int status_code = args->status_code;
2485  const char *reason = args->reason;
2486  const char *body = args->body;
2487  const size_t body_len = args->body_len;
2488 
2489  int was_ei = conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
2490  smartlist_t *which = NULL;
2491  int n_asked_for = 0;
2492  int descriptor_digests = conn->requested_resource &&
2493  !strcmpstart(conn->requested_resource,"d/");
2494  log_info(LD_DIR,"Received %s (body size %d) from server %s",
2495  was_ei ? "extra server info" : "server info",
2496  (int)body_len, connection_describe_peer(TO_CONN(conn)));
2497  if (conn->requested_resource &&
2498  (!strcmpstart(conn->requested_resource,"d/") ||
2499  !strcmpstart(conn->requested_resource,"fp/"))) {
2500  which = smartlist_new();
2502  (descriptor_digests ? 2 : 3),
2503  which, NULL, 0);
2504  n_asked_for = smartlist_len(which);
2505  }
2506  if (status_code != 200) {
2507  int dir_okay = status_code == 404 ||
2508  (status_code == 400 && !strcmp(reason, "Servers unavailable."));
2509  /* 404 means that it didn't have them; no big deal.
2510  * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead. */
2511  log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
2512  "Received http status code %d (%s) from server %s "
2513  "while fetching \"/tor/server/%s\". I'll try again soon.",
2514  status_code, escaped(reason),
2516  conn->requested_resource);
2517  if (!which) {
2519  } else {
2520  dir_routerdesc_download_failed(which, status_code,
2521  conn->router_purpose,
2522  was_ei, descriptor_digests);
2523  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2524  smartlist_free(which);
2525  }
2526  return dir_okay ? 0 : -1;
2527  }
2528  /* Learn the routers, assuming we requested by fingerprint or "all"
2529  * or "authority".
2530  *
2531  * We use "authority" to fetch our own descriptor for
2532  * testing, and to fetch bridge descriptors for bootstrapping. Ignore
2533  * the output of "authority" requests unless we are using bridges,
2534  * since otherwise they'll be the response from reachability tests,
2535  * and we don't really want to add that to our routerlist. */
2536  if (which || (conn->requested_resource &&
2537  (!strcmpstart(conn->requested_resource, "all") ||
2538  (!strcmpstart(conn->requested_resource, "authority") &&
2539  get_options()->UseBridges)))) {
2540  /* as we learn from them, we remove them from 'which' */
2541  if (was_ei) {
2543  descriptor_digests);
2544  } else {
2545  //router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
2546  // descriptor_digests, conn->router_purpose);
2547  if (load_downloaded_routers(body, which, descriptor_digests,
2548  conn->router_purpose,
2549  conn->base_.address)) {
2550  time_t now = approx_time();
2551  directory_info_has_arrived(now, 0, 1);
2552  }
2553  }
2554  }
2555  if (which) { /* mark remaining ones as failed */
2556  log_info(LD_DIR, "Received %d/%d %s requested from %s",
2557  n_asked_for-smartlist_len(which), n_asked_for,
2558  was_ei ? "extra-info documents" : "router descriptors",
2560  if (smartlist_len(which)) {
2561  dir_routerdesc_download_failed(which, status_code,
2562  conn->router_purpose,
2563  was_ei, descriptor_digests);
2564  }
2565  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2566  smartlist_free(which);
2567  }
2570 
2571  return 0;
2572 }
2573 
2574 /**
2575  * Handler function: processes a response to a request for a group of
2576  * microdescriptors
2577  **/
2578 STATIC int
2580  const response_handler_args_t *args)
2581 {
2583  const int status_code = args->status_code;
2584  const char *reason = args->reason;
2585  const char *body = args->body;
2586  const size_t body_len = args->body_len;
2587 
2588  smartlist_t *which = NULL;
2589  log_info(LD_DIR,"Received answer to microdescriptor request (status %d, "
2590  "body size %d) from server %s",
2591  status_code, (int)body_len,
2594  !strcmpstart(conn->requested_resource, "d/"));
2595  tor_assert_nonfatal(!fast_mem_is_zero(conn->identity_digest, DIGEST_LEN));
2596  which = smartlist_new();
2598  which, NULL,
2599  DSR_DIGEST256|DSR_BASE64);
2600  if (status_code != 200) {
2601  log_info(LD_DIR, "Received status code %d (%s) from server "
2602  "%s while fetching \"/tor/micro/%s\". I'll try again "
2603  "soon.",
2604  status_code, escaped(reason),
2606  conn->requested_resource);
2607  dir_microdesc_download_failed(which, status_code, conn->identity_digest);
2608  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2609  smartlist_free(which);
2610  return 0;
2611  } else {
2612  smartlist_t *mds;
2613  time_t now = approx_time();
2615  body, body+body_len, SAVED_NOWHERE, 0,
2616  now, which);
2617  if (smartlist_len(which)) {
2618  /* Mark remaining ones as failed. */
2619  dir_microdesc_download_failed(which, status_code, conn->identity_digest);
2620  }
2621  if (mds && smartlist_len(mds)) {
2622  control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
2624  directory_info_has_arrived(now, 0, 1);
2625  }
2626  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2627  smartlist_free(which);
2628  smartlist_free(mds);
2629  }
2630 
2631  return 0;
2632 }
2633 
2634 /**
2635  * Handler function: processes a response to a POST request to upload our
2636  * router descriptor.
2637  **/
2638 static int
2640  const response_handler_args_t *args)
2641 {
2642  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_DIR);
2643  const int status_code = args->status_code;
2644  const char *reason = args->reason;
2645  const char *headers = args->headers;
2646 
2647  switch (status_code) {
2648  case 200: {
2649  dir_server_t *ds =
2651  char *rejected_hdr = http_get_header(headers,
2652  "X-Descriptor-Not-New: ");
2653  if (rejected_hdr) {
2654  if (!strcmp(rejected_hdr, "Yes")) {
2655  log_info(LD_GENERAL,
2656  "Authority '%s' declined our descriptor (not new)",
2657  ds->nickname);
2658  /* XXXX use this information; be sure to upload next one
2659  * sooner. -NM */
2660  /* XXXX++ On further thought, the task above implies that we're
2661  * basing our regenerate-descriptor time on when we uploaded the
2662  * last descriptor, not on the published time of the last
2663  * descriptor. If those are different, that's a bad thing to
2664  * do. -NM */
2665  }
2666  tor_free(rejected_hdr);
2667  }
2668  log_info(LD_GENERAL,"eof (status 200) after uploading server "
2669  "descriptor: finished.");
2671  LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
2672  conn->base_.address, conn->base_.port);
2673 
2674  ds->has_accepted_serverdesc = 1;
2676  control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
2677  }
2678  break;
2679  case 400:
2680  log_warn(LD_GENERAL,"http status 400 (%s) response from "
2681  "dirserver %s. Please correct.",
2682  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2684  "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
2685  conn->base_.address, conn->base_.port, escaped(reason));
2686  break;
2687  default:
2688  log_warn(LD_GENERAL,
2689  "HTTP status %d (%s) was unexpected while uploading "
2690  "descriptor to server %s'. Possibly the server is "
2691  "misconfigured?",
2692  status_code, escaped(reason),
2694  break;
2695  }
2696  /* return 0 in all cases, since we don't want to mark any
2697  * dirservers down just because they don't like us. */
2698 
2699  return 0;
2700 }
2701 
2702 /**
2703  * Handler function: processes a response to POST request to upload our
2704  * own networkstatus vote.
2705  **/
2706 static int
2708  const response_handler_args_t *args)
2709 {
2710  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE);
2711  const int status_code = args->status_code;
2712  const char *reason = args->reason;
2713 
2714  switch (status_code) {
2715  case 200: {
2716  log_notice(LD_DIR,"Uploaded a vote to dirserver %s",
2718  }
2719  break;
2720  case 400:
2721  log_warn(LD_DIR,"http status 400 (%s) response after uploading "
2722  "vote to dirserver %s. Please correct.",
2723  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2724  break;
2725  default:
2726  log_warn(LD_GENERAL,
2727  "HTTP status %d (%s) was unexpected while uploading "
2728  "vote to server %s.",
2729  status_code, escaped(reason),
2731  break;
2732  }
2733  /* return 0 in all cases, since we don't want to mark any
2734  * dirservers down just because they don't like us. */
2735  return 0;
2736 }
2737 
2738 /**
2739  * Handler function: processes a response to POST request to upload our
2740  * view of the signatures on the current consensus.
2741  **/
2742 static int
2744  const response_handler_args_t *args)
2745 {
2747  const int status_code = args->status_code;
2748  const char *reason = args->reason;
2749 
2750  switch (status_code) {
2751  case 200: {
2752  log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s",
2754  }
2755  break;
2756  case 400:
2757  log_warn(LD_DIR,"http status 400 (%s) response after uploading "
2758  "signatures to dirserver %s. Please correct.",
2759  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2760  break;
2761  default:
2762  log_warn(LD_GENERAL,
2763  "HTTP status %d (%s) was unexpected while uploading "
2764  "signatures to server %s.",
2765  status_code, escaped(reason),
2767  break;
2768  }
2769  /* return 0 in all cases, since we don't want to mark any
2770  * dirservers down just because they don't like us. */
2771 
2772  return 0;
2773 }
2774 
2775 /**
2776  * Handler function: processes a response to a request for a v3 hidden service
2777  * descriptor.
2778  **/
2779 STATIC int
2781  const response_handler_args_t *args)
2782 {
2783  const int status_code = args->status_code;
2784  const char *reason = args->reason;
2785  const char *body = args->body;
2786  const size_t body_len = args->body_len;
2787 
2788  tor_assert(conn->hs_ident);
2789 
2790  log_info(LD_REND,"Received v3 hsdesc (body size %d, status %d (%s))",
2791  (int)body_len, status_code, escaped(reason));
2792 
2793  hs_client_dir_fetch_done(conn, reason, body, status_code);
2794  return 0;
2795 }
2796 
2797 /**
2798  * Handler function: processes a response to a request for a v2 hidden service
2799  * descriptor.
2800  **/
2801 static int
2803  const response_handler_args_t *args)
2804 {
2806  const int status_code = args->status_code;
2807  const char *reason = args->reason;
2808  const char *body = args->body;
2809  const size_t body_len = args->body_len;
2810 
2811 #define SEND_HS_DESC_FAILED_EVENT(reason) \
2812  (control_event_hsv2_descriptor_failed(conn->rend_data, \
2813  conn->identity_digest, \
2814  reason))
2815 #define SEND_HS_DESC_FAILED_CONTENT() \
2816  (control_event_hs_descriptor_content( \
2817  rend_data_get_address(conn->rend_data), \
2818  conn->requested_resource, \
2819  conn->identity_digest, \
2820  NULL))
2821 
2822  tor_assert(conn->rend_data);
2823  log_info(LD_REND,"Received rendezvous descriptor (body size %d, status %d "
2824  "(%s))",
2825  (int)body_len, status_code, escaped(reason));
2826  switch (status_code) {
2827  case 200:
2828  {
2829  rend_cache_entry_t *entry = NULL;
2830 
2832  conn->requested_resource,
2833  conn->rend_data, &entry) < 0) {
2834  log_warn(LD_REND,"Fetching v2 rendezvous descriptor failed. "
2835  "Retrying at another directory.");
2836  /* We'll retry when connection_about_to_close_connection()
2837  * cleans this dir conn up. */
2838  SEND_HS_DESC_FAILED_EVENT("BAD_DESC");
2839  SEND_HS_DESC_FAILED_CONTENT();
2840  } else {
2841  char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
2842  /* Should never be NULL here if we found the descriptor. */
2843  tor_assert(entry);
2844  rend_get_service_id(entry->parsed->pk, service_id);
2845 
2846  /* success. notify pending connections about this. */
2847  log_info(LD_REND, "Successfully fetched v2 rendezvous "
2848  "descriptor.");
2850  conn->rend_data,
2851  conn->identity_digest);
2853  conn->requested_resource,
2854  conn->identity_digest,
2855  body);
2857  rend_client_desc_trynow(service_id);
2858  memwipe(service_id, 0, sizeof(service_id));
2859  }
2860  break;
2861  }
2862  case 404:
2863  /* Not there. We'll retry when
2864  * connection_about_to_close_connection() cleans this conn up. */
2865  log_info(LD_REND,"Fetching v2 rendezvous descriptor failed: "
2866  "Retrying at another directory.");
2867  SEND_HS_DESC_FAILED_EVENT("NOT_FOUND");
2868  SEND_HS_DESC_FAILED_CONTENT();
2869  break;
2870  case 400:
2871  log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
2872  "http status 400 (%s). Dirserver didn't like our "
2873  "v2 rendezvous query? Retrying at another directory.",
2874  escaped(reason));
2875  SEND_HS_DESC_FAILED_EVENT("QUERY_REJECTED");
2876  SEND_HS_DESC_FAILED_CONTENT();
2877  break;
2878  default:
2879  log_warn(LD_REND, "Fetching v2 rendezvous descriptor failed: "
2880  "http status %d (%s) response unexpected while "
2881  "fetching v2 hidden service descriptor (server %s). "
2882  "Retrying at another directory.",
2883  status_code, escaped(reason),
2885  SEND_HS_DESC_FAILED_EVENT("UNEXPECTED");
2886  SEND_HS_DESC_FAILED_CONTENT();
2887  break;
2888  }
2889 
2890  return 0;
2891 }
2892 
2893 /**
2894  * Handler function: processes a response to a POST request to upload a v2
2895  * hidden service descriptor.
2896  **/
2897 static int
2899  const response_handler_args_t *args)
2900 {
2902  const int status_code = args->status_code;
2903  const char *reason = args->reason;
2904 
2905 #define SEND_HS_DESC_UPLOAD_FAILED_EVENT(reason) \
2906  (control_event_hs_descriptor_upload_failed( \
2907  conn->identity_digest, \
2908  rend_data_get_address(conn->rend_data), \
2909  reason))
2910 
2911  log_info(LD_REND,"Uploaded rendezvous descriptor (status %d "
2912  "(%s))",
2913  status_code, escaped(reason));
2914  /* Without the rend data, we'll have a problem identifying what has been
2915  * uploaded for which service. */
2916  tor_assert(conn->rend_data);
2917  switch (status_code) {
2918  case 200:
2919  log_info(LD_REND,
2920  "Uploading rendezvous descriptor: finished with status "
2921  "200 (%s)", escaped(reason));
2925  break;
2926  case 400:
2927  log_warn(LD_REND,"http status 400 (%s) response from dirserver "
2928  "%s. Malformed rendezvous descriptor?",
2929  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2930  SEND_HS_DESC_UPLOAD_FAILED_EVENT("UPLOAD_REJECTED");
2931  break;
2932  default:
2933  log_warn(LD_REND,"http status %d (%s) response unexpected (server "
2934  "%s).",
2935  status_code, escaped(reason),
2937  SEND_HS_DESC_UPLOAD_FAILED_EVENT("UNEXPECTED");
2938  break;
2939  }
2940 
2941  return 0;
2942 }
2943 
2944 /**
2945  * Handler function: processes a response to a POST request to upload an
2946  * hidden service descriptor.
2947  **/
2948 static int
2950  const response_handler_args_t *args)
2951 {
2952  const int status_code = args->status_code;
2953  const char *reason = args->reason;
2954 
2955  tor_assert(conn);
2957 
2958  log_info(LD_REND, "Uploaded hidden service descriptor (status %d "
2959  "(%s))",
2960  status_code, escaped(reason));
2961  /* For this directory response, it MUST have an hidden service identifier on
2962  * this connection. */
2963  tor_assert(conn->hs_ident);
2964  switch (status_code) {
2965  case 200:
2966  log_info(LD_REND, "Uploading hidden service descriptor: "
2967  "finished with status 200 (%s)", escaped(reason));
2968  hs_control_desc_event_uploaded(conn->hs_ident, conn->identity_digest);
2969  break;
2970  case 400:
2971  log_fn(LOG_PROTOCOL_WARN, LD_REND,
2972  "Uploading hidden service descriptor: http "
2973  "status 400 (%s) response from dirserver "
2974  "%s. Malformed hidden service descriptor?",
2975  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2976  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2977  "UPLOAD_REJECTED");
2978  break;
2979  default:
2980  log_warn(LD_REND, "Uploading hidden service descriptor: http "
2981  "status %d (%s) response unexpected (server "
2982  "%s').",
2983  status_code, escaped(reason),
2985  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2986  "UNEXPECTED");
2987  break;
2988  }
2989 
2990  return 0;
2991 }
2992 
2993 /** Called when a directory connection reaches EOF. */
2994 int
2996 {
2997  int retval;
2998  if (conn->base_.state != DIR_CONN_STATE_CLIENT_READING) {
2999  log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
3000  conn->base_.state);
3001  connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
3002  connection_mark_for_close(TO_CONN(conn));
3003  return -1;
3004  }
3005 
3006  retval = connection_dir_client_reached_eof(conn);
3007  if (retval == 0) /* success */
3008  conn->base_.state = DIR_CONN_STATE_CLIENT_FINISHED;
3009  connection_mark_for_close(TO_CONN(conn));
3010  return retval;
3011 }
3012 /** We are closing a dir connection: If <b>dir_conn</b> is a dir connection
3013  * that tried to fetch an HS descriptor, check if it successfully fetched it,
3014  * or if we need to try again. */
3015 void
3017 {
3018  connection_t *conn = TO_CONN(dir_conn);
3019 
3020  /* If we were trying to fetch a v2 rend desc and did not succeed, retry as
3021  * needed. (If a fetch is successful, the connection state is changed to
3022  * DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2 or DIR_PURPOSE_HAS_FETCHED_HSDESC to
3023  * mark that refetching is unnecessary.) */
3024  if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
3025  dir_conn->rend_data &&
3027  rend_data_get_address(dir_conn->rend_data))) {
3029  }
3030 
3031  /* Check for v3 rend desc fetch */
3032  if (conn->purpose == DIR_PURPOSE_FETCH_HSDESC &&
3033  dir_conn->hs_ident &&
3034  !ed25519_public_key_is_zero(&dir_conn->hs_ident->identity_pk)) {
3035  hs_client_refetch_hsdesc(&dir_conn->hs_ident->identity_pk);
3036  }
3037 }
3038 
3039 /** Array of compression methods to use (if supported) for requesting
3040  * compressed data, ordered from best to worst. */
3042  LZMA_METHOD,
3043  ZSTD_METHOD,
3044  ZLIB_METHOD,
3045  GZIP_METHOD,
3046  NO_METHOD
3047 };
3048 
3049 /** Array of allowed compression methods to use (if supported) when receiving a
3050  * response from a request that was required to be anonymous. */
3052  ZLIB_METHOD,
3053  GZIP_METHOD,
3054  NO_METHOD
3055 };
3056 
3057 /** Return a newly allocated string containing a comma separated list of
3058  * supported encodings. */
3059 STATIC char *
3061 {
3062  smartlist_t *methods = smartlist_new();
3063  char *header = NULL;
3064  compress_method_t method;
3065  unsigned i;
3066 
3067  for (i = 0; i < ARRAY_LENGTH(client_meth_pref); ++i) {
3068  method = client_meth_pref[i];
3069  if (tor_compress_supports_method(method))
3070  smartlist_add(methods, (char *)compression_method_get_name(method));
3071  }
3072 
3073  header = smartlist_join_strings(methods, ", ", 0, NULL);
3074  smartlist_free(methods);
3075 
3076  return header;
3077 }
3078 
3079 /** Check if the given compression method is allowed for a connection that is
3080  * supposed to be anonymous. Returns 1 if the compression method is allowed,
3081  * otherwise 0. */
3082 STATIC int
3084 {
3085  unsigned u;
3086 
3088  ++u) {
3089  compress_method_t allowed_method =
3091 
3092  if (! tor_compress_supports_method(allowed_method))
3093  continue;
3094 
3095  if (method == allowed_method)
3096  return 1;
3097  }
3098 
3099  return 0;
3100 }
3101 
3102 /** Log a warning when a remote server has sent us a document using a
3103  * compression method that is not allowed for anonymous directory requests. */
3104 STATIC void
3106 {
3107  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
3108  "Received a %s HTTP response, which is not "
3109  "allowed for anonymous directory requests.",
3111 }
3112 
3113 /* We just got a new consensus! If there are other in-progress requests
3114  * for this consensus flavor (for example because we launched several in
3115  * parallel), cancel them.
3116  *
3117  * We do this check here (not just in
3118  * connection_ap_handshake_attach_circuit()) to handle the edge case where
3119  * a consensus fetch begins and ends before some other one tries to attach to
3120  * a circuit, in which case the other one won't know that we're all happy now.
3121  *
3122  * Don't mark the conn that just gave us the consensus -- otherwise we
3123  * would end up double-marking it when it cleans itself up.
3124  */
3125 static void
3126 connection_dir_close_consensus_fetches(dir_connection_t *except_this_one,
3127  const char *resource)
3128 {
3129  smartlist_t *conns_to_close =
3131  resource);
3132  SMARTLIST_FOREACH_BEGIN(conns_to_close, dir_connection_t *, d) {
3133  if (d == except_this_one)
3134  continue;
3135  log_info(LD_DIR, "Closing consensus fetch (to %s) since one "
3136  "has just arrived.", connection_describe_peer(TO_CONN(d)));
3137  connection_mark_for_close(TO_CONN(d));
3138  } SMARTLIST_FOREACH_END(d);
3139  smartlist_free(conns_to_close);
3140 }
3141 /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
3142  * fetches have failed (with uppercase fingerprints listed in <b>failed</b>,
3143  * either as descriptor digests or as identity digests based on
3144  * <b>was_descriptor_digests</b>).
3145  */
3146 static void
3148  int router_purpose,
3149  int was_extrainfo, int was_descriptor_digests)
3150 {
3151  char digest[DIGEST_LEN];
3152  time_t now = time(NULL);
3154  if (!was_descriptor_digests) {
3155  if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
3156  tor_assert(!was_extrainfo);
3158  }
3159  return; /* FFFF should implement for other-than-router-purpose someday */
3160  }
3161  SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
3162  download_status_t *dls = NULL;
3163  if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
3164  log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
3165  continue;
3166  }
3167  if (was_extrainfo) {
3168  signed_descriptor_t *sd =
3170  if (sd)
3171  dls = &sd->ei_dl_status;
3172  } else {
3174  }
3175  if (!dls)
3176  continue;
3177  download_status_increment_failure(dls, status_code, cp, server, now);
3178  } SMARTLIST_FOREACH_END(cp);
3179 
3180  /* No need to relaunch descriptor downloads here: we already do it
3181  * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
3182 }
3183 
3184 /** Called when a connection to download microdescriptors from relay with
3185  * <b>dir_id</b> has failed in whole or in part. <b>failed</b> is a list
3186  * of every microdesc digest we didn't get. <b>status_code</b> is the http
3187  * status code we received. Reschedule the microdesc downloads as
3188  * appropriate. */
3189 static void
3191  int status_code, const char *dir_id)
3192 {
3193  networkstatus_t *consensus
3195  routerstatus_t *rs;
3196  download_status_t *dls;
3197  time_t now = time(NULL);
3199 
3200  if (! consensus)
3201  return;
3202 
3203  /* We failed to fetch a microdescriptor from 'dir_id', note it down
3204  * so that we don't try the same relay next time... */
3206 
3207  SMARTLIST_FOREACH_BEGIN(failed, const char *, d) {
3209  if (!rs)
3210  continue;
3211  dls = &rs->dl_status;
3212 
3213  { /* Increment the failure count for this md fetch */
3214  char buf[BASE64_DIGEST256_LEN+1];
3215  digest256_to_base64(buf, d);
3216  log_info(LD_DIR, "Failed to download md %s from %s",
3217  buf, hex_str(dir_id, DIGEST_LEN));
3218  download_status_increment_failure(dls, status_code, buf,
3219  server, now);
3220  }
3221  } SMARTLIST_FOREACH_END(d);
3222 }
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:2113
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.
reachable_addr_allows_dir_server
int reachable_addr_allows_dir_server(const dir_server_t *ds, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:726
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:4062
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:1526
authority_cert_dl_failed
void authority_cert_dl_failed(const char *id_digest, const char *signing_key_digest, int status)
Definition: authcert.c:681
tor_addr_family
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
or_options_t::HTTPProxyPort
uint16_t HTTPProxyPort
Definition: or_options_st.h:422
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:548
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:421
rend_cache_entry_t
Definition: rendcache.h:29
or_options_t::Socks5Proxy
char * Socks5Proxy
Definition: or_options_st.h:434
routerstatus_describe
const char * routerstatus_describe(const routerstatus_t *rs)
Definition: describe.c:182
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:997
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:1016
tor_addr_t
Definition: address.h:69
ED25519_BASE64_LEN
#define ED25519_BASE64_LEN
Definition: x25519_sizes.h:43
DIR_PURPOSE_UPLOAD_RENDDESC_V2
#define DIR_PURPOSE_UPLOAD_RENDDESC_V2
Definition: directory.h:66
directory_initiate_request
void directory_initiate_request(directory_request_t *request)
Definition: dirclient.c:1235
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:166
router_get_descriptor_gen_reason
const char * router_get_descriptor_gen_reason(void)
Definition: router.c:1825
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:970
parse_http_response
int parse_http_response(const char *headers, int *code, time_t *date, compress_method_t *compression, char **reason)
Definition: directory.c:364
DIR_CONN_STATE_CLIENT_READING
#define DIR_CONN_STATE_CLIENT_READING
Definition: directory.h:24
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:55
relay_find_addr.h
Header file for relay_find_addr.c.
download_want_authority_t
download_want_authority_t
Definition: or.h:779
microdesc.h
Header file for microdesc.c.
LD_BUG
#define LD_BUG
Definition: log.h:86
authcert.h
Header file for authcert.c.
DIR_PURPOSE_FETCH_MICRODESC
#define DIR_PURPOSE_FETCH_MICRODESC
Definition: directory.h:71
or_options_t::PublishServerDescriptor_
dirinfo_type_t PublishServerDescriptor_
Definition: or_options_st.h:283
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:1698
routers_update_all_from_networkstatus
void routers_update_all_from_networkstatus(time_t now, int dir_version)
Definition: networkstatus.c:2220
connection_dir_client_refetch_hsdesc_if_needed
void connection_dir_client_refetch_hsdesc_if_needed(dir_connection_t *dir_conn)
Definition: dirclient.c:3016
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:73
router_get_my_routerinfo
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1760
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:332
DIR_PURPOSE_FETCH_CONSENSUS
#define DIR_PURPOSE_FETCH_CONSENSUS
Definition: directory.h:57
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:3170
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:2407
entry_guard_cancel
void entry_guard_cancel(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2513
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:2346
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:2780
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:2486
READ_EVENT
@ READ_EVENT
Definition: mainloop.h:37
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:2160
ISO_SESSIONGRP
#define ISO_SESSIONGRP
Definition: or.h:984
directory_request_set_rend_query
void directory_request_set_rend_query(directory_request_t *req, const rend_data_t *query)
Definition: dirclient.c:1096
tor_mmap_t::data
const char * data
Definition: mmap.h:26
or_options_t::HTTPProxy
char * HTTPProxy
Definition: or_options_st.h:420
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:2292
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:70
reachable_addr_choose_from_rs
void reachable_addr_choose_from_rs(const routerstatus_t *rs, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:869
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:82
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:146
connection_start_reading
void connection_start_reading(connection_t *conn)
Definition: mainloop.c:633
DIR_PURPOSE_FETCH_RENDDESC_V2
#define DIR_PURPOSE_FETCH_RENDDESC_V2
Definition: directory.h:69
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:1070
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:2391
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:1509
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:387
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:644
router_get_trusteddirserver_by_digest
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:144
networkstatus.h
Header file for networkstatus.c.
connection_dir_retry_bridges
static void connection_dir_retry_bridges(smartlist_t *descs)
Definition: dirclient.c:776
digest256_to_base64
void digest256_to_base64(char *d64, const char *digest)
Definition: crypto_format.c:304
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:2639
REND_DESC_ID_V2_LEN_BASE32
#define REND_DESC_ID_V2_LEN_BASE32
Definition: or.h:355
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:46
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:1084
client_meth_allowed_anonymous_compression
static compress_method_t client_meth_allowed_anonymous_compression[]
Definition: dirclient.c:3051
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:3147
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:106
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:329
reachable_addr_allows_addr
int reachable_addr_allows_addr(const tor_addr_t *addr, uint16_t port, firewall_connection_t fw_connection, int pref_only, int pref_ipv6)
Definition: policies.c:531
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:39
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:3190
DIR_PURPOSE_FETCH_HSDESC
#define DIR_PURPOSE_FETCH_HSDESC
Definition: directory.h:75
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:48
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:1004
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
routerinfo_t::ipv4_addr
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:25
count_loading_descriptors_progress
int count_loading_descriptors_progress(void)
Definition: nodelist.c:2690
tor_addr_port_t
Definition: address.h:81
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:711
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:4971
MICRODESC_DIRINFO
@ MICRODESC_DIRINFO
Definition: or.h:914
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:1162
directory_request_set_payload
void directory_request_set_payload(directory_request_t *req, const char *payload, size_t payload_len)
Definition: dirclient.c:1056
router_pick_directory_server
const routerstatus_t * router_pick_directory_server(dirinfo_type_t type, int flags)
Definition: node_select.c:72
connection_describe_peer
const char * connection_describe_peer(const connection_t *conn)
Definition: connection.c:515
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:1778
or_options_t::HTTPProxyAuthenticator
char * HTTPProxyAuthenticator
Definition: or_options_st.h:423
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:2215
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:739
cached_dir_st.h
Cached large directory object structure.
DIR_CONN_STATE_CLIENT_SENDING
#define DIR_CONN_STATE_CLIENT_SENDING
Definition: directory.h:22
DIR_PURPOSE_IS_UPLOAD
#define DIR_PURPOSE_IS_UPLOAD(p)
Definition: directory.h:83
or_options_t::ExcludeNodes
struct routerset_t * ExcludeNodes
Definition: or_options_st.h:102
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:983
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:99
strcmpstart
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:215
DIR_PURPOSE_FETCH_STATUS_VOTE
#define DIR_PURPOSE_FETCH_STATUS_VOTE
Definition: directory.h:51
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:2949
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:4193
allowed_anonymous_connection_compression_method
STATIC int allowed_anonymous_connection_compression_method(compress_method_t method)
Definition: dirclient.c:3083
nodelist.h
Header file for nodelist.c.
rend_valid_v2_service_id
int rend_valid_v2_service_id(const char *query)
Definition: rendcommon.c:725
DIR_PURPOSE_FETCH_EXTRAINFO
#define DIR_PURPOSE_FETCH_EXTRAINFO
Definition: directory.h:42
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:1986
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:1117
warn_disallowed_anonymous_compression_method
STATIC void warn_disallowed_anonymous_compression_method(compress_method_t method)
Definition: dirclient.c:3105
router_pick_trusteddirserver
const routerstatus_t * router_pick_trusteddirserver(dirinfo_type_t type, int flags)
Definition: node_select.c:1040
PDS_NO_EXISTING_SERVERDESC_FETCH
#define PDS_NO_EXISTING_SERVERDESC_FETCH
Definition: node_select.h:67
control_event_boot_dir
void control_event_boot_dir(bootstrap_status_t status, int progress)
Definition: control_bootstrap.c:234
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:1804
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:164
connection_close_immediate
void connection_close_immediate(connection_t *conn)
Definition: connection.c:1033
or_options_t::DownloadExtraInfo
int DownloadExtraInfo
Definition: or_options_st.h:622
ctassert.h
Compile-time assertions: CTASSERT(expression).
connection_t::addr
tor_addr_t addr
Definition: connection_st.h:145
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:60
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:5825
DIR_CONN_STATE_CLIENT_FINISHED
#define DIR_CONN_STATE_CLIENT_FINISHED
Definition: directory.h:26
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:1137
tor_addr_to_str_dup
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1164
rend_client_desc_trynow
void rend_client_desc_trynow(const char *query)
Definition: rendclient.c:922
cached_dir_t
Definition: cached_dir_st.h:17
router_dirport_found_reachable
void router_dirport_found_reachable(void)
Definition: selftest.c:477
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:994
networkstatus_parse_flavor_name
int networkstatus_parse_flavor_name(const char *flavname)
Definition: networkstatus.c:2588
CTASSERT
#define CTASSERT(x)
Definition: ctassert.h:44
DIR_PURPOSE_UPLOAD_DIR
#define DIR_PURPOSE_UPLOAD_DIR
Definition: directory.h:44
retry_bridge_descriptor_fetch_directly
void retry_bridge_descriptor_fetch_directly(const char *digest)
Definition: bridges.c:693
PDS_NO_EXISTING_MICRODESC_FETCH
#define PDS_NO_EXISTING_MICRODESC_FETCH
Definition: node_select.h:73
handle_response_upload_vote
static int handle_response_upload_vote(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2707
entry_list_is_constrained
int entry_list_is_constrained(const or_options_t *options)
Definition: entrynodes.c:3392
EXTRAINFO_DIRINFO
@ EXTRAINFO_DIRINFO
Definition: or.h:912
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:716
connection_t
Definition: connection_st.h:45
rendcache.h
Header file for rendcache.c.
tor_addr_parse
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1349
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:796
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:1111
hs_client_refetch_hsdesc
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:2054
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:239
connection_dir_client_request_failed
void connection_dir_client_request_failed(dir_connection_t *conn)
Definition: dirclient.c:730
handle_response_fetch_renddesc_v2
static int handle_response_fetch_renddesc_v2(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2802
control_events.h
Header file for control_events.c.
get_options
const or_options_t * get_options(void)
Definition: config.c:928
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:1773
connection_dir_list_by_purpose_and_resource
smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4844
connection_watch_events
void connection_watch_events(connection_t *conn, watchable_events_t events)
Definition: mainloop.c:495
connection_dir_download_cert_failed
static void connection_dir_download_cert_failed(dir_connection_t *conn, int status_code)
Definition: dirclient.c:837
directory_request_set_resource
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1045
directory_request_dir_contact_info_specified
static int directory_request_dir_contact_info_specified(const directory_request_t *req)
Definition: dirclient.c:1147
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:2367
reachable_addr_choose_from_node
void reachable_addr_choose_from_node(const node_t *node, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition: policies.c:983
DIR_PURPOSE_FETCH_CERTIFICATE
#define DIR_PURPOSE_FETCH_CERTIFICATE
Definition: directory.h:60
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:988
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:2212
guards_choose_dirguard
const node_t * guards_choose_dirguard(uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
Definition: entrynodes.c:3813
DIR_PURPOSE_HAS_FETCHED_HSDESC
#define DIR_PURPOSE_HAS_FETCHED_HSDESC
Definition: directory.h:78
connection_describe
const char * connection_describe(const connection_t *conn)
Definition: connection.c:530
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
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:573
directory_request_new
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:948
dir_split_resource_into_fingerprint_pairs
int dir_split_resource_into_fingerprint_pairs(const char *res, smartlist_t *pairs_out)
Definition: directory.c:585
V3_DIRINFO
@ V3_DIRINFO
Definition: or.h:908
connection_dir_reached_eof
int connection_dir_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:2995
dirserv.h
Header file for dirserv.c.
directory_get_consensus_url
static char * directory_get_consensus_url(const char *resource)
Definition: dirclient.c:1463
tor_addr_is_null
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
dirinfo_type_t
dirinfo_type_t
Definition: or.h:905
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:2444
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:3446
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:106
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:1125
relay_address_new_suggestion
void relay_address_new_suggestion(const tor_addr_t *suggested_addr, const tor_addr_t *peer_addr, const char *identity_digest)
Definition: relay_find_addr.c:43
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:233
dir_conn_purpose_to_string
STATIC const char * dir_conn_purpose_to_string(int purpose)
Definition: dirclient.c:101
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:1917
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:557
or_options_t::FetchUselessDescriptors
int FetchUselessDescriptors
Definition: or_options_st.h:289
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:2020
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:419
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:1173
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:1979
rend_data_get_address
const char * rend_data_get_address(const rend_data_t *rend_data)
Definition: hs_common.c:529
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:2579
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:813
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:1051
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:1445
purpose_needs_anonymity
int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
Definition: directory.c:114
or_options_t::FetchServerDescriptors
int FetchServerDescriptors
Definition: or_options_st.h:286
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:910
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:2233
routerset_contains_routerstatus
int routerset_contains_routerstatus(const routerset_t *set, const routerstatus_t *rs, country_t country)
Definition: routerset.c:338
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:98
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:2743
or_options_t
Definition: or_options_st.h:45
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:893
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:836
or_options_t::Socks4Proxy
char * Socks4Proxy
Definition: or_options_st.h:430
handle_response_fetch_desc
static int handle_response_fetch_desc(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2479
handle_response_upload_renddesc_v2
static int handle_response_upload_renddesc_v2(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2898
TO_CONN
#define TO_CONN(c)
Definition: or.h:736
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:794
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:2495
networkstatus_st.h
Networkstatus consensus/vote structure.
entry_connection_t
Definition: entry_connection_st.h:19
WRITE_EVENT
@ WRITE_EVENT
Definition: mainloop.h:38
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:750
DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2
#define DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2
Definition: directory.h:36
entry_guard_failed
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2534
smartlist_t
Definition: smartlist_core.h:26
client_meth_pref
static compress_method_t client_meth_pref[]
Definition: dirclient.c:3041
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:1099
rendclient.h
Header file for rendclient.c.
tor_addr_eq
#define tor_addr_eq(a, b)
Definition: address.h:280
or_options_t::FetchDirInfoExtraEarly
int FetchDirInfoExtraEarly
Definition: or_options_st.h:576
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:1032
DIR_CONN_STATE_CONNECTING
#define DIR_CONN_STATE_CONNECTING
Definition: directory.h:20
DIR_PURPOSE_SERVER
#define DIR_PURPOSE_SERVER
Definition: directory.h:63
accept_encoding_header
STATIC char * accept_encoding_header(void)
Definition: dirclient.c:3060
dirvote_add_signatures
int dirvote_add_signatures(const char *detached_signatures_body, const char *source, const char **msg)
Definition: dirvote.c:3686
tor_addr_copy
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
#define DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
Definition: directory.h:54
rend_get_service_id
int rend_get_service_id(crypto_pk_t *pk, char *out)
Definition: rendcommon.c:712
or.h
Master header file for Tor-specific functionality.
router_set_status
void router_set_status(const char *digest, int up)
Definition: nodelist.c:2290
LD_PROTOCOL
#define LD_PROTOCOL
Definition: log.h:72