2 * @file notify_payload.c
4 * @brief Declaration of the class notify_payload_t.
6 * An object of this type represents an IKEv2 Notify-Payload.
8 * See section 3.10 of Draft for details of this payload type.
13 * Copyright (C) 2005 Jan Hutter, Martin Willi
14 * Hochschule fuer Technik Rapperswil
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 #include "notify_payload.h"
32 #include "encodings.h"
33 #include "../utils/allocator.h"
36 * Private data of an notify_payload_t Object
39 typedef struct private_notify_payload_s private_notify_payload_t
;
41 struct private_notify_payload_s
{
43 * public notify_payload_t interface
45 notify_payload_t
public;
50 u_int8_t next_payload
;
58 * Length of this payload
60 u_int16_t payload_length
;
75 u_int16_t notify_message_type
;
78 * Security parameter index (spi)
85 chunk_t notification_data
;
88 * @brief Computes the length of this payload.
90 * @param this calling private_ke_payload_t object
94 status_t (*compute_length
) (private_notify_payload_t
*this);
98 * Encoding rules to parse or generate a IKEv2-Notify Payload
100 * The defined offsets are the positions in a object of type
101 * private_notify_payload_t.
104 encoding_rule_t notify_payload_encodings
[] = {
105 /* 1 Byte next payload type, stored in the field next_payload */
106 { U_INT_8
, offsetof(private_notify_payload_t
, next_payload
) },
107 /* the critical bit */
108 { FLAG
, offsetof(private_notify_payload_t
, critical
) },
109 /* 7 Bit reserved bits, nowhere stored */
117 /* Length of the whole payload*/
118 { PAYLOAD_LENGTH
, offsetof(private_notify_payload_t
, payload_length
) },
119 /* Protocol ID as 8 bit field*/
120 { U_INT_8
, offsetof(private_notify_payload_t
, protocol_id
) },
121 /* SPI Size as 8 bit field*/
122 { SPI_SIZE
, offsetof(private_notify_payload_t
, spi_size
) },
123 /* Notify message type as 16 bit field*/
124 { U_INT_16
, offsetof(private_notify_payload_t
, notify_message_type
) },
125 /* SPI as variable length field*/
126 { SPI
, offsetof(private_notify_payload_t
, spi
) },
127 /* Key Exchange Data is from variable size */
128 { NOTIFICATION_DATA
, offsetof(private_notify_payload_t
, notification_data
) }
132 * Implements payload_t's and notify_payload_t's destroy function.
133 * See #payload_s.destroy or notify_payload_s.destroy for description.
135 static status_t
destroy(private_notify_payload_t
*this)
137 if (this->notification_data
.ptr
!= NULL
)
139 allocator_free(this->notification_data
.ptr
);
141 if (this->spi
.ptr
!= NULL
)
143 allocator_free(this->spi
.ptr
);
146 allocator_free(this);
151 * Implements payload_t's get_encoding_rules function.
152 * See #payload_s.get_encoding_rules for description.
154 static status_t
get_encoding_rules(private_notify_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
156 *rules
= notify_payload_encodings
;
157 *rule_count
= sizeof(notify_payload_encodings
) / sizeof(encoding_rule_t
);
162 * Implements payload_t's get_type function.
163 * See #payload_s.get_type for description.
165 static payload_type_t
get_type(private_notify_payload_t
*this)
171 * Implements payload_t's get_next_type function.
172 * See #payload_s.get_next_type for description.
174 static payload_type_t
get_next_type(private_notify_payload_t
*this)
176 return (this->next_payload
);
180 * Implements payload_t's set_next_type function.
181 * See #payload_s.set_next_type for description.
183 static status_t
set_next_type(private_notify_payload_t
*this,payload_type_t type
)
185 this->next_payload
= type
;
190 * Implements payload_t's get_length function.
191 * See #payload_s.get_length for description.
193 static size_t get_length(private_notify_payload_t
*this)
195 this->compute_length(this);
196 return this->payload_length
;
200 * Implements private_ke_payload_t's compute_length function.
201 * See #private_ke_payload_s.compute_length for description.
203 static status_t
compute_length (private_notify_payload_t
*this)
205 size_t length
= NOTIFY_PAYLOAD_HEADER_LENGTH
;
206 if (this->notification_data
.ptr
!= NULL
)
208 length
+= this->notification_data
.len
;
210 if (this->spi
.ptr
!= NULL
)
212 length
+= this->spi
.len
;
215 this->payload_length
= length
;
222 * Implements notify_payload_t's get_protocol_id function.
223 * See #notify_payload_s.get_protocol_id for description.
225 u_int8_t
get_protocol_id(private_notify_payload_t
*this)
227 return this->protocol_id
;
231 * Implements notify_payload_t's set_protocol_id function.
232 * See #notify_payload_s.set_protocol_id for description.
234 status_t
set_protocol_id(private_notify_payload_t
*this, u_int8_t protocol_id
)
236 this->protocol_id
= protocol_id
;
241 * Implements notify_payload_t's get_notification_data function.
242 * See #notify_payload_s.get_notification_data for description.
244 u_int16_t
get_notify_message_type(private_notify_payload_t
*this)
246 return this->notify_message_type
;
250 * Implements notify_payload_t's get_notification_data function.
251 * See #notify_payload_s.get_notification_data for description.
253 status_t
set_notify_message_type(private_notify_payload_t
*this, u_int16_t notify_message_type
)
255 this->notify_message_type
= notify_message_type
;
260 * Implements notify_payload_t's get_spi function.
261 * See #notify_payload_s.get_spi for description.
263 chunk_t
get_spi(private_notify_payload_t
*this)
269 * Implements notify_payload_t's set_spi function.
270 * See #notify_payload_s.set_spi for description.
272 status_t
set_spi(private_notify_payload_t
*this, chunk_t spi
)
274 /* destroy existing data first */
275 if (this->spi
.ptr
!= NULL
)
277 /* free existing value */
278 allocator_free(this->spi
.ptr
);
279 this->spi
.ptr
= NULL
;
284 this->spi
.ptr
= allocator_clone_bytes(spi
.ptr
,spi
.len
);
285 if (this->spi
.ptr
== NULL
)
289 this->spi
.len
= spi
.len
;
290 this->spi_size
= spi
.len
;
291 this->compute_length(this);
298 * Implements notify_payload_t's get_notification_data function.
299 * See #notify_payload_s.get_notification_data for description.
301 chunk_t
get_notification_data(private_notify_payload_t
*this)
303 return (this->notification_data
);
307 * Implements notify_payload_t's get_notification_data function.
308 * See #notify_payload_s.get_notification_data for description.
310 status_t
set_notification_data(private_notify_payload_t
*this, chunk_t notification_data
)
312 /* destroy existing data first */
313 if (this->notification_data
.ptr
!= NULL
)
315 /* free existing value */
316 allocator_free(this->notification_data
.ptr
);
317 this->notification_data
.ptr
= NULL
;
318 this->notification_data
.len
= 0;
322 this->notification_data
.ptr
= allocator_clone_bytes(notification_data
.ptr
,notification_data
.len
);
323 if (this->notification_data
.ptr
== NULL
)
327 this->notification_data
.len
= notification_data
.len
;
328 this->compute_length(this);
334 * Described in header
336 notify_payload_t
*notify_payload_create()
338 private_notify_payload_t
*this = allocator_alloc_thing(private_notify_payload_t
);
343 /* interface functions */
344 this->public.payload_interface
.get_encoding_rules
= (status_t (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
345 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
346 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
347 this->public.payload_interface
.set_next_type
= (status_t (*) (payload_t
*,payload_type_t
)) set_next_type
;
348 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
349 this->public.payload_interface
.destroy
= (status_t (*) (payload_t
*))destroy
;
351 /* public functions */
352 this->public.get_protocol_id
= (u_int8_t (*) (notify_payload_t
*)) get_protocol_id
;
353 this->public.set_protocol_id
= (status_t (*) (notify_payload_t
*,u_int8_t
)) set_protocol_id
;
354 this->public.get_notify_message_type
= (u_int16_t (*) (notify_payload_t
*)) get_notify_message_type
;
355 this->public.set_notify_message_type
= (status_t (*) (notify_payload_t
*,u_int16_t
)) set_notify_message_type
;
356 this->public.get_spi
= (chunk_t (*) (notify_payload_t
*)) get_spi
;
357 this->public.set_spi
= (status_t (*) (notify_payload_t
*,chunk_t
)) set_spi
;
358 this->public.get_notification_data
= (chunk_t (*) (notify_payload_t
*)) get_notification_data
;
359 this->public.set_notification_data
= (status_t (*) (notify_payload_t
*,chunk_t
)) set_notification_data
;
360 this->public.destroy
= (status_t (*) (notify_payload_t
*)) destroy
;
362 /* private functions */
363 this->compute_length
= compute_length
;
365 /* set default values of the fields */
366 this->critical
= NOTIFY_PAYLOAD_CRITICAL_FLAG
;
367 this->next_payload
= NO_PAYLOAD
;
368 this->payload_length
= NOTIFY_PAYLOAD_HEADER_LENGTH
;
369 this->protocol_id
= 0;
370 this->notify_message_type
= 0;
371 this->spi
.ptr
= NULL
;
373 this->notification_data
.ptr
= NULL
;
374 this->notification_data
.len
= 0;
376 return (&(this->public));