- implemented generate-method
[strongswan.git] / Source / charon / message.c
1 /**
2 * @file message.c
3 *
4 * @brief Class message_t. Object of this type represents an IKEv2-Message.
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 #include <stdlib.h>
24
25 #include "message.h"
26
27 #include "types.h"
28 #include "globals.h"
29 #include "ike_sa_id.h"
30 #include "generator.h"
31 #include "utils/linked_list.h"
32 #include "utils/allocator.h"
33 #include "utils/logger_manager.h"
34 #include "payloads/encodings.h"
35 #include "payloads/payload.h"
36
37 /**
38 * Entry for a payload in the internal used linked list
39 *
40 */
41 typedef struct payload_entry_s payload_entry_t;
42
43 struct payload_entry_s {
44 /**
45 * Type of payload
46 */
47 payload_type_t payload_type;
48 /**
49 * Data struct holding the data of given payload
50 */
51 void *data_struct;
52 };
53
54
55 /**
56 * Private data of an message_t object
57 */
58 typedef struct private_message_s private_message_t;
59
60 struct private_message_s {
61
62 /**
63 * Public part of a message_t object
64 */
65 message_t public;
66
67
68 /**
69 * Assigned exchange type
70 */
71 exchange_type_t exchange_type;
72
73 /**
74 * TRUE if message is from original initiator, FALSE otherwise.
75 */
76 bool original_initiator;
77
78 /**
79 * TRUE if message is request.
80 * FALSE if message is reply.
81 */
82 bool is_request;
83
84 /**
85 * Message ID of this message
86 */
87 u_int32_t message_id;
88
89 /**
90 * ID of assigned IKE_SA
91 */
92 ike_sa_id_t *ike_sa_id;
93
94 /**
95 * Assigned UDP packet.
96 *
97 * Stores incoming packet or last generated one.
98 */
99 packet_t *packet;
100
101 /**
102 * Linked List where payload data are stored in
103 */
104 linked_list_t *payloads;
105
106 /**
107 * logger for this message
108 */
109 logger_t *logger;
110
111 /**
112 * destination of this message
113 */
114
115 };
116
117
118 /**
119 * Implements message_t's set_ike_sa_id function.
120 * See #message_s.set_ike_sa_id.
121 */
122 static status_t set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
123 {
124 status_t status;
125 status = ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
126 return status;
127 }
128
129 /**
130 * Implements message_t's get_ike_sa_id function.
131 * See #message_s.get_ike_sa_id.
132 */
133 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
134 {
135 status_t status;
136 if (this->ike_sa_id == NULL)
137 {
138 return FAILED;
139 }
140 status = this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
141 return status;
142 }
143
144
145 /**
146 * Implements message_t's set_message_id function.
147 * See #message_s.set_message_id.
148 */
149 static status_t set_message_id (private_message_t *this,u_int32_t message_id)
150 {
151 this->message_id = message_id;
152 return SUCCESS;
153 }
154
155
156 /**
157 * Implements message_t's set_message_id function.
158 * See #message_s.set_message_id.
159 */
160 static u_int32_t get_message_id (private_message_t *this)
161 {
162 return this->message_id;
163 }
164
165
166 /**
167 * Implements message_t's set_exchange_type function.
168 * See #message_s.set_exchange_type.
169 */
170 static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
171 {
172 this->exchange_type = exchange_type;
173 return SUCCESS;
174 }
175
176
177 /**
178 * Implements message_t's get_exchange_type function.
179 * See #message_s.get_exchange_type.
180 */
181 static exchange_type_t get_exchange_type (private_message_t *this)
182 {
183 return this->exchange_type;
184 }
185
186 /**
187 * Implements message_t's set_original_initiator function.
188 * See #message_s.set_original_initiator.
189 */
190 static status_t set_original_initiator (private_message_t *this,bool original_initiator)
191 {
192 this->original_initiator = original_initiator;
193 return SUCCESS;
194 }
195
196 /**
197 * Implements message_t's get_original_initiator function.
198 * See #message_s.get_original_initiator.
199 */
200 static exchange_type_t get_original_initiator (private_message_t *this)
201 {
202 return this->original_initiator;
203 }
204
205 /**
206 * Implements message_t's set_request function.
207 * See #message_s.set_request.
208 */
209 static status_t set_request (private_message_t *this,bool request)
210 {
211 this->is_request = request;
212 return SUCCESS;
213 }
214
215 /**
216 * Implements message_t's get_request function.
217 * See #message_s.get_request.
218 */
219 static exchange_type_t get_request (private_message_t *this)
220 {
221 return this->is_request;
222 }
223
224 static status_t add_payload(private_message_t *this, payload_t *payload)
225 {
226 if (this->payloads->insert_last(this->payloads, payload) != SUCCESS)
227 {
228 return OUT_OF_RES;
229 }
230 return SUCCESS;
231 }
232
233 static status_t set_source(private_message_t *this, host_t *host)
234 {
235 if (this->packet->source != NULL)
236 {
237 this->packet->source->destroy(this->packet->source);
238 }
239 this->packet->source = host;
240 return SUCCESS;
241 }
242
243 static status_t set_destination(private_message_t *this, host_t *host)
244 {
245 if (this->packet->destination != NULL)
246 {
247 this->packet->destination->destroy(this->packet->destination);
248 }
249 this->packet->destination = host;
250 return SUCCESS;
251 }
252
253 static status_t get_source(private_message_t *this, host_t **host)
254 {
255 *host = this->packet->source;
256 return SUCCESS;
257 }
258
259 static status_t get_destination(private_message_t *this, host_t **host)
260 {
261 *host = this->packet->destination;
262 return SUCCESS;
263 }
264
265
266 /**
267 * Implements message_t's generate function.
268 * See #message_s.generate.
269 */
270 static status_t generate(private_message_t *this, packet_t **packet)
271 {
272 generator_t *generator;
273 ike_header_t *ike_header;
274 payload_t *payload, *next_payload;
275 linked_list_iterator_t *iterator;
276 spi_t initiator_spi, responder_spi;
277 bool is_initiator;
278 status_t status;
279
280 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
281 {
282 return INVALID_STATE;
283 }
284
285 if (this->packet->source == NULL ||
286 this->packet->destination == NULL)
287 {
288 return INVALID_STATE;
289 }
290
291 ike_header = ike_header_create();
292 if (ike_header == NULL)
293 {
294 return OUT_OF_RES;
295 }
296
297 this->ike_sa_id->get_values(this->ike_sa_id, &initiator_spi, &responder_spi, &is_initiator);
298
299 ike_header->set_exchange_type(ike_header, this->exchange_type);
300 ike_header->set_initiator_flag(ike_header, this->original_initiator);
301 ike_header->set_message_id(ike_header, this->message_id);
302 ike_header->set_response_flag(ike_header, !this->is_request);
303 ike_header->set_initiator_flag(ike_header, is_initiator);
304 ike_header->set_initiator_spi(ike_header, initiator_spi);
305 ike_header->set_initiator_spi(ike_header, responder_spi);
306
307 generator = generator_create();
308
309 payload = (payload_t*)ike_header;
310
311 if (this->payloads->create_iterator(this->payloads, &iterator, TRUE) != SUCCESS)
312 {
313 generator->destroy(generator);
314 ike_header->destroy(ike_header);
315 return OUT_OF_RES;
316 }
317 while(iterator->has_next(iterator))
318 {
319 iterator->current(iterator, (void**)&next_payload);
320 payload->set_next_type(payload, next_payload->get_type(next_payload));
321 status = generator->generate_payload(generator, payload);
322 if (status != SUCCESS)
323 {
324 generator->destroy(generator);
325 ike_header->destroy(ike_header);
326 return status;
327 }
328 payload = next_payload;
329 }
330 iterator->destroy(iterator);
331
332 payload->set_next_type(payload, NO_PAYLOAD);
333 status = generator->generate_payload(generator, payload);
334 if (status != SUCCESS)
335 {
336 generator->destroy(generator);
337 ike_header->destroy(ike_header);
338 return status;
339 }
340
341 ike_header->destroy(ike_header);
342
343
344
345 if (this->packet->data.ptr != NULL)
346 {
347 allocator_free(this->packet->data.ptr);
348 }
349
350 status = generator->write_to_chunk(generator, &(this->packet->data));
351 if (status != SUCCESS)
352 {
353 generator->destroy(generator);
354 return status;
355 }
356
357 this->packet->clone(this->packet, packet);
358
359 generator->destroy(generator);
360 return SUCCESS;
361 }
362
363 /**
364 * Implements message_t's destroy function.
365 * See #message_s.destroy.
366 */
367 static status_t destroy (private_message_t *this)
368 {
369 linked_list_iterator_t *iterator;
370
371 if (this->packet != NULL)
372 {
373 this->packet->destroy(this->packet);
374 }
375 if (this->ike_sa_id != NULL)
376 {
377 this->ike_sa_id->destroy(this->ike_sa_id);
378 }
379
380 this->payloads->create_iterator(this->payloads, &iterator, TRUE);
381 while (iterator->has_next(iterator))
382 {
383 payload_t *payload;
384 iterator->current(iterator, (void**)&payload);
385 this->logger->log(this->logger, CONTROL_MORE, "Destroying payload of type %s",
386 mapping_find(payload_type_m, payload->get_type(payload)));
387 payload->destroy(payload);
388 }
389 iterator->destroy(iterator);
390 this->payloads->destroy(this->payloads);
391 allocator_free(this);
392 return SUCCESS;
393 }
394
395 /*
396 * Described in Header-File
397 */
398 message_t *message_create_from_packet(packet_t *packet)
399 {
400 private_message_t *this = allocator_alloc_thing(private_message_t);
401 if (this == NULL)
402 {
403 return NULL;
404 }
405
406 /* public functions */
407 this->public.set_message_id = (status_t(*)(message_t*, u_int32_t))set_message_id;
408 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
409 this->public.set_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
410 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
411 this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
412 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
413 this->public.set_original_initiator = (status_t(*)(message_t*, bool))set_original_initiator;
414 this->public.get_original_initiator = (bool(*)(message_t*))get_original_initiator;
415 this->public.set_request = (status_t(*)(message_t*, bool))set_request;
416 this->public.get_request = (bool(*)(message_t*))get_request;
417 this->public.add_payload = (status_t(*)(message_t*,payload_t*))add_payload;
418 this->public.generate = (status_t (*) (message_t *, packet_t**)) generate;
419 this->public.set_source = (status_t (*) (message_t*,host_t*)) set_source;
420 this->public.get_source = (status_t (*) (message_t*,host_t**)) get_source;
421 this->public.set_destination = (status_t (*) (message_t*,host_t*)) set_destination;
422 this->public.get_destination = (status_t (*) (message_t*,host_t**)) get_destination;
423 this->public.destroy = (status_t(*)(message_t*))destroy;
424
425 /* public values */
426 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
427 this->original_initiator = TRUE;
428 this->is_request = TRUE;
429 this->ike_sa_id = NULL;
430 this->message_id = 0;
431
432 /* private values */
433 if (packet == NULL)
434 {
435 packet = packet_create();
436 }
437 if (packet == NULL)
438 {
439 allocator_free(this);
440 return NULL;
441 }
442 this->packet = packet;
443 this->payloads = linked_list_create();
444 if (this->payloads == NULL)
445 {
446 allocator_free(this);
447 return NULL;
448 }
449
450 this->logger = global_logger_manager->create_logger(global_logger_manager, MESSAGE, NULL);
451 if (this->logger == NULL)
452 {
453 this->payloads->destroy(this->payloads);
454 allocator_free(this);
455 }
456
457 return (&this->public);
458 }
459
460 /*
461 * Described in Header-File
462 */
463 message_t *message_create()
464 {
465 return message_create_from_packet(NULL);
466 }