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