ip_packet: Add getter for IP payload

This commit is contained in:
Tobias Brunner 2014-07-15 13:51:49 +02:00
parent d56d9a45d4
commit 46bb36980b
2 changed files with 25 additions and 5 deletions

View File

@ -55,6 +55,11 @@ struct private_ip_packet_t {
*/ */
chunk_t packet; chunk_t packet;
/**
* IP payload (points into packet)
*/
chunk_t payload;
/** /**
* IP version * IP version
*/ */
@ -91,6 +96,12 @@ METHOD(ip_packet_t, get_encoding, chunk_t,
return this->packet; 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, METHOD(ip_packet_t, get_next_header, u_int8_t,
private_ip_packet_t *this) 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_int8_t version, next_header;
u_int16_t sport = 0, dport = 0; u_int16_t sport = 0, dport = 0;
host_t *src, *dst; host_t *src, *dst;
chunk_t payload;
if (packet.len < 1) if (packet.len < 1)
{ {
@ -186,9 +198,8 @@ ip_packet_t *ip_packet_create(chunk_t packet)
ip = (struct ip*)packet.ptr; ip = (struct ip*)packet.ptr;
/* remove any RFC 4303 TFC extra padding */ /* remove any RFC 4303 TFC extra padding */
packet.len = min(packet.len, untoh16(&ip->ip_len)); packet.len = min(packet.len, untoh16(&ip->ip_len));
payload = chunk_skip(packet, ip->ip_hl * 4);
if (!parse_transport_header(chunk_skip(packet, ip->ip_hl * 4), if (!parse_transport_header(payload, ip->ip_p, &sport, &dport))
ip->ip_p, &sport, &dport))
{ {
goto failed; goto failed;
} }
@ -214,8 +225,8 @@ ip_packet_t *ip_packet_create(chunk_t packet)
packet.len = min(packet.len, untoh16(&ip->ip6_plen)); packet.len = min(packet.len, untoh16(&ip->ip6_plen));
/* we only handle packets without extension headers, just skip the /* we only handle packets without extension headers, just skip the
* basic IPv6 header */ * basic IPv6 header */
if (!parse_transport_header(chunk_skip(packet, 40), ip->ip6_nxt, payload = chunk_skip(packet, 40);
&sport, &dport)) if (!parse_transport_header(payload, ip->ip6_nxt, &sport, &dport))
{ {
goto failed; goto failed;
} }
@ -239,12 +250,14 @@ ip_packet_t *ip_packet_create(chunk_t packet)
.get_destination = _get_destination, .get_destination = _get_destination,
.get_next_header = _get_next_header, .get_next_header = _get_next_header,
.get_encoding = _get_encoding, .get_encoding = _get_encoding,
.get_payload = _get_payload,
.clone = _clone_, .clone = _clone_,
.destroy = _destroy, .destroy = _destroy,
}, },
.src = src, .src = src,
.dst = dst, .dst = dst,
.packet = packet, .packet = packet,
.payload = payload,
.version = version, .version = version,
.next_header = next_header, .next_header = next_header,
); );

View File

@ -67,6 +67,13 @@ struct ip_packet_t {
*/ */
chunk_t (*get_encoding)(ip_packet_t *this); 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 * Clone the IP packet
* *