]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
traffic-selector: Make `net` optional in to_subnet() to determine just the mask
authorTobias Brunner <tobias@strongswan.org>
Fri, 20 Oct 2023 14:40:38 +0000 (16:40 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 20 Oct 2023 14:43:24 +0000 (16:43 +0200)
src/libstrongswan/selectors/traffic_selector.c

index fe61e3768b8763c69b0bac2f620f9b43dff55977..1f5021f0e11d630417459ef9dd01afaff546ec95 100644 (file)
@@ -607,23 +607,25 @@ METHOD(traffic_selector_t, to_subnet, bool,
                        return FALSE;
        }
 
-       net_chunk.ptr = malloc(net_chunk.len);
-       memset(net_chunk.ptr, 0x00, net_chunk.len);
-       if (*mask)
-       {
-               non_zero_bytes = (*mask + 7) / 8;
-               memcpy(net_chunk.ptr, this->from, non_zero_bytes);
-               net_chunk.ptr[non_zero_bytes-1] &= 0xFF << (8 * non_zero_bytes - *mask);
-       }
-
-       if (this->to_port == this->from_port)
+       if (net)
        {
-               port = this->to_port;
-       }
+               net_chunk.ptr = malloc(net_chunk.len);
+               memset(net_chunk.ptr, 0x00, net_chunk.len);
+               if (*mask)
+               {
+                       non_zero_bytes = (*mask + 7) / 8;
+                       memcpy(net_chunk.ptr, this->from, non_zero_bytes);
+                       net_chunk.ptr[non_zero_bytes-1] &= 0xFF << (8 * non_zero_bytes - *mask);
+               }
 
-       *net = host_create_from_chunk(family, net_chunk, port);
-       chunk_free(&net_chunk);
+               if (this->to_port == this->from_port)
+               {
+                       port = this->to_port;
+               }
 
+               *net = host_create_from_chunk(family, net_chunk, port);
+               chunk_free(&net_chunk);
+       }
        return this->netbits != NON_SUBNET_ADDRESS_RANGE;
 }