]>
Commit | Line | Data |
---|---|---|
21b8051c AS |
1 | /* |
2 | * Copyright (C) 2015 Andreas Steffen | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
21b8051c AS |
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 "pwg_attr_vendor_smi_code.h" | |
18 | ||
19 | #include <pa_tnc/pa_tnc_msg.h> | |
20 | #include <bio/bio_writer.h> | |
21 | #include <bio/bio_reader.h> | |
22 | #include <utils/debug.h> | |
23 | ||
24 | typedef struct private_pwg_attr_vendor_smi_code_t private_pwg_attr_vendor_smi_code_t; | |
25 | ||
26 | /** | |
27 | * PWG HCD PA-TNC Vendor SMI Code | |
28 | * | |
29 | * 1 2 3 | |
30 | * 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 | |
31 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
32 | * | Reserved | Vendor SMI Code | | |
33 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
34 | */ | |
35 | ||
36 | #define VENDOR_SMI_CODE_SIZE 4 | |
37 | ||
38 | /** | |
39 | * Private data of an pwg_attr_vendor_smi_code_t object. | |
40 | */ | |
41 | struct private_pwg_attr_vendor_smi_code_t { | |
42 | ||
43 | /** | |
44 | * Public members of pwg_attr_vendor_smi_code_t | |
45 | */ | |
46 | pwg_attr_vendor_smi_code_t public; | |
47 | ||
48 | /** | |
49 | * Vendor-specific attribute type | |
50 | */ | |
51 | pen_type_t type; | |
52 | ||
53 | /** | |
54 | * Length of attribute value | |
55 | */ | |
56 | size_t length; | |
57 | ||
58 | /** | |
59 | * Attribute value or segment | |
60 | */ | |
61 | chunk_t value; | |
62 | ||
63 | /** | |
64 | * Noskip flag | |
65 | */ | |
66 | bool noskip_flag; | |
67 | ||
68 | /** | |
69 | * Vendor SMI code | |
70 | */ | |
71 | pen_t vendor_smi_code; | |
72 | ||
73 | /** | |
74 | * Reference count | |
75 | */ | |
76 | refcount_t ref; | |
77 | }; | |
78 | ||
79 | METHOD(pa_tnc_attr_t, get_type, pen_type_t, | |
80 | private_pwg_attr_vendor_smi_code_t *this) | |
81 | { | |
82 | return this->type; | |
83 | } | |
84 | ||
85 | METHOD(pa_tnc_attr_t, get_value, chunk_t, | |
86 | private_pwg_attr_vendor_smi_code_t *this) | |
87 | { | |
88 | return this->value; | |
89 | } | |
90 | ||
91 | METHOD(pa_tnc_attr_t, get_noskip_flag, bool, | |
92 | private_pwg_attr_vendor_smi_code_t *this) | |
93 | { | |
94 | return this->noskip_flag; | |
95 | } | |
96 | ||
97 | METHOD(pa_tnc_attr_t, set_noskip_flag,void, | |
98 | private_pwg_attr_vendor_smi_code_t *this, bool noskip) | |
99 | { | |
100 | this->noskip_flag = noskip; | |
101 | } | |
102 | ||
103 | METHOD(pa_tnc_attr_t, build, void, | |
104 | private_pwg_attr_vendor_smi_code_t *this) | |
105 | { | |
106 | bio_writer_t *writer; | |
107 | ||
108 | if (this->value.ptr) | |
109 | { | |
110 | return; | |
111 | } | |
112 | writer = bio_writer_create(VENDOR_SMI_CODE_SIZE); | |
113 | writer->write_uint32(writer, this->vendor_smi_code); | |
114 | ||
115 | this->value = writer->extract_buf(writer); | |
116 | this->length = this->value.len; | |
117 | writer->destroy(writer); | |
118 | } | |
119 | ||
120 | METHOD(pa_tnc_attr_t, process, status_t, | |
b12c53ce | 121 | private_pwg_attr_vendor_smi_code_t *this, uint32_t *offset) |
21b8051c AS |
122 | { |
123 | bio_reader_t *reader; | |
124 | uint32_t vendor_smi_code; | |
125 | uint8_t reserved; | |
126 | ||
127 | *offset = 0; | |
128 | ||
129 | if (this->value.len < this->length) | |
130 | { | |
131 | return NEED_MORE; | |
132 | } | |
133 | if (this->value.len != VENDOR_SMI_CODE_SIZE) | |
134 | { | |
135 | DBG1(DBG_TNC, "incorrect attribute length for PWG HCD Vendor SMI Code"); | |
136 | return FAILED; | |
137 | } | |
138 | reader = bio_reader_create(this->value); | |
139 | reader->read_uint8 (reader, &reserved); | |
140 | reader->read_uint24(reader, &vendor_smi_code); | |
141 | reader->destroy(reader); | |
142 | this->vendor_smi_code = vendor_smi_code; | |
143 | ||
144 | return SUCCESS; | |
145 | } | |
146 | ||
147 | METHOD(pa_tnc_attr_t, add_segment, void, | |
148 | private_pwg_attr_vendor_smi_code_t *this, chunk_t segment) | |
149 | { | |
150 | this->value = chunk_cat("mc", this->value, segment); | |
151 | } | |
152 | ||
153 | METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*, | |
154 | private_pwg_attr_vendor_smi_code_t *this) | |
155 | { | |
156 | ref_get(&this->ref); | |
157 | return &this->public.pa_tnc_attribute; | |
158 | } | |
159 | ||
160 | METHOD(pa_tnc_attr_t, destroy, void, | |
161 | private_pwg_attr_vendor_smi_code_t *this) | |
162 | { | |
163 | if (ref_put(&this->ref)) | |
164 | { | |
165 | free(this->value.ptr); | |
166 | free(this); | |
167 | } | |
168 | } | |
169 | ||
170 | METHOD(pwg_attr_vendor_smi_code_t, get_vendor_smi_code, pen_t, | |
171 | private_pwg_attr_vendor_smi_code_t *this) | |
172 | { | |
173 | return this->vendor_smi_code; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Described in header. | |
178 | */ | |
179 | pa_tnc_attr_t *pwg_attr_vendor_smi_code_create(pen_t vendor_smi_code) | |
180 | { | |
181 | private_pwg_attr_vendor_smi_code_t *this; | |
182 | ||
183 | INIT(this, | |
184 | .public = { | |
185 | .pa_tnc_attribute = { | |
186 | .get_type = _get_type, | |
187 | .get_value = _get_value, | |
188 | .get_noskip_flag = _get_noskip_flag, | |
189 | .set_noskip_flag = _set_noskip_flag, | |
190 | .build = _build, | |
191 | .process = _process, | |
192 | .add_segment = _add_segment, | |
193 | .get_ref = _get_ref, | |
194 | .destroy = _destroy, | |
195 | }, | |
196 | .get_vendor_smi_code = _get_vendor_smi_code, | |
197 | }, | |
198 | .type = { PEN_PWG, PWG_HCD_VENDOR_SMI_CODE }, | |
199 | .vendor_smi_code = vendor_smi_code, | |
200 | .ref = 1, | |
201 | ); | |
202 | ||
203 | return &this->public.pa_tnc_attribute; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Described in header. | |
208 | */ | |
209 | pa_tnc_attr_t *pwg_attr_vendor_smi_code_create_from_data(size_t length, | |
210 | chunk_t data) | |
211 | { | |
212 | private_pwg_attr_vendor_smi_code_t *this; | |
213 | ||
214 | INIT(this, | |
215 | .public = { | |
216 | .pa_tnc_attribute = { | |
217 | .get_type = _get_type, | |
218 | .get_value = _get_value, | |
219 | .get_noskip_flag = _get_noskip_flag, | |
220 | .set_noskip_flag = _set_noskip_flag, | |
221 | .build = _build, | |
222 | .process = _process, | |
223 | .add_segment = _add_segment, | |
224 | .get_ref = _get_ref, | |
225 | .destroy = _destroy, | |
226 | }, | |
227 | .get_vendor_smi_code = _get_vendor_smi_code, | |
228 | }, | |
229 | .type = { PEN_PWG, PWG_HCD_VENDOR_SMI_CODE }, | |
230 | .length = length, | |
231 | .value = chunk_clone(data), | |
232 | .ref = 1, | |
233 | ); | |
234 | ||
235 | return &this->public.pa_tnc_attribute; | |
236 | } | |
237 |