*/
static config_var_t option_vars_[] = {
V(AccountingMax, MEMUNIT, "0 bytes"),
- V(AccountingRule, STRING, "max"),
+ VAR("AccountingRule", STRING, AccountingRule_option, "max"),
V(AccountingStart, STRING, NULL),
V(Address, STRING, NULL),
V(AllowDotExit, BOOL, "0"),
"risky: they will all turn off at the same time, which may "
"alert observers that they are being run by the same party.");
}
- if (options->AccountingRule &&
- strcmp(options->AccountingRule, "sum") != 0 &&
- strcmp(options->AccountingRule, "max") != 0)
+ }
+
+ options->AccountingRule = ACCT_MAX;
+ if (options->AccountingRule_option) {
+ if (!strcmp(options->AccountingRule_option, "sum"))
+ options->AccountingRule = ACCT_SUM;
+ else if (!strcmp(options->AccountingRule_option, "max"))
+ options->AccountingRule = ACCT_MAX;
+ else
REJECT("AccountingRule must be 'sum' or 'max'");
}
static uint64_t
get_accounting_bytes(void)
{
- if (strcmp(get_options()->AccountingRule, "sum") == 0)
+ if (get_options()->AccountingRule == ACCT_SUM)
return n_bytes_read_in_interval+n_bytes_written_in_interval;
- return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
+ else
+ return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
}
/** Set expected_bandwidth_usage based on how much we sent/received
/* max_configured is the larger of bytes read and bytes written
* If we are accounting based on sum, worst case is both are
* at max, doubling the expected sum of bandwidth */
- if (strcmp(get_options()->AccountingRule, "sum") == 0)
+ if (get_options()->AccountingRule == ACCT_SUM)
max_configured *= 2;
#define MIN_TIME_FOR_MEASUREMENT (1800)
U64_PRINTF_ARG(n_bytes_written_in_interval));
} else if (!strcmp(question, "accounting/bytes-left")) {
uint64_t limit = get_options()->AccountingMax;
- if (strcmp(get_options()->AccountingRule, "sum") == 0) {
+ if (get_options()->AccountingRule == ACCT_SUM) {
uint64_t total_left = 0;
uint64_t total_bytes = get_accounting_bytes();
if (total_bytes < limit)
uint64_t AccountingMax; /**< How many bytes do we allow per accounting
* interval before hibernation? 0 for "never
* hibernate." */
- char *AccountingRule; /**< How do we determine when our AccountingMax
- * has been reached?
- * "max" for when in or out reaches AccountingMax
- * "sum for when in plus out reaches AccountingMax */
+ /** How do we determine when our AccountingMax has been reached?
+ * "max" for when in or out reaches AccountingMax
+ * "sum for when in plus out reaches AccountingMax */
+ char *AccountingRule_option;
+ enum { ACCT_MAX, ACCT_SUM } AccountingRule;
/** Base64-encoded hash of accepted passwords for the control system. */
config_line_t *HashedControlPassword;
interval_length);
acc_bytes = options->AccountingMax;
- if (strcmp(options->AccountingRule, "sum") == 0)
+ if (get_options()->AccountingRule == ACCT_SUM)
acc_bytes /= 2;
if (effective_bw >=
acc_bytes / interval_length) {
or_state_t *state = get_or_state();
char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
- const char *acc_rule = options->AccountingRule;
uint64_t acc_bytes = options->AccountingMax;
char *acc_max;
time_t interval_end = accounting_get_end_time();
char end_buf[ISO_TIME_LEN + 1];
char *remaining = NULL;
- if (strcmp(acc_rule, "sum") == 0)
+ if (options->AccountingRule == ACCT_SUM)
acc_bytes *= 2;
acc_max = bytes_to_usage(acc_bytes);
format_local_iso_time(end_buf, interval_end);
or_state = or_state_new();
options->AccountingMax = 100;
- options->AccountingRule = tor_strdup("max");
+ options->AccountingRule = ACCT_MAX;
tor_assert(accounting_is_enabled(options));
configure_accounting(fake_time);
tor_assert(we_are_hibernating() == 1);
options->AccountingMax = 200;
- options->AccountingRule = tor_strdup("sum");
+ options->AccountingRule = ACCT_SUM;
accounting_add_bytes(0, 10, 1);
fake_time += 1;