]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libimcv/generic/generic_attr_string.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libimcv / generic / generic_attr_string.c
1 /*
2 * Copyright (C) 2013-2015 Andreas Steffen
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "generic_attr_string.h"
18
19 #include <imcv.h>
20 #include <pen/pen.h>
21 #include <utils/debug.h>
22
23 typedef struct private_generic_attr_string_t private_generic_attr_string_t;
24
25 /**
26 * Private data of an generic_attr_string_t object.
27 */
28 struct private_generic_attr_string_t {
29
30 /**
31 * Public members of generic_attr_string_t
32 */
33 generic_attr_string_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 * Reference count
57 */
58 refcount_t ref;
59 };
60
61 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
62 private_generic_attr_string_t *this)
63 {
64 return this->type;
65 }
66
67 METHOD(pa_tnc_attr_t, get_value, chunk_t,
68 private_generic_attr_string_t *this)
69 {
70 return this->value;
71 }
72
73 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
74 private_generic_attr_string_t *this)
75 {
76 return this->noskip_flag;
77 }
78
79 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
80 private_generic_attr_string_t *this, bool noskip)
81 {
82 this->noskip_flag = noskip;
83 }
84
85 METHOD(pa_tnc_attr_t, build, void,
86 private_generic_attr_string_t *this)
87 {
88 return;
89 }
90
91 METHOD(pa_tnc_attr_t, process, status_t,
92 private_generic_attr_string_t *this, uint32_t *offset)
93 {
94 enum_name_t *pa_attr_names;
95 u_char *pos;
96 *offset = 0;
97
98 if (this->value.len < this->length)
99 {
100 return NEED_MORE;
101 }
102 pa_attr_names = imcv_pa_tnc_attributes->get_names(imcv_pa_tnc_attributes,
103 this->type.vendor_id);
104 if (this->value.len > this->length)
105 {
106 DBG1(DBG_TNC, "inconsistent length of %N/%N string attribute",
107 pen_names, this->type.vendor_id, pa_attr_names, this->type.type);
108 return FAILED;
109 }
110
111 pos = memchr(this->value.ptr, '\0', this->value.len);
112 if (pos)
113 {
114 DBG1(DBG_TNC, "nul termination in %N/%N string attribute",
115 pen_names, this->type.vendor_id, pa_attr_names, this->type.type);
116 *offset = pos - this->value.ptr;
117 return FAILED;
118 }
119
120 return SUCCESS;
121 }
122
123 METHOD(pa_tnc_attr_t, add_segment, void,
124 private_generic_attr_string_t *this, chunk_t segment)
125 {
126 this->value = chunk_cat("mc", this->value, segment);
127 }
128
129 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
130 private_generic_attr_string_t *this)
131 {
132 ref_get(&this->ref);
133 return &this->public.pa_tnc_attribute;
134 }
135
136 METHOD(pa_tnc_attr_t, destroy, void,
137 private_generic_attr_string_t *this)
138 {
139 if (ref_put(&this->ref))
140 {
141 free(this->value.ptr);
142 free(this);
143 }
144 }
145
146 /**
147 * Described in header.
148 */
149 pa_tnc_attr_t *generic_attr_string_create_from_data(size_t length,
150 chunk_t value, pen_type_t type)
151 {
152 private_generic_attr_string_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 },
168 .type = type,
169 .length = length,
170 .value = chunk_clone(value),
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 *generic_attr_string_create(chunk_t value, pen_type_t type)
181 {
182 return generic_attr_string_create_from_data(value.len, value, type);
183 }
184