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