Back to index

tor  0.2.3.18-rc
Classes | Defines | Typedefs | Functions | Variables
routerlist.c File Reference

Code to maintain and access the global list of routerinfos for known servers. More...

#include "or.h"
#include "circuitbuild.h"
#include "config.h"
#include "connection.h"
#include "control.h"
#include "directory.h"
#include "dirserv.h"
#include "dirvote.h"
#include "geoip.h"
#include "hibernate.h"
#include "main.h"
#include "microdesc.h"
#include "networkstatus.h"
#include "nodelist.h"
#include "policies.h"
#include "reasons.h"
#include "rendcommon.h"
#include "rendservice.h"
#include "rephist.h"
#include "router.h"
#include "routerlist.h"
#include "routerparse.h"

Go to the source code of this file.

Classes

struct  cert_list_t
 List of for a given authority, and download status for latest certificate. More...
struct  duration_idx_t
 Internal type used to represent how long an old descriptor was valid, where it appeared in the list of old descriptors, and whether it's extra old. More...
struct  routerset_t
 A routerset specifies constraints on a set of possible routerinfos, based on their names, identities, or addresses. More...

Defines

#define SDMAP_FOREACH(map, keyvar, valvar)
#define RIMAP_FOREACH(map, keyvar, valvar)   DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
#define EIMAP_FOREACH(map, keyvar, valvar)   DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
#define get_n_v2_authorities()   get_n_authorities(V2_DIRINFO)
#define DEAD_CERT_LIFETIME   (2*24*60*60)
#define OLD_CERT_LIFETIME   (7*24*60*60)
#define CERT_EXPIRY_SKEW   (60*60)
#define N_AUTH_CERT_DL_FAILURES_TO_BUG_USER   2
#define MAX_CERT_DL_FAILURES   8
 How many times will we try to fetch a certificate before giving up?
#define RRS_FORCE   1
#define RRS_DONT_REMOVE_OLD   2
#define DIR_503_TIMEOUT   (60*60)
 How long do we avoid using a directory server after it's given us a 503?
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH   10000000 /* 10 MB/sec */
 Do not weight any declared bandwidth more than this much when picking routers by bandwidth.
#define BRIDGE_MIN_BELIEVABLE_BANDWIDTH   20000 /* 20 kB/sec */
 When weighting bridges, enforce these values as lower and upper bound for believable bandwidth, because there is no way for us to verify a bridge's bandwidth currently.
#define BRIDGE_MAX_BELIEVABLE_BANDWIDTH   100000 /* 100 kB/sec */
#define ZERO_BANDWIDTH_WARNING_INTERVAL   (15)
#define should_cache_old_descriptors()   directory_caches_dir_info(get_options())
#define MAX_DL_PER_REQUEST   96
 Max amount of hashes to download per request.
#define MAX_MICRODESC_DL_PER_REQUEST   92
#define MIN_DL_PER_REQUEST   4
 Don't split our requests so finely that we are requesting fewer than this number per server.
#define MIN_REQUESTS   3
 To prevent a single screwy cache from confusing us by selective reply, try to split our requests into at least this many requests.
#define MAX_DL_TO_DELAY   16
 If we want fewer than this many descriptors, wait until we want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has passed.
#define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST   (10*60)
 When directory clients have only a few servers to request, they batch them until they have more, or until this amount of time has passed.
#define DUMMY_DOWNLOAD_INTERVAL   (20*60)
 How often should we launch a server/authority request to be sure of getting a guess for our IP?
#define FRAC_USABLE_NEEDED   .75
#define FRAC_EXIT_USABLE_NEEDED   .5
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE   (2*60*60)
 Any changes in a router descriptor's publication time larger than this are automatically non-cosmetic.
#define ROUTER_ALLOW_UPTIME_DRIFT   (6*60*60)
 We allow uptime to vary from how much it ought to be by this much.

Typedefs

typedef struct cert_list_t cert_list_t
 List of for a given authority, and download status for latest certificate.

Functions

static const routerstatus_trouter_pick_directory_server_impl (dirinfo_type_t type, int flags)
 Pick a random running valid directory server/mirror from our routerlist.
static const routerstatus_trouter_pick_trusteddirserver_impl (dirinfo_type_t type, int flags, int *n_busy_out)
 Choose randomly from among the trusted dirservers that are up.
static void mark_all_trusteddirservers_up (void)
 Go through and mark the authoritative dirservers as up.
static int router_nickname_matches (const routerinfo_t *router, const char *nickname)
 Return true if router's nickname matches nickname (case-insensitive), or if router's identity key digest matches a hexadecimal value stored in nickname.
static int node_nickname_matches (const node_t *node, const char *nickname)
 Return true if node's nickname matches nickname (case-insensitive), or if node's identity key digest matches a hexadecimal value stored in nickname.
static void trusted_dir_server_free (trusted_dir_server_t *ds)
 Free storage held in ds.
static int signed_desc_digest_is_recognized (signed_descriptor_t *desc)
 Return true iff any networkstatus includes a descriptor whose digest is that of desc.
static void update_router_have_minimum_dir_info (void)
 Change the value of have_min_dir_info, setting it true iff we have enough network and router information to build circuits.
static const char * signed_descriptor_get_body_impl (const signed_descriptor_t *desc, int with_annotations)
 Return a pointer to the signed textual representation of a descriptor.
static void list_pending_downloads (digestmap_t *result, int purpose, const char *prefix)
 For every current directory connection whose purpose is purpose, and where the resource being downloaded begins with prefix, split rest of the resource into base16 fingerprints (or base64 fingerprints if purpose==DIR_PURPPOSE_FETCH_MICRODESC), decode them, and set the corresponding elements of result to a nonzero value.
static void launch_dummy_descriptor_download_as_needed (time_t now, const or_options_t *options)
 As needed, launch a dummy router descriptor fetch to see if our address has changed.
int get_n_authorities (dirinfo_type_t type)
 Return the number of directory authorities whose type matches some bit set in type
static cert_list_tget_cert_list (const char *id_digest)
 Helper: Return the cert_list_t for an authority whose authority ID is id_digest, allocating a new list if necessary.
int trusted_dirs_reload_certs (void)
 Reload the cached v3 key certificates from the cached-certs file in the data directory.
static INLINE int already_have_cert (authority_cert_t *cert)
 Helper: return true iff we already have loaded the exact cert cert.
int trusted_dirs_load_certs_from_string (const char *contents, int from_store, int flush)
 Load a bunch of new key certificates from the string contents.
void trusted_dirs_flush_certs_to_disk (void)
 Save all v3 key certificates to the cached-certs file.
static void trusted_dirs_remove_old_certs (void)
 Remove all v3 authority certificates that have been superseded for more than 48 hours.
authority_cert_tauthority_cert_get_newest_by_id (const char *id_digest)
 Return the newest v3 authority certificate whose v3 authority identity key has digest id_digest.
authority_cert_tauthority_cert_get_by_sk_digest (const char *sk_digest)
 Return the newest v3 authority certificate whose directory signing key has digest sk_digest.
authority_cert_tauthority_cert_get_by_digests (const char *id_digest, const char *sk_digest)
 Return the v3 authority certificate with signing key matching sk_digest, for the authority with identity digest id_digest.
void authority_cert_get_all (smartlist_t *certs_out)
 Add every known authority_cert_t to certs_out.
void authority_cert_dl_failed (const char *id_digest, int status)
 Called when an attempt to download a certificate with the authority with ID id_digest fails with HTTP response code status: remember the failure, so we don't try again immediately.
int authority_cert_dl_looks_uncertain (const char *id_digest)
 Return true iff when we've been getting enough failures when trying to download the certificate with ID digest id_digest that we're willing to start bugging the user about it.
void authority_certs_fetch_missing (networkstatus_t *status, time_t now)
 Try to download any v3 authority certificates that we may be missing.
static int router_should_rebuild_store (desc_store_t *store)
 Helper: return 1 iff the router log is so big we want to rebuild the store.
static INLINE desc_store_tdesc_get_store (routerlist_t *rl, const signed_descriptor_t *sd)
 Return the desc_store_t in rl that should be used to store sd.
static int signed_desc_append_to_journal (signed_descriptor_t *desc, desc_store_t *store)
 Add the signed_descriptor_t in desc to the router journal; change its saved_location to SAVED_IN_JOURNAL and set its offset appropriately.
static int _compare_signed_descriptors_by_age (const void **_a, const void **_b)
 Sorting helper: return <0, 0, or >0 depending on whether the signed_descriptor_t* in *a is older, the same age as, or newer than the signed_descriptor_t* in *b.
static int router_rebuild_store (int flags, desc_store_t *store)
 If the journal of store is too long, or if RRS_FORCE is set in flags, then atomically replace the saved router store with the routers currently in our routerlist, and clear the journal.
static int router_reload_router_list_impl (desc_store_t *store)
 Helper: Reload a cache file and its associated journal, setting metadata appropriately.
int router_reload_router_list (void)
 Load all cached router descriptors and extra-info documents from the store.
smartlist_trouter_get_trusted_dir_servers (void)
 Return a smartlist containing a list of trusted_dir_server_t * for all known trusted dirservers.
const routerstatus_trouter_pick_directory_server (dirinfo_type_t type, int flags)
 Try to find a running dirserver that supports operations of type.
int router_get_my_share_of_directory_requests (double *v2_share_out, double *v3_share_out)
 Try to determine which fraction of v2 and v3 directory requests aimed at caches will be sent to us.
trusted_dir_server_trouter_get_trusteddirserver_by_digest (const char *digest)
 Return the trusted_dir_server_t for the directory authority whose identity key hashes to digest, or NULL if no such authority is known.
trusted_dir_server_ttrusteddirserver_get_by_v3_auth_digest (const char *digest)
 Return the trusted_dir_server_t for the directory authority whose v3 identity key hashes to digest, or NULL if no such authority is known.
const routerstatus_trouter_pick_trusteddirserver (dirinfo_type_t type, int flags)
 Try to find a running trusted dirserver.
int routers_have_same_or_addr (const routerinfo_t *r1, const routerinfo_t *r2)
 Return true iff r1 and r2 have the same address and OR port.
void router_reset_status_download_failures (void)
 Reset all internal variables used to count failed downloads of network status objects.
static INLINE int addrs_in_same_network_family (const tor_addr_t *a1, const tor_addr_t *a2)
 Return true iff router1 and router2 have similar enough network addresses that we should treat them as being in the same family.
void nodelist_add_node_and_family (smartlist_t *sl, const node_t *node)
 Add all the family of node, including node itself, to the smartlist sl.
static void routerlist_add_node_and_family (smartlist_t *sl, const routerinfo_t *router)
 Given a router, add every node_t in its family (including the node itself!) to sl.
static INLINE int node_in_nickname_smartlist (const smartlist_t *lst, const node_t *node)
 Return true iff node is named by some nickname in lst.
int nodes_in_same_family (const node_t *node1, const node_t *node2)
 Return true iff r1 and r2 are in the same family, but not the same router.
int router_nickname_is_in_list (const routerinfo_t *router, const char *list)
 Return 1 iff any member of the (possibly NULL) comma-separated list list is an acceptable nickname or hexdigest for router.
static void router_add_running_nodes_to_smartlist (smartlist_t *sl, int allow_invalid, int need_uptime, int need_capacity, int need_guard, int need_desc)
 Add every suitable node from our nodelist to sl, so that we can pick a node for a circuit.
const routerinfo_trouterlist_find_my_routerinfo (void)
 Look through the routerlist until we find a router that has my key.
const node_trouter_find_exact_exit_enclave (const char *address, uint16_t port)
 Find a router that's up, that has this IP address, and that allows exit to this address:port, or return NULL if there isn't a good one.
int node_is_unreliable (const node_t *node, int need_uptime, int need_capacity, int need_guard)
 Return 1 if router is not suitable for these parameters, else 0.
uint32_t router_get_advertised_bandwidth (const routerinfo_t *router)
 Return the smaller of the router's configured BandwidthRate and its advertised capacity.
uint32_t router_get_advertised_bandwidth_capped (const routerinfo_t *router)
 Return the smaller of the router's configured BandwidthRate and its advertised capacity, capped by max-believe-bw.
static uint32_t bridge_get_advertised_bandwidth_bounded (routerinfo_t *router)
 Return the smaller of the router's configured BandwidthRate and its advertised capacity, making sure to stay within the interval between bridge-min-believe-bw and bridge-max-believe-bw.
static INLINE int32_t kb_to_bytes (uint32_t bw)
 Return bw*1000, unless bw*1000 would overflow, in which case return INT32_MAX.
static const node_tsmartlist_choose_node_by_bandwidth_weights (smartlist_t *sl, bandwidth_weight_rule_t rule)
 Helper function: choose a random element of smartlist sl of nodes, weighted by the advertised bandwidth of each element using the consensus bandwidth weights.
static const node_tsmartlist_choose_node_by_bandwidth (smartlist_t *sl, bandwidth_weight_rule_t rule)
 Helper function: choose a random node_t element of smartlist sl, weighted by the advertised bandwidth of each element.
const node_tnode_sl_choose_by_bandwidth (smartlist_t *sl, bandwidth_weight_rule_t rule)
 Choose a random element of status list sl, weighted by the advertised bandwidth of each node.
const node_trouter_choose_random_node (smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
 Return a random running node from the nodelist.
int hex_digest_nickname_decode (const char *hexdigest, char *digest_out, char *nickname_qualifier_char_out, char *nickname_out)
 Helper: given an extended nickname in hexdigest try to decode it.
static int hex_digest_nickname_matches (const char *hexdigest, const char *identity_digest, const char *nickname, int is_named)
 Helper: Return true iff the identity_digest and nickname combination of a router, encoded in hexadecimal, matches hexdigest (which is optionally prefixed with a single dollar sign).
int router_is_named (const routerinfo_t *router)
 Return true iff router is listed as named in the current consensus.
static INLINE int router_hex_digest_matches (const routerinfo_t *router, const char *hexdigest)
 Return true iff the digest of router's identity key, encoded in hexadecimal, matches hexdigest (which is optionally prefixed with a single dollar sign).
const routerinfo_trouter_get_by_nickname (const char *nickname, int warn_if_unnamed)
 Return the router in our routerlist whose (case-insensitive) nickname or (case-sensitive) hexadecimal key digest is nickname.
int router_digest_is_trusted_dir_type (const char *digest, dirinfo_type_t type)
 Return true iff digest is the digest of the identity key of a trusted directory matching at least one bit of type.
int router_addr_is_trusted_dir (uint32_t addr)
 Return true iff addr is the address of one of our trusted directory authorities.
int hexdigest_to_digest (const char *hexdigest, char *digest)
 If hexdigest is correctly formed, base16_decode it into digest, which must have DIGEST_LEN space in it.
const routerinfo_trouter_get_by_hexdigest (const char *hexdigest)
 Return the router in our routerlist whose hexadecimal key digest is hexdigest.
routerinfo_trouter_get_mutable_by_digest (const char *digest)
 As router_get_by_id_digest,but return a pointer that you're allowed to modify.
const routerinfo_trouter_get_by_id_digest (const char *digest)
 Return the router in our routerlist whose 20-byte key digest is digest.
signed_descriptor_trouter_get_by_descriptor_digest (const char *digest)
 Return the router in our routerlist whose 20-byte descriptor is digest.
signed_descriptor_trouter_get_by_extrainfo_digest (const char *digest)
 Return the signed descriptor for the router in our routerlist whose 20-byte extra-info digest is digest.
signed_descriptor_textrainfo_get_by_descriptor_digest (const char *digest)
 Return the signed descriptor for the extrainfo_t in our routerlist whose extra-info-digest is digest.
const char * signed_descriptor_get_body (const signed_descriptor_t *desc)
 Return a pointer to the signed textual representation of a descriptor.
const char * signed_descriptor_get_annotations (const signed_descriptor_t *desc)
 As signed_descriptor_get_body(), but points to the beginning of the annotations section rather than the beginning of the descriptor.
routerlist_trouter_get_routerlist (void)
 Return the current list of all known routers.
void routerinfo_free (routerinfo_t *router)
 Free all storage held by router.
void extrainfo_free (extrainfo_t *extrainfo)
 Release all storage held by extrainfo
static void signed_descriptor_free (signed_descriptor_t *sd)
 Release storage held by sd.
static signed_descriptor_tsigned_descriptor_from_routerinfo (routerinfo_t *ri)
 Extract a signed_descriptor_t from a general routerinfo, and free the routerinfo.
static void _extrainfo_free (void *e)
 Helper: free the storage held by the extrainfo_t in e.
void routerlist_free (routerlist_t *rl)
 Free all storage held by a routerlist rl.
void dump_routerlist_mem_usage (int severity)
 Log information about how much memory is being used for routerlist, at log level severity.
static INLINE int _routerlist_find_elt (smartlist_t *sl, void *ri, int idx)
 Debugging helper: If idx is nonnegative, assert that ri is in sl at position idx.
static void routerlist_insert (routerlist_t *rl, routerinfo_t *ri)
 Insert an item ri into the routerlist rl, updating indices as needed.
static int extrainfo_insert (routerlist_t *rl, extrainfo_t *ei)
 Adds the extrainfo_t ei to the routerlist rl, if there is a corresponding router in rl->routers or rl->old_routers.
static void routerlist_insert_old (routerlist_t *rl, routerinfo_t *ri)
 If we're a directory cache and routerlist rl doesn't have a copy of router ri yet, add it to the list of old (not recommended but still served) descriptors.
void routerlist_remove (routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
 Remove an item ri from the routerlist rl, updating indices as needed.
static void routerlist_remove_old (routerlist_t *rl, signed_descriptor_t *sd, int idx)
 Remove a signed_descriptor_t sd from rl->old_routers, and adjust rl as appropriate.
static void routerlist_replace (routerlist_t *rl, routerinfo_t *ri_old, routerinfo_t *ri_new)
 Remove ri_old from the routerlist rl, and replace it with ri_new, updating all index info.
static routerinfo_trouterlist_reparse_old (routerlist_t *rl, signed_descriptor_t *sd)
 Extract the descriptor sd from old_routerlist, and re-parse it as a fresh routerinfo_t.
void routerlist_free_all (void)
 Free all memory held by the routerlist module.
void routerlist_reset_warnings (void)
 Forget that we have issued any router-related warnings, so that we'll warn again if we see the same errors.
void router_set_status (const char *digest, int up)
 Mark the router with ID digest as running or non-running in our routerlist.
was_router_added_t router_add_to_routerlist (routerinfo_t *router, const char **msg, int from_cache, int from_fetch)
 Add router to the routerlist, if we don't already have it.
was_router_added_t router_add_extrainfo_to_routerlist (extrainfo_t *ei, const char **msg, int from_cache, int from_fetch)
 Insert ei into the routerlist, or free it.
static int _compare_old_routers_by_identity (const void **_a, const void **_b)
 Sorting helper: return <0, 0, or >0 depending on whether the signed_descriptor_t* in *a has an identity digest preceding, equal to, or later than that of *b.
static int _compare_duration_idx (const void *_d1, const void *_d2)
 Sorting helper: compare two duration_idx_t by their duration.
static void routerlist_remove_old_cached_routers_with_id (time_t now, time_t cutoff, int lo, int hi, digestset_t *retain)
 The range lo through hi inclusive of routerlist->old_routers must contain routerinfo_t with the same identity and with publication time in ascending order.
void routerlist_remove_old_routers (void)
 Deactivate any routers from the routerlist that are more than ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses; remove old routers from the list of cached routers if we have too many.
void routerlist_descriptors_added (smartlist_t *sl, int from_cache)
 We just added a new set of descriptors.
int router_load_single_router (const char *s, uint8_t purpose, int cache, const char **msg)
 Code to parse a single router descriptor and insert it into the routerlist.
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)
 Given a string s containing some routerdescs, parse it and put the routers into our directory.
void router_load_extrainfo_from_string (const char *s, const char *eos, saved_location_t saved_location, smartlist_t *requested_fingerprints, int descriptor_digests)
 Parse one or more extrainfos from s (ending immediately before eos if eos is present).
void update_all_descriptor_downloads (time_t now)
 Update downloads for router descriptors and/or microdescriptors as appropriate.
void routerlist_retry_directory_downloads (time_t now)
 Clear all our timeouts for fetching v2 and v3 directory stuff, and then give it all a try again.
int router_exit_policy_all_nodes_reject (const tor_addr_t *addr, uint16_t port, int need_uptime)
 Return 1 if all running sufficiently-stable routers we can use will reject addr:port, return 0 if any might accept it.
int router_exit_policy_rejects_all (const routerinfo_t *router)
 Return true iff router does not permit exit streams.
trusted_dir_server_tadd_trusted_dir_server (const char *nickname, const char *address, uint16_t dir_port, uint16_t or_port, const char *digest, const char *v3_auth_digest, dirinfo_type_t type)
 Add to the list of authoritative directory servers one at address:port, with identity key digest.
void authority_cert_free (authority_cert_t *cert)
 Free storage held in cert.
void clear_trusted_dir_servers (void)
 Remove all members from the list of trusted dir servers.
int any_trusted_dir_is_v1_authority (void)
 Return 1 if any trusted dir server supports v1 directories, else return 0.
static void list_pending_descriptor_downloads (digestmap_t *result, int extrainfo)
 For every router descriptor (or extra-info document if extrainfo is true) we are currently downloading by descriptor digest, set result[d] to (void*)1.
void list_pending_microdesc_downloads (digestmap_t *result)
 For every microdescriptor we are currently downloading by descriptor digest, set result[d] to (void*)1.
static void initiate_descriptor_downloads (const routerstatus_t *source, int purpose, smartlist_t *digests, int lo, int hi, int pds_flags)
 Launch downloads for all the descriptors whose digests or digests256 are listed as digests[i] for lo <= i < hi.
static INLINE int client_would_use_router (const routerstatus_t *rs, time_t now, const or_options_t *options)
 Return 0 if this routerstatus is obsolete, too new, isn't running, or otherwise not a descriptor that we would make any use of even if we had it.
void launch_descriptor_downloads (int purpose, smartlist_t *downloadable, const routerstatus_t *source, time_t now)
 Given a purpose (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of router descriptor digests or microdescriptor digest256s in downloadable, decide whether to delay fetching until we have more.
static void update_router_descriptor_cache_downloads_v2 (time_t now)
 Launch downloads for router status as needed, using the strategy used by authorities and caches: based on the v2 networkstatuses we have, download every descriptor we don't have but would serve, from a random authority that lists it.
void update_consensus_router_descriptor_downloads (time_t now, int is_vote, networkstatus_t *consensus)
 For any descriptor that we want that's currently listed in consensus, download it as appropriate.
void update_router_descriptor_downloads (time_t now)
 Launch downloads for router status as needed.
void update_extrainfo_downloads (time_t now)
 Launch extrainfo downloads as needed.
int router_have_minimum_dir_info (void)
 Return true iff we have enough networkstatus and router information to start building circuits.
void router_dir_info_changed (void)
 Called when our internal view of the directory has changed.
const char * get_dir_info_status_string (void)
 Return a string describing what we're missing before we have enough directory info.
static void count_usable_descriptors (int *num_present, int *num_usable, const networkstatus_t *consensus, const or_options_t *options, time_t now, routerset_t *in_set, int exit_only)
 Iterate over the servers listed in consensus, and count how many of them seem like ones we'd use, and how many of those we have descriptors for.
int count_loading_descriptors_progress (void)
 We just fetched a new set of descriptors.
void router_reset_descriptor_download_failures (void)
 Reset the descriptor download failure count on all routers, so that we can retry any long-failed routers immediately.
int router_differences_are_cosmetic (const routerinfo_t *r1, const routerinfo_t *r2)
 Return true iff the only differences between r1 and r2 are such that would not cause a recent (post 0.1.1.6) dirserver to republish.
int routerinfo_incompatible_with_extrainfo (const routerinfo_t *ri, extrainfo_t *ei, signed_descriptor_t *sd, const char **msg)
 Check whether ri (a.k.a.
void routerlist_assert_ok (const routerlist_t *rl)
 Assert that the internal representation of rl is self-consistent.
const char * esc_router_info (const routerinfo_t *router)
 Allocate and return a new string representing the contact info and platform string for router, surrounded by quotes and using standard C escapes.
static int _compare_routerinfo_by_id_digest (const void **a, const void **b)
 Helper for sorting: compare two routerinfos by their identity digest.
void routers_sort_by_identity (smartlist_t *routers)
 Sort a list of routerinfo_t in ascending order of identity digest.
routerset_trouterset_new (void)
 Return a new empty routerset.
static char * routerset_get_countryname (const char *c)
 If c is a country code in the form {cc}, return a newly allocated string holding the "cc" part.
void routerset_refresh_countries (routerset_t *target)
 Update the routerset's countries bitarray_t.
int routerset_parse (routerset_t *target, const char *s, const char *description)
 Parse the string s to create a set of routerset entries, and add them to target.
void refresh_all_country_info (void)
 Called when we change a node set, or when we reload the geoip list: recompute all country info in all configuration node sets and in the routerlist.
void routerset_union (routerset_t *target, const routerset_t *source)
 Add all members of the set source to target.
int routerset_is_list (const routerset_t *set)
 Return true iff set lists only nicknames and digests, and includes no IP ranges or countries.
int routerset_needs_geoip (const routerset_t *set)
 Return true iff we need a GeoIP IP-to-country database to make sense of set.
int routerset_is_empty (const routerset_t *set)
 Return true iff there are no entries in set.
static int routerset_contains (const routerset_t *set, const tor_addr_t *addr, uint16_t orport, const char *nickname, const char *id_digest, country_t country)
 Helper.
int routerset_contains_extendinfo (const routerset_t *set, const extend_info_t *ei)
 Return true iff we can tell that ei is a member of set.
int routerset_contains_router (const routerset_t *set, const routerinfo_t *ri, country_t country)
 Return true iff ri is in set.
int routerset_contains_routerstatus (const routerset_t *set, const routerstatus_t *rs, country_t country)
 Return true iff rs is in set.
int routerset_contains_node (const routerset_t *set, const node_t *node)
 Return true iff node is in set.
void routerset_get_all_nodes (smartlist_t *out, const routerset_t *routerset, const routerset_t *excludeset, int running_only)
 Add every known node_t that is a member of routerset to out, but never add any that are part of excludeset.
void routerset_subtract_nodes (smartlist_t *lst, const routerset_t *routerset)
 Remove every node_t from lst that is in routerset.
char * routerset_to_string (const routerset_t *set)
 Return a new string that when parsed by routerset_parse_string() will yield set.
int routerset_equal (const routerset_t *old, const routerset_t *new)
 Helper: return true iff old and new are both NULL, or both non-NULL equal routersets.
void routerset_free (routerset_t *routerset)
 Free all storage held in routerset.
void node_set_country (node_t *node)
 Refresh the country code of ri.
void nodelist_refresh_countries (void)
 Set the country code of all routers in the routerlist.
int hid_serv_get_responsible_directories (smartlist_t *responsible_dirs, const char *id)
 Determine the routers that are responsible for id (binary) and add pointers to those routers' routerstatus_t to responsible_dirs.
int hid_serv_acting_as_directory (void)
 Return true if this node is currently acting as hidden service directory, false otherwise.
int hid_serv_responsible_for_desc_id (const char *query)
 Return true if this node is responsible for storing the descriptor ID in query and false otherwise.

Variables

static smartlist_ttrusted_dir_servers = NULL
 Global list of a trusted_dir_server_t object for each trusted directory server.
static digestmap_t * trusted_dir_certs = NULL
 Map from v3 identity key digest to cert_list_t.
static int trusted_dir_servers_certs_changed = 0
 True iff any key certificate in at least one member of trusted_dir_certs has changed since we last flushed the certificates to disk.
static routerlist_trouterlist = NULL
 Global list of all of the routers that we know about.
static smartlist_twarned_nicknames = NULL
 List of strings for nicknames we've already warned about and that are still unknown / unavailable.
static time_t last_descriptor_download_attempted = 0
 The last time we tried to download any routerdesc, or 0 for "never".
static int have_min_dir_info = 0
 True iff, the last time we checked whether we had enough directory info to build circuits, the answer was "yes".
static int need_to_update_have_min_dir_info = 1
 True iff enough has changed since the last time we checked whether we had enough directory info to build circuits that our old answer can no longer be trusted.
static char dir_info_status [128] = ""
 String describing what we're missing before we have enough directory info.
static uint64_t sl_last_total_weighted_bw = 0
 When we last computed the weights to use for bandwidths on directory requests, what were the total weighted bandwidth, and our share of that bandwidth? Used to determine what fraction of directory requests we should expect to see.
static uint64_t sl_last_weighted_bw_of_me = 0

Detailed Description

Code to maintain and access the global list of routerinfos for known servers.

Definition in file routerlist.c.


Class Documentation

struct cert_list_t

List of for a given authority, and download status for latest certificate.

Definition at line 81 of file routerlist.c.

Collaboration diagram for cert_list_t:
Class Members
smartlist_t * certs
download_status_t dl_status
struct duration_idx_t

Internal type used to represent how long an old descriptor was valid, where it appeared in the list of old descriptors, and whether it's extra old.

Used only by routerlist_remove_old_cached_routers_with_id().

Definition at line 3525 of file routerlist.c.

Class Members
int duration
int idx
int old
struct routerset_t

A routerset specifies constraints on a set of possible routerinfos, based on their names, identities, or addresses.

It is optimized for determining whether a router is a member or not, in O(1+P) time, where P is the number of address policy constraints.

Definition at line 5422 of file routerlist.c.

Collaboration diagram for routerset_t:
Class Members
bitarray_t * countries Bit array mapping the return value of geoip_get_country() to 1 iff the country is a member of this routerset. Note that we MUST call routerset_refresh_countries() whenever the geoip country list is reloaded.
smartlist_t * country_names A list of the country codes in this set.
char * description A human-readable description of what this routerset is for. Used in log messages.
digestmap_t * digests A map from identity digests routers in the set to (void*)1.
smartlist_t * list A list of strings for the elements of the policy. Each string is either a nickname, a hexadecimal identity fingerprint, or an address policy. A router belongs to the set if its nickname OR its identity OR its address matches an entry here.
int n_countries Total number of countries we knew about when we built countries.
strmap_t * names A map from lowercase nicknames of routers in the set to (void*)1.
smartlist_t * policies An address policy for routers in the set. For implementation reasons, a router belongs to the set if it is rejected by this policy.

Define Documentation

#define BRIDGE_MAX_BELIEVABLE_BANDWIDTH   100000 /* 100 kB/sec */

Definition at line 1662 of file routerlist.c.

#define BRIDGE_MIN_BELIEVABLE_BANDWIDTH   20000 /* 20 kB/sec */

When weighting bridges, enforce these values as lower and upper bound for believable bandwidth, because there is no way for us to verify a bridge's bandwidth currently.

Definition at line 1661 of file routerlist.c.

#define CERT_EXPIRY_SKEW   (60*60)
#define DEAD_CERT_LIFETIME   (2*24*60*60)
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH   10000000 /* 10 MB/sec */

Do not weight any declared bandwidth more than this much when picking routers by bandwidth.

Definition at line 1643 of file routerlist.c.

#define DIR_503_TIMEOUT   (60*60)

How long do we avoid using a directory server after it's given us a 503?

Definition at line 1080 of file routerlist.c.

#define DUMMY_DOWNLOAD_INTERVAL   (20*60)

How often should we launch a server/authority request to be sure of getting a guess for our IP?

Definition at line 4767 of file routerlist.c.

#define EIMAP_FOREACH (   map,
  keyvar,
  valvar 
)    DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)

Definition at line 70 of file routerlist.c.

#define FRAC_EXIT_USABLE_NEEDED   .5
#define FRAC_USABLE_NEEDED   .75

Definition at line 128 of file routerlist.c.

#define MAX_CERT_DL_FAILURES   8

How many times will we try to fetch a certificate before giving up?

Definition at line 479 of file routerlist.c.

When directory clients have only a few servers to request, they batch them until they have more, or until this amount of time has passed.

Definition at line 4382 of file routerlist.c.

#define MAX_DL_PER_REQUEST   96

Max amount of hashes to download per request.

Since squid does not like URLs >= 4096 bytes we limit it to 96. 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058 4058/41 (40 for the hash and 1 for the + that separates them) => 98 So use 96 because it's a nice number.

Definition at line 4368 of file routerlist.c.

#define MAX_DL_TO_DELAY   16

If we want fewer than this many descriptors, wait until we want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has passed.

Definition at line 4379 of file routerlist.c.

Definition at line 4369 of file routerlist.c.

#define MIN_DL_PER_REQUEST   4

Don't split our requests so finely that we are requesting fewer than this number per server.

Definition at line 4372 of file routerlist.c.

#define MIN_REQUESTS   3

To prevent a single screwy cache from confusing us by selective reply, try to split our requests into at least this many requests.

Definition at line 4375 of file routerlist.c.

#define OLD_CERT_LIFETIME   (7*24*60*60)
#define RIMAP_FOREACH (   map,
  keyvar,
  valvar 
)    DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)

Definition at line 68 of file routerlist.c.

#define ROUTER_ALLOW_UPTIME_DRIFT   (6*60*60)

We allow uptime to vary from how much it ought to be by this much.

Definition at line 5130 of file routerlist.c.

#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE   (2*60*60)

Any changes in a router descriptor's publication time larger than this are automatically non-cosmetic.

Definition at line 5127 of file routerlist.c.

#define RRS_DONT_REMOVE_OLD   2

Definition at line 665 of file routerlist.c.

#define RRS_FORCE   1

Definition at line 664 of file routerlist.c.

#define SDMAP_FOREACH (   map,
  keyvar,
  valvar 
)
Value:
DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
                    valvar)

Definition at line 65 of file routerlist.c.

Definition at line 2937 of file routerlist.c.


Typedef Documentation

typedef struct cert_list_t cert_list_t

List of for a given authority, and download status for latest certificate.


Function Documentation

static int _compare_duration_idx ( const void *  _d1,
const void *  _d2 
) [static]

Sorting helper: compare two duration_idx_t by their duration.

Definition at line 3533 of file routerlist.c.

{
  const struct duration_idx_t *d1 = _d1;
  const struct duration_idx_t *d2 = _d2;
  return d1->duration - d2->duration;
}

Here is the caller graph for this function:

static int _compare_old_routers_by_identity ( const void **  _a,
const void **  _b 
) [static]

Sorting helper: return <0, 0, or >0 depending on whether the signed_descriptor_t* in *a has an identity digest preceding, equal to, or later than that of *b.

Definition at line 3513 of file routerlist.c.

{
  int i;
  const signed_descriptor_t *r1 = *_a, *r2 = *_b;
  if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
    return i;
  return (int)(r1->published_on - r2->published_on);
}

Here is the caller graph for this function:

static int _compare_routerinfo_by_id_digest ( const void **  a,
const void **  b 
) [static]

Helper for sorting: compare two routerinfos by their identity digest.

Definition at line 5403 of file routerlist.c.

{
  routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
  return fast_memcmp(first->cache_info.identity_digest,
                second->cache_info.identity_digest,
                DIGEST_LEN);
}

Here is the caller graph for this function:

static int _compare_signed_descriptors_by_age ( const void **  _a,
const void **  _b 
) [static]

Sorting helper: return <0, 0, or >0 depending on whether the signed_descriptor_t* in *a is older, the same age as, or newer than the signed_descriptor_t* in *b.

Definition at line 658 of file routerlist.c.

{
  const signed_descriptor_t *r1 = *_a, *r2 = *_b;
  return (int)(r1->published_on - r2->published_on);
}

Here is the caller graph for this function:

static void _extrainfo_free ( void *  e) [static]

Helper: free the storage held by the extrainfo_t in e.

Definition at line 2769 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int _routerlist_find_elt ( smartlist_t sl,
void *  ri,
int  idx 
) [static]

Debugging helper: If idx is nonnegative, assert that ri is in sl at position idx.

Otherwise, search sl for ri. Return the index of ri in sl, or -1 if ri is not in sl.

Definition at line 2825 of file routerlist.c.

{
  if (idx < 0) {
    idx = -1;
    SMARTLIST_FOREACH(sl, routerinfo_t *, r,
                      if (r == ri) {
                        idx = r_sl_idx;
                        break;
                      });
  } else {
    tor_assert(idx < smartlist_len(sl));
    tor_assert(smartlist_get(sl, idx) == ri);
  };
  return idx;
}

Here is the caller graph for this function:

trusted_dir_server_t* add_trusted_dir_server ( const char *  nickname,
const char *  address,
uint16_t  dir_port,
uint16_t  or_port,
const char *  digest,
const char *  v3_auth_digest,
dirinfo_type_t  type 
)

Add to the list of authoritative directory servers one at address:port, with identity key digest.

If address is NULL, add ourself. Return the new trusted directory server entry on success or NULL if we couldn't add it.

Definition at line 4093 of file routerlist.c.

{
  trusted_dir_server_t *ent;
  uint32_t a;
  char *hostname = NULL;
  if (!trusted_dir_servers)
    trusted_dir_servers = smartlist_new();

  if (!address) { /* The address is us; we should guess. */
    if (resolve_my_address(LOG_WARN, get_options(), &a, &hostname) < 0) {
      log_warn(LD_CONFIG,
               "Couldn't find a suitable address when adding ourself as a "
               "trusted directory server.");
      return NULL;
    }
  } else {
    if (tor_lookup_hostname(address, &a)) {
      log_warn(LD_CONFIG,
               "Unable to lookup address for directory server at '%s'",
               address);
      return NULL;
    }
    hostname = tor_strdup(address);
  }

  ent = tor_malloc_zero(sizeof(trusted_dir_server_t));
  ent->nickname = nickname ? tor_strdup(nickname) : NULL;
  ent->address = hostname;
  ent->addr = a;
  ent->dir_port = dir_port;
  ent->or_port = or_port;
  ent->is_running = 1;
  ent->type = type;
  memcpy(ent->digest, digest, DIGEST_LEN);
  if (v3_auth_digest && (type & V3_DIRINFO))
    memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);

  if (nickname)
    tor_asprintf(&ent->description, "directory server \"%s\" at %s:%d",
                 nickname, hostname, (int)dir_port);
  else
    tor_asprintf(&ent->description, "directory server at %s:%d",
                 hostname, (int)dir_port);

  ent->fake_status.addr = ent->addr;
  memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
  if (nickname)
    strlcpy(ent->fake_status.nickname, nickname,
            sizeof(ent->fake_status.nickname));
  else
    ent->fake_status.nickname[0] = '\0';
  ent->fake_status.dir_port = ent->dir_port;
  ent->fake_status.or_port = ent->or_port;

  if (ent->or_port)
    ent->fake_status.version_supports_begindir = 1;

  ent->fake_status.version_supports_conditional_consensus = 1;

  smartlist_add(trusted_dir_servers, ent);
  router_dir_info_changed();
  return ent;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int addrs_in_same_network_family ( const tor_addr_t a1,
const tor_addr_t a2 
) [static]

Return true iff router1 and router2 have similar enough network addresses that we should treat them as being in the same family.

Definition at line 1365 of file routerlist.c.

{
  /* XXXX MOVE ? */
  return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int already_have_cert ( authority_cert_t cert) [static]

Helper: return true iff we already have loaded the exact cert cert.

Definition at line 170 of file routerlist.c.

{
  cert_list_t *cl = get_cert_list(cert->cache_info.identity_digest);

  SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
  {
    if (tor_memeq(c->cache_info.signed_descriptor_digest,
                cert->cache_info.signed_descriptor_digest,
                DIGEST_LEN))
      return 1;
  });
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return 1 if any trusted dir server supports v1 directories, else return 0.

Definition at line 4204 of file routerlist.c.

{
  if (trusted_dir_servers)
    return get_n_authorities(V1_DIRINFO) > 0;

  return 0;
}

Here is the call graph for this function:

void authority_cert_dl_failed ( const char *  id_digest,
int  status 
)

Called when an attempt to download a certificate with the authority with ID id_digest fails with HTTP response code status: remember the failure, so we don't try again immediately.

Definition at line 451 of file routerlist.c.

{
  cert_list_t *cl;
  if (!trusted_dir_certs ||
      !(cl = digestmap_get(trusted_dir_certs, id_digest)))
    return;

  download_status_failed(&cl->dl_status, status);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int authority_cert_dl_looks_uncertain ( const char *  id_digest)

Return true iff when we've been getting enough failures when trying to download the certificate with ID digest id_digest that we're willing to start bugging the user about it.

Definition at line 465 of file routerlist.c.

{
#define N_AUTH_CERT_DL_FAILURES_TO_BUG_USER 2
  cert_list_t *cl;
  int n_failures;
  if (!trusted_dir_certs ||
      !(cl = digestmap_get(trusted_dir_certs, id_digest)))
    return 0;

  n_failures = download_status_get_n_failures(&cl->dl_status);
  return n_failures >= N_AUTH_CERT_DL_FAILURES_TO_BUG_USER;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Free storage held in cert.

Definition at line 4162 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void authority_cert_get_all ( smartlist_t certs_out)

Add every known authority_cert_t to certs_out.

Definition at line 435 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

authority_cert_t* authority_cert_get_by_digests ( const char *  id_digest,
const char *  sk_digest 
)

Return the v3 authority certificate with signing key matching sk_digest, for the authority with identity digest id_digest.

Return NULL if no such authority is known.

Definition at line 419 of file routerlist.c.

{
  cert_list_t *cl;
  if (!trusted_dir_certs ||
      !(cl = digestmap_get(trusted_dir_certs, id_digest)))
    return NULL;
  SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
    if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
      return cert; );

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

authority_cert_t* authority_cert_get_by_sk_digest ( const char *  sk_digest)

Return the newest v3 authority certificate whose directory signing key has digest sk_digest.

Return NULL if no such certificate is known.

Definition at line 392 of file routerlist.c.

{
  authority_cert_t *c;
  if (!trusted_dir_certs)
    return NULL;

  if ((c = get_my_v3_authority_cert()) &&
      tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
    return c;
  if ((c = get_my_v3_legacy_cert()) &&
      tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
    return c;

  DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
    SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
    {
      if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
        return cert;
    });
  } DIGESTMAP_FOREACH_END;
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

authority_cert_t* authority_cert_get_newest_by_id ( const char *  id_digest)

Return the newest v3 authority certificate whose v3 authority identity key has digest id_digest.

Return NULL if no such authority is known, or it has no certificate.

Definition at line 372 of file routerlist.c.

{
  cert_list_t *cl;
  authority_cert_t *best = NULL;
  if (!trusted_dir_certs ||
      !(cl = digestmap_get(trusted_dir_certs, id_digest)))
    return NULL;

  SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
  {
    if (!best || cert->cache_info.published_on > best->cache_info.published_on)
      best = cert;
  });
  return best;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void authority_certs_fetch_missing ( networkstatus_t status,
time_t  now 
)

Try to download any v3 authority certificates that we may be missing.

If status is provided, try to get all the ones that were used to sign status. Additionally, try to have a non-expired certificate for every V3 authority in trusted_dir_servers. Don't fetch certificates we already have.

Definition at line 488 of file routerlist.c.

{
  digestmap_t *pending;
  authority_cert_t *cert;
  smartlist_t *missing_digests;
  char *resource = NULL;
  cert_list_t *cl;
  const int cache = directory_caches_unknown_auth_certs(get_options());

  if (should_delay_dir_fetches(get_options()))
    return;

  pending = digestmap_new();
  missing_digests = smartlist_new();

  list_pending_downloads(pending, DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
  if (status) {
    SMARTLIST_FOREACH_BEGIN(status->voters, networkstatus_voter_info_t *,
                            voter) {
      if (!smartlist_len(voter->sigs))
        continue; /* This authority never signed this consensus, so don't
                   * go looking for a cert with key digest 0000000000. */
      if (!cache &&
          !trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
        continue; /* We are not a cache, and we don't know this authority.*/
      cl = get_cert_list(voter->identity_digest);
      SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
        cert = authority_cert_get_by_digests(voter->identity_digest,
                                             sig->signing_key_digest);
        if (cert) {
          if (now < cert->expires)
            download_status_reset(&cl->dl_status);
          continue;
        }
        if (download_status_is_ready(&cl->dl_status, now,
                                     MAX_CERT_DL_FAILURES) &&
            !digestmap_get(pending, voter->identity_digest)) {
          log_info(LD_DIR, "We're missing a certificate from authority "
                   "with signing key %s: launching request.",
                   hex_str(sig->signing_key_digest, DIGEST_LEN));
          smartlist_add(missing_digests, sig->identity_digest);
        }
      } SMARTLIST_FOREACH_END(sig);
    } SMARTLIST_FOREACH_END(voter);
  }
  SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, ds) {
    int found = 0;
    if (!(ds->type & V3_DIRINFO))
      continue;
    if (smartlist_digest_isin(missing_digests, ds->v3_identity_digest))
      continue;
    cl = get_cert_list(ds->v3_identity_digest);
    SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert, {
      if (now < cert->expires) {
        /* It's not expired, and we weren't looking for something to
         * verify a consensus with.  Call it done. */
        download_status_reset(&cl->dl_status);
        found = 1;
        break;
      }
    });
    if (!found &&
        download_status_is_ready(&cl->dl_status, now,MAX_CERT_DL_FAILURES) &&
        !digestmap_get(pending, ds->v3_identity_digest)) {
      log_info(LD_DIR, "No current certificate known for authority %s; "
               "launching request.", ds->nickname);
        smartlist_add(missing_digests, ds->v3_identity_digest);
    }
  } SMARTLIST_FOREACH_END(ds);

  if (!smartlist_len(missing_digests)) {
    goto done;
  } else {
    smartlist_t *fps = smartlist_new();
    smartlist_add(fps, tor_strdup("fp/"));
    SMARTLIST_FOREACH(missing_digests, const char *, d, {
        char *fp;
        if (digestmap_get(pending, d))
          continue;
        fp = tor_malloc(HEX_DIGEST_LEN+2);
        base16_encode(fp, HEX_DIGEST_LEN+1, d, DIGEST_LEN);
        fp[HEX_DIGEST_LEN] = '+';
        fp[HEX_DIGEST_LEN+1] = '\0';
        smartlist_add(fps, fp);
      });
    if (smartlist_len(fps) == 1) {
      /* we didn't add any: they were all pending */
      SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
      smartlist_free(fps);
      goto done;
    }
    resource = smartlist_join_strings(fps, "", 0, NULL);
    resource[strlen(resource)-1] = '\0';
    SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
    smartlist_free(fps);
  }
  directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
                               resource, PDS_RETRY_IF_NO_SERVERS);

 done:
  tor_free(resource);
  smartlist_free(missing_digests);
  digestmap_free(pending, NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static uint32_t bridge_get_advertised_bandwidth_bounded ( routerinfo_t router) [static]

Return the smaller of the router's configured BandwidthRate and its advertised capacity, making sure to stay within the interval between bridge-min-believe-bw and bridge-max-believe-bw.

Definition at line 1669 of file routerlist.c.

{
  uint32_t result = router->bandwidthcapacity;
  if (result > router->bandwidthrate)
    result = router->bandwidthrate;
  if (result > BRIDGE_MAX_BELIEVABLE_BANDWIDTH)
    result = BRIDGE_MAX_BELIEVABLE_BANDWIDTH;
  else if (result < BRIDGE_MIN_BELIEVABLE_BANDWIDTH)
    result = BRIDGE_MIN_BELIEVABLE_BANDWIDTH;
  return result;
}

Here is the caller graph for this function:

void clear_trusted_dir_servers ( void  )

Remove all members from the list of trusted dir servers.

Definition at line 4189 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int client_would_use_router ( const routerstatus_t rs,
time_t  now,
const or_options_t options 
) [static]

Return 0 if this routerstatus is obsolete, too new, isn't running, or otherwise not a descriptor that we would make any use of even if we had it.

Else return 1.

Definition at line 4342 of file routerlist.c.

{
  if (!rs->is_flagged_running && !options->FetchUselessDescriptors) {
    /* If we had this router descriptor, we wouldn't even bother using it.
     * But, if we want to have a complete list, fetch it anyway. */
    return 0;
  }
  if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
      > now) {
    /* Most caches probably don't have this descriptor yet. */
    return 0;
  }
  if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
    /* We'd drop it immediately for being too old. */
    return 0;
  }
  return 1;
}

Here is the caller graph for this function:

We just fetched a new set of descriptors.

Compute how far through the "loading descriptors" bootstrapping phase we are, so we can inform the controller of our progress.

Definition at line 4970 of file routerlist.c.

{
  int num_present = 0, num_usable=0;
  time_t now = time(NULL);
  const networkstatus_t *consensus =
    networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  double fraction;

  if (!consensus)
    return 0; /* can't count descriptors if we have no list of them */

  count_usable_descriptors(&num_present, &num_usable,
                           consensus, get_options(), now, NULL, 0);

  if (num_usable == 0)
    return 0; /* don't div by 0 */
  fraction = num_present / (num_usable/4.);
  if (fraction > 1.0)
    return 0; /* it's not the number of descriptors holding us back */
  return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
    (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
               BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void count_usable_descriptors ( int *  num_present,
int *  num_usable,
const networkstatus_t consensus,
const or_options_t options,
time_t  now,
routerset_t in_set,
int  exit_only 
) [static]

Iterate over the servers listed in consensus, and count how many of them seem like ones we'd use, and how many of those we have descriptors for.

Store the former in *num_usable and the latter in *num_present. If in_set is non-NULL, only consider those routers in in_set. If exit_only is true, only consider nodes with the Exit flag.

Definition at line 4933 of file routerlist.c.

{
  const int md = (consensus->flavor == FLAV_MICRODESC);
  *num_present = 0, *num_usable=0;

  SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
    {
       if (exit_only && ! rs->is_exit)
         continue;
       if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
         continue;
       if (client_would_use_router(rs, now, options)) {
         const char * const digest = rs->descriptor_digest;
         int present;
         ++*num_usable; /* the consensus says we want it. */
         if (md)
           present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
         else
           present = NULL != router_get_by_descriptor_digest(digest);
         if (present) {
           /* we have the descriptor listed in the consensus. */
           ++*num_present;
         }
       }
     }
  SMARTLIST_FOREACH_END(rs);

  log_debug(LD_DIR, "%d usable, %d present.", *num_usable, *num_present);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE desc_store_t* desc_get_store ( routerlist_t rl,
const signed_descriptor_t sd 
) [static]

Return the desc_store_t in rl that should be used to store sd.

Definition at line 621 of file routerlist.c.

{
  if (sd->is_extrainfo)
    return &rl->extrainfo_store;
  else
    return &rl->desc_store;
}

Here is the caller graph for this function:

void dump_routerlist_mem_usage ( int  severity)

Log information about how much memory is being used for routerlist, at log level severity.

Definition at line 2802 of file routerlist.c.

{
  uint64_t livedescs = 0;
  uint64_t olddescs = 0;
  if (!routerlist)
    return;
  SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
                    livedescs += r->cache_info.signed_descriptor_len);
  SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
                    olddescs += sd->signed_descriptor_len);

  log(severity, LD_DIR,
      "In %d live descriptors: "U64_FORMAT" bytes.  "
      "In %d old descriptors: "U64_FORMAT" bytes.",
      smartlist_len(routerlist->routers), U64_PRINTF_ARG(livedescs),
      smartlist_len(routerlist->old_routers), U64_PRINTF_ARG(olddescs));
}

Here is the caller graph for this function:

const char* esc_router_info ( const routerinfo_t router)

Allocate and return a new string representing the contact info and platform string for router, surrounded by quotes and using standard C escapes.

THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main thread. Also, each call invalidates the last-returned value, so don't try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));

If router is NULL, it just frees its internal memory and returns.

Definition at line 5381 of file routerlist.c.

{
  static char *info=NULL;
  char *esc_contact, *esc_platform;
  tor_free(info);

  if (!router)
    return NULL; /* we're exiting; just free the memory we use */

  esc_contact = esc_for_log(router->contact_info);
  esc_platform = esc_for_log(router->platform);

  tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
  tor_free(esc_contact);
  tor_free(esc_platform);

  return info;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void extrainfo_free ( extrainfo_t extrainfo)

Release all storage held by extrainfo

Definition at line 2727 of file routerlist.c.

{
  if (!extrainfo)
    return;
  tor_free(extrainfo->cache_info.signed_descriptor_body);
  tor_free(extrainfo->pending_sig);

  memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
  tor_free(extrainfo);
}

Here is the caller graph for this function:

Return the signed descriptor for the extrainfo_t in our routerlist whose extra-info-digest is digest.

Return NULL if no such extra-info document is known.

Definition at line 2589 of file routerlist.c.

{
  extrainfo_t *ei;
  tor_assert(digest);
  if (!routerlist) return NULL;
  ei = eimap_get(routerlist->extra_info_map, digest);
  return ei ? &ei->cache_info : NULL;
}

Here is the caller graph for this function:

static int extrainfo_insert ( routerlist_t rl,
extrainfo_t ei 
) [static]

Adds the extrainfo_t ei to the routerlist rl, if there is a corresponding router in rl->routers or rl->old_routers.

Return true iff we actually inserted ei. Free ei if it isn't inserted.

Definition at line 2892 of file routerlist.c.

{
  int r = 0;
  routerinfo_t *ri = rimap_get(rl->identity_map,
                               ei->cache_info.identity_digest);
  signed_descriptor_t *sd =
    sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
  extrainfo_t *ei_tmp;

  {
    extrainfo_t *ei_generated = router_get_my_extrainfo();
    tor_assert(ei_generated != ei);
  }

  if (!ri) {
    /* This router is unknown; we can't even verify the signature. Give up.*/
    goto done;
  }
  if (routerinfo_incompatible_with_extrainfo(ri, ei, sd, NULL)) {
    goto done;
  }

  /* Okay, if we make it here, we definitely have a router corresponding to
   * this extrainfo. */

  ei_tmp = eimap_set(rl->extra_info_map,
                     ei->cache_info.signed_descriptor_digest,
                     ei);
  r = 1;
  if (ei_tmp) {
    rl->extrainfo_store.bytes_dropped +=
      ei_tmp->cache_info.signed_descriptor_len;
    extrainfo_free(ei_tmp);
  }

 done:
  if (r == 0)
    extrainfo_free(ei);

#ifdef DEBUG_ROUTERLIST
  routerlist_assert_ok(rl);
#endif
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static cert_list_t* get_cert_list ( const char *  id_digest) [static]

Helper: Return the cert_list_t for an authority whose authority ID is id_digest, allocating a new list if necessary.

Definition at line 133 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

const char* get_dir_info_status_string ( void  )

Return a string describing what we're missing before we have enough directory info.

Definition at line 4920 of file routerlist.c.

{
  return dir_info_status;
}

Here is the caller graph for this function:

Return the number of directory authorities whose type matches some bit set in type

Definition at line 117 of file routerlist.c.

{
  int n = 0;
  if (!trusted_dir_servers)
    return 0;
  SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
                    if (ds->type & type)
                      ++n);
  return n;
}

Here is the caller graph for this function:

int hex_digest_nickname_decode ( const char *  hexdigest,
char *  digest_out,
char *  nickname_qualifier_char_out,
char *  nickname_out 
)

Helper: given an extended nickname in hexdigest try to decode it.

Return 0 on success, -1 on failure. Store the result into the DIGEST_LEN-byte buffer at digest_out, the single character at nickname_qualifier_char_out, and the MAXNICKNAME_LEN+1-byte buffer at nickname_out.

The recognized format is: HexName = Dollar? HexDigest NamePart? Dollar = '?' HexDigest = HexChar*20 HexChar = 'a'..'f' | 'A'..'F' | '0'..'9' NamePart = QualChar Name QualChar = '=' | '~' Name = NameChar*(1..MAX_NICKNAME_LEN) NameChar = Any ASCII alphanumeric character

Definition at line 2263 of file routerlist.c.

{
  size_t len;

  tor_assert(hexdigest);
  if (hexdigest[0] == '$')
    ++hexdigest;

  len = strlen(hexdigest);
  if (len < HEX_DIGEST_LEN) {
    return -1;
  } else if (len > HEX_DIGEST_LEN && (hexdigest[HEX_DIGEST_LEN] == '=' ||
                                    hexdigest[HEX_DIGEST_LEN] == '~') &&
           len <= HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN) {
    *nickname_qualifier_char_out = hexdigest[HEX_DIGEST_LEN];
    strlcpy(nickname_out, hexdigest+HEX_DIGEST_LEN+1 , MAX_NICKNAME_LEN+1);
  } else if (len == HEX_DIGEST_LEN) {
    ;
  } else {
    return -1;
  }

  if (base16_decode(digest_out, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
    return -1;
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int hex_digest_nickname_matches ( const char *  hexdigest,
const char *  identity_digest,
const char *  nickname,
int  is_named 
) [static]

Helper: Return true iff the identity_digest and nickname combination of a router, encoded in hexadecimal, matches hexdigest (which is optionally prefixed with a single dollar sign).

Return false if hexdigest is malformed, or it doesn't match.

Definition at line 2298 of file routerlist.c.

{
  char digest[DIGEST_LEN];
  char nn_char='\0';
  char nn_buf[MAX_NICKNAME_LEN+1];

  if (hex_digest_nickname_decode(hexdigest, digest, &nn_char, nn_buf) == -1)
    return 0;

  if (nn_char == '=' || nn_char == '~') {
    if (!nickname)
      return 0;
    if (strcasecmp(nn_buf, nickname))
      return 0;
    if (nn_char == '=' && !is_named)
      return 0;
  }

  return tor_memeq(digest, identity_digest, DIGEST_LEN);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int hexdigest_to_digest ( const char *  hexdigest,
char *  digest 
)

If hexdigest is correctly formed, base16_decode it into digest, which must have DIGEST_LEN space in it.

Return 0 on success, -1 on failure.

Definition at line 2516 of file routerlist.c.

{
  if (hexdigest[0]=='$')
    ++hexdigest;
  if (strlen(hexdigest) < HEX_DIGEST_LEN ||
      base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
    return -1;
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return true if this node is currently acting as hidden service directory, false otherwise.

Definition at line 5912 of file routerlist.c.

{
  const routerinfo_t *me = router_get_my_routerinfo();
  if (!me)
    return 0;
  if (!get_options()->HidServDirectoryV2) {
    log_info(LD_REND, "We are not acting as hidden service directory, "
                      "because we have not been configured as such.");
    return 0;
  }
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int hid_serv_get_responsible_directories ( smartlist_t responsible_dirs,
const char *  id 
)

Determine the routers that are responsible for id (binary) and add pointers to those routers' routerstatus_t to responsible_dirs.

Return -1 if we're returning an empty smartlist, else return 0.

Definition at line 5879 of file routerlist.c.

{
  int start, found, n_added = 0, i;
  networkstatus_t *c = networkstatus_get_latest_consensus();
  if (!c || !smartlist_len(c->routerstatus_list)) {
    log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
             "rendezvous operations.");
    return -1;
  }
  tor_assert(id);
  start = networkstatus_vote_find_entry_idx(c, id, &found);
  if (start == smartlist_len(c->routerstatus_list)) start = 0;
  i = start;
  do {
    routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
    if (r->is_hs_dir) {
      smartlist_add(responsible_dirs, r);
      if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
        return 0;
    }
    if (++i == smartlist_len(c->routerstatus_list))
      i = 0;
  } while (i != start);

  /* Even though we don't have the desired number of hidden service
   * directories, be happy if we got any. */
  return smartlist_len(responsible_dirs) ? 0 : -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int hid_serv_responsible_for_desc_id ( const char *  query)

Return true if this node is responsible for storing the descriptor ID in query and false otherwise.

Definition at line 5928 of file routerlist.c.

{
  const routerinfo_t *me;
  routerstatus_t *last_rs;
  const char *my_id, *last_id;
  int result;
  smartlist_t *responsible;
  if (!hid_serv_acting_as_directory())
    return 0;
  if (!(me = router_get_my_routerinfo()))
    return 0; /* This is redundant, but let's be paranoid. */
  my_id = me->cache_info.identity_digest;
  responsible = smartlist_new();
  if (hid_serv_get_responsible_directories(responsible, query) < 0) {
    smartlist_free(responsible);
    return 0;
  }
  last_rs = smartlist_get(responsible, smartlist_len(responsible)-1);
  last_id = last_rs->identity_digest;
  result = rend_id_is_in_interval(my_id, query, last_id);
  smartlist_free(responsible);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void initiate_descriptor_downloads ( const routerstatus_t source,
int  purpose,
smartlist_t digests,
int  lo,
int  hi,
int  pds_flags 
) [static]

Launch downloads for all the descriptors whose digests or digests256 are listed as digests[i] for lo <= i < hi.

(Lo and hi may be out of range.) If source is given, download from source; otherwise, download from an appropriate random directory server.

Definition at line 4280 of file routerlist.c.

{
  int i, n = hi-lo;
  char *resource, *cp;
  size_t r_len;

  int digest_len = DIGEST_LEN, enc_digest_len = HEX_DIGEST_LEN;
  char sep = '+';
  int b64_256 = 0;

  if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
    /* Microdescriptors are downloaded by "-"-separated base64-encoded
     * 256-bit digests. */
    digest_len = DIGEST256_LEN;
    enc_digest_len = BASE64_DIGEST256_LEN;
    sep = '-';
    b64_256 = 1;
  }

  if (n <= 0)
    return;
  if (lo < 0)
    lo = 0;
  if (hi > smartlist_len(digests))
    hi = smartlist_len(digests);

  r_len = 8 + (enc_digest_len+1)*n;
  cp = resource = tor_malloc(r_len);
  memcpy(cp, "d/", 2);
  cp += 2;
  for (i = lo; i < hi; ++i) {
    if (b64_256) {
      digest256_to_base64(cp, smartlist_get(digests, i));
    } else {
      base16_encode(cp, r_len-(cp-resource),
                    smartlist_get(digests,i), digest_len);
    }
    cp += enc_digest_len;
    *cp++ = sep;
  }
  memcpy(cp-1, ".z", 3);

  if (source) {
    /* We know which authority we want. */
    directory_initiate_command_routerstatus(source, purpose,
                                            ROUTER_PURPOSE_GENERAL,
                                            0, /* not private */
                                            resource, NULL, 0, 0);
  } else {
    directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource,
                                 pds_flags);
  }
  tor_free(resource);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int32_t kb_to_bytes ( uint32_t  bw) [static]

Return bw*1000, unless bw*1000 would overflow, in which case return INT32_MAX.

Definition at line 1684 of file routerlist.c.

{
  return (bw > (INT32_MAX/1000)) ? INT32_MAX : bw*1000;
}

Here is the caller graph for this function:

void launch_descriptor_downloads ( int  purpose,
smartlist_t downloadable,
const routerstatus_t source,
time_t  now 
)

Given a purpose (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of router descriptor digests or microdescriptor digest256s in downloadable, decide whether to delay fetching until we have more.

If we don't want to delay, launch one or more requests to the appropriate directory authorities.

Definition at line 4391 of file routerlist.c.

{
  int should_delay = 0, n_downloadable;
  const or_options_t *options = get_options();
  const char *descname;

  tor_assert(purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
             purpose == DIR_PURPOSE_FETCH_MICRODESC);

  descname = (purpose == DIR_PURPOSE_FETCH_SERVERDESC) ?
    "routerdesc" : "microdesc";

  n_downloadable = smartlist_len(downloadable);
  if (!directory_fetches_dir_info_early(options)) {
    if (n_downloadable >= MAX_DL_TO_DELAY) {
      log_debug(LD_DIR,
                "There are enough downloadable %ss to launch requests.",
                descname);
      should_delay = 0;
    } else {
      should_delay = (last_descriptor_download_attempted +
                      MAX_CLIENT_INTERVAL_WITHOUT_REQUEST) > now;
      if (!should_delay && n_downloadable) {
        if (last_descriptor_download_attempted) {
          log_info(LD_DIR,
                   "There are not many downloadable %ss, but we've "
                   "been waiting long enough (%d seconds). Downloading.",
                   descname,
                   (int)(now-last_descriptor_download_attempted));
        } else {
          log_info(LD_DIR,
                   "There are not many downloadable %ss, but we haven't "
                   "tried downloading descriptors recently. Downloading.",
                   descname);
        }
      }
    }
  }
  /* XXX should we consider having even the dir mirrors delay
   * a little bit, so we don't load the authorities as much? -RD
   * I don't think so.  If we do, clients that want those descriptors may
   * not actually find them if the caches haven't got them yet. -NM
   */

  if (! should_delay && n_downloadable) {
    int i, n_per_request;
    const char *req_plural = "", *rtr_plural = "";
    int pds_flags = PDS_RETRY_IF_NO_SERVERS;
    if (! authdir_mode_any_nonhidserv(options)) {
      /* If we wind up going to the authorities, we want to only open one
       * connection to each authority at a time, so that we don't overload
       * them.  We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
       * regardless of whether we're a cache or not; it gets ignored if we're
       * not calling router_pick_trusteddirserver.
       *
       * Setting this flag can make initiate_descriptor_downloads() ignore
       * requests.  We need to make sure that we do in fact call
       * update_router_descriptor_downloads() later on, once the connections
       * have succeeded or failed.
       */
      pds_flags |= (purpose == DIR_PURPOSE_FETCH_MICRODESC) ?
        PDS_NO_EXISTING_MICRODESC_FETCH :
        PDS_NO_EXISTING_SERVERDESC_FETCH;
    }

    n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
    if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
      if (n_per_request > MAX_MICRODESC_DL_PER_REQUEST)
        n_per_request = MAX_MICRODESC_DL_PER_REQUEST;
    } else {
      if (n_per_request > MAX_DL_PER_REQUEST)
        n_per_request = MAX_DL_PER_REQUEST;
    }
    if (n_per_request < MIN_DL_PER_REQUEST)
      n_per_request = MIN_DL_PER_REQUEST;

    if (n_downloadable > n_per_request)
      req_plural = rtr_plural = "s";
    else if (n_downloadable > 1)
      rtr_plural = "s";

    log_info(LD_DIR,
             "Launching %d request%s for %d router%s, %d at a time",
             CEIL_DIV(n_downloadable, n_per_request),
             req_plural, n_downloadable, rtr_plural, n_per_request);
    smartlist_sort_digests(downloadable);
    for (i=0; i < n_downloadable; i += n_per_request) {
      initiate_descriptor_downloads(source, purpose,
                                    downloadable, i, i+n_per_request,
                                    pds_flags);
    }
    last_descriptor_download_attempted = now;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void launch_dummy_descriptor_download_as_needed ( time_t  now,
const or_options_t options 
) [static]

As needed, launch a dummy router descriptor fetch to see if our address has changed.

Definition at line 4772 of file routerlist.c.

{
  static time_t last_dummy_download = 0;
  /* XXXX024 we could be smarter here; see notes on bug 652. */
  /* If we're a server that doesn't have a configured address, we rely on
   * directory fetches to learn when our address changes.  So if we haven't
   * tried to get any routerdescs in a long time, try a dummy fetch now. */
  if (!options->Address &&
      server_mode(options) &&
      last_descriptor_download_attempted + DUMMY_DOWNLOAD_INTERVAL < now &&
      last_dummy_download + DUMMY_DOWNLOAD_INTERVAL < now) {
    last_dummy_download = now;
    directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
                                 ROUTER_PURPOSE_GENERAL, "authority.z",
                                 PDS_RETRY_IF_NO_SERVERS);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void list_pending_descriptor_downloads ( digestmap_t *  result,
int  extrainfo 
) [static]

For every router descriptor (or extra-info document if extrainfo is true) we are currently downloading by descriptor digest, set result[d] to (void*)1.

Definition at line 4254 of file routerlist.c.

{
  int purpose =
    extrainfo ? DIR_PURPOSE_FETCH_EXTRAINFO : DIR_PURPOSE_FETCH_SERVERDESC;
  list_pending_downloads(result, purpose, "d/");
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void list_pending_downloads ( digestmap_t *  result,
int  purpose,
const char *  prefix 
) [static]

For every current directory connection whose purpose is purpose, and where the resource being downloaded begins with prefix, split rest of the resource into base16 fingerprints (or base64 fingerprints if purpose==DIR_PURPPOSE_FETCH_MICRODESC), decode them, and set the corresponding elements of result to a nonzero value.

Definition at line 4219 of file routerlist.c.

{
  const size_t p_len = strlen(prefix);
  smartlist_t *tmp = smartlist_new();
  smartlist_t *conns = get_connection_array();
  int flags = DSR_HEX;
  if (purpose == DIR_PURPOSE_FETCH_MICRODESC)
    flags = DSR_DIGEST256|DSR_BASE64;

  tor_assert(result);

  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
    if (conn->type == CONN_TYPE_DIR &&
        conn->purpose == purpose &&
        !conn->marked_for_close) {
      const char *resource = TO_DIR_CONN(conn)->requested_resource;
      if (!strcmpstart(resource, prefix))
        dir_split_resource_into_fingerprints(resource + p_len,
                                             tmp, NULL, flags);
    }
  } SMARTLIST_FOREACH_END(conn);

  SMARTLIST_FOREACH(tmp, char *, d,
                    {
                      digestmap_set(result, d, (void*)1);
                      tor_free(d);
                    });
  smartlist_free(tmp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void list_pending_microdesc_downloads ( digestmap_t *  result)

For every microdescriptor we are currently downloading by descriptor digest, set result[d] to (void*)1.

(Note that microdescriptor digests are 256-bit, and digestmap_t only holds 160-bit digests, so we're only getting the first 20 bytes of each digest here.)

XXXX Let there be a digestmap256_t, and use that instead.

Definition at line 4269 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static void mark_all_trusteddirservers_up ( void  ) [static]

Go through and mark the authoritative dirservers as up.

Definition at line 1325 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int node_in_nickname_smartlist ( const smartlist_t lst,
const node_t node 
) [static]

Return true iff node is named by some nickname in lst.

Definition at line 1467 of file routerlist.c.

{
  /* XXXX MOVE */
  if (!lst) return 0;
  SMARTLIST_FOREACH(lst, const char *, name, {
    if (node_nickname_matches(node, name))
      return 1;
  });
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int node_is_unreliable ( const node_t node,
int  need_uptime,
int  need_capacity,
int  need_guard 
)

Return 1 if router is not suitable for these parameters, else 0.

If need_uptime is non-zero, we require a minimum uptime. If need_capacity is non-zero, we require a minimum advertised bandwidth. If need_guard, we require that the router is a possible entry guard.

Definition at line 1619 of file routerlist.c.

{
  if (need_uptime && !node->is_stable)
    return 1;
  if (need_capacity && !node->is_fast)
    return 1;
  if (need_guard && !node->is_possible_guard)
    return 1;
  return 0;
}

Here is the caller graph for this function:

static int node_nickname_matches ( const node_t node,
const char *  nickname 
) [static]

Return true if node's nickname matches nickname (case-insensitive), or if node's identity key digest matches a hexadecimal value stored in nickname.

Return false otherwise.

Definition at line 2362 of file routerlist.c.

{
  const char *n = node_get_nickname(node);
  if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
    return 1;
  return hex_digest_nickname_matches(nickname,
                                     node->identity,
                                     n,
                                     node_is_named(node));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void node_set_country ( node_t node)

Refresh the country code of ri.

This function MUST be called on each router when the GeoIP database is reloaded, and on all new routers.

Definition at line 5855 of file routerlist.c.

{
  if (node->rs)
    node->country = geoip_get_country_by_ip(node->rs->addr);
  else if (node->ri)
    node->country = geoip_get_country_by_ip(node->ri->addr);
  else
    node->country = -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Choose a random element of status list sl, weighted by the advertised bandwidth of each node.

Definition at line 2150 of file routerlist.c.

{ /*XXXX MOVE */
  const node_t *ret;
  if ((ret = smartlist_choose_node_by_bandwidth_weights(sl, rule))) {
    return ret;
  } else {
    return smartlist_choose_node_by_bandwidth(sl, rule);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nodelist_add_node_and_family ( smartlist_t sl,
const node_t node 
)

Add all the family of node, including node itself, to the smartlist sl.

This is used to make sure we don't pick siblings in a single path, or pick more than one relay from a family for our entry guard list. Note that a node may be added to sl more than once if it is part of node's family for more than one reason.

Definition at line 1382 of file routerlist.c.

{
  /* XXXX MOVE */
  const smartlist_t *all_nodes = nodelist_get_list();
  const smartlist_t *declared_family;
  const or_options_t *options = get_options();

  tor_assert(node);

  declared_family = node_get_declared_family(node);

  /* Let's make sure that we have the node itself, if it's a real node. */
  {
    const node_t *real_node = node_get_by_id(node->identity);
    if (real_node)
      smartlist_add(sl, (node_t*)real_node);
  }

  /* First, add any nodes with similar network addresses. */
  if (options->EnforceDistinctSubnets) {
    tor_addr_t node_addr;
    node_get_addr(node, &node_addr);

    SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
      tor_addr_t a;
      node_get_addr(node2, &a);
      if (addrs_in_same_network_family(&a, &node_addr))
        smartlist_add(sl, (void*)node2);
    } SMARTLIST_FOREACH_END(node2);
  }

  /* Now, add all nodes in the declared_family of this node, if they
   * also declare this node to be in their family. */
  if (declared_family) {
    /* Add every r such that router declares familyness with node, and node
     * declares familyhood with router. */
    SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
      const node_t *node2;
      const smartlist_t *family2;
      if (!(node2 = node_get_by_nickname(name, 0)))
        continue;
      if (!(family2 = node_get_declared_family(node2)))
        continue;
      SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
          if (node_nickname_matches(node, name2)) {
            smartlist_add(sl, (void*)node2);
            break;
          }
      } SMARTLIST_FOREACH_END(name2);
    } SMARTLIST_FOREACH_END(name);
  }

  /* If the user declared any families locally, honor those too. */
  if (options->NodeFamilySets) {
    SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
      if (routerset_contains_node(rs, node)) {
        routerset_get_all_nodes(sl, rs, NULL, 0);
      }
    });
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nodelist_refresh_countries ( void  )

Set the country code of all routers in the routerlist.

Definition at line 5867 of file routerlist.c.

{
  smartlist_t *nodes = nodelist_get_list();
  SMARTLIST_FOREACH(nodes, node_t *, node,
                    node_set_country(node));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nodes_in_same_family ( const node_t node1,
const node_t node2 
)

Return true iff r1 and r2 are in the same family, but not the same router.

Definition at line 1481 of file routerlist.c.

{
  /* XXXX MOVE */
  const or_options_t *options = get_options();

  /* Are they in the same family because of their addresses? */
  if (options->EnforceDistinctSubnets) {
    tor_addr_t a1, a2;
    node_get_addr(node1, &a1);
    node_get_addr(node2, &a2);
    if (addrs_in_same_network_family(&a1, &a2))
      return 1;
  }

  /* Are they in the same family because the agree they are? */
  {
    const smartlist_t *f1, *f2;
    f1 = node_get_declared_family(node1);
    f2 = node_get_declared_family(node2);
    if (f1 && f2 &&
        node_in_nickname_smartlist(f1, node2) &&
        node_in_nickname_smartlist(f2, node1))
      return 1;
  }

  /* Are they in the same option because the user says they are? */
  if (options->NodeFamilySets) {
    SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
        if (routerset_contains_node(rs, node1) &&
            routerset_contains_node(rs, node2))
          return 1;
      });
  }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void refresh_all_country_info ( void  )

Called when we change a node set, or when we reload the geoip list: recompute all country info in all configuration node sets and in the routerlist.

Definition at line 5565 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

was_router_added_t router_add_extrainfo_to_routerlist ( extrainfo_t ei,
const char **  msg,
int  from_cache,
int  from_fetch 
)

Insert ei into the routerlist, or free it.

Other arguments are as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff we actually inserted it, ROUTER_BAD_EI otherwise.

Definition at line 3489 of file routerlist.c.

{
  int inserted;
  (void)from_fetch;
  if (msg) *msg = NULL;
  /*XXXX023 Do something with msg */

  inserted = extrainfo_insert(router_get_routerlist(), ei);

  if (inserted && !from_cache)
    signed_desc_append_to_journal(&ei->cache_info,
                                  &routerlist->extrainfo_store);

  if (inserted)
    return ROUTER_ADDED_SUCCESSFULLY;
  else
    return ROUTER_BAD_EI;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void router_add_running_nodes_to_smartlist ( smartlist_t sl,
int  allow_invalid,
int  need_uptime,
int  need_capacity,
int  need_guard,
int  need_desc 
) [static]

Add every suitable node from our nodelist to sl, so that we can pick a node for a circuit.

Definition at line 1546 of file routerlist.c.

{ /* XXXX MOVE */
  SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
    if (!node->is_running ||
        (!node->is_valid && !allow_invalid))
      continue;
    if (need_desc && !(node->ri || (node->rs && node->md)))
      continue;
    if (node->ri && node->ri->purpose != ROUTER_PURPOSE_GENERAL)
      continue;
    if (node_is_unreliable(node, need_uptime, need_capacity, need_guard))
      continue;

    smartlist_add(sl, (void *)node);
  } SMARTLIST_FOREACH_END(node);
}

Here is the call graph for this function:

Here is the caller graph for this function:

was_router_added_t router_add_to_routerlist ( routerinfo_t router,
const char **  msg,
int  from_cache,
int  from_fetch 
)

Add router to the routerlist, if we don't already have it.

Replace older entries (if any) with the same key. Note: Callers should not hold their pointers to router if this function fails; router will either be inserted into the routerlist or freed. Similarly, even if this call succeeds, they should not hold their pointers to router after subsequent calls with other routerinfo's -- they might cause the original routerinfo to get freed.

Returns the status for the operation. Might set *msg if it wants the poster of the router to know something.

If from_cache, this descriptor came from our disk cache. If from_fetch, we received it in response to a request we made. (If both are false, that means it was uploaded to us as an auth dir server or via the controller.)

This function should be called after routers_update_status_from_consensus_networkstatus; subsequently, you should call router_rebuild_store and routerlist_descriptors_added.

Definition at line 3302 of file routerlist.c.

{
  const char *id_digest;
  const or_options_t *options = get_options();
  int authdir = authdir_mode_handles_descs(options, router->purpose);
  int authdir_believes_valid = 0;
  routerinfo_t *old_router;
  networkstatus_t *consensus =
    networkstatus_get_latest_consensus_by_flavor(FLAV_NS);
  const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
  int in_consensus = 0;

  tor_assert(msg);

  if (!routerlist)
    router_get_routerlist();

  id_digest = router->cache_info.identity_digest;

  old_router = router_get_mutable_by_digest(id_digest);

  /* Make sure that we haven't already got this exact descriptor. */
  if (sdmap_get(routerlist->desc_digest_map,
                router->cache_info.signed_descriptor_digest)) {
    /* If we have this descriptor already and the new descriptor is a bridge
     * descriptor, replace it. If we had a bridge descriptor before and the
     * new one is not a bridge descriptor, don't replace it. */

    /* Only members of routerlist->identity_map can be bridges; we don't
     * put bridges in old_routers. */
    const int was_bridge = old_router &&
      old_router->purpose == ROUTER_PURPOSE_BRIDGE;

    if (routerinfo_is_a_configured_bridge(router) &&
        router->purpose == ROUTER_PURPOSE_BRIDGE &&
        !was_bridge) {
      log_info(LD_DIR, "Replacing non-bridge descriptor with bridge "
               "descriptor for router %s",
               router_describe(router));
    } else {
      log_info(LD_DIR,
               "Dropping descriptor that we already have for router %s",
               router_describe(router));
      *msg = "Router descriptor was not new.";
      routerinfo_free(router);
      return ROUTER_WAS_NOT_NEW;
    }
  }

  if (authdir) {
    if (authdir_wants_to_reject_router(router, msg,
                                       !from_cache && !from_fetch,
                                       &authdir_believes_valid)) {
      tor_assert(*msg);
      routerinfo_free(router);
      return ROUTER_AUTHDIR_REJECTS;
    }
  } else if (from_fetch) {
    /* Only check the descriptor digest against the network statuses when
     * we are receiving in response to a fetch. */

    if (!signed_desc_digest_is_recognized(&router->cache_info) &&
        !routerinfo_is_a_configured_bridge(router)) {
      /* We asked for it, so some networkstatus must have listed it when we
       * did.  Save it if we're a cache in case somebody else asks for it. */
      log_info(LD_DIR,
               "Received a no-longer-recognized descriptor for router %s",
               router_describe(router));
      *msg = "Router descriptor is not referenced by any network-status.";

      /* Only journal this desc if we'll be serving it. */
      if (!from_cache && should_cache_old_descriptors())
        signed_desc_append_to_journal(&router->cache_info,
                                      &routerlist->desc_store);
      routerlist_insert_old(routerlist, router);
      return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
    }
  }

  /* We no longer need a router with this descriptor digest. */
  SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
  {
    routerstatus_t *rs =
      networkstatus_v2_find_mutable_entry(ns, id_digest);
    if (rs && tor_memeq(rs->descriptor_digest,
                      router->cache_info.signed_descriptor_digest,
                      DIGEST_LEN))
      rs->need_to_mirror = 0;
  });
  if (consensus) {
    routerstatus_t *rs = networkstatus_vote_find_mutable_entry(
                                                     consensus, id_digest);
    if (rs && tor_memeq(rs->descriptor_digest,
                      router->cache_info.signed_descriptor_digest,
                      DIGEST_LEN)) {
      in_consensus = 1;
      rs->need_to_mirror = 0;
    }
  }

  if (router->purpose == ROUTER_PURPOSE_GENERAL &&
      consensus && !in_consensus && !authdir) {
    /* If it's a general router not listed in the consensus, then don't
     * consider replacing the latest router with it. */
    if (!from_cache && should_cache_old_descriptors())
      signed_desc_append_to_journal(&router->cache_info,
                                    &routerlist->desc_store);
    routerlist_insert_old(routerlist, router);
    *msg = "Skipping router descriptor: not in consensus.";
    return ROUTER_NOT_IN_CONSENSUS;
  }

  /* If we're reading a bridge descriptor from our cache, and we don't
   * recognize it as one of our currently configured bridges, drop the
   * descriptor. Otherwise we could end up using it as one of our entry
   * guards even if it isn't in our Bridge config lines. */
  if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache &&
      !authdir_mode_bridge(options) &&
      !routerinfo_is_a_configured_bridge(router)) {
    log_info(LD_DIR, "Dropping bridge descriptor for %s because we have "
             "no bridge configured at that address.",
             safe_str_client(router_describe(router)));
    *msg = "Router descriptor was not a configured bridge.";
    routerinfo_free(router);
    return ROUTER_WAS_NOT_WANTED;
  }

  /* If we have a router with the same identity key, choose the newer one. */
  if (old_router) {
    if (!in_consensus && (router->cache_info.published_on <=
                          old_router->cache_info.published_on)) {
      /* Same key, but old.  This one is not listed in the consensus. */
      log_debug(LD_DIR, "Not-new descriptor for router %s",
                router_describe(router));
      /* Only journal this desc if we'll be serving it. */
      if (!from_cache && should_cache_old_descriptors())
        signed_desc_append_to_journal(&router->cache_info,
                                      &routerlist->desc_store);
      routerlist_insert_old(routerlist, router);
      *msg = "Router descriptor was not new.";
      return ROUTER_WAS_NOT_NEW;
    } else {
      /* Same key, and either new, or listed in the consensus. */
      log_debug(LD_DIR, "Replacing entry for router %s",
                router_describe(router));
      if (routers_have_same_or_addr(router, old_router)) {
        /* these carry over when the address and orport are unchanged. */
        router->last_reachable = old_router->last_reachable;
        router->testing_since = old_router->testing_since;
      }
      routerlist_replace(routerlist, old_router, router);
      if (!from_cache) {
        signed_desc_append_to_journal(&router->cache_info,
                                      &routerlist->desc_store);
      }
      directory_set_dirty();
      *msg = authdir_believes_valid ? "Valid server updated" :
        ("Invalid server updated. (This dirserver is marking your "
         "server as unapproved.)");
      return ROUTER_ADDED_SUCCESSFULLY;
    }
  }

  if (!in_consensus && from_cache &&
      router->cache_info.published_on < time(NULL) - OLD_ROUTER_DESC_MAX_AGE) {
    *msg = "Router descriptor was really old.";
    routerinfo_free(router);
    return ROUTER_WAS_NOT_NEW;
  }

  /* We haven't seen a router with this identity before. Add it to the end of
   * the list. */
  routerlist_insert(routerlist, router);
  if (!from_cache) {
    signed_desc_append_to_journal(&router->cache_info,
                                  &routerlist->desc_store);
  }
  directory_set_dirty();
  return ROUTER_ADDED_SUCCESSFULLY;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_addr_is_trusted_dir ( uint32_t  addr)

Return true iff addr is the address of one of our trusted directory authorities.

Definition at line 2500 of file routerlist.c.

{
  if (!trusted_dir_servers)
    return 0;
  SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
    if (ent->addr == addr)
      return 1;
    );
  return 0;
}

Here is the caller graph for this function:

const node_t* router_choose_random_node ( smartlist_t excludedsmartlist,
routerset_t excludedset,
router_crn_flags_t  flags 
)

Return a random running node from the nodelist.

Never pick a node that is in excludedsmartlist, or which matches excludedset, even if they are the only nodes available. If CRN_NEED_UPTIME is set in flags and any router has more than a minimum uptime, return one of those. If CRN_NEED_CAPACITY is set in flags, weight your choice by the advertised capacity of each router. If CRN_ALLOW_INVALID is not set in flags, consider only Valid routers. If CRN_NEED_GUARD is set in flags, consider only Guard routers. If CRN_WEIGHT_AS_EXIT is set in flags, we weight bandwidths as if picking an exit node, otherwise we weight bandwidths for picking a relay node (that is, possibly discounting exit nodes). If CRN_NEED_DESC is set in flags, we only consider nodes that have a routerinfo or microdescriptor -- that is, enough info to be used to build a circuit.

Definition at line 2180 of file routerlist.c.

{ /* XXXX MOVE */
  const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
  const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
  const int need_guard = (flags & CRN_NEED_GUARD) != 0;
  const int allow_invalid = (flags & CRN_ALLOW_INVALID) != 0;
  const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0;
  const int need_desc = (flags & CRN_NEED_DESC) != 0;

  smartlist_t *sl=smartlist_new(),
    *excludednodes=smartlist_new();
  const node_t *choice = NULL;
  const routerinfo_t *r;
  bandwidth_weight_rule_t rule;

  tor_assert(!(weight_for_exit && need_guard));
  rule = weight_for_exit ? WEIGHT_FOR_EXIT :
    (need_guard ? WEIGHT_FOR_GUARD : WEIGHT_FOR_MID);

  /* Exclude relays that allow single hop exit circuits, if the user
   * wants to (such relays might be risky) */
  if (get_options()->ExcludeSingleHopRelays) {
    SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
      if (node_allows_single_hop_exits(node)) {
        smartlist_add(excludednodes, node);
      });
  }

  if ((r = routerlist_find_my_routerinfo()))
    routerlist_add_node_and_family(excludednodes, r);

  router_add_running_nodes_to_smartlist(sl, allow_invalid,
                                        need_uptime, need_capacity,
                                        need_guard, need_desc);
  smartlist_subtract(sl,excludednodes);
  if (excludedsmartlist)
    smartlist_subtract(sl,excludedsmartlist);
  if (excludedset)
    routerset_subtract_nodes(sl,excludedset);

  // Always weight by bandwidth
  choice = node_sl_choose_by_bandwidth(sl, rule);

  smartlist_free(sl);
  if (!choice && (need_uptime || need_capacity || need_guard)) {
    /* try once more -- recurse but with fewer restrictions. */
    log_info(LD_CIRC,
             "We couldn't find any live%s%s%s routers; falling back "
             "to list of all routers.",
             need_capacity?", fast":"",
             need_uptime?", stable":"",
             need_guard?", guard":"");
    flags &= ~ (CRN_NEED_UPTIME|CRN_NEED_CAPACITY|CRN_NEED_GUARD);
    choice = router_choose_random_node(
                     excludedsmartlist, excludedset, flags);
  }
  smartlist_free(excludednodes);
  if (!choice) {
    log_warn(LD_CIRC,
             "No available nodes when trying to choose node. Failing.");
  }
  return choice;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_differences_are_cosmetic ( const routerinfo_t r1,
const routerinfo_t r2 
)

Return true iff the only differences between r1 and r2 are such that would not cause a recent (post 0.1.1.6) dirserver to republish.

Definition at line 5136 of file routerlist.c.

{
  time_t r1pub, r2pub;
  long time_difference;
  tor_assert(r1 && r2);

  /* r1 should be the one that was published first. */
  if (r1->cache_info.published_on > r2->cache_info.published_on) {
    const routerinfo_t *ri_tmp = r2;
    r2 = r1;
    r1 = ri_tmp;
  }

  /* If any key fields differ, they're different. */
  if (strcasecmp(r1->address, r2->address) ||
      strcasecmp(r1->nickname, r2->nickname) ||
      r1->or_port != r2->or_port ||
      r1->dir_port != r2->dir_port ||
      r1->purpose != r2->purpose ||
      crypto_pk_cmp_keys(r1->onion_pkey, r2->onion_pkey) ||
      crypto_pk_cmp_keys(r1->identity_pkey, r2->identity_pkey) ||
      strcasecmp(r1->platform, r2->platform) ||
      (r1->contact_info && !r2->contact_info) || /* contact_info is optional */
      (!r1->contact_info && r2->contact_info) ||
      (r1->contact_info && r2->contact_info &&
       strcasecmp(r1->contact_info, r2->contact_info)) ||
      r1->is_hibernating != r2->is_hibernating ||
      cmp_addr_policies(r1->exit_policy, r2->exit_policy))
    return 0;
  if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
    return 0;
  if (r1->declared_family && r2->declared_family) {
    int i, n;
    if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
      return 0;
    n = smartlist_len(r1->declared_family);
    for (i=0; i < n; ++i) {
      if (strcasecmp(smartlist_get(r1->declared_family, i),
                     smartlist_get(r2->declared_family, i)))
        return 0;
    }
  }

  /* Did bandwidth change a lot? */
  if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
      (r2->bandwidthcapacity < r1->bandwidthcapacity/2))
    return 0;

  /* Did the bandwidthrate or bandwidthburst change? */
  if ((r1->bandwidthrate != r2->bandwidthrate) ||
      (r1->bandwidthburst != r2->bandwidthburst))
    return 0;

  /* Did more than 12 hours pass? */
  if (r1->cache_info.published_on + ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
      < r2->cache_info.published_on)
    return 0;

  /* Did uptime fail to increase by approximately the amount we would think,
   * give or take some slop? */
  r1pub = r1->cache_info.published_on;
  r2pub = r2->cache_info.published_on;
  time_difference = labs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
  if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
      time_difference > r1->uptime * .05 &&
      time_difference > r2->uptime * .05)
    return 0;

  /* Otherwise, the difference is cosmetic. */
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_digest_is_trusted_dir_type ( const char *  digest,
dirinfo_type_t  type 
)

Return true iff digest is the digest of the identity key of a trusted directory matching at least one bit of type.

If type is zero, any authority is okay.

Definition at line 2484 of file routerlist.c.

{
  if (!trusted_dir_servers)
    return 0;
  if (authdir_mode(get_options()) && router_digest_is_me(digest))
    return 1;
  SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
    if (tor_memeq(digest, ent->digest, DIGEST_LEN)) {
      return (!type) || ((type & ent->type) != 0);
    });
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void router_dir_info_changed ( void  )

Called when our internal view of the directory has changed.

This can be when the authorities change, networkstatuses change, the list of routerdescs changes, or number of running routers changes.

Definition at line 4911 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int router_exit_policy_all_nodes_reject ( const tor_addr_t addr,
uint16_t  port,
int  need_uptime 
)

Return 1 if all running sufficiently-stable routers we can use will reject addr:port, return 0 if any might accept it.

Definition at line 4062 of file routerlist.c.

{ /* XXXX MOVE */
  addr_policy_result_t r;

  SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
    if (node->is_running &&
        !node_is_unreliable(node, need_uptime, 0, 0)) {

      r = compare_tor_addr_to_node_policy(addr, port, node);

      if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
        return 0; /* this one could be ok. good enough. */
    }
  } SMARTLIST_FOREACH_END(node);
  return 1; /* all will reject. */
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return true iff router does not permit exit streams.

Definition at line 4083 of file routerlist.c.

{
  return router->policy_is_reject_star;
}

Here is the caller graph for this function:

const node_t* router_find_exact_exit_enclave ( const char *  address,
uint16_t  port 
)

Find a router that's up, that has this IP address, and that allows exit to this address:port, or return NULL if there isn't a good one.

Don't exit enclave to excluded relays -- it wouldn't actually hurt anything, but this way there are fewer confused users.

Definition at line 1588 of file routerlist.c.

{/*XXXX MOVE*/
  uint32_t addr;
  struct in_addr in;
  tor_addr_t a;
  const or_options_t *options = get_options();

  if (!tor_inet_aton(address, &in))
    return NULL; /* it's not an IP already */
  addr = ntohl(in.s_addr);

  tor_addr_from_ipv4h(&a, addr);

  SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
    if (node_get_addr_ipv4h(node) == addr &&
        node->is_running &&
        compare_tor_addr_to_node_policy(&a, port, node) ==
          ADDR_POLICY_ACCEPTED &&
        !routerset_contains_node(options->_ExcludeExitNodesUnion, node))
      return node;
  });
  return NULL;
}

Here is the call graph for this function:

uint32_t router_get_advertised_bandwidth ( const routerinfo_t router)

Return the smaller of the router's configured BandwidthRate and its advertised capacity.

Definition at line 1634 of file routerlist.c.

{
  if (router->bandwidthcapacity < router->bandwidthrate)
    return router->bandwidthcapacity;
  return router->bandwidthrate;
}

Here is the caller graph for this function:

Return the smaller of the router's configured BandwidthRate and its advertised capacity, capped by max-believe-bw.

Definition at line 1648 of file routerlist.c.

{
  uint32_t result = router->bandwidthcapacity;
  if (result > router->bandwidthrate)
    result = router->bandwidthrate;
  if (result > DEFAULT_MAX_BELIEVABLE_BANDWIDTH)
    result = DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
  return result;
}

Here is the caller graph for this function:

Return the router in our routerlist whose 20-byte descriptor is digest.

Return NULL if no such router is known.

Definition at line 2563 of file routerlist.c.

{
  tor_assert(digest);

  if (!routerlist) return NULL;

  return sdmap_get(routerlist->desc_digest_map, digest);
}

Here is the caller graph for this function:

Return the signed descriptor for the router in our routerlist whose 20-byte extra-info digest is digest.

Return NULL if no such router is known.

Definition at line 2576 of file routerlist.c.

{
  tor_assert(digest);

  if (!routerlist) return NULL;

  return sdmap_get(routerlist->desc_by_eid_map, digest);
}

Here is the caller graph for this function:

const routerinfo_t* router_get_by_hexdigest ( const char *  hexdigest)

Return the router in our routerlist whose hexadecimal key digest is hexdigest.

Return NULL if no such router is known.

Definition at line 2529 of file routerlist.c.

{
  if (is_legal_nickname(hexdigest))
    return NULL;

  /* It's not a legal nickname, so it must be a hexdigest or nothing. */
  return router_get_by_nickname(hexdigest, 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const routerinfo_t* router_get_by_id_digest ( const char *  digest)

Return the router in our routerlist whose 20-byte key digest is digest.

Return NULL if no such router is known.

Definition at line 2555 of file routerlist.c.

{
  return router_get_mutable_by_digest(digest);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const routerinfo_t* router_get_by_nickname ( const char *  nickname,
int  warn_if_unnamed 
)

Return the router in our routerlist whose (case-insensitive) nickname or (case-sensitive) hexadecimal key digest is nickname.

Return NULL if no such router is known.

Definition at line 2378 of file routerlist.c.

{
#if 1
  const node_t *node = node_get_by_nickname(nickname, warn_if_unnamed);
  if (node)
    return node->ri;
  else
    return NULL;
#else
  int maybedigest;
  char digest[DIGEST_LEN];
  routerinfo_t *best_match=NULL;
  int n_matches = 0;
  const char *named_digest = NULL;

  tor_assert(nickname);
  if (!routerlist)
    return NULL;
  if (nickname[0] == '$')
    return router_get_by_hexdigest(nickname);
  if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
    return NULL;

  maybedigest = (strlen(nickname) >= HEX_DIGEST_LEN) &&
    (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);

  if ((named_digest = networkstatus_get_router_digest_by_nickname(nickname))) {
    return rimap_get(routerlist->identity_map, named_digest);
  }
  if (networkstatus_nickname_is_unnamed(nickname))
    return NULL;

  /* If we reach this point, there's no canonical value for the nickname. */

  SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
  {
    if (!strcasecmp(router->nickname, nickname)) {
      ++n_matches;
      if (n_matches <= 1 || router->is_running)
        best_match = router;
    } else if (maybedigest &&
               tor_memeq(digest, router->cache_info.identity_digest,
                         DIGEST_LEN)) {
      if (router_hex_digest_matches(router, nickname))
        return router;
      /* If we reach this point, we have a ID=name syntax that matches the
       * identity but not the name. That isn't an acceptable match. */
    }
  });

  if (best_match) {
    if (warn_if_unnamed && n_matches > 1) {
      smartlist_t *fps = smartlist_new();
      int any_unwarned = 0;
      SMARTLIST_FOREACH_BEGIN(routerlist->routers, routerinfo_t *, router) {
          routerstatus_t *rs;
          char fp[HEX_DIGEST_LEN+1];
          if (strcasecmp(router->nickname, nickname))
            continue;
          rs = router_get_mutable_consensus_status_by_id(
                                          router->cache_info.identity_digest);
          if (rs && !rs->name_lookup_warned) {
            rs->name_lookup_warned = 1;
            any_unwarned = 1;
          }
          base16_encode(fp, sizeof(fp),
                        router->cache_info.identity_digest, DIGEST_LEN);
          smartlist_add_asprintf(fps, "\"$%s\" for the one at %s:%d",
                       fp, router->address, router->or_port);
      } SMARTLIST_FOREACH_END(router);
      if (any_unwarned) {
        char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
        log_warn(LD_CONFIG,
                 "There are multiple matches for the nickname \"%s\","
                 " but none is listed as named by the directory authorities. "
                 "Choosing one arbitrarily. If you meant one in particular, "
                 "you should say %s.", nickname, alternatives);
        tor_free(alternatives);
      }
      SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
      smartlist_free(fps);
    } else if (warn_if_unnamed) {
      routerstatus_t *rs = router_get_mutable_consensus_status_by_id(
          best_match->cache_info.identity_digest);
      if (rs && !rs->name_lookup_warned) {
        char fp[HEX_DIGEST_LEN+1];
        base16_encode(fp, sizeof(fp),
                      best_match->cache_info.identity_digest, DIGEST_LEN);
        log_warn(LD_CONFIG, "You specified a server \"%s\" by name, but this "
             "name is not registered, so it could be used by any server, "
             "not just the one you meant. "
             "To make sure you get the same server in the future, refer to "
             "it by key, as \"$%s\".", nickname, fp);
        rs->name_lookup_warned = 1;
      }
    }
    return best_match;
  }
  return NULL;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

routerinfo_t* router_get_mutable_by_digest ( const char *  digest)

As router_get_by_id_digest,but return a pointer that you're allowed to modify.

Definition at line 2541 of file routerlist.c.

{
  tor_assert(digest);

  if (!routerlist) return NULL;

  // routerlist_assert_ok(routerlist);

  return rimap_get(routerlist->identity_map, digest);
}

Here is the caller graph for this function:

int router_get_my_share_of_directory_requests ( double *  v2_share_out,
double *  v3_share_out 
)

Try to determine which fraction of v2 and v3 directory requests aimed at caches will be sent to us.

Set *v2_share_out and *v3_share_out to the fractions of v2 and v3 protocol shares we expect to see, respectively. Return 0 on success, negative on failure.

Definition at line 976 of file routerlist.c.

{
  const routerinfo_t *me = router_get_my_routerinfo();
  const routerstatus_t *rs;
  const int pds_flags = PDS_ALLOW_SELF|PDS_IGNORE_FASCISTFIREWALL;
  *v2_share_out = *v3_share_out = 0.0;
  if (!me)
    return -1;
  rs = router_get_consensus_status_by_id(me->cache_info.identity_digest);
  if (!rs)
    return -1;

  /* Calling for side effect */
  /* XXXX This is a bit of a kludge */
  if (rs->is_v2_dir) {
    sl_last_total_weighted_bw = 0;
    router_pick_directory_server(V2_DIRINFO, pds_flags);
    if (sl_last_total_weighted_bw != 0) {
      *v2_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
        U64_TO_DBL(sl_last_total_weighted_bw);
    }
  }

  if (rs->version_supports_v3_dir) {
    sl_last_total_weighted_bw = 0;
    router_pick_directory_server(V3_DIRINFO, pds_flags);
    if (sl_last_total_weighted_bw != 0) {
      *v3_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
        U64_TO_DBL(sl_last_total_weighted_bw);
    }
  }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return the current list of all known routers.

Definition at line 2674 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Return a smartlist containing a list of trusted_dir_server_t * for all known trusted dirservers.

Callers must not modify the list or its contents.

Definition at line 924 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Return the trusted_dir_server_t for the directory authority whose identity key hashes to digest, or NULL if no such authority is known.

Definition at line 1016 of file routerlist.c.

{
  if (!trusted_dir_servers)
    return NULL;

  SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
     {
       if (tor_memeq(ds->digest, digest, DIGEST_LEN))
         return ds;
     });

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return true iff we have enough networkstatus and router information to start building circuits.

Right now, this means "more than half the networkstatus documents, and at least 1/4 of expected routers."

Definition at line 4897 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE int router_hex_digest_matches ( const routerinfo_t router,
const char *  hexdigest 
) [static]

Return true iff the digest of router's identity key, encoded in hexadecimal, matches hexdigest (which is optionally prefixed with a single dollar sign).

Return false if hexdigest is malformed, or it doesn't match.

Definition at line 2337 of file routerlist.c.

{
  return hex_digest_nickname_matches(hexdigest,
                                     router->cache_info.identity_digest,
                                     router->nickname,
                                     router_is_named(router));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_is_named ( const routerinfo_t router)

Return true iff router is listed as named in the current consensus.

Definition at line 2323 of file routerlist.c.

{
  const char *digest =
    networkstatus_get_router_digest_by_nickname(router->nickname);

  return (digest &&
          tor_memeq(digest, router->cache_info.identity_digest, DIGEST_LEN));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void router_load_extrainfo_from_string ( const char *  s,
const char *  eos,
saved_location_t  saved_location,
smartlist_t requested_fingerprints,
int  descriptor_digests 
)

Parse one or more extrainfos from s (ending immediately before eos if eos is present).

Other arguments are as for router_load_routers_from_string().

Definition at line 3968 of file routerlist.c.

{
  smartlist_t *extrainfo_list = smartlist_new();
  const char *msg;
  int from_cache = (saved_location != SAVED_NOWHERE);

  router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
                                NULL);

  log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));

  SMARTLIST_FOREACH(extrainfo_list, extrainfo_t *, ei, {
      was_router_added_t added =
        router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
      if (WRA_WAS_ADDED(added) && requested_fingerprints) {
        char fp[HEX_DIGEST_LEN+1];
        base16_encode(fp, sizeof(fp), descriptor_digests ?
                        ei->cache_info.signed_descriptor_digest :
                        ei->cache_info.identity_digest,
                      DIGEST_LEN);
        smartlist_string_remove(requested_fingerprints, fp);
        /* We silently let people stuff us with extrainfos we didn't ask for,
         * so long as we would have wanted them anyway.  Since we always fetch
         * all the extrainfos we want, and we never actually act on them
         * inside Tor, this should be harmless. */
      }
    });

  routerlist_assert_ok(routerlist);
  router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);

  smartlist_free(extrainfo_list);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 
)

Given a string s containing some routerdescs, parse it and put the routers into our directory.

If saved_location is SAVED_NOWHERE, the routers are in response to a query to the network: cache them by adding them to the journal.

Return the number of routers actually added.

If requested_fingerprints is provided, it must contain a list of uppercased fingerprints. Do not update any router whose fingerprint is not on the list; after updating a router, remove its fingerprint from the list.

If descriptor_digests is non-zero, then the requested_fingerprints are descriptor digests. Otherwise they are identity digests.

Definition at line 3892 of file routerlist.c.

{
  smartlist_t *routers = smartlist_new(), *changed = smartlist_new();
  char fp[HEX_DIGEST_LEN+1];
  const char *msg;
  int from_cache = (saved_location != SAVED_NOWHERE);
  int allow_annotations = (saved_location != SAVED_NOWHERE);
  int any_changed = 0;

  router_parse_list_from_string(&s, eos, routers, saved_location, 0,
                                allow_annotations, prepend_annotations);

  routers_update_status_from_consensus_networkstatus(routers, !from_cache);

  log_info(LD_DIR, "%d elements to add", smartlist_len(routers));

  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
    was_router_added_t r;
    char d[DIGEST_LEN];
    if (requested_fingerprints) {
      base16_encode(fp, sizeof(fp), descriptor_digests ?
                      ri->cache_info.signed_descriptor_digest :
                      ri->cache_info.identity_digest,
                    DIGEST_LEN);
      if (smartlist_string_isin(requested_fingerprints, fp)) {
        smartlist_string_remove(requested_fingerprints, fp);
      } else {
        char *requested =
          smartlist_join_strings(requested_fingerprints," ",0,NULL);
        log_warn(LD_DIR,
                 "We received a router descriptor with a fingerprint (%s) "
                 "that we never requested. (We asked for: %s.) Dropping.",
                 fp, requested);
        tor_free(requested);
        routerinfo_free(ri);
        continue;
      }
    }

    memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
    r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
    if (WRA_WAS_ADDED(r)) {
      any_changed++;
      smartlist_add(changed, ri);
      routerlist_descriptors_added(changed, from_cache);
      smartlist_clear(changed);
    } else if (WRA_WAS_REJECTED(r)) {
      download_status_t *dl_status;
      dl_status = router_get_dl_status_by_descriptor_digest(d);
      if (dl_status) {
        log_info(LD_GENERAL, "Marking router %s as never downloadable",
                 hex_str(d, DIGEST_LEN));
        download_status_mark_impossible(dl_status);
      }
    }
  } SMARTLIST_FOREACH_END(ri);

  routerlist_assert_ok(routerlist);

  if (any_changed)
    router_rebuild_store(0, &routerlist->desc_store);

  smartlist_free(routers);
  smartlist_free(changed);

  return any_changed;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_load_single_router ( const char *  s,
uint8_t  purpose,
int  cache,
const char **  msg 
)

Code to parse a single router descriptor and insert it into the routerlist.

Return -1 if the descriptor was ill-formed; 0 if the descriptor was well-formed but could not be added; and 1 if the descriptor was added.

If we don't add it and msg is not NULL, then assign to *msg a static string describing the reason for refusing the descriptor.

This is used only by the controller.

Definition at line 3826 of file routerlist.c.

{
  routerinfo_t *ri;
  was_router_added_t r;
  smartlist_t *lst;
  char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
  tor_assert(msg);
  *msg = NULL;

  tor_snprintf(annotation_buf, sizeof(annotation_buf),
               "@source controller\n"
               "@purpose %s\n", router_purpose_to_string(purpose));

  if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0, annotation_buf))) {
    log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
    *msg = "Couldn't parse router descriptor.";
    return -1;
  }
  tor_assert(ri->purpose == purpose);
  if (router_is_me(ri)) {
    log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
    *msg = "Router's identity key matches mine.";
    routerinfo_free(ri);
    return 0;
  }

  if (!cache) /* obey the preference of the controller */
    ri->cache_info.do_not_cache = 1;

  lst = smartlist_new();
  smartlist_add(lst, ri);
  routers_update_status_from_consensus_networkstatus(lst, 0);

  r = router_add_to_routerlist(ri, msg, 0, 0);
  if (!WRA_WAS_ADDED(r)) {
    /* we've already assigned to *msg now, and ri is already freed */
    tor_assert(*msg);
    if (r == ROUTER_AUTHDIR_REJECTS)
      log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
    smartlist_free(lst);
    return 0;
  } else {
    routerlist_descriptors_added(lst, 0);
    smartlist_free(lst);
    log_debug(LD_DIR, "Added router to list");
    return 1;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_nickname_is_in_list ( const routerinfo_t router,
const char *  list 
)

Return 1 iff any member of the (possibly NULL) comma-separated list list is an acceptable nickname or hexdigest for router.

Else return 0.

Definition at line 1523 of file routerlist.c.

{
  smartlist_t *nickname_list;
  int v = 0;

  if (!list)
    return 0; /* definitely not */
  tor_assert(router);

  nickname_list = smartlist_new();
  smartlist_split_string(nickname_list, list, ",",
    SPLIT_SKIP_SPACE|SPLIT_STRIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  SMARTLIST_FOREACH(nickname_list, const char *, cp,
                    if (router_nickname_matches(router, cp)) {v=1;break;});
  SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
  smartlist_free(nickname_list);
  return v;
}

Here is the call graph for this function:

static int router_nickname_matches ( const routerinfo_t router,
const char *  nickname 
) [static]

Return true if router's nickname matches nickname (case-insensitive), or if router's identity key digest matches a hexadecimal value stored in nickname.

Return false otherwise.

Definition at line 2350 of file routerlist.c.

{
  if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
    return 1;
  return router_hex_digest_matches(router, nickname);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Try to find a running dirserver that supports operations of type.

If there are no running dirservers in our routerlist and the PDS_RETRY_IF_NO_SERVERS flag is set, set all the authoritative ones as running again, and pick one.

If the PDS_IGNORE_FASCISTFIREWALL flag is set, then include dirservers that we can't reach.

If the PDS_ALLOW_SELF flag is not set, then don't include ourself (if we're a dirserver).

Don't pick an authority if any non-authority is viable; try to avoid using servers that have returned 503 recently.

Definition at line 948 of file routerlist.c.

{
  const routerstatus_t *choice;
  if (get_options()->PreferTunneledDirConns)
    flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;

  if (!routerlist)
    return NULL;

  choice = router_pick_directory_server_impl(type, flags);
  if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
    return choice;

  log_info(LD_DIR,
           "No reachable router entries for dirservers. "
           "Trying them all again.");
  /* mark all authdirservers as up again */
  mark_all_trusteddirservers_up();
  /* try again */
  choice = router_pick_directory_server_impl(type, flags);
  return choice;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static const routerstatus_t * router_pick_directory_server_impl ( dirinfo_type_t  type,
int  flags 
) [static]

Pick a random running valid directory server/mirror from our routerlist.

Arguments are as for router_pick_directory_server(), except that RETRY_IF_NO_SERVERS is ignored, and:

If the _PDS_PREFER_TUNNELED_DIR_CONNS flag is set, prefer directory servers that we can use with BEGINDIR.

Definition at line 1090 of file routerlist.c.

{
  const or_options_t *options = get_options();
  const node_t *result;
  smartlist_t *direct, *tunnel;
  smartlist_t *trusted_direct, *trusted_tunnel;
  smartlist_t *overloaded_direct, *overloaded_tunnel;
  time_t now = time(NULL);
  const networkstatus_t *consensus = networkstatus_get_latest_consensus();
  int requireother = ! (flags & PDS_ALLOW_SELF);
  int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
  int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
  int try_excluding = 1, n_excluded = 0;

  if (!consensus)
    return NULL;

 retry_without_exclude:

  direct = smartlist_new();
  tunnel = smartlist_new();
  trusted_direct = smartlist_new();
  trusted_tunnel = smartlist_new();
  overloaded_direct = smartlist_new();
  overloaded_tunnel = smartlist_new();

  /* Find all the running dirservers we know about. */
  SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
    int is_trusted;
    int is_overloaded;
    tor_addr_t addr;
    const routerstatus_t *status = node->rs;
    const country_t country = node->country;
    if (!status)
      continue;

    if (!node->is_running || !status->dir_port || !node->is_valid)
      continue;
    if (node->is_bad_directory)
      continue;
    if (requireother && router_digest_is_me(node->identity))
      continue;
    if (type & V3_DIRINFO) {
      if (!(status->version_supports_v3_dir ||
            router_digest_is_trusted_dir_type(node->identity,
                                              V3_DIRINFO)))
        continue;
    }
    is_trusted = router_digest_is_trusted_dir(node->identity);
    if ((type & V2_DIRINFO) && !(node->rs->is_v2_dir || is_trusted))
      continue;
    if ((type & EXTRAINFO_DIRINFO) &&
        !router_supports_extrainfo(node->identity, 0))
      continue;
    if ((type & MICRODESC_DIRINFO) && !is_trusted &&
        !node->rs->version_supports_microdesc_cache)
      continue;
    if (try_excluding &&
        routerset_contains_routerstatus(options->ExcludeNodes, status,
                                        country)) {
      ++n_excluded;
      continue;
    }

    /* XXXX IP6 proposal 118 */
    tor_addr_from_ipv4h(&addr, node->rs->addr);

    is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;

    if (prefer_tunnel &&
        status->version_supports_begindir &&
        (!fascistfirewall ||
         fascist_firewall_allows_address_or(&addr, status->or_port)))
      smartlist_add(is_trusted ? trusted_tunnel :
                    is_overloaded ? overloaded_tunnel : tunnel, (void*)node);
    else if (!fascistfirewall ||
             fascist_firewall_allows_address_dir(&addr, status->dir_port))
      smartlist_add(is_trusted ? trusted_direct :
                    is_overloaded ? overloaded_direct : direct, (void*)node);
  } SMARTLIST_FOREACH_END(node);

  if (smartlist_len(tunnel)) {
    result = node_sl_choose_by_bandwidth(tunnel, WEIGHT_FOR_DIR);
  } else if (smartlist_len(overloaded_tunnel)) {
    result = node_sl_choose_by_bandwidth(overloaded_tunnel,
                                                 WEIGHT_FOR_DIR);
  } else if (smartlist_len(trusted_tunnel)) {
    /* FFFF We don't distinguish between trusteds and overloaded trusteds
     * yet. Maybe one day we should. */
    /* FFFF We also don't load balance over authorities yet. I think this
     * is a feature, but it could easily be a bug. -RD */
    result = smartlist_choose(trusted_tunnel);
  } else if (smartlist_len(direct)) {
    result = node_sl_choose_by_bandwidth(direct, WEIGHT_FOR_DIR);
  } else if (smartlist_len(overloaded_direct)) {
    result = node_sl_choose_by_bandwidth(overloaded_direct,
                                         WEIGHT_FOR_DIR);
  } else {
    result = smartlist_choose(trusted_direct);
  }
  smartlist_free(direct);
  smartlist_free(tunnel);
  smartlist_free(trusted_direct);
  smartlist_free(trusted_tunnel);
  smartlist_free(overloaded_direct);
  smartlist_free(overloaded_tunnel);

  if (result == NULL && try_excluding && !options->StrictNodes && n_excluded) {
    /* If we got no result, and we are excluding nodes, and StrictNodes is
     * not set, try again without excluding nodes. */
    try_excluding = 0;
    n_excluded = 0;
    goto retry_without_exclude;
  }

  return result ? result->rs : NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Try to find a running trusted dirserver.

Flags are as for router_pick_directory_server.

Definition at line 1054 of file routerlist.c.

{
  const routerstatus_t *choice;
  int busy = 0;
  if (get_options()->PreferTunneledDirConns)
    flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;

  choice = router_pick_trusteddirserver_impl(type, flags, &busy);
  if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
    return choice;
  if (busy) {
    /* If the reason that we got no server is that servers are "busy",
     * we must be excluding good servers because we already have serverdesc
     * fetches with them.  Do not mark down servers up because of this. */
    tor_assert((flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
                         PDS_NO_EXISTING_MICRODESC_FETCH)));
    return NULL;
  }

  log_info(LD_DIR,
           "No trusted dirservers are reachable. Trying them all again.");
  mark_all_trusteddirservers_up();
  return router_pick_trusteddirserver_impl(type, flags, NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static const routerstatus_t * router_pick_trusteddirserver_impl ( dirinfo_type_t  type,
int  flags,
int *  n_busy_out 
) [static]

Choose randomly from among the trusted dirservers that are up.

Flags are as for router_pick_directory_server_impl().

Definition at line 1212 of file routerlist.c.

{
  const or_options_t *options = get_options();
  smartlist_t *direct, *tunnel;
  smartlist_t *overloaded_direct, *overloaded_tunnel;
  const routerinfo_t *me = router_get_my_routerinfo();
  const routerstatus_t *result;
  time_t now = time(NULL);
  const int requireother = ! (flags & PDS_ALLOW_SELF);
  const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
  const int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
  const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
  const int no_microdesc_fetching =(flags & PDS_NO_EXISTING_MICRODESC_FETCH);
  int n_busy = 0;
  int try_excluding = 1, n_excluded = 0;

  if (!trusted_dir_servers)
    return NULL;

 retry_without_exclude:

  direct = smartlist_new();
  tunnel = smartlist_new();
  overloaded_direct = smartlist_new();
  overloaded_tunnel = smartlist_new();

  SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, d)
    {
      int is_overloaded =
          d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
      tor_addr_t addr;
      if (!d->is_running) continue;
      if ((type & d->type) == 0)
        continue;
      if ((type & EXTRAINFO_DIRINFO) &&
          !router_supports_extrainfo(d->digest, 1))
        continue;
      if (requireother && me && router_digest_is_me(d->digest))
          continue;
      if (try_excluding &&
          routerset_contains_routerstatus(options->ExcludeNodes,
                                          &d->fake_status, -1)) {
        ++n_excluded;
        continue;
      }

      /* XXXX IP6 proposal 118 */
      tor_addr_from_ipv4h(&addr, d->addr);

      if (no_serverdesc_fetching) {
        if (connection_get_by_type_addr_port_purpose(
            CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_SERVERDESC)
         || connection_get_by_type_addr_port_purpose(
             CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_EXTRAINFO)) {
          //log_debug(LD_DIR, "We have an existing connection to fetch "
          //           "descriptor from %s; delaying",d->description);
          ++n_busy;
          continue;
        }
      }
      if (no_microdesc_fetching) {
        if (connection_get_by_type_addr_port_purpose(
             CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_MICRODESC)) {
          ++n_busy;
          continue;
        }
      }

      if (prefer_tunnel &&
          d->or_port &&
          (!fascistfirewall ||
           fascist_firewall_allows_address_or(&addr, d->or_port)))
        smartlist_add(is_overloaded ? overloaded_tunnel : tunnel,
                      &d->fake_status);
      else if (!fascistfirewall ||
               fascist_firewall_allows_address_dir(&addr, d->dir_port))
        smartlist_add(is_overloaded ? overloaded_direct : direct,
                      &d->fake_status);
    }
  SMARTLIST_FOREACH_END(d);

  if (smartlist_len(tunnel)) {
    result = smartlist_choose(tunnel);
  } else if (smartlist_len(overloaded_tunnel)) {
    result = smartlist_choose(overloaded_tunnel);
  } else if (smartlist_len(direct)) {
    result = smartlist_choose(direct);
  } else {
    result = smartlist_choose(overloaded_direct);
  }

  if (n_busy_out)
    *n_busy_out = n_busy;

  smartlist_free(direct);
  smartlist_free(tunnel);
  smartlist_free(overloaded_direct);
  smartlist_free(overloaded_tunnel);

  if (result == NULL && try_excluding && !options->StrictNodes && n_excluded) {
    /* If we got no result, and we are excluding nodes, and StrictNodes is
     * not set, try again without excluding nodes. */
    try_excluding = 0;
    n_excluded = 0;
    goto retry_without_exclude;
  }

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int router_rebuild_store ( int  flags,
desc_store_t store 
) [static]

If the journal of store is too long, or if RRS_FORCE is set in flags, then atomically replace the saved router store with the routers currently in our routerlist, and clear the journal.

Unless RRS_DONT_REMOVE_OLD is set in flags, delete expired routers before rebuilding the store. Return 0 on success, -1 on failure.

Definition at line 674 of file routerlist.c.

{
  smartlist_t *chunk_list = NULL;
  char *fname = NULL, *fname_tmp = NULL;
  int r = -1;
  off_t offset = 0;
  smartlist_t *signed_descriptors = NULL;
  int nocache=0;
  size_t total_expected_len = 0;
  int had_any;
  int force = flags & RRS_FORCE;

  if (!force && !router_should_rebuild_store(store)) {
    r = 0;
    goto done;
  }
  if (!routerlist) {
    r = 0;
    goto done;
  }

  if (store->type == EXTRAINFO_STORE)
    had_any = !eimap_isempty(routerlist->extra_info_map);
  else
    had_any = (smartlist_len(routerlist->routers)+
               smartlist_len(routerlist->old_routers))>0;

  /* Don't save deadweight. */
  if (!(flags & RRS_DONT_REMOVE_OLD))
    routerlist_remove_old_routers();

  log_info(LD_DIR, "Rebuilding %s cache", store->description);

  fname = get_datadir_fname(store->fname_base);
  fname_tmp = get_datadir_fname_suffix(store->fname_base, ".tmp");

  chunk_list = smartlist_new();

  /* We sort the routers by age to enhance locality on disk. */
  signed_descriptors = smartlist_new();
  if (store->type == EXTRAINFO_STORE) {
    eimap_iter_t *iter;
    for (iter = eimap_iter_init(routerlist->extra_info_map);
         !eimap_iter_done(iter);
         iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
      const char *key;
      extrainfo_t *ei;
      eimap_iter_get(iter, &key, &ei);
      smartlist_add(signed_descriptors, &ei->cache_info);
    }
  } else {
    SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
                      smartlist_add(signed_descriptors, sd));
    SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
                      smartlist_add(signed_descriptors, &ri->cache_info));
  }

  smartlist_sort(signed_descriptors, _compare_signed_descriptors_by_age);

  /* Now, add the appropriate members to chunk_list */
  SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
    {
      sized_chunk_t *c;
      const char *body = signed_descriptor_get_body_impl(sd, 1);
      if (!body) {
        log_warn(LD_BUG, "No descriptor available for router.");
        goto done;
      }
      if (sd->do_not_cache) {
        ++nocache;
        continue;
      }
      c = tor_malloc(sizeof(sized_chunk_t));
      c->bytes = body;
      c->len = sd->signed_descriptor_len + sd->annotations_len;
      total_expected_len += c->len;
      smartlist_add(chunk_list, c);
    });

  if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
    log_warn(LD_FS, "Error writing router store to disk.");
    goto done;
  }

  /* Our mmap is now invalid. */
  if (store->mmap) {
    tor_munmap_file(store->mmap);
    store->mmap = NULL;
  }

  if (replace_file(fname_tmp, fname)<0) {
    log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
    goto done;
  }

  errno = 0;
  store->mmap = tor_mmap_file(fname);
  if (! store->mmap) {
    if (errno == ERANGE) {
      /* empty store.*/
      if (total_expected_len) {
        log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
                 " but when we went to mmap it, it was empty!", fname);
      } else if (had_any) {
        log_info(LD_FS, "We just removed every descriptor in '%s'.  This is "
                 "okay if we're just starting up after a long time. "
                 "Otherwise, it's a bug.", fname);
      }
    } else {
      log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
    }
  }

  log_info(LD_DIR, "Reconstructing pointers into cache");

  offset = 0;
  SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
    {
      if (sd->do_not_cache)
        continue;
      sd->saved_location = SAVED_IN_CACHE;
      if (store->mmap) {
        tor_free(sd->signed_descriptor_body); // sets it to null
        sd->saved_offset = offset;
      }
      offset += sd->signed_descriptor_len + sd->annotations_len;
      signed_descriptor_get_body(sd); /* reconstruct and assert */
    });

  tor_free(fname);
  fname = get_datadir_fname_suffix(store->fname_base, ".new");
  write_str_to_file(fname, "", 1);

  r = 0;
  store->store_len = (size_t) offset;
  store->journal_len = 0;
  store->bytes_dropped = 0;
 done:
  smartlist_free(signed_descriptors);
  tor_free(fname);
  tor_free(fname_tmp);
  if (chunk_list) {
    SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
    smartlist_free(chunk_list);
  }

  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int router_reload_router_list ( void  )

Load all cached router descriptors and extra-info documents from the store.

Return 0 on success and -1 on failure.

Definition at line 909 of file routerlist.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static int router_reload_router_list_impl ( desc_store_t store) [static]

Helper: Reload a cache file and its associated journal, setting metadata appropriately.

If extrainfo is true, reload the extrainfo store; else reload the router descriptor store.

Definition at line 827 of file routerlist.c.

{
  char *fname = NULL, *altname = NULL, *contents = NULL;
  struct stat st;
  int read_from_old_location = 0;
  int extrainfo = (store->type == EXTRAINFO_STORE);
  time_t now = time(NULL);
  store->journal_len = store->store_len = 0;

  fname = get_datadir_fname(store->fname_base);
  if (store->fname_alt_base)
    altname = get_datadir_fname(store->fname_alt_base);

  if (store->mmap) /* get rid of it first */
    tor_munmap_file(store->mmap);
  store->mmap = NULL;

  store->mmap = tor_mmap_file(fname);
  if (!store->mmap && altname && file_status(altname) == FN_FILE) {
    read_from_old_location = 1;
    log_notice(LD_DIR, "Couldn't read %s; trying to load routers from old "
               "location %s.", fname, altname);
    if ((store->mmap = tor_mmap_file(altname)))
      read_from_old_location = 1;
  }
  if (altname && !read_from_old_location) {
    remove_file_if_very_old(altname, now);
  }
  if (store->