cleanups in kernel interface code
[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 typedef enum ts_type_t ts_type_t;
31
32 /**
33 * Traffic selector types.
34 *
35 * @ingroup config
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 * string mappings for ts_type_t
60 */
61 extern mapping_t ts_type_m[];
62
63
64 typedef struct traffic_selector_t traffic_selector_t;
65
66 /**
67 * @brief Object representing a traffic selector entry.
68 *
69 * A traffic selector defines an range of addresses
70 * and a range of ports. IPv6 is not fully supported yet.
71 *
72 * @b Constructors:
73 * - traffic_selector_create_from_bytes()
74 * - traffic_selector_create_from_string()
75 *
76 * @todo Add IPv6 support
77 *
78 * @ingroup config
79 */
80 struct traffic_selector_t {
81
82 /**
83 * @brief Compare two traffic selectors, and create a new one
84 * which is the largest subset of both (subnet & port).
85 *
86 * Resulting traffic_selector is newly created and must be destroyed.
87 *
88 * @param this first to compare
89 * @param other second to compare
90 * @return
91 * - created subset of them
92 * - or NULL if no match between this and other
93 */
94 traffic_selector_t *(*get_subset) (traffic_selector_t *this,
95 traffic_selector_t *other);
96
97 /**
98 * @brief Clone a traffic selector.
99 *
100 * @param this traffic selector to clone
101 * @return clone of it
102 */
103 traffic_selector_t *(*clone) (traffic_selector_t *this);
104
105 /**
106 * @brief Get starting address of this ts as a chunk.
107 *
108 * Chunk is in network order gets allocated.
109 *
110 * @param this calling object
111 * @return chunk containing the address
112 */
113 chunk_t (*get_from_address) (traffic_selector_t *this);
114
115 /**
116 * @brief Get ending address of this ts as a chunk.
117 *
118 * Chunk is in network order gets allocated.
119 *
120 * @param this calling object
121 * @return chunk containing the address
122 */
123 chunk_t (*get_to_address) (traffic_selector_t *this);
124
125 /**
126 * @brief Get starting port of this ts.
127 *
128 * Port is in host order, since the parser converts it.
129 * Size depends on protocol.
130 *
131 * @param this calling object
132 * @return port
133 */
134 u_int16_t (*get_from_port) (traffic_selector_t *this);
135
136 /**
137 * @brief Get ending port of this ts.
138 *
139 * Port is in host order, since the parser converts it.
140 * Size depends on protocol.
141 *
142 * @param this calling object
143 * @return port
144 */
145 u_int16_t (*get_to_port) (traffic_selector_t *this);
146
147 /**
148 * @brief Get the type of the traffic selector.
149 *
150 * @param this calling obect
151 * @return ts_type_t specifying the type
152 */
153 ts_type_t (*get_type) (traffic_selector_t *this);
154
155 /**
156 * @brief Get the protocol id of this ts.
157 *
158 * @param this calling obect
159 * @return protocol id
160 */
161 u_int8_t (*get_protocol) (traffic_selector_t *this);
162
163 /**
164 * @brief Update the address of a traffic selector.
165 *
166 * Update the address range of a traffic selector,
167 * if the current address is 0.0.0.0. The new address range
168 * starts from the supplied address and also ends there
169 * (which means it is a one-host-address-range ;-).
170 *
171 * @param this calling obect
172 * @param host host_t specifying the address range
173 */
174 void (*update_address_range) (traffic_selector_t *this, host_t* host);
175
176 /**
177 * @brief Get a string representation of the traffic selector.
178 *
179 * String points to internal data, do not free/modify.
180 *
181 * @param this calling object
182 * @return pointer to a string.
183 */
184 char* (*get_string) (traffic_selector_t *this);
185
186 /**
187 * @brief Destroys the ts object
188 *
189 * @param this calling object
190 */
191 void (*destroy) (traffic_selector_t *this);
192 };
193
194 /**
195 * @brief Create a new traffic selector using human readable params.
196 *
197 * @param protocol protocol for this ts, such as TCP or UDP
198 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
199 * @param from_addr start of address range as string
200 * @param from_port port number in host order
201 * @param to_addr end of address range as string
202 * @param to_port port number in host order
203 * @return
204 * - traffic_selector_t object
205 * - NULL if invalid address strings/protocol
206 *
207 * @ingroup config
208 */
209 traffic_selector_t *traffic_selector_create_from_string(
210 u_int8_t protocol, ts_type_t type,
211 char *from_addr, u_int16_t from_port,
212 char *to_addr, u_int16_t to_port);
213
214 /**
215 * @brief Create a new traffic selector using data read from the net.
216 *
217 * There exists a mix of network and host order in the params.
218 * But the parser gives us this data in this format, so we
219 * don't have to convert twice.
220 *
221 * @param protocol protocol for this ts, such as TCP or UDP
222 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
223 * @param from_address start of address range, network order
224 * @param from_port port number, host order
225 * @param to_address end of address range as string, network
226 * @param to_port port number, host order
227 * @return
228 * - traffic_selector_t object
229 * - NULL if invalid address input/protocol
230 *
231 * @ingroup config
232 */
233 traffic_selector_t *traffic_selector_create_from_bytes(
234 u_int8_t protocol, ts_type_t type,
235 chunk_t from_address, u_int16_t from_port,
236 chunk_t to_address, u_int16_t to_port);
237
238 /**
239 * @brief Create a new traffic selector defining a whole subnet.
240 *
241 * In most cases, definition of a traffic selector for full subnets
242 * is sufficient. This constructor creates a traffic selector for
243 * all protocols, all ports and the address range specified by the
244 * subnet.
245 * Additionally, a protocol and a port may be specified. Port ranges
246 * are not supported via this constructor.
247 *
248 * @param net subnet to use
249 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
250 * @return
251 * - traffic_selector_t object
252 * - NULL if address family of net not supported
253 *
254 * @ingroup config
255 */
256 traffic_selector_t *traffic_selector_create_from_subnet(
257 host_t *net, u_int8_t netbits,
258 u_int8_t protocol, u_int16_t port);
259
260 #endif /* TRAFFIC_SELECTOR_H_ */