- memory allocation now works with allocator-functions...
[strongswan.git] / Source / charon / generator.c
1 /**
2 * @file generator.c
3 *
4 * @brief Generic generator class used to generate IKEv2-Header and Payload
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 #include <freeswan.h>
25 #include <pluto/constants.h>
26 #include <pluto/defs.h>
27
28 #include "allocator.h"
29 #include "types.h"
30 #include "generator.h"
31
32 /**
33 * Private data of a generator_t object
34 */
35 typedef struct private_generator_s private_generator_t;
36
37 struct private_generator_s {
38 /**
39 * Public part of a generator object
40 */
41 generator_t public;
42
43 /* private functions and fields */
44
45 /**
46 * Generates a chunk_t with specific encoding rules
47 *
48 * items are bytewhise written
49 *
50 * @param this private_generator_t-object
51 * @param data_struct data_struct to read data from
52 * @param encoding_rules pointer to first encoding_rule of encoding rules array
53 * @param encoding_rules_count number of encoding rules in encoding rules array
54 * @param data pointer to chunk where to write the data in
55 *
56 * @return SUCCESS if succeeded,
57 * OUT_OF_RES if out of ressources
58 */
59 status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data);
60
61 /**
62 * TODO
63 */
64 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int8_t **buffer,u_int8_t **out_position,u_int8_t **roof_position,size_t *current_bit);
65
66 /**
67 * Pointer to the payload informations needed to automatic
68 * generate a specific payload type
69 */
70 payload_info_t **payload_infos;
71 };
72
73
74 /**
75 * implements private_generator_t's generate_u_int_type function
76 */
77
78 static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int8_t **buffer,u_int8_t **out_position,u_int8_t **roof_position,size_t *current_bit)
79 {
80 size_t number_of_bits = 0;
81
82 switch (int_type)
83 {
84 case U_INT_4:
85 number_of_bits = 4;
86 break;
87 case U_INT_8:
88 number_of_bits = 8;
89 break;
90 case U_INT_16:
91 number_of_bits = 16;
92 break;
93 case U_INT_32:
94 number_of_bits = 32;
95 break;
96 case U_INT_64:
97 number_of_bits = 64;
98 break;
99 default:
100 return FAILED;
101 }
102 return SUCCESS;
103 }
104
105 /**
106 * implements private_generator_t's generate function
107 */
108 static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data)
109 {
110 u_int8_t * buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
111 u_int8_t * out_position = buffer;
112 u_int8_t * roof_position = buffer + GENERATOR_DATA_BUFFER_SIZE;
113 size_t current_bit = 0;
114 int i;
115
116 if (buffer == NULL)
117 {
118 return OUT_OF_RES;
119 }
120 for (i = 0; i < encoding_rules_count;i++)
121 {
122 status_t status = SUCCESS;
123 switch (encoding_rules[i].type)
124 {
125 case U_INT_4:
126 case U_INT_8:
127 case U_INT_16:
128 case U_INT_32:
129 case U_INT_64:
130 status = this->generate_u_int_type(this,encoding_rules[i].type,&buffer,&out_position,&roof_position,&current_bit);
131 break;
132 case RESERVED_BIT:
133 case RESERVED_BYTE:
134 case FLAG:
135 case LENGTH:
136 case SPI_SIZE:
137 default:
138 break;
139 }
140 if (status != SUCCESS)
141 {
142 allocator_free(buffer);
143 return status;
144 }
145 }
146
147 return SUCCESS;
148 }
149
150 static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, chunk_t *data)
151 {
152 int i;
153
154 /* check every payload info for specific type */
155 for (i = 0; this->payload_infos[i] != NULL; i++)
156 {
157 if (this->payload_infos[i]->payload_type == payload_type)
158 {
159 /* found payload informations, generating is done in private function generate() */
160 return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,data));
161 }
162 }
163 return NOT_SUPPORTED;
164 }
165
166 /**
167 * Implementation of generator_t's destroy function
168 */
169 static status_t destroy(private_generator_t *this)
170 {
171 if (this == NULL)
172 {
173 return FAILED;
174 }
175
176 allocator_free(this);
177 return SUCCESS;
178 }
179
180 /*
181 * Described in header
182 */
183 generator_t * generator_create(payload_info_t ** payload_infos)
184 {
185 private_generator_t *this;
186
187 if (payload_infos == NULL)
188 {
189 return NULL;
190 }
191
192 this = allocator_alloc_thing(private_generator_t);
193 if (this == NULL)
194 {
195 return NULL;
196 }
197
198 this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, chunk_t *)) generate_payload;
199 this->public.destroy = (status_t(*)(generator_t*)) destroy;
200
201 /* initiate private fields */
202 this->generate = generate;
203 this->generate_u_int_type = generate_u_int_type;
204
205 this->payload_infos = payload_infos;
206
207 return &(this->public);
208 }