- memory allocation checks removed
[strongswan.git] / Source / charon / encoding / message.c
1 /**
2 * @file message.c
3 *
4 * @brief Implementation of message_t.
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
67 * are supported by each message type.
68 *
69 */
70 struct message_rule_t {
71 /**
72 * Type of message.
73 */
74 exchange_type_t exchange_type;
75
76 /**
77 * Is message a request or response.
78 */
79 bool is_request;
80 /**
81 * Number of supported payloads.
82 */
83 size_t supported_payloads_count;
84 /**
85 * Pointer to first supported payload entry.
86 */
87 supported_payload_entry_t *supported_payloads;
88 };
89
90 /**
91 * Message rule for IKE_SA_INIT from initiator.
92 */
93 static supported_payload_entry_t supported_ike_sa_init_i_payloads[] =
94 {
95 {SECURITY_ASSOCIATION,1,1},
96 {KEY_EXCHANGE,1,1},
97 {NONCE,1,1},
98 };
99
100 /**
101 * Message rule for IKE_SA_INIT from responder.
102 */
103 static supported_payload_entry_t supported_ike_sa_init_r_payloads[] =
104 {
105 {SECURITY_ASSOCIATION,1,1},
106 {KEY_EXCHANGE,1,1},
107 {NONCE,1,1},
108 };
109
110
111 /**
112 * Message rules, defines allowed payloads.
113 */
114 static message_rule_t message_rules[] = {
115 {IKE_SA_INIT,TRUE,(sizeof(supported_ike_sa_init_i_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_i_payloads},
116 {IKE_SA_INIT,FALSE,(sizeof(supported_ike_sa_init_r_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_r_payloads}
117 };
118
119 typedef struct payload_entry_t payload_entry_t;
120
121 /**
122 * Entry for a payload in the internal used linked list.
123 *
124 */
125 struct payload_entry_t {
126 /**
127 * Type of payload.
128 */
129 payload_type_t payload_type;
130 /**
131 * Data struct holding the data of given payload.
132 */
133 void *data_struct;
134 };
135
136
137 typedef struct private_message_t private_message_t;
138
139 /**
140 * Private data of an message_t object.
141 */
142 struct private_message_t {
143
144 /**
145 * Public part of a message_t object.
146 */
147 message_t public;
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 * Assigned logger.
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
216 * - SUCCESS
217 * - NOT_FOUND if no supported payload definition could be found
218 */
219 status_t (*get_supported_payloads) (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count);
220
221 };
222
223 /**
224 * Implementation of 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 * Implementation of message_t.set_ike_sa_id.
254 */
255 static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
256 {
257 ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
258 }
259
260 /**
261 * Implementation of message_t.get_ike_sa_id.
262 */
263 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
264 {
265 if (this->ike_sa_id == NULL)
266 {
267 return FAILED;
268 }
269 this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
270 return SUCCESS;
271 }
272
273 /**
274 * Implementation of message_t.set_message_id.
275 */
276 static void set_message_id (private_message_t *this,u_int32_t message_id)
277 {
278 this->message_id = message_id;
279 }
280
281 /**
282 * Implementation of message_t.get_message_id.
283 */
284 static u_int32_t get_message_id (private_message_t *this)
285 {
286 return this->message_id;
287 }
288
289 /**
290 * Implementation of message_t.get_responder_spi.
291 */
292 static u_int64_t get_responder_spi (private_message_t *this)
293 {
294 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
295 }
296
297 /**
298 * Implementation of message_t.set_major_version.
299 */
300 static void set_major_version (private_message_t *this,u_int8_t major_version)
301 {
302 this->major_version = major_version;
303 }
304
305
306 /**
307 * Implementation of message_t.set_major_version.
308 */
309 static u_int8_t get_major_version (private_message_t *this)
310 {
311 return this->major_version;
312 }
313
314 /**
315 * Implementation of message_t.set_minor_version.
316 */
317 static void set_minor_version (private_message_t *this,u_int8_t minor_version)
318 {
319 this->minor_version = minor_version;
320 }
321
322 /**
323 * Implementation of message_t.get_minor_version.
324 */
325 static u_int8_t get_minor_version (private_message_t *this)
326 {
327 return this->minor_version;
328 }
329
330 /**
331 * Implementation of message_t.set_exchange_type.
332 */
333 static void set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
334 {
335 this->exchange_type = exchange_type;
336 }
337
338 /**
339 * Implementation of message_t.get_exchange_type.
340 */
341 static exchange_type_t get_exchange_type (private_message_t *this)
342 {
343 return this->exchange_type;
344 }
345
346 /**
347 * Implementation of message_t.set_request.
348 */
349 static void set_request (private_message_t *this,bool request)
350 {
351 this->is_request = request;
352 }
353
354 /**
355 * Implementation of message_t.get_request.
356 */
357 static exchange_type_t get_request (private_message_t *this)
358 {
359 return this->is_request;
360 }
361
362 /**
363 * Implementation of message_t.add_payload.
364 */
365 static void add_payload(private_message_t *this, payload_t *payload)
366 {
367 payload_t *last_payload;
368 if (this->payloads->get_count(this->payloads) > 0)
369 {
370 this->payloads->get_last(this->payloads,(void **) &last_payload);
371 }
372
373 this->payloads->insert_last(this->payloads, payload);
374
375 if (this->payloads->get_count(this->payloads) == 1)
376 {
377 this->first_payload = payload->get_type(payload);
378 }
379 else
380 {
381 last_payload->set_next_type(last_payload,payload->get_type(payload));
382 }
383
384 this->logger->log(this->logger, CONTROL|MORE, "added payload of type %s to message",
385 mapping_find(payload_type_m, payload->get_type(payload)));
386
387 }
388
389 /**
390 * Implementation of message_t.set_source.
391 */
392 static void set_source(private_message_t *this, host_t *host)
393 {
394 if (this->packet->source != NULL)
395 {
396 this->packet->source->destroy(this->packet->source);
397 }
398 this->packet->source = host;
399 }
400
401 /**
402 * Implementation of message_t.set_destination.
403 */
404 static void set_destination(private_message_t *this, host_t *host)
405 {
406 if (this->packet->destination != NULL)
407 {
408 this->packet->destination->destroy(this->packet->destination);
409 }
410 this->packet->destination = host;
411 }
412
413 /**
414 * Implementation of message_t.get_source.
415 */
416 static void get_source(private_message_t *this, host_t **host)
417 {
418 *host = this->packet->source;
419 }
420
421 /**
422 * Implementation of message_t.get_destination.
423 */
424 static void get_destination(private_message_t *this, host_t **host)
425 {
426 *host = this->packet->destination;
427 }
428
429 /**
430 * Implementation of message_t.get_destination.
431 */
432 static void get_payload_iterator(private_message_t *this, iterator_t **iterator)
433 {
434 this->payloads->create_iterator(this->payloads, iterator, TRUE);
435 }
436
437
438 /**
439 * Implementation of message_t.generate.
440 */
441 static status_t generate(private_message_t *this, crypter_t *crypter, signer_t* signer, packet_t **packet)
442 {
443 generator_t *generator;
444 ike_header_t *ike_header;
445 payload_t *payload, *next_payload;
446 iterator_t *iterator;
447 status_t status;
448
449
450 this->logger->log(this->logger, CONTROL, "generating message, contains %d payloads",
451 this->payloads->get_count(this->payloads));
452
453 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
454 {
455 this->logger->log(this->logger, ERROR, "exchange type is not defined");
456 return INVALID_STATE;
457 }
458
459 if (this->packet->source == NULL ||
460 this->packet->destination == NULL)
461 {
462 this->logger->log(this->logger, ERROR, "source/destination not defined");
463 return INVALID_STATE;
464 }
465
466 /* build ike header */
467 ike_header = ike_header_create();
468
469 ike_header->set_exchange_type(ike_header, this->exchange_type);
470 ike_header->set_message_id(ike_header, this->message_id);
471 ike_header->set_response_flag(ike_header, !this->is_request);
472 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
473 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
474 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
475
476 generator = generator_create();
477
478 payload = (payload_t*)ike_header;
479
480 this->payloads->create_iterator(this->payloads, &iterator, TRUE);
481
482 /* generate every payload, except last one */
483 while(iterator->has_next(iterator))
484 {
485 iterator->current(iterator, (void**)&next_payload);
486 payload->set_next_type(payload, next_payload->get_type(next_payload));
487 generator->generate_payload(generator, payload);
488
489 payload = next_payload;
490 }
491 iterator->destroy(iterator);
492
493 /* build last payload */
494 payload->set_next_type(payload, NO_PAYLOAD);
495 /* if it's an encryption payload, build it first */
496 if (payload->get_type(payload) == ENCRYPTED)
497 {
498 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
499 encryption_payload->set_signer(encryption_payload, signer);
500 status = encryption_payload->encrypt(encryption_payload, crypter);
501 if (status != SUCCESS)
502 {
503 generator->destroy(generator);
504 ike_header->destroy(ike_header);
505 return status;
506 }
507 }
508 generator->generate_payload(generator, payload);
509 ike_header->destroy(ike_header);
510
511 /* build packet */
512 if (this->packet->data.ptr != NULL)
513 {
514 allocator_free(this->packet->data.ptr);
515 }
516 generator->write_to_chunk(generator, &(this->packet->data));
517 generator->destroy(generator);
518
519 /* append integrity checksum if necessary */
520 if (payload->get_type(payload) == ENCRYPTED)
521 {
522 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
523 status = encryption_payload->build_signature(encryption_payload, this->packet->data);
524 if (status != SUCCESS)
525 {
526 return status;
527 }
528 }
529
530 /* colen packet for caller */
531 this->packet->clone(this->packet, packet);
532
533 this->logger->log(this->logger, CONTROL, "message generated successfully");
534 return SUCCESS;
535 }
536
537 /**
538 * Implements message_t's parse_header function.
539 * See #message_s.parse_header.
540 */
541 static status_t parse_header(private_message_t *this)
542 {
543 ike_header_t *ike_header;
544 status_t status;
545
546
547 this->logger->log(this->logger, CONTROL, "parsing header of message");
548
549 this->parser->reset_context(this->parser);
550 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
551 if (status != SUCCESS)
552 {
553 this->logger->log(this->logger, ERROR, "Header could not be parsed");
554 return status;
555
556 }
557
558 /* verify payload */
559 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
560 if (status != SUCCESS)
561 {
562 this->logger->log(this->logger, ERROR, "Header verification failed");
563 ike_header->destroy(ike_header);
564 return status;
565 }
566
567 if (this->ike_sa_id != NULL)
568 {
569 this->ike_sa_id->destroy(this->ike_sa_id);
570 }
571
572 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
573 ike_header->get_responder_spi(ike_header),
574 ike_header->get_initiator_flag(ike_header));
575
576 this->exchange_type = ike_header->get_exchange_type(ike_header);
577 this->message_id = ike_header->get_message_id(ike_header);
578 this->is_request = (!(ike_header->get_response_flag(ike_header)));
579 this->major_version = ike_header->get_maj_version(ike_header);
580 this->minor_version = ike_header->get_min_version(ike_header);
581 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
582
583
584 this->logger->log(this->logger, CONTROL, "parsing header successfully");
585
586 ike_header->destroy(ike_header);
587 return SUCCESS;
588 }
589
590 /**
591 * Implements message_t.parse_body.
592 */
593 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
594 {
595 status_t status = SUCCESS;
596 payload_type_t current_payload_type = this->first_payload;
597
598 this->logger->log(this->logger, CONTROL, "parsing body of message");
599
600 while (current_payload_type != NO_PAYLOAD)
601 {
602 payload_t *current_payload;
603
604 this->logger->log(this->logger, CONTROL|MORE, "start parsing payload of type %s",
605 mapping_find(payload_type_m, current_payload_type));
606
607 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
608 if (status != SUCCESS)
609 {
610 this->logger->log(this->logger, ERROR, "payload type %s could not be parsed",mapping_find(payload_type_m,current_payload_type));
611 return status;
612 }
613
614 status = current_payload->verify(current_payload);
615 if (status != SUCCESS)
616 {
617 this->logger->log(this->logger, ERROR, "payload type %s could not be verified",mapping_find(payload_type_m,current_payload_type));
618 status = VERIFY_ERROR;
619 return status;
620 }
621
622 /* encrypted payload must be decrypted */
623 if (current_payload->get_type(current_payload) == ENCRYPTED)
624 {
625 encryption_payload_t *encryption_payload = (encryption_payload_t*)current_payload;
626 encryption_payload->set_signer(encryption_payload, signer);
627 status = encryption_payload->verify_signature(encryption_payload, this->packet->data);
628 if (status != SUCCESS)
629 {
630 this->logger->log(this->logger, ERROR, "encryption payload signature invaild");
631 return status;
632 }
633 status = encryption_payload->decrypt(encryption_payload, crypter);
634 if (status != SUCCESS)
635 {
636 this->logger->log(this->logger, ERROR, "parsing decrypted encryption payload failed");
637 return status;
638 }
639 }
640
641 /* get next payload type */
642 current_payload_type = current_payload->get_next_type(current_payload);
643
644 status = this->payloads->insert_last(this->payloads,current_payload);
645 if (status != SUCCESS)
646 {
647 this->logger->log(this->logger, ERROR, "%s on adding payload", mapping_find(status_m, status));
648 return status;;
649 }
650
651 }
652 return this->public.verify(&(this->public));
653
654 }
655
656 /**
657 * implements message_t.verify
658 */
659 static status_t verify(private_message_t *this)
660 {
661 iterator_t *iterator;
662 status_t status;
663 int i;
664 supported_payload_entry_t *supported_payloads;
665 size_t supported_payloads_count;
666
667 this->logger->log(this->logger, CONTROL|MORE, "verifying message");
668
669 status = this->get_supported_payloads(this, &supported_payloads, &supported_payloads_count);
670 if (status != SUCCESS)
671 {
672 this->logger->log(this->logger, ERROR, "could not get supported payloads: %s");
673 return status;
674 }
675
676 this->payloads->create_iterator(this->payloads,&iterator,TRUE);
677
678 /* check for payloads with wrong count*/
679 for (i = 0; i < supported_payloads_count;i++)
680 {
681 size_t min_occurence = supported_payloads[i].min_occurence;
682 size_t max_occurence = supported_payloads[i].max_occurence;
683 payload_type_t payload_type = supported_payloads[i].payload_type;
684 size_t found_payloads = 0;
685
686 iterator->reset(iterator);
687
688 while(iterator->has_next(iterator))
689 {
690 payload_t *current_payload;
691 iterator->current(iterator,(void **)&current_payload);
692
693 if (current_payload->get_type(current_payload) == payload_type)
694 {
695 found_payloads++;
696 if (found_payloads > max_occurence)
697 {
698 this->logger->log(this->logger, ERROR, "Payload of type %s more than %d times (%d) occured in current message",
699 mapping_find(payload_type_m,current_payload->get_type(current_payload)),max_occurence,found_payloads);
700 iterator->destroy(iterator);
701 return NOT_SUPPORTED;
702 }
703 }
704 }
705 if (found_payloads < min_occurence)
706 {
707 this->logger->log(this->logger, ERROR, "Payload of type %s not occured %d times",
708 mapping_find(payload_type_m,payload_type),min_occurence);
709 iterator->destroy(iterator);
710 return NOT_SUPPORTED;
711 }
712 }
713 iterator->destroy(iterator);
714
715 return SUCCESS;
716 }
717
718
719 /**
720 * Implements message_t's destroy function.
721 * See #message_s.destroy.
722 */
723 static void destroy (private_message_t *this)
724 {
725 iterator_t *iterator;
726
727 this->packet->destroy(this->packet);
728
729 if (this->ike_sa_id != NULL)
730 {
731 this->ike_sa_id->destroy(this->ike_sa_id);
732 }
733
734 this->payloads->create_iterator(this->payloads, &iterator, TRUE);
735 while (iterator->has_next(iterator))
736 {
737 payload_t *payload;
738 iterator->current(iterator, (void**)&payload);
739 this->logger->log(this->logger, CONTROL|MOST, "Destroying payload of type %s",
740 mapping_find(payload_type_m, payload->get_type(payload)));
741 payload->destroy(payload);
742 }
743 iterator->destroy(iterator);
744 this->payloads->destroy(this->payloads);
745 this->parser->destroy(this->parser);
746 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
747
748 allocator_free(this);
749 }
750
751 /*
752 * Described in Header-File
753 */
754 message_t *message_create_from_packet(packet_t *packet)
755 {
756 private_message_t *this = allocator_alloc_thing(private_message_t);
757 if (this == NULL)
758 {
759 return NULL;
760 }
761
762 /* public functions */
763 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
764 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
765 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
766 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
767 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
768 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
769 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
770 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
771 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
772 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
773 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
774 this->public.set_request = (void(*)(message_t*, bool))set_request;
775 this->public.get_request = (bool(*)(message_t*))get_request;
776 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
777 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
778 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
779 this->public.get_source = (void (*) (message_t*,host_t**)) get_source;
780 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
781 this->public.get_destination = (void (*) (message_t*,host_t**)) get_destination;
782 this->public.get_payload_iterator = (void (*) (message_t *, iterator_t **)) get_payload_iterator;
783 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
784 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
785 this->public.verify = (status_t (*) (message_t*)) verify;
786 this->public.destroy = (void(*)(message_t*))destroy;
787
788 /* public values */
789 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
790 this->is_request = TRUE;
791 this->ike_sa_id = NULL;
792 this->first_payload = NO_PAYLOAD;
793 this->message_id = 0;
794
795 /* private functions */
796 this->get_supported_payloads = get_supported_payloads;
797
798 /* private values */
799 if (packet == NULL)
800 {
801 packet = packet_create();
802 }
803 if (packet == NULL)
804 {
805 allocator_free(this);
806 return NULL;
807 }
808 this->packet = packet;
809 this->payloads = linked_list_create();
810 if (this->payloads == NULL)
811 {
812 allocator_free(this);
813 return NULL;
814 }
815
816 /* parser is created from data of packet */
817 this->parser = parser_create(this->packet->data);
818 if (this->parser == NULL)
819 {
820 this->payloads->destroy(this->payloads);
821 allocator_free(this);
822 return NULL;
823 }
824
825 this->logger = global_logger_manager->create_logger(global_logger_manager, MESSAGE, NULL);
826 if (this->logger == NULL)
827 {
828 this->parser->destroy(this->parser);
829 this->payloads->destroy(this->payloads);
830 allocator_free(this);
831 return NULL;
832 }
833
834 return (&this->public);
835 }
836
837 /*
838 * Described in Header-File
839 */
840 message_t *message_create()
841 {
842 return message_create_from_packet(NULL);
843 }