]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
packet: Add getter/setter for metadata handling
authorTobias Brunner <tobias@strongswan.org>
Thu, 2 Dec 2021 14:34:18 +0000 (15:34 +0100)
committerTobias Brunner <tobias@strongswan.org>
Fri, 14 Jan 2022 09:13:21 +0000 (10:13 +0100)
src/libipsec/esp_packet.c
src/libstrongswan/networking/packet.c
src/libstrongswan/networking/packet.h

index d1140e25208f183e0e068e8444b4d0d995a193bf..779f903a837dcc33f05f42488041249bd745f5db 100644 (file)
@@ -111,6 +111,18 @@ METHOD(packet_t, set_dscp, void,
        this->packet->set_dscp(this->packet, value);
 }
 
+METHOD(packet_t, get_metadata, metadata_t*,
+       private_esp_packet_t *this, const char *key)
+{
+       return this->packet->get_metadata(this->packet, key);
+}
+
+METHOD(packet_t, set_metadata, void,
+       private_esp_packet_t *this, const char *key, metadata_t *data)
+{
+       this->packet->set_metadata(this->packet, key, data);
+}
+
 METHOD(packet_t, skip_bytes, void,
        private_esp_packet_t *this, size_t bytes)
 {
@@ -415,6 +427,8 @@ static private_esp_packet_t *esp_packet_create_internal(packet_t *packet)
                                .set_data = _set_data,
                                .get_dscp = _get_dscp,
                                .set_dscp = _set_dscp,
+                               .get_metadata = _get_metadata,
+                               .set_metadata = _set_metadata,
                                .skip_bytes = _skip_bytes,
                                .clone = _clone_,
                                .destroy = _destroy,
index 00993f92b42c7f718334e3ab15bef987e5cd84e8..a54ec79a142f90398abd55f842b86c138bb40e13 100644 (file)
@@ -17,6 +17,8 @@
 
 #include "packet.h"
 
+#include <metadata/metadata_set.h>
+
 typedef struct private_packet_t private_packet_t;
 
 /**
@@ -53,6 +55,11 @@ struct private_packet_t {
         * actual chunk returned from get_data, adjusted when skip_bytes is called
         */
        chunk_t adjusted_data;
+
+       /**
+        * Set of metadata objects, if any
+        */
+       metadata_set_t *metadata;
 };
 
 METHOD(packet_t, set_source, void,
@@ -111,11 +118,28 @@ METHOD(packet_t, skip_bytes, void,
        this->adjusted_data = chunk_skip(this->adjusted_data, bytes);
 }
 
+METHOD(packet_t, get_metadata, metadata_t*,
+       private_packet_t *this, const char *key)
+{
+       return metadata_set_get(this->metadata, key);
+}
+
+METHOD(packet_t, set_metadata, void,
+       private_packet_t *this, const char *key, metadata_t *data)
+{
+       if (!this->metadata && data)
+       {
+               this->metadata = metadata_set_create();
+       }
+       metadata_set_put(this->metadata, key, data);
+}
+
 METHOD(packet_t, destroy, void,
        private_packet_t *this)
 {
        DESTROY_IF(this->source);
        DESTROY_IF(this->destination);
+       metadata_set_destroy(this->metadata);
        free(this->data.ptr);
        free(this);
 }
@@ -123,24 +147,24 @@ METHOD(packet_t, destroy, void,
 METHOD(packet_t, clone_, packet_t*,
        private_packet_t *this)
 {
-       packet_t *other;
+       private_packet_t *other;
 
-       other = packet_create();
+       other = (private_packet_t*)packet_create();
        if (this->destination)
        {
-               other->set_destination(other,
-                                                          this->destination->clone(this->destination));
+               set_destination(other, this->destination->clone(this->destination));
        }
        if (this->source)
        {
-               other->set_source(other, this->source->clone(this->source));
+               set_source(other, this->source->clone(this->source));
        }
        if (this->data.ptr)
        {
-               other->set_data(other, chunk_clone(this->adjusted_data));
+               set_data(other, chunk_clone(this->adjusted_data));
        }
-       other->set_dscp(other, this->dscp);
-       return other;
+       other->metadata = metadata_set_clone(this->metadata);
+       set_dscp(other, this->dscp);
+       return &other->public;
 }
 
 /**
@@ -160,6 +184,8 @@ packet_t *packet_create_from_data(host_t *src, host_t *dst, chunk_t data)
                        .get_destination = _get_destination,
                        .get_dscp = _get_dscp,
                        .set_dscp = _set_dscp,
+                       .get_metadata = _get_metadata,
+                       .set_metadata = _set_metadata,
                        .skip_bytes = _skip_bytes,
                        .clone = _clone_,
                        .destroy = _destroy,
index 806337ba0e13b58f748d63cd769b1ad119821a44..9859b2c47468c4bae13ce4452b0f0ea1140f9229 100644 (file)
@@ -94,6 +94,23 @@ struct packet_t {
         */
        void (*set_dscp)(packet_t *this, uint8_t value);
 
+       /**
+        * Get metadata of this packet.
+        *
+        * @param key           key of the metadata to retrieve
+        * @return                      metadata object (internal data), NULL if not found
+        */
+       metadata_t *(*get_metadata)(packet_t *packet, const char *key);
+
+       /**
+        * Set/remove metadata of this packet.
+        *
+        * @param key           key of the metadata (cloned)
+        * @param data          metadata object (adopted), NULL to remove and destroy
+        *                                      existing object with the given key
+        */
+       void (*set_metadata)(packet_t *packet, const char *key, metadata_t *data);
+
        /**
         * Increase the offset where the actual packet data starts.
         *