Slightly refactor traffic_selector_t.get_subset()

This commit is contained in:
Martin Willi
2013-02-21 10:48:48 +01:00
parent de5d569b24
commit 7dbe1feef1
+34 -27
View File
@@ -284,8 +284,9 @@ int traffic_selector_printf_hook(printf_hook_data_t *data,
{ {
if (this->from_port == this->to_port) if (this->from_port == this->to_port)
{ {
struct servent *serv = getservbyport(htons(this->from_port), serv_proto); struct servent *serv;
serv = getservbyport(htons(this->from_port), serv_proto);
if (serv) if (serv)
{ {
written += print_in_hook(data, "%s", serv->s_name); written += print_in_hook(data, "%s", serv->s_name);
@@ -297,7 +298,8 @@ int traffic_selector_printf_hook(printf_hook_data_t *data,
} }
else else
{ {
written += print_in_hook(data, "%d-%d", this->from_port, this->to_port); written += print_in_hook(data, "%d-%d",
this->from_port, this->to_port);
} }
} }
@@ -309,32 +311,23 @@ int traffic_selector_printf_hook(printf_hook_data_t *data,
METHOD(traffic_selector_t, get_subset, traffic_selector_t*, METHOD(traffic_selector_t, get_subset, traffic_selector_t*,
private_traffic_selector_t *this, traffic_selector_t *other_public) private_traffic_selector_t *this, traffic_selector_t *other_public)
{ {
private_traffic_selector_t *other; private_traffic_selector_t *other, *subset;
other = (private_traffic_selector_t*)other_public;
if (this->dynamic || other->dynamic)
{ /* no set_address() applied, TS has no subset */
return NULL;
}
if (this->type == other->type && (this->protocol == other->protocol ||
this->protocol == 0 || other->protocol == 0))
{
u_int16_t from_port, to_port; u_int16_t from_port, to_port;
u_char *from, *to; u_char *from, *to;
u_int8_t protocol; u_int8_t protocol;
size_t size; size_t size;
private_traffic_selector_t *new_ts;
/* calculate the maximum port range allowed for both */ other = (private_traffic_selector_t*)other_public;
from_port = max(this->from_port, other->from_port);
to_port = min(this->to_port, other->to_port); if (this->dynamic || other->dynamic)
if (from_port > to_port) { /* no set_address() applied, TS has no subset */
return NULL;
}
if (this->type != other->type)
{ {
return NULL; return NULL;
} }
/* select protocol, which is not zero */
protocol = max(this->protocol, other->protocol);
switch (this->type) switch (this->type)
{ {
case TS_IPV4_ADDR_RANGE: case TS_IPV4_ADDR_RANGE:
@@ -347,6 +340,21 @@ METHOD(traffic_selector_t, get_subset, traffic_selector_t*,
return NULL; return NULL;
} }
if (this->protocol != other->protocol &&
this->protocol != 0 && other->protocol != 0)
{
return NULL;
}
/* select protocol, which is not zero */
protocol = max(this->protocol, other->protocol);
/* calculate the maximum port range allowed for both */
from_port = max(this->from_port, other->from_port);
to_port = min(this->to_port, other->to_port);
if (from_port > to_port)
{
return NULL;
}
/* get higher from-address */ /* get higher from-address */
if (memcmp(this->from, other->from, size) > 0) if (memcmp(this->from, other->from, size) > 0)
{ {
@@ -372,13 +380,12 @@ METHOD(traffic_selector_t, get_subset, traffic_selector_t*,
} }
/* we have a match in protocol, port, and address: return it... */ /* we have a match in protocol, port, and address: return it... */
new_ts = traffic_selector_create(protocol, this->type, from_port, to_port); subset = traffic_selector_create(protocol, this->type, from_port, to_port);
memcpy(new_ts->from, from, size); memcpy(subset->from, from, size);
memcpy(new_ts->to, to, size); memcpy(subset->to, to, size);
calc_netbits(new_ts); calc_netbits(subset);
return &new_ts->public;
} return &subset->public;
return NULL;
} }
METHOD(traffic_selector_t, equals, bool, METHOD(traffic_selector_t, equals, bool,