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 gets allocated.
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 gets allocated.
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 * Update the address of a traffic selector.
160 * Update the address range of a traffic selector, if it is
161 * constructed with the traffic_selector_create_dynamic().
163 * @param host host_t specifying the address
165 void (*set_address
) (traffic_selector_t
*this, host_t
* host
);
168 * Compare two traffic selectors for equality.
170 * @param other ts to compare with this
171 * @return pointer to a string.
173 bool (*equals
) (traffic_selector_t
*this, traffic_selector_t
*other
);
176 * Check if a traffic selector is contained completly in another.
178 * contains() allows to check if multiple traffic selectors are redundant.
180 * @param other ts that contains this
181 * @return TRUE if other contains this completly, FALSE otherwise
183 bool (*is_contained_in
) (traffic_selector_t
*this, traffic_selector_t
*other
);
186 * Check if a specific host is included in the address range of
187 * this traffic selector.
189 * @param host the host to check
191 bool (*includes
) (traffic_selector_t
*this, host_t
*host
);
194 * Convert a traffic selector address range to a subnet
196 * If from and to ports of this traffic selector are equal,
197 * the port of the returned host_t is set to that port.
199 * @param net converted subnet (has to be freed)
200 * @param mask converted net mask
202 void (*to_subnet
) (traffic_selector_t
*this, host_t
**net
, u_int8_t
*mask
);
205 * Destroys the ts object
207 void (*destroy
) (traffic_selector_t
*this);
211 * Create a new traffic selector using human readable params.
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
220 * - traffic_selector_t object
221 * - NULL if invalid address strings/protocol
223 traffic_selector_t
*traffic_selector_create_from_string(
224 u_int8_t protocol
, ts_type_t type
,
225 char *from_addr
, u_int16_t from_port
,
226 char *to_addr
, u_int16_t to_port
);
229 * Create a new traffic selector using data read from the net.
231 * There exists a mix of network and host order in the params.
232 * But the parser gives us this data in this format, so we
233 * don't have to convert twice.
235 * @param protocol protocol for this ts, such as TCP or UDP
236 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
237 * @param from_address start of address range, network order
238 * @param from_port port number, host order
239 * @param to_address end of address range, network order
240 * @param to_port port number, host order
241 * @return traffic_selector_t object
243 traffic_selector_t
*traffic_selector_create_from_bytes(
244 u_int8_t protocol
, ts_type_t type
,
245 chunk_t from_address
, u_int16_t from_port
,
246 chunk_t to_address
, u_int16_t to_port
);
249 * Create a new traffic selector defining a whole subnet.
251 * In most cases, definition of a traffic selector for full subnets
252 * is sufficient. This constructor creates a traffic selector for
253 * all protocols, all ports and the address range specified by the
255 * Additionally, a protocol and a port may be specified. Port ranges
256 * are not supported via this constructor.
258 * @param net subnet to use
259 * @param netbits size of the subnet, as used in e.g. 192.168.0.0/24 notation
261 * - traffic_selector_t object
262 * - NULL if address family of net not supported
264 traffic_selector_t
*traffic_selector_create_from_subnet(
265 host_t
*net
, u_int8_t netbits
,
266 u_int8_t protocol
, u_int16_t port
);
269 * Create a traffic selector for host-to-host cases.
271 * For host2host or virtual IP setups, the traffic selectors gets
272 * created at runtime using the external/virtual IP. Using this constructor,
273 * a call to set_address() sets this traffic selector to the supplied host.
276 * @param protocol upper layer protocl to allow
277 * @param from_port start of allowed port range
278 * @param to_port end of range
280 * - traffic_selector_t object
281 * - NULL if type not supported
283 traffic_selector_t
*traffic_selector_create_dynamic(u_int8_t protocol
,
284 u_int16_t from_port
, u_int16_t to_port
);
287 * Get printf hooks for a traffic selector.
290 * traffic_selector_t *ts
291 * With the #-specifier, arguments are:
292 * linked_list_t *list containing traffic_selector_t*
294 printf_hook_functions_t
traffic_selector_get_printf_hooks();
296 #endif /* TRAFFIC_SELECTOR_H_ @} */