updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / traffic_selector_substructure.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "traffic_selector_substructure.h"
20
21 #include <encoding/payloads/encodings.h>
22 #include <utils/linked_list.h>
23
24
25 typedef struct private_traffic_selector_substructure_t private_traffic_selector_substructure_t;
26
27 /**
28 * Private data of an traffic_selector_substructure_t object.
29 *
30 */
31 struct private_traffic_selector_substructure_t {
32 /**
33 * Public traffic_selector_substructure_t interface.
34 */
35 traffic_selector_substructure_t public;
36
37 /**
38 * Type of traffic selector.
39 */
40 u_int8_t ts_type;
41
42 /**
43 * IP Protocol ID.
44 */
45 u_int8_t ip_protocol_id;
46
47 /**
48 * Length of this payload.
49 */
50 u_int16_t payload_length;
51
52 /**
53 * Start port number.
54 */
55 u_int16_t start_port;
56
57 /**
58 * End port number.
59 */
60 u_int16_t end_port;
61
62 /**
63 * Starting address.
64 */
65 chunk_t starting_address;
66
67 /**
68 * Ending address.
69 */
70 chunk_t ending_address;
71 };
72
73 /**
74 * Encoding rules to parse or generate a TS payload
75 *
76 * The defined offsets are the positions in a object of type
77 * private_traffic_selector_substructure_t.
78 *
79 */
80 encoding_rule_t traffic_selector_substructure_encodings[] = {
81 /* 1 Byte next ts type*/
82 { TS_TYPE, offsetof(private_traffic_selector_substructure_t, ts_type) },
83 /* 1 Byte IP protocol id*/
84 { U_INT_8, offsetof(private_traffic_selector_substructure_t, ip_protocol_id) },
85 /* Length of the whole payload*/
86 { PAYLOAD_LENGTH, offsetof(private_traffic_selector_substructure_t, payload_length) },
87 /* 2 Byte start port*/
88 { U_INT_16, offsetof(private_traffic_selector_substructure_t, start_port) },
89 /* 2 Byte end port*/
90 { U_INT_16, offsetof(private_traffic_selector_substructure_t, end_port) },
91 /* starting address is either 4 or 16 byte */
92 { ADDRESS, offsetof(private_traffic_selector_substructure_t, starting_address) },
93 /* ending address is either 4 or 16 byte */
94 { ADDRESS, offsetof(private_traffic_selector_substructure_t, ending_address) }
95
96 };
97
98 /*
99 1 2 3
100 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 ! TS Type !IP Protocol ID*| Selector Length |
103 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 | Start Port* | End Port* |
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ! !
107 ~ Starting Address* ~
108 ! !
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 ! !
111 ~ Ending Address* ~
112 ! !
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 */
115
116 /**
117 * Implementation of payload_t.verify.
118 */
119 static status_t verify(private_traffic_selector_substructure_t *this)
120 {
121 if (this->start_port > this->end_port)
122 {
123 return FAILED;
124 }
125 switch (this->ts_type)
126 {
127 case TS_IPV4_ADDR_RANGE:
128 {
129 if ((this->starting_address.len != 4) ||
130 (this->ending_address.len != 4))
131 {
132 /* ipv4 address must be 4 bytes long */
133 return FAILED;
134 }
135 break;
136 }
137 case TS_IPV6_ADDR_RANGE:
138 {
139 if ((this->starting_address.len != 16) ||
140 (this->ending_address.len != 16))
141 {
142 /* ipv6 address must be 16 bytes long */
143 return FAILED;
144 }
145 break;
146 }
147 default:
148 {
149 /* not supported ts type */
150 return FAILED;
151 }
152 }
153
154 return SUCCESS;
155 }
156
157 /**
158 * Implementation of traffic_selector_substructure_t.get_encoding_rules.
159 */
160 static void get_encoding_rules(private_traffic_selector_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
161 {
162 *rules = traffic_selector_substructure_encodings;
163 *rule_count = sizeof(traffic_selector_substructure_encodings) / sizeof(encoding_rule_t);
164 }
165
166 /**
167 * Implementation of payload_t.get_type.
168 */
169 static payload_type_t get_payload_type(private_traffic_selector_substructure_t *this)
170 {
171 return TRAFFIC_SELECTOR_SUBSTRUCTURE;
172 }
173
174 /**
175 * Implementation of payload_t.get_next_type.
176 */
177 static payload_type_t get_next_type(private_traffic_selector_substructure_t *this)
178 {
179 return 0;
180 }
181
182 /**
183 * Implementation of payload_t.set_next_type.
184 */
185 static void set_next_type(private_traffic_selector_substructure_t *this,payload_type_t type)
186 {
187
188 }
189
190 /**
191 * Implementation of payload_t.get_length.
192 */
193 static size_t get_length(private_traffic_selector_substructure_t *this)
194 {
195 return this->payload_length;
196 }
197
198 /**
199 * Implementation of traffic_selector_substructure_t.get_traffic_selector.
200 */
201 static traffic_selector_t *get_traffic_selector(private_traffic_selector_substructure_t *this)
202 {
203 traffic_selector_t *ts;
204 ts = traffic_selector_create_from_bytes(this->ip_protocol_id, this->ts_type,
205 this->starting_address, this->start_port,
206 this->ending_address, this->end_port);
207 return ts;
208 }
209
210 /**
211 * recompute length field of the payload
212 */
213 void compute_length(private_traffic_selector_substructure_t *this)
214 {
215 this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH +
216 this->ending_address.len + this->starting_address.len;
217 }
218
219 /**
220 * Implementation of payload_t.destroy and traffic_selector_substructure_t.destroy.
221 */
222 static void destroy(private_traffic_selector_substructure_t *this)
223 {
224 free(this->starting_address.ptr);
225 free(this->ending_address.ptr);
226 free(this);
227 }
228
229 /*
230 * Described in header
231 */
232 traffic_selector_substructure_t *traffic_selector_substructure_create()
233 {
234 private_traffic_selector_substructure_t *this = malloc_thing(private_traffic_selector_substructure_t);
235
236 /* interface functions */
237 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
238 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
239 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
240 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
241 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
242 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
243 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
244
245 /* public functions */
246 this->public.get_traffic_selector = (traffic_selector_t* (*)(traffic_selector_substructure_t*))get_traffic_selector;
247 this->public.destroy = (void (*) (traffic_selector_substructure_t *)) destroy;
248
249 /* private variables */
250 this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH;
251 this->start_port = 0;
252 this->end_port = 0;
253 this->starting_address = chunk_empty;
254 this->ending_address = chunk_empty;
255 this->ip_protocol_id = 0;
256 /* must be set to be valid */
257 this->ts_type = TS_IPV4_ADDR_RANGE;
258
259 return (&(this->public));
260 }
261
262 /*
263 * Described in header
264 */
265 traffic_selector_substructure_t *traffic_selector_substructure_create_from_traffic_selector(traffic_selector_t *traffic_selector)
266 {
267 private_traffic_selector_substructure_t *this = (private_traffic_selector_substructure_t*)traffic_selector_substructure_create();
268 this->ts_type = traffic_selector->get_type(traffic_selector);
269 this->ip_protocol_id = traffic_selector->get_protocol(traffic_selector);
270 this->start_port = traffic_selector->get_from_port(traffic_selector);
271 this->end_port = traffic_selector->get_to_port(traffic_selector);
272 this->starting_address = chunk_clone(traffic_selector->get_from_address(traffic_selector));
273 this->ending_address = chunk_clone(traffic_selector->get_to_address(traffic_selector));
274
275 compute_length(this);
276
277 return &(this->public);
278 }