tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

dirclient_modes.c (2925B)


      1 /* Copyright (c) 2001 Matej Pfajfar.
      2 * Copyright (c) 2001-2004, Roger Dingledine.
      3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
      4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
      5 /* See LICENSE for licensing information */
      6 
      7 /**
      8 * @file dirclient_modes.c
      9 * @brief Functions to answer questions about how we'd like to behave
     10 *     as a directory client
     11 **/
     12 
     13 #include "orconfig.h"
     14 
     15 #include "core/or/or.h"
     16 
     17 #include "feature/dirclient/dirclient_modes.h"
     18 #include "feature/dircache/dirserv.h"
     19 #include "feature/relay/relay_find_addr.h"
     20 #include "feature/relay/router.h"
     21 #include "feature/relay/routermode.h"
     22 #include "feature/stats/predict_ports.h"
     23 
     24 #include "app/config/or_options_st.h"
     25 #include "feature/nodelist/routerinfo_st.h"
     26 
     27 /* Should this tor instance only use begindir for all its directory requests?
     28 */
     29 int
     30 dirclient_must_use_begindir(const or_options_t *options)
     31 {
     32  /* Clients, onion services, and bridges must use begindir,
     33   * relays and authorities do not have to */
     34  return !public_server_mode(options);
     35 }
     36 
     37 /** Return 1 if we fetch our directory material directly from the
     38 * authorities, rather than from a mirror. */
     39 int
     40 dirclient_fetches_from_authorities(const or_options_t *options)
     41 {
     42  const routerinfo_t *me;
     43  int refuseunknown;
     44  if (options->FetchDirInfoEarly)
     45    return 1;
     46  if (options->BridgeRelay == 1)
     47    return 0;
     48  refuseunknown = ! router_my_exit_policy_is_reject_star() &&
     49    should_refuse_unknown_exits(options);
     50  if (!dir_server_mode(options) && !refuseunknown)
     51    return 0;
     52  if (!server_mode(options) || !advertised_server_mode())
     53    return 0;
     54  me = router_get_my_routerinfo();
     55  if (!me || (!me->supports_tunnelled_dir_requests && !refuseunknown))
     56    return 0; /* if we don't service directory requests, return 0 too */
     57  return 1;
     58 }
     59 
     60 /** Return 1 if we should fetch new networkstatuses, descriptors, etc
     61 * on the "mirror" schedule rather than the "client" schedule.
     62 */
     63 int
     64 dirclient_fetches_dir_info_early(const or_options_t *options)
     65 {
     66  return dirclient_fetches_from_authorities(options);
     67 }
     68 
     69 /** Return 1 if we should fetch new networkstatuses, descriptors, etc
     70 * on a very passive schedule -- waiting long enough for ordinary clients
     71 * to probably have the info we want. These would include bridge users,
     72 * and maybe others in the future e.g. if a Tor client uses another Tor
     73 * client as a directory guard.
     74 */
     75 int
     76 dirclient_fetches_dir_info_later(const or_options_t *options)
     77 {
     78  return options->UseBridges != 0;
     79 }
     80 
     81 /** Return 1 if we have no need to fetch new descriptors. This generally
     82 * happens when we're not a dir cache and we haven't built any circuits
     83 * lately.
     84 */
     85 int
     86 dirclient_too_idle_to_fetch_descriptors(const or_options_t *options,
     87                                        time_t now)
     88 {
     89  return !directory_caches_dir_info(options) &&
     90         !options->FetchUselessDescriptors &&
     91         rep_hist_circbuilding_dormant(now);
     92 }