Moved host_t and host_resolver_t to a new networking subfolder
[strongswan.git] / src / libstrongswan / utils / packet.h
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup packet packet
20 * @{ @ingroup utils
21 */
22
23 #ifndef PACKET_H_
24 #define PACKET_H_
25
26 typedef struct packet_t packet_t;
27
28 #include <library.h>
29 #include <networking/host.h>
30
31 /**
32 * Abstraction of an IP/UDP-Packet, contains data, sender and receiver.
33 */
34 struct packet_t {
35
36 /**
37 * Set the source address.
38 *
39 * @param source address to set as source (gets owned)
40 */
41 void (*set_source)(packet_t *packet, host_t *source);
42
43 /**
44 * Set the destination address.
45 *
46 * @param source address to set as destination (gets owned)
47 */
48 void (*set_destination)(packet_t *packet, host_t *destination);
49
50 /**
51 * Get the source address.
52 *
53 * @return source address (internal data)
54 */
55 host_t *(*get_source)(packet_t *packet);
56
57 /**
58 * Get the destination address.
59 *
60 * @return destination address (internal data)
61 */
62 host_t *(*get_destination)(packet_t *packet);
63
64 /**
65 * Get the data from the packet.
66 *
67 * @return chunk containing the data (internal data)
68 */
69 chunk_t (*get_data)(packet_t *packet);
70
71 /**
72 * Set the data in the packet.
73 *
74 * @param data chunk with data to set (gets owned)
75 */
76 void (*set_data)(packet_t *packet, chunk_t data);
77
78 /**
79 * Increase the offset where the actual packet data starts.
80 *
81 * The total offset applies to future calls of get_data() and clone().
82 *
83 * @note The offset is reset to 0 when set_data() is called.
84 *
85 * @param bytes the number of additional bytes to skip
86 */
87 void (*skip_bytes)(packet_t *packet, size_t bytes);
88
89 /**
90 * Clones a packet_t object.
91 *
92 * @note Data is cloned without skipped bytes.
93 *
94 * @param clone clone of the packet
95 */
96 packet_t* (*clone)(packet_t *packet);
97
98 /**
99 * Destroy the packet, freeing contained data.
100 */
101 void (*destroy)(packet_t *packet);
102 };
103
104 /**
105 * Create an empty packet
106 *
107 * @return packet_t object
108 */
109 packet_t *packet_create();
110
111 /**
112 * Create a packet from the supplied data
113 *
114 * @param src source address (gets owned)
115 * @param dst destination address (gets owned)
116 * @param data packet data (gets owned)
117 * @return packet_t object
118 */
119 packet_t *packet_create_from_data(host_t *src, host_t *dst, chunk_t data);
120
121 #endif /** PACKET_H_ @}*/