[or-cvs] [tor/master] Parameter access function, with unit tests.
Nick Mathewson
nickm at seul.org
Tue Sep 15 03:40:45 UTC 2009
Author: Nick Mathewson <nickm at torproject.org>
Date: Mon, 14 Sep 2009 23:39:08 -0400
Subject: Parameter access function, with unit tests.
Commit: 56c6d78520a98fb643e67b80b9192a2875f95e29
---
src/or/networkstatus.c | 26 ++++++++++++++++++++++++++
src/or/or.h | 2 ++
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 70d43e6..0ed9279 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 297a8f0..dd94027 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 338195f..f2cc7cc 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);
--
1.5.6.5
More information about the tor-commits
mailing list