Removed strayed code fragment
[strongswan.git] / src / charon / 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
22
23 typedef struct private_unknown_payload_t private_unknown_payload_t;
24
25 /**
26 * Private data of an unknown_payload_t object.
27 */
28 struct private_unknown_payload_t {
29
30 /**
31 * Public unknown_payload_t interface.
32 */
33 unknown_payload_t public;
34
35 /**
36 * Next payload type.
37 */
38 u_int8_t next_payload;
39
40 /**
41 * Critical flag.
42 */
43 bool critical;
44
45 /**
46 * Length of this payload.
47 */
48 u_int16_t payload_length;
49
50 /**
51 * The contained data.
52 */
53 chunk_t data;
54 };
55
56 /**
57 * Encoding rules to parse an payload which is not further specified.
58 *
59 * The defined offsets are the positions in a object of type
60 * private_unknown_payload_t.
61 *
62 */
63 encoding_rule_t unknown_payload_encodings[] = {
64 /* 1 Byte next payload type, stored in the field next_payload */
65 { U_INT_8, offsetof(private_unknown_payload_t, next_payload)},
66 /* the critical bit */
67 { FLAG, offsetof(private_unknown_payload_t, critical) },
68 /* 7 Bit reserved bits, nowhere stored */
69 { RESERVED_BIT, 0 },
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 /* Length of the whole payload*/
77 { PAYLOAD_LENGTH, offsetof(private_unknown_payload_t, payload_length)},
78 /* some unknown data bytes, length is defined in PAYLOAD_LENGTH */
79 { UNKNOWN_DATA, offsetof(private_unknown_payload_t, data) }
80 };
81
82 /*
83 1 2 3
84 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
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 ! Next Payload !C! RESERVED ! Payload Length !
87 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 ! !
89 ~ Data of any type ~
90 ! !
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 */
93
94 /**
95 * Implementation of payload_t.verify.
96 */
97 static status_t verify(private_unknown_payload_t *this)
98 {
99 /* can't do any checks, so we assume its good */
100 return SUCCESS;
101 }
102
103 /**
104 * Implementation of payload_t.get_encoding_rules.
105 */
106 static void get_encoding_rules(private_unknown_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
107 {
108 *rules = unknown_payload_encodings;
109 *rule_count = sizeof(unknown_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_unknown_payload_t *this)
116 {
117 return UNKNOWN_PAYLOAD;
118 }
119
120 /**
121 * Implementation of payload_t.get_next_type.
122 */
123 static payload_type_t get_next_type(private_unknown_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_unknown_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_unknown_payload_t *this)
140 {
141 return this->payload_length;
142 }
143
144 /**
145 * Implementation of unknown_payload_t.get_data.
146 */
147 static bool is_critical(private_unknown_payload_t *this)
148 {
149 return this->critical;
150 }
151
152 /**
153 * Implementation of unknown_payload_t.get_data.
154 */
155 static chunk_t get_data (private_unknown_payload_t *this)
156 {
157 return (this->data);
158 }
159
160 /**
161 * Implementation of payload_t.destroy and unknown_payload_t.destroy.
162 */
163 static void destroy(private_unknown_payload_t *this)
164 {
165 if (this->data.ptr != NULL)
166 {
167 chunk_free(&(this->data));
168 }
169
170 free(this);
171 }
172
173 /*
174 * Described in header
175 */
176 unknown_payload_t *unknown_payload_create()
177 {
178 private_unknown_payload_t *this = malloc_thing(private_unknown_payload_t);
179
180 /* interface functions */
181 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
182 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
183 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
184 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
185 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
186 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
187 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
188
189 /* public functions */
190 this->public.destroy = (void (*) (unknown_payload_t *)) destroy;
191 this->public.is_critical = (bool (*) (unknown_payload_t *)) is_critical;
192 this->public.get_data = (chunk_t (*) (unknown_payload_t *)) get_data;
193
194 /* private variables */
195 this->critical = FALSE;
196 this->next_payload = NO_PAYLOAD;
197 this->payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH;
198 this->data = chunk_empty;
199
200 return (&(this->public));
201 }