]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
packet: Add helper function to create a clone without data
authorTobias Brunner <tobias@strongswan.org>
Thu, 2 Dec 2021 15:10:46 +0000 (16:10 +0100)
committerTobias Brunner <tobias@strongswan.org>
Fri, 14 Jan 2022 09:13:21 +0000 (10:13 +0100)
src/libstrongswan/networking/packet.c
src/libstrongswan/networking/packet.h

index a54ec79a142f90398abd55f842b86c138bb40e13..9d6899285352c862820aca0414daec8cdb4f6722 100644 (file)
@@ -144,8 +144,10 @@ METHOD(packet_t, destroy, void,
        free(this);
 }
 
-METHOD(packet_t, clone_, packet_t*,
-       private_packet_t *this)
+/**
+ * Clone the packet with or without data
+ */
+static packet_t *clone_packet(private_packet_t *this, bool skip_data)
 {
        private_packet_t *other;
 
@@ -158,15 +160,30 @@ METHOD(packet_t, clone_, packet_t*,
        {
                set_source(other, this->source->clone(this->source));
        }
-       if (this->data.ptr)
+       other->metadata = metadata_set_clone(this->metadata);
+       set_dscp(other, this->dscp);
+
+       if (!skip_data && this->data.ptr)
        {
                set_data(other, chunk_clone(this->adjusted_data));
        }
-       other->metadata = metadata_set_clone(this->metadata);
-       set_dscp(other, this->dscp);
        return &other->public;
 }
 
+METHOD(packet_t, clone_, packet_t*,
+       private_packet_t *this)
+{
+       return clone_packet(this, FALSE);
+}
+
+/*
+ * Described in header
+ */
+packet_t *packet_clone_no_data(packet_t *packet)
+{
+       return clone_packet((private_packet_t*)packet, TRUE);
+}
+
 /**
  * Described in header.
  */
index 9859b2c47468c4bae13ce4452b0f0ea1140f9229..48382612ab13ced1d766935184c74450278b2fde 100644 (file)
@@ -150,8 +150,16 @@ packet_t *packet_create();
  * @param src                  source address (gets owned)
  * @param dst                  destination address (gets owned)
  * @param data                 packet data (gets owned)
- * @return packet_t object
+ * @return                             packet_t object
  */
 packet_t *packet_create_from_data(host_t *src, host_t *dst, chunk_t data);
 
+/**
+ * Create a clone of the given packet but without data
+ *
+ * @param packet               packet to clone
+ * @return                             cloned packet
+ */
+packet_t *packet_clone_no_data(packet_t *packet);
+
 #endif /** PACKET_H_ @}*/