From 46bb36980b22853bf2469d412ba593c197308f81 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 15 Jul 2014 13:51:49 +0200 Subject: [PATCH] ip_packet: Add getter for IP payload --- src/libipsec/ip_packet.c | 23 ++++++++++++++++++----- src/libipsec/ip_packet.h | 7 +++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/libipsec/ip_packet.c b/src/libipsec/ip_packet.c index a846717ef..4ef8fd5b8 100644 --- a/src/libipsec/ip_packet.c +++ b/src/libipsec/ip_packet.c @@ -55,6 +55,11 @@ struct private_ip_packet_t { */ chunk_t packet; + /** + * IP payload (points into packet) + */ + chunk_t payload; + /** * IP version */ @@ -91,6 +96,12 @@ METHOD(ip_packet_t, get_encoding, chunk_t, return this->packet; } +METHOD(ip_packet_t, get_payload, chunk_t, + private_ip_packet_t *this) +{ + return this->payload; +} + METHOD(ip_packet_t, get_next_header, u_int8_t, private_ip_packet_t *this) { @@ -163,6 +174,7 @@ ip_packet_t *ip_packet_create(chunk_t packet) u_int8_t version, next_header; u_int16_t sport = 0, dport = 0; host_t *src, *dst; + chunk_t payload; if (packet.len < 1) { @@ -186,9 +198,8 @@ ip_packet_t *ip_packet_create(chunk_t packet) ip = (struct ip*)packet.ptr; /* remove any RFC 4303 TFC extra padding */ packet.len = min(packet.len, untoh16(&ip->ip_len)); - - if (!parse_transport_header(chunk_skip(packet, ip->ip_hl * 4), - ip->ip_p, &sport, &dport)) + payload = chunk_skip(packet, ip->ip_hl * 4); + if (!parse_transport_header(payload, ip->ip_p, &sport, &dport)) { goto failed; } @@ -214,8 +225,8 @@ ip_packet_t *ip_packet_create(chunk_t packet) packet.len = min(packet.len, untoh16(&ip->ip6_plen)); /* we only handle packets without extension headers, just skip the * basic IPv6 header */ - if (!parse_transport_header(chunk_skip(packet, 40), ip->ip6_nxt, - &sport, &dport)) + payload = chunk_skip(packet, 40); + if (!parse_transport_header(payload, ip->ip6_nxt, &sport, &dport)) { goto failed; } @@ -239,12 +250,14 @@ ip_packet_t *ip_packet_create(chunk_t packet) .get_destination = _get_destination, .get_next_header = _get_next_header, .get_encoding = _get_encoding, + .get_payload = _get_payload, .clone = _clone_, .destroy = _destroy, }, .src = src, .dst = dst, .packet = packet, + .payload = payload, .version = version, .next_header = next_header, ); diff --git a/src/libipsec/ip_packet.h b/src/libipsec/ip_packet.h index 0f44c8951..7f7c1a314 100644 --- a/src/libipsec/ip_packet.h +++ b/src/libipsec/ip_packet.h @@ -67,6 +67,13 @@ struct ip_packet_t { */ chunk_t (*get_encoding)(ip_packet_t *this); + /** + * Get only the payload + * + * @return IP payload (internal data) + */ + chunk_t (*get_payload)(ip_packet_t *this); + /** * Clone the IP packet *