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