2 This file is part of GNUnet
3 Copyright (C) 2010-2015 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file reclaim-attribute/reclaim_attribute.c
23 * @brief helper library to manage identity attributes
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "reclaim_attribute.h"
29 #include "gnunet_reclaim_attribute_plugin.h"
44 struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api;
50 static struct Plugin **attr_plugins;
55 static unsigned int num_plugins;
60 static int initialized;
66 add_plugin (void* cls,
67 const char *library_name,
70 struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = lib_ret;
71 struct Plugin *plugin;
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74 "Loading attribute plugin `%s'\n",
76 plugin = GNUNET_new (struct Plugin);
78 plugin->library_name = GNUNET_strdup (library_name);
79 GNUNET_array_append (attr_plugins, num_plugins, plugin);
88 if (GNUNET_YES == initialized)
90 initialized = GNUNET_YES;
91 GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_", NULL,
96 * Convert a type name to the corresponding number
98 * @param typename name to convert
99 * @return corresponding number, UINT32_MAX on error
102 GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename)
105 struct Plugin *plugin;
109 for (i = 0; i < num_plugins; i++)
111 plugin = attr_plugins[i];
112 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
120 * Convert a type number to the corresponding type string
122 * @param type number of a type
123 * @return corresponding typestring, NULL on error
126 GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type)
129 struct Plugin *plugin;
133 for (i = 0; i < num_plugins; i++)
135 plugin = attr_plugins[i];
136 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
144 * Convert human-readable version of a 'claim' of an attribute to the binary
147 * @param type type of the claim
148 * @param s human-readable string
149 * @param data set to value in binary encoding (will be allocated)
150 * @param data_size set to number of bytes in @a data
151 * @return #GNUNET_OK on success
154 GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type,
160 struct Plugin *plugin;
163 for (i = 0; i < num_plugins; i++)
165 plugin = attr_plugins[i];
166 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
173 return GNUNET_SYSERR;
177 * Convert the 'claim' of an attribute to a string
179 * @param type the type of attribute
180 * @param data claim in binary encoding
181 * @param data_size number of bytes in @a data
182 * @return NULL on error, otherwise human-readable representation of the claim
185 GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type,
190 struct Plugin *plugin;
194 for (i = 0; i < num_plugins; i++)
196 plugin = attr_plugins[i];
197 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
207 * Create a new attribute.
209 * @param attr_name the attribute name
210 * @param type the attribute type
211 * @param data the attribute value
212 * @param data_size the attribute value size
213 * @return the new attribute
215 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
216 GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char* attr_name,
221 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
223 char *attr_name_tmp = GNUNET_strdup (attr_name);
225 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
227 attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) +
228 strlen (attr_name_tmp) + 1 +
231 attr->data_size = data_size;
233 write_ptr = (char*)&attr[1];
234 GNUNET_memcpy (write_ptr,
236 strlen (attr_name_tmp) + 1);
237 attr->name = write_ptr;
238 write_ptr += strlen (attr->name) + 1;
239 GNUNET_memcpy (write_ptr,
242 attr->data = write_ptr;
243 GNUNET_free (attr_name_tmp);
248 * Add a new claim list entry.
250 * @param claim_list the attribute name
251 * @param attr_name the attribute name
252 * @param type the attribute type
253 * @param data the attribute value
254 * @param data_size the attribute value size
258 GNUNET_RECLAIM_ATTRIBUTE_list_add (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list,
259 const char* attr_name,
264 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
265 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
266 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name,
270 GNUNET_CONTAINER_DLL_insert (claim_list->list_head,
271 claim_list->list_tail,
276 GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
278 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
280 for (le = attrs->list_head; NULL != le; le = le->next)
281 len += GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
286 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
289 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
296 for (le = attrs->list_head; NULL != le; le = le->next)
298 len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim,
306 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *
307 GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char* data,
310 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs;
311 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
313 const char* read_ptr;
315 if (data_size < sizeof (struct Attribute))
318 attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
320 while (((data + data_size) - read_ptr) >= sizeof (struct Attribute))
323 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
324 le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr,
325 data_size - (read_ptr - data));
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Deserialized attribute %s\n", le->claim->name);
328 GNUNET_CONTAINER_DLL_insert (attrs->list_head,
331 attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
332 read_ptr += attr_len;
337 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList*
338 GNUNET_RECLAIM_ATTRIBUTE_list_dup (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
340 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
341 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le;
342 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result;
344 result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
345 for (le = attrs->list_head; NULL != le; le = le->next)
347 result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
348 result_le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name,
351 le->claim->data_size);
352 result_le->claim->version = le->claim->version;
353 result_le->claim->id = le->claim->id;
354 GNUNET_CONTAINER_DLL_insert (result->list_head,
363 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
365 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
366 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le;
368 for (le = attrs->list_head; NULL != le;)
370 GNUNET_free (le->claim);
373 GNUNET_free (tmp_le);
380 GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
382 return sizeof (struct Attribute)
383 + strlen (attr->name)
388 GNUNET_RECLAIM_ATTRIBUTE_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
393 struct Attribute *attr_ser;
396 attr_ser = (struct Attribute*)result;
397 attr_ser->attribute_type = htons (attr->type);
398 attr_ser->attribute_version = htonl (attr->version);
399 attr_ser->attribute_id = GNUNET_htonll (attr->id);
400 name_len = strlen (attr->name);
401 attr_ser->name_len = htons (name_len);
402 write_ptr = (char*)&attr_ser[1];
403 GNUNET_memcpy (write_ptr, attr->name, name_len);
404 write_ptr += name_len;
406 //data_len_ser = plugin->serialize_attribute_value (attr,
408 data_len_ser = attr->data_size;
409 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
410 attr_ser->data_size = htons (data_len_ser);
412 return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size;
415 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
416 GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char* data,
419 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
420 struct Attribute *attr_ser;
425 if (data_size < sizeof (struct Attribute))
428 attr_ser = (struct Attribute*)data;
429 data_len = ntohs (attr_ser->data_size);
430 name_len = ntohs (attr_ser->name_len);
431 attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
432 + data_len + name_len + 1);
433 attr->type = ntohs (attr_ser->attribute_type);
434 attr->version = ntohl (attr_ser->attribute_version);
435 attr->id = GNUNET_ntohll (attr_ser->attribute_id);
436 attr->data_size = ntohs (attr_ser->data_size);
438 write_ptr = (char*)&attr[1];
439 GNUNET_memcpy (write_ptr,
442 write_ptr[name_len] = '\0';
443 attr->name = write_ptr;
445 write_ptr += name_len + 1;
446 GNUNET_memcpy (write_ptr,
447 (char*)&attr_ser[1] + name_len,
449 attr->data = write_ptr;
454 /* end of reclaim_attribute.c */