fixed a bug introduced in [3973]
[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 * Destroys the ts object
195 */
196 void (*destroy) (traffic_selector_t *this);
197 };
198
199 /**
200 * Create a new traffic selector using human readable params.
201 *
202 * @param protocol protocol for this ts, such as TCP or UDP
203 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
204 * @param from_addr start of address range as string
205 * @param from_port port number in host order
206 * @param to_addr end of address range as string
207 * @param to_port port number in host order
208 * @return
209 * - traffic_selector_t object
210 * - NULL if invalid address strings/protocol
211 */
212 traffic_selector_t *traffic_selector_create_from_string(
213 u_int8_t protocol, ts_type_t type,
214 char *from_addr, u_int16_t from_port,
215 char *to_addr, u_int16_t to_port);
216
217 /**
218 * Create a new traffic selector using data read from the net.
219 *
220 * There exists a mix of network and host order in the params.
221 * But the parser gives us this data in this format, so we
222 * don't have to convert twice.
223 *
224 * @param protocol protocol for this ts, such as TCP or UDP
225 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
226 * @param from_address start of address range, network order
227 * @param from_port port number, host order
228 * @param to_address end of address range as string, network
229 * @param to_port port number, host order
230 * @return traffic_selector_t object
231 */
232 traffic_selector_t *traffic_selector_create_from_bytes(
233 u_int8_t protocol, ts_type_t type,
234 chunk_t from_address, u_int16_t from_port,
235 chunk_t to_address, u_int16_t to_port);
236
237 /**
238 * Create a new traffic selector defining a whole subnet.
239 *
240 * In most cases, definition of a traffic selector for full subnets
241 * is sufficient. This constructor creates a traffic selector for
242 * all protocols, all ports and the address range specified by the
243 * subnet.
244 * Additionally, a protocol and a port may be specified. Port ranges
245 * are not supported via this constructor.
246 *
247 * @param net subnet to use
248 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
249 * @return
250 * - traffic_selector_t object
251 * - NULL if address family of net not supported
252 */
253 traffic_selector_t *traffic_selector_create_from_subnet(
254 host_t *net, u_int8_t netbits,
255 u_int8_t protocol, u_int16_t port);
256
257 /**
258 * Create a traffic selector for host-to-host cases.
259 *
260 * For host2host or virtual IP setups, the traffic selectors gets
261 * created at runtime using the external/virtual IP. Using this constructor,
262 * a call to set_address() sets this traffic selector to the supplied host.
263 *
264 *
265 * @param protocol upper layer protocl to allow
266 * @param from_port start of allowed port range
267 * @param to_port end of range
268 * @return
269 * - traffic_selector_t object
270 * - NULL if type not supported
271 */
272 traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
273 u_int16_t from_port, u_int16_t to_port);
274
275 /**
276 * Get printf hooks for a traffic selector.
277 *
278 * Arguments are:
279 * traffic_selector_t *ts
280 * With the #-specifier, arguments are:
281 * linked_list_t *list containing traffic_selector_t*
282 */
283 printf_hook_functions_t traffic_selector_get_printf_hooks();
284
285 #endif /* TRAFFIC_SELECTOR_H_ @} */