read PDP server name from strongswan.conf
[strongswan.git] / src / libimcv / ietf / ietf_attr_product_info.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 "ietf_attr_product_info.h"
16
17 #include <pa_tnc/pa_tnc_msg.h>
18 #include <bio/bio_writer.h>
19 #include <bio/bio_reader.h>
20 #include <debug.h>
21
22 typedef struct private_ietf_attr_product_info_t private_ietf_attr_product_info_t;
23
24 /**
25 * PA-TNC Product Information type (see section 4.2.2 of RFC 5792)
26 *
27 * 1 2 3
28 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * | Product Vendor ID | Product ID |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 * | Product ID | Product Name (Variable Length) |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 */
35
36 #define PRODUCT_INFO_MIN_SIZE 5
37
38 /**
39 * Private data of an ietf_attr_product_info_t object.
40 */
41 struct private_ietf_attr_product_info_t {
42
43 /**
44 * Public members of ietf_attr_product_info_t
45 */
46 ietf_attr_product_info_t public;
47
48 /**
49 * Attribute vendor ID
50 */
51 pen_t vendor_id;
52
53 /**
54 * Attribute type
55 */
56 u_int32_t type;
57
58 /**
59 * Attribute value
60 */
61 chunk_t value;
62
63 /**
64 * Noskip flag
65 */
66 bool noskip_flag;
67
68 /**
69 * Product vendor ID
70 */
71 pen_t product_vendor_id;
72
73 /**
74 * Product ID
75 */
76 u_int16_t product_id;
77
78 /**
79 * Product Name
80 */
81 char *product_name;
82
83 /**
84 * Reference count
85 */
86 refcount_t ref;
87 };
88
89 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
90 private_ietf_attr_product_info_t *this)
91 {
92 return this->vendor_id;
93 }
94
95 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
96 private_ietf_attr_product_info_t *this)
97 {
98 return this->type;
99 }
100
101 METHOD(pa_tnc_attr_t, get_value, chunk_t,
102 private_ietf_attr_product_info_t *this)
103 {
104 return this->value;
105 }
106
107 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
108 private_ietf_attr_product_info_t *this)
109 {
110 return this->noskip_flag;
111 }
112
113 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
114 private_ietf_attr_product_info_t *this, bool noskip)
115 {
116 this->noskip_flag = noskip;
117 }
118
119 METHOD(pa_tnc_attr_t, build, void,
120 private_ietf_attr_product_info_t *this)
121 {
122 bio_writer_t *writer;
123 chunk_t product_name;
124
125 product_name = chunk_create(this->product_name, strlen(this->product_name));
126
127 writer = bio_writer_create(PRODUCT_INFO_MIN_SIZE);
128 writer->write_uint24(writer, this->product_vendor_id);
129 writer->write_uint16(writer, this->product_id);
130 writer->write_data (writer, product_name);
131
132 this->value = chunk_clone(writer->get_buf(writer));
133 writer->destroy(writer);
134 }
135
136 METHOD(pa_tnc_attr_t, process, status_t,
137 private_ietf_attr_product_info_t *this, u_int32_t *offset)
138 {
139 bio_reader_t *reader;
140 chunk_t product_name;
141
142 if (this->value.len < PRODUCT_INFO_MIN_SIZE)
143 {
144 DBG1(DBG_TNC, "insufficient data for IETF product information");
145 *offset = 0;
146 return FAILED;
147 }
148 reader = bio_reader_create(this->value);
149 reader->read_uint24(reader, &this->product_vendor_id);
150 reader->read_uint16(reader, &this->product_id);
151 reader->read_data (reader, reader->remaining(reader), &product_name);
152 reader->destroy(reader);
153
154 this->product_name = malloc(product_name.len + 1);
155 memcpy(this->product_name, product_name.ptr, product_name.len);
156 this->product_name[product_name.len] = '\0';
157
158 return SUCCESS;
159 }
160
161 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
162 private_ietf_attr_product_info_t *this)
163 {
164 ref_get(&this->ref);
165 return &this->public.pa_tnc_attribute;
166 }
167
168 METHOD(pa_tnc_attr_t, destroy, void,
169 private_ietf_attr_product_info_t *this)
170 {
171 if (ref_put(&this->ref))
172 {
173 free(this->product_name);
174 free(this->value.ptr);
175 free(this);
176 }
177 }
178
179 METHOD(ietf_attr_product_info_t, get_info, char*,
180 private_ietf_attr_product_info_t *this, pen_t *vendor_id, u_int16_t *id)
181 {
182 if (vendor_id)
183 {
184 *vendor_id = this->product_vendor_id;
185 }
186 if (id)
187 {
188 *id = this->product_id;
189 }
190 return this->product_name;
191 }
192
193 /**
194 * Described in header.
195 */
196 pa_tnc_attr_t *ietf_attr_product_info_create(pen_t vendor_id, u_int16_t id,
197 char *name)
198 {
199 private_ietf_attr_product_info_t *this;
200
201 INIT(this,
202 .public = {
203 .pa_tnc_attribute = {
204 .get_vendor_id = _get_vendor_id,
205 .get_type = _get_type,
206 .get_value = _get_value,
207 .get_noskip_flag = _get_noskip_flag,
208 .set_noskip_flag = _set_noskip_flag,
209 .build = _build,
210 .process = _process,
211 .get_ref = _get_ref,
212 .destroy = _destroy,
213 },
214 .get_info = _get_info,
215 },
216 .vendor_id = PEN_IETF,
217 .type = IETF_ATTR_PRODUCT_INFORMATION,
218 .product_vendor_id = vendor_id,
219 .product_id = id,
220 .product_name = strdup(name),
221 .ref = 1,
222 );
223
224 return &this->public.pa_tnc_attribute;
225 }
226
227 /**
228 * Described in header.
229 */
230 pa_tnc_attr_t *ietf_attr_product_info_create_from_data(chunk_t data)
231 {
232 private_ietf_attr_product_info_t *this;
233
234 INIT(this,
235 .public = {
236 .pa_tnc_attribute = {
237 .get_vendor_id = _get_vendor_id,
238 .get_type = _get_type,
239 .get_value = _get_value,
240 .build = _build,
241 .process = _process,
242 .get_ref = _get_ref,
243 .destroy = _destroy,
244 },
245 .get_info = _get_info,
246 },
247 .vendor_id = PEN_IETF,
248 .type = IETF_ATTR_PRODUCT_INFORMATION,
249 .value = chunk_clone(data),
250 .ref = 1,
251 );
252
253 return &this->public.pa_tnc_attribute;
254 }
255