packet: Add getter/setter for metadata handling

This commit is contained in:
Tobias Brunner
2022-01-14 10:13:21 +01:00
parent 269ca19f13
commit 93583d23d6
3 changed files with 65 additions and 8 deletions
+14
View File
@@ -111,6 +111,18 @@ METHOD(packet_t, set_dscp, void,
this->packet->set_dscp(this->packet, value); 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, METHOD(packet_t, skip_bytes, void,
private_esp_packet_t *this, size_t bytes) 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, .set_data = _set_data,
.get_dscp = _get_dscp, .get_dscp = _get_dscp,
.set_dscp = _set_dscp, .set_dscp = _set_dscp,
.get_metadata = _get_metadata,
.set_metadata = _set_metadata,
.skip_bytes = _skip_bytes, .skip_bytes = _skip_bytes,
.clone = _clone_, .clone = _clone_,
.destroy = _destroy, .destroy = _destroy,
+34 -8
View File
@@ -17,6 +17,8 @@
#include "packet.h" #include "packet.h"
#include <metadata/metadata_set.h>
typedef struct private_packet_t private_packet_t; 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 * actual chunk returned from get_data, adjusted when skip_bytes is called
*/ */
chunk_t adjusted_data; chunk_t adjusted_data;
/**
* Set of metadata objects, if any
*/
metadata_set_t *metadata;
}; };
METHOD(packet_t, set_source, void, 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); 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, METHOD(packet_t, destroy, void,
private_packet_t *this) private_packet_t *this)
{ {
DESTROY_IF(this->source); DESTROY_IF(this->source);
DESTROY_IF(this->destination); DESTROY_IF(this->destination);
metadata_set_destroy(this->metadata);
free(this->data.ptr); free(this->data.ptr);
free(this); free(this);
} }
@@ -123,24 +147,24 @@ METHOD(packet_t, destroy, void,
METHOD(packet_t, clone_, packet_t*, METHOD(packet_t, clone_, packet_t*,
private_packet_t *this) private_packet_t *this)
{ {
packet_t *other; private_packet_t *other;
other = packet_create(); other = (private_packet_t*)packet_create();
if (this->destination) if (this->destination)
{ {
other->set_destination(other, set_destination(other, this->destination->clone(this->destination));
this->destination->clone(this->destination));
} }
if (this->source) if (this->source)
{ {
other->set_source(other, this->source->clone(this->source)); set_source(other, this->source->clone(this->source));
} }
if (this->data.ptr) 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); other->metadata = metadata_set_clone(this->metadata);
return other; 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_destination = _get_destination,
.get_dscp = _get_dscp, .get_dscp = _get_dscp,
.set_dscp = _set_dscp, .set_dscp = _set_dscp,
.get_metadata = _get_metadata,
.set_metadata = _set_metadata,
.skip_bytes = _skip_bytes, .skip_bytes = _skip_bytes,
.clone = _clone_, .clone = _clone_,
.destroy = _destroy, .destroy = _destroy,
+17
View File
@@ -94,6 +94,23 @@ struct packet_t {
*/ */
void (*set_dscp)(packet_t *this, uint8_t value); 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. * Increase the offset where the actual packet data starts.
* *