diff options
Diffstat (limited to 'src/reclaim/reclaim_attribute.c')
-rw-r--r-- | src/reclaim/reclaim_attribute.c | 582 |
1 files changed, 0 insertions, 582 deletions
diff --git a/src/reclaim/reclaim_attribute.c b/src/reclaim/reclaim_attribute.c deleted file mode 100644 index 21fdd83a2..000000000 --- a/src/reclaim/reclaim_attribute.c +++ /dev/null | |||
@@ -1,582 +0,0 @@ | |||
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 | /** | ||
254 | * Create a new attribute. | ||
255 | * | ||
256 | * @param attr_name the attribute name | ||
257 | * @param credential credential ID of the attribute (maybe NULL) | ||
258 | * @param type the attribute type | ||
259 | * @param data the attribute value | ||
260 | * @param data_size the attribute value size | ||
261 | * @return the new attribute | ||
262 | */ | ||
263 | struct GNUNET_RECLAIM_Attribute * | ||
264 | GNUNET_RECLAIM_attribute_new (const char *attr_name, | ||
265 | const struct | ||
266 | GNUNET_RECLAIM_Identifier *credential, | ||
267 | uint32_t type, | ||
268 | const void *data, | ||
269 | size_t data_size) | ||
270 | { | ||
271 | struct GNUNET_RECLAIM_Attribute *attr; | ||
272 | char *write_ptr; | ||
273 | char *attr_name_tmp = GNUNET_strdup (attr_name); | ||
274 | |||
275 | GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); | ||
276 | |||
277 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute) | ||
278 | + strlen (attr_name_tmp) + 1 + data_size); | ||
279 | if (NULL != credential) | ||
280 | attr->credential = *credential; | ||
281 | attr->type = type; | ||
282 | attr->data_size = data_size; | ||
283 | attr->flag = 0; | ||
284 | write_ptr = (char *) &attr[1]; | ||
285 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | ||
286 | attr->name = write_ptr; | ||
287 | write_ptr += strlen (attr->name) + 1; | ||
288 | GNUNET_memcpy (write_ptr, data, data_size); | ||
289 | attr->data = write_ptr; | ||
290 | GNUNET_free (attr_name_tmp); | ||
291 | return attr; | ||
292 | } | ||
293 | |||
294 | |||
295 | /** | ||
296 | * Add a new attribute to a claim list | ||
297 | * | ||
298 | * @param attr_name the name of the new attribute claim | ||
299 | * @param type the type of the claim | ||
300 | * @param data claim payload | ||
301 | * @param data_size claim payload size | ||
302 | */ | ||
303 | void | ||
304 | GNUNET_RECLAIM_attribute_list_add ( | ||
305 | struct GNUNET_RECLAIM_AttributeList *al, | ||
306 | const char *attr_name, | ||
307 | const struct GNUNET_RECLAIM_Identifier *credential, | ||
308 | uint32_t type, | ||
309 | const void *data, | ||
310 | size_t data_size) | ||
311 | { | ||
312 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
313 | |||
314 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
315 | ale->attribute = | ||
316 | GNUNET_RECLAIM_attribute_new (attr_name, credential, | ||
317 | type, data, data_size); | ||
318 | GNUNET_CONTAINER_DLL_insert (al->list_head, | ||
319 | al->list_tail, | ||
320 | ale); | ||
321 | } | ||
322 | |||
323 | |||
324 | /** | ||
325 | * Get required size for serialization buffer | ||
326 | * | ||
327 | * @param attrs the attribute list to serialize | ||
328 | * @return the required buffer size | ||
329 | */ | ||
330 | size_t | ||
331 | GNUNET_RECLAIM_attribute_list_serialize_get_size ( | ||
332 | const struct GNUNET_RECLAIM_AttributeList *al) | ||
333 | { | ||
334 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
335 | size_t len = 0; | ||
336 | |||
337 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
338 | { | ||
339 | GNUNET_assert (NULL != ale->attribute); | ||
340 | len += GNUNET_RECLAIM_attribute_serialize_get_size (ale->attribute); | ||
341 | } | ||
342 | return len; | ||
343 | } | ||
344 | |||
345 | |||
346 | /** | ||
347 | * Serialize an attribute list | ||
348 | * | ||
349 | * @param attrs the attribute list to serialize | ||
350 | * @param result the serialized attribute | ||
351 | * @return length of serialized data | ||
352 | */ | ||
353 | size_t | ||
354 | GNUNET_RECLAIM_attribute_list_serialize ( | ||
355 | const struct GNUNET_RECLAIM_AttributeList *al, | ||
356 | char *result) | ||
357 | { | ||
358 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
359 | size_t len; | ||
360 | size_t total_len; | ||
361 | char *write_ptr; | ||
362 | write_ptr = result; | ||
363 | total_len = 0; | ||
364 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
365 | { | ||
366 | GNUNET_assert (NULL != ale->attribute); | ||
367 | len = GNUNET_RECLAIM_attribute_serialize (ale->attribute, write_ptr); | ||
368 | total_len += len; | ||
369 | write_ptr += len; | ||
370 | } | ||
371 | return total_len; | ||
372 | } | ||
373 | |||
374 | |||
375 | /** | ||
376 | * Deserialize an attribute list | ||
377 | * | ||
378 | * @param data the serialized attribute list | ||
379 | * @param data_size the length of the serialized data | ||
380 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
381 | */ | ||
382 | struct GNUNET_RECLAIM_AttributeList * | ||
383 | GNUNET_RECLAIM_attribute_list_deserialize (const char *data, size_t data_size) | ||
384 | { | ||
385 | struct GNUNET_RECLAIM_AttributeList *al; | ||
386 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
387 | size_t attr_len; | ||
388 | const char *read_ptr; | ||
389 | size_t left = data_size; | ||
390 | |||
391 | al = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
392 | if (data_size < sizeof(struct Attribute)) | ||
393 | return al; | ||
394 | read_ptr = data; | ||
395 | while (left >= sizeof(struct Attribute)) | ||
396 | { | ||
397 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
398 | attr_len = | ||
399 | GNUNET_RECLAIM_attribute_deserialize (read_ptr, | ||
400 | left, | ||
401 | &ale->attribute); | ||
402 | if (-1 == attr_len) | ||
403 | { | ||
404 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
405 | "Failed to deserialize malformed attribute.\n"); | ||
406 | GNUNET_free (ale); | ||
407 | return al; | ||
408 | } | ||
409 | left -= attr_len; | ||
410 | GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale); | ||
411 | read_ptr += attr_len; | ||
412 | } | ||
413 | return al; | ||
414 | } | ||
415 | |||
416 | |||
417 | /** | ||
418 | * Make a (deep) copy of a claim list | ||
419 | * @param attrs claim list to copy | ||
420 | * @return copied claim list | ||
421 | */ | ||
422 | struct GNUNET_RECLAIM_AttributeList * | ||
423 | GNUNET_RECLAIM_attribute_list_dup ( | ||
424 | const struct GNUNET_RECLAIM_AttributeList *al) | ||
425 | { | ||
426 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
427 | struct GNUNET_RECLAIM_AttributeListEntry *result_ale; | ||
428 | struct GNUNET_RECLAIM_AttributeList *result; | ||
429 | |||
430 | result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
431 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
432 | { | ||
433 | result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
434 | GNUNET_assert (NULL != ale->attribute); | ||
435 | { | ||
436 | result_ale->attribute = | ||
437 | GNUNET_RECLAIM_attribute_new (ale->attribute->name, | ||
438 | &ale->attribute->credential, | ||
439 | ale->attribute->type, | ||
440 | ale->attribute->data, | ||
441 | ale->attribute->data_size); | ||
442 | |||
443 | result_ale->attribute->id = ale->attribute->id; | ||
444 | result_ale->attribute->flag = ale->attribute->flag; | ||
445 | } | ||
446 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
447 | result->list_tail, | ||
448 | result_ale); | ||
449 | } | ||
450 | return result; | ||
451 | } | ||
452 | |||
453 | |||
454 | /** | ||
455 | * Destroy claim list | ||
456 | * | ||
457 | * @param attrs list to destroy | ||
458 | */ | ||
459 | void | ||
460 | GNUNET_RECLAIM_attribute_list_destroy ( | ||
461 | struct GNUNET_RECLAIM_AttributeList *al) | ||
462 | { | ||
463 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
464 | struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale; | ||
465 | |||
466 | for (ale = al->list_head; NULL != ale;) | ||
467 | { | ||
468 | if (NULL != ale->attribute) | ||
469 | GNUNET_free (ale->attribute); | ||
470 | tmp_ale = ale; | ||
471 | ale = ale->next; | ||
472 | GNUNET_free (tmp_ale); | ||
473 | } | ||
474 | GNUNET_free (al); | ||
475 | } | ||
476 | |||
477 | |||
478 | /** | ||
479 | * Get required size for serialization buffer | ||
480 | * | ||
481 | * @param attr the attribute to serialize | ||
482 | * @return the required buffer size | ||
483 | */ | ||
484 | size_t | ||
485 | GNUNET_RECLAIM_attribute_serialize_get_size ( | ||
486 | const struct GNUNET_RECLAIM_Attribute *attr) | ||
487 | { | ||
488 | return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size; | ||
489 | } | ||
490 | |||
491 | |||
492 | /** | ||
493 | * Serialize an attribute | ||
494 | * | ||
495 | * @param attr the attribute to serialize | ||
496 | * @param result the serialized attribute | ||
497 | * @return length of serialized data | ||
498 | */ | ||
499 | size_t | ||
500 | GNUNET_RECLAIM_attribute_serialize ( | ||
501 | const struct GNUNET_RECLAIM_Attribute *attr, | ||
502 | char *result) | ||
503 | { | ||
504 | size_t data_len_ser; | ||
505 | size_t name_len; | ||
506 | struct Attribute *attr_ser; | ||
507 | char *write_ptr; | ||
508 | |||
509 | attr_ser = (struct Attribute *) result; | ||
510 | attr_ser->attribute_type = htons (attr->type); | ||
511 | attr_ser->attribute_flag = htonl (attr->flag); | ||
512 | attr_ser->attribute_id = attr->id; | ||
513 | attr_ser->credential_id = attr->credential; | ||
514 | name_len = strlen (attr->name); | ||
515 | attr_ser->name_len = htons (name_len); | ||
516 | write_ptr = (char *) &attr_ser[1]; | ||
517 | GNUNET_memcpy (write_ptr, attr->name, name_len); | ||
518 | write_ptr += name_len; | ||
519 | // TODO plugin-ize | ||
520 | // data_len_ser = plugin->serialize_attribute_value (attr, | ||
521 | // &attr_ser[1]); | ||
522 | data_len_ser = attr->data_size; | ||
523 | GNUNET_memcpy (write_ptr, attr->data, attr->data_size); | ||
524 | attr_ser->data_size = htons (data_len_ser); | ||
525 | |||
526 | return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size; | ||
527 | } | ||
528 | |||
529 | |||
530 | /** | ||
531 | * Deserialize an attribute | ||
532 | * | ||
533 | * @param data the serialized attribute | ||
534 | * @param data_size the length of the serialized data | ||
535 | * | ||
536 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
537 | */ | ||
538 | ssize_t | ||
539 | GNUNET_RECLAIM_attribute_deserialize (const char *data, size_t data_size, | ||
540 | struct GNUNET_RECLAIM_Attribute **attr) | ||
541 | { | ||
542 | struct Attribute *attr_ser; | ||
543 | struct GNUNET_RECLAIM_Attribute *attribute; | ||
544 | size_t data_len; | ||
545 | size_t name_len; | ||
546 | char *write_ptr; | ||
547 | |||
548 | if (data_size < sizeof(struct Attribute)) | ||
549 | return -1; | ||
550 | |||
551 | attr_ser = (struct Attribute *) data; | ||
552 | data_len = ntohs (attr_ser->data_size); | ||
553 | name_len = ntohs (attr_ser->name_len); | ||
554 | if (data_size < sizeof(struct Attribute) + data_len + name_len) | ||
555 | { | ||
556 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
557 | "Buffer too small to deserialize\n"); | ||
558 | return -1; | ||
559 | } | ||
560 | attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute) | ||
561 | + data_len + name_len + 1); | ||
562 | attribute->type = ntohs (attr_ser->attribute_type); | ||
563 | attribute->flag = ntohl (attr_ser->attribute_flag); | ||
564 | attribute->id = attr_ser->attribute_id; | ||
565 | attribute->credential = attr_ser->credential_id; | ||
566 | attribute->data_size = data_len; | ||
567 | |||
568 | write_ptr = (char *) &attribute[1]; | ||
569 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); | ||
570 | write_ptr[name_len] = '\0'; | ||
571 | attribute->name = write_ptr; | ||
572 | |||
573 | write_ptr += name_len + 1; | ||
574 | GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, | ||
575 | attribute->data_size); | ||
576 | *attr = attribute; | ||
577 | attribute->data = write_ptr; | ||
578 | return sizeof(struct Attribute) + data_len + name_len; | ||
579 | } | ||
580 | |||
581 | |||
582 | /* end of reclaim_attribute.c */ | ||