Completed implementation of PWG HCD attributes
[strongswan.git] / src / libimcv / generic / generic_attr_chunk.c
1 /*
2 * Copyright (C) 2015 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 #include "generic_attr_chunk.h"
17
18 #include <imcv.h>
19 #include <pen/pen.h>
20 #include <utils/debug.h>
21
22 typedef struct private_generic_attr_chunk_t private_generic_attr_chunk_t;
23
24 /**
25 * Private data of an generic_attr_chunk_t object.
26 */
27 struct private_generic_attr_chunk_t {
28
29 /**
30 * Public members of generic_attr_chunk_t
31 */
32 generic_attr_chunk_t public;
33
34 /**
35 * Vendor-specific attribute type
36 */
37 pen_type_t type;
38
39 /**
40 * Length of attribute value
41 */
42 size_t length;
43
44 /**
45 * Fixed size of attribute value, set to 0 if dynamic
46 */
47 size_t size;
48
49 /**
50 * Attribute value or segment
51 */
52 chunk_t value;
53
54 /**
55 * Noskip flag
56 */
57 bool noskip_flag;
58
59 /**
60 * Reference count
61 */
62 refcount_t ref;
63 };
64
65 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
66 private_generic_attr_chunk_t *this)
67 {
68 return this->type;
69 }
70
71 METHOD(pa_tnc_attr_t, get_value, chunk_t,
72 private_generic_attr_chunk_t *this)
73 {
74 return this->value;
75 }
76
77 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
78 private_generic_attr_chunk_t *this)
79 {
80 return this->noskip_flag;
81 }
82
83 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
84 private_generic_attr_chunk_t *this, bool noskip)
85 {
86 this->noskip_flag = noskip;
87 }
88
89 METHOD(pa_tnc_attr_t, build, void,
90 private_generic_attr_chunk_t *this)
91 {
92 return;
93 }
94
95 METHOD(pa_tnc_attr_t, process, status_t,
96 private_generic_attr_chunk_t *this, u_int32_t *offset)
97 {
98 enum_name_t *pa_attr_names;
99 *offset = 0;
100
101 if (this->value.len < this->length)
102 {
103 return NEED_MORE;
104 }
105 pa_attr_names = imcv_pa_tnc_attributes->get_names(imcv_pa_tnc_attributes,
106 this->type.vendor_id);
107
108 if ((this->size == 0 && this->value.len > this->length) ||
109 (this->size != 0 && this->value.len != this->size))
110 {
111 DBG1(DBG_TNC, "inconsistent length of %N/%N string attribute",
112 pen_names, this->type.vendor_id, pa_attr_names, this->type.type);
113 return FAILED;
114 }
115
116 return SUCCESS;
117 }
118
119 METHOD(pa_tnc_attr_t, add_segment, void,
120 private_generic_attr_chunk_t *this, chunk_t segment)
121 {
122 this->value = chunk_cat("mc", this->value, segment);
123 }
124
125 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
126 private_generic_attr_chunk_t *this)
127 {
128 ref_get(&this->ref);
129 return &this->public.pa_tnc_attribute;
130 }
131
132 METHOD(pa_tnc_attr_t, destroy, void,
133 private_generic_attr_chunk_t *this)
134 {
135 if (ref_put(&this->ref))
136 {
137 free(this->value.ptr);
138 free(this);
139 }
140 }
141
142 /**
143 * Described in header.
144 */
145 pa_tnc_attr_t *generic_attr_chunk_create_from_data(size_t length, chunk_t value,
146 size_t size, pen_type_t type)
147 {
148 private_generic_attr_chunk_t *this;
149
150 INIT(this,
151 .public = {
152 .pa_tnc_attribute = {
153 .get_type = _get_type,
154 .get_value = _get_value,
155 .get_noskip_flag = _get_noskip_flag,
156 .set_noskip_flag = _set_noskip_flag,
157 .build = _build,
158 .process = _process,
159 .add_segment = _add_segment,
160 .get_ref = _get_ref,
161 .destroy = _destroy,
162 },
163 },
164 .type = type,
165 .length = length,
166 .size = size,
167 .value = chunk_clone(value),
168 .ref = 1,
169 );
170
171 return &this->public.pa_tnc_attribute;
172 }
173
174 /**
175 * Described in header.
176 */
177 pa_tnc_attr_t *generic_attr_chunk_create(chunk_t value, pen_type_t type)
178 {
179 return generic_attr_chunk_create_from_data(value.len, value,
180 value.len, type);
181 }
182