tor  0.4.2.0-alpha-dev
Data Structures | Macros | Typedefs | Functions | Variables
bwauth.c File Reference
#include "core/or/or.h"
#include "feature/dirauth/bwauth.h"
#include "app/config/config.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/routerlist.h"
#include "feature/dirparse/ns_parse.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/vote_routerstatus_st.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/encoding/keyval.h"

Go to the source code of this file.

Data Structures

struct  mbw_cache_entry_s
 

Typedefs

typedef struct mbw_cache_entry_s mbw_cache_entry_t
 

Functions

void dirserv_count_measured_bws (const smartlist_t *routers)
 
int dirserv_get_last_n_measured_bws (void)
 
STATIC void dirserv_cache_measured_bw (const measured_bw_line_t *parsed_line, time_t as_of)
 
void dirserv_clear_measured_bw_cache (void)
 
STATIC void dirserv_expire_measured_bw_cache (time_t now)
 
int dirserv_query_measured_bw_cache_kb (const char *node_id, long *bw_kb_out, time_t *as_of_out)
 
int dirserv_has_measured_bw (const char *node_id)
 
int dirserv_get_measured_bw_cache_size (void)
 
uint32_t dirserv_get_credible_bandwidth_kb (const routerinfo_t *ri)
 
int dirserv_read_measured_bandwidths (const char *from_file, smartlist_t *routerstatuses, smartlist_t *bw_file_headers, uint8_t *digest_out)
 
STATIC int measured_bw_line_parse (measured_bw_line_t *out, const char *orig_line, int line_is_after_headers)
 
STATIC int measured_bw_line_apply (measured_bw_line_t *parsed_line, smartlist_t *routerstatuses)
 

Variables

static int routers_with_measured_bw = 0
 
static digestmap_t * mbw_cache = NULL
 

Detailed Description

Code to read and apply bandwidth authority data.

Code to read and apply guard fraction data.

Definition in file bwauth.c.

Typedef Documentation

◆ mbw_cache_entry_t

Measured bandwidth cache entry

Function Documentation

◆ dirserv_cache_measured_bw()

STATIC void dirserv_cache_measured_bw ( const measured_bw_line_t *  parsed_line,
time_t  as_of 
)

Store a measured bandwidth cache entry when reading the measured bandwidths file.

Definition at line 71 of file bwauth.c.

References mbw_cache, and tor_assert().

◆ dirserv_clear_measured_bw_cache()

void dirserv_clear_measured_bw_cache ( void  )

Clear and free the measured bandwidth cache

Definition at line 101 of file bwauth.c.

References mbw_cache.

◆ dirserv_count_measured_bws()

void dirserv_count_measured_bws ( const smartlist_t routers)

Look through the routerlist, and using the measured bandwidth cache count how many measured bandwidths we know. This is used to decide whether we ever trust advertised bandwidths for purposes of assigning flags.

Definition at line 37 of file bwauth.c.

References dirserv_has_measured_bw(), routers_with_measured_bw, and SMARTLIST_FOREACH_BEGIN.

◆ dirserv_expire_measured_bw_cache()

STATIC void dirserv_expire_measured_bw_cache ( time_t  now)

Scan the measured bandwidth cache and remove expired entries

Definition at line 112 of file bwauth.c.

References DIGESTMAP_FOREACH_MODIFY, and mbw_cache.

◆ dirserv_get_credible_bandwidth_kb()

uint32_t dirserv_get_credible_bandwidth_kb ( const routerinfo_t ri)

Return the bandwidth we believe for assigning flags; prefer measured over advertised, and if we have above a threshold quantity of measured bandwidths, we don't want to ever give flags to unmeasured routers, so return 0.

Definition at line 175 of file bwauth.c.

References dirserv_query_measured_bw_cache_kb(), signed_descriptor_t::identity_digest, router_get_advertised_bandwidth_capped(), routers_with_measured_bw, and tor_assert().

Referenced by dirauth_set_routerstatus_from_routerinfo(), dirserv_thinks_router_is_unreliable(), and router_counts_toward_thresholds().

◆ dirserv_get_last_n_measured_bws()

int dirserv_get_last_n_measured_bws ( void  )

Return the last-computed result from dirserv_count_mesured_bws().

Definition at line 53 of file bwauth.c.

References routers_with_measured_bw.

Referenced by dirserv_compute_performance_thresholds(), and dirserv_get_flag_thresholds_line().

◆ dirserv_get_measured_bw_cache_size()

int dirserv_get_measured_bw_cache_size ( void  )

Get the current size of the measured bandwidth cache

Definition at line 164 of file bwauth.c.

References mbw_cache.

◆ dirserv_has_measured_bw()

int dirserv_has_measured_bw ( const char *  node_id)

Predicate wrapper for dirserv_query_measured_bw_cache()

Definition at line 157 of file bwauth.c.

References dirserv_query_measured_bw_cache_kb().

Referenced by dirserv_count_measured_bws(), and router_counts_toward_thresholds().

◆ dirserv_query_measured_bw_cache_kb()

int dirserv_query_measured_bw_cache_kb ( const char *  node_id,
long *  bw_kb_out,
time_t *  as_of_out 
)

Query the cache by identity digest, return value indicates whether we found it. The bw_out and as_of_out pointers receive the cached bandwidth value and the time it was cached if not NULL.

Definition at line 136 of file bwauth.c.

Referenced by dirserv_get_bandwidth_for_router_kb(), dirserv_get_credible_bandwidth_kb(), and dirserv_has_measured_bw().

◆ dirserv_read_measured_bandwidths()

int dirserv_read_measured_bandwidths ( const char *  from_file,
smartlist_t routerstatuses,
smartlist_t bw_file_headers,
uint8_t *  digest_out 
)

Read the measured bandwidth list from_file:

  • store all the headers in bw_file_headers,
  • apply bandwidth lines to the list of vote_routerstatus_t in routerstatuses,
  • cache bandwidth lines for dirserv_get_bandwidth_for_router(),
  • expire old entries in the measured bandwidth cache, and
  • store the DIGEST_SHA256 of the contents of the file in digest_out.

Returns -1 on error, 0 otherwise.

If the file can't be read, or is empty:

  • bw_file_headers is empty,
  • routerstatuses is not modified,
  • the measured bandwidth cache is not modified, and
  • digest_out is the zero-byte digest.

Otherwise, if there is an error later in the file:

  • bw_file_headers contains all the headers up to the error,
  • routerstatuses is updated with all the relay lines up to the error,
  • the measured bandwidth cache is updated with all the relay lines up to the error,
  • if the timestamp is valid and recent, old entries in the measured bandwidth cache are expired, and
  • digest_out is the digest up to the first read error (if any). The digest is taken over all the readable file contents, even if the file is outdated or unparseable.

Definition at line 230 of file bwauth.c.

References crypto_digest256_new(), and tor_fopen_cloexec().

◆ measured_bw_line_apply()

STATIC int measured_bw_line_apply ( measured_bw_line_t *  parsed_line,
smartlist_t routerstatuses 
)

Helper function to apply a parsed measurement line to a list of bandwidth statuses. Returns true if a line is found, false otherwise.

Definition at line 481 of file bwauth.c.

References compare_digest_to_vote_routerstatus_entry(), vote_routerstatus_t::has_measured_bw, vote_routerstatus_t::measured_bw_kb, and smartlist_bsearch().

◆ measured_bw_line_parse()

STATIC int measured_bw_line_parse ( measured_bw_line_t *  out,
const char *  orig_line,
int  line_is_after_headers 
)

Helper function to parse out a line in the measured bandwidth file into a measured_bw_line_t output structure.

If line_is_after_headers is true, then if we encounter an incomplete bw line, return -1 and warn, since we are after the headers and we should only parse bw lines. Return 0 otherwise.

If line_is_after_headers is false then it means that we are not past the header block yet. If we encounter an incomplete bw line, return -1 but don't warn since there could be additional header lines coming. If we encounter a proper bw line, return 0 (and we got past the headers).

If the line contains "vote=0", stop parsing it, and return -1, so that the line is ignored during voting.

Definition at line 373 of file bwauth.c.

Variable Documentation

◆ mbw_cache

digestmap_t* mbw_cache = NULL
static

Measured bandwidth cache - keys are identity_digests, values are mbw_cache_entry_t *.

Definition at line 66 of file bwauth.c.

Referenced by dirserv_cache_measured_bw(), dirserv_clear_measured_bw_cache(), dirserv_expire_measured_bw_cache(), and dirserv_get_measured_bw_cache_size().

◆ routers_with_measured_bw

int routers_with_measured_bw = 0
static

Total number of routers with measured bandwidth; this is set by dirserv_count_measured_bs() before the loop in dirserv_generate_networkstatus_vote_obj() and checked by dirserv_get_credible_bandwidth() and dirserv_compute_performance_thresholds()

Definition at line 31 of file bwauth.c.

Referenced by dirserv_count_measured_bws(), dirserv_get_credible_bandwidth_kb(), and dirserv_get_last_n_measured_bws().