updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / vendor_id_payload.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <stddef.h>
20
21 #include "vendor_id_payload.h"
22
23
24 typedef struct private_vendor_id_payload_t private_vendor_id_payload_t;
25
26 /**
27 * Private data of an vendor_id_payload_t object.
28 *
29 */
30 struct private_vendor_id_payload_t {
31 /**
32 * Public vendor_id_payload_t interface.
33 */
34 vendor_id_payload_t public;
35
36 /**
37 * Next payload type.
38 */
39 u_int8_t next_payload;
40
41 /**
42 * Critical flag.
43 */
44 bool critical;
45
46 /**
47 * Length of this payload.
48 */
49 u_int16_t payload_length;
50
51 /**
52 * The contained vendor_id data value.
53 */
54 chunk_t vendor_id_data;
55 };
56
57 /**
58 * Encoding rules to parse or generate a VENDOR ID payload
59 *
60 * The defined offsets are the positions in a object of type
61 * private_vendor_id_payload_t.
62 *
63 */
64 encoding_rule_t vendor_id_payload_encodings[] = {
65 /* 1 Byte next payload type, stored in the field next_payload */
66 { U_INT_8, offsetof(private_vendor_id_payload_t, next_payload) },
67 /* the critical bit */
68 { FLAG, offsetof(private_vendor_id_payload_t, critical) },
69 /* 7 Bit reserved bits, nowhere stored */
70 { RESERVED_BIT, 0 },
71 { RESERVED_BIT, 0 },
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 /* Length of the whole payload*/
78 { PAYLOAD_LENGTH, offsetof(private_vendor_id_payload_t, payload_length)},
79 /* some vendor_id data bytes, length is defined in PAYLOAD_LENGTH */
80 { VID_DATA, offsetof(private_vendor_id_payload_t, vendor_id_data) }
81 };
82
83 /*
84 1 2 3
85 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
86 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 ! Next Payload !C! RESERVED ! Payload Length !
88 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 + !
90 ~ VID Data ~
91 ! !
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 */
94
95 /**
96 * Implementation of payload_t.verify.
97 */
98 static status_t verify(private_vendor_id_payload_t *this)
99 {
100 return SUCCESS;
101 }
102
103 /**
104 * Implementation of vendor_id_payload_t.get_encoding_rules.
105 */
106 static void get_encoding_rules(private_vendor_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
107 {
108 *rules = vendor_id_payload_encodings;
109 *rule_count = sizeof(vendor_id_payload_encodings) / sizeof(encoding_rule_t);
110 }
111
112 /**
113 * Implementation of payload_t.get_type.
114 */
115 static payload_type_t get_payload_type(private_vendor_id_payload_t *this)
116 {
117 return VENDOR_ID;
118 }
119
120 /**
121 * Implementation of payload_t.get_next_type.
122 */
123 static payload_type_t get_next_type(private_vendor_id_payload_t *this)
124 {
125 return (this->next_payload);
126 }
127
128 /**
129 * Implementation of payload_t.set_next_type.
130 */
131 static void set_next_type(private_vendor_id_payload_t *this,payload_type_t type)
132 {
133 this->next_payload = type;
134 }
135
136 /**
137 * Implementation of payload_t.get_length.
138 */
139 static size_t get_length(private_vendor_id_payload_t *this)
140 {
141 return this->payload_length;
142 }
143
144 /**
145 * Implementation of vendor_id_payload_t.set_data.
146 */
147 static void set_data (private_vendor_id_payload_t *this, chunk_t data)
148 {
149 if (this->vendor_id_data.ptr != NULL)
150 {
151 chunk_free(&(this->vendor_id_data));
152 }
153 this->vendor_id_data.ptr = clalloc(data.ptr,data.len);
154 this->vendor_id_data.len = data.len;
155 this->payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH + this->vendor_id_data.len;
156 }
157
158 /**
159 * Implementation of vendor_id_payload_t.get_data.
160 */
161 static chunk_t get_data (private_vendor_id_payload_t *this)
162 {
163 return (this->vendor_id_data);
164 }
165
166 /**
167 * Implementation of vendor_id_payload_t.get_data_clone.
168 */
169 static chunk_t get_data_clone (private_vendor_id_payload_t *this)
170 {
171 chunk_t cloned_data;
172 if (this->vendor_id_data.ptr == NULL)
173 {
174 return (this->vendor_id_data);
175 }
176 cloned_data.ptr = clalloc(this->vendor_id_data.ptr,this->vendor_id_data.len);
177 cloned_data.len = this->vendor_id_data.len;
178 return cloned_data;
179 }
180
181 /**
182 * Implementation of payload_t.destroy and vendor_id_payload_t.destroy.
183 */
184 static void destroy(private_vendor_id_payload_t *this)
185 {
186 if (this->vendor_id_data.ptr != NULL)
187 {
188 chunk_free(&(this->vendor_id_data));
189 }
190 free(this);
191 }
192
193 /*
194 * Described in header
195 */
196 vendor_id_payload_t *vendor_id_payload_create()
197 {
198 private_vendor_id_payload_t *this = malloc_thing(private_vendor_id_payload_t);
199
200 /* interface functions */
201 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
202 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
203 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
204 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
205 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
206 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
207 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
208
209 /* public functions */
210 this->public.destroy = (void (*) (vendor_id_payload_t *)) destroy;
211 this->public.set_data = (void (*) (vendor_id_payload_t *,chunk_t)) set_data;
212 this->public.get_data_clone = (chunk_t (*) (vendor_id_payload_t *)) get_data_clone;
213 this->public.get_data = (chunk_t (*) (vendor_id_payload_t *)) get_data;
214
215 /* private variables */
216 this->critical = FALSE;
217 this->next_payload = NO_PAYLOAD;
218 this->payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH;
219 this->vendor_id_data = chunk_empty;
220
221 return (&(this->public));
222 }