aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/message.c
blob: 26832296a21b1855c0090c36fd5914162a7064c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
 * @file message.c
 *
 * @brief Class message_t. Object of this type represents an IKEv2-Message.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <stdlib.h>

#include "allocator.h"
#include "types.h"
#include "message.h"
#include "ike_sa_id.h"
#include "linked_list.h"
#include "encodings.h"

/**
 * Entry for a payload in the internal used linked list
 * 
 */
typedef struct payload_entry_s payload_entry_t;

struct payload_entry_s{
	/**
	 * Type of payload
	 */
	payload_type_t payload_type;
	/**
	 * Data struct holding the data of given payload
	 */
	void *data_struct;
};


/**
 * Private data of an message_t object
 */
typedef struct private_message_s private_message_t;

struct private_message_s {

	/**
	 * Public part of a message_t object
	 */
	message_t public;


	/* Private values */
	/**
	 * Assigned exchange type
	 */
	 exchange_type_t exchange_type;
	
	/**
	 * TRUE if message is from original initiator, FALSE otherwise.
	 */
	bool original_initiator;

	/**
	 * TRUE if message is request.
	 * FALSE if message is reply.
	 */
	bool is_request;
	
	/**
	 * First Payload type following the header
	 */
	payload_type_t first_payload_type;
	
	/**
	 * Message ID of this message
	 */
	u_int32_t message_id;
	
	/**
	 * ID of assigned IKE_SA
	 */
	ike_sa_id_t *ike_sa_id;
	
	/**
	 * Assigned UDP packet.
	 * 
	 * Stores incoming packet or last generated one.
	 */
	 packet_t *packet;
	 
	 /**
	  * Linked List where payload data are stored in
	  */
	linked_list_t *payloads;
};


/**
 * Implements message_t's set_ike_sa_id function.
 * See #message_s.set_ike_sa_id.
 */
static status_t set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
{
	status_t status;
	status = ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
	return status;
}

/**
 * Implements message_t's get_ike_sa_id function.
 * See #message_s.get_ike_sa_id.
 */
static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
{
	status_t status;
	if (this->ike_sa_id == NULL)
	{
		return FAILED;
	}
	status = this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
	return status;
}


/**
 * Implements message_t's set_message_id function.
 * See #message_s.set_message_id.
 */
static status_t set_message_id (private_message_t *this,u_int32_t message_id)
{
	this->message_id = message_id;
	return SUCCESS;
}


/**
 * Implements message_t's set_message_id function.
 * See #message_s.set_message_id.
 */
static u_int32_t get_message_id (private_message_t *this)
{
	return this->message_id;
}


/**
 * Implements message_t's set_exchange_type function.
 * See #message_s.set_exchange_type.
 */
static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
{
	this->exchange_type = exchange_type;
	return SUCCESS;
}


/**
 * Implements message_t's get_exchange_type function.
 * See #message_s.get_exchange_type.
 */
static exchange_type_t get_exchange_type (private_message_t *this)
{
	return this->exchange_type;
}

/**
 * Implements message_t's set_original_initiator function.
 * See #message_s.set_original_initiator.
 */
static status_t set_original_initiator (private_message_t *this,bool original_initiator)
{
	this->original_initiator = original_initiator;
	return SUCCESS;
}

/**
 * Implements message_t's get_original_initiator function.
 * See #message_s.get_original_initiator.
 */
static exchange_type_t get_original_initiator (private_message_t *this)
{
	return this->original_initiator;
}

/**
 * Implements message_t's set_request function.
 * See #message_s.set_request.
 */
static status_t set_request (private_message_t *this,bool request)
{
	this->is_request = request;
	return SUCCESS;
}

/**
 * Implements message_t's get_request function.
 * See #message_s.get_request.
 */
static exchange_type_t get_request (private_message_t *this)
{
	return this->is_request;
}

/**
 * Implements message_t's generate_packet function.
 * See #message_s.generate_packet.
 */
static status_t generate_packet (private_message_t *this, packet_t **packet)
{
	if (this->exchange_type == NOT_SET)
	{
		return EXCHANGE_TYPE_NOT_SET;
	}
	
	
	return SUCCESS;
}

/**
 * Implements message_t's destroy function.
 * See #message_s.destroy.
 */
static status_t destroy (private_message_t *this)
{
	if (this->packet != NULL)
	{
		this->packet->destroy(this->packet);
	}
	if (this->ike_sa_id != NULL)
	{
		this->ike_sa_id->destroy(this->ike_sa_id);
	}
	this->payloads->destroy(this->payloads);
	allocator_free(this);
	return SUCCESS;
}

/*
 * Described in Header-File
 */
message_t *message_create_from_packet(packet_t *packet)
{
	private_message_t *this = allocator_alloc_thing(private_message_t);
	if (this == NULL)
	{
		return NULL;
	}

	/* public functions */
	this->public.set_message_id = (status_t(*)(message_t*, u_int32_t))set_message_id;
	this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
	this->public.set_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
	this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
	this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
	this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
	this->public.set_original_initiator = (status_t(*)(message_t*, bool))set_original_initiator;
	this->public.get_original_initiator = (bool(*)(message_t*))get_original_initiator;
	this->public.set_request = (status_t(*)(message_t*, bool))set_request;
	this->public.get_request = (bool(*)(message_t*))get_request;
	this->public.generate_packet = (status_t (*) (message_t *, packet_t **)) generate_packet;
	this->public.destroy = (status_t(*)(message_t*))destroy;
		
	/* public values */
	this->exchange_type = NOT_SET;
 	this->original_initiator = TRUE;
 	this->is_request = TRUE;
 	this->first_payload_type = NO_PAYLOAD;
 	this->ike_sa_id = NULL;
 	this->message_id = 0;

	/* private values */
	this->packet = packet;
	this->payloads = linked_list_create();
	if (this->payloads == NULL)
	{
		allocator_free(this);
		return NULL;
	}

	return (&this->public);
}

/*
 * Described in Header-File
 */
message_t *message_create()
{
	return message_create_from_packet(NULL);
}