27fd7f5d5184773b769b408c56c881700239c1b5
[strongswan.git] / Source / charon / encoding / 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 <sa/ike_sa_id.h>
30 #include <encoding/generator.h>
31 #include <encoding/parser.h>
32 #include <utils/linked_list.h>
33 #include <utils/allocator.h>
34 #include <utils/logger_manager.h>
35 #include <encoding/payloads/encodings.h>
36 #include <encoding/payloads/payload.h>
37
38
39 typedef struct supported_payload_entry_t supported_payload_entry_t;
40
41 /**
42 * Supported payload entry used in message_rule_t
43 *
44 */
45 struct supported_payload_entry_t {
46 /**
47 * Payload type
48 */
49 payload_type_t payload_type;
50
51 /**
52 * Minimal occurence of this payload
53 */
54 size_t min_occurence;
55
56 /**
57 * Max occurence of this payload
58 */
59 size_t max_occurence;
60 };
61
62 typedef struct message_rule_t message_rule_t;
63
64 /**
65 * Message Rule used to find out which payloads are supported by each message type
66 *
67 */
68 struct message_rule_t {
69 /**
70 * Type of message
71 */
72 exchange_type_t exchange_type;
73
74 /**
75 * Is message a request or response
76 */
77 bool is_request;
78 /**
79 * Number of supported payloads
80 */
81 size_t supported_payloads_count;
82 /**
83 * Pointer to first supported payload entry
84 */
85 supported_payload_entry_t *supported_payloads;
86 };
87
88 /**
89 * message rule for ike_sa_init from initiator
90 */
91 static supported_payload_entry_t supported_ike_sa_init_i_payloads[] =
92 {
93 {SECURITY_ASSOCIATION,1,1},
94 {KEY_EXCHANGE,1,1},
95 {NONCE,1,1},
96 };
97
98 /**
99 * message rule for ike_sa_init from responder
100 */
101 static supported_payload_entry_t supported_ike_sa_init_r_payloads[] =
102 {
103 {SECURITY_ASSOCIATION,1,1},
104 {KEY_EXCHANGE,1,1},
105 {NONCE,1,1},
106 };
107
108
109 /**
110 * message rules, defines allowed payloads
111 */
112 static message_rule_t message_rules[] = {
113 {IKE_SA_INIT,TRUE,(sizeof(supported_ike_sa_init_i_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_i_payloads},
114 {IKE_SA_INIT,FALSE,(sizeof(supported_ike_sa_init_r_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_r_payloads}
115 };
116
117 typedef struct payload_entry_t payload_entry_t;
118
119 /**
120 * Entry for a payload in the internal used linked list
121 *
122 */
123 struct payload_entry_t {
124 /**
125 * Type of payload
126 */
127 payload_type_t payload_type;
128 /**
129 * Data struct holding the data of given payload
130 */
131 void *data_struct;
132 };
133
134
135 typedef struct private_message_t private_message_t;
136
137 /**
138 * Private data of an message_t object
139 */
140 struct private_message_t {
141
142 /**
143 * Public part of a message_t object
144 */
145 message_t public;
146
147
148 /**
149 * Minor version of message
150 */
151 u_int8_t major_version;
152
153 /**
154 * Major version of message
155 */
156 u_int8_t minor_version;
157
158 /**
159 * First Payload in message
160 */
161 payload_type_t first_payload;
162
163 /**
164 * Assigned exchange type
165 */
166 exchange_type_t exchange_type;
167
168
169 /**
170 * TRUE if message is request.
171 * FALSE if message is reply.
172 */
173 bool is_request;
174
175 /**
176 * Message ID of this message
177 */
178 u_int32_t message_id;
179
180 /**
181 * ID of assigned IKE_SA
182 */
183 ike_sa_id_t *ike_sa_id;
184
185 /**
186 * Assigned UDP packet.
187 *
188 * Stores incoming packet or last generated one.
189 */
190 packet_t *packet;
191
192 /**
193 * Linked List where payload data are stored in
194 */
195 linked_list_t *payloads;
196
197 /**
198 * Assigned parser to parse Header and Body of this message
199 */
200 parser_t *parser;
201
202 /**
203 * logger for this message
204 */
205 logger_t *logger;
206
207 /**
208 * Gets a list of supported payloads of this message type
209 *
210 * @param this calling object
211 * @param[out] supported_payloads first entry of supported payloads
212 * @param[out] supported_payloads_count number of supported payload entries
213 *
214 * @return SUCCESS
215 * NOT_FOUND if no supported payload definition could be found
216 */
217 status_t (*get_supported_payloads) (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count);
218
219 };
220
221 /**
222 * Implements private_message_t's get_supported_payloads function.
223 * See #private_message_t.get_supported_payloads.
224 */
225 status_t get_supported_payloads (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count)
226 {
227 int i;
228 exchange_type_t exchange_type = this->public.get_exchange_type(&(this->public));
229 bool is_request = this->public.get_request(&(this->public));
230
231
232 for (i = 0; i < (sizeof(message_rules) / sizeof(message_rule_t)); i++)
233 {
234 if ((exchange_type == message_rules[i].exchange_type) &&
235 (is_request == message_rules[i].is_request))
236 {
237 /* found rule for given exchange_type*/
238 *supported_payloads = message_rules[i].supported_payloads;
239 *supported_payloads_count = message_rules[i].supported_payloads_count;
240
241 return SUCCESS;
242 }
243
244
245 }
246 *supported_payloads = NULL;
247 *supported_payloads_count = 0;
248 return NOT_FOUND;
249 }
250
251 /**
252 * Implements message_t's set_ike_sa_id function.
253 * See #message_s.set_ike_sa_id.
254 */
255 static status_t set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
256 {
257 status_t status;
258 status = ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
259 return status;
260 }
261
262 /**
263 * Implements message_t's get_ike_sa_id function.
264 * See #message_s.get_ike_sa_id.
265 */
266 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
267 {
268 status_t status;
269 if (this->ike_sa_id == NULL)
270 {
271 return FAILED;
272 }
273 status = this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
274 return status;
275 }
276
277
278 /**
279 * Implements message_t's set_message_id function.
280 * See #message_s.set_message_id.
281 */
282 static status_t set_message_id (private_message_t *this,u_int32_t message_id)
283 {
284 this->message_id = message_id;
285 return SUCCESS;
286 }
287
288
289 /**
290 * Implements message_t's set_message_id function.
291 * See #message_s.set_message_id.
292 */
293 static u_int32_t get_message_id (private_message_t *this)
294 {
295 return this->message_id;
296 }
297
298 /**
299 * Implements message_t's get_responder_spi function.
300 * See #message_s.get_responder_spi.
301 */
302 static u_int64_t get_responder_spi (private_message_t *this)
303 {
304 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
305 }
306
307 /**
308 * Implements message_t's set_major_version function.
309 * See #message_s.set_major_version.
310 */
311 static status_t set_major_version (private_message_t *this,u_int8_t major_version)
312 {
313 this->major_version = major_version;
314 return SUCCESS;
315 }
316
317
318 /**
319 * Implements message_t's get_major_version function.
320 * See #message_s.get_major_version.
321 */
322 static u_int8_t get_major_version (private_message_t *this)
323 {
324 return this->major_version;
325 }
326
327 /**
328 * Implements message_t's set_minor_version function.
329 * See #message_s.set_minor_version.
330 */
331 static status_t set_minor_version (private_message_t *this,u_int8_t minor_version)
332 {
333 this->minor_version = minor_version;
334 return SUCCESS;
335 }
336
337
338 /**
339 * Implements message_t's get_minor_version function.
340 * See #message_s.get_minor_version.
341 */
342 static u_int8_t get_minor_version (private_message_t *this)
343 {
344 return this->minor_version;
345 }
346
347 /**
348 * Implements message_t's set_exchange_type function.
349 * See #message_s.set_exchange_type.
350 */
351 static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
352 {
353 this->exchange_type = exchange_type;
354 return SUCCESS;
355 }
356
357
358 /**
359 * Implements message_t's get_exchange_type function.
360 * See #message_s.get_exchange_type.
361 */
362 static exchange_type_t get_exchange_type (private_message_t *this)
363 {
364 return this->exchange_type;
365 }
366
367
368 /**
369 * Implements message_t's set_request function.
370 * See #message_s.set_request.
371 */
372 static status_t set_request (private_message_t *this,bool request)
373 {
374 this->is_request = request;
375 return SUCCESS;
376 }
377
378 /**
379 * Implements message_t's get_request function.
380 * See #message_s.get_request.
381 */
382 static exchange_type_t get_request (private_message_t *this)
383 {
384 return this->is_request;
385 }
386
387 static status_t add_payload(private_message_t *this, payload_t *payload)
388 {
389 payload_t *last_payload;
390 if ((this->payloads->get_count(this->payloads) > 0) &&
391 (this->payloads->get_last(this->payloads,(void **) &last_payload) != SUCCESS))
392 {
393 return OUT_OF_RES;
394 }
395
396 if (this->payloads->insert_last(this->payloads, payload) != SUCCESS)
397 {
398 return OUT_OF_RES;
399 }
400 if (this->payloads->get_count(this->payloads) == 1)
401 {
402 this->first_payload = payload->get_type(payload);
403 }
404 else
405 {
406 last_payload->set_next_type(last_payload,payload->get_type(payload));
407 }
408
409 this->logger->log(this->logger, CONTROL|MORE, "added payload of type %s to message",
410 mapping_find(payload_type_m, payload->get_type(payload)));
411
412 return SUCCESS;
413 }
414
415 static status_t set_source(private_message_t *this, host_t *host)
416 {
417 if (this->packet->source != NULL)
418 {
419 this->packet->source->destroy(this->packet->source);
420 }
421 this->packet->source = host;
422 return SUCCESS;
423 }
424
425 static status_t set_destination(private_message_t *this, host_t *host)
426 {
427 if (this->packet->destination != NULL)
428 {
429 this->packet->destination->destroy(this->packet->destination);
430 }
431 this->packet->destination = host;
432 return SUCCESS;
433 }
434
435 static status_t get_source(private_message_t *this, host_t **host)
436 {
437 *host = this->packet->source;
438 return SUCCESS;
439 }
440
441 static status_t get_destination(private_message_t *this, host_t **host)
442 {
443 *host = this->packet->destination;
444 return SUCCESS;
445 }
446
447
448 static status_t get_payload_iterator(private_message_t *this, iterator_t **iterator)
449 {
450 return this->payloads->create_iterator(this->payloads, iterator, TRUE);
451 }
452
453
454 /**
455 * Implements message_t's generate function.
456 * See #message_s.generate.
457 */
458 static status_t generate(private_message_t *this, packet_t **packet)
459 {
460 generator_t *generator;
461 ike_header_t *ike_header;
462 payload_t *payload, *next_payload;
463 iterator_t *iterator;
464 status_t status;
465
466
467 this->logger->log(this->logger, CONTROL, "generating message, contains %d payloads",
468 this->payloads->get_count(this->payloads));
469
470 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
471 {
472 this->logger->log(this->logger, ERROR, "exchange type is not defined");
473 return INVALID_STATE;
474 }
475
476 if (this->packet->source == NULL ||
477 this->packet->destination == NULL)
478 {
479 this->logger->log(this->logger, ERROR, "source/destination not defined");
480 return INVALID_STATE;
481 }
482
483
484 ike_header = ike_header_create();
485 if (ike_header == NULL)
486 {
487 return OUT_OF_RES;
488 }
489
490
491 ike_header->set_exchange_type(ike_header, this->exchange_type);
492 ike_header->set_message_id(ike_header, this->message_id);
493 ike_header->set_response_flag(ike_header, !this->is_request);
494 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
495 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
496 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
497
498 generator = generator_create();
499 if (generator == NULL)
500 {
501 return OUT_OF_RES;
502 }
503
504 payload = (payload_t*)ike_header;
505
506 if (this->payloads->create_iterator(this->payloads, &iterator, TRUE) != SUCCESS)
507 {
508 generator->destroy(generator);
509 ike_header->destroy(ike_header);
510 return OUT_OF_RES;
511 }
512 while(iterator->has_next(iterator))
513 {
514 iterator->current(iterator, (void**)&next_payload);
515 payload->set_next_type(payload, next_payload->get_type(next_payload));
516 status = generator->generate_payload(generator, payload);
517 if (status != SUCCESS)
518 {
519 generator->destroy(generator);
520 ike_header->destroy(ike_header);
521 return status;
522 }
523 payload = next_payload;
524 }
525 iterator->destroy(iterator);
526
527 payload->set_next_type(payload, NO_PAYLOAD);
528 status = generator->generate_payload(generator, payload);
529 if (status != SUCCESS)
530 {
531 generator->destroy(generator);
532 ike_header->destroy(ike_header);
533 return status;
534 }
535
536 ike_header->destroy(ike_header);
537
538
539
540 if (this->packet->data.ptr != NULL)
541 {
542 allocator_free(this->packet->data.ptr);
543 }
544
545 status = generator->write_to_chunk(generator, &(this->packet->data));
546 if (status != SUCCESS)
547 {
548 generator->destroy(generator);
549 return status;
550 }
551
552 this->packet->clone(this->packet, packet);
553
554 generator->destroy(generator);
555
556
557 this->logger->log(this->logger, CONTROL, "message generated successfully");
558 return SUCCESS;
559 }
560
561 /**
562 * Implements message_t's parse_header function.
563 * See #message_s.parse_header.
564 */
565 static status_t parse_header(private_message_t *this)
566 {
567 ike_header_t *ike_header;
568 status_t status;
569
570
571 this->logger->log(this->logger, CONTROL, "parsing header of message");
572
573 this->parser->reset_context(this->parser);
574 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
575 if (status != SUCCESS)
576 {
577 this->logger->log(this->logger, ERROR, "Header could not be parsed");
578 return status;
579
580 }
581
582 /* verify payload */
583 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
584 if (status != SUCCESS)
585 {
586 this->logger->log(this->logger, ERROR, "Header verification failed");
587 ike_header->destroy(ike_header);
588 return status;
589 }
590
591 if (this->ike_sa_id != NULL)
592 {
593 this->ike_sa_id->destroy(this->ike_sa_id);
594 }
595
596 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
597 ike_header->get_responder_spi(ike_header),
598 ike_header->get_initiator_flag(ike_header));
599 if (this->ike_sa_id == NULL)
600 {
601 this->logger->log(this->logger, ERROR, "could not create ike_sa_id object");
602 ike_header->destroy(ike_header);
603 return OUT_OF_RES;
604 }
605 this->exchange_type = ike_header->get_exchange_type(ike_header);
606 this->message_id = ike_header->get_message_id(ike_header);
607 this->is_request = (!(ike_header->get_response_flag(ike_header)));
608 this->major_version = ike_header->get_maj_version(ike_header);
609 this->minor_version = ike_header->get_min_version(ike_header);
610 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
611
612
613 this->logger->log(this->logger, CONTROL, "parsing header successfully");
614
615 ike_header->destroy(ike_header);
616 return SUCCESS;
617 }
618
619 /**
620 * Implements message_t's parse_body function.
621 * See #message_s.parse_body.
622 */
623 static status_t parse_body (private_message_t *this)
624 {
625 status_t status = SUCCESS;
626 int i;
627 payload_type_t current_payload_type = this->first_payload;
628 supported_payload_entry_t *supported_payloads;
629 size_t supported_payloads_count;
630
631
632 this->logger->log(this->logger, CONTROL, "parsing body of message");
633
634 if (this->get_supported_payloads (this, &supported_payloads, &supported_payloads_count) != SUCCESS)
635 {
636 this->logger->log(this->logger, ERROR, "could not get supported payloads");
637 return FAILED;
638 }
639
640 while (current_payload_type != NO_PAYLOAD)
641 {
642 payload_t *current_payload;
643 bool supported = FALSE;
644
645 this->logger->log(this->logger, CONTROL|MORE, "start parsing payload of type %s",
646 mapping_find(payload_type_m, current_payload_type));
647 for (i = 0; i < supported_payloads_count;i++)
648 {
649 if (supported_payloads[i].payload_type == current_payload_type)
650 {
651 supported = TRUE;
652 break;
653 }
654 }
655 if (!supported && (current_payload_type != NO_PAYLOAD))
656 {
657 /* type not supported */
658 status = NOT_SUPPORTED;
659 this->logger->log(this->logger, ERROR, "payload type %s not supported",mapping_find(payload_type_m,current_payload_type));
660 break;
661 }
662
663 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
664 if (status != SUCCESS)
665 {
666 this->logger->log(this->logger, ERROR, "payload type %s could not be parsed",mapping_find(payload_type_m,current_payload_type));
667 break;
668 }
669
670 status = current_payload->verify(current_payload);
671 if (status != SUCCESS)
672 {
673 this->logger->log(this->logger, ERROR, "payload type %s could not be verified",mapping_find(payload_type_m,current_payload_type));
674 status = VERIFY_ERROR;
675 break;
676 }
677
678 /* get next payload type */
679 current_payload_type = current_payload->get_next_type(current_payload);
680
681 status = this->payloads->insert_last(this->payloads,current_payload);
682 if (status != SUCCESS)
683 {
684 this->logger->log(this->logger, ERROR, "Could not insert current payload to internal list cause of ressource exhausting");
685 break;
686 }
687
688 }
689 if (status != SUCCESS)
690 {
691 /* already parsed payload is destroyed later in destroy call from outside this object */
692 }
693 else
694 {
695 iterator_t *iterator;
696
697 status = this->payloads->create_iterator(this->payloads,&iterator,TRUE);
698 if (status != SUCCESS)
699 {
700 this->logger->log(this->logger, ERROR, "Could not create iterator to check supported payloads");
701 return status;
702 }
703
704 /* check for payloads with wrong count*/
705 for (i = 0; i < supported_payloads_count;i++)
706 {
707 size_t min_occurence = supported_payloads[i].min_occurence;
708 size_t max_occurence = supported_payloads[i].max_occurence;
709 payload_type_t payload_type = supported_payloads[i].payload_type;
710 size_t found_payloads = 0;
711
712 iterator->reset(iterator);
713
714 while(iterator->has_next(iterator))
715 {
716 payload_t *current_payload;
717 status = iterator->current(iterator,(void **)&current_payload);
718 if (status != SUCCESS)
719 {
720 this->logger->log(this->logger, ERROR, "Could not get payload from internal list");
721 iterator->destroy(iterator);
722 return status;
723 }
724 if (current_payload->get_type(current_payload) == payload_type)
725 {
726 found_payloads++;
727 if (found_payloads > max_occurence)
728 {
729 this->logger->log(this->logger, ERROR, "Payload of type %s more than %d times (%d) occured in current message",
730 mapping_find(payload_type_m,current_payload->get_type(current_payload)),max_occurence,found_payloads);
731 iterator->destroy(iterator);
732 return NOT_SUPPORTED;
733 }
734 }
735
736 }
737 if (found_payloads < min_occurence)
738 {
739 this->logger->log(this->logger, ERROR, "Payload of type %s not occured %d times",
740 mapping_find(payload_type_m,payload_type),min_occurence);
741 iterator->destroy(iterator);
742 return NOT_SUPPORTED;
743 }
744
745 }
746 iterator->destroy(iterator);
747 }
748 return status;
749 }
750
751
752
753 /**
754 * Implements message_t's destroy function.
755 * See #message_s.destroy.
756 */
757 static status_t destroy (private_message_t *this)
758 {
759 iterator_t *iterator;
760
761 this->packet->destroy(this->packet);
762
763 if (this->ike_sa_id != NULL)
764 {
765 this->ike_sa_id->destroy(this->ike_sa_id);
766 }
767
768 this->payloads->create_iterator(this->payloads, &iterator, TRUE);
769 while (iterator->has_next(iterator))
770 {
771 payload_t *payload;
772 iterator->current(iterator, (void**)&payload);
773 this->logger->log(this->logger, CONTROL|MOST, "Destroying payload of type %s",
774 mapping_find(payload_type_m, payload->get_type(payload)));
775 payload->destroy(payload);
776 }
777 iterator->destroy(iterator);
778 this->payloads->destroy(this->payloads);
779 this->parser->destroy(this->parser);
780 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
781
782 allocator_free(this);
783 return SUCCESS;
784 }
785
786 /*
787 * Described in Header-File
788 */
789 message_t *message_create_from_packet(packet_t *packet)
790 {
791 private_message_t *this = allocator_alloc_thing(private_message_t);
792 if (this == NULL)
793 {
794 return NULL;
795 }
796
797 /* public functions */
798 this->public.set_major_version = (status_t(*)(message_t*, u_int8_t))set_major_version;
799 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
800 this->public.set_minor_version = (status_t(*)(message_t*, u_int8_t))set_minor_version;
801 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
802 this->public.set_message_id = (status_t(*)(message_t*, u_int32_t))set_message_id;
803 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
804 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
805 this->public.set_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
806 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
807 this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
808 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
809 this->public.set_request = (status_t(*)(message_t*, bool))set_request;
810 this->public.get_request = (bool(*)(message_t*))get_request;
811 this->public.add_payload = (status_t(*)(message_t*,payload_t*))add_payload;
812 this->public.generate = (status_t (*) (message_t *, packet_t**)) generate;
813 this->public.set_source = (status_t (*) (message_t*,host_t*)) set_source;
814 this->public.get_source = (status_t (*) (message_t*,host_t**)) get_source;
815 this->public.set_destination = (status_t (*) (message_t*,host_t*)) set_destination;
816 this->public.get_destination = (status_t (*) (message_t*,host_t**)) get_destination;
817 this->public.get_payload_iterator = (status_t (*) (message_t *, iterator_t **)) get_payload_iterator;
818 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
819 this->public.parse_body = (status_t (*) (message_t *)) parse_body;
820 this->public.destroy = (status_t(*)(message_t*))destroy;
821
822 /* public values */
823 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
824 this->is_request = TRUE;
825 this->ike_sa_id = NULL;
826 this->first_payload = NO_PAYLOAD;
827 this->message_id = 0;
828
829 /* private functions */
830 this->get_supported_payloads = get_supported_payloads;
831
832 /* private values */
833 if (packet == NULL)
834 {
835 packet = packet_create();
836 }
837 if (packet == NULL)
838 {
839 allocator_free(this);
840 return NULL;
841 }
842 this->packet = packet;
843 this->payloads = linked_list_create();
844 if (this->payloads == NULL)
845 {
846 allocator_free(this);
847 return NULL;
848 }
849
850 /* parser is created from data of packet */
851 this->parser = parser_create(this->packet->data);
852 if (this->parser == NULL)
853 {
854 this->payloads->destroy(this->payloads);
855 allocator_free(this);
856 return NULL;
857 }
858
859 this->logger = global_logger_manager->create_logger(global_logger_manager, MESSAGE, NULL);
860 if (this->logger == NULL)
861 {
862 this->parser->destroy(this->parser);
863 this->payloads->destroy(this->payloads);
864 allocator_free(this);
865 return NULL;
866 }
867
868 return (&this->public);
869 }
870
871 /*
872 * Described in Header-File
873 */
874 message_t *message_create()
875 {
876 return message_create_from_packet(NULL);
877 }