7f7c1a314a9aa316f7bf4741024eee0d58b06c4c
[strongswan.git] / src / libipsec / ip_packet.h
1 /*
2 * Copyright (C) 2012-2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 /**
17 * @defgroup ip_packet ip_packet
18 * @{ @ingroup libipsec
19 */
20
21 #ifndef IP_PACKET_H_
22 #define IP_PACKET_H_
23
24 #include <library.h>
25 #include <networking/host.h>
26 #include <networking/packet.h>
27
28 typedef struct ip_packet_t ip_packet_t;
29
30 /**
31 * IP packet
32 */
33 struct ip_packet_t {
34
35 /**
36 * IP version of this packet
37 *
38 * @return ip version
39 */
40 u_int8_t (*get_version)(ip_packet_t *this);
41
42 /**
43 * Get the source address of this packet
44 *
45 * @return source host
46 */
47 host_t *(*get_source)(ip_packet_t *this);
48
49 /**
50 * Get the destination address of this packet
51 *
52 * @return destination host
53 */
54 host_t *(*get_destination)(ip_packet_t *this);
55
56 /**
57 * Get the protocol (IPv4) or next header (IPv6) field of this packet.
58 *
59 * @return protocol|next header field
60 */
61 u_int8_t (*get_next_header)(ip_packet_t *this);
62
63 /**
64 * Get the complete IP packet (including the header)
65 *
66 * @return IP packet (internal data)
67 */
68 chunk_t (*get_encoding)(ip_packet_t *this);
69
70 /**
71 * Get only the payload
72 *
73 * @return IP payload (internal data)
74 */
75 chunk_t (*get_payload)(ip_packet_t *this);
76
77 /**
78 * Clone the IP packet
79 *
80 * @return clone of the packet
81 */
82 ip_packet_t *(*clone)(ip_packet_t *this);
83
84 /**
85 * Destroy an ip_packet_t
86 */
87 void (*destroy)(ip_packet_t *this);
88
89 };
90
91 /**
92 * Create an IP packet out of data from the wire (or decapsulated from another
93 * packet).
94 *
95 * @note The raw IP packet gets either owned by the new object, or destroyed,
96 * if the data is invalid.
97 *
98 * @param packet the IP packet (including header), gets owned
99 * @return ip_packet_t instance, or NULL if invalid
100 */
101 ip_packet_t *ip_packet_create(chunk_t packet);
102
103 /**
104 * Encode an IP packet from the given data.
105 *
106 * @param src source address (cloned)
107 * @param dst destination address (cloned)
108 * @param next_header the protocol (IPv4) or next header (IPv6)
109 * @param data complete data after basic IP header (cloned)
110 * @return ip_packet_t instance, or NULL if invalid
111 */
112 ip_packet_t *ip_packet_create_from_data(host_t *src, host_t *dst,
113 u_int8_t next_header, chunk_t data);
114
115 #endif /** IP_PACKET_H_ @}*/