22179623a1f2bf8d3ce054d78fd4d76121185249
[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 gets allocated.
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 gets allocated.
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 * Update the address of a traffic selector.
159 *
160 * Update the address range of a traffic selector, if it is
161 * constructed with the traffic_selector_create_dynamic().
162 *
163 * @param host host_t specifying the address
164 */
165 void (*set_address) (traffic_selector_t *this, host_t* host);
166
167 /**
168 * Compare two traffic selectors for equality.
169 *
170 * @param other ts to compare with this
171 * @return pointer to a string.
172 */
173 bool (*equals) (traffic_selector_t *this, traffic_selector_t *other);
174
175 /**
176 * Check if a traffic selector is contained completly in another.
177 *
178 * contains() allows to check if multiple traffic selectors are redundant.
179 *
180 * @param other ts that contains this
181 * @return TRUE if other contains this completly, FALSE otherwise
182 */
183 bool (*is_contained_in) (traffic_selector_t *this, traffic_selector_t *other);
184
185 /**
186 * Check if a specific host is included in the address range of
187 * this traffic selector.
188 *
189 * @param host the host to check
190 */
191 bool (*includes) (traffic_selector_t *this, host_t *host);
192
193 /**
194 * Convert a traffic selector address range to a subnet
195 * and its net mask.
196 * If from and to ports of this traffic selector are equal,
197 * the port of the returned host_t is set to that port.
198 *
199 * @param net converted subnet (has to be freed)
200 * @param mask converted net mask
201 */
202 void (*to_subnet) (traffic_selector_t *this, host_t **net, u_int8_t *mask);
203
204 /**
205 * Destroys the ts object
206 */
207 void (*destroy) (traffic_selector_t *this);
208 };
209
210 /**
211 * Create a new traffic selector using human readable params.
212 *
213 * @param protocol protocol for this ts, such as TCP or UDP
214 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
215 * @param from_addr start of address range as string
216 * @param from_port port number in host order
217 * @param to_addr end of address range as string
218 * @param to_port port number in host order
219 * @return
220 * - traffic_selector_t object
221 * - NULL if invalid address strings/protocol
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 * 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 traffic_selector_t object
242 */
243 traffic_selector_t *traffic_selector_create_from_bytes(
244 u_int8_t protocol, ts_type_t type,
245 chunk_t from_address, u_int16_t from_port,
246 chunk_t to_address, u_int16_t to_port);
247
248 /**
249 * Create a new traffic selector defining a whole subnet.
250 *
251 * In most cases, definition of a traffic selector for full subnets
252 * is sufficient. This constructor creates a traffic selector for
253 * all protocols, all ports and the address range specified by the
254 * subnet.
255 * Additionally, a protocol and a port may be specified. Port ranges
256 * are not supported via this constructor.
257 *
258 * @param net subnet to use
259 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
260 * @return
261 * - traffic_selector_t object
262 * - NULL if address family of net not supported
263 */
264 traffic_selector_t *traffic_selector_create_from_subnet(
265 host_t *net, u_int8_t netbits,
266 u_int8_t protocol, u_int16_t port);
267
268 /**
269 * Create a traffic selector for host-to-host cases.
270 *
271 * For host2host or virtual IP setups, the traffic selectors gets
272 * created at runtime using the external/virtual IP. Using this constructor,
273 * a call to set_address() sets this traffic selector to the supplied host.
274 *
275 *
276 * @param protocol upper layer protocl to allow
277 * @param from_port start of allowed port range
278 * @param to_port end of range
279 * @return
280 * - traffic_selector_t object
281 * - NULL if type not supported
282 */
283 traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
284 u_int16_t from_port, u_int16_t to_port);
285
286 /**
287 * Get printf hooks for a traffic selector.
288 *
289 * Arguments are:
290 * traffic_selector_t *ts
291 * With the #-specifier, arguments are:
292 * linked_list_t *list containing traffic_selector_t*
293 */
294 printf_hook_functions_t traffic_selector_get_printf_hooks();
295
296 #endif /* TRAFFIC_SELECTOR_H_ @} */