Slightly refactor traffic_selector_t.get_subset()
[strongswan.git] / src / libstrongswan / selectors / traffic_selector.c
index 38d4b2d..964e5a5 100644 (file)
@@ -22,9 +22,9 @@
 
 #include "traffic_selector.h"
 
-#include <utils/linked_list.h>
+#include <collections/linked_list.h>
 #include <utils/identification.h>
-#include <debug.h>
+#include <utils/debug.h>
 
 #define NON_SUBNET_ADDRESS_RANGE       255
 
@@ -174,7 +174,8 @@ static u_int8_t calc_netbits(private_traffic_selector_t *this)
 /**
  * internal generic constructor
  */
-static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts_type_t type, u_int16_t from_port, u_int16_t to_port);
+static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol,
+                                               ts_type_t type, u_int16_t from_port, u_int16_t to_port);
 
 /**
  * Described in header.
@@ -283,8 +284,9 @@ int traffic_selector_printf_hook(printf_hook_data_t *data,
        {
                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)
                        {
                                written += print_in_hook(data, "%s", serv->s_name);
@@ -296,7 +298,8 @@ int traffic_selector_printf_hook(printf_hook_data_t *data,
                }
                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);
                }
        }
 
@@ -305,82 +308,92 @@ int traffic_selector_printf_hook(printf_hook_data_t *data,
        return written;
 }
 
-/**
- * Implements traffic_selector_t.get_subset
- */
-static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_traffic_selector_t *other)
+METHOD(traffic_selector_t, get_subset, traffic_selector_t*,
+       private_traffic_selector_t *this, traffic_selector_t *other_public)
 {
-       if (this->type == other->type && (this->protocol == other->protocol ||
-                                                               this->protocol == 0 || other->protocol == 0))
-       {
-               u_int16_t from_port, to_port;
-               u_char *from, *to;
-               u_int8_t protocol;
-               size_t size;
-               private_traffic_selector_t *new_ts;
-
-               /* 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;
-               }
-               /* select protocol, which is not zero */
-               protocol = max(this->protocol, other->protocol);
+       private_traffic_selector_t *other, *subset;
+       u_int16_t from_port, to_port;
+       u_char *from, *to;
+       u_int8_t protocol;
+       size_t size;
 
-               switch (this->type)
-               {
-                       case TS_IPV4_ADDR_RANGE:
-                               size = sizeof(this->from4);
-                               break;
-                       case TS_IPV6_ADDR_RANGE:
-                               size = sizeof(this->from6);
-                               break;
-                       default:
-                               return NULL;
-               }
+       other = (private_traffic_selector_t*)other_public;
 
-               /* get higher from-address */
-               if (memcmp(this->from, other->from, size) > 0)
-               {
-                       from = this->from;
-               }
-               else
-               {
-                       from = other->from;
-               }
-               /* get lower to-address */
-               if (memcmp(this->to, other->to, size) > 0)
-               {
-                       to = other->to;
-               }
-               else
-               {
-                       to = this->to;
-               }
-               /* if "from" > "to", we don't have a match */
-               if (memcmp(from, to, size) > 0)
-               {
+       if (this->dynamic || other->dynamic)
+       {       /* no set_address() applied, TS has no subset */
+               return NULL;
+       }
+
+       if (this->type != other->type)
+       {
+               return NULL;
+       }
+       switch (this->type)
+       {
+               case TS_IPV4_ADDR_RANGE:
+                       size = sizeof(this->from4);
+                       break;
+               case TS_IPV6_ADDR_RANGE:
+                       size = sizeof(this->from6);
+                       break;
+               default:
                        return NULL;
-               }
+       }
 
-               /* we have a match in protocol, port, and address: return it... */
-               new_ts = traffic_selector_create(protocol, this->type, from_port, to_port);
-               new_ts->dynamic = this->dynamic || other->dynamic;
-               memcpy(new_ts->from, from, size);
-               memcpy(new_ts->to, to, size);
-               calc_netbits(new_ts);
-               return &new_ts->public;
+       if (this->protocol != other->protocol &&
+               this->protocol != 0 && other->protocol != 0)
+       {
+               return NULL;
        }
-       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 */
+       if (memcmp(this->from, other->from, size) > 0)
+       {
+               from = this->from;
+       }
+       else
+       {
+               from = other->from;
+       }
+       /* get lower to-address */
+       if (memcmp(this->to, other->to, size) > 0)
+       {
+               to = other->to;
+       }
+       else
+       {
+               to = this->to;
+       }
+       /* if "from" > "to", we don't have a match */
+       if (memcmp(from, to, size) > 0)
+       {
+               return NULL;
+       }
+
+       /* we have a match in protocol, port, and address: return it... */
+       subset = traffic_selector_create(protocol, this->type, from_port, to_port);
+       memcpy(subset->from, from, size);
+       memcpy(subset->to, to, size);
+       calc_netbits(subset);
+
+       return &subset->public;
 }
 
-/**
- * Implements traffic_selector_t.equals
- */
-static bool equals(private_traffic_selector_t *this, private_traffic_selector_t *other)
+METHOD(traffic_selector_t, equals, bool,
+       private_traffic_selector_t *this, traffic_selector_t *other_public)
 {
+       private_traffic_selector_t *other;
+
+       other = (private_traffic_selector_t*)other_public;
        if (this->type != other->type)
        {
                return FALSE;
@@ -510,7 +523,7 @@ METHOD(traffic_selector_t, is_dynamic, bool,
 METHOD(traffic_selector_t, set_address, void,
        private_traffic_selector_t *this, host_t *host)
 {
-       if (this->dynamic)
+       if (is_host(this, NULL))
        {
                this->type = host->get_family(host) == AF_INET ?
                                TS_IPV4_ADDR_RANGE : TS_IPV6_ADDR_RANGE;
@@ -528,14 +541,12 @@ METHOD(traffic_selector_t, set_address, void,
                        memcpy(this->to, from.ptr, from.len);
                        this->netbits = from.len * 8;
                }
+               this->dynamic = FALSE;
        }
 }
 
-/**
- * Implements traffic_selector_t.is_contained_in.
- */
-static bool is_contained_in(private_traffic_selector_t *this,
-                                                       private_traffic_selector_t *other)
+METHOD(traffic_selector_t, is_contained_in, bool,
+       private_traffic_selector_t *this, traffic_selector_t *other)
 {
        private_traffic_selector_t *subset;
        bool contained_in = FALSE;
@@ -544,7 +555,7 @@ static bool is_contained_in(private_traffic_selector_t *this,
 
        if (subset)
        {
-               if (equals(subset, this))
+               if (equals(subset, &this->public))
                {
                        contained_in = TRUE;
                }
@@ -814,6 +825,23 @@ traffic_selector_t *traffic_selector_create_from_string(
 /*
  * see header
  */
+traffic_selector_t *traffic_selector_create_from_cidr(char *string,
+                                                                       u_int8_t protocol, u_int16_t port)
+{
+       host_t *net;
+       int bits;
+
+       net = host_create_from_subnet(string, &bits);
+       if (net)
+       {
+               return traffic_selector_create_from_subnet(net, bits, protocol, port);
+       }
+       return NULL;
+}
+
+/*
+ * see header
+ */
 traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
                                                                        u_int16_t from_port, u_int16_t to_port)
 {
@@ -838,8 +866,8 @@ static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol,
 
        INIT(this,
                .public = {
-                       .get_subset = (traffic_selector_t*(*)(traffic_selector_t*,traffic_selector_t*))get_subset,
-                       .equals = (bool(*)(traffic_selector_t*,traffic_selector_t*))equals,
+                       .get_subset = _get_subset,
+                       .equals = _equals,
                        .get_from_address = _get_from_address,
                        .get_to_address = _get_to_address,
                        .get_from_port = _get_from_port,
@@ -848,7 +876,7 @@ static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol,
                        .get_protocol = _get_protocol,
                        .is_host = _is_host,
                        .is_dynamic = _is_dynamic,
-                       .is_contained_in = (bool(*)(traffic_selector_t*,traffic_selector_t*))is_contained_in,
+                       .is_contained_in = _is_contained_in,
                        .includes = _includes,
                        .set_address = _set_address,
                        .to_subnet = _to_subnet,
@@ -863,4 +891,3 @@ static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol,
 
        return this;
 }
-