9692e1ffdb799ca9a67d9b811be36ebddb2bf535
[strongswan.git] / src / libimcv / ita / ita_attr_command.c
1 /*
2 * Copyright (C) 2011-2012 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 * Attribute value
44 */
45 chunk_t value;
46
47 /**
48 * Noskip flag
49 */
50 bool noskip_flag;
51
52 /**
53 * Command string
54 */
55 char *command;
56
57 /**
58 * Reference count
59 */
60 refcount_t ref;
61 };
62
63 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
64 private_ita_attr_command_t *this)
65 {
66 return this->type;
67 }
68
69 METHOD(pa_tnc_attr_t, get_value, chunk_t,
70 private_ita_attr_command_t *this)
71 {
72 return this->value;
73 }
74
75 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
76 private_ita_attr_command_t *this)
77 {
78 return this->noskip_flag;
79 }
80
81 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
82 private_ita_attr_command_t *this, bool noskip)
83 {
84 this->noskip_flag = noskip;
85 }
86
87 METHOD(pa_tnc_attr_t, build, void,
88 private_ita_attr_command_t *this)
89 {
90 if (this->value.ptr)
91 {
92 return;
93 }
94 this->value = chunk_create(this->command, strlen(this->command));
95 this->value = chunk_clone(this->value);
96 }
97
98 METHOD(pa_tnc_attr_t, process, status_t,
99 private_ita_attr_command_t *this, u_int32_t *offset)
100 {
101 this->command = strndup(this->value.ptr, this->value.len);
102
103 return SUCCESS;
104 }
105
106 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
107 private_ita_attr_command_t *this)
108 {
109 ref_get(&this->ref);
110 return &this->public.pa_tnc_attribute;
111 }
112
113 METHOD(pa_tnc_attr_t, destroy, void,
114 private_ita_attr_command_t *this)
115 {
116 if (ref_put(&this->ref))
117 {
118 free(this->value.ptr);
119 free(this->command);
120 free(this);
121 }
122 }
123
124 METHOD(ita_attr_command_t, get_command, char*,
125 private_ita_attr_command_t *this)
126 {
127 return this->command;
128 }
129
130 /**
131 * Described in header.
132 */
133 pa_tnc_attr_t *ita_attr_command_create(char *command)
134 {
135 private_ita_attr_command_t *this;
136
137 INIT(this,
138 .public = {
139 .pa_tnc_attribute = {
140 .get_type = _get_type,
141 .get_value = _get_value,
142 .get_noskip_flag = _get_noskip_flag,
143 .set_noskip_flag = _set_noskip_flag,
144 .build = _build,
145 .process = _process,
146 .get_ref = _get_ref,
147 .destroy = _destroy,
148 },
149 .get_command = _get_command,
150 },
151 .type = { PEN_ITA, ITA_ATTR_COMMAND },
152 .command = strdup(command),
153 .ref = 1,
154 );
155
156 return &this->public.pa_tnc_attribute;
157 }
158
159 /**
160 * Described in header.
161 */
162 pa_tnc_attr_t *ita_attr_command_create_from_data(chunk_t data)
163 {
164 private_ita_attr_command_t *this;
165
166 INIT(this,
167 .public = {
168 .pa_tnc_attribute = {
169 .get_type = _get_type,
170 .get_value = _get_value,
171 .get_noskip_flag = _get_noskip_flag,
172 .set_noskip_flag = _set_noskip_flag,
173 .build = _build,
174 .process = _process,
175 .get_ref = _get_ref,
176 .destroy = _destroy,
177 },
178 .get_command = _get_command,
179 },
180 .type = {PEN_ITA, ITA_ATTR_COMMAND },
181 .value = chunk_clone(data),
182 .ref = 1,
183 );
184
185 return &this->public.pa_tnc_attribute;
186 }
187
188