""
[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 "message.h"
26
27 #include "types.h"
28 #include "ike_sa_id.h"
29 #include "utils/linked_list.h"
30 #include "utils/allocator.h"
31 #include "payloads/encodings.h"
32 #include "payloads/payload.h"
33
34 /**
35 * Entry for a payload in the internal used linked list
36 *
37 */
38 typedef struct payload_entry_s payload_entry_t;
39
40 struct payload_entry_s{
41 /**
42 * Type of payload
43 */
44 payload_type_t payload_type;
45 /**
46 * Data struct holding the data of given payload
47 */
48 void *data_struct;
49 };
50
51
52 /**
53 * Private data of an message_t object
54 */
55 typedef struct private_message_s private_message_t;
56
57 struct private_message_s {
58
59 /**
60 * Public part of a message_t object
61 */
62 message_t public;
63
64
65 /* Private values */
66 /**
67 * Assigned exchange type
68 */
69 exchange_type_t exchange_type;
70
71 /**
72 * TRUE if message is from original initiator, FALSE otherwise.
73 */
74 bool original_initiator;
75
76 /**
77 * TRUE if message is request.
78 * FALSE if message is reply.
79 */
80 bool is_request;
81
82 /**
83 * First Payload type following the header
84 */
85 payload_type_t first_payload_type;
86
87 /**
88 * Message ID of this message
89 */
90 u_int32_t message_id;
91
92 /**
93 * ID of assigned IKE_SA
94 */
95 ike_sa_id_t *ike_sa_id;
96
97 /**
98 * Assigned UDP packet.
99 *
100 * Stores incoming packet or last generated one.
101 */
102 packet_t *packet;
103
104 /**
105 * Linked List where payload data are stored in
106 */
107 linked_list_t *payloads;
108 };
109
110
111 /**
112 * Implements message_t's set_ike_sa_id function.
113 * See #message_s.set_ike_sa_id.
114 */
115 static status_t set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
116 {
117 status_t status;
118 status = ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
119 return status;
120 }
121
122 /**
123 * Implements message_t's get_ike_sa_id function.
124 * See #message_s.get_ike_sa_id.
125 */
126 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
127 {
128 status_t status;
129 if (this->ike_sa_id == NULL)
130 {
131 return FAILED;
132 }
133 status = this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
134 return status;
135 }
136
137
138 /**
139 * Implements message_t's set_message_id function.
140 * See #message_s.set_message_id.
141 */
142 static status_t set_message_id (private_message_t *this,u_int32_t message_id)
143 {
144 this->message_id = message_id;
145 return SUCCESS;
146 }
147
148
149 /**
150 * Implements message_t's set_message_id function.
151 * See #message_s.set_message_id.
152 */
153 static u_int32_t get_message_id (private_message_t *this)
154 {
155 return this->message_id;
156 }
157
158
159 /**
160 * Implements message_t's set_exchange_type function.
161 * See #message_s.set_exchange_type.
162 */
163 static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
164 {
165 this->exchange_type = exchange_type;
166 return SUCCESS;
167 }
168
169
170 /**
171 * Implements message_t's get_exchange_type function.
172 * See #message_s.get_exchange_type.
173 */
174 static exchange_type_t get_exchange_type (private_message_t *this)
175 {
176 return this->exchange_type;
177 }
178
179 /**
180 * Implements message_t's set_original_initiator function.
181 * See #message_s.set_original_initiator.
182 */
183 static status_t set_original_initiator (private_message_t *this,bool original_initiator)
184 {
185 this->original_initiator = original_initiator;
186 return SUCCESS;
187 }
188
189 /**
190 * Implements message_t's get_original_initiator function.
191 * See #message_s.get_original_initiator.
192 */
193 static exchange_type_t get_original_initiator (private_message_t *this)
194 {
195 return this->original_initiator;
196 }
197
198 /**
199 * Implements message_t's set_request function.
200 * See #message_s.set_request.
201 */
202 static status_t set_request (private_message_t *this,bool request)
203 {
204 this->is_request = request;
205 return SUCCESS;
206 }
207
208 /**
209 * Implements message_t's get_request function.
210 * See #message_s.get_request.
211 */
212 static exchange_type_t get_request (private_message_t *this)
213 {
214 return this->is_request;
215 }
216
217 /**
218 * Implements message_t's generate_packet function.
219 * See #message_s.generate_packet.
220 */
221 static status_t generate_packet (private_message_t *this, packet_t **packet)
222 {
223 if (this->exchange_type == NOT_SET)
224 {
225 return EXCHANGE_TYPE_NOT_SET;
226 }
227
228
229 return SUCCESS;
230 }
231
232 /**
233 * Implements message_t's destroy function.
234 * See #message_s.destroy.
235 */
236 static status_t destroy (private_message_t *this)
237 {
238 if (this->packet != NULL)
239 {
240 this->packet->destroy(this->packet);
241 }
242 if (this->ike_sa_id != NULL)
243 {
244 this->ike_sa_id->destroy(this->ike_sa_id);
245 }
246 this->payloads->destroy(this->payloads);
247 allocator_free(this);
248 return SUCCESS;
249 }
250
251 /*
252 * Described in Header-File
253 */
254 message_t *message_create_from_packet(packet_t *packet)
255 {
256 private_message_t *this = allocator_alloc_thing(private_message_t);
257 if (this == NULL)
258 {
259 return NULL;
260 }
261
262 /* public functions */
263 this->public.set_message_id = (status_t(*)(message_t*, u_int32_t))set_message_id;
264 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
265 this->public.set_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
266 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
267 this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
268 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
269 this->public.set_original_initiator = (status_t(*)(message_t*, bool))set_original_initiator;
270 this->public.get_original_initiator = (bool(*)(message_t*))get_original_initiator;
271 this->public.set_request = (status_t(*)(message_t*, bool))set_request;
272 this->public.get_request = (bool(*)(message_t*))get_request;
273 this->public.generate_packet = (status_t (*) (message_t *, packet_t **)) generate_packet;
274 this->public.destroy = (status_t(*)(message_t*))destroy;
275
276 /* public values */
277 this->exchange_type = NOT_SET;
278 this->original_initiator = TRUE;
279 this->is_request = TRUE;
280 this->first_payload_type = NO_PAYLOAD;
281 this->ike_sa_id = NULL;
282 this->message_id = 0;
283
284 /* private values */
285 this->packet = packet;
286 this->payloads = linked_list_create();
287 if (this->payloads == NULL)
288 {
289 allocator_free(this);
290 return NULL;
291 }
292
293 return (&this->public);
294 }
295
296 /*
297 * Described in Header-File
298 */
299 message_t *message_create()
300 {
301 return message_create_from_packet(NULL);
302 }