]> git.ipfire.org Git - thirdparty/strongswan.git/blame - programs/charon/charon/encoding/payloads/unknown_payload.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / charon / charon / encoding / payloads / unknown_payload.c
CommitLineData
34852cae
JH
1/**
2 * @file unknown_payload.c
3 *
4 * @brief Implementation of unknown_payload_t.
5 *
6 */
7
8/*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
5113680f
MW
23#include <stddef.h>
24
34852cae
JH
25#include "unknown_payload.h"
26
34852cae
JH
27
28
29typedef struct private_unknown_payload_t private_unknown_payload_t;
30
31/**
32 * Private data of an unknown_payload_t object.
34852cae
JH
33 */
34struct private_unknown_payload_t {
668f9fcb 35
34852cae
JH
36 /**
37 * Public unknown_payload_t interface.
38 */
39 unknown_payload_t public;
40
41 /**
42 * Next payload type.
43 */
668f9fcb 44 u_int8_t next_payload;
34852cae
JH
45
46 /**
47 * Critical flag.
48 */
49 bool critical;
50
51 /**
52 * Length of this payload.
53 */
54 u_int16_t payload_length;
55
34852cae
JH
56 /**
57 * The contained data.
58 */
59 chunk_t data;
60};
61
62/**
668f9fcb 63 * Encoding rules to parse an payload which is not further specified.
34852cae
JH
64 *
65 * The defined offsets are the positions in a object of type
66 * private_unknown_payload_t.
67 *
68 */
69encoding_rule_t unknown_payload_encodings[] = {
70 /* 1 Byte next payload type, stored in the field next_payload */
71 { U_INT_8, offsetof(private_unknown_payload_t, next_payload)},
72 /* the critical bit */
73 { FLAG, offsetof(private_unknown_payload_t, critical) },
74 /* 7 Bit reserved bits, nowhere stored */
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 { RESERVED_BIT, 0 },
80 { RESERVED_BIT, 0 },
81 { RESERVED_BIT, 0 },
82 /* Length of the whole payload*/
83 { PAYLOAD_LENGTH, offsetof(private_unknown_payload_t, payload_length)},
84 /* some unknown data bytes, length is defined in PAYLOAD_LENGTH */
668f9fcb 85 { UNKNOWN_DATA, offsetof(private_unknown_payload_t, data) }
34852cae
JH
86};
87
88/*
89 1 2 3
90 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
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 ! Next Payload !C! RESERVED ! Payload Length !
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! !
95 ~ Data of any type ~
96 ! !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98*/
99
100/**
101 * Implementation of payload_t.verify.
102 */
103static status_t verify(private_unknown_payload_t *this)
104{
c3dc864e 105 /* can't do any checks, so we assume its good */
34852cae
JH
106 return SUCCESS;
107}
108
109/**
668f9fcb 110 * Implementation of payload_t.get_encoding_rules.
34852cae
JH
111 */
112static void get_encoding_rules(private_unknown_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
113{
114 *rules = unknown_payload_encodings;
115 *rule_count = sizeof(unknown_payload_encodings) / sizeof(encoding_rule_t);
116}
117
118/**
119 * Implementation of payload_t.get_type.
120 */
121static payload_type_t get_payload_type(private_unknown_payload_t *this)
122{
123 return UNKNOWN_PAYLOAD;
124}
125
126/**
127 * Implementation of payload_t.get_next_type.
128 */
129static payload_type_t get_next_type(private_unknown_payload_t *this)
130{
131 return (this->next_payload);
132}
133
134/**
135 * Implementation of payload_t.set_next_type.
136 */
137static void set_next_type(private_unknown_payload_t *this,payload_type_t type)
138{
139 this->next_payload = type;
140}
141
34852cae
JH
142/**
143 * Implementation of payload_t.get_length.
144 */
145static size_t get_length(private_unknown_payload_t *this)
146{
147 return this->payload_length;
148}
149
150/**
668f9fcb 151 * Implementation of unknown_payload_t.get_data.
34852cae 152 */
668f9fcb 153static bool is_critical(private_unknown_payload_t *this)
34852cae 154{
668f9fcb 155 return this->critical;
34852cae
JH
156}
157
158/**
159 * Implementation of unknown_payload_t.get_data.
160 */
161static chunk_t get_data (private_unknown_payload_t *this)
162{
163 return (this->data);
164}
165
34852cae
JH
166/**
167 * Implementation of payload_t.destroy and unknown_payload_t.destroy.
168 */
169static void destroy(private_unknown_payload_t *this)
170{
171 if (this->data.ptr != NULL)
172 {
5113680f 173 chunk_free(&(this->data));
34852cae
JH
174 }
175
5113680f 176 free(this);
34852cae
JH
177}
178
179/*
180 * Described in header
181 */
182unknown_payload_t *unknown_payload_create()
183{
5113680f 184 private_unknown_payload_t *this = malloc_thing(private_unknown_payload_t);
34852cae
JH
185
186 /* interface functions */
187 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
188 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
189 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
190 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
191 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
192 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
193 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
194
195 /* public functions */
196 this->public.destroy = (void (*) (unknown_payload_t *)) destroy;
668f9fcb 197 this->public.is_critical = (bool (*) (unknown_payload_t *)) is_critical;
34852cae
JH
198 this->public.get_data = (chunk_t (*) (unknown_payload_t *)) get_data;
199
200 /* private variables */
201 this->critical = FALSE;
202 this->next_payload = NO_PAYLOAD;
668f9fcb 203 this->payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH;
34852cae
JH
204 this->data = CHUNK_INITIALIZER;
205
206 return (&(this->public));
207}