2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * @defgroup traffic_selector traffic_selector
25 #ifndef TRAFFIC_SELECTOR_H_
26 #define TRAFFIC_SELECTOR_H_
28 typedef enum ts_type_t ts_type_t
;
29 typedef struct traffic_selector_t traffic_selector_t
;
32 #include <utils/host.h>
35 * Traffic selector types.
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.
46 TS_IPV4_ADDR_RANGE
= 7,
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.
55 TS_IPV6_ADDR_RANGE
= 8
59 * enum names for ts_type_t
61 extern enum_name_t
*ts_type_name
;
64 * Object representing a traffic selector entry.
66 * A traffic selector defines an range of addresses
67 * and a range of ports. IPv6 is not fully supported yet.
69 struct traffic_selector_t
{
72 * Compare two traffic selectors, and create a new one
73 * which is the largest subset of both (subnet & port).
75 * Resulting traffic_selector is newly created and must be destroyed.
77 * @param other traffic selector to compare
79 * - created subset of them
80 * - or NULL if no match between this and other
82 traffic_selector_t
*(*get_subset
) (traffic_selector_t
*this,
83 traffic_selector_t
*other
);
86 * Clone a traffic selector.
90 traffic_selector_t
*(*clone
) (traffic_selector_t
*this);
93 * Get starting address of this ts as a chunk.
95 * Chunk is in network order and points to internal data.
97 * @return chunk containing the address
99 chunk_t (*get_from_address
) (traffic_selector_t
*this);
102 * Get ending address of this ts as a chunk.
104 * Chunk is in network order and points to internal data.
106 * @return chunk containing the address
108 chunk_t (*get_to_address
) (traffic_selector_t
*this);
111 * Get starting port of this ts.
113 * Port is in host order, since the parser converts it.
114 * Size depends on protocol.
118 u_int16_t (*get_from_port
) (traffic_selector_t
*this);
121 * Get ending port of this ts.
123 * Port is in host order, since the parser converts it.
124 * Size depends on protocol.
128 u_int16_t (*get_to_port
) (traffic_selector_t
*this);
131 * Get the type of the traffic selector.
133 * @return ts_type_t specifying the type
135 ts_type_t (*get_type
) (traffic_selector_t
*this);
138 * Get the protocol id of this ts.
140 * @return protocol id
142 u_int8_t (*get_protocol
) (traffic_selector_t
*this);
145 * Check if the traffic selector is for a single host.
147 * Traffic selector may describe the end of *-to-host tunnel. In this
148 * case, the address range is a single address equal to the hosts
150 * If host is NULL, the traffic selector is checked if it is a single host,
151 * but not a specific one.
153 * @param host host_t specifying the address range
155 bool (*is_host
) (traffic_selector_t
*this, host_t
* host
);
158 * Check if a traffic selector has been created by create_dynamic().
160 * @return TRUE if TS is dynamic
162 bool (*is_dynamic
)(traffic_selector_t
*this);
165 * Update the address of a traffic selector.
167 * Update the address range of a traffic selector, if it is
168 * constructed with the traffic_selector_create_dynamic().
170 * @param host host_t specifying the address
172 void (*set_address
) (traffic_selector_t
*this, host_t
* host
);
175 * Compare two traffic selectors for equality.
177 * @param other ts to compare with this
178 * @return TRUE if equal, FALSE otherwise
180 bool (*equals
) (traffic_selector_t
*this, traffic_selector_t
*other
);
183 * Check if a traffic selector is contained completly in another.
185 * contains() allows to check if multiple traffic selectors are redundant.
187 * @param other ts that contains this
188 * @return TRUE if other contains this completly, FALSE otherwise
190 bool (*is_contained_in
) (traffic_selector_t
*this, traffic_selector_t
*other
);
193 * Check if a specific host is included in the address range of
194 * this traffic selector.
196 * @param host the host to check
198 bool (*includes
) (traffic_selector_t
*this, host_t
*host
);
201 * Convert a traffic selector address range to a subnet
203 * If from and to ports of this traffic selector are equal,
204 * the port of the returned host_t is set to that port.
206 * @param net converted subnet (has to be freed)
207 * @param mask converted net mask
209 void (*to_subnet
) (traffic_selector_t
*this, host_t
**net
, u_int8_t
*mask
);
212 * Destroys the ts object
214 void (*destroy
) (traffic_selector_t
*this);
218 * Create a new traffic selector using human readable params.
220 * @param protocol protocol for this ts, such as TCP or UDP
221 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
222 * @param from_addr start of address range as string
223 * @param from_port port number in host order
224 * @param to_addr end of address range as string
225 * @param to_port port number in host order
227 * - traffic_selector_t object
228 * - NULL if invalid address strings/protocol
230 traffic_selector_t
*traffic_selector_create_from_string(
231 u_int8_t protocol
, ts_type_t type
,
232 char *from_addr
, u_int16_t from_port
,
233 char *to_addr
, u_int16_t to_port
);
236 * Create a new traffic selector using data read from the net.
238 * There exists a mix of network and host order in the params.
239 * But the parser gives us this data in this format, so we
240 * don't have to convert twice.
242 * @param protocol protocol for this ts, such as TCP or UDP
243 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
244 * @param from_address start of address range, network order
245 * @param from_port port number, host order
246 * @param to_address end of address range, network order
247 * @param to_port port number, host order
248 * @return traffic_selector_t object
250 traffic_selector_t
*traffic_selector_create_from_bytes(
251 u_int8_t protocol
, ts_type_t type
,
252 chunk_t from_address
, u_int16_t from_port
,
253 chunk_t to_address
, u_int16_t to_port
);
256 * Create a new traffic selector defining a whole subnet.
258 * In most cases, definition of a traffic selector for full subnets
259 * is sufficient. This constructor creates a traffic selector for
260 * all protocols, all ports and the address range specified by the
262 * Additionally, a protocol and a port may be specified. Port ranges
263 * are not supported via this constructor.
265 * @param net subnet to use
266 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
268 * - traffic_selector_t object
269 * - NULL if address family of net not supported
271 traffic_selector_t
*traffic_selector_create_from_subnet(
272 host_t
*net
, u_int8_t netbits
,
273 u_int8_t protocol
, u_int16_t port
);
276 * Create a traffic selector for host-to-host cases.
278 * For host2host or virtual IP setups, the traffic selectors gets
279 * created at runtime using the external/virtual IP. Using this constructor,
280 * a call to set_address() sets this traffic selector to the supplied host.
283 * @param protocol upper layer protocl to allow
284 * @param from_port start of allowed port range
285 * @param to_port end of range
287 * - traffic_selector_t object
288 * - NULL if type not supported
290 traffic_selector_t
*traffic_selector_create_dynamic(u_int8_t protocol
,
291 u_int16_t from_port
, u_int16_t to_port
);
294 * Get printf hooks for a traffic selector.
297 * traffic_selector_t *ts
298 * With the #-specifier, arguments are:
299 * linked_list_t *list containing traffic_selector_t*
301 printf_hook_functions_t
traffic_selector_get_printf_hooks();
303 #endif /* TRAFFIC_SELECTOR_H_ @} */