]> git.ipfire.org Git - people/ms/strongswan.git/blob - programs/charon/charon/encoding/payloads/vendor_id_payload.c
- import of strongswan-2.7.0
[people/ms/strongswan.git] / programs / charon / charon / encoding / payloads / vendor_id_payload.c
1 /**
2 * @file vendor_id_payload.c
3 *
4 * @brief Implementation of vendor_id_payload_t.
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 <stddef.h>
24
25 #include "vendor_id_payload.h"
26
27
28 typedef struct private_vendor_id_payload_t private_vendor_id_payload_t;
29
30 /**
31 * Private data of an vendor_id_payload_t object.
32 *
33 */
34 struct private_vendor_id_payload_t {
35 /**
36 * Public vendor_id_payload_t interface.
37 */
38 vendor_id_payload_t public;
39
40 /**
41 * Next payload type.
42 */
43 u_int8_t next_payload;
44
45 /**
46 * Critical flag.
47 */
48 bool critical;
49
50 /**
51 * Length of this payload.
52 */
53 u_int16_t payload_length;
54
55 /**
56 * The contained vendor_id data value.
57 */
58 chunk_t vendor_id_data;
59 };
60
61 /**
62 * Encoding rules to parse or generate a VENDOR ID payload
63 *
64 * The defined offsets are the positions in a object of type
65 * private_vendor_id_payload_t.
66 *
67 */
68 encoding_rule_t vendor_id_payload_encodings[] = {
69 /* 1 Byte next payload type, stored in the field next_payload */
70 { U_INT_8, offsetof(private_vendor_id_payload_t, next_payload) },
71 /* the critical bit */
72 { FLAG, offsetof(private_vendor_id_payload_t, critical) },
73 /* 7 Bit reserved bits, nowhere stored */
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 { RESERVED_BIT, 0 },
80 { RESERVED_BIT, 0 },
81 /* Length of the whole payload*/
82 { PAYLOAD_LENGTH, offsetof(private_vendor_id_payload_t, payload_length)},
83 /* some vendor_id data bytes, length is defined in PAYLOAD_LENGTH */
84 { VID_DATA, offsetof(private_vendor_id_payload_t, vendor_id_data) }
85 };
86
87 /*
88 1 2 3
89 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
90 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 ! Next Payload !C! RESERVED ! Payload Length !
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 ! Cert Encoding ! !
94 +-+-+-+-+-+-+-+-+ !
95 ~ Certificate Data ~
96 ! !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 */
99
100 /**
101 * Implementation of payload_t.verify.
102 */
103 static status_t verify(private_vendor_id_payload_t *this)
104 {
105 return SUCCESS;
106 }
107
108 /**
109 * Implementation of vendor_id_payload_t.get_encoding_rules.
110 */
111 static void get_encoding_rules(private_vendor_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
112 {
113 *rules = vendor_id_payload_encodings;
114 *rule_count = sizeof(vendor_id_payload_encodings) / sizeof(encoding_rule_t);
115 }
116
117 /**
118 * Implementation of payload_t.get_type.
119 */
120 static payload_type_t get_payload_type(private_vendor_id_payload_t *this)
121 {
122 return VENDOR_ID;
123 }
124
125 /**
126 * Implementation of payload_t.get_next_type.
127 */
128 static payload_type_t get_next_type(private_vendor_id_payload_t *this)
129 {
130 return (this->next_payload);
131 }
132
133 /**
134 * Implementation of payload_t.set_next_type.
135 */
136 static void set_next_type(private_vendor_id_payload_t *this,payload_type_t type)
137 {
138 this->next_payload = type;
139 }
140
141 /**
142 * Implementation of payload_t.get_length.
143 */
144 static size_t get_length(private_vendor_id_payload_t *this)
145 {
146 return this->payload_length;
147 }
148
149 /**
150 * Implementation of vendor_id_payload_t.set_data.
151 */
152 static void set_data (private_vendor_id_payload_t *this, chunk_t data)
153 {
154 if (this->vendor_id_data.ptr != NULL)
155 {
156 chunk_free(&(this->vendor_id_data));
157 }
158 this->vendor_id_data.ptr = clalloc(data.ptr,data.len);
159 this->vendor_id_data.len = data.len;
160 this->payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH + this->vendor_id_data.len;
161 }
162
163 /**
164 * Implementation of vendor_id_payload_t.get_data.
165 */
166 static chunk_t get_data (private_vendor_id_payload_t *this)
167 {
168 return (this->vendor_id_data);
169 }
170
171 /**
172 * Implementation of vendor_id_payload_t.get_data_clone.
173 */
174 static chunk_t get_data_clone (private_vendor_id_payload_t *this)
175 {
176 chunk_t cloned_data;
177 if (this->vendor_id_data.ptr == NULL)
178 {
179 return (this->vendor_id_data);
180 }
181 cloned_data.ptr = clalloc(this->vendor_id_data.ptr,this->vendor_id_data.len);
182 cloned_data.len = this->vendor_id_data.len;
183 return cloned_data;
184 }
185
186 /**
187 * Implementation of payload_t.destroy and vendor_id_payload_t.destroy.
188 */
189 static void destroy(private_vendor_id_payload_t *this)
190 {
191 if (this->vendor_id_data.ptr != NULL)
192 {
193 chunk_free(&(this->vendor_id_data));
194 }
195 free(this);
196 }
197
198 /*
199 * Described in header
200 */
201 vendor_id_payload_t *vendor_id_payload_create()
202 {
203 private_vendor_id_payload_t *this = malloc_thing(private_vendor_id_payload_t);
204
205 /* interface functions */
206 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
207 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
208 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
209 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
210 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
211 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
212 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
213
214 /* public functions */
215 this->public.destroy = (void (*) (vendor_id_payload_t *)) destroy;
216 this->public.set_data = (void (*) (vendor_id_payload_t *,chunk_t)) set_data;
217 this->public.get_data_clone = (chunk_t (*) (vendor_id_payload_t *)) get_data_clone;
218 this->public.get_data = (chunk_t (*) (vendor_id_payload_t *)) get_data;
219
220 /* private variables */
221 this->critical = FALSE;
222 this->next_payload = NO_PAYLOAD;
223 this->payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH;
224 this->vendor_id_data = CHUNK_INITIALIZER;
225
226 return (&(this->public));
227 }