r16881@catbus: nickm | 2007-11-30 15:07:42 -0500

Do not keep a string representation of every single addr_policy_t lying around.  This might save a few hundred K.


svn:r12617
This commit is contained in:
Nick Mathewson 2007-11-30 20:09:09 +00:00
parent 6fe70f4c69
commit f061994487
4 changed files with 13 additions and 31 deletions

View File

@ -1119,7 +1119,6 @@ typedef enum {
/** A linked list of policy rules */
typedef struct addr_policy_t {
addr_policy_action_t policy_type; /**< What to do when the policy matches.*/
char *string; /**< String representation of this rule. */
/* XXXX020 make this ipv6-capable */
uint32_t addr; /**< Base address to accept or reject. */

View File

@ -486,8 +486,11 @@ exit_policy_remove_redundancies(addr_policy_t **dest)
tmp=ap;
while (tmp) {
if (tmp->next && addr_policy_covers(ap, tmp->next)) {
char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
policy_write_item(p1, sizeof(p1), tmp->next);
policy_write_item(p2, sizeof(p2), ap);
log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s. It is made "
"redundant by %s.", tmp->next->string, ap->string);
"redundant by %s.", p1, p2);
victim = tmp->next;
tmp->next = victim->next;
victim->next = NULL;
@ -516,8 +519,11 @@ exit_policy_remove_redundancies(addr_policy_t **dest)
}
} else { /* policy_types are equal. */
if (addr_policy_covers(tmp, ap)) {
char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
policy_write_item(p1, sizeof(p1), ap);
policy_write_item(p2, sizeof(p2), tmp);
log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s. It is already "
"covered by %s.", ap->string, tmp->string);
"covered by %s.", ap, tmp);
victim = ap;
ap = ap->next;
@ -694,7 +700,6 @@ addr_policy_free(addr_policy_t *p)
while (p) {
e = p;
p = p->next;
tor_free(e->string);
tor_free(e);
}
}

View File

@ -2480,7 +2480,6 @@ router_parse_addr_policy(directory_token_t *tok)
{
addr_policy_t *newe;
char *arg;
char buf[POLICY_BUF_LEN];
tor_assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
@ -2500,10 +2499,6 @@ router_parse_addr_policy(directory_token_t *tok)
&newe->prt_min, &newe->prt_max))
goto policy_read_failed;
if (policy_write_item(buf, sizeof(buf), newe) < 0)
goto policy_read_failed;
newe->string = tor_strdup(buf);
return newe;
policy_read_failed:
@ -2540,16 +2535,13 @@ router_parse_addr_policy_private(directory_token_t *tok)
nextp = &result;
for (net = 0; private_nets[net]; ++net) {
size_t len;
char buf[POLICY_BUF_LEN];
*nextp = tor_malloc_zero(sizeof(addr_policy_t));
(*nextp)->policy_type = (tok->tp == K_REJECT) ? ADDR_POLICY_REJECT
: ADDR_POLICY_ACCEPT;
len = strlen(arg)+strlen(private_nets[net])+16;
(*nextp)->string = tor_malloc(len+1);
tor_snprintf((*nextp)->string, len, "%s %s%s",
tok->tp == K_REJECT ? "reject" : "accept",
tor_snprintf(buf, sizeof(buf), "%s%s",
private_nets[net], arg);
if (parse_addr_and_port_range((*nextp)->string + 7,
if (parse_addr_and_port_range(buf,
&(*nextp)->addr, &(*nextp)->maskbits,
&(*nextp)->prt_min, &(*nextp)->prt_max)) {
log_warn(LD_BUG, "Couldn't parse an address range we generated!");
@ -2565,22 +2557,10 @@ router_parse_addr_policy_private(directory_token_t *tok)
void
assert_addr_policy_ok(addr_policy_t *t)
{
addr_policy_t *t2;
while (t) {
tor_assert(t->policy_type == ADDR_POLICY_REJECT ||
t->policy_type == ADDR_POLICY_ACCEPT);
tor_assert(t->prt_min <= t->prt_max);
t2 = router_parse_addr_policy_from_string(t->string, -1);
tor_assert(t2);
tor_assert(t2->policy_type == t->policy_type);
tor_assert(t2->addr == t->addr);
tor_assert(t2->maskbits == t->maskbits);
tor_assert(t2->prt_min == t->prt_min);
tor_assert(t2->prt_max == t->prt_max);
tor_assert(!strcmp(t2->string, t->string));
tor_assert(t2->next == NULL);
addr_policy_free(t2);
t = t->next;
}

View File

@ -2191,7 +2191,6 @@ test_dir_format(void)
r1.platform = tor_strdup(platform);
ex1.policy_type = ADDR_POLICY_ACCEPT;
ex1.string = NULL;
ex1.addr = 0;
ex1.maskbits = 0;
ex1.prt_min = ex1.prt_max = 80;
@ -2925,7 +2924,6 @@ test_policies(void)
test_eq(16, policy->maskbits);
test_eq(1, policy->prt_min);
test_eq(65535, policy->prt_max);
test_streq("reject 192.168.0.0/16:*", policy->string);
test_assert(ADDR_POLICY_ACCEPTED ==
compare_addr_to_addr_policy(0x01020304u, 2, policy));
@ -2957,8 +2955,8 @@ test_policies(void)
line.next = NULL;
test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL));
test_assert(policy);
test_streq(policy->string, "accept *:80");
test_streq(policy->next->string, "reject *:*");
//test_streq(policy->string, "accept *:80");
//test_streq(policy->next->string, "reject *:*");
test_eq_ptr(policy->next->next, NULL);
addr_policy_free(policy);