moved typedefs to beginning of files to solve some include problems
[strongswan.git] / src / charon / config / traffic_selector.h
1 /**
2 * @file traffic_selector.h
3 *
4 * @brief Interface of traffic_selector_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #ifndef TRAFFIC_SELECTOR_H_
25 #define TRAFFIC_SELECTOR_H_
26
27 typedef enum ts_type_t ts_type_t;
28 typedef struct traffic_selector_t traffic_selector_t;
29
30 #include <types.h>
31 #include <utils/host.h>
32
33 /**
34 * printf() specifier for tRaffic selectors
35 */
36 #define TRAFFIC_SELECTOR_PRINTF_SPEC 'R'
37
38 /**
39 * Traffic selector types.
40 *
41 * @ingroup config
42 */
43 enum ts_type_t {
44
45 /**
46 * A range of IPv4 addresses, represented by two four (4) octet
47 * values. The first value is the beginning IPv4 address
48 * (inclusive) and the second value is the ending IPv4 address
49 * (inclusive). All addresses falling between the two specified
50 * addresses are considered to be within the list.
51 */
52 TS_IPV4_ADDR_RANGE = 7,
53
54 /**
55 * A range of IPv6 addresses, represented by two sixteen (16)
56 * octet values. The first value is the beginning IPv6 address
57 * (inclusive) and the second value is the ending IPv6 address
58 * (inclusive). All addresses falling between the two specified
59 * addresses are considered to be within the list.
60 */
61 TS_IPV6_ADDR_RANGE = 8
62 };
63
64 /**
65 * enum names for ts_type_t
66 */
67 extern enum_name_t *ts_type_name;
68
69 /**
70 * @brief Object representing a traffic selector entry.
71 *
72 * A traffic selector defines an range of addresses
73 * and a range of ports. IPv6 is not fully supported yet.
74 *
75 * @b Constructors:
76 * - traffic_selector_create_from_bytes()
77 * - traffic_selector_create_from_string()
78 *
79 * @todo Add IPv6 support
80 *
81 * @ingroup config
82 */
83 struct traffic_selector_t {
84
85 /**
86 * @brief Compare two traffic selectors, and create a new one
87 * which is the largest subset of both (subnet & port).
88 *
89 * Resulting traffic_selector is newly created and must be destroyed.
90 *
91 * @param this first to compare
92 * @param other second to compare
93 * @return
94 * - created subset of them
95 * - or NULL if no match between this and other
96 */
97 traffic_selector_t *(*get_subset) (traffic_selector_t *this,
98 traffic_selector_t *other);
99
100 /**
101 * @brief Clone a traffic selector.
102 *
103 * @param this traffic selector to clone
104 * @return clone of it
105 */
106 traffic_selector_t *(*clone) (traffic_selector_t *this);
107
108 /**
109 * @brief Get starting address of this ts as a chunk.
110 *
111 * Chunk is in network order gets allocated.
112 *
113 * @param this calling object
114 * @return chunk containing the address
115 */
116 chunk_t (*get_from_address) (traffic_selector_t *this);
117
118 /**
119 * @brief Get ending address of this ts as a chunk.
120 *
121 * Chunk is in network order gets allocated.
122 *
123 * @param this calling object
124 * @return chunk containing the address
125 */
126 chunk_t (*get_to_address) (traffic_selector_t *this);
127
128 /**
129 * @brief Get starting port of this ts.
130 *
131 * Port is in host order, since the parser converts it.
132 * Size depends on protocol.
133 *
134 * @param this calling object
135 * @return port
136 */
137 u_int16_t (*get_from_port) (traffic_selector_t *this);
138
139 /**
140 * @brief Get ending port of this ts.
141 *
142 * Port is in host order, since the parser converts it.
143 * Size depends on protocol.
144 *
145 * @param this calling object
146 * @return port
147 */
148 u_int16_t (*get_to_port) (traffic_selector_t *this);
149
150 /**
151 * @brief Get the type of the traffic selector.
152 *
153 * @param this calling obect
154 * @return ts_type_t specifying the type
155 */
156 ts_type_t (*get_type) (traffic_selector_t *this);
157
158 /**
159 * @brief Get the protocol id of this ts.
160 *
161 * @param this calling obect
162 * @return protocol id
163 */
164 u_int8_t (*get_protocol) (traffic_selector_t *this);
165
166 /**
167 * @brief Check if the traffic selector is for a single host.
168 *
169 * Traffic selector may describe the end of *-to-host tunnel. In this
170 * case, the address range is a single address equal to the hosts
171 * peer address.
172 *
173 * @param this calling obect
174 * @param host host_t specifying the address range
175 */
176 bool (*is_host) (traffic_selector_t *this, host_t* host);
177
178 /**
179 * @brief Update the address of a traffic selector.
180 *
181 * Update the address range of a traffic selector,
182 * if the current address is 0.0.0.0. The new address range
183 * starts from the supplied address and also ends there
184 * (which means it is a one-host-address-range ;-).
185 *
186 * @param this calling obect
187 * @param host host_t specifying the address range
188 */
189 void (*update_address_range) (traffic_selector_t *this, host_t* host);
190
191 /**
192 * @brief Compare two traffic selectors for equality.
193 *
194 * @param this first to compare
195 * @param other second to compare with first
196 * @return pointer to a string.
197 */
198 bool (*equals) (traffic_selector_t *this, traffic_selector_t *other);
199
200 /**
201 * @brief Destroys the ts object
202 *
203 * @param this calling object
204 */
205 void (*destroy) (traffic_selector_t *this);
206 };
207
208 /**
209 * @brief Create a new traffic selector using human readable params.
210 *
211 * @param protocol protocol for this ts, such as TCP or UDP
212 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
213 * @param from_addr start of address range as string
214 * @param from_port port number in host order
215 * @param to_addr end of address range as string
216 * @param to_port port number in host order
217 * @return
218 * - traffic_selector_t object
219 * - NULL if invalid address strings/protocol
220 *
221 * @ingroup config
222 */
223 traffic_selector_t *traffic_selector_create_from_string(
224 u_int8_t protocol, ts_type_t type,
225 char *from_addr, u_int16_t from_port,
226 char *to_addr, u_int16_t to_port);
227
228 /**
229 * @brief Create a new traffic selector using data read from the net.
230 *
231 * There exists a mix of network and host order in the params.
232 * But the parser gives us this data in this format, so we
233 * don't have to convert twice.
234 *
235 * @param protocol protocol for this ts, such as TCP or UDP
236 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
237 * @param from_address start of address range, network order
238 * @param from_port port number, host order
239 * @param to_address end of address range as string, network
240 * @param to_port port number, host order
241 * @return
242 * - traffic_selector_t object
243 * - NULL if invalid address input/protocol
244 *
245 * @ingroup config
246 */
247 traffic_selector_t *traffic_selector_create_from_bytes(
248 u_int8_t protocol, ts_type_t type,
249 chunk_t from_address, u_int16_t from_port,
250 chunk_t to_address, u_int16_t to_port);
251
252 /**
253 * @brief Create a new traffic selector defining a whole subnet.
254 *
255 * In most cases, definition of a traffic selector for full subnets
256 * is sufficient. This constructor creates a traffic selector for
257 * all protocols, all ports and the address range specified by the
258 * subnet.
259 * Additionally, a protocol and a port may be specified. Port ranges
260 * are not supported via this constructor.
261 *
262 * @param net subnet to use
263 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
264 * @return
265 * - traffic_selector_t object
266 * - NULL if address family of net not supported
267 *
268 * @ingroup config
269 */
270 traffic_selector_t *traffic_selector_create_from_subnet(
271 host_t *net, u_int8_t netbits,
272 u_int8_t protocol, u_int16_t port);
273
274 #endif /* TRAFFIC_SELECTOR_H_ */