Tor  0.4.7.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-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * @file dirclient.c
8  * @brief Download directory information
9  **/
10 
11 #define DIRCLIENT_PRIVATE
12 
13 #include "core/or/or.h"
14 
15 #include "app/config/config.h"
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 /**
1124  * Helper: update the addresses, ports, and identities in <b>req</b>
1125  * from the routerstatus object in <b>req</b>. Return 0 on success.
1126  * On failure, warn and return -1.
1127  */
1128 static int
1130 
1131 {
1132  const routerstatus_t *status = req->routerstatus;
1133  if (BUG(status == NULL))
1134  return -1;
1135  const or_options_t *options = get_options();
1136  const node_t *node;
1137  tor_addr_port_t use_or_ap, use_dir_ap;
1138  const int anonymized_connection = dirind_is_anon(req->indirection);
1139 
1140  tor_assert(status != NULL);
1141 
1142  node = node_get_by_id(status->identity_digest);
1143 
1144  /* XXX The below check is wrong: !node means it's not in the consensus,
1145  * but we haven't checked if we have a descriptor for it -- and also,
1146  * we only care about the descriptor if it's a begindir-style anonymized
1147  * connection. */
1148  if (!node && anonymized_connection) {
1149  log_info(LD_DIR, "Not sending anonymized request to directory '%s'; we "
1150  "don't have its router descriptor.",
1151  routerstatus_describe(status));
1152  return -1;
1153  }
1154 
1155  if (options->ExcludeNodes && options->StrictNodes &&
1156  routerset_contains_routerstatus(options->ExcludeNodes, status, -1)) {
1157  log_warn(LD_DIR, "Wanted to contact directory mirror %s for %s, but "
1158  "it's in our ExcludedNodes list and StrictNodes is set. "
1159  "Skipping. This choice might make your Tor not work.",
1160  routerstatus_describe(status),
1161  dir_conn_purpose_to_string(req->dir_purpose));
1162  return -1;
1163  }
1164 
1165  /* At this point, if we are a client making a direct connection to a
1166  * directory server, we have selected a server that has at least one address
1167  * allowed by ClientUseIPv4/6 and Reachable{"",OR,Dir}Addresses. This
1168  * selection uses the preference in ClientPreferIPv6{OR,Dir}Port, if
1169  * possible. (If UseBridges is set, clients always use IPv6, and prefer it
1170  * by default.)
1171  *
1172  * Now choose an address that we can use to connect to the directory server.
1173  */
1174  if (directory_choose_address_routerstatus(status,
1175  req->indirection, &use_or_ap,
1176  &use_dir_ap) < 0) {
1177  return -1;
1178  }
1179 
1180  /* One last thing: If we're talking to an authority, we might want to use
1181  * a special HTTP port for it based on our purpose.
1182  */
1183  if (req->indirection == DIRIND_DIRECT_CONN && status->is_authority) {
1185  status->identity_digest);
1186  if (ds) {
1187  const tor_addr_port_t *v4 = NULL;
1188  if (authdir_mode_v3(get_options())) {
1189  // An authority connecting to another authority should always
1190  // prefer the VOTING usage, if one is specifically configured.
1192  ds, AUTH_USAGE_VOTING, AF_INET);
1193  }
1194  if (! v4) {
1195  // Everybody else should prefer a usage dependent on their
1196  // the dir_purpose.
1197  auth_dirport_usage_t usage =
1198  auth_dirport_usage_for_purpose(req->dir_purpose);
1199  v4 = trusted_dir_server_get_dirport(ds, usage, AF_INET);
1200  }
1201  tor_assert_nonfatal(v4);
1202  if (v4) {
1203  // XXXX We could, if we wanted, also select a v6 address. But a v4
1204  // address must exist here, and we as a relay are required to support
1205  // ipv4. So we just that.
1206  tor_addr_port_copy(&use_dir_ap, v4);
1207  }
1208  }
1209  }
1210 
1211  directory_request_set_or_addr_port(req, &use_or_ap);
1212  directory_request_set_dir_addr_port(req, &use_dir_ap);
1213  directory_request_set_directory_id_digest(req, status->identity_digest);
1214  return 0;
1215 }
1216 
1217 /**
1218  * Launch the provided directory request, configured in <b>request</b>.
1219  * After this function is called, you can free <b>request</b>.
1220  */
1221 MOCK_IMPL(void,
1223 {
1224  tor_assert(request);
1225  if (request->routerstatus) {
1226  tor_assert_nonfatal(
1229  return; // or here XXXX
1230  }
1231  }
1232 
1233  const tor_addr_port_t *or_addr_port = &request->or_addr_port;
1234  const tor_addr_port_t *dir_addr_port = &request->dir_addr_port;
1235  const char *digest = request->digest;
1236  const uint8_t dir_purpose = request->dir_purpose;
1237  const uint8_t router_purpose = request->router_purpose;
1238  const dir_indirection_t indirection = request->indirection;
1239  const char *resource = request->resource;
1240  const hs_ident_dir_conn_t *hs_ident = request->hs_ident;
1241  circuit_guard_state_t *guard_state = request->guard_state;
1242 
1243  tor_assert(or_addr_port->port || dir_addr_port->port);
1244  tor_assert(digest);
1245 
1246  dir_connection_t *conn;
1247  const or_options_t *options = get_options();
1248  int socket_error = 0;
1249  const char *begindir_reason = NULL;
1250  /* Should the connection be to a relay's OR port (and inside that we will
1251  * send our directory request)? */
1252  const int use_begindir =
1253  directory_command_should_use_begindir(options, request, &begindir_reason);
1254 
1255  /* Will the connection go via a three-hop Tor circuit? Note that this
1256  * is separate from whether it will use_begindir. */
1257  const int anonymized_connection = dirind_is_anon(indirection);
1258 
1259  /* What is the address we want to make the directory request to? If
1260  * we're making a begindir request this is the ORPort of the relay
1261  * we're contacting; if not a begindir request, this is its DirPort.
1262  * Note that if anonymized_connection is true, we won't be initiating
1263  * a connection directly to this address. */
1264  tor_addr_t addr;
1265  tor_addr_copy(&addr, &(use_begindir ? or_addr_port : dir_addr_port)->addr);
1266  uint16_t port = (use_begindir ? or_addr_port : dir_addr_port)->port;
1267 
1268  log_debug(LD_DIR, "anonymized %d, use_begindir %d.",
1269  anonymized_connection, use_begindir);
1270 
1271  log_debug(LD_DIR, "Initiating %s", dir_conn_purpose_to_string(dir_purpose));
1272 
1273  if (purpose_needs_anonymity(dir_purpose, router_purpose, resource)) {
1274  tor_assert(anonymized_connection ||
1275  hs_service_non_anonymous_mode_enabled(options));
1276  }
1277 
1278  /* use encrypted begindir connections for everything except relays
1279  * this provides better protection for directory fetches */
1280  if (!use_begindir && dirclient_must_use_begindir(options)) {
1281  log_warn(LD_BUG, "Client could not use begindir connection: %s",
1282  begindir_reason ? begindir_reason : "(NULL)");
1283  return;
1284  }
1285 
1286  /* ensure that we don't make direct connections when a SOCKS server is
1287  * configured. */
1288  if (!anonymized_connection && !use_begindir && !options->HTTPProxy &&
1289  (options->Socks4Proxy || options->Socks5Proxy)) {
1290  log_warn(LD_DIR, "Cannot connect to a directory server through a "
1291  "SOCKS proxy!");
1292  return;
1293  }
1294 
1295  /* Make sure that the destination addr and port we picked is viable. */
1296  if (!port || tor_addr_is_null(&addr)) {
1297  static int logged_backtrace = 0;
1298  log_warn(LD_DIR,
1299  "Cannot make an outgoing %sconnection without a remote %sPort.",
1300  use_begindir ? "begindir " : "",
1301  use_begindir ? "OR" : "Dir");
1302  if (!logged_backtrace) {
1303  log_backtrace(LOG_INFO, LD_BUG, "Address came from");
1304  logged_backtrace = 1;
1305  }
1306  return;
1307  }
1308 
1309  conn = dir_connection_new(tor_addr_family(&addr));
1310 
1311  /* set up conn so it's got all the data we need to remember */
1312  tor_addr_copy(&conn->base_.addr, &addr);
1313  conn->base_.port = port;
1314  conn->base_.address = tor_addr_to_str_dup(&addr);
1315  memcpy(conn->identity_digest, digest, DIGEST_LEN);
1316 
1317  conn->base_.purpose = dir_purpose;
1318  conn->router_purpose = router_purpose;
1319 
1320  /* give it an initial state */
1321  conn->base_.state = DIR_CONN_STATE_CONNECTING;
1322 
1323  /* decide whether we can learn our IP address from this conn */
1324  /* XXXX This is a bad name for this field now. */
1325  conn->dirconn_direct = !anonymized_connection;
1326 
1327  if (hs_ident) {
1328  conn->hs_ident = hs_ident_dir_conn_dup(hs_ident);
1329  }
1330 
1331  if (!anonymized_connection && !use_begindir) {
1332  /* then we want to connect to dirport directly */
1333 
1334  if (options->HTTPProxy) {
1335  tor_addr_copy(&addr, &options->HTTPProxyAddr);
1336  port = options->HTTPProxyPort;
1337  }
1338 
1339  // In this case we should not have picked a directory guard.
1340  if (BUG(guard_state)) {
1341  entry_guard_cancel(&guard_state);
1342  }
1343 
1344  // XXXX This is the case where we replace.
1345 
1346  switch (connection_connect(TO_CONN(conn), conn->base_.address, &addr,
1347  port, &socket_error)) {
1348  case -1:
1349  connection_mark_for_close(TO_CONN(conn));
1350  return;
1351  case 1:
1352  /* start flushing conn */
1353  conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
1354  FALLTHROUGH;
1355  case 0:
1356  /* queue the command on the outbuf */
1357  directory_send_command(conn, 1, request);
1359  /* writable indicates finish, readable indicates broken link,
1360  error indicates broken link in windowsland. */
1361  }
1362  } else {
1363  /* We will use a Tor circuit (maybe 1-hop, maybe 3-hop, maybe with
1364  * begindir, maybe not with begindir) */
1365 
1366  entry_connection_t *linked_conn;
1367 
1368  /* Anonymized tunneled connections can never share a circuit.
1369  * One-hop directory connections can share circuits with each other
1370  * but nothing else. */
1371  int iso_flags = anonymized_connection ? ISO_STREAM : ISO_SESSIONGRP;
1372 
1373  /* If it's an anonymized connection, remember the fact that we
1374  * wanted it for later: maybe we'll want it again soon. */
1375  if (anonymized_connection && use_begindir)
1376  rep_hist_note_used_internal(time(NULL), 0, 1);
1377  else if (anonymized_connection && !use_begindir)
1378  rep_hist_note_used_port(time(NULL), conn->base_.port);
1379 
1380  // In this case we should not have a directory guard; we'll
1381  // get a regular guard later when we build the circuit.
1382  if (BUG(anonymized_connection && guard_state)) {
1383  entry_guard_cancel(&guard_state);
1384  }
1385 
1386  conn->guard_state = guard_state;
1387 
1388  /* make an AP connection
1389  * populate it and add it at the right state
1390  * hook up both sides
1391  */
1392  linked_conn =
1394  conn->base_.address, conn->base_.port,
1395  digest,
1396  SESSION_GROUP_DIRCONN, iso_flags,
1397  use_begindir, !anonymized_connection);
1398  if (!linked_conn) {
1399  log_warn(LD_NET,"Making tunnel to dirserver failed.");
1400  connection_mark_for_close(TO_CONN(conn));
1401  return;
1402  }
1403 
1404  if (connection_add(TO_CONN(conn)) < 0) {
1405  log_warn(LD_NET,"Unable to add connection for link to dirserver.");
1406  connection_mark_for_close(TO_CONN(conn));
1407  return;
1408  }
1409  conn->base_.state = DIR_CONN_STATE_CLIENT_SENDING;
1410  /* queue the command on the outbuf */
1411  directory_send_command(conn, 0, request);
1412 
1415  }
1416 }
1417 
1418 /** Helper for sorting
1419  *
1420  * sort strings alphabetically
1421  *
1422  * XXXX we have a smartlist_sort_strings() function, right?
1423  */
1424 static int
1425 compare_strs_(const void **a, const void **b)
1426 {
1427  const char *s1 = *a, *s2 = *b;
1428  return strcmp(s1, s2);
1429 }
1430 
1431 #define CONDITIONAL_CONSENSUS_FPR_LEN 3
1432 CTASSERT(CONDITIONAL_CONSENSUS_FPR_LEN <= DIGEST_LEN);
1433 
1434 /** Return the URL we should use for a consensus download.
1435  *
1436  * Use the "conditional consensus downloading" feature described in
1437  * dir-spec.txt, i.e.
1438  * GET .../consensus/<b>fpr</b>+<b>fpr</b>+<b>fpr</b>
1439  *
1440  * If 'resource' is provided, it is the name of a consensus flavor to request.
1441  */
1442 static char *
1443 directory_get_consensus_url(const char *resource)
1444 {
1445  char *url = NULL;
1446  const char *hyphen, *flavor;
1447  if (resource==NULL || strcmp(resource, "ns")==0) {
1448  flavor = ""; /* Request ns consensuses as "", so older servers will work*/
1449  hyphen = "";
1450  } else {
1451  flavor = resource;
1452  hyphen = "-";
1453  }
1454 
1455  {
1456  char *authority_id_list;
1457  smartlist_t *authority_digests = smartlist_new();
1458 
1459  SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
1460  dir_server_t *, ds) {
1461  char *hex;
1462  if (!(ds->type & V3_DIRINFO))
1463  continue;
1464 
1465  hex = tor_malloc(2*CONDITIONAL_CONSENSUS_FPR_LEN+1);
1466  base16_encode(hex, 2*CONDITIONAL_CONSENSUS_FPR_LEN+1,
1467  ds->v3_identity_digest, CONDITIONAL_CONSENSUS_FPR_LEN);
1468  smartlist_add(authority_digests, hex);
1469  } SMARTLIST_FOREACH_END(ds);
1470  smartlist_sort(authority_digests, compare_strs_);
1471  authority_id_list = smartlist_join_strings(authority_digests,
1472  "+", 0, NULL);
1473 
1474  tor_asprintf(&url, "/tor/status-vote/current/consensus%s%s/%s.z",
1475  hyphen, flavor, authority_id_list);
1476 
1477  SMARTLIST_FOREACH(authority_digests, char *, cp, tor_free(cp));
1478  smartlist_free(authority_digests);
1479  tor_free(authority_id_list);
1480  }
1481  return url;
1482 }
1483 
1484 /**
1485  * Copies the ipv6 from source to destination, subject to buffer size limit
1486  * size. If decorate is true, makes sure the copied address is decorated.
1487  */
1488 static void
1489 copy_ipv6_address(char* destination, const char* source, size_t len,
1490  int decorate) {
1491  tor_assert(destination);
1492  tor_assert(source);
1493 
1494  if (decorate && source[0] != '[') {
1495  tor_snprintf(destination, len, "[%s]", source);
1496  } else {
1497  strlcpy(destination, source, len);
1498  }
1499 }
1500 
1501 /** Queue an appropriate HTTP command for <b>request</b> on
1502  * <b>conn</b>->outbuf. If <b>direct</b> is true, we're making a
1503  * non-anonymized connection to the dirport.
1504  */
1505 static void
1507  const int direct,
1508  const directory_request_t *req)
1509 {
1510  tor_assert(req);
1511  const int purpose = req->dir_purpose;
1512  const char *resource = req->resource;
1513  const char *payload = req->payload;
1514  const size_t payload_len = req->payload_len;
1515  const time_t if_modified_since = req->if_modified_since;
1516  const int anonymized_connection = dirind_is_anon(req->indirection);
1517 
1518  char proxystring[256];
1519  char hoststring[128];
1520  /* NEEDS to be the same size hoststring.
1521  Will be decorated with brackets around it if it is ipv6. */
1522  char decorated_address[128];
1523  smartlist_t *headers = smartlist_new();
1524  char *url;
1525  char *accept_encoding;
1526  size_t url_len;
1527  char request[8192];
1528  size_t request_len, total_request_len = 0;
1529  const char *httpcommand = NULL;
1530 
1531  tor_assert(conn);
1532  tor_assert(conn->base_.type == CONN_TYPE_DIR);
1533 
1535  if (resource)
1536  conn->requested_resource = tor_strdup(resource);
1537 
1538  /* decorate the ip address if it is ipv6 */
1539  if (strchr(conn->base_.address, ':')) {
1540  copy_ipv6_address(decorated_address, conn->base_.address,
1541  sizeof(decorated_address), 1);
1542  } else {
1543  strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
1544  }
1545 
1546  /* come up with a string for which Host: we want */
1547  if (conn->base_.port == 80) {
1548  strlcpy(hoststring, decorated_address, sizeof(hoststring));
1549  } else {
1550  tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
1551  decorated_address, conn->base_.port);
1552  }
1553 
1554  /* Format if-modified-since */
1555  if (if_modified_since) {
1556  char b[RFC1123_TIME_LEN+1];
1557  format_rfc1123_time(b, if_modified_since);
1558  smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
1559  }
1560 
1561  /* come up with some proxy lines, if we're using one. */
1562  if (direct && get_options()->HTTPProxy) {
1563  char *base64_authenticator=NULL;
1564  const char *authenticator = get_options()->HTTPProxyAuthenticator;
1565 
1566  tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
1567  if (authenticator) {
1568  base64_authenticator = alloc_http_authenticator(authenticator);
1569  if (!base64_authenticator)
1570  log_warn(LD_BUG, "Encoding http authenticator failed");
1571  }
1572  if (base64_authenticator) {
1573  smartlist_add_asprintf(headers,
1574  "Proxy-Authorization: Basic %s\r\n",
1575  base64_authenticator);
1576  tor_free(base64_authenticator);
1577  }
1578  } else {
1579  proxystring[0] = 0;
1580  }
1581 
1582  if (! anonymized_connection) {
1583  /* Add Accept-Encoding. */
1584  accept_encoding = accept_encoding_header();
1585  smartlist_add_asprintf(headers, "Accept-Encoding: %s\r\n",
1586  accept_encoding);
1587  tor_free(accept_encoding);
1588  }
1589 
1590  /* Add additional headers, if any */
1591  {
1592  config_line_t *h;
1593  for (h = req->additional_headers; h; h = h->next) {
1594  smartlist_add_asprintf(headers, "%s%s\r\n", h->key, h->value);
1595  }
1596  }
1597 
1598  switch (purpose) {
1600  /* resource is optional. If present, it's a flavor name */
1601  tor_assert(!payload);
1602  httpcommand = "GET";
1603  url = directory_get_consensus_url(resource);
1604  log_info(LD_DIR, "Downloading consensus from %s using %s",
1605  hoststring, url);
1606  break;
1608  tor_assert(resource);
1609  tor_assert(!payload);
1610  httpcommand = "GET";
1611  tor_asprintf(&url, "/tor/keys/%s", resource);
1612  break;
1614  tor_assert(resource);
1615  tor_assert(!payload);
1616  httpcommand = "GET";
1617  tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
1618  break;
1620  tor_assert(!resource);
1621  tor_assert(!payload);
1622  httpcommand = "GET";
1623  url = tor_strdup("/tor/status-vote/next/consensus-signatures.z");
1624  break;
1626  tor_assert(resource);
1627  httpcommand = "GET";
1628  tor_asprintf(&url, "/tor/server/%s", resource);
1629  break;
1631  tor_assert(resource);
1632  httpcommand = "GET";
1633  tor_asprintf(&url, "/tor/extra/%s", resource);
1634  break;
1636  tor_assert(resource);
1637  httpcommand = "GET";
1638  tor_asprintf(&url, "/tor/micro/%s", resource);
1639  break;
1640  case DIR_PURPOSE_UPLOAD_DIR: {
1641  const char *why = router_get_descriptor_gen_reason();
1642  tor_assert(!resource);
1643  tor_assert(payload);
1644  httpcommand = "POST";
1645  url = tor_strdup("/tor/");
1646  if (!why) {
1647  why = "for no reason at all";
1648  }
1649  smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
1650  break;
1651  }
1653  tor_assert(!resource);
1654  tor_assert(payload);
1655  httpcommand = "POST";
1656  url = tor_strdup("/tor/post/vote");
1657  break;
1659  tor_assert(!resource);
1660  tor_assert(payload);
1661  httpcommand = "POST";
1662  url = tor_strdup("/tor/post/consensus-signature");
1663  break;
1665  tor_assert(resource);
1666  tor_assert(strlen(resource) <= ED25519_BASE64_LEN);
1667  tor_assert(!payload);
1668  httpcommand = "GET";
1669  tor_asprintf(&url, "/tor/hs/3/%s", resource);
1670  break;
1672  tor_assert(resource);
1673  tor_assert(payload);
1674  httpcommand = "POST";
1675  tor_asprintf(&url, "/tor/hs/%s/publish", resource);
1676  break;
1677  default:
1678  tor_assert(0);
1679  return;
1680  }
1681 
1682  /* warn in the non-tunneled case */
1683  if (direct && (strlen(proxystring) + strlen(url) >= 4096)) {
1684  log_warn(LD_BUG,
1685  "Squid does not like URLs longer than 4095 bytes, and this "
1686  "one is %d bytes long: %s%s",
1687  (int)(strlen(proxystring) + strlen(url)), proxystring, url);
1688  }
1689 
1690  tor_snprintf(request, sizeof(request), "%s %s", httpcommand, proxystring);
1691 
1692  request_len = strlen(request);
1693  total_request_len += request_len;
1694  connection_buf_add(request, request_len, TO_CONN(conn));
1695 
1696  url_len = strlen(url);
1697  total_request_len += url_len;
1698  connection_buf_add(url, url_len, TO_CONN(conn));
1699  tor_free(url);
1700 
1701  if (!strcmp(httpcommand, "POST") || payload) {
1702  smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
1703  payload ? (unsigned long)payload_len : 0);
1704  }
1705 
1706  {
1707  char *header = smartlist_join_strings(headers, "", 0, NULL);
1708  tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
1709  hoststring, header);
1710  tor_free(header);
1711  }
1712 
1713  request_len = strlen(request);
1714  total_request_len += request_len;
1715  connection_buf_add(request, request_len, TO_CONN(conn));
1716 
1717  if (payload) {
1718  /* then send the payload afterwards too */
1719  connection_buf_add(payload, payload_len, TO_CONN(conn));
1720  total_request_len += payload_len;
1721  }
1722 
1723  SMARTLIST_FOREACH(headers, char *, h, tor_free(h));
1724  smartlist_free(headers);
1725 
1726  log_debug(LD_DIR,
1727  "Sent request to directory server %s "
1728  "(purpose: %d, request size: %"TOR_PRIuSZ", "
1729  "payload size: %"TOR_PRIuSZ")",
1731  conn->base_.purpose,
1732  (total_request_len),
1733  (payload ? payload_len : 0));
1734 }
1735 
1736 /** Return true iff <b>body</b> doesn't start with a plausible router or
1737  * network-status or microdescriptor opening. This is a sign of possible
1738  * compression. */
1739 static int
1740 body_is_plausible(const char *body, size_t len, int purpose)
1741 {
1742  int i;
1743  if (len == 0)
1744  return 1; /* empty bodies don't need decompression */
1745  if (len < 32)
1746  return 0;
1747  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
1748  return (!strcmpstart(body,"onion-key"));
1749  }
1750 
1751  if (!strcmpstart(body,"router") ||
1752  !strcmpstart(body,"network-status"))
1753  return 1;
1754  for (i=0;i<32;++i) {
1755  if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
1756  return 0;
1757  }
1758 
1759  return 1;
1760 }
1761 
1762 /** Called when we've just fetched a bunch of router descriptors in
1763  * <b>body</b>. The list <b>which</b>, if present, holds digests for
1764  * descriptors we requested: descriptor digests if <b>descriptor_digests</b>
1765  * is true, or identity digests otherwise. Parse the descriptors, validate
1766  * them, and annotate them as having purpose <b>purpose</b> and as having been
1767  * downloaded from <b>source</b>.
1768  *
1769  * Return the number of routers actually added. */
1770 static int
1771 load_downloaded_routers(const char *body, smartlist_t *which,
1772  int descriptor_digests,
1773  int router_purpose,
1774  const char *source)
1775 {
1776  char buf[256];
1777  char time_buf[ISO_TIME_LEN+1];
1778  int added = 0;
1779  int general = router_purpose == ROUTER_PURPOSE_GENERAL;
1780  format_iso_time(time_buf, time(NULL));
1781  tor_assert(source);
1782 
1783  if (tor_snprintf(buf, sizeof(buf),
1784  "@downloaded-at %s\n"
1785  "@source %s\n"
1786  "%s%s%s", time_buf, escaped(source),
1787  !general ? "@purpose " : "",
1788  !general ? router_purpose_to_string(router_purpose) : "",
1789  !general ? "\n" : "")<0)
1790  return added;
1791 
1792  added = router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
1793  descriptor_digests, buf);
1794  if (added && general)
1795  control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
1797  return added;
1798 }
1799 
1801  const response_handler_args_t *);
1803  const response_handler_args_t *);
1805  const response_handler_args_t *);
1807  const response_handler_args_t *);
1809  const response_handler_args_t *);
1811  const response_handler_args_t *);
1813  const response_handler_args_t *);
1815  const response_handler_args_t *);
1816 
1817 static int
1818 dir_client_decompress_response_body(char **bodyp, size_t *bodylenp,
1819  dir_connection_t *conn,
1820  compress_method_t compression,
1821  int anonymized_connection)
1822 {
1823  int rv = 0;
1824  const char *body = *bodyp;
1825  size_t body_len = *bodylenp;
1826  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
1827  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
1828  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
1829 
1830  int plausible = body_is_plausible(body, body_len, conn->base_.purpose);
1831 
1832  if (plausible && compression == NO_METHOD) {
1833  return 0;
1834  }
1835 
1836  int severity = LOG_DEBUG;
1837  char *new_body = NULL;
1838  size_t new_len = 0;
1839  const char *description1, *description2;
1840  int want_to_try_both = 0;
1841  int tried_both = 0;
1842  compress_method_t guessed = detect_compression_method(body, body_len);
1843 
1844  description1 = compression_method_get_human_name(compression);
1845 
1846  if (BUG(description1 == NULL))
1847  description1 = compression_method_get_human_name(UNKNOWN_METHOD);
1848 
1849  if (guessed == UNKNOWN_METHOD && !plausible)
1850  description2 = "confusing binary junk";
1851  else
1852  description2 = compression_method_get_human_name(guessed);
1853 
1854  /* Tell the user if we don't believe what we're told about compression.*/
1855  want_to_try_both = (compression == UNKNOWN_METHOD ||
1856  guessed != compression);
1857  if (want_to_try_both) {
1858  severity = LOG_PROTOCOL_WARN;
1859  }
1860 
1861  tor_log(severity, LD_HTTP,
1862  "HTTP body from %s was labeled as %s, "
1863  "%s it seems to be %s.%s",
1865  description1,
1866  guessed != compression?"but":"and",
1867  description2,
1868  (compression>0 && guessed>0 && want_to_try_both)?
1869  " Trying both.":"");
1870 
1871  /* Try declared compression first if we can.
1872  * tor_compress_supports_method() also returns true for NO_METHOD.
1873  * Ensure that the server is not sending us data compressed using a
1874  * compression method that is not allowed for anonymous connections. */
1875  if (anonymized_connection &&
1878  rv = -1;
1879  goto done;
1880  }
1881 
1882  if (tor_compress_supports_method(compression)) {
1883  tor_uncompress(&new_body, &new_len, body, body_len, compression,
1884  !allow_partial, LOG_PROTOCOL_WARN);
1885  if (new_body) {
1886  /* We succeeded with the declared compression method. Great! */
1887  rv = 0;
1888  goto done;
1889  }
1890  }
1891 
1892  /* Okay, if that didn't work, and we think that it was compressed
1893  * differently, try that. */
1894  if (anonymized_connection &&
1897  rv = -1;
1898  goto done;
1899  }
1900 
1901  if (tor_compress_supports_method(guessed) &&
1902  compression != guessed) {
1903  tor_uncompress(&new_body, &new_len, body, body_len, guessed,
1904  !allow_partial, LOG_INFO);
1905  tried_both = 1;
1906  }
1907  /* If we're pretty sure that we have a compressed directory, and
1908  * we didn't manage to uncompress it, then warn and bail. */
1909  if (!plausible && !new_body) {
1910  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
1911  "Unable to decompress HTTP body (tried %s%s%s, on %s).",
1912  description1,
1913  tried_both?" and ":"",
1914  tried_both?description2:"",
1915  connection_describe(TO_CONN(conn)));
1916  rv = -1;
1917  goto done;
1918  }
1919 
1920  done:
1921  if (new_body) {
1922  if (rv == 0) {
1923  /* success! */
1924  tor_free(*bodyp);
1925  *bodyp = new_body;
1926  *bodylenp = new_len;
1927  } else {
1928  tor_free(new_body);
1929  }
1930  }
1931 
1932  return rv;
1933 }
1934 
1935 /**
1936  * Total number of bytes downloaded of each directory purpose, when
1937  * bootstrapped, and when not bootstrapped.
1938  *
1939  * (For example, the number of bytes downloaded of purpose p while
1940  * not fully bootstrapped is total_dl[p][false].)
1941  **/
1942 static uint64_t total_dl[DIR_PURPOSE_MAX_][2];
1943 
1944 /**
1945  * Heartbeat: dump a summary of how many bytes of which purpose we've
1946  * downloaded, when bootstrapping and when not bootstrapping.
1947  **/
1948 void
1950 {
1951  const or_options_t *options = get_options();
1952  for (int bootstrapped = 0; bootstrapped < 2; ++bootstrapped) {
1953  smartlist_t *lines = smartlist_new();
1954  for (int i=0; i < DIR_PURPOSE_MAX_; ++i) {
1955  uint64_t n = total_dl[i][bootstrapped];
1956  if (n == 0)
1957  continue;
1958  if (options->SafeLogging_ != SAFELOG_SCRUB_NONE &&
1960  continue;
1961  smartlist_add_asprintf(lines, "%"PRIu64" (%s)",
1963  }
1964 
1965  if (smartlist_len(lines) > 0) {
1966  char *log_line = smartlist_join_strings(lines, "; ", 0, NULL);
1967  log_notice(LD_NET, "While %sbootstrapping, fetched this many bytes: %s",
1968  bootstrapped?"not ":"", log_line);
1969  tor_free(log_line);
1970 
1971  SMARTLIST_FOREACH(lines, char *, s, tor_free(s));
1972  }
1973  smartlist_free(lines);
1974  }
1975 }
1976 
1977 /** We are a client, and we've finished reading the server's
1978  * response. Parse it and act appropriately.
1979  *
1980  * If we're still happy with using this directory server in the future, return
1981  * 0. Otherwise return -1; and the caller should consider trying the request
1982  * again.
1983  *
1984  * The caller will take care of marking the connection for close.
1985  */
1986 static int
1988 {
1989  char *body = NULL;
1990  char *headers = NULL;
1991  char *reason = NULL;
1992  size_t body_len = 0;
1993  int status_code;
1994  time_t date_header = 0;
1995  long apparent_skew;
1996  compress_method_t compression;
1997  int skewed = 0;
1998  int rv;
1999  int allow_partial = (conn->base_.purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
2000  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO ||
2001  conn->base_.purpose == DIR_PURPOSE_FETCH_MICRODESC);
2002  size_t received_bytes;
2003  const int anonymized_connection =
2004  purpose_needs_anonymity(conn->base_.purpose,
2005  conn->router_purpose,
2006  conn->requested_resource);
2007 
2008  received_bytes = connection_get_inbuf_len(TO_CONN(conn));
2009 
2010  log_debug(LD_DIR, "Downloaded %"TOR_PRIuSZ" bytes on connection of purpose "
2011  "%s; bootstrap %d%%",
2012  received_bytes,
2013  dir_conn_purpose_to_string(conn->base_.purpose),
2014  control_get_bootstrap_percent());
2015  {
2016  bool bootstrapped = control_get_bootstrap_percent() == 100;
2017  total_dl[conn->base_.purpose][bootstrapped] += received_bytes;
2018  }
2019 
2021  &headers, MAX_HEADERS_SIZE,
2022  &body, &body_len, MAX_DIR_DL_SIZE,
2023  allow_partial)) {
2024  case -1: /* overflow */
2025  log_warn(LD_PROTOCOL,
2026  "'fetch' response too large (%s). Closing.",
2027  connection_describe(TO_CONN(conn)));
2028  return -1;
2029  case 0:
2030  log_info(LD_HTTP,
2031  "'fetch' response not all here, but we're at eof. Closing.");
2032  return -1;
2033  /* case 1, fall through */
2034  }
2035 
2036  if (parse_http_response(headers, &status_code, &date_header,
2037  &compression, &reason) < 0) {
2038  log_warn(LD_HTTP,"Unparseable headers (%s). Closing.",
2039  connection_describe(TO_CONN(conn)));
2040  rv = -1;
2041  goto done;
2042  }
2043  if (!reason) reason = tor_strdup("[no reason given]");
2044 
2046  "Received response on %s: %d %s "
2047  "(purpose: %d, response size: %"TOR_PRIuSZ
2048 #ifdef MEASUREMENTS_21206
2049  ", data cells received: %d, data cells sent: %d"
2050 #endif
2051  ", compression: %d)",
2053  status_code,
2054  escaped(reason), conn->base_.purpose,
2055  (received_bytes),
2056 #ifdef MEASUREMENTS_21206
2057  conn->data_cells_received, conn->data_cells_sent,
2058 #endif
2059  compression);
2060 
2061  if (conn->guard_state) {
2062  /* we count the connection as successful once we can read from it. We do
2063  * not, however, delay use of the circuit here, since it's just for a
2064  * one-hop directory request. */
2065  /* XXXXprop271 note that this will not do the right thing for other
2066  * waiting circuits that would be triggered by this circuit becoming
2067  * complete/usable. But that's ok, I think.
2068  */
2070  circuit_guard_state_free(conn->guard_state);
2071  conn->guard_state = NULL;
2072  }
2073 
2074  /* now check if it's got any hints for us about our IP address. */
2075  if (conn->dirconn_direct) {
2076  char *guess = http_get_header(headers, X_ADDRESS_HEADER);
2077  if (guess) {
2078  tor_addr_t addr;
2079  if (tor_addr_parse(&addr, guess) < 0) {
2080  log_debug(LD_DIR, "Malformed X-Your-Address-Is header %s. Ignoring.",
2081  escaped(guess));
2082  } else {
2083  relay_address_new_suggestion(&addr, &TO_CONN(conn)->addr, NULL);
2084  }
2085  tor_free(guess);
2086  }
2087  }
2088 
2089  if (date_header > 0) {
2090  /* The date header was written very soon after we sent our request,
2091  * so compute the skew as the difference between sending the request
2092  * and the date header. (We used to check now-date_header, but that's
2093  * inaccurate if we spend a lot of time downloading.)
2094  */
2095  apparent_skew = conn->base_.timestamp_last_write_allowed - date_header;
2096  if (labs(apparent_skew)>ALLOW_DIRECTORY_TIME_SKEW) {
2097  int trusted = router_digest_is_trusted_dir(conn->identity_digest);
2098  clock_skew_warning(TO_CONN(conn), apparent_skew, trusted, LD_HTTP,
2099  "directory", "DIRSERV");
2100  skewed = 1; /* don't check the recommended-versions line */
2101  } else {
2102  log_debug(LD_HTTP, "Time on received directory is within tolerance; "
2103  "we are %ld seconds skewed. (That's okay.)", apparent_skew);
2104  }
2105  }
2106  (void) skewed; /* skewed isn't used yet. */
2107 
2108  if (status_code == 503) {
2109  routerstatus_t *rs;
2110  dir_server_t *ds;
2111  const char *id_digest = conn->identity_digest;
2112  log_info(LD_DIR,"Received http status code %d (%s) from server "
2113  "%s. I'll try again soon.",
2114  status_code, escaped(reason),
2116  time_t now = approx_time();
2117  if ((rs = router_get_mutable_consensus_status_by_id(id_digest)))
2118  rs->last_dir_503_at = now;
2119  if ((ds = router_get_fallback_dirserver_by_digest(id_digest)))
2120  ds->fake_status.last_dir_503_at = now;
2121 
2122  rv = -1;
2123  goto done;
2124  }
2125 
2126  if (dir_client_decompress_response_body(&body, &body_len,
2127  conn, compression, anonymized_connection) < 0) {
2128  rv = -1;
2129  goto done;
2130  }
2131 
2132  response_handler_args_t args;
2133  memset(&args, 0, sizeof(args));
2134  args.status_code = status_code;
2135  args.reason = reason;
2136  args.body = body;
2137  args.body_len = body_len;
2138  args.headers = headers;
2139 
2140  switch (conn->base_.purpose) {
2142  rv = handle_response_fetch_consensus(conn, &args);
2143  break;
2145  rv = handle_response_fetch_certificate(conn, &args);
2146  break;
2148  rv = handle_response_fetch_status_vote(conn, &args);
2149  break;
2152  break;
2155  rv = handle_response_fetch_desc(conn, &args);
2156  break;
2158  rv = handle_response_fetch_microdesc(conn, &args);
2159  break;
2161  rv = handle_response_upload_dir(conn, &args);
2162  break;
2164  rv = handle_response_upload_signatures(conn, &args);
2165  break;
2167  rv = handle_response_upload_vote(conn, &args);
2168  break;
2170  rv = handle_response_upload_hsdesc(conn, &args);
2171  break;
2173  rv = handle_response_fetch_hsdesc_v3(conn, &args);
2174  break;
2175  default:
2177  rv = -1;
2178  break;
2179  }
2180 
2181  done:
2182  tor_free(body);
2183  tor_free(headers);
2184  tor_free(reason);
2185  return rv;
2186 }
2187 
2188 /**
2189  * Handler function: processes a response to a request for a networkstatus
2190  * consensus document by checking the consensus, storing it, and marking
2191  * router requests as reachable.
2192  **/
2193 STATIC int
2195  const response_handler_args_t *args)
2196 {
2198  const int status_code = args->status_code;
2199  const char *body = args->body;
2200  const size_t body_len = args->body_len;
2201  const char *reason = args->reason;
2202  const time_t now = approx_time();
2203 
2204  const char *consensus;
2205  char *new_consensus = NULL;
2206  const char *sourcename;
2207 
2208  int r;
2209  const char *flavname = conn->requested_resource;
2210  if (status_code != 200) {
2211  int severity = (status_code == 304) ? LOG_INFO : LOG_WARN;
2212  tor_log(severity, LD_DIR,
2213  "Received http status code %d (%s) from server "
2214  "%s while fetching consensus directory.",
2215  status_code, escaped(reason),
2217  networkstatus_consensus_download_failed(status_code, flavname);
2218  return -1;
2219  }
2220 
2221  if (looks_like_a_consensus_diff(body, body_len)) {
2222  /* First find our previous consensus. Maybe it's in ram, maybe not. */
2223  cached_dir_t *cd = dirserv_get_consensus(flavname);
2224  const char *consensus_body = NULL;
2225  size_t consensus_body_len;
2226  tor_mmap_t *mapped_consensus = NULL;
2227  if (cd) {
2228  consensus_body = cd->dir;
2229  consensus_body_len = cd->dir_len;
2230  } else {
2231  mapped_consensus = networkstatus_map_cached_consensus(flavname);
2232  if (mapped_consensus) {
2233  consensus_body = mapped_consensus->data;
2234  consensus_body_len = mapped_consensus->size;
2235  }
2236  }
2237  if (!consensus_body) {
2238  log_warn(LD_DIR, "Received a consensus diff, but we can't find "
2239  "any %s-flavored consensus in our current cache.",flavname);
2240  tor_munmap_file(mapped_consensus);
2242  // XXXX if this happens too much, see below
2243  return -1;
2244  }
2245 
2246  new_consensus = consensus_diff_apply(consensus_body, consensus_body_len,
2247  body, body_len);
2248  tor_munmap_file(mapped_consensus);
2249  if (new_consensus == NULL) {
2250  log_warn(LD_DIR, "Could not apply consensus diff received from server "
2251  "%s", connection_describe_peer(TO_CONN(conn)));
2252  // XXXX If this happens too many times, we should maybe not use
2253  // XXXX this directory for diffs any more?
2255  return -1;
2256  }
2257  log_info(LD_DIR, "Applied consensus diff (size %d) from server "
2258  "%s, resulting in a new consensus document (size %d).",
2259  (int)body_len, connection_describe_peer(TO_CONN(conn)),
2260  (int)strlen(new_consensus));
2261  consensus = new_consensus;
2262  sourcename = "generated based on a diff";
2263  } else {
2264  log_info(LD_DIR,"Received consensus directory (body size %d) from server "
2265  "%s", (int)body_len, connection_describe_peer(TO_CONN(conn)));
2266  consensus = body;
2267  sourcename = "downloaded";
2268  }
2269 
2270  if ((r=networkstatus_set_current_consensus(consensus,
2271  strlen(consensus),
2272  flavname, 0,
2273  conn->identity_digest))<0) {
2275  "Unable to load %s consensus directory %s from "
2276  "server %s. I'll try again soon.",
2277  flavname, sourcename,
2280  tor_free(new_consensus);
2281  return -1;
2282  }
2283 
2284  /* If we launched other fetches for this consensus, cancel them. */
2285  connection_dir_close_consensus_fetches(conn, flavname);
2286 
2287  /* update the list of routers and directory guards */
2290  directory_info_has_arrived(now, 0, 0);
2291 
2292  if (authdir_mode_v3(get_options())) {
2295  }
2296  log_info(LD_DIR, "Successfully loaded consensus.");
2297 
2298  tor_free(new_consensus);
2299  return 0;
2300 }
2301 
2302 /**
2303  * Handler function: processes a response to a request for one or more
2304  * authority certificates
2305  **/
2306 static int
2308  const response_handler_args_t *args)
2309 {
2311  const int status_code = args->status_code;
2312  const char *reason = args->reason;
2313  const char *body = args->body;
2314  const size_t body_len = args->body_len;
2315 
2316  if (status_code != 200) {
2317  log_warn(LD_DIR,
2318  "Received http status code %d (%s) from server "
2319  "%s while fetching \"/tor/keys/%s\".",
2320  status_code, escaped(reason),
2322  conn->requested_resource);
2323  connection_dir_download_cert_failed(conn, status_code);
2324  return -1;
2325  }
2326  log_info(LD_DIR,"Received authority certificates (body size %d) from "
2327  "server %s",
2328  (int)body_len, connection_describe_peer(TO_CONN(conn)));
2329 
2330  /*
2331  * Tell trusted_dirs_load_certs_from_string() whether it was by fp
2332  * or fp-sk pair.
2333  */
2334  int src_code = -1;
2335  if (!strcmpstart(conn->requested_resource, "fp/")) {
2336  src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST;
2337  } else if (!strcmpstart(conn->requested_resource, "fp-sk/")) {
2338  src_code = TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST;
2339  }
2340 
2341  if (src_code != -1) {
2342  if (trusted_dirs_load_certs_from_string(body, src_code, 1,
2343  conn->identity_digest)<0) {
2344  log_warn(LD_DIR, "Unable to parse fetched certificates");
2345  /* if we fetched more than one and only some failed, the successful
2346  * ones got flushed to disk so it's safe to call this on them */
2347  connection_dir_download_cert_failed(conn, status_code);
2348  } else {
2349  time_t now = approx_time();
2350  directory_info_has_arrived(now, 0, 0);
2351  log_info(LD_DIR, "Successfully loaded certificates from fetch.");
2352  }
2353  } else {
2354  log_warn(LD_DIR,
2355  "Couldn't figure out what to do with fetched certificates for "
2356  "unknown resource %s",
2357  conn->requested_resource);
2358  connection_dir_download_cert_failed(conn, status_code);
2359  }
2360  return 0;
2361 }
2362 
2363 /**
2364  * Handler function: processes a response to a request for an authority's
2365  * current networkstatus vote.
2366  **/
2367 static int
2369  const response_handler_args_t *args)
2370 {
2372  const int status_code = args->status_code;
2373  const char *reason = args->reason;
2374  const char *body = args->body;
2375  const size_t body_len = args->body_len;
2376 
2377  const char *msg;
2378  int st;
2379  log_notice(LD_DIR,"Got votes (body size %d) from server %s",
2380  (int)body_len, connection_describe_peer(TO_CONN(conn)));
2381  if (status_code != 200) {
2382  log_warn(LD_DIR,
2383  "Received http status code %d (%s) from server "
2384  "%s while fetching \"/tor/status-vote/next/%s.z\".",
2385  status_code, escaped(reason),
2387  conn->requested_resource);
2388  return -1;
2389  }
2390  dirvote_add_vote(body, 0, TO_CONN(conn)->address, &msg, &st);
2391  if (st > 299) {
2392  log_warn(LD_DIR, "Error adding retrieved vote: %s", msg);
2393  } else {
2394  log_info(LD_DIR, "Added vote(s) successfully [msg: %s]", msg);
2395  }
2396 
2397  return 0;
2398 }
2399 
2400 /**
2401  * Handler function: processes a response to a request for the signatures
2402  * that an authority knows about on a given consensus.
2403  **/
2404 static int
2406  const response_handler_args_t *args)
2407 {
2409  const int status_code = args->status_code;
2410  const char *reason = args->reason;
2411  const char *body = args->body;
2412  const size_t body_len = args->body_len;
2413 
2414  const char *msg = NULL;
2415  log_info(LD_DIR,"Got detached signatures (body size %d) from server %s",
2416  (int)body_len,
2418  if (status_code != 200) {
2419  log_warn(LD_DIR,
2420  "Received http status code %d (%s) from server %s while fetching "
2421  "\"/tor/status-vote/next/consensus-signatures.z\".",
2422  status_code, escaped(reason),
2424  return -1;
2425  }
2426  if (dirvote_add_signatures(body, conn->base_.address, &msg)<0) {
2427  log_warn(LD_DIR, "Problem adding detached signatures from %s: %s",
2429  msg?msg:"???");
2430  }
2431 
2432  return 0;
2433 }
2434 
2435 /**
2436  * Handler function: processes a response to a request for a group of server
2437  * descriptors or an extrainfo documents.
2438  **/
2439 static int
2441  const response_handler_args_t *args)
2442 {
2444  conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO);
2445  const int status_code = args->status_code;
2446  const char *reason = args->reason;
2447  const char *body = args->body;
2448  const size_t body_len = args->body_len;
2449 
2450  int was_ei = conn->base_.purpose == DIR_PURPOSE_FETCH_EXTRAINFO;
2451  smartlist_t *which = NULL;
2452  int n_asked_for = 0;
2453  int descriptor_digests = conn->requested_resource &&
2454  !strcmpstart(conn->requested_resource,"d/");
2455  log_info(LD_DIR,"Received %s (body size %d) from server %s",
2456  was_ei ? "extra server info" : "server info",
2457  (int)body_len, connection_describe_peer(TO_CONN(conn)));
2458  if (conn->requested_resource &&
2459  (!strcmpstart(conn->requested_resource,"d/") ||
2460  !strcmpstart(conn->requested_resource,"fp/"))) {
2461  which = smartlist_new();
2463  (descriptor_digests ? 2 : 3),
2464  which, NULL, 0);
2465  n_asked_for = smartlist_len(which);
2466  }
2467  if (status_code != 200) {
2468  int dir_okay = status_code == 404 ||
2469  (status_code == 400 && !strcmp(reason, "Servers unavailable.")) ||
2470  status_code == 301;
2471  /* 404 means that it didn't have them; no big deal.
2472  * Older (pre-0.1.1.8) servers said 400 Servers unavailable instead.
2473  * 301 is considered as an error since Tor does not follow redirects,
2474  * which means we failed to reach the server we wanted. */
2475  log_fn(dir_okay ? LOG_INFO : LOG_WARN, LD_DIR,
2476  "Received http status code %d (%s) from server %s "
2477  "while fetching \"/tor/server/%s\". I'll try again soon.",
2478  status_code, escaped(reason),
2480  conn->requested_resource);
2481  if (!which) {
2483  } else {
2484  dir_routerdesc_download_failed(which, status_code,
2485  conn->router_purpose,
2486  was_ei, descriptor_digests);
2487  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2488  smartlist_free(which);
2489  }
2490  return dir_okay ? 0 : -1;
2491  }
2492  /* Learn the routers, assuming we requested by fingerprint or "all"
2493  * or "authority".
2494  *
2495  * We use "authority" to fetch our own descriptor for
2496  * testing, and to fetch bridge descriptors for bootstrapping. Ignore
2497  * the output of "authority" requests unless we are using bridges,
2498  * since otherwise they'll be the response from reachability tests,
2499  * and we don't really want to add that to our routerlist. */
2500  if (which || (conn->requested_resource &&
2501  (!strcmpstart(conn->requested_resource, "all") ||
2502  (!strcmpstart(conn->requested_resource, "authority") &&
2503  get_options()->UseBridges)))) {
2504  /* as we learn from them, we remove them from 'which' */
2505  if (was_ei) {
2507  descriptor_digests);
2508  } else {
2509  //router_load_routers_from_string(body, NULL, SAVED_NOWHERE, which,
2510  // descriptor_digests, conn->router_purpose);
2511  if (load_downloaded_routers(body, which, descriptor_digests,
2512  conn->router_purpose,
2513  conn->base_.address)) {
2514  time_t now = approx_time();
2515  directory_info_has_arrived(now, 0, 1);
2516  }
2517  }
2518  }
2519  if (which) { /* mark remaining ones as failed */
2520  log_info(LD_DIR, "Received %d/%d %s requested from %s",
2521  n_asked_for-smartlist_len(which), n_asked_for,
2522  was_ei ? "extra-info documents" : "router descriptors",
2524  if (smartlist_len(which)) {
2525  dir_routerdesc_download_failed(which, status_code,
2526  conn->router_purpose,
2527  was_ei, descriptor_digests);
2528  }
2529  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2530  smartlist_free(which);
2531  }
2532 
2533  return 0;
2534 }
2535 
2536 /**
2537  * Handler function: processes a response to a request for a group of
2538  * microdescriptors
2539  **/
2540 STATIC int
2542  const response_handler_args_t *args)
2543 {
2545  const int status_code = args->status_code;
2546  const char *reason = args->reason;
2547  const char *body = args->body;
2548  const size_t body_len = args->body_len;
2549 
2550  smartlist_t *which = NULL;
2551  log_info(LD_DIR,"Received answer to microdescriptor request (status %d, "
2552  "body size %d) from server %s",
2553  status_code, (int)body_len,
2556  !strcmpstart(conn->requested_resource, "d/"));
2557  tor_assert_nonfatal(!fast_mem_is_zero(conn->identity_digest, DIGEST_LEN));
2558  which = smartlist_new();
2560  which, NULL,
2561  DSR_DIGEST256|DSR_BASE64);
2562  if (status_code != 200) {
2563  log_info(LD_DIR, "Received status code %d (%s) from server "
2564  "%s while fetching \"/tor/micro/%s\". I'll try again "
2565  "soon.",
2566  status_code, escaped(reason),
2568  conn->requested_resource);
2569  dir_microdesc_download_failed(which, status_code, conn->identity_digest);
2570  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2571  smartlist_free(which);
2572  return 0;
2573  } else {
2574  smartlist_t *mds;
2575  time_t now = approx_time();
2577  body, body+body_len, SAVED_NOWHERE, 0,
2578  now, which);
2579  if (smartlist_len(which)) {
2580  /* Mark remaining ones as failed. */
2581  dir_microdesc_download_failed(which, status_code, conn->identity_digest);
2582  }
2583  if (mds && smartlist_len(mds)) {
2584  control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
2586  directory_info_has_arrived(now, 0, 1);
2587  }
2588  SMARTLIST_FOREACH(which, char *, cp, tor_free(cp));
2589  smartlist_free(which);
2590  smartlist_free(mds);
2591  }
2592 
2593  return 0;
2594 }
2595 
2596 /**
2597  * Handler function: processes a response to a POST request to upload our
2598  * router descriptor.
2599  **/
2600 static int
2602  const response_handler_args_t *args)
2603 {
2604  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_DIR);
2605  const int status_code = args->status_code;
2606  const char *reason = args->reason;
2607  const char *headers = args->headers;
2608 
2609  switch (status_code) {
2610  case 200: {
2611  dir_server_t *ds =
2613  char *rejected_hdr = http_get_header(headers,
2614  "X-Descriptor-Not-New: ");
2615  if (rejected_hdr) {
2616  if (!strcmp(rejected_hdr, "Yes")) {
2617  log_info(LD_GENERAL,
2618  "Authority '%s' declined our descriptor (not new)",
2619  ds->nickname);
2620  /* XXXX use this information; be sure to upload next one
2621  * sooner. -NM */
2622  /* XXXX++ On further thought, the task above implies that we're
2623  * basing our regenerate-descriptor time on when we uploaded the
2624  * last descriptor, not on the published time of the last
2625  * descriptor. If those are different, that's a bad thing to
2626  * do. -NM */
2627  }
2628  tor_free(rejected_hdr);
2629  }
2630  log_info(LD_GENERAL,"eof (status 200) after uploading server "
2631  "descriptor: finished.");
2633  LOG_NOTICE, "ACCEPTED_SERVER_DESCRIPTOR DIRAUTH=%s:%d",
2634  conn->base_.address, conn->base_.port);
2635 
2636  ds->has_accepted_serverdesc = 1;
2638  control_event_server_status(LOG_NOTICE, "GOOD_SERVER_DESCRIPTOR");
2639  }
2640  break;
2641  case 400:
2642  log_warn(LD_GENERAL,"http status 400 (%s) response from "
2643  "dirserver %s. Please correct.",
2644  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2646  "BAD_SERVER_DESCRIPTOR DIRAUTH=%s:%d REASON=\"%s\"",
2647  conn->base_.address, conn->base_.port, escaped(reason));
2648  break;
2649  default:
2650  log_warn(LD_GENERAL,
2651  "HTTP status %d (%s) was unexpected while uploading "
2652  "descriptor to server %s'. Possibly the server is "
2653  "misconfigured?",
2654  status_code, escaped(reason),
2656  break;
2657  }
2658  /* return 0 in all cases, since we don't want to mark any
2659  * dirservers down just because they don't like us. */
2660 
2661  return 0;
2662 }
2663 
2664 /**
2665  * Handler function: processes a response to POST request to upload our
2666  * own networkstatus vote.
2667  **/
2668 static int
2670  const response_handler_args_t *args)
2671 {
2672  tor_assert(conn->base_.purpose == DIR_PURPOSE_UPLOAD_VOTE);
2673  const int status_code = args->status_code;
2674  const char *reason = args->reason;
2675 
2676  switch (status_code) {
2677  case 200: {
2678  log_notice(LD_DIR,"Uploaded my vote to dirserver %s",
2680  }
2681  break;
2682  case 400:
2683  log_warn(LD_DIR,"http status 400 (%s) response after uploading "
2684  "vote to dirserver %s. Please correct.",
2685  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2686  break;
2687  default:
2688  log_warn(LD_GENERAL,
2689  "HTTP status %d (%s) was unexpected while uploading "
2690  "vote to server %s.",
2691  status_code, escaped(reason),
2693  break;
2694  }
2695  /* return 0 in all cases, since we don't want to mark any
2696  * dirservers down just because they don't like us. */
2697  return 0;
2698 }
2699 
2700 /**
2701  * Handler function: processes a response to POST request to upload our
2702  * view of the signatures on the current consensus.
2703  **/
2704 static int
2706  const response_handler_args_t *args)
2707 {
2709  const int status_code = args->status_code;
2710  const char *reason = args->reason;
2711 
2712  switch (status_code) {
2713  case 200: {
2714  log_notice(LD_DIR,"Uploaded signature(s) to dirserver %s",
2716  }
2717  break;
2718  case 400:
2719  log_warn(LD_DIR,"http status 400 (%s) response after uploading "
2720  "signatures to dirserver %s. Please correct.",
2721  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2722  break;
2723  default:
2724  log_warn(LD_GENERAL,
2725  "HTTP status %d (%s) was unexpected while uploading "
2726  "signatures to server %s.",
2727  status_code, escaped(reason),
2729  break;
2730  }
2731  /* return 0 in all cases, since we don't want to mark any
2732  * dirservers down just because they don't like us. */
2733 
2734  return 0;
2735 }
2736 
2737 /**
2738  * Handler function: processes a response to a request for a v3 hidden service
2739  * descriptor.
2740  **/
2741 STATIC int
2743  const response_handler_args_t *args)
2744 {
2745  const int status_code = args->status_code;
2746  const char *reason = args->reason;
2747  const char *body = args->body;
2748  const size_t body_len = args->body_len;
2749 
2750  tor_assert(conn->hs_ident);
2751 
2752  log_info(LD_REND,"Received v3 hsdesc (body size %d, status %d (%s))",
2753  (int)body_len, status_code, escaped(reason));
2754 
2755  hs_client_dir_fetch_done(conn, reason, body, status_code);
2756  return 0;
2757 }
2758 
2759 /**
2760  * Handler function: processes a response to a POST request to upload an
2761  * hidden service descriptor.
2762  **/
2763 static int
2765  const response_handler_args_t *args)
2766 {
2767  const int status_code = args->status_code;
2768  const char *reason = args->reason;
2769 
2770  tor_assert(conn);
2772 
2773  log_info(LD_REND, "Uploaded hidden service descriptor (status %d "
2774  "(%s))",
2775  status_code, escaped(reason));
2776  /* For this directory response, it MUST have an hidden service identifier on
2777  * this connection. */
2778  tor_assert(conn->hs_ident);
2779  switch (status_code) {
2780  case 200:
2781  log_info(LD_REND, "Uploading hidden service descriptor: "
2782  "finished with status 200 (%s)", escaped(reason));
2783  hs_control_desc_event_uploaded(conn->hs_ident, conn->identity_digest);
2784  break;
2785  case 400:
2786  log_fn(LOG_PROTOCOL_WARN, LD_REND,
2787  "Uploading hidden service descriptor: http "
2788  "status 400 (%s) response from dirserver "
2789  "%s. Malformed hidden service descriptor?",
2790  escaped(reason), connection_describe_peer(TO_CONN(conn)));
2791  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2792  "UPLOAD_REJECTED");
2793  break;
2794  default:
2795  log_warn(LD_REND, "Uploading hidden service descriptor: http "
2796  "status %d (%s) response unexpected (server "
2797  "%s').",
2798  status_code, escaped(reason),
2800  hs_control_desc_event_failed(conn->hs_ident, conn->identity_digest,
2801  "UNEXPECTED");
2802  break;
2803  }
2804 
2805  return 0;
2806 }
2807 
2808 /** Called when a directory connection reaches EOF. */
2809 int
2811 {
2812  int retval;
2813  if (conn->base_.state != DIR_CONN_STATE_CLIENT_READING) {
2814  log_info(LD_HTTP,"conn reached eof, not reading. [state=%d] Closing.",
2815  conn->base_.state);
2816  connection_close_immediate(TO_CONN(conn)); /* error: give up on flushing */
2817  connection_mark_for_close(TO_CONN(conn));
2818  return -1;
2819  }
2820 
2821  retval = connection_dir_client_reached_eof(conn);
2822  if (retval == 0) /* success */
2823  conn->base_.state = DIR_CONN_STATE_CLIENT_FINISHED;
2824  connection_mark_for_close(TO_CONN(conn));
2825  return retval;
2826 }
2827 /** We are closing a dir connection: If <b>dir_conn</b> is a dir connection
2828  * that tried to fetch an HS descriptor, check if it successfully fetched it,
2829  * or if we need to try again. */
2830 void
2832 {
2833  connection_t *conn = TO_CONN(dir_conn);
2834 
2835  /* Check for v3 rend desc fetch */
2836  if (conn->purpose == DIR_PURPOSE_FETCH_HSDESC &&
2837  dir_conn->hs_ident &&
2838  !ed25519_public_key_is_zero(&dir_conn->hs_ident->identity_pk)) {
2839  hs_client_refetch_hsdesc(&dir_conn->hs_ident->identity_pk);
2840  }
2841 }
2842 
2843 /** Array of compression methods to use (if supported) for requesting
2844  * compressed data, ordered from best to worst. */
2846  LZMA_METHOD,
2847  ZSTD_METHOD,
2848  ZLIB_METHOD,
2849  GZIP_METHOD,
2850  NO_METHOD
2851 };
2852 
2853 /** Array of allowed compression methods to use (if supported) when receiving a
2854  * response from a request that was required to be anonymous. */
2856  ZLIB_METHOD,
2857  GZIP_METHOD,
2858  NO_METHOD
2859 };
2860 
2861 /** Return a newly allocated string containing a comma separated list of
2862  * supported encodings. */
2863 STATIC char *
2865 {
2866  smartlist_t *methods = smartlist_new();
2867  char *header = NULL;
2868  compress_method_t method;
2869  unsigned i;
2870 
2871  for (i = 0; i < ARRAY_LENGTH(client_meth_pref); ++i) {
2872  method = client_meth_pref[i];
2873  if (tor_compress_supports_method(method))
2874  smartlist_add(methods, (char *)compression_method_get_name(method));
2875  }
2876 
2877  header = smartlist_join_strings(methods, ", ", 0, NULL);
2878  smartlist_free(methods);
2879 
2880  return header;
2881 }
2882 
2883 /** Check if the given compression method is allowed for a connection that is
2884  * supposed to be anonymous. Returns 1 if the compression method is allowed,
2885  * otherwise 0. */
2886 STATIC int
2888 {
2889  unsigned u;
2890 
2892  ++u) {
2893  compress_method_t allowed_method =
2895 
2896  if (! tor_compress_supports_method(allowed_method))
2897  continue;
2898 
2899  if (method == allowed_method)
2900  return 1;
2901  }
2902 
2903  return 0;
2904 }
2905 
2906 /** Log a warning when a remote server has sent us a document using a
2907  * compression method that is not allowed for anonymous directory requests. */
2908 STATIC void
2910 {
2911  log_fn(LOG_PROTOCOL_WARN, LD_HTTP,
2912  "Received a %s HTTP response, which is not "
2913  "allowed for anonymous directory requests.",
2915 }
2916 
2917 /* We just got a new consensus! If there are other in-progress requests
2918  * for this consensus flavor (for example because we launched several in
2919  * parallel), cancel them.
2920  *
2921  * We do this check here (not just in
2922  * connection_ap_handshake_attach_circuit()) to handle the edge case where
2923  * a consensus fetch begins and ends before some other one tries to attach to
2924  * a circuit, in which case the other one won't know that we're all happy now.
2925  *
2926  * Don't mark the conn that just gave us the consensus -- otherwise we
2927  * would end up double-marking it when it cleans itself up.
2928  */
2929 static void
2930 connection_dir_close_consensus_fetches(dir_connection_t *except_this_one,
2931  const char *resource)
2932 {
2933  smartlist_t *conns_to_close =
2935  resource);
2936  SMARTLIST_FOREACH_BEGIN(conns_to_close, dir_connection_t *, d) {
2937  if (d == except_this_one)
2938  continue;
2939  log_info(LD_DIR, "Closing consensus fetch (to %s) since one "
2940  "has just arrived.", connection_describe_peer(TO_CONN(d)));
2941  connection_mark_for_close(TO_CONN(d));
2942  } SMARTLIST_FOREACH_END(d);
2943  smartlist_free(conns_to_close);
2944 }
2945 /** Called when one or more routerdesc (or extrainfo, if <b>was_extrainfo</b>)
2946  * fetches have failed (with uppercase fingerprints listed in <b>failed</b>,
2947  * either as descriptor digests or as identity digests based on
2948  * <b>was_descriptor_digests</b>).
2949  */
2950 static void
2952  int router_purpose,
2953  int was_extrainfo, int was_descriptor_digests)
2954 {
2955  char digest[DIGEST_LEN];
2956  time_t now = time(NULL);
2958  if (!was_descriptor_digests) {
2959  if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
2960  tor_assert(!was_extrainfo);
2962  }
2963  return; /* FFFF should implement for other-than-router-purpose someday */
2964  }
2965  SMARTLIST_FOREACH_BEGIN(failed, const char *, cp) {
2966  download_status_t *dls = NULL;
2967  if (base16_decode(digest, DIGEST_LEN, cp, strlen(cp)) != DIGEST_LEN) {
2968  log_warn(LD_BUG, "Malformed fingerprint in list: %s", escaped(cp));
2969  continue;
2970  }
2971  if (was_extrainfo) {
2972  signed_descriptor_t *sd =
2974  if (sd)
2975  dls = &sd->ei_dl_status;
2976  } else {
2978  }
2979  if (!dls)
2980  continue;
2981  download_status_increment_failure(dls, status_code, cp, server, now);
2982  } SMARTLIST_FOREACH_END(cp);
2983 
2984  /* No need to relaunch descriptor downloads here: we already do it
2985  * every 10 or 60 seconds (FOO_DESCRIPTOR_RETRY_INTERVAL) in main.c. */
2986 }
2987 
2988 /** Called when a connection to download microdescriptors from relay with
2989  * <b>dir_id</b> has failed in whole or in part. <b>failed</b> is a list
2990  * of every microdesc digest we didn't get. <b>status_code</b> is the http
2991  * status code we received. Reschedule the microdesc downloads as
2992  * appropriate. */
2993 static void
2995  int status_code, const char *dir_id)
2996 {
2997  networkstatus_t *consensus
2999  routerstatus_t *rs;
3000  download_status_t *dls;
3001  time_t now = time(NULL);
3003 
3004  if (! consensus)
3005  return;
3006 
3007  /* We failed to fetch a microdescriptor from 'dir_id', note it down
3008  * so that we don't try the same relay next time... */
3010 
3011  SMARTLIST_FOREACH_BEGIN(failed, const char *, d) {
3013  if (!rs)
3014  continue;
3015  dls = &rs->dl_status;
3016 
3017  { /* Increment the failure count for this md fetch */
3018  char buf[BASE64_DIGEST256_LEN+1];
3019  digest256_to_base64(buf, d);
3020  log_info(LD_DIR, "Failed to download md %s from %s",
3021  buf, hex_str(dir_id, DIGEST_LEN));
3022  download_status_increment_failure(dls, status_code, buf,
3023  server, now);
3024  }
3025  } SMARTLIST_FOREACH_END(d);
3026 }
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1349
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
Definition: address.c:235
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1164
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
void tor_addr_port_copy(tor_addr_port_t *dest, const tor_addr_port_t *source)
Definition: address.c:2121
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
#define fmt_addr(a)
Definition: address.h:239
time_t approx_time(void)
Definition: approx_time.c:32
void authority_cert_dl_failed(const char *id_digest, const char *signing_key_digest, int status)
Definition: authcert.c:681
int trusted_dirs_load_certs_from_string(const char *contents, int source, int flush, const char *source_dir)
Definition: authcert.c:373
Header file for authcert.c.
Header file for directory authority mode.
Header for backtrace.c.
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
void retry_bridge_descriptor_fetch_directly(const char *digest)
Definition: bridges.c:724
Header file for circuitbuild.c.
Cached large directory object structure.
#define ARRAY_LENGTH(x)
int tor_compress_supports_method(compress_method_t method)
Definition: compress.c:304
const char * compression_method_get_name(compress_method_t method)
Definition: compress.c:364
const char * compression_method_get_human_name(compress_method_t method)
Definition: compress.c:390
compress_method_t detect_compression_method(const char *in, size_t in_len)
Definition: compress.c:284
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
Headers for compress.c.
compress_method_t
Definition: compress.h:21
const or_options_t * get_options(void)
Definition: config.c:919
Header file for config.c.
void config_line_prepend(config_line_t **lst, const char *key, const char *val)
Definition: confline.c:53
Header for confline.c.
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:5860
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:4248
dir_connection_t * dir_connection_new(int socket_family)
Definition: connection.c:561
void connection_close_immediate(connection_t *conn)
Definition: connection.c:1043
const char * connection_describe(const connection_t *conn)
Definition: connection.c:543
smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4871
const char * connection_describe_peer(const connection_t *conn)
Definition: connection.c:528
char * alloc_http_authenticator(const char *authenticator)
Definition: connection.c:4998
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2396
Header file for connection.c.
#define CONN_TYPE_DIR
Definition: connection.h:55
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)
Header file for connection_edge.c.
char * consensus_diff_apply(const char *consensus, size_t consensus_len, const char *diff, size_t diff_len)
Definition: consdiff.c:1381
int looks_like_a_consensus_diff(const char *document, size_t len)
Definition: consdiff.c:1416
Header for consdiff.c.
void control_event_boot_dir(bootstrap_status_t status, int progress)
int control_event_server_status(int severity, const char *format,...)
Header file for control_events.c.
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
#define HEX_DIGEST256_LEN
Definition: crypto_digest.h:37
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
void digest256_to_base64(char *d64, const char *digest)
Header for crypto_format.c.
Common functions for cryptographic routines.
Compile-time assertions: CTASSERT(expression).
#define CTASSERT(x)
Definition: ctassert.h:44
const char * routerstatus_describe(const routerstatus_t *rs)
Definition: describe.c:203
Header file for describe.c.
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Client/server directory connection structure.
Trusted/fallback directory server structure.
STATIC void warn_disallowed_anonymous_compression_method(compress_method_t method)
Definition: dirclient.c:2909
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1015
void directory_request_set_or_addr_port(directory_request_t *req, const tor_addr_port_t *p)
Definition: dirclient.c:953
static void connection_dir_download_cert_failed(dir_connection_t *conn, int status_code)
Definition: dirclient.c:808
void directory_get_from_all_authorities(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
Definition: dirclient.c:580
static int handle_response_upload_hsdesc(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2764
static void connection_dir_download_routerdesc_failed(dir_connection_t *conn)
Definition: dirclient.c:765
void directory_request_set_if_modified_since(directory_request_t *req, time_t if_modified_since)
Definition: dirclient.c:1040
#define ALLOW_DIRECTORY_TIME_SKEW
Definition: dirclient.c:73
static void dir_microdesc_download_failed(smartlist_t *failed, int status_code, const char *dir_id)
Definition: dirclient.c:2994
void connection_dir_client_request_failed(dir_connection_t *conn)
Definition: dirclient.c:704
static void connection_dir_retry_bridges(smartlist_t *descs)
Definition: dirclient.c:747
static compress_method_t client_meth_allowed_anonymous_compression[]
Definition: dirclient.c:2855
static int directory_command_should_use_begindir(const or_options_t *options, const directory_request_t *req, const char **reason)
Definition: dirclient.c:864
STATIC int handle_response_fetch_consensus(dir_connection_t *conn, const response_handler_args_t *args)
Definition: dirclient.c:2194
static void connection_dir_bridge_routerdesc_failed(dir_connection_t *conn)
Definition: dirclient.c:784
STATIC int should_use_directory_guards(const or_options_t *options)
Definition: dirclient.c:309
void directory_request_set_dir_addr_port(directory_request_t *req, const tor_addr_port_t *p)
Definition: dirclient.c:964
#define MAX_DIR_DL_SIZE
Definition: dirclient.c:69
void connection_dir_client_refetch_hsdesc_if_needed(dir_connection_t *dir_conn)
Definition: dirclient.c:2831
static int handle_response_fetch_certificate(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2307
void directory_request_set_guard_state(directory_request_t *req, circuit_guard_state_t *state)
Definition: dirclient.c:1092
void dirclient_dump_total_dls(void)
Definition: dirclient.c:1949
STATIC int allowed_anonymous_connection_compression_method(compress_method_t method)
Definition: dirclient.c:2887
void directory_request_set_indirection(directory_request_t *req, dir_indirection_t indirection)
Definition: dirclient.c:1002
void directory_request_free_(directory_request_t *req)
Definition: dirclient.c:940
static int handle_response_upload_vote(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2669
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *status)
Definition: dirclient.c:1117
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:919
static int connection_dir_client_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:1987
STATIC char * accept_encoding_header(void)
Definition: dirclient.c:2864
STATIC const char * dir_conn_purpose_to_string(int purpose)
Definition: dirclient.c:97
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
static int handle_response_fetch_desc(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2440
static int load_downloaded_routers(const char *body, smartlist_t *which, int descriptor_digests, int router_purpose, const char *source)
Definition: dirclient.c:1771
void directory_request_set_router_purpose(directory_request_t *req, uint8_t router_purpose)
Definition: dirclient.c:986
static int handle_response_fetch_status_vote(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2368
static int body_is_plausible(const char *body, size_t body_len, int purpose)
Definition: dirclient.c:1740
static compress_method_t client_meth_pref[]
Definition: dirclient.c:2845
static void dir_consensus_request_set_additional_headers(directory_request_t *req, const char *resource)
Definition: dirclient.c:364
static int handle_response_fetch_detached_signatures(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2405
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
static void copy_ipv6_address(char *destination, const char *source, size_t len, int decorate)
Definition: dirclient.c:1489
void directory_request_set_directory_id_digest(directory_request_t *req, const char *digest)
Definition: dirclient.c:974
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
static int handle_response_upload_signatures(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2705
static int handle_response_upload_dir(dir_connection_t *, const response_handler_args_t *)
Definition: dirclient.c:2601
STATIC int handle_response_fetch_hsdesc_v3(dir_connection_t *conn, const response_handler_args_t *args)
Definition: dirclient.c:2742
void directory_initiate_request(directory_request_t *request)
Definition: dirclient.c:1222
static int directory_request_set_dir_from_routerstatus(directory_request_t *req)
Definition: dirclient.c:1129
static int directory_request_dir_contact_info_specified(const directory_request_t *req)
Definition: dirclient.c:1102
void directory_request_set_payload(directory_request_t *req, const char *payload, size_t payload_len)
Definition: dirclient.c:1026
int connection_dir_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:2810
static void directory_send_command(dir_connection_t *conn, const int direct, const directory_request_t *req)
Definition: dirclient.c:1506
static char * directory_get_consensus_url(const char *resource)
Definition: dirclient.c:1443
STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose, const char *resource)
Definition: dirclient.c:133
static int dirind_is_anon(dir_indirection_t ind)
Definition: dirclient.c:605
void directory_request_fetch_set_hs_ident(directory_request_t *req, const hs_ident_dir_conn_t *ident)
Definition: dirclient.c:1080
STATIC int handle_response_fetch_microdesc(dir_connection_t *conn, const response_handler_args_t *args)
Definition: dirclient.c:2541
int router_supports_extrainfo(const char *identity_digest, int is_authority)
Definition: dirclient.c:175
int directories_have_accepted_server_descriptor(void)
Definition: dirclient.c:198
static uint64_t total_dl[DIR_PURPOSE_MAX_][2]
Definition: dirclient.c:1942
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:2951
void directory_request_add_header(directory_request_t *req, const char *key, const char *val)
Definition: dirclient.c:1054
static int compare_strs_(const void **a, const void **b)
Definition: dirclient.c:1425
void directory_request_upload_set_hs_ident(directory_request_t *req, const hs_ident_dir_conn_t *ident)
Definition: dirclient.c:1066
Header file for dirclient.c.
dir_indirection_t
Definition: dirclient.h:34
@ DIRIND_ONEHOP
Definition: dirclient.h:37
@ DIRIND_ANON_DIRPORT
Definition: dirclient.h:43
@ DIRIND_ANONYMOUS
Definition: dirclient.h:39
@ DIRIND_DIRECT_CONN
Definition: dirclient.h:41
struct directory_request_t directory_request_t
Definition: dirclient.h:52
int dirclient_fetches_from_authorities(const or_options_t *options)
Header for feature/dirclient/dirclient_modes.c.
int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
Definition: directory.c:113
char * authdir_type_to_string(dirinfo_type_t auth)
Definition: directory.c:160
int dir_split_resource_into_fingerprint_pairs(const char *res, smartlist_t *pairs_out)
Definition: directory.c:581
int parse_http_response(const char *headers, int *code, time_t *date, compress_method_t *compression, char **reason)
Definition: directory.c:360
char * http_get_header(const char *headers, const char *which)
Definition: directory.c:325
int dir_split_resource_into_fingerprints(const char *resource, smartlist_t *fp_out, int *compressed_out, int flags)
Definition: directory.c:640
Header file for directory.c.
#define DIR_PURPOSE_FETCH_EXTRAINFO
Definition: directory.h:39
#define DIR_PURPOSE_FETCH_CERTIFICATE
Definition: directory.h:57
#define DIR_PURPOSE_UPLOAD_HSDESC
Definition: directory.h:67
#define DIR_PURPOSE_FETCH_MICRODESC
Definition: directory.h:65
#define DIR_CONN_STATE_CONNECTING
Definition: directory.h:20
#define DIR_CONN_STATE_CLIENT_FINISHED
Definition: directory.h:26
#define DIR_CONN_STATE_CLIENT_READING
Definition: directory.h:24
#define DIR_PURPOSE_UPLOAD_VOTE
Definition: directory.h:43
#define DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
Definition: directory.h:51
#define DIR_PURPOSE_IS_UPLOAD(p)
Definition: directory.h:77
#define DIR_PURPOSE_FETCH_CONSENSUS
Definition: directory.h:54
#define DIR_PURPOSE_SERVER
Definition: directory.h:60
#define DIR_PURPOSE_FETCH_SERVERDESC
Definition: directory.h:36
#define DIR_PURPOSE_UPLOAD_SIGNATURES
Definition: directory.h:45
#define DIR_CONN_STATE_CLIENT_SENDING
Definition: directory.h:22
#define DIR_PURPOSE_FETCH_STATUS_VOTE
Definition: directory.h:48
#define DIR_PURPOSE_HAS_FETCHED_HSDESC
Definition: directory.h:72
#define DIR_PURPOSE_UPLOAD_DIR
Definition: directory.h:41
#define DIR_PURPOSE_FETCH_HSDESC
Definition: directory.h:69
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:160
const tor_addr_port_t * trusted_dir_server_get_dirport_exact(const dir_server_t *ds, auth_dirport_usage_t usage, int addr_family)
Definition: dirlist.c:503
auth_dirport_usage_t auth_dirport_usage_for_purpose(int purpose)
Definition: dirlist.c:304
const tor_addr_port_t * trusted_dir_server_get_dirport(const dir_server_t *ds, auth_dirport_usage_t usage, int addr_family)
Definition: dirlist.c:529
dir_server_t * router_get_fallback_dirserver_by_digest(const char *digest)
Definition: dirlist.c:181
Header file for dirlist.c.
auth_dirport_usage_t
Definition: dirlist.h:22
@ AUTH_USAGE_VOTING
Definition: dirlist.h:30
cached_dir_t * dirserv_get_consensus(const char *flavor_name)
Definition: dirserv.c:201
Header file for dirserv.c.
int dirvote_add_signatures(const char *detached_signatures_body, const char *source, const char **msg)
Definition: dirvote.c:3720
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
Header file for dirvote.c.
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
Header file for dlstatus.c.
Entry connection structure.
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2534
void entry_guard_cancel(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2513
int entry_list_is_constrained(const or_options_t *options)
Definition: entrynodes.c:3392
guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
Definition: entrynodes.c:2486
const node_t * guards_choose_dirguard(uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
Definition: entrynodes.c:3813
Header file for circuitbuild.c.
const char * escaped(const char *s)
Definition: escape.c:126
Header file for fp_pair.c.
Header file for hs_cache.c.
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Definition: hs_client.c:2037
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
Header file containing client data for the HS subsystem.
void hs_control_desc_event_uploaded(const hs_ident_dir_conn_t *ident, const char *hsdir_id_digest)
Definition: hs_control.c:159
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
Header file containing control port event related code.
hs_ident_dir_conn_t * hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src)
Definition: hs_ident.c:47
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:590
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_HTTP
Definition: log.h:76
#define LD_REND
Definition: log.h:84
#define LD_PROTOCOL
Definition: log.h:72
#define LOG_DEBUG
Definition: log.h:42
#define LD_BUG
Definition: log.h:86
#define LD_NET
Definition: log.h:66
#define LD_GENERAL
Definition: log.h:62
#define LD_DIR
Definition: log.h:88
#define LOG_NOTICE
Definition: log.h:50
#define LOG_WARN
Definition: log.h:53
#define LOG_INFO
Definition: log.h:45
void connection_watch_events(connection_t *conn, watchable_events_t events)
Definition: mainloop.c:493
void directory_all_unreachable(time_t now)
Definition: mainloop.c:1097
void connection_start_reading(connection_t *conn)
Definition: mainloop.c:631
void directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
Definition: mainloop.c:1115
Header file for mainloop.c.
@ WRITE_EVENT
Definition: mainloop.h:38
@ READ_EVENT
Definition: mainloop.h:37
#define tor_free(p)
Definition: malloc.h:52
void microdesc_note_outdated_dirserver(const char *relay_digest)
Definition: microdesc.c:111
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
microdesc_cache_t * get_microdesc_cache(void)
Definition: microdesc.c:251
void update_microdescs_from_networkstatus(time_t now)
Definition: microdesc.c:1033
Header file for microdesc.c.
networkstatus_t * networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
void routers_update_all_from_networkstatus(time_t now, int dir_version)
int networkstatus_parse_flavor_name(const char *flavname)
int networkstatus_set_current_consensus(const char *consensus, size_t consensus_len, const char *flavor, unsigned flags, const char *source_dir)
download_status_t * router_get_dl_status_by_descriptor_digest(const char *d)
void update_certificate_downloads(time_t now)
tor_mmap_t * networkstatus_map_cached_consensus(const char *flavorname)
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)
routerstatus_t * router_get_mutable_consensus_status_by_id(const char *digest)
routerstatus_t * router_get_mutable_consensus_status_by_descriptor_digest(networkstatus_t *consensus, const char *digest)
void networkstatus_consensus_download_failed(int status_code, const char *flavname)
Header file for networkstatus.c.
Networkstatus consensus/vote structure.
const routerstatus_t * router_pick_fallback_dirserver(dirinfo_type_t type, int flags)
Definition: node_select.c:1051
const routerstatus_t * router_pick_directory_server(dirinfo_type_t type, int flags)
Definition: node_select.c:72
const routerstatus_t * router_pick_trusteddirserver(dirinfo_type_t type, int flags)
Definition: node_select.c:1040
Header file for node_select.c.
#define PDS_NO_EXISTING_SERVERDESC_FETCH
Definition: node_select.h:67
#define PDS_NO_EXISTING_MICRODESC_FETCH
Definition: node_select.h:73
#define PDS_IGNORE_FASCISTFIREWALL
Definition: node_select.h:60
Node information structure.
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:226
int count_loading_descriptors_progress(void)
Definition: nodelist.c:2770
void router_set_status(const char *digest, int up)
Definition: nodelist.c:2371
Header file for nodelist.c.
Master header file for Tor-specific functionality.
@ SAVED_NOWHERE
Definition: or.h:630
#define ISO_STREAM
Definition: or.h:868
#define ISO_SESSIONGRP
Definition: or.h:864
#define SESSION_GROUP_DIRCONN
Definition: or.h:877
download_want_authority_t
Definition: or.h:659
#define TO_CONN(c)
Definition: or.h:616
#define MAX_HEADERS_SIZE
Definition: or.h:122
dirinfo_type_t
Definition: or.h:785
@ V3_DIRINFO
Definition: or.h:788
@ BRIDGE_DIRINFO
Definition: or.h:790
@ EXTRAINFO_DIRINFO
Definition: or.h:792
@ MICRODESC_DIRINFO
Definition: or.h:794
#define ENTRY_TO_CONN(c)
Definition: or.h:619
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
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
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
int reachable_addr_allows_dir_server(const dir_server_t *ds, firewall_connection_t fw_connection, int pref_only)
Definition: policies.c:728
Header file for policies.c.
void rep_hist_note_used_port(time_t now, uint16_t port)
void rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
Header file for predict_ports.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
void relay_address_new_suggestion(const tor_addr_t *suggested_addr, const tor_addr_t *peer_addr, const char *identity_digest)
Header file for relay_find_addr.c.
Header file for rendcommon.c.
const char * router_get_descriptor_gen_reason(void)
Definition: router.c:1866
int router_digest_is_me(const char *digest)
Definition: router.c:1739
const char * router_purpose_to_string(uint8_t p)
Definition: routerinfo.c:98
Header file for routerinfo.c.
Router descriptor structure.
#define ROUTER_PURPOSE_GENERAL
Definition: routerinfo_st.h:98
#define ROUTER_PURPOSE_BRIDGE
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
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
signed_descriptor_t * router_get_by_extrainfo_digest(const char *digest)
Definition: routerlist.c:797
Header file for routerlist.c.
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
Header file for routermode.c.
int routerset_contains_routerstatus(const routerset_t *set, const routerstatus_t *rs, country_t country)
Definition: routerset.c:339
Header file for routerset.c.
Header file for selftest.c.
void sr_act_post_consensus(const networkstatus_t *consensus)
This file contains ABI/API of the shared random protocol defined in proposal #250....
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
size_t dir_len
Definition: cached_dir_st.h:20
uint8_t digest_sha3_as_signed[DIGEST256_LEN]
Definition: cached_dir_st.h:25
time_t published
Definition: cached_dir_st.h:22
uint8_t state
Definition: connection_st.h:49
unsigned int type
Definition: connection_st.h:50
uint16_t port
unsigned int purpose
Definition: connection_st.h:51
time_t timestamp_last_write_allowed
tor_addr_t addr
unsigned int dirconn_direct
char identity_digest[DIGEST_LEN]
struct circuit_guard_state_t * guard_state
routerstatus_t fake_status
Definition: dir_server_st.h:57
unsigned int has_accepted_serverdesc
Definition: dir_server_st.h:45
ed25519_public_key_t identity_pk
Definition: hs_ident.h:90
Definition: node_st.h:34
tor_addr_t HTTPProxyAddr
dirinfo_type_t PublishServerDescriptor_
int FetchServerDescriptors
char * HTTPProxy
int FetchDirInfoExtraEarly
char * HTTPProxyAuthenticator
char * Socks5Proxy
char * Socks4Proxy
int FetchUselessDescriptors
struct routerset_t * ExcludeNodes
uint16_t HTTPProxyPort
unsigned int caches_extra_info
Definition: routerinfo_st.h:70
time_t last_dir_503_at
char identity_digest[DIGEST_LEN]
download_status_t ei_dl_status
size_t size
Definition: mmap.h:27
const char * data
Definition: mmap.h:26
#define STATIC
Definition: testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
void format_rfc1123_time(char *buf, time_t t)
Definition: time_fmt.c:182
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
#define tor_assert_nonfatal_unreached()
Definition: util_bug.h:176
#define tor_assert(expr)
Definition: util_bug.h:102
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:215
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
#define ED25519_BASE64_LEN
Definition: x25519_sizes.h:43