Migrated vendor_id_payload to INIT/METHOD macros
[strongswan.git] / src / libcharon / encoding / payloads / vendor_id_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * 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 "vendor_id_payload.h"
21
22 typedef struct private_vendor_id_payload_t private_vendor_id_payload_t;
23
24 /**
25 * Private data of an vendor_id_payload_t object.
26 */
27 struct private_vendor_id_payload_t {
28
29 /**
30 * Public vendor_id_payload_t interface.
31 */
32 vendor_id_payload_t public;
33
34 /**
35 * Next payload type.
36 */
37 u_int8_t next_payload;
38
39 /**
40 * Critical flag.
41 */
42 bool critical;
43
44 /**
45 * Length of this payload.
46 */
47 u_int16_t payload_length;
48
49 /**
50 * The contained data.
51 */
52 chunk_t data;
53 };
54
55 /**
56 * Encoding rules to parse or generate a VENDOR ID payload
57 *
58 * The defined offsets are the positions in a object of type
59 * private_vendor_id_payload_t.
60 */
61 encoding_rule_t vendor_id_payload_encodings[] = {
62 /* 1 Byte next payload type, stored in the field next_payload */
63 { U_INT_8, offsetof(private_vendor_id_payload_t, next_payload) },
64 /* the critical bit */
65 { FLAG, offsetof(private_vendor_id_payload_t, critical) },
66 /* 7 Bit reserved bits, nowhere stored */
67 { RESERVED_BIT, 0 },
68 { RESERVED_BIT, 0 },
69 { RESERVED_BIT, 0 },
70 { RESERVED_BIT, 0 },
71 { RESERVED_BIT, 0 },
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 /* Length of the whole payload*/
75 { PAYLOAD_LENGTH, offsetof(private_vendor_id_payload_t, payload_length)},
76 /* some vendor_id data bytes, length is defined in PAYLOAD_LENGTH */
77 { VID_DATA, offsetof(private_vendor_id_payload_t, data) }
78 };
79
80 /*
81 1 2 3
82 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
83 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84 ! Next Payload !C! RESERVED ! Payload Length !
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 + !
87 ~ VID Data ~
88 ! !
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 */
91
92 METHOD(payload_t, verify, status_t,
93 private_vendor_id_payload_t *this)
94 {
95 return SUCCESS;
96 }
97
98 METHOD(payload_t, get_encoding_rules, void,
99 private_vendor_id_payload_t *this, encoding_rule_t **rules,
100 size_t *rule_count)
101 {
102 *rules = vendor_id_payload_encodings;
103 *rule_count = countof(vendor_id_payload_encodings);
104 }
105
106 METHOD(payload_t, get_type, payload_type_t,
107 private_vendor_id_payload_t *this)
108 {
109 return VENDOR_ID;
110 }
111
112 METHOD(payload_t, get_next_type, payload_type_t,
113 private_vendor_id_payload_t *this)
114 {
115 return this->next_payload;
116 }
117
118 METHOD(payload_t, set_next_type, void,
119 private_vendor_id_payload_t *this, payload_type_t type)
120 {
121 this->next_payload = type;
122 }
123
124 METHOD(payload_t, get_length, size_t,
125 private_vendor_id_payload_t *this)
126 {
127 return this->payload_length;
128 }
129
130 METHOD(vendor_id_payload_t, get_data, chunk_t,
131 private_vendor_id_payload_t *this)
132 {
133 return this->data;
134 }
135
136 METHOD2(payload_t, vendor_id_payload_t, destroy, void,
137 private_vendor_id_payload_t *this)
138 {
139 free(this->data.ptr);
140 free(this);
141 }
142
143 /*
144 * Described in header
145 */
146 vendor_id_payload_t *vendor_id_payload_create_data(chunk_t data)
147 {
148 private_vendor_id_payload_t *this;
149
150 INIT(this,
151 .public = {
152 .payload_interface = {
153 .verify = _verify,
154 .get_encoding_rules = _get_encoding_rules,
155 .get_length = _get_length,
156 .get_next_type = _get_next_type,
157 .set_next_type = _set_next_type,
158 .get_type = _get_type,
159 .destroy = _destroy,
160 },
161 .get_data = _get_data,
162 .destroy = _destroy,
163 },
164 .next_payload = NO_PAYLOAD,
165 .payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH + data.len,
166 .data = data,
167 );
168 return &this->public;
169 }
170
171 /*
172 * Described in header
173 */
174 vendor_id_payload_t *vendor_id_payload_create()
175 {
176 return vendor_id_payload_create_data(chunk_empty);
177 }