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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file identity-attribute/identity_attribute.c
23 * @brief helper library to manage identity attributes
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "identity_attribute.h"
29 #include "gnunet_identity_attribute_plugin.h"
44 struct GNUNET_IDENTITY_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_IDENTITY_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_identity_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_IDENTITY_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_IDENTITY_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_IDENTITY_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_IDENTITY_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_IDENTITY_ATTRIBUTE_Claim *
216 GNUNET_IDENTITY_ATTRIBUTE_claim_new (const char* attr_name,
221 struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr;
224 attr = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ATTRIBUTE_Claim) +
225 strlen (attr_name) + 1 +
228 attr->data_size = data_size;
230 write_ptr = (char*)&attr[1];
231 GNUNET_memcpy (write_ptr,
233 strlen (attr_name) + 1);
234 attr->name = write_ptr;
235 write_ptr += strlen (attr->name) + 1;
236 GNUNET_memcpy (write_ptr,
239 attr->data = write_ptr;
244 GNUNET_IDENTITY_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
246 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
248 for (le = attrs->list_head; NULL != le; le = le->next)
249 len += GNUNET_IDENTITY_ATTRIBUTE_serialize_get_size (le->claim);
254 GNUNET_IDENTITY_ATTRIBUTE_list_serialize (const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs,
257 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
264 for (le = attrs->list_head; NULL != le; le = le->next)
266 len = GNUNET_IDENTITY_ATTRIBUTE_serialize (le->claim,
274 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *
275 GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (const char* data,
278 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs;
279 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
281 const char* read_ptr;
283 if (data_size < sizeof (struct Attribute))
286 attrs = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList);
288 while (((data + data_size) - read_ptr) >= sizeof (struct Attribute))
291 le = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry);
292 le->claim = GNUNET_IDENTITY_ATTRIBUTE_deserialize (read_ptr,
293 data_size - (read_ptr - data));
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Deserialized attribute %s\n", le->claim->name);
296 GNUNET_CONTAINER_DLL_insert (attrs->list_head,
299 attr_len = GNUNET_IDENTITY_ATTRIBUTE_serialize_get_size (le->claim);
300 read_ptr += attr_len;
305 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList*
306 GNUNET_IDENTITY_ATTRIBUTE_list_dup (const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
308 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
309 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *result_le;
310 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *result;
313 result = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList);
314 for (le = attrs->list_head; NULL != le; le = le->next)
316 result_le = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry);
317 len = sizeof (struct GNUNET_IDENTITY_ATTRIBUTE_Claim) + le->claim->data_size;
318 result_le->claim = GNUNET_malloc (len);
319 GNUNET_memcpy (result_le->claim,
322 result_le->claim->name = (const char*)&result_le->claim[1];
323 GNUNET_CONTAINER_DLL_insert (result->list_head,
332 GNUNET_IDENTITY_ATTRIBUTE_list_destroy (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
334 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
335 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *tmp_le;
337 for (le = attrs->list_head; NULL != le;)
339 GNUNET_free (le->claim);
342 GNUNET_free (tmp_le);
349 GNUNET_IDENTITY_ATTRIBUTE_serialize_get_size (const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr)
351 return sizeof (struct Attribute)
352 + strlen (attr->name)
357 GNUNET_IDENTITY_ATTRIBUTE_serialize (const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr,
362 struct Attribute *attr_ser;
365 attr_ser = (struct Attribute*)result;
366 attr_ser->attribute_type = htons (attr->type);
367 attr_ser->attribute_version = htonl (attr->version);
368 name_len = strlen (attr->name);
369 attr_ser->name_len = htons (name_len);
370 write_ptr = (char*)&attr_ser[1];
371 GNUNET_memcpy (write_ptr, attr->name, name_len);
372 write_ptr += name_len;
374 //data_len_ser = plugin->serialize_attribute_value (attr,
376 data_len_ser = attr->data_size;
377 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
378 attr_ser->data_size = htons (data_len_ser);
380 return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size;
383 struct GNUNET_IDENTITY_ATTRIBUTE_Claim *
384 GNUNET_IDENTITY_ATTRIBUTE_deserialize (const char* data,
387 struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr;
388 struct Attribute *attr_ser;
393 if (data_size < sizeof (struct Attribute))
396 attr_ser = (struct Attribute*)data;
397 data_len = ntohs (attr_ser->data_size);
398 name_len = ntohs (attr_ser->name_len);
399 attr = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ATTRIBUTE_Claim)
400 + data_len + name_len + 1);
401 attr->type = ntohs (attr_ser->attribute_type);
402 attr->version = ntohl (attr_ser->attribute_version);
403 attr->data_size = ntohs (attr_ser->data_size);
405 write_ptr = (char*)&attr[1];
406 GNUNET_memcpy (write_ptr,
409 write_ptr[name_len] = '\0';
410 attr->name = write_ptr;
412 write_ptr += name_len + 1;
413 GNUNET_memcpy (write_ptr,
414 (char*)&attr_ser[1] + name_len,
416 attr->data = write_ptr;
421 /* end of identity_attribute.c */