payload: Use common prefixes for all payload type identifiers
[strongswan.git] / src / libcharon / encoding / payloads / unknown_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
17 #include <stddef.h>
18
19 #include "unknown_payload.h"
20
21 typedef struct private_unknown_payload_t private_unknown_payload_t;
22
23 /**
24 * Private data of an unknown_payload_t object.
25 */
26 struct private_unknown_payload_t {
27
28 /**
29 * Public unknown_payload_t interface.
30 */
31 unknown_payload_t public;
32
33 /**
34 * Type of this payload
35 */
36 payload_type_t type;
37
38 /**
39 * Next payload type.
40 */
41 u_int8_t next_payload;
42
43 /**
44 * Critical flag.
45 */
46 bool critical;
47
48 /**
49 * Reserved bits
50 */
51 bool reserved[7];
52
53 /**
54 * Length of this payload.
55 */
56 u_int16_t payload_length;
57
58 /**
59 * The contained data.
60 */
61 chunk_t data;
62 };
63
64 /**
65 * Encoding rules to parse an payload which is not further specified.
66 *
67 * The defined offsets are the positions in a object of type
68 * private_unknown_payload_t.
69 *
70 */
71 static encoding_rule_t encodings[] = {
72 /* 1 Byte next payload type, stored in the field next_payload */
73 { U_INT_8, offsetof(private_unknown_payload_t, next_payload) },
74 /* the critical bit */
75 { FLAG, offsetof(private_unknown_payload_t, critical) },
76 /* 7 Bit reserved bits */
77 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[0]) },
78 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[1]) },
79 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[2]) },
80 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[3]) },
81 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[4]) },
82 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[5]) },
83 { RESERVED_BIT, offsetof(private_unknown_payload_t, reserved[6]) },
84 /* Length of the whole payload*/
85 { PAYLOAD_LENGTH, offsetof(private_unknown_payload_t, payload_length) },
86 /* some unknown data bytes, length is defined in PAYLOAD_LENGTH */
87 { CHUNK_DATA, offsetof(private_unknown_payload_t, data) },
88 };
89
90 /*
91 1 2 3
92 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
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! Next Payload !C! RESERVED ! Payload Length !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 ! !
97 ~ Data of any type ~
98 ! !
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 */
101
102 METHOD(payload_t, verify, status_t,
103 private_unknown_payload_t *this)
104 {
105 return SUCCESS;
106 }
107
108 METHOD(payload_t, get_encoding_rules, int,
109 private_unknown_payload_t *this, encoding_rule_t **rules)
110 {
111 *rules = encodings;
112 return countof(encodings);
113 }
114
115 METHOD(payload_t, get_header_length, int,
116 private_unknown_payload_t *this)
117 {
118 return 4;
119 }
120
121 METHOD(payload_t, get_payload_type, payload_type_t,
122 private_unknown_payload_t *this)
123 {
124 return this->type;
125 }
126
127 METHOD(payload_t, get_next_type, payload_type_t,
128 private_unknown_payload_t *this)
129 {
130 return this->next_payload;
131 }
132
133 METHOD(payload_t, set_next_type, void,
134 private_unknown_payload_t *this,payload_type_t type)
135 {
136 this->next_payload = type;
137 }
138
139 METHOD(payload_t, get_length, size_t,
140 private_unknown_payload_t *this)
141 {
142 return this->payload_length;
143 }
144
145 METHOD(unknown_payload_t, is_critical, bool,
146 private_unknown_payload_t *this)
147 {
148 return this->critical;
149 }
150
151 METHOD(unknown_payload_t, get_data, chunk_t,
152 private_unknown_payload_t *this)
153 {
154 return this->data;
155 }
156
157 METHOD2(payload_t, unknown_payload_t, destroy, void,
158 private_unknown_payload_t *this)
159 {
160 free(this->data.ptr);
161 free(this);
162 }
163
164 /*
165 * Described in header
166 */
167 unknown_payload_t *unknown_payload_create(payload_type_t type)
168 {
169 private_unknown_payload_t *this;
170
171 INIT(this,
172 .public = {
173 .payload_interface = {
174 .verify = _verify,
175 .get_encoding_rules = _get_encoding_rules,
176 .get_header_length = _get_header_length,
177 .get_length = _get_length,
178 .get_next_type = _get_next_type,
179 .set_next_type = _set_next_type,
180 .get_type = _get_payload_type,
181 .destroy = _destroy,
182 },
183 .is_critical = _is_critical,
184 .get_data = _get_data,
185 .destroy = _destroy,
186 },
187 .next_payload = PL_NONE,
188 .payload_length = get_header_length(this),
189 .type = type,
190 );
191
192 return &this->public;
193 }
194
195
196 /*
197 * Described in header
198 */
199 unknown_payload_t *unknown_payload_create_data(payload_type_t type,
200 bool critical, chunk_t data)
201 {
202 private_unknown_payload_t *this;
203
204 this = (private_unknown_payload_t*)unknown_payload_create(type);
205 this->data = data;
206 this->critical = critical;
207 this->payload_length = get_header_length(this) + data.len;
208
209 return &this->public;
210 }