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