11 #define BWAUTH_PRIVATE
50 } SMARTLIST_FOREACH_END(ri);
84 e = digestmap_get(
mbw_cache, parsed_line->node_id);
88 if (as_of > e->as_of) {
89 e->mbw_kb = parsed_line->bw_kb;
94 e = tor_malloc(
sizeof(*e));
95 e->mbw_kb = parsed_line->bw_kb;
97 digestmap_set(
mbw_cache, parsed_line->node_id, e);
120 if (now > e->as_of + MAX_MEASUREMENT_AGE) {
149 if (bw_kb_out) *bw_kb_out = v->mbw_kb;
150 if (as_of_out) *as_of_out = v->as_of;
197 bw_kb = (uint32_t)mbw_kb;
238 int applied_lines = 0;
239 time_t file_time, now;
245 int line_is_after_headers = 0;
252 log_warn(
LD_CONFIG,
"Can't open bandwidth file at configured location: %s",
257 if (tor_getline(&line,&n,fp) <= 0) {
264 if (!strlen(line) || line[strlen(line)-1] !=
'\n') {
265 log_warn(
LD_DIRSERV,
"Long or truncated time in bandwidth file: %s",
268 goto continue_digest;
271 line[strlen(line)-1] =
'\0';
272 file_time = (time_t)
tor_parse_ulong(line, 10, 0, ULONG_MAX, &ok, NULL);
274 log_warn(
LD_DIRSERV,
"Non-integer time in bandwidth file: %s",
276 goto continue_digest;
280 if ((now - file_time) > MAX_MEASUREMENT_AGE) {
281 log_warn(
LD_DIRSERV,
"Bandwidth measurement file stale. Age: %u",
282 (
unsigned)(time(NULL) - file_time));
283 goto continue_digest;
290 (
unsigned long)file_time);
293 smartlist_sort(routerstatuses, compare_vote_routerstatus_entries);
296 measured_bw_line_t parsed_line;
297 if (tor_getline(&line, &n, fp) >= 0) {
300 line_is_after_headers) != -1) {
303 line_is_after_headers = 1;
311 line_is_after_headers = 1;
317 }
else if (bw_file_headers &&
318 (line_is_after_headers == 0) &&
320 !strchr(line,
' ') &&
321 (smartlist_len(bw_file_headers)
323 line[strlen(line)-1] =
'\0';
333 "Bandwidth measurement file successfully read. "
334 "Applied %d measurements.", applied_lines);
340 if (tor_getline(&line, &n, fp) >= 0) {
376 int line_is_after_headers)
378 char *line = tor_strdup(orig_line);
384 if (strlen(line) == 0) {
385 log_warn(
LD_DIRSERV,
"Empty line in bandwidth file");
391 if (line[strlen(line) - 1] ==
'\n') {
392 line[strlen(line) - 1] =
'\0';
395 cp = tor_strtok_r(cp,
" \t", &strtok_state);
398 log_warn(
LD_DIRSERV,
"Invalid line in bandwidth file: %s",
404 if (orig_line[strlen(orig_line)-1] !=
'\n') {
405 log_warn(
LD_DIRSERV,
"Incomplete line in bandwidth file: %s",
414 log_debug(
LD_DIRSERV,
"Ignoring bandwidth file line that contains "
415 "vote=0: %s",
escaped(orig_line));
422 log_warn(
LD_DIRSERV,
"Double bw= in bandwidth file line: %s",
429 out->bw_kb =
tor_parse_long(cp, 10, 0, LONG_MAX, &parse_ok, &endptr);
430 if (!parse_ok || (*endptr && !TOR_ISSPACE(*endptr))) {
431 log_warn(
LD_DIRSERV,
"Invalid bandwidth in bandwidth file line: %s",
439 log_warn(
LD_DIRSERV,
"Double node_id= in bandwidth file line: %s",
444 cp+=strlen(
"node_id=$");
449 log_warn(
LD_DIRSERV,
"Invalid node_id in bandwidth file line: %s",
454 strlcpy(out->node_hex, cp,
sizeof(out->node_hex));
457 }
while ((cp = tor_strtok_r(NULL,
" \t", &strtok_state)));
459 if (got_bw && got_node_id) {
462 }
else if (line_is_after_headers == 0) {
465 log_debug(
LD_DIRSERV,
"Missing bw or node_id in bandwidth file line: %s",
470 log_warn(
LD_DIRSERV,
"Incomplete line in bandwidth file: %s",
497 log_info(
LD_DIRSERV,
"Node ID %s not found in routerstatus list",
498 parsed_line->node_hex);
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
uint32_t dirserv_get_credible_bandwidth_kb(const routerinfo_t *ri)
int dirserv_get_measured_bw_cache_size(void)
int dirserv_read_measured_bandwidths(const char *from_file, smartlist_t *routerstatuses, smartlist_t *bw_file_headers, uint8_t *digest_out)
void dirserv_count_measured_bws(const smartlist_t *routers)
int dirserv_query_measured_bw_cache_kb(const char *node_id, long *bw_kb_out, time_t *as_of_out)
static int routers_with_measured_bw
int dirserv_get_last_n_measured_bws(void)
STATIC int measured_bw_line_apply(measured_bw_line_t *parsed_line, smartlist_t *routerstatuses)
int dirserv_has_measured_bw(const char *node_id)
STATIC void dirserv_expire_measured_bw_cache(time_t now)
STATIC void dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line, time_t as_of)
STATIC int measured_bw_line_parse(measured_bw_line_t *out, const char *orig_line, int line_is_after_headers)
void dirserv_clear_measured_bw_cache(void)
static digestmap_t * mbw_cache
Header file for bwauth.c.
#define MAX_BW_FILE_HEADER_COUNT_IN_VOTE
#define BW_FILE_HEADERS_TERMINATOR
Header file for config.c.
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
crypto_digest_t * crypto_digest256_new(digest_algorithm_t algorithm)
#define crypto_digest_free(d)
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
Structure dirauth_options_t to hold directory authority options.
Header for dirauth_sys.c.
const char * escaped(const char *s)
FILE * tor_fopen_cloexec(const char *path, const char *mode)
int string_is_key_value(int severity, const char *string)
void tor_free_(void *mem)
#define MAP_DEL_CURRENT(keyvar)
#define DIGESTMAP_FOREACH_END
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar)
int compare_digest_to_vote_routerstatus_entry(const void *_key, const void **_member)
Header file for networkstatus.c.
Header file for ns_parse.c.
Master header file for Tor-specific functionality.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Router descriptor structure.
uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router)
Header file for routerlist.c.
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
void * smartlist_bsearch(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member))
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
INT MinMeasuredBWsForAuthToIgnoreAdvertised
char identity_digest[DIGEST_LEN]
unsigned int has_measured_bw
int strcmpstart(const char *s1, const char *s2)
Routerstatus (vote entry) structure.