generator now has its generator_context which is used to generate more
[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 #include "encodings.h"
30
31 /**
32 * Entry for a payload in the internal used linked list
33 *
34 */
35 typedef struct payload_entry_s payload_entry_t;
36
37 struct payload_entry_s{
38 /**
39 * Type of payload
40 */
41 payload_type_t payload_type;
42 /**
43 * Data struct holding the data of given payload
44 */
45 void *data_struct;
46 };
47
48
49 /**
50 * Private data of an message_t object
51 */
52 typedef struct private_message_s private_message_t;
53
54 struct private_message_s {
55
56 /**
57 * Public part of a message_t object
58 */
59 message_t public;
60
61
62 /* Private values */
63 /**
64 * Assigned exchange type
65 */
66 exchange_type_t exchange_type;
67
68 /**
69 * TRUE if message is from original initiator, FALSE otherwise.
70 */
71 bool original_initiator;
72
73 /**
74 * TRUE if message is request.
75 * FALSE if message is reply.
76 */
77 bool is_request;
78
79 /**
80 * Assigned UDP packet.
81 *
82 * Stores incoming packet or last generated one.
83 */
84 packet_t *packet;
85
86 /**
87 * Linked List where payload data are stored in
88 */
89 linked_list_t *payloads;
90 };
91
92 /**
93 * Implements message_t's set_exchange_type function.
94 * See #message_s.set_exchange_type.
95 */
96 static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
97 {
98 this->exchange_type = exchange_type;
99 return SUCCESS;
100 }
101
102
103 /**
104 * Implements message_t's get_exchange_type function.
105 * See #message_s.get_exchange_type.
106 */
107 static exchange_type_t get_exchange_type (private_message_t *this)
108 {
109 return this->exchange_type;
110 }
111
112 /**
113 * Implements message_t's set_original_initiator function.
114 * See #message_s.set_original_initiator.
115 */
116 static status_t set_original_initiator (private_message_t *this,bool original_initiator)
117 {
118 this->original_initiator = original_initiator;
119 return SUCCESS;
120 }
121
122 /**
123 * Implements message_t's get_original_initiator function.
124 * See #message_s.get_original_initiator.
125 */
126 static exchange_type_t get_original_initiator (private_message_t *this)
127 {
128 return this->original_initiator;
129 }
130
131 /**
132 * Implements message_t's set_request function.
133 * See #message_s.set_request.
134 */
135 static status_t set_request (private_message_t *this,bool request)
136 {
137 this->is_request = request;
138 return SUCCESS;
139 }
140
141 /**
142 * Implements message_t's get_request function.
143 * See #message_s.get_request.
144 */
145 static exchange_type_t get_request (private_message_t *this)
146 {
147 return this->is_request;
148 }
149
150 /**
151 * Implements message_t's generate_packet function.
152 * See #message_s.generate_packet.
153 */
154 static status_t generate_packet (private_message_t *this, packet_t **packet)
155 {
156 if (this->exchange_type == NOT_SET)
157 {
158 return EXCHANGE_TYPE_NOT_SET;
159 }
160
161
162
163 return SUCCESS;
164 }
165
166 /**
167 * Implements message_t's destroy function.
168 * See #message_s.destroy.
169 */
170 static status_t destroy (private_message_t *this)
171 {
172 if (this->packet != NULL)
173 {
174 this->packet->destroy(this->packet);
175 }
176 this->payloads->destroy(this->payloads);
177 allocator_free(this);
178 return SUCCESS;
179 }
180
181 /*
182 * Described in Header-File
183 */
184 message_t *message_create_from_packet(packet_t *packet)
185 {
186 private_message_t *this = allocator_alloc_thing(private_message_t);
187 if (this == NULL)
188 {
189 return NULL;
190 }
191
192 /* public functions */
193 this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
194 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
195 this->public.set_original_initiator = (status_t(*)(message_t*, bool))set_original_initiator;
196 this->public.get_original_initiator = (bool(*)(message_t*))get_original_initiator;
197 this->public.set_request = (status_t(*)(message_t*, bool))set_request;
198 this->public.get_request = (bool(*)(message_t*))get_request;
199 this->public.generate_packet = (status_t (*) (message_t *, packet_t **)) generate_packet;
200 this->public.destroy = (status_t(*)(message_t*))destroy;
201
202 /* public values */
203 this->exchange_type = NOT_SET;
204 this->original_initiator = TRUE;
205 this->is_request = TRUE;
206
207 /* private values */
208 this->packet = packet;
209 this->payloads = linked_list_create();
210 if (this->payloads == NULL)
211 {
212 allocator_free(this);
213 return NULL;
214 }
215
216 return (&this->public);
217 }
218
219 /*
220 * Described in Header-File
221 */
222 message_t *message_create()
223 {
224 return message_create_from_packet(NULL);
225 }