cosmetics
[strongswan.git] / src / libstrongswan / selectors / traffic_selector.c
index aa762e9..87e3464 100644 (file)
@@ -105,31 +105,22 @@ struct private_traffic_selector_t {
  */
 static void calc_range(private_traffic_selector_t *this, u_int8_t netbits)
 {
-       int byte;
-       size_t size = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
+       size_t len;
+       int bytes, bits;
+       u_int8_t mask;
 
        this->netbits = netbits;
-       
-       /* go through the from address, starting at the tail. While we
-        * have not processed the bits belonging to the host, set them to 1 on
-        * the to address. If we reach the bits for the net, copy them from "from". */
-       for (byte = size - 1; byte >= 0; byte--)
-       {
-               u_char mask = 0x00;
-               int shift;
 
-               shift = (byte+1) * 8 - netbits;
-               if (shift > 0)
-               {
-                       mask = 1 << shift;
-                       if (mask != 0xFF)
-                       {
-                               mask--;
-                       }
-               }
-               this->from[byte] &= ~mask;
-               this->to[byte] = this->from[byte] | mask;
-       }
+       len   = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
+       bytes = (netbits + 7)/8;
+       bits  = (bytes * 8) - netbits;
+       mask  = bits ? (1 << bits) - 1 : 0;
+
+       memcpy(this->to, this->from, bytes);
+       memset(this->from + bytes, 0x00, len - bytes);
+       memset(this->to   + bytes, 0xff, len - bytes);
+       this->from[bytes-1] &= ~mask;
+       this->to[bytes-1]   |=  mask;
 }
 
 /**
@@ -652,8 +643,9 @@ static traffic_selector_t *clone_(private_traffic_selector_t *this)
 
        clone = traffic_selector_create(this->protocol, this->type,
                                                                        this->from_port, this->to_port);
-
+       clone->netbits = this->netbits;
        clone->dynamic = this->dynamic;
+
        switch (clone->type)
        {
                case TS_IPV4_ADDR_RANGE:
@@ -747,8 +739,10 @@ traffic_selector_t *traffic_selector_create_from_rfc3779_format(ts_type_t type,
        }
        if (to.len > 1)
        {
+               u_int8_t mask = to.ptr[0] ? (1 << to.ptr[0]) - 1 : 0;
+
                memcpy(this->to, to.ptr+1, to.len-1);
-               this->to[to.len-2] |= to.ptr[0] ? (1 << to.ptr[0]) - 1 : 0;
+               this->to[to.len-2] |= mask;
        }
        this->netbits = chunk_equals(from, to) ? (from.len-1)*8 - from.ptr[0]
                                                                                   : NON_SUBNET_ADDRESS_RANGE;
@@ -771,7 +765,7 @@ traffic_selector_t *traffic_selector_create_from_subnet(host_t *net,
 
                        this->type = TS_IPV4_ADDR_RANGE;
                        from = net->get_address(net);
-                       memcpy(this->from4, from.ptr, from.len);
+                       memcpy(this->from, from.ptr, from.len);
                        if (this->from4[0] == 0)
                        {
                                /* use /0 for 0.0.0.0 */
@@ -790,7 +784,7 @@ traffic_selector_t *traffic_selector_create_from_subnet(host_t *net,
 
                        this->type = TS_IPV6_ADDR_RANGE;
                        from = net->get_address(net);
-                       memcpy(this->from6, from.ptr, from.len);
+                       memcpy(this->from, from.ptr, from.len);
                        if (this->from6[0] == 0 && this->from6[1] == 0 &&
                                this->from6[2] == 0 && this->from6[3] == 0)
                        {
@@ -876,7 +870,7 @@ traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
 
        memset(this->from6, 0, sizeof(this->from6));
        memset(this->to6, 0xFF, sizeof(this->to6));
-
+       this->netbits = 0;
        this->dynamic = TRUE;
 
        return &this->public;