Make use of new CIDR string ts constructor where appropriate

This commit is contained in:
Martin Willi
2012-10-24 13:25:08 +02:00
parent fd6c0c8fb4
commit 1efd6c6f2a
4 changed files with 23 additions and 74 deletions
+4 -8
View File
@@ -143,9 +143,7 @@ static child_cfg_t *load_child_config(private_config_t *this,
proposal_t *proposal; proposal_t *proposal;
traffic_selector_t *ts; traffic_selector_t *ts;
ipsec_mode_t mode = MODE_TUNNEL; ipsec_mode_t mode = MODE_TUNNEL;
host_t *net;
char *token; char *token;
int bits;
u_int32_t tfc; u_int32_t tfc;
if (settings->get_bool(settings, "configs.%s.%s.transport", if (settings->get_bool(settings, "configs.%s.%s.transport",
@@ -189,10 +187,9 @@ static child_cfg_t *load_child_config(private_config_t *this,
enumerator = enumerator_create_token(token, ",", " "); enumerator = enumerator_create_token(token, ",", " ");
while (enumerator->enumerate(enumerator, &token)) while (enumerator->enumerate(enumerator, &token))
{ {
net = host_create_from_subnet(token, &bits); ts = traffic_selector_create_from_cidr(token, 0, 0);
if (net) if (ts)
{ {
ts = traffic_selector_create_from_subnet(net, bits, 0, 0);
child_cfg->add_traffic_selector(child_cfg, TRUE, ts); child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
} }
else else
@@ -214,10 +211,9 @@ static child_cfg_t *load_child_config(private_config_t *this,
enumerator = enumerator_create_token(token, ",", " "); enumerator = enumerator_create_token(token, ",", " ");
while (enumerator->enumerate(enumerator, &token)) while (enumerator->enumerate(enumerator, &token))
{ {
net = host_create_from_subnet(token, &bits); ts = traffic_selector_create_from_cidr(token, 0, 0);
if (net) if (ts)
{ {
ts = traffic_selector_create_from_subnet(net, bits, 0, 0);
child_cfg->add_traffic_selector(child_cfg, FALSE, ts); child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
} }
else else
+4 -20
View File
@@ -61,28 +61,12 @@ static traffic_selector_t *ts_from_string(char *str)
{ {
if (str) if (str)
{ {
int netbits = 32; traffic_selector_t *ts;
host_t *net;
char *pos;
str = strdupa(str); ts = traffic_selector_create_from_cidr(str, 0, 0);
pos = strchr(str, '/'); if (ts)
if (pos)
{ {
*pos++ = '\0'; return ts;
netbits = atoi(pos);
}
else
{
if (strchr(str, ':'))
{
netbits = 128;
}
}
net = host_create_from_string(str, 0);
if (net)
{
return traffic_selector_create_from_subnet(net, netbits, 0, 0);
} }
} }
return traffic_selector_create_dynamic(0, 0, 65535); return traffic_selector_create_dynamic(0, 0, 65535);
+11 -26
View File
@@ -881,10 +881,10 @@ static void add_ts(private_stroke_config_t *this,
} }
else else
{ {
host_t *net;
if (!end->subnets) if (!end->subnets)
{ {
host_t *net;
net = host_create_from_string(end->address, 0); net = host_create_from_string(end->address, 0);
if (net) if (net)
{ {
@@ -895,39 +895,24 @@ static void add_ts(private_stroke_config_t *this,
} }
else else
{ {
char *del, *start, *bits; enumerator_t *enumerator;
char *subnet;
start = end->subnets; enumerator = enumerator_create_token(end->subnets, ",", " ");
do while (enumerator->enumerate(enumerator, &subnet))
{ {
int intbits = 0; ts = traffic_selector_create_from_cidr(subnet,
end->protocol, end->port);
del = strchr(start, ','); if (ts)
if (del)
{ {
*del = '\0';
}
bits = strchr(start, '/');
if (bits)
{
*bits = '\0';
intbits = atoi(bits + 1);
}
net = host_create_from_string(start, 0);
if (net)
{
ts = traffic_selector_create_from_subnet(net, intbits,
end->protocol, end->port);
child_cfg->add_traffic_selector(child_cfg, local, ts); child_cfg->add_traffic_selector(child_cfg, local, ts);
} }
else else
{ {
DBG1(DBG_CFG, "invalid subnet: %s, skipped", start); DBG1(DBG_CFG, "invalid subnet: %s, skipped", subnet);
} }
start = del + 1;
} }
while (del); enumerator->destroy(enumerator);
} }
} }
} }
+4 -20
View File
@@ -87,28 +87,12 @@ static traffic_selector_t *create_ts(char *string)
{ {
if (string) if (string)
{ {
int netbits = 32; traffic_selector_t *ts;
host_t *net;
char *pos;
string = strdupa(string); ts = traffic_selector_create_from_cidr(string, 0, 0);
pos = strchr(string, '/'); if (ts)
if (pos)
{ {
*pos++ = '\0'; return ts;
netbits = atoi(pos);
}
else
{
if (strchr(string, ':'))
{
netbits = 128;
}
}
net = host_create_from_string(string, 0);
if (net)
{
return traffic_selector_create_from_subnet(net, netbits, 0, 0);
} }
} }
return traffic_selector_create_dynamic(0, 0, 65535); return traffic_selector_create_dynamic(0, 0, 65535);