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