From 5c00bee1b1cde4bb77a2e3fa1f5110850ddede6e Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Wed, 21 Oct 2020 14:43:29 +0300 Subject: [PATCH] Introduce v3 stat formatting functions. They will be merged with the v2 ones in later commits. --- src/feature/stats/rephist.c | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/feature/stats/rephist.c b/src/feature/stats/rephist.c index 7f5ec93597..24712707ca 100644 --- a/src/feature/stats/rephist.c +++ b/src/feature/stats/rephist.c @@ -2048,6 +2048,82 @@ rep_hist_hs_v2_stats_write(time_t now) return start_of_hs_v2_stats_interval + WRITE_STATS_INTERVAL; } +/** Allocate and return a string containing hidden service stats that + * are meant to be placed in the extra-info descriptor. */ +STATIC char * +rep_hist_format_hs_v3_stats(time_t now) +{ + char t[ISO_TIME_LEN+1]; + char *hs_v3_stats_string; + int64_t obfuscated_onions_seen, obfuscated_cells_seen; + + uint64_t rounded_cells_seen + = round_uint64_to_next_multiple_of(hs_v3_stats->rp_v3_relay_cells_seen, + REND_CELLS_BIN_SIZE); + rounded_cells_seen = MIN(rounded_cells_seen, INT64_MAX); + obfuscated_cells_seen = add_laplace_noise((int64_t)rounded_cells_seen, + crypto_rand_double(), + REND_CELLS_DELTA_F, REND_CELLS_EPSILON); + + uint64_t rounded_onions_seen = + round_uint64_to_next_multiple_of((size_t)digestmap_size( + hs_v3_stats->v3_onions_seen_this_period), + ONIONS_SEEN_BIN_SIZE); + rounded_onions_seen = MIN(rounded_onions_seen, INT64_MAX); + obfuscated_onions_seen = add_laplace_noise((int64_t)rounded_onions_seen, + crypto_rand_double(), ONIONS_SEEN_DELTA_F, + ONIONS_SEEN_EPSILON); + + format_iso_time(t, now); + tor_asprintf(&hs_v3_stats_string, "hidserv-v3-stats-end %s (%d s)\n" + "hidserv-rend-v3-relayed-cells %"PRId64" delta_f=%d " + "epsilon=%.2f bin_size=%d\n" + "hidserv-dir-v3-onions-seen %"PRId64" delta_f=%d " + "epsilon=%.2f bin_size=%d\n", + t, (unsigned) (now - start_of_hs_v3_stats_interval), + (obfuscated_cells_seen), REND_CELLS_DELTA_F, + REND_CELLS_EPSILON, REND_CELLS_BIN_SIZE, + (obfuscated_onions_seen), ONIONS_SEEN_DELTA_F, + ONIONS_SEEN_EPSILON, ONIONS_SEEN_BIN_SIZE); + + return hs_v3_stats_string; +} + +/** If 24 hours have passed since the beginning of the current HS + * stats period, write buffer stats to $DATADIR/stats/hidserv-v3-stats + * (possibly overwriting an existing file) and reset counters. Return + * when we would next want to write buffer stats or 0 if we never want to + * write. */ +time_t +rep_hist_hs_v3_stats_write(time_t now) +{ + char *str = NULL; + + if (!start_of_hs_v3_stats_interval) { + return 0; /* Not initialized. */ + } + + if (start_of_hs_v3_stats_interval + WRITE_STATS_INTERVAL > now) { + goto done; /* Not ready to write */ + } + + /* Generate history string. */ + str = rep_hist_format_hs_v3_stats(now); + + /* Reset HS history. */ + rep_hist_reset_hs_v3_stats(now); + + /* Try to write to disk. */ + if (!check_or_create_data_subdir("stats")) { + write_to_data_subdir("stats", "hidserv-v3-stats", str, + "hidden service stats"); + } + + done: + tor_free(str); + return start_of_hs_v3_stats_interval + WRITE_STATS_INTERVAL; +} + static uint64_t link_proto_count[MAX_LINK_PROTO+1][2]; /** Note that we negotiated link protocol version link_proto, on