From 93583d23d67cd06b69dcafefe3a63f53464450b1 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 2 Dec 2021 15:34:18 +0100 Subject: [PATCH] packet: Add getter/setter for metadata handling --- src/libipsec/esp_packet.c | 14 +++++++++ src/libstrongswan/networking/packet.c | 42 ++++++++++++++++++++++----- src/libstrongswan/networking/packet.h | 17 +++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/libipsec/esp_packet.c b/src/libipsec/esp_packet.c index d1140e2520..779f903a83 100644 --- a/src/libipsec/esp_packet.c +++ b/src/libipsec/esp_packet.c @@ -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, diff --git a/src/libstrongswan/networking/packet.c b/src/libstrongswan/networking/packet.c index 00993f92b4..a54ec79a14 100644 --- a/src/libstrongswan/networking/packet.c +++ b/src/libstrongswan/networking/packet.c @@ -17,6 +17,8 @@ #include "packet.h" +#include + 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, diff --git a/src/libstrongswan/networking/packet.h b/src/libstrongswan/networking/packet.h index 806337ba0e..9859b2c474 100644 --- a/src/libstrongswan/networking/packet.h +++ b/src/libstrongswan/networking/packet.h @@ -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. * -- 2.47.2