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