553a73e66ddceaaa26e95504c30503c9d305551c
[strongswan.git] / Source / 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 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef _TRAFFIC_SELECTOR_H_
24 #define _TRAFFIC_SELECTOR_H_
25
26 #include <types.h>
27 #include <network/host.h>
28
29 typedef enum ts_type_t ts_type_t;
30
31 /**
32 * Traffic selector types.
33 *
34 * @ingroup config
35 */
36 enum ts_type_t {
37
38 /**
39 * A range of IPv4 addresses, represented by two four (4) octet
40 * values. The first value is the beginning IPv4 address
41 * (inclusive) and the second value is the ending IPv4 address
42 * (inclusive). All addresses falling between the two specified
43 * addresses are considered to be within the list.
44 */
45 TS_IPV4_ADDR_RANGE = 7,
46
47 /**
48 * A range of IPv6 addresses, represented by two sixteen (16)
49 * octet values. The first value is the beginning IPv6 address
50 * (inclusive) and the second value is the ending IPv6 address
51 * (inclusive). All addresses falling between the two specified
52 * addresses are considered to be within the list.
53 */
54 TS_IPV6_ADDR_RANGE = 8
55 };
56
57 /**
58 * string mappings for ts_type_t
59 */
60 extern mapping_t ts_type_m[];
61
62
63 typedef struct traffic_selector_t traffic_selector_t;
64
65 /**
66 * @brief Object representing a traffic selector entry.
67 *
68 * A traffic selector defines an range of addresses
69 * and a range of ports.
70 *
71 * @b Constructors:
72 * - traffic_selector_create_from_bytes()
73 * - traffic_selector_create_from_string()
74 *
75 * @ingroup config
76 */
77 struct traffic_selector_t {
78
79 /**
80 * @brief Compare two traffic selectors, and create a new one
81 * which is the largest subset of both (subnet & port).
82 *
83 * Resulting traffic_selector is newly created and must be destroyed.
84 *
85 * @param this first to compare
86 * @param other second to compare
87 * @return
88 * - created subset of them
89 * - or NULL if no match between this and other
90 */
91 traffic_selector_t *(*get_subset) (traffic_selector_t *this, traffic_selector_t *other);
92
93 /**
94 * @brief Clone a traffic selector.
95 *
96 * @param this traffic selector to clone
97 * @return clone of it
98 */
99 traffic_selector_t *(*clone) (traffic_selector_t *this);
100
101 /**
102 * @brief Get starting address of this ts as a chunk.
103 *
104 * Data is in network order and represents the address.
105 * Size depends on protocol.
106 *
107 * Resulting chunk data is allocated and must be freed!
108 *
109 * @param this calling object
110 * @return chunk containing the address
111 */
112 chunk_t (*get_from_address) (traffic_selector_t *this);
113
114 /**
115 * @brief Get ending address of this ts as a chunk.
116 *
117 * Data is in network order and represents the address.
118 * Size depends on protocol.
119 *
120 * Resulting chunk data is allocated and must be freed!
121 *
122 * @param this calling object
123 * @return chunk containing the address
124 */
125 chunk_t (*get_to_address) (traffic_selector_t *this);
126
127 /**
128 * @brief Get starting port of this ts.
129 *
130 * Port is in host order, since the parser converts it.
131 * Size depends on protocol.
132 *
133 * @param this calling object
134 * @return port
135 */
136 u_int16_t (*get_from_port) (traffic_selector_t *this);
137
138 /**
139 * @brief Get ending port of this ts.
140 *
141 * Port is in host order, since the parser converts it.
142 * Size depends on protocol.
143 *
144 * @param this calling object
145 * @return port
146 */
147 u_int16_t (*get_to_port) (traffic_selector_t *this);
148
149 /**
150 * @brief Get the type of the traffic selector.
151 *
152 * @param this calling obect
153 * @return ts_type_t specifying the type
154 */
155 ts_type_t (*get_type) (traffic_selector_t *this);
156
157 /**
158 * @brief Get the protocol id of this ts.
159 *
160 * @param this calling obect
161 * @return protocol id
162 */
163 u_int8_t (*get_protocol) (traffic_selector_t *this);
164
165 /**
166 * @brief Destroys the ts object
167 *
168 * @param this calling object
169 */
170 void (*destroy) (traffic_selector_t *this);
171 };
172
173 /**
174 * @brief Create a new traffic selector using human readable params.
175 *
176 * @param protocol protocol for this ts, such as TCP or UDP
177 * @param type type of following addresses, such as TS_IPV4_ADDR_RANGE
178 * @param from_addr start of address range as string
179 * @param from_port port number in host order
180 * @param to_addr end of address range as string
181 * @param to_port port number in host order
182 * @return
183 * - traffic_selector_t object
184 * - NULL if invalid address strings/protocol
185 *
186 * @ingroup config
187 */
188 traffic_selector_t *traffic_selector_create_from_string(u_int8_t protocol, ts_type_t type, char *from_addr, u_int16_t from_port, char *to_addr, u_int16_t to_port);
189
190 /**
191 * @brief Create a new traffic selector using data read from the net.
192 *
193 * There exists a mix of network and host order in the params.
194 * But the parser gives us this data in this format, so we
195 * don't have to convert twice.
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_address start of address range, network order
200 * @param from_port port number, host order
201 * @param to_address end of address range as string, network
202 * @param to_port port number, host order
203 * @return
204 * - traffic_selector_t object
205 * - NULL if invalid address input/protocol
206 *
207 * @ingroup config
208 */
209 traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_type_t type, chunk_t from_address, int16_t from_port, chunk_t to_address, u_int16_t to_port);
210
211 #endif //_TRAFFIC_SELECTOR_H_