better split up of library files "types.h" & "definitions.h"
[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 *
168 * @param this calling obect
169 * @param host host_t specifying the address range
170 */
171 bool (*is_host) (traffic_selector_t *this, host_t* host);
172
173 /**
174 * @brief Update the address of a traffic selector.
175 *
176 * Update the address range of a traffic selector,
177 * if the current address is 0.0.0.0. The new address range
178 * starts from the supplied address and also ends there
179 * (which means it is a one-host-address-range ;-).
180 *
181 * @param this calling obect
182 * @param host host_t specifying the address range
183 */
184 void (*update_address_range) (traffic_selector_t *this, host_t* host);
185
186 /**
187 * @brief Compare two traffic selectors for equality.
188 *
189 * @param this first to compare
190 * @param other second to compare with first
191 * @return pointer to a string.
192 */
193 bool (*equals) (traffic_selector_t *this, traffic_selector_t *other);
194
195 /**
196 * @brief Destroys the ts object
197 *
198 * @param this calling object
199 */
200 void (*destroy) (traffic_selector_t *this);
201 };
202
203 /**
204 * @brief Create a new traffic selector using human readable params.
205 *
206 * @param protocol protocol for this ts, such as TCP or UDP
207 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
208 * @param from_addr start of address range as string
209 * @param from_port port number in host order
210 * @param to_addr end of address range as string
211 * @param to_port port number in host order
212 * @return
213 * - traffic_selector_t object
214 * - NULL if invalid address strings/protocol
215 *
216 * @ingroup config
217 */
218 traffic_selector_t *traffic_selector_create_from_string(
219 u_int8_t protocol, ts_type_t type,
220 char *from_addr, u_int16_t from_port,
221 char *to_addr, u_int16_t to_port);
222
223 /**
224 * @brief Create a new traffic selector using data read from the net.
225 *
226 * There exists a mix of network and host order in the params.
227 * But the parser gives us this data in this format, so we
228 * don't have to convert twice.
229 *
230 * @param protocol protocol for this ts, such as TCP or UDP
231 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
232 * @param from_address start of address range, network order
233 * @param from_port port number, host order
234 * @param to_address end of address range as string, network
235 * @param to_port port number, host order
236 * @return
237 * - traffic_selector_t object
238 * - NULL if invalid address input/protocol
239 *
240 * @ingroup config
241 */
242 traffic_selector_t *traffic_selector_create_from_bytes(
243 u_int8_t protocol, ts_type_t type,
244 chunk_t from_address, u_int16_t from_port,
245 chunk_t to_address, u_int16_t to_port);
246
247 /**
248 * @brief Create a new traffic selector defining a whole subnet.
249 *
250 * In most cases, definition of a traffic selector for full subnets
251 * is sufficient. This constructor creates a traffic selector for
252 * all protocols, all ports and the address range specified by the
253 * subnet.
254 * Additionally, a protocol and a port may be specified. Port ranges
255 * are not supported via this constructor.
256 *
257 * @param net subnet to use
258 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
259 * @return
260 * - traffic_selector_t object
261 * - NULL if address family of net not supported
262 *
263 * @ingroup config
264 */
265 traffic_selector_t *traffic_selector_create_from_subnet(
266 host_t *net, u_int8_t netbits,
267 u_int8_t protocol, u_int16_t port);
268
269 #endif /* TRAFFIC_SELECTOR_H_ */