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