96d553ece6de7da542ab7a54df4c38d7f7c82705
[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 "allocator.h"
26 #include "types.h"
27 #include "message.h"
28 #include "linked_list.h"
29
30 /**
31 * Private data of an message_t object
32 */
33 typedef struct private_message_s private_message_t;
34
35 struct private_message_s {
36
37 /**
38 * Public part of a message_t object
39 */
40 message_t public;
41
42
43 /* Private values */
44
45 /**
46 * Assigned UDP packet.
47 *
48 * Stores incoming packet or last generated one.
49 */
50 packet_t *packet;
51
52 /**
53 * Linked List where payload data are stored in
54 */
55 linked_list_t *payloads;
56 };
57
58 /**
59 * Implements message_t's destroy function.
60 * See #message_s.destroy.
61 */
62 static status_t destroy (private_message_t *this)
63 {
64 if (this->packet != NULL)
65 {
66 this->packet->destroy(this->packet);
67 }
68 this->payloads->destroy(this->payloads);
69 allocator_free(this);
70 return SUCCESS;
71 }
72
73 /*
74 * Described in Header-File
75 */
76 message_t *message_create_from_packet(packet_t *packet)
77 {
78 private_message_t *this = allocator_alloc_thing(private_message_t);
79 if (this == NULL)
80 {
81 return NULL;
82 }
83
84 /* public functions */
85 this->public.destroy = (status_t(*)(message_t*))destroy;
86
87 /* private values */
88 this->packet = packet;
89 this->payloads = linked_list_create();
90 if (this->payloads == NULL)
91 {
92 allocator_free(this);
93 return NULL;
94 }
95
96 return (&this->public);
97 }
98
99 /*
100 * Described in Header-File
101 */
102 message_t *message_create()
103 {
104 return message_create_from_packet(NULL);
105 }