diff options
author | Nick Mathewson <nickm@torproject.org> | 2009-09-14 23:39:08 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2009-09-14 23:39:08 -0400 |
commit | 56c6d78520a98fb643e67b80b9192a2875f95e29 (patch) | |
tree | b4cfcdd84c2f41eab6ca30885d9612c3cfeecf28 /src | |
parent | d9872cc67632d6c1f81e0a81fc50a62198f1b35d (diff) | |
download | tor-56c6d78520a98fb643e67b80b9192a2875f95e29.tar.gz tor-56c6d78520a98fb643e67b80b9192a2875f95e29.zip |
Parameter access function, with unit tests.
Diffstat (limited to 'src')
-rw-r--r-- | src/or/networkstatus.c | 26 | ||||
-rw-r--r-- | src/or/or.h | 2 | ||||
-rw-r--r-- | src/or/test.c | 3 |
3 files changed, 31 insertions, 0 deletions
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c index 70d43e6503..0ed9279002 100644 --- a/src/or/networkstatus.c +++ b/src/or/networkstatus.c @@ -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 diff --git a/src/or/or.h b/src/or/or.h index 297a8f0bbb..dd940273b2 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -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); diff --git a/src/or/test.c b/src/or/test.c index 338195fd25..f2cc7cc1f3 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -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); |