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/>.
20 * @file reclaim-attribute/reclaim_attribute.c
21 * @brief helper library to manage identity attributes
22 * @author Martin Schanzenbach
25 #include "gnunet_util_lib.h"
26 #include "reclaim_attribute.h"
27 #include "gnunet_reclaim_attribute_plugin.h"
42 struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api;
48 static struct Plugin **attr_plugins;
53 static unsigned int num_plugins;
58 static int initialized;
64 add_plugin (void* cls,
65 const char *library_name,
68 struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = lib_ret;
69 struct Plugin *plugin;
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72 "Loading attribute plugin `%s'\n",
74 plugin = GNUNET_new (struct Plugin);
76 plugin->library_name = GNUNET_strdup (library_name);
77 GNUNET_array_append (attr_plugins, num_plugins, plugin);
86 if (GNUNET_YES == initialized)
88 initialized = GNUNET_YES;
89 GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_", NULL,
94 * Convert a type name to the corresponding number
96 * @param typename name to convert
97 * @return corresponding number, UINT32_MAX on error
100 GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename)
103 struct Plugin *plugin;
107 for (i = 0; i < num_plugins; i++)
109 plugin = attr_plugins[i];
110 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
118 * Convert a type number to the corresponding type string
120 * @param type number of a type
121 * @return corresponding typestring, NULL on error
124 GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type)
127 struct Plugin *plugin;
131 for (i = 0; i < num_plugins; i++)
133 plugin = attr_plugins[i];
134 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
142 * Convert human-readable version of a 'claim' of an attribute to the binary
145 * @param type type of the claim
146 * @param s human-readable string
147 * @param data set to value in binary encoding (will be allocated)
148 * @param data_size set to number of bytes in @a data
149 * @return #GNUNET_OK on success
152 GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type,
158 struct Plugin *plugin;
161 for (i = 0; i < num_plugins; i++)
163 plugin = attr_plugins[i];
164 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
171 return GNUNET_SYSERR;
175 * Convert the 'claim' of an attribute to a string
177 * @param type the type of attribute
178 * @param data claim in binary encoding
179 * @param data_size number of bytes in @a data
180 * @return NULL on error, otherwise human-readable representation of the claim
183 GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type,
188 struct Plugin *plugin;
192 for (i = 0; i < num_plugins; i++)
194 plugin = attr_plugins[i];
195 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
205 * Create a new attribute.
207 * @param attr_name the attribute name
208 * @param type the attribute type
209 * @param data the attribute value
210 * @param data_size the attribute value size
211 * @return the new attribute
213 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
214 GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char* attr_name,
219 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
221 char *attr_name_tmp = GNUNET_strdup (attr_name);
223 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
225 attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) +
226 strlen (attr_name_tmp) + 1 +
229 attr->data_size = data_size;
231 write_ptr = (char*)&attr[1];
232 GNUNET_memcpy (write_ptr,
234 strlen (attr_name_tmp) + 1);
235 attr->name = write_ptr;
236 write_ptr += strlen (attr->name) + 1;
237 GNUNET_memcpy (write_ptr,
240 attr->data = write_ptr;
241 GNUNET_free (attr_name_tmp);
246 * Add a new claim list entry.
248 * @param claim_list the attribute name
249 * @param attr_name the attribute name
250 * @param type the attribute type
251 * @param data the attribute value
252 * @param data_size the attribute value size
256 GNUNET_RECLAIM_ATTRIBUTE_list_add (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list,
257 const char* attr_name,
262 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
263 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
264 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name,
268 GNUNET_CONTAINER_DLL_insert (claim_list->list_head,
269 claim_list->list_tail,
274 GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
276 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
278 for (le = attrs->list_head; NULL != le; le = le->next)
279 len += GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
284 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
287 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
294 for (le = attrs->list_head; NULL != le; le = le->next)
296 len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim,
304 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *
305 GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char* data,
308 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs;
309 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
311 const char* read_ptr;
313 if (data_size < sizeof (struct Attribute))
316 attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
318 while (((data + data_size) - read_ptr) >= sizeof (struct Attribute))
321 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
322 le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr,
323 data_size - (read_ptr - data));
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Deserialized attribute %s\n", le->claim->name);
326 GNUNET_CONTAINER_DLL_insert (attrs->list_head,
329 attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
330 read_ptr += attr_len;
335 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList*
336 GNUNET_RECLAIM_ATTRIBUTE_list_dup (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
338 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
339 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le;
340 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result;
342 result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
343 for (le = attrs->list_head; NULL != le; le = le->next)
345 result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
346 result_le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name,
349 le->claim->data_size);
350 GNUNET_CONTAINER_DLL_insert (result->list_head,
359 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
361 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
362 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le;
364 for (le = attrs->list_head; NULL != le;)
366 GNUNET_free (le->claim);
369 GNUNET_free (tmp_le);
376 GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
378 return sizeof (struct Attribute)
379 + strlen (attr->name)
384 GNUNET_RECLAIM_ATTRIBUTE_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
389 struct Attribute *attr_ser;
392 attr_ser = (struct Attribute*)result;
393 attr_ser->attribute_type = htons (attr->type);
394 attr_ser->attribute_version = htonl (attr->version);
395 name_len = strlen (attr->name);
396 attr_ser->name_len = htons (name_len);
397 write_ptr = (char*)&attr_ser[1];
398 GNUNET_memcpy (write_ptr, attr->name, name_len);
399 write_ptr += name_len;
401 //data_len_ser = plugin->serialize_attribute_value (attr,
403 data_len_ser = attr->data_size;
404 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
405 attr_ser->data_size = htons (data_len_ser);
407 return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size;
410 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
411 GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char* data,
414 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
415 struct Attribute *attr_ser;
420 if (data_size < sizeof (struct Attribute))
423 attr_ser = (struct Attribute*)data;
424 data_len = ntohs (attr_ser->data_size);
425 name_len = ntohs (attr_ser->name_len);
426 attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
427 + data_len + name_len + 1);
428 attr->type = ntohs (attr_ser->attribute_type);
429 attr->version = ntohl (attr_ser->attribute_version);
430 attr->data_size = ntohs (attr_ser->data_size);
432 write_ptr = (char*)&attr[1];
433 GNUNET_memcpy (write_ptr,
436 write_ptr[name_len] = '\0';
437 attr->name = write_ptr;
439 write_ptr += name_len + 1;
440 GNUNET_memcpy (write_ptr,
441 (char*)&attr_ser[1] + name_len,
443 attr->data = write_ptr;
448 /* end of reclaim_attribute.c */