2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "ip_packet.h"
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip6.h>
26 typedef struct private_ip_packet_t private_ip_packet_t
;
29 * Private additions to ip_packet_t.
31 struct private_ip_packet_t
{
59 * Protocol|Next Header field
65 METHOD(ip_packet_t
, get_version
, u_int8_t
,
66 private_ip_packet_t
*this)
71 METHOD(ip_packet_t
, get_source
, host_t
*,
72 private_ip_packet_t
*this)
77 METHOD(ip_packet_t
, get_destination
, host_t
*,
78 private_ip_packet_t
*this)
83 METHOD(ip_packet_t
, get_encoding
, chunk_t
,
84 private_ip_packet_t
*this)
89 METHOD(ip_packet_t
, get_next_header
, u_int8_t
,
90 private_ip_packet_t
*this)
92 return this->next_header
;
95 METHOD(ip_packet_t
, clone
, ip_packet_t
*,
96 private_ip_packet_t
*this)
98 return ip_packet_create(this->packet
);
101 METHOD(ip_packet_t
, destroy
, void,
102 private_ip_packet_t
*this)
104 this->src
->destroy(this->src
);
105 this->dst
->destroy(this->dst
);
106 chunk_free(&this->packet
);
111 * Described in header.
113 ip_packet_t
*ip_packet_create(chunk_t packet
)
115 private_ip_packet_t
*this;
116 u_int8_t version
, next_header
;
121 DBG1(DBG_ESP
, "IP packet too short");
125 version
= (packet
.ptr
[0] & 0xf0) >> 4;
133 if (packet
.len
< sizeof(struct iphdr
))
135 DBG1(DBG_ESP
, "IPv4 packet too short");
138 ip
= (struct iphdr
*)packet
.ptr
;
139 src
= host_create_from_chunk(AF_INET
,
140 chunk_from_thing(ip
->saddr
), 0);
141 dst
= host_create_from_chunk(AF_INET
,
142 chunk_from_thing(ip
->daddr
), 0);
143 next_header
= ip
->protocol
;
150 if (packet
.len
< sizeof(struct ip6_hdr
))
152 DBG1(DBG_ESP
, "IPv6 packet too short");
155 ip
= (struct ip6_hdr
*)packet
.ptr
;
156 src
= host_create_from_chunk(AF_INET6
,
157 chunk_from_thing(ip
->ip6_src
), 0);
158 dst
= host_create_from_chunk(AF_INET6
,
159 chunk_from_thing(ip
->ip6_dst
), 0);
160 next_header
= ip
->ip6_nxt
;
163 DBG1(DBG_ESP
, "unsupported IP version");
169 .get_version
= _get_version
,
170 .get_source
= _get_source
,
171 .get_destination
= _get_destination
,
172 .get_next_header
= _get_next_header
,
173 .get_encoding
= _get_encoding
,
181 .next_header
= next_header
,
183 return &this->public;