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;
222 attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) +
223 strlen (attr_name) + 1 +
226 attr->data_size = data_size;
228 write_ptr = (char*)&attr[1];
229 GNUNET_memcpy (write_ptr,
231 strlen (attr_name) + 1);
232 attr->name = write_ptr;
233 write_ptr += strlen (attr->name) + 1;
234 GNUNET_memcpy (write_ptr,
237 attr->data = write_ptr;
242 * Add a new claim list entry.
244 * @param claim_list the attribute name
245 * @param attr_name the attribute name
246 * @param type the attribute type
247 * @param data the attribute value
248 * @param data_size the attribute value size
252 GNUNET_RECLAIM_ATTRIBUTE_list_add (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list,
253 const char* attr_name,
258 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
259 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
260 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name,
264 GNUNET_CONTAINER_DLL_insert (claim_list->list_head,
265 claim_list->list_tail,
270 GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
272 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
274 for (le = attrs->list_head; NULL != le; le = le->next)
275 len += GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
280 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
283 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
290 for (le = attrs->list_head; NULL != le; le = le->next)
292 len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim,
300 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *
301 GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char* data,
304 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs;
305 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
307 const char* read_ptr;
309 if (data_size < sizeof (struct Attribute))
312 attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
314 while (((data + data_size) - read_ptr) >= sizeof (struct Attribute))
317 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
318 le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr,
319 data_size - (read_ptr - data));
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Deserialized attribute %s\n", le->claim->name);
322 GNUNET_CONTAINER_DLL_insert (attrs->list_head,
325 attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
326 read_ptr += attr_len;
331 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList*
332 GNUNET_RECLAIM_ATTRIBUTE_list_dup (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
334 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
335 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le;
336 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result;
338 result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
339 for (le = attrs->list_head; NULL != le; le = le->next)
341 result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
342 result_le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name,
345 le->claim->data_size);
346 GNUNET_CONTAINER_DLL_insert (result->list_head,
355 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
357 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
358 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le;
360 for (le = attrs->list_head; NULL != le;)
362 GNUNET_free (le->claim);
365 GNUNET_free (tmp_le);
372 GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
374 return sizeof (struct Attribute)
375 + strlen (attr->name)
380 GNUNET_RECLAIM_ATTRIBUTE_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
385 struct Attribute *attr_ser;
388 attr_ser = (struct Attribute*)result;
389 attr_ser->attribute_type = htons (attr->type);
390 attr_ser->attribute_version = htonl (attr->version);
391 name_len = strlen (attr->name);
392 attr_ser->name_len = htons (name_len);
393 write_ptr = (char*)&attr_ser[1];
394 GNUNET_memcpy (write_ptr, attr->name, name_len);
395 write_ptr += name_len;
397 //data_len_ser = plugin->serialize_attribute_value (attr,
399 data_len_ser = attr->data_size;
400 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
401 attr_ser->data_size = htons (data_len_ser);
403 return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size;
406 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
407 GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char* data,
410 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
411 struct Attribute *attr_ser;
416 if (data_size < sizeof (struct Attribute))
419 attr_ser = (struct Attribute*)data;
420 data_len = ntohs (attr_ser->data_size);
421 name_len = ntohs (attr_ser->name_len);
422 attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
423 + data_len + name_len + 1);
424 attr->type = ntohs (attr_ser->attribute_type);
425 attr->version = ntohl (attr_ser->attribute_version);
426 attr->data_size = ntohs (attr_ser->data_size);
428 write_ptr = (char*)&attr[1];
429 GNUNET_memcpy (write_ptr,
432 write_ptr[name_len] = '\0';
433 attr->name = write_ptr;
435 write_ptr += name_len + 1;
436 GNUNET_memcpy (write_ptr,
437 (char*)&attr_ser[1] + name_len,
439 attr->data = write_ptr;
444 /* end of reclaim_attribute.c */