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