Added add_segment() method to TCG/SEG attributes
[strongswan.git] / src / libimcv / ita / ita_attr_dummy.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 "ita_attr.h"
17 #include "ita_attr_dummy.h"
18
19 #include <pen/pen.h>
20
21 #include <utils/debug.h>
22
23 typedef struct private_ita_attr_dummy_t private_ita_attr_dummy_t;
24
25 /**
26 * Private data of an ita_attr_dummy_t object.
27 */
28 struct private_ita_attr_dummy_t {
29
30 /**
31 * Public members of ita_attr_dummy_t
32 */
33 ita_attr_dummy_t public;
34
35 /**
36 * Vendor-specific attribute type
37 */
38 pen_type_t type;
39
40 /**
41 * Length of attribute value
42 */
43 size_t length;
44
45 /**
46 * Attribute value or segment
47 */
48 chunk_t value;
49
50 /**
51 * Noskip flag
52 */
53 bool noskip_flag;
54
55 /**
56 * Size of the attribute value
57 */
58 int size;
59
60 /**
61 * Reference count
62 */
63 refcount_t ref;
64 };
65
66 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
67 private_ita_attr_dummy_t *this)
68 {
69 return this->type;
70 }
71
72 METHOD(pa_tnc_attr_t, get_value, chunk_t,
73 private_ita_attr_dummy_t *this)
74 {
75 return this->value;
76 }
77
78 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
79 private_ita_attr_dummy_t *this)
80 {
81 return this->noskip_flag;
82 }
83
84 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
85 private_ita_attr_dummy_t *this, bool noskip)
86 {
87 this->noskip_flag = noskip;
88 }
89
90 METHOD(pa_tnc_attr_t, build, void,
91 private_ita_attr_dummy_t *this)
92 {
93 if (this->value.ptr)
94 {
95 return;
96 }
97 this->value = chunk_alloc(this->length);
98 memset(this->value.ptr, 0xdd, this->value.len);
99 }
100
101 METHOD(pa_tnc_attr_t, process, status_t,
102 private_ita_attr_dummy_t *this, u_int32_t *offset)
103 {
104 *offset = 0;
105
106 if (this->value.len < this->length)
107 {
108 return NEED_MORE;
109 }
110 return SUCCESS;
111 }
112
113 METHOD(pa_tnc_attr_t, add_segment, void,
114 private_ita_attr_dummy_t *this, chunk_t segment)
115 {
116 this->value = chunk_cat("mc", this->value, segment);
117 }
118
119 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
120 private_ita_attr_dummy_t *this)
121 {
122 ref_get(&this->ref);
123 return &this->public.pa_tnc_attribute;
124 }
125
126 METHOD(pa_tnc_attr_t, destroy, void,
127 private_ita_attr_dummy_t *this)
128 {
129 if (ref_put(&this->ref))
130 {
131 free(this->value.ptr);
132 free(this);
133 }
134 }
135
136 METHOD(ita_attr_dummy_t, get_size, int,
137 private_ita_attr_dummy_t *this)
138 {
139 return this->length;
140 }
141
142 /**
143 * Described in header.
144 */
145 pa_tnc_attr_t *ita_attr_dummy_create(size_t size)
146 {
147 private_ita_attr_dummy_t *this;
148
149 INIT(this,
150 .public = {
151 .pa_tnc_attribute = {
152 .get_type = _get_type,
153 .get_value = _get_value,
154 .get_noskip_flag = _get_noskip_flag,
155 .set_noskip_flag = _set_noskip_flag,
156 .build = _build,
157 .process = _process,
158 .add_segment = _add_segment,
159 .get_ref = _get_ref,
160 .destroy = _destroy,
161 },
162 .get_size = _get_size,
163 },
164 .type = { PEN_ITA, ITA_ATTR_DUMMY },
165 .length = size,
166 .ref = 1,
167 );
168
169 return &this->public.pa_tnc_attribute;
170 }
171
172 /**
173 * Described in header.
174 */
175 pa_tnc_attr_t *ita_attr_dummy_create_from_data(size_t length, chunk_t data)
176 {
177 private_ita_attr_dummy_t *this;
178
179 INIT(this,
180 .public = {
181 .pa_tnc_attribute = {
182 .get_type = _get_type,
183 .get_value = _get_value,
184 .get_noskip_flag = _get_noskip_flag,
185 .set_noskip_flag = _set_noskip_flag,
186 .build = _build,
187 .process = _process,
188 .add_segment = _add_segment,
189 .get_ref = _get_ref,
190 .destroy = _destroy,
191 },
192 .get_size = _get_size,
193 },
194 .type = { PEN_ITA, ITA_ATTR_DUMMY },
195 .length = length,
196 .value = chunk_clone(data),
197 .ref = 1,
198 );
199
200 return &this->public.pa_tnc_attribute;
201 }
202
203