aboutsummaryrefslogtreecommitdiff
path: root/src/or/networkstatus.c
diff options
context:
space:
mode:
authorRoger Dingledine <arma@torproject.org>2011-01-15 22:39:15 -0500
committerRoger Dingledine <arma@torproject.org>2011-01-15 22:39:15 -0500
commit4ff97e3775316ceb500cb5d2a562ec5dfd8dfb23 (patch)
tree77b0407257fb8cd4cb94719df396969095d69e88 /src/or/networkstatus.c
parent8e9b25e6c7a2e70c370881ab5748651b1c7de66f (diff)
parent7699014e1e9e183d1fd840e59909bcf77ba3321b (diff)
downloadtor-4ff97e3775316ceb500cb5d2a562ec5dfd8dfb23.tar.gz
tor-4ff97e3775316ceb500cb5d2a562ec5dfd8dfb23.zip
Merge branch 'maint-0.2.2'
Diffstat (limited to 'src/or/networkstatus.c')
-rw-r--r--src/or/networkstatus.c54
1 files changed, 43 insertions, 11 deletions
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index 68dc9f6712..cccb1c0b6e 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -2154,32 +2154,52 @@ networkstatus_dump_bridge_status_to_file(time_t now)
tor_free(status);
}
-int32_t
+static int32_t
get_net_param_from_list(smartlist_t *net_params, const char *param_name,
- int default_val)
+ int32_t default_val, int32_t min_val, int32_t max_val)
{
+ int32_t res = default_val;
size_t name_len = strlen(param_name);
+ tor_assert(max_val > min_val);
+ tor_assert(min_val <= default_val);
+ tor_assert(max_val >= default_val);
+
SMARTLIST_FOREACH_BEGIN(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;
+ if (ok) {
+ res = (int32_t) v;
+ break;
+ }
}
} SMARTLIST_FOREACH_END(p);
- return default_val;
+ if (res < min_val) {
+ log_warn(LD_DIR, "Consensus parameter %s is too small. Got %d, raising to "
+ "%d.", param_name, res, min_val);
+ res = min_val;
+ } else if (res > max_val) {
+ log_warn(LD_DIR, "Consensus parameter %s is too large. Got %d, capping to "
+ "%d.", param_name, res, max_val);
+ res = max_val;
+ }
+
+ return res;
}
/** Return the value of a integer parameter from the networkstatus <b>ns</b>
* whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
* latest consensus ourselves. Return <b>default_val</b> if no latest
- * consensus, or if it has no parameter called <b>param_name</b>. */
+ * consensus, or if it has no parameter called <b>param_name</b>.
+ * Make sure the value parsed from the consensus is at least
+ * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
+ * if necessary. */
int32_t
networkstatus_get_param(networkstatus_t *ns, const char *param_name,
- int32_t default_val)
+ int32_t default_val, int32_t min_val, int32_t max_val)
{
if (!ns) /* if they pass in null, go find it ourselves */
ns = networkstatus_get_latest_consensus();
@@ -2187,24 +2207,36 @@ networkstatus_get_param(networkstatus_t *ns, const char *param_name,
if (!ns || !ns->net_params)
return default_val;
- return get_net_param_from_list(ns->net_params, param_name, default_val);
+ return get_net_param_from_list(ns->net_params, param_name,
+ default_val, min_val, max_val);
}
/** Return the value of a integer bw weight parameter from the networkstatus
* <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
* loading the latest consensus ourselves. Return <b>default_val</b> if no
- * latest consensus, or if it has no parameter called <b>param_name</b>. */
+ * latest consensus, or if it has no parameter called <b>weight_name</b>. */
int32_t
networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight_name,
- int32_t default_val)
+ int32_t default_val)
{
+ int32_t param;
+ int max;
if (!ns) /* if they pass in null, go find it ourselves */
ns = networkstatus_get_latest_consensus();
if (!ns || !ns->weight_params)
return default_val;
- return get_net_param_from_list(ns->weight_params, weight_name, default_val);
+ max = circuit_build_times_get_bw_scale(ns);
+ param = get_net_param_from_list(ns->weight_params, weight_name,
+ default_val, -1,
+ BW_MAX_WEIGHT_SCALE);
+ if (param > max) {
+ log_warn(LD_DIR, "Value of consensus weight %s was too large, capping "
+ "to %d", weight_name, max);
+ param = max;
+ }
+ return param;
}
/** Return the name of the consensus flavor <b>flav</b> as used to identify