Added a hook to set the critical bit on arbitrary payloads
[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 * Length of this payload.
50 */
51 u_int16_t payload_length;
52
53 /**
54 * The contained data.
55 */
56 chunk_t data;
57 };
58
59 /**
60 * Encoding rules to parse an payload which is not further specified.
61 *
62 * The defined offsets are the positions in a object of type
63 * private_unknown_payload_t.
64 *
65 */
66 encoding_rule_t unknown_payload_encodings[] = {
67 /* 1 Byte next payload type, stored in the field next_payload */
68 { U_INT_8, offsetof(private_unknown_payload_t, next_payload) },
69 /* the critical bit */
70 { FLAG, offsetof(private_unknown_payload_t, critical) },
71 /* 7 Bit reserved bits, nowhere stored */
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 /* Length of the whole payload*/
80 { PAYLOAD_LENGTH, offsetof(private_unknown_payload_t, payload_length) },
81 /* some unknown data bytes, length is defined in PAYLOAD_LENGTH */
82 { UNKNOWN_DATA, offsetof(private_unknown_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 ~ Data of any type ~
93 ! !
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 */
96
97 METHOD(payload_t, verify, status_t,
98 private_unknown_payload_t *this)
99 {
100 return SUCCESS;
101 }
102
103 METHOD(payload_t, get_encoding_rules, void,
104 private_unknown_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
105 {
106 *rules = unknown_payload_encodings;
107 *rule_count = sizeof(unknown_payload_encodings) / sizeof(encoding_rule_t);
108 }
109
110 METHOD(payload_t, get_payload_type, payload_type_t,
111 private_unknown_payload_t *this)
112 {
113 return this->type;
114 }
115
116 METHOD(payload_t, get_next_type, payload_type_t,
117 private_unknown_payload_t *this)
118 {
119 return this->next_payload;
120 }
121
122 METHOD(payload_t, set_next_type, void,
123 private_unknown_payload_t *this,payload_type_t type)
124 {
125 this->next_payload = type;
126 }
127
128 METHOD(payload_t, get_length, size_t,
129 private_unknown_payload_t *this)
130 {
131 return this->payload_length;
132 }
133
134 METHOD(unknown_payload_t, is_critical, bool,
135 private_unknown_payload_t *this)
136 {
137 return this->critical;
138 }
139
140 METHOD(unknown_payload_t, get_data, chunk_t,
141 private_unknown_payload_t *this)
142 {
143 return this->data;
144 }
145
146 METHOD2(payload_t, unknown_payload_t, destroy, void,
147 private_unknown_payload_t *this)
148 {
149 free(this->data.ptr);
150 free(this);
151 }
152
153 /*
154 * Described in header
155 */
156 unknown_payload_t *unknown_payload_create(payload_type_t type)
157 {
158 private_unknown_payload_t *this;
159
160 INIT(this,
161 .public = {
162 .payload_interface = {
163 .verify = _verify,
164 .get_encoding_rules = _get_encoding_rules,
165 .get_length = _get_length,
166 .get_next_type = _get_next_type,
167 .set_next_type = _set_next_type,
168 .get_type = _get_payload_type,
169 .destroy = _destroy,
170 },
171 .is_critical = _is_critical,
172 .get_data = _get_data,
173 .destroy = _destroy,
174 },
175 .next_payload = NO_PAYLOAD,
176 .payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH,
177 .type = type,
178 );
179
180 return &this->public;
181 }
182
183
184 /*
185 * Described in header
186 */
187 unknown_payload_t *unknown_payload_create_data(payload_type_t type,
188 bool critical, chunk_t data)
189 {
190 private_unknown_payload_t *this;
191
192 this = (private_unknown_payload_t*)unknown_payload_create(type);
193 this->data = data;
194 this->critical = critical;
195
196 return &this->public;
197 }