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