Parameter access function, with unit tests.

This commit is contained in:
Nick Mathewson 2009-09-14 23:39:08 -04:00
parent d9872cc676
commit 56c6d78520
3 changed files with 31 additions and 0 deletions

View file

@ -1893,6 +1893,32 @@ networkstatus_dump_bridge_status_to_file(time_t now)
tor_free(status);
}
/** Return the value of a integer parameter from the networkstatus <b>ns</b>
* whose name is <b>param_name</b>. Return <b>default_val</b> if ns is NULL,
* or if it has no parameter called <b>param_name</b>. */
int32_t
networkstatus_get_param(networkstatus_t *ns, const char *param_name,
int32_t default_val)
{
size_t name_len;
if (!ns || !ns->net_params)
return default_val;
name_len = strlen(param_name);
SMARTLIST_FOREACH_BEGIN(ns->net_params, const char *, p) {
if (!strcmpstart(p, param_name) && p[name_len] == '=') {
int ok=0;
long v = tor_parse_long(p+name_len+1, 10, INT32_MIN, INT32_MAX, &ok,NULL);
if (ok)
return (int32_t) v;
}
} SMARTLIST_FOREACH_END(p);
return default_val;
}
/** If <b>question</b> is a string beginning with "ns/" in a format the
* control interface expects for a GETINFO question, set *<b>answer</b> to a
* newly-allocated string containing networkstatus lines for the appropriate

View file

@ -3960,6 +3960,8 @@ void signed_descs_update_status_from_consensus_networkstatus(
char *networkstatus_getinfo_helper_single(routerstatus_t *rs);
char *networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now);
void networkstatus_dump_bridge_status_to_file(time_t now);
int32_t networkstatus_get_param(networkstatus_t *ns, const char *param_name,
int32_t default_val);
int getinfo_helper_networkstatus(control_connection_t *conn,
const char *question, char **answer);
void networkstatus_free_all(void);

View file

@ -3381,6 +3381,9 @@ test_dirutil_param_voting(void)
"abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
smartlist_split_string(vote4.net_params,
"ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
test_eq(100, networkstatus_get_param(&vote4, "x-yz", 50));
test_eq(222, networkstatus_get_param(&vote4, "foobar", 222));
smartlist_add(votes, &vote1);
smartlist_add(votes, &vote2);
smartlist_add(votes, &vote3);