From e3d84bc6f68167a25138cbf956b335ecc9901c5d Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 2 Dec 2021 16:10:46 +0100 Subject: [PATCH] packet: Add helper function to create a clone without data --- src/libstrongswan/networking/packet.c | 27 ++++++++++++++++++++++----- src/libstrongswan/networking/packet.h | 10 +++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/libstrongswan/networking/packet.c b/src/libstrongswan/networking/packet.c index a54ec79a1..9d6899285 100644 --- a/src/libstrongswan/networking/packet.c +++ b/src/libstrongswan/networking/packet.c @@ -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. */ diff --git a/src/libstrongswan/networking/packet.h b/src/libstrongswan/networking/packet.h index 9859b2c47..48382612a 100644 --- a/src/libstrongswan/networking/packet.h +++ b/src/libstrongswan/networking/packet.h @@ -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_ @}*/ -- 2.47.3