diff options
author | Nick Mathewson <nickm@torproject.org> | 2012-10-23 16:28:34 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2012-10-23 16:28:34 -0400 |
commit | 98c24670e7e87b61d57baad142b090df4a3dbbcb (patch) | |
tree | 5664cf3bdab8e1809f8416c0267fd3e24f69cb10 /src/or | |
parent | 848333c6d6d65775cb787755577c4b24ae512389 (diff) | |
parent | 85659d3964669f9f419123c648e517f4ba539462 (diff) | |
download | tor-98c24670e7e87b61d57baad142b090df4a3dbbcb.tar.gz tor-98c24670e7e87b61d57baad142b090df4a3dbbcb.zip |
Merge remote-tracking branch 'origin/maint-0.2.3'
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/policies.c | 41 | ||||
-rw-r--r-- | src/or/policies.h | 1 |
2 files changed, 37 insertions, 5 deletions
diff --git a/src/or/policies.c b/src/or/policies.c index 568bc88a05..dee0380173 100644 --- a/src/or/policies.c +++ b/src/or/policies.c @@ -1347,8 +1347,10 @@ parse_short_policy(const char *summary) unsigned low, high; char dummy; char ent_buf[32]; + size_t len; next = comma ? comma+1 : strchr(summary, '\0'); + len = comma ? (size_t)(comma - summary) : strlen(summary); if (n_entries == MAX_EXITPOLICY_SUMMARY_LEN) { log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Impossibly long policy summary %s", @@ -1356,20 +1358,22 @@ parse_short_policy(const char *summary) return NULL; } - if (! TOR_ISDIGIT(*summary) || next-summary > (int)(sizeof(ent_buf)-1)) { + if (! TOR_ISDIGIT(*summary) || len > (sizeof(ent_buf)-1)) { /* unrecognized entry format. skip it. */ continue; } - if (next-summary < 2) { + if (len < 1) { /* empty; skip it. */ + /* XXX This happens to be unreachable, since if len==0, then *summary is + * ',' or '\0', and the TOR_ISDIGIT test above would have failed. */ continue; } - memcpy(ent_buf, summary, next-summary-1); - ent_buf[next-summary-1] = '\0'; + memcpy(ent_buf, summary, len); + ent_buf[len] = '\0'; if (tor_sscanf(ent_buf, "%u-%u%c", &low, &high, &dummy) == 2) { - if (low<1 || low>65535 || high<1 || high>65535) { + if (low<1 || low>65535 || high<1 || high>65535 || low>high) { log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Found bad entry in policy summary %s", escaped(orig_summary)); return NULL; @@ -1412,6 +1416,33 @@ parse_short_policy(const char *summary) return result; } +/** Write <b>policy</b> back out into a string. Used only for unit tests + * currently. */ +char * +write_short_policy(const short_policy_t *policy) +{ + int i; + char *answer; + smartlist_t *sl = smartlist_new(); + + smartlist_add_asprintf(sl, "%s", policy->is_accept ? "accept " : "reject "); + + for (i=0; i < policy->n_entries; i++) { + const short_policy_entry_t *e = &policy->entries[i]; + if (e->min_port == e->max_port) { + smartlist_add_asprintf(sl, "%d", e->min_port); + } else { + smartlist_add_asprintf(sl, "%d-%d", e->min_port, e->max_port); + } + if (i < policy->n_entries-1) + smartlist_add(sl, tor_strdup(",")); + } + answer = smartlist_join_strings(sl, "", 0, NULL); + SMARTLIST_FOREACH(sl, char *, a, tor_free(a)); + smartlist_free(sl); + return answer; +} + /** Release all storage held in <b>policy</b>. */ void short_policy_free(short_policy_t *policy) diff --git a/src/or/policies.h b/src/or/policies.h index 93fd572e2c..431e69eb0d 100644 --- a/src/or/policies.h +++ b/src/or/policies.h @@ -61,6 +61,7 @@ void policies_free_all(void); char *policy_summarize(smartlist_t *policy); short_policy_t *parse_short_policy(const char *summary); +char *write_short_policy(const short_policy_t *policy); void short_policy_free(short_policy_t *policy); int short_policy_is_reject_star(const short_policy_t *policy); addr_policy_result_t compare_tor_addr_to_short_policy( |