4 * @brief Declaration of the class sa_payload_t.
6 * An object of this type represents an IKEv2 SA-Payload and contains proposal
12 * Copyright (C) 2005 Jan Hutter, Martin Willi
13 * Hochschule fuer Technik Rapperswil
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 #include "sa_payload.h"
31 #include "encodings.h"
32 #include "../utils/allocator.h"
33 #include "../utils/linked_list.h"
37 * Private data of an sa_payload_t' Object
40 typedef struct private_sa_payload_s private_sa_payload_t
;
42 struct private_sa_payload_s
{
44 * public sa_payload_t interface
51 u_int8_t next_payload
;
59 * Length of this payload
61 u_int16_t payload_length
;
64 * Proposals in this payload are stored in a linked_list_t
66 linked_list_t
* proposals
;
69 * @brief Computes the length of this payload.
71 * @param this calling private_sa_payload_t object
75 status_t (*compute_length
) (private_sa_payload_t
*this);
79 * Encoding rules to parse or generate a IKEv2-Header
81 * The defined offsets are the positions in a object of type
82 * private_sa_payload_t.
85 encoding_rule_t sa_payload_encodings
[] = {
86 /* 1 Byte next payload type, stored in the field next_payload */
87 { U_INT_8
, offsetof(private_sa_payload_t
, next_payload
) },
88 /* the critical bit */
89 { FLAG
, offsetof(private_sa_payload_t
, critical
) },
90 /* 7 Bit reserved bits, nowhere stored */
98 /* Length of the whole SA payload*/
99 { PAYLOAD_LENGTH
, offsetof(private_sa_payload_t
, payload_length
) },
100 /* Proposals are stored in a proposal substructure,
101 offset points to a linked_list_t pointer */
102 { PROPOSALS
, offsetof(private_sa_payload_t
, proposals
) }
106 * Implements payload_t's and sa_payload_t's destroy function.
107 * See #payload_s.destroy or sa_payload_s.destroy for description.
109 static status_t
destroy(private_sa_payload_t
*this)
111 /* all proposals are getting destroyed */
112 while (this->proposals
->get_count(this->proposals
) > 0)
114 proposal_substructure_t
*current_proposal
;
115 if (this->proposals
->remove_last(this->proposals
,(void **)¤t_proposal
) != SUCCESS
)
119 current_proposal
->destroy(current_proposal
);
121 this->proposals
->destroy(this->proposals
);
123 allocator_free(this);
129 * Implements payload_t's get_encoding_rules function.
130 * See #payload_s.get_encoding_rules for description.
132 static status_t
get_encoding_rules(private_sa_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
134 *rules
= sa_payload_encodings
;
135 *rule_count
= sizeof(sa_payload_encodings
) / sizeof(encoding_rule_t
);
141 * Implements payload_t's get_type function.
142 * See #payload_s.get_type for description.
144 static payload_type_t
get_type(private_sa_payload_t
*this)
146 return SECURITY_ASSOCIATION
;
150 * Implements payload_t's get_next_type function.
151 * See #payload_s.get_next_type for description.
153 static payload_type_t
get_next_type(private_sa_payload_t
*this)
155 return (this->next_payload
);
159 * Implements payload_t's set_next_type function.
160 * See #payload_s.set_next_type for description.
162 static status_t
set_next_type(private_sa_payload_t
*this,payload_type_t type
)
164 this->next_payload
= type
;
169 * Implements payload_t's get_length function.
170 * See #payload_s.get_length for description.
172 static size_t get_length(private_sa_payload_t
*this)
174 this->compute_length(this);
175 return this->payload_length
;
179 * Implements sa_payload_t's create_proposal_substructure_iterator function.
180 * See #sa_payload_s.create_proposal_substructure_iterator for description.
182 static status_t
create_proposal_substructure_iterator (private_sa_payload_t
*this,linked_list_iterator_t
**iterator
,bool forward
)
184 return (this->proposals
->create_iterator(this->proposals
,iterator
,forward
));
188 * Implements sa_payload_t's add_proposal_substructure function.
189 * See #sa_payload_s.add_proposal_substructure for description.
191 static status_t
add_proposal_substructure (private_sa_payload_t
*this,proposal_substructure_t
*proposal
)
194 status
= this->proposals
->insert_last(this->proposals
,(void *) proposal
);
195 this->compute_length(this);
200 * Implements private_sa_payload_t's compute_length function.
201 * See #private_sa_payload_s.compute_length for description.
203 static status_t
compute_length (private_sa_payload_t
*this)
205 linked_list_iterator_t
*iterator
;
207 size_t length
= SA_PAYLOAD_HEADER_LENGTH
;
208 status
= this->proposals
->create_iterator(this->proposals
,&iterator
,TRUE
);
209 if (status
!= SUCCESS
)
213 while (iterator
->has_next(iterator
))
215 payload_t
*current_proposal
;
216 iterator
->current(iterator
,(void **) ¤t_proposal
);
217 length
+= current_proposal
->get_length(current_proposal
);
219 iterator
->destroy(iterator
);
221 this->payload_length
= length
;
227 * Described in header
229 sa_payload_t
*sa_payload_create()
231 private_sa_payload_t
*this = allocator_alloc_thing(private_sa_payload_t
);
237 this->public.payload_interface
.get_encoding_rules
= (status_t (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
238 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
239 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
240 this->public.payload_interface
.set_next_type
= (status_t (*) (payload_t
*,payload_type_t
)) set_next_type
;
241 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
242 this->public.payload_interface
.destroy
= (status_t (*) (payload_t
*))destroy
;
243 this->public.create_proposal_substructure_iterator
= (status_t (*) (sa_payload_t
*,linked_list_iterator_t
**,bool)) create_proposal_substructure_iterator
;
244 this->public.add_proposal_substructure
= (status_t (*) (sa_payload_t
*,proposal_substructure_t
*)) add_proposal_substructure
;
245 this->public.destroy
= (status_t (*) (sa_payload_t
*)) destroy
;
247 /* private functions */
248 this->compute_length
= compute_length
;
250 /* set default values of the fields */
251 this->critical
= SA_PAYLOAD_CRITICAL_FLAG
;
252 this->next_payload
= NO_PAYLOAD
;
253 this->payload_length
= SA_PAYLOAD_HEADER_LENGTH
;
255 this->proposals
= linked_list_create();
257 if (this->proposals
== NULL
)
259 allocator_free(this);
262 return (&(this->public));