f2c7808857499d51592befeceab7cfee1d40f7bd
[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 if (this->payload_length != UNKNOWN_PAYLOAD_HEADER_LENGTH + this->data.len)
101 {
102 return FAILED;
103 }
104 return SUCCESS;
105 }
106
107 METHOD(payload_t, get_encoding_rules, void,
108 private_unknown_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
109 {
110 *rules = unknown_payload_encodings;
111 *rule_count = sizeof(unknown_payload_encodings) / sizeof(encoding_rule_t);
112 }
113
114 METHOD(payload_t, get_payload_type, payload_type_t,
115 private_unknown_payload_t *this)
116 {
117 return this->type;
118 }
119
120 METHOD(payload_t, get_next_type, payload_type_t,
121 private_unknown_payload_t *this)
122 {
123 return this->next_payload;
124 }
125
126 METHOD(payload_t, set_next_type, void,
127 private_unknown_payload_t *this,payload_type_t type)
128 {
129 this->next_payload = type;
130 }
131
132 METHOD(payload_t, get_length, size_t,
133 private_unknown_payload_t *this)
134 {
135 return this->payload_length;
136 }
137
138 METHOD(unknown_payload_t, is_critical, bool,
139 private_unknown_payload_t *this)
140 {
141 return this->critical;
142 }
143
144 METHOD(unknown_payload_t, get_data, chunk_t,
145 private_unknown_payload_t *this)
146 {
147 return this->data;
148 }
149
150 METHOD2(payload_t, unknown_payload_t, destroy, void,
151 private_unknown_payload_t *this)
152 {
153 free(this->data.ptr);
154 free(this);
155 }
156
157 /*
158 * Described in header
159 */
160 unknown_payload_t *unknown_payload_create(payload_type_t type)
161 {
162 private_unknown_payload_t *this;
163
164 INIT(this,
165 .public = {
166 .payload_interface = {
167 .verify = _verify,
168 .get_encoding_rules = _get_encoding_rules,
169 .get_length = _get_length,
170 .get_next_type = _get_next_type,
171 .set_next_type = _set_next_type,
172 .get_type = _get_payload_type,
173 .destroy = _destroy,
174 },
175 .is_critical = _is_critical,
176 .get_data = _get_data,
177 .destroy = _destroy,
178 },
179 .next_payload = NO_PAYLOAD,
180 .payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH,
181 .type = type,
182 );
183
184 return &this->public;
185 }
186
187
188 /*
189 * Described in header
190 */
191 unknown_payload_t *unknown_payload_create_data(payload_type_t type,
192 bool critical, chunk_t data)
193 {
194 private_unknown_payload_t *this;
195
196 this = (private_unknown_payload_t*)unknown_payload_create(type);
197 this->data = data;
198 this->critical = critical;
199 this->payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH + data.len;
200
201 return &this->public;
202 }