Added add_segment() method to IETF attributes
[strongswan.git] / src / libimcv / ietf / ietf_attr_assess_result.c
1 /*
2 * Copyright (C) 2012-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "ietf_attr_assess_result.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <utils/debug.h>
22
23 typedef struct private_ietf_attr_assess_result_t private_ietf_attr_assess_result_t;
24
25 /**
26 * PA-TNC Product Information type (see section 4.2.2 of RFC 5792)
27 *
28 * 1 2 3
29 * 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
30 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 * | Assessment Result |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 */
34
35 #define ASSESS_RESULT_SIZE 4
36
37 /**
38 * Private data of an ietf_attr_assess_result_t object.
39 */
40 struct private_ietf_attr_assess_result_t {
41
42 /**
43 * Public members of ietf_attr_assess_result_t
44 */
45 ietf_attr_assess_result_t public;
46
47 /**
48 * Vendor-specific attribute type
49 */
50 pen_type_t type;
51
52 /**
53 * Length of attribute value
54 */
55 size_t length;
56
57 /**
58 * Attribute value or segment
59 */
60 chunk_t value;
61
62 /**
63 * Noskip flag
64 */
65 bool noskip_flag;
66
67 /**
68 * Assessment Result
69 */
70 u_int32_t result;
71
72 /**
73 * Reference count
74 */
75 refcount_t ref;
76 };
77
78 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
79 private_ietf_attr_assess_result_t *this)
80 {
81 return this->type;
82 }
83
84 METHOD(pa_tnc_attr_t, get_value, chunk_t,
85 private_ietf_attr_assess_result_t *this)
86 {
87 return this->value;
88 }
89
90 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
91 private_ietf_attr_assess_result_t *this)
92 {
93 return this->noskip_flag;
94 }
95
96 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
97 private_ietf_attr_assess_result_t *this, bool noskip)
98 {
99 this->noskip_flag = noskip;
100 }
101
102 METHOD(pa_tnc_attr_t, build, void,
103 private_ietf_attr_assess_result_t *this)
104 {
105 bio_writer_t *writer;
106
107 if (this->value.ptr)
108 {
109 return;
110 }
111
112 writer = bio_writer_create(ASSESS_RESULT_SIZE);
113 writer->write_uint32(writer, this->result);
114 this->value = writer->extract_buf(writer);
115 this->length = this->value.len;
116 writer->destroy(writer);
117 }
118
119 METHOD(pa_tnc_attr_t, process, status_t,
120 private_ietf_attr_assess_result_t *this, u_int32_t *offset)
121 {
122 bio_reader_t *reader;
123
124 *offset = 0;
125
126 if (this->value.len < this->length)
127 {
128 return NEED_MORE;
129 }
130 if (this->value.len < ASSESS_RESULT_SIZE)
131 {
132 DBG1(DBG_TNC, "insufficient data for IETF assessment result");
133 return FAILED;
134 }
135 reader = bio_reader_create(this->value);
136 reader->read_uint32(reader, &this->result);
137 reader->destroy(reader);
138
139 return SUCCESS;
140 }
141
142 METHOD(pa_tnc_attr_t, add_segment, void,
143 private_ietf_attr_assess_result_t *this, chunk_t segment)
144 {
145 this->value = chunk_cat("mc", this->value, segment);
146 }
147
148 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
149 private_ietf_attr_assess_result_t *this)
150 {
151 ref_get(&this->ref);
152 return &this->public.pa_tnc_attribute;
153 }
154
155 METHOD(pa_tnc_attr_t, destroy, void,
156 private_ietf_attr_assess_result_t *this)
157 {
158 if (ref_put(&this->ref))
159 {
160 free(this->value.ptr);
161 free(this);
162 }
163 }
164
165 METHOD(ietf_attr_assess_result_t, get_result, u_int32_t,
166 private_ietf_attr_assess_result_t *this)
167 {
168 return this->result;
169 }
170
171 /**
172 * Described in header.
173 */
174 pa_tnc_attr_t *ietf_attr_assess_result_create(u_int32_t result)
175 {
176 private_ietf_attr_assess_result_t *this;
177
178 INIT(this,
179 .public = {
180 .pa_tnc_attribute = {
181 .get_type = _get_type,
182 .get_value = _get_value,
183 .get_noskip_flag = _get_noskip_flag,
184 .set_noskip_flag = _set_noskip_flag,
185 .build = _build,
186 .process = _process,
187 .add_segment = _add_segment,
188 .get_ref = _get_ref,
189 .destroy = _destroy,
190 },
191 .get_result = _get_result,
192 },
193 .type = { PEN_IETF, IETF_ATTR_ASSESSMENT_RESULT },
194 .result = result,
195 .ref = 1,
196 );
197
198 return &this->public.pa_tnc_attribute;
199 }
200
201 /**
202 * Described in header.
203 */
204 pa_tnc_attr_t *ietf_attr_assess_result_create_from_data(size_t length,
205 chunk_t data)
206 {
207 private_ietf_attr_assess_result_t *this;
208
209 INIT(this,
210 .public = {
211 .pa_tnc_attribute = {
212 .get_type = _get_type,
213 .get_value = _get_value,
214 .get_noskip_flag = _get_noskip_flag,
215 .set_noskip_flag = _set_noskip_flag,
216 .build = _build,
217 .process = _process,
218 .add_segment = _add_segment,
219 .get_ref = _get_ref,
220 .destroy = _destroy,
221 },
222 .get_result = _get_result,
223 },
224 .type = { PEN_IETF, IETF_ATTR_ASSESSMENT_RESULT },
225 .length = length,
226 .value = chunk_clone(data),
227 .ref = 1,
228 );
229
230 return &this->public.pa_tnc_attribute;
231 }
232