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