- defined all substructures for a SA payload
[strongswan.git] / Source / charon / payloads / transform_substructure.c
1 /**
2 * @file transform_substructure.h
3 *
4 * @brief Declaration of the class transform_substructure_t.
5 *
6 * An object of this type represents an IKEv2 TRANSFORM Substructure and contains Attributes.
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 /* offsetof macro */
26 #include <stddef.h>
27
28 #include "transform_substructure.h"
29
30 #include "transform_attribute.h"
31 #include "encodings.h"
32 #include "../types.h"
33 #include "../utils/allocator.h"
34 #include "../utils/linked_list.h"
35
36 /**
37 * Private data of an transform_substructure_t' Object
38 *
39 */
40 typedef struct private_transform_substructure_s private_transform_substructure_t;
41
42 struct private_transform_substructure_s {
43 /**
44 * public transform_substructure_t interface
45 */
46 transform_substructure_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53
54 /**
55 * Length of this payload
56 */
57 u_int16_t transform_length;
58
59
60 /**
61 * Type of the transform
62 */
63 u_int8_t transform_type;
64
65 /**
66 * Transform ID
67 */
68 u_int8_t transform_id;
69
70 /**
71 * Transforms Attributes are stored in a linked_list_t
72 */
73 linked_list_t * attributes;
74 };
75
76
77 /**
78 * Encoding rules to parse or generate a Transform substructure
79 *
80 * The defined offsets are the positions in a object of type
81 * private_transform_substructure_t.
82 *
83 */
84 encoding_rule_t transform_substructure_encodings[] = {
85 /* 1 Byte next payload type, stored in the field next_payload */
86 { U_INT_8, offsetof(private_transform_substructure_t, next_payload) },
87 /* Reserved Byte is skipped */
88 { RESERVED_BYTE, 0 },
89 /* Length of the whole transform substructure*/
90 { PAYLOAD_LENGTH, offsetof(private_transform_substructure_t, transform_length) },
91 /* transform type is a number of 8 bit */
92 { U_INT_8, offsetof(private_transform_substructure_t, transform_type) },
93 /* Reserved Byte is skipped */
94 { RESERVED_BYTE, 0 },
95 /* tranform ID is a number of 8 bit */
96 { U_INT_8, offsetof(private_transform_substructure_t, transform_id) },
97 /* Attributes are stored in a transform attribute,
98 offset points to a linked_list_t pointer */
99 { TRANSFORM_ATTRIBUTES, offsetof(private_transform_substructure_t, attributes) }
100 };
101
102 /**
103 * Implements payload_t's and transform_substructure_t's destroy function.
104 * See #payload_s.destroy or transform_substructure_s.destroy for description.
105 */
106 static status_t destroy(private_transform_substructure_t *this)
107 {
108 /* all proposals are getting destroyed */
109 while (this->attributes->get_count(this->attributes) > 0)
110 {
111 transform_attribute_t *current_attribute;
112 if (this->attributes->remove_last(this->attributes,(void **)&current_attribute) != SUCCESS)
113 {
114 break;
115 }
116 current_attribute->destroy(current_attribute);
117 }
118 this->attributes->destroy(this->attributes);
119
120 allocator_free(this);
121
122 return SUCCESS;
123 }
124
125 /**
126 * Implements payload_t's get_encoding_rules function.
127 * See #payload_s.get_encoding_rules for description.
128 */
129 static status_t get_encoding_rules(private_transform_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
130 {
131 *rules = transform_substructure_encodings;
132 *rule_count = sizeof(transform_substructure_encodings) / sizeof(encoding_rule_t);
133
134 return SUCCESS;
135 }
136
137 /**
138 * Implements payload_t's get_type function.
139 * See #payload_s.get_type for description.
140 */
141 static payload_type_t get_type(private_transform_substructure_t *this)
142 {
143 return TRANSFORM_SUBSTRUCTURE;
144 }
145
146 /**
147 * Implements payload_t's get_next_type function.
148 * See #payload_s.get_next_type for description.
149 */
150 static payload_type_t get_next_type(private_transform_substructure_t *this)
151 {
152 return (this->next_payload);
153 }
154
155 /**
156 * Implements payload_t's get_length function.
157 * See #payload_s.get_length for description.
158 */
159 static size_t get_length(private_transform_substructure_t *this)
160 {
161 return this->transform_length;
162 }
163
164 /**
165 * Implements transform_substructure_t's create_transform_attribute_iterator function.
166 * See #transform_substructure_s.create_transform_attribute_iterator for description.
167 */
168 static status_t create_transform_attribute_iterator (private_transform_substructure_t *this,linked_list_iterator_t **iterator,bool forward)
169 {
170 return (this->attributes->create_iterator(this->attributes,iterator,forward));
171 }
172
173 /**
174 * Implements transform_substructure_t's add_transform_attribute function.
175 * See #transform_substructure_s.add_transform_attribute for description.
176 */
177 static status_t add_transform_attribute (private_transform_substructure_t *this,transform_attribute_t *attribute)
178 {
179 return (this->attributes->insert_last(this->attributes,(void *) attribute));
180 }
181
182
183 /*
184 * Described in header
185 */
186 transform_substructure_t *transform_substructure_create()
187 {
188 private_transform_substructure_t *this = allocator_alloc_thing(private_transform_substructure_t);
189 if (this == NULL)
190 {
191 return NULL;
192 }
193
194 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
195 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
196 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
197 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
198 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
199 this->public.create_transform_attribute_iterator = (status_t (*) (transform_substructure_t *,linked_list_iterator_t **,bool)) create_transform_attribute_iterator;
200 this->public.add_transform_attribute = (status_t (*) (transform_substructure_t *,transform_attribute_t *)) add_transform_attribute;
201 this->public.destroy = (status_t (*) (transform_substructure_t *)) destroy;
202
203 /* set default values of the fields */
204 this->next_payload = NO_PAYLOAD;
205 this->transform_length = 0;
206 this->transform_id = 0;
207 this->transform_type = 0;
208
209 this->attributes = linked_list_create();
210
211 if (this->attributes == NULL)
212 {
213 allocator_free(this);
214 return NULL;
215 }
216 return (&(this->public));
217 }