Compacted chunk creation in ita_attr_command constructor
[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_clone(chunk_from_str(this->command));
95 }
96
97 METHOD(pa_tnc_attr_t, process, status_t,
98 private_ita_attr_command_t *this, u_int32_t *offset)
99 {
100 this->command = strndup(this->value.ptr, this->value.len);
101
102 return SUCCESS;
103 }
104
105 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
106 private_ita_attr_command_t *this)
107 {
108 ref_get(&this->ref);
109 return &this->public.pa_tnc_attribute;
110 }
111
112 METHOD(pa_tnc_attr_t, destroy, void,
113 private_ita_attr_command_t *this)
114 {
115 if (ref_put(&this->ref))
116 {
117 free(this->value.ptr);
118 free(this->command);
119 free(this);
120 }
121 }
122
123 METHOD(ita_attr_command_t, get_command, char*,
124 private_ita_attr_command_t *this)
125 {
126 return this->command;
127 }
128
129 /**
130 * Described in header.
131 */
132 pa_tnc_attr_t *ita_attr_command_create(char *command)
133 {
134 private_ita_attr_command_t *this;
135
136 INIT(this,
137 .public = {
138 .pa_tnc_attribute = {
139 .get_type = _get_type,
140 .get_value = _get_value,
141 .get_noskip_flag = _get_noskip_flag,
142 .set_noskip_flag = _set_noskip_flag,
143 .build = _build,
144 .process = _process,
145 .get_ref = _get_ref,
146 .destroy = _destroy,
147 },
148 .get_command = _get_command,
149 },
150 .type = { PEN_ITA, ITA_ATTR_COMMAND },
151 .command = strdup(command),
152 .ref = 1,
153 );
154
155 return &this->public.pa_tnc_attribute;
156 }
157
158 /**
159 * Described in header.
160 */
161 pa_tnc_attr_t *ita_attr_command_create_from_data(chunk_t data)
162 {
163 private_ita_attr_command_t *this;
164
165 INIT(this,
166 .public = {
167 .pa_tnc_attribute = {
168 .get_type = _get_type,
169 .get_value = _get_value,
170 .get_noskip_flag = _get_noskip_flag,
171 .set_noskip_flag = _set_noskip_flag,
172 .build = _build,
173 .process = _process,
174 .get_ref = _get_ref,
175 .destroy = _destroy,
176 },
177 .get_command = _get_command,
178 },
179 .type = {PEN_ITA, ITA_ATTR_COMMAND },
180 .value = chunk_clone(data),
181 .ref = 1,
182 );
183
184 return &this->public.pa_tnc_attribute;
185 }
186
187