42d188157611602a572ceadb735c23b65a340920
[strongswan.git] / src / charon / config / traffic_selector.h
1 /*
2 * Copyright (C) 2007 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 * $Id$
18 */
19
20 /**
21 * @defgroup traffic_selector traffic_selector
22 * @{ @ingroup config
23 */
24
25 #ifndef TRAFFIC_SELECTOR_H_
26 #define TRAFFIC_SELECTOR_H_
27
28 typedef enum ts_type_t ts_type_t;
29 typedef struct traffic_selector_t traffic_selector_t;
30
31 #include <library.h>
32 #include <utils/host.h>
33
34 /**
35 * Traffic selector types.
36 */
37 enum ts_type_t {
38
39 /**
40 * A range of IPv4 addresses, represented by two four (4) octet
41 * values. The first value is the beginning IPv4 address
42 * (inclusive) and the second value is the ending IPv4 address
43 * (inclusive). All addresses falling between the two specified
44 * addresses are considered to be within the list.
45 */
46 TS_IPV4_ADDR_RANGE = 7,
47
48 /**
49 * A range of IPv6 addresses, represented by two sixteen (16)
50 * octet values. The first value is the beginning IPv6 address
51 * (inclusive) and the second value is the ending IPv6 address
52 * (inclusive). All addresses falling between the two specified
53 * addresses are considered to be within the list.
54 */
55 TS_IPV6_ADDR_RANGE = 8
56 };
57
58 /**
59 * enum names for ts_type_t
60 */
61 extern enum_name_t *ts_type_name;
62
63 /**
64 * Object representing a traffic selector entry.
65 *
66 * A traffic selector defines an range of addresses
67 * and a range of ports. IPv6 is not fully supported yet.
68 */
69 struct traffic_selector_t {
70
71 /**
72 * Compare two traffic selectors, and create a new one
73 * which is the largest subset of both (subnet & port).
74 *
75 * Resulting traffic_selector is newly created and must be destroyed.
76 *
77 * @param other traffic selector to compare
78 * @return
79 * - created subset of them
80 * - or NULL if no match between this and other
81 */
82 traffic_selector_t *(*get_subset) (traffic_selector_t *this,
83 traffic_selector_t *other);
84
85 /**
86 * Clone a traffic selector.
87 *
88 * @return clone of it
89 */
90 traffic_selector_t *(*clone) (traffic_selector_t *this);
91
92 /**
93 * Get starting address of this ts as a chunk.
94 *
95 * Chunk is in network order and points to internal data.
96 *
97 * @return chunk containing the address
98 */
99 chunk_t (*get_from_address) (traffic_selector_t *this);
100
101 /**
102 * Get ending address of this ts as a chunk.
103 *
104 * Chunk is in network order and points to internal data.
105 *
106 * @return chunk containing the address
107 */
108 chunk_t (*get_to_address) (traffic_selector_t *this);
109
110 /**
111 * Get starting port of this ts.
112 *
113 * Port is in host order, since the parser converts it.
114 * Size depends on protocol.
115 *
116 * @return port
117 */
118 u_int16_t (*get_from_port) (traffic_selector_t *this);
119
120 /**
121 * Get ending port of this ts.
122 *
123 * Port is in host order, since the parser converts it.
124 * Size depends on protocol.
125 *
126 * @return port
127 */
128 u_int16_t (*get_to_port) (traffic_selector_t *this);
129
130 /**
131 * Get the type of the traffic selector.
132 *
133 * @return ts_type_t specifying the type
134 */
135 ts_type_t (*get_type) (traffic_selector_t *this);
136
137 /**
138 * Get the protocol id of this ts.
139 *
140 * @return protocol id
141 */
142 u_int8_t (*get_protocol) (traffic_selector_t *this);
143
144 /**
145 * Check if the traffic selector is for a single host.
146 *
147 * Traffic selector may describe the end of *-to-host tunnel. In this
148 * case, the address range is a single address equal to the hosts
149 * peer address.
150 * If host is NULL, the traffic selector is checked if it is a single host,
151 * but not a specific one.
152 *
153 * @param host host_t specifying the address range
154 */
155 bool (*is_host) (traffic_selector_t *this, host_t* host);
156
157 /**
158 * Check if a traffic selector has been created by create_dynamic().
159 *
160 * @return TRUE if TS is dynamic
161 */
162 bool (*is_dynamic)(traffic_selector_t *this);
163
164 /**
165 * Update the address of a traffic selector.
166 *
167 * Update the address range of a traffic selector, if it is
168 * constructed with the traffic_selector_create_dynamic().
169 *
170 * @param host host_t specifying the address
171 */
172 void (*set_address) (traffic_selector_t *this, host_t* host);
173
174 /**
175 * Compare two traffic selectors for equality.
176 *
177 * @param other ts to compare with this
178 * @return TRUE if equal, FALSE otherwise
179 */
180 bool (*equals) (traffic_selector_t *this, traffic_selector_t *other);
181
182 /**
183 * Check if a traffic selector is contained completly in another.
184 *
185 * contains() allows to check if multiple traffic selectors are redundant.
186 *
187 * @param other ts that contains this
188 * @return TRUE if other contains this completly, FALSE otherwise
189 */
190 bool (*is_contained_in) (traffic_selector_t *this, traffic_selector_t *other);
191
192 /**
193 * Check if a specific host is included in the address range of
194 * this traffic selector.
195 *
196 * @param host the host to check
197 */
198 bool (*includes) (traffic_selector_t *this, host_t *host);
199
200 /**
201 * Convert a traffic selector address range to a subnet
202 * and its net mask.
203 * If from and to ports of this traffic selector are equal,
204 * the port of the returned host_t is set to that port.
205 *
206 * @param net converted subnet (has to be freed)
207 * @param mask converted net mask
208 */
209 void (*to_subnet) (traffic_selector_t *this, host_t **net, u_int8_t *mask);
210
211 /**
212 * Destroys the ts object
213 */
214 void (*destroy) (traffic_selector_t *this);
215 };
216
217 /**
218 * Create a new traffic selector using human readable params.
219 *
220 * @param protocol protocol for this ts, such as TCP or UDP
221 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
222 * @param from_addr start of address range as string
223 * @param from_port port number in host order
224 * @param to_addr end of address range as string
225 * @param to_port port number in host order
226 * @return
227 * - traffic_selector_t object
228 * - NULL if invalid address strings/protocol
229 */
230 traffic_selector_t *traffic_selector_create_from_string(
231 u_int8_t protocol, ts_type_t type,
232 char *from_addr, u_int16_t from_port,
233 char *to_addr, u_int16_t to_port);
234
235 /**
236 * Create a new traffic selector using data read from the net.
237 *
238 * There exists a mix of network and host order in the params.
239 * But the parser gives us this data in this format, so we
240 * don't have to convert twice.
241 *
242 * @param protocol protocol for this ts, such as TCP or UDP
243 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
244 * @param from_address start of address range, network order
245 * @param from_port port number, host order
246 * @param to_address end of address range, network order
247 * @param to_port port number, host order
248 * @return traffic_selector_t object
249 */
250 traffic_selector_t *traffic_selector_create_from_bytes(
251 u_int8_t protocol, ts_type_t type,
252 chunk_t from_address, u_int16_t from_port,
253 chunk_t to_address, u_int16_t to_port);
254
255 /**
256 * Create a new traffic selector defining a whole subnet.
257 *
258 * In most cases, definition of a traffic selector for full subnets
259 * is sufficient. This constructor creates a traffic selector for
260 * all protocols, all ports and the address range specified by the
261 * subnet.
262 * Additionally, a protocol and a port may be specified. Port ranges
263 * are not supported via this constructor.
264 *
265 * @param net subnet to use
266 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
267 * @return
268 * - traffic_selector_t object
269 * - NULL if address family of net not supported
270 */
271 traffic_selector_t *traffic_selector_create_from_subnet(
272 host_t *net, u_int8_t netbits,
273 u_int8_t protocol, u_int16_t port);
274
275 /**
276 * Create a traffic selector for host-to-host cases.
277 *
278 * For host2host or virtual IP setups, the traffic selectors gets
279 * created at runtime using the external/virtual IP. Using this constructor,
280 * a call to set_address() sets this traffic selector to the supplied host.
281 *
282 *
283 * @param protocol upper layer protocl to allow
284 * @param from_port start of allowed port range
285 * @param to_port end of range
286 * @return
287 * - traffic_selector_t object
288 * - NULL if type not supported
289 */
290 traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
291 u_int16_t from_port, u_int16_t to_port);
292
293 /**
294 * printf hook function for traffic_selector_t.
295 *
296 * Arguments are:
297 * traffic_selector_t *ts
298 * With the #-specifier, arguments are:
299 * linked_list_t *list containing traffic_selector_t*
300 */
301 int traffic_selector_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
302 const void *const *args);
303
304 #endif /* TRAFFIC_SELECTOR_H_ @} */