diff options
Diffstat (limited to 'src/service/reclaim/reclaim_attribute.c')
-rw-r--r-- | src/service/reclaim/reclaim_attribute.c | 547 |
1 files changed, 547 insertions, 0 deletions
diff --git a/src/service/reclaim/reclaim_attribute.c b/src/service/reclaim/reclaim_attribute.c new file mode 100644 index 000000000..b235ed945 --- /dev/null +++ b/src/service/reclaim/reclaim_attribute.c | |||
@@ -0,0 +1,547 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file reclaim-attribute/reclaim_attribute.c | ||
23 | * @brief helper library to manage identity attributes | ||
24 | * @author Martin Schanzenbach | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_reclaim_plugin.h" | ||
29 | #include "reclaim_attribute.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Handle for a plugin | ||
34 | */ | ||
35 | struct Plugin | ||
36 | { | ||
37 | /** | ||
38 | * Name of the plugin | ||
39 | */ | ||
40 | char *library_name; | ||
41 | |||
42 | /** | ||
43 | * Plugin API | ||
44 | */ | ||
45 | struct GNUNET_RECLAIM_AttributePluginFunctions *api; | ||
46 | }; | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Plugins | ||
51 | */ | ||
52 | static struct Plugin **attr_plugins; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Number of plugins | ||
57 | */ | ||
58 | static unsigned int num_plugins; | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Init canary | ||
63 | */ | ||
64 | static int initialized; | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Add a plugin | ||
69 | * | ||
70 | * @param cls closure | ||
71 | * @param library_name name of the API library | ||
72 | * @param lib_ret the plugin API pointer | ||
73 | */ | ||
74 | static void | ||
75 | add_plugin (void *cls, const char *library_name, void *lib_ret) | ||
76 | { | ||
77 | struct GNUNET_RECLAIM_AttributePluginFunctions *api = lib_ret; | ||
78 | struct Plugin *plugin; | ||
79 | |||
80 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
81 | "Loading attribute plugin `%s'\n", | ||
82 | library_name); | ||
83 | plugin = GNUNET_new (struct Plugin); | ||
84 | plugin->api = api; | ||
85 | plugin->library_name = GNUNET_strdup (library_name); | ||
86 | GNUNET_array_append (attr_plugins, num_plugins, plugin); | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Load plugins | ||
92 | */ | ||
93 | static void | ||
94 | init () | ||
95 | { | ||
96 | if (GNUNET_YES == initialized) | ||
97 | return; | ||
98 | initialized = GNUNET_YES; | ||
99 | GNUNET_PLUGIN_load_all_in_context (GNUNET_OS_project_data_default (), | ||
100 | "libgnunet_plugin_reclaim_attribute_", | ||
101 | NULL, | ||
102 | &add_plugin, | ||
103 | NULL); | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * Dual function to #init(). | ||
108 | */ | ||
109 | void __attribute__ ((destructor)) | ||
110 | RECLAIM_ATTRIBUTE_fini () | ||
111 | { | ||
112 | struct Plugin *plugin; | ||
113 | const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get (); | ||
114 | const struct GNUNET_OS_ProjectData *dpd = GNUNET_OS_project_data_default (); | ||
115 | |||
116 | if (pd != dpd) | ||
117 | GNUNET_OS_init (dpd); | ||
118 | |||
119 | for (unsigned int i = 0; i < num_plugins; i++) | ||
120 | { | ||
121 | plugin = attr_plugins[i]; | ||
122 | GNUNET_break (NULL == | ||
123 | GNUNET_PLUGIN_unload (plugin->library_name, | ||
124 | plugin->api)); | ||
125 | GNUNET_free (plugin->library_name); | ||
126 | GNUNET_free (plugin); | ||
127 | } | ||
128 | GNUNET_free (attr_plugins); | ||
129 | |||
130 | if (pd != dpd) | ||
131 | GNUNET_OS_init (pd); | ||
132 | |||
133 | attr_plugins = NULL; | ||
134 | } | ||
135 | |||
136 | |||
137 | |||
138 | /** | ||
139 | * Convert a type name to the corresponding number | ||
140 | * | ||
141 | * @param typename name to convert | ||
142 | * @return corresponding number, UINT32_MAX on error | ||
143 | */ | ||
144 | uint32_t | ||
145 | GNUNET_RECLAIM_attribute_typename_to_number (const char *typename) | ||
146 | { | ||
147 | unsigned int i; | ||
148 | struct Plugin *plugin; | ||
149 | uint32_t ret; | ||
150 | |||
151 | init (); | ||
152 | for (i = 0; i < num_plugins; i++) | ||
153 | { | ||
154 | plugin = attr_plugins[i]; | ||
155 | if (UINT32_MAX != | ||
156 | (ret = plugin->api->typename_to_number (plugin->api->cls, typename))) | ||
157 | return ret; | ||
158 | } | ||
159 | return UINT32_MAX; | ||
160 | } | ||
161 | |||
162 | |||
163 | /** | ||
164 | * Convert a type number to the corresponding type string | ||
165 | * | ||
166 | * @param type number of a type | ||
167 | * @return corresponding typestring, NULL on error | ||
168 | */ | ||
169 | const char * | ||
170 | GNUNET_RECLAIM_attribute_number_to_typename (uint32_t type) | ||
171 | { | ||
172 | unsigned int i; | ||
173 | struct Plugin *plugin; | ||
174 | const char *ret; | ||
175 | |||
176 | init (); | ||
177 | for (i = 0; i < num_plugins; i++) | ||
178 | { | ||
179 | plugin = attr_plugins[i]; | ||
180 | if (NULL != | ||
181 | (ret = plugin->api->number_to_typename (plugin->api->cls, type))) | ||
182 | return ret; | ||
183 | } | ||
184 | return NULL; | ||
185 | } | ||
186 | |||
187 | |||
188 | /** | ||
189 | * Convert human-readable version of a 'claim' of an attribute to the binary | ||
190 | * representation | ||
191 | * | ||
192 | * @param type type of the claim | ||
193 | * @param s human-readable string | ||
194 | * @param data set to value in binary encoding (will be allocated) | ||
195 | * @param data_size set to number of bytes in @a data | ||
196 | * @return #GNUNET_OK on success | ||
197 | */ | ||
198 | int | ||
199 | GNUNET_RECLAIM_attribute_string_to_value (uint32_t type, | ||
200 | const char *s, | ||
201 | void **data, | ||
202 | size_t *data_size) | ||
203 | { | ||
204 | unsigned int i; | ||
205 | struct Plugin *plugin; | ||
206 | |||
207 | init (); | ||
208 | for (i = 0; i < num_plugins; i++) | ||
209 | { | ||
210 | plugin = attr_plugins[i]; | ||
211 | if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, | ||
212 | type, | ||
213 | s, | ||
214 | data, | ||
215 | data_size)) | ||
216 | return GNUNET_OK; | ||
217 | } | ||
218 | return GNUNET_SYSERR; | ||
219 | } | ||
220 | |||
221 | |||
222 | /** | ||
223 | * Convert the 'claim' of an attribute to a string | ||
224 | * | ||
225 | * @param type the type of attribute | ||
226 | * @param data claim in binary encoding | ||
227 | * @param data_size number of bytes in @a data | ||
228 | * @return NULL on error, otherwise human-readable representation of the claim | ||
229 | */ | ||
230 | char * | ||
231 | GNUNET_RECLAIM_attribute_value_to_string (uint32_t type, | ||
232 | const void *data, | ||
233 | size_t data_size) | ||
234 | { | ||
235 | unsigned int i; | ||
236 | struct Plugin *plugin; | ||
237 | char *ret; | ||
238 | |||
239 | init (); | ||
240 | for (i = 0; i < num_plugins; i++) | ||
241 | { | ||
242 | plugin = attr_plugins[i]; | ||
243 | if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, | ||
244 | type, | ||
245 | data, | ||
246 | data_size))) | ||
247 | return ret; | ||
248 | } | ||
249 | return NULL; | ||
250 | } | ||
251 | |||
252 | |||
253 | struct GNUNET_RECLAIM_Attribute * | ||
254 | GNUNET_RECLAIM_attribute_new (const char *attr_name, | ||
255 | const struct | ||
256 | GNUNET_RECLAIM_Identifier *credential, | ||
257 | uint32_t type, | ||
258 | const void *data, | ||
259 | size_t data_size) | ||
260 | { | ||
261 | struct GNUNET_RECLAIM_Attribute *attr; | ||
262 | char *write_ptr; | ||
263 | char *attr_name_tmp = GNUNET_strdup (attr_name); | ||
264 | |||
265 | GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); | ||
266 | |||
267 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute) | ||
268 | + strlen (attr_name_tmp) + 1 + data_size); | ||
269 | if (NULL != credential) | ||
270 | attr->credential = *credential; | ||
271 | attr->type = type; | ||
272 | attr->data_size = data_size; | ||
273 | attr->flag = 0; | ||
274 | write_ptr = (char *) &attr[1]; | ||
275 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | ||
276 | attr->name = write_ptr; | ||
277 | write_ptr += strlen (attr->name) + 1; | ||
278 | GNUNET_memcpy (write_ptr, data, data_size); | ||
279 | attr->data = write_ptr; | ||
280 | GNUNET_free (attr_name_tmp); | ||
281 | return attr; | ||
282 | } | ||
283 | |||
284 | |||
285 | void | ||
286 | GNUNET_RECLAIM_attribute_list_add ( | ||
287 | struct GNUNET_RECLAIM_AttributeList *al, | ||
288 | const char *attr_name, | ||
289 | const struct GNUNET_RECLAIM_Identifier *credential, | ||
290 | uint32_t type, | ||
291 | const void *data, | ||
292 | size_t data_size) | ||
293 | { | ||
294 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
295 | |||
296 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
297 | ale->attribute = | ||
298 | GNUNET_RECLAIM_attribute_new (attr_name, credential, | ||
299 | type, data, data_size); | ||
300 | GNUNET_CONTAINER_DLL_insert (al->list_head, | ||
301 | al->list_tail, | ||
302 | ale); | ||
303 | } | ||
304 | |||
305 | |||
306 | /** | ||
307 | * Get required size for serialization buffer | ||
308 | * | ||
309 | * @param attrs the attribute list to serialize | ||
310 | * @return the required buffer size | ||
311 | */ | ||
312 | size_t | ||
313 | GNUNET_RECLAIM_attribute_list_serialize_get_size ( | ||
314 | const struct GNUNET_RECLAIM_AttributeList *al) | ||
315 | { | ||
316 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
317 | size_t len = 0; | ||
318 | |||
319 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
320 | { | ||
321 | GNUNET_assert (NULL != ale->attribute); | ||
322 | len += GNUNET_RECLAIM_attribute_serialize_get_size (ale->attribute); | ||
323 | } | ||
324 | return len; | ||
325 | } | ||
326 | |||
327 | |||
328 | size_t | ||
329 | GNUNET_RECLAIM_attribute_list_serialize ( | ||
330 | const struct GNUNET_RECLAIM_AttributeList *attrs, | ||
331 | char *result) | ||
332 | { | ||
333 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
334 | size_t len; | ||
335 | size_t total_len; | ||
336 | char *write_ptr; | ||
337 | write_ptr = result; | ||
338 | total_len = 0; | ||
339 | for (ale = attrs->list_head; NULL != ale; ale = ale->next) | ||
340 | { | ||
341 | GNUNET_assert (NULL != ale->attribute); | ||
342 | len = GNUNET_RECLAIM_attribute_serialize (ale->attribute, write_ptr); | ||
343 | total_len += len; | ||
344 | write_ptr += len; | ||
345 | } | ||
346 | return total_len; | ||
347 | } | ||
348 | |||
349 | |||
350 | /** | ||
351 | * Deserialize an attribute list | ||
352 | * | ||
353 | * @param data the serialized attribute list | ||
354 | * @param data_size the length of the serialized data | ||
355 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
356 | */ | ||
357 | struct GNUNET_RECLAIM_AttributeList * | ||
358 | GNUNET_RECLAIM_attribute_list_deserialize (const char *data, size_t data_size) | ||
359 | { | ||
360 | struct GNUNET_RECLAIM_AttributeList *al; | ||
361 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
362 | size_t attr_len; | ||
363 | const char *read_ptr; | ||
364 | size_t left = data_size; | ||
365 | |||
366 | al = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
367 | if (data_size < sizeof(struct Attribute)) | ||
368 | return al; | ||
369 | read_ptr = data; | ||
370 | while (left >= sizeof(struct Attribute)) | ||
371 | { | ||
372 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
373 | attr_len = | ||
374 | GNUNET_RECLAIM_attribute_deserialize (read_ptr, | ||
375 | left, | ||
376 | &ale->attribute); | ||
377 | if (-1 == attr_len) | ||
378 | { | ||
379 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
380 | "Failed to deserialize malformed attribute.\n"); | ||
381 | GNUNET_free (ale); | ||
382 | return al; | ||
383 | } | ||
384 | left -= attr_len; | ||
385 | GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale); | ||
386 | read_ptr += attr_len; | ||
387 | } | ||
388 | return al; | ||
389 | } | ||
390 | |||
391 | |||
392 | struct GNUNET_RECLAIM_AttributeList * | ||
393 | GNUNET_RECLAIM_attribute_list_dup ( | ||
394 | const struct GNUNET_RECLAIM_AttributeList *attrs) | ||
395 | { | ||
396 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
397 | struct GNUNET_RECLAIM_AttributeListEntry *result_ale; | ||
398 | struct GNUNET_RECLAIM_AttributeList *result; | ||
399 | |||
400 | result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
401 | for (ale = attrs->list_head; NULL != ale; ale = ale->next) | ||
402 | { | ||
403 | result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
404 | GNUNET_assert (NULL != ale->attribute); | ||
405 | { | ||
406 | result_ale->attribute = | ||
407 | GNUNET_RECLAIM_attribute_new (ale->attribute->name, | ||
408 | &ale->attribute->credential, | ||
409 | ale->attribute->type, | ||
410 | ale->attribute->data, | ||
411 | ale->attribute->data_size); | ||
412 | |||
413 | result_ale->attribute->id = ale->attribute->id; | ||
414 | result_ale->attribute->flag = ale->attribute->flag; | ||
415 | } | ||
416 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
417 | result->list_tail, | ||
418 | result_ale); | ||
419 | } | ||
420 | return result; | ||
421 | } | ||
422 | |||
423 | |||
424 | void | ||
425 | GNUNET_RECLAIM_attribute_list_destroy ( | ||
426 | struct GNUNET_RECLAIM_AttributeList *al) | ||
427 | { | ||
428 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
429 | struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale; | ||
430 | |||
431 | for (ale = al->list_head; NULL != ale;) | ||
432 | { | ||
433 | if (NULL != ale->attribute) | ||
434 | GNUNET_free (ale->attribute); | ||
435 | tmp_ale = ale; | ||
436 | ale = ale->next; | ||
437 | GNUNET_free (tmp_ale); | ||
438 | } | ||
439 | GNUNET_free (al); | ||
440 | } | ||
441 | |||
442 | |||
443 | /** | ||
444 | * Get required size for serialization buffer | ||
445 | * | ||
446 | * @param attr the attribute to serialize | ||
447 | * @return the required buffer size | ||
448 | */ | ||
449 | size_t | ||
450 | GNUNET_RECLAIM_attribute_serialize_get_size ( | ||
451 | const struct GNUNET_RECLAIM_Attribute *attr) | ||
452 | { | ||
453 | return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size; | ||
454 | } | ||
455 | |||
456 | |||
457 | /** | ||
458 | * Serialize an attribute | ||
459 | * | ||
460 | * @param attr the attribute to serialize | ||
461 | * @param result the serialized attribute | ||
462 | * @return length of serialized data | ||
463 | */ | ||
464 | size_t | ||
465 | GNUNET_RECLAIM_attribute_serialize ( | ||
466 | const struct GNUNET_RECLAIM_Attribute *attr, | ||
467 | char *result) | ||
468 | { | ||
469 | size_t data_len_ser; | ||
470 | size_t name_len; | ||
471 | struct Attribute *attr_ser; | ||
472 | char *write_ptr; | ||
473 | |||
474 | attr_ser = (struct Attribute *) result; | ||
475 | attr_ser->attribute_type = htonl (attr->type); | ||
476 | attr_ser->attribute_flag = htonl (attr->flag); | ||
477 | attr_ser->attribute_id = attr->id; | ||
478 | attr_ser->credential_id = attr->credential; | ||
479 | name_len = strlen (attr->name); | ||
480 | attr_ser->name_len = htons (name_len); | ||
481 | write_ptr = (char *) &attr_ser[1]; | ||
482 | GNUNET_memcpy (write_ptr, attr->name, name_len); | ||
483 | write_ptr += name_len; | ||
484 | // TODO plugin-ize | ||
485 | // data_len_ser = plugin->serialize_attribute_value (attr, | ||
486 | // &attr_ser[1]); | ||
487 | data_len_ser = attr->data_size; | ||
488 | GNUNET_memcpy (write_ptr, attr->data, attr->data_size); | ||
489 | attr_ser->data_size = htons (data_len_ser); | ||
490 | |||
491 | return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size; | ||
492 | } | ||
493 | |||
494 | |||
495 | /** | ||
496 | * Deserialize an attribute | ||
497 | * | ||
498 | * @param data the serialized attribute | ||
499 | * @param data_size the length of the serialized data | ||
500 | * | ||
501 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
502 | */ | ||
503 | ssize_t | ||
504 | GNUNET_RECLAIM_attribute_deserialize (const char *data, size_t data_size, | ||
505 | struct GNUNET_RECLAIM_Attribute **attr) | ||
506 | { | ||
507 | struct Attribute *attr_ser; | ||
508 | struct GNUNET_RECLAIM_Attribute *attribute; | ||
509 | size_t data_len; | ||
510 | size_t name_len; | ||
511 | char *write_ptr; | ||
512 | |||
513 | if (data_size < sizeof(struct Attribute)) | ||
514 | return -1; | ||
515 | |||
516 | attr_ser = (struct Attribute *) data; | ||
517 | data_len = ntohs (attr_ser->data_size); | ||
518 | name_len = ntohs (attr_ser->name_len); | ||
519 | if (data_size < sizeof(struct Attribute) + data_len + name_len) | ||
520 | { | ||
521 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
522 | "Buffer too small to deserialize\n"); | ||
523 | return -1; | ||
524 | } | ||
525 | attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute) | ||
526 | + data_len + name_len + 1); | ||
527 | attribute->type = ntohl (attr_ser->attribute_type); | ||
528 | attribute->flag = ntohl (attr_ser->attribute_flag); | ||
529 | attribute->id = attr_ser->attribute_id; | ||
530 | attribute->credential = attr_ser->credential_id; | ||
531 | attribute->data_size = data_len; | ||
532 | |||
533 | write_ptr = (char *) &attribute[1]; | ||
534 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); | ||
535 | write_ptr[name_len] = '\0'; | ||
536 | attribute->name = write_ptr; | ||
537 | |||
538 | write_ptr += name_len + 1; | ||
539 | GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, | ||
540 | attribute->data_size); | ||
541 | *attr = attribute; | ||
542 | attribute->data = write_ptr; | ||
543 | return sizeof(struct Attribute) + data_len + name_len; | ||
544 | } | ||
545 | |||
546 | |||
547 | /* end of reclaim_attribute.c */ | ||