69eb7b05546467a926417601be6f38174bd573e1
[strongswan.git] / src / libimcv / ita / ita_attr_command.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "ita_attr_command.h"
16
17 #include <pen/pen.h>
18
19 #include <debug.h>
20
21 typedef struct private_ita_attr_command_t private_ita_attr_command_t;
22
23 /**
24 * Private data of an ita_attr_command_t object.
25 */
26 struct private_ita_attr_command_t {
27
28 /**
29 * Public members of ita_attr_command_t
30 */
31 ita_attr_command_t public;
32
33 /**
34 * Attribute vendor ID
35 */
36 pen_t vendor_id;
37
38 /**
39 * Attribute type
40 */
41 u_int32_t type;
42
43 /**
44 * Attribute value
45 */
46 chunk_t value;
47
48 /**
49 * Noskip flag
50 */
51 bool noskip_flag;
52
53 /**
54 * Command string
55 */
56 char *command;
57
58 /**
59 * Reference count
60 */
61 refcount_t ref;
62 };
63
64 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
65 private_ita_attr_command_t *this)
66 {
67 return this->vendor_id;
68 }
69
70 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
71 private_ita_attr_command_t *this)
72 {
73 return this->type;
74 }
75
76 METHOD(pa_tnc_attr_t, get_value, chunk_t,
77 private_ita_attr_command_t *this)
78 {
79 return this->value;
80 }
81
82 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
83 private_ita_attr_command_t *this)
84 {
85 return this->noskip_flag;
86 }
87
88 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
89 private_ita_attr_command_t *this, bool noskip)
90 {
91 this->noskip_flag = noskip;
92 }
93
94 METHOD(pa_tnc_attr_t, build, void,
95 private_ita_attr_command_t *this)
96 {
97 this->value = chunk_create(this->command, strlen(this->command));
98 this->value = chunk_clone(this->value);
99 }
100
101 METHOD(pa_tnc_attr_t, process, status_t,
102 private_ita_attr_command_t *this)
103 {
104 this->command = malloc(this->value.len + 1);
105 memcpy(this->command, this->value.ptr, this->value.len);
106 this->command[this->value.len] = '\0';
107
108 return SUCCESS;
109 }
110
111 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
112 private_ita_attr_command_t *this)
113 {
114 ref_get(&this->ref);
115 return &this->public.pa_tnc_attribute;
116 }
117
118 METHOD(pa_tnc_attr_t, destroy, void,
119 private_ita_attr_command_t *this)
120 {
121 if (ref_put(&this->ref))
122 {
123 free(this->value.ptr);
124 free(this->command);
125 free(this);
126 }
127 }
128
129 METHOD(ita_attr_command_t, get_command, char*,
130 private_ita_attr_command_t *this)
131 {
132 return this->command;
133 }
134
135 /**
136 * Described in header.
137 */
138 pa_tnc_attr_t *ita_attr_command_create(char *command)
139 {
140 private_ita_attr_command_t *this;
141
142 INIT(this,
143 .public = {
144 .pa_tnc_attribute = {
145 .get_vendor_id = _get_vendor_id,
146 .get_type = _get_type,
147 .get_value = _get_value,
148 .get_noskip_flag = _get_noskip_flag,
149 .set_noskip_flag = _set_noskip_flag,
150 .build = _build,
151 .process = _process,
152 .get_ref = _get_ref,
153 .destroy = _destroy,
154 },
155 .get_command = _get_command,
156 },
157 .vendor_id = PEN_ITA,
158 .type = ITA_ATTR_COMMAND,
159 .command = strdup(command),
160 .ref = 1,
161 );
162
163 return &this->public.pa_tnc_attribute;
164 }
165
166 /**
167 * Described in header.
168 */
169 pa_tnc_attr_t *ita_attr_command_create_from_data(chunk_t data)
170 {
171 private_ita_attr_command_t *this;
172
173 INIT(this,
174 .public = {
175 .pa_tnc_attribute = {
176 .get_vendor_id = _get_vendor_id,
177 .get_type = _get_type,
178 .get_value = _get_value,
179 .build = _build,
180 .process = _process,
181 .get_ref = _get_ref,
182 .destroy = _destroy,
183 },
184 .get_command = _get_command,
185 },
186 .vendor_id = PEN_ITA,
187 .type = ITA_ATTR_COMMAND,
188 .value = chunk_clone(data),
189 .ref = 1,
190 );
191
192 return &this->public.pa_tnc_attribute;
193 }
194
195