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