GNUnet 0.22.2
reclaim_attribute.c
Go to the documentation of this file.
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
26#include "platform.h"
27#include "gnunet_util_lib.h"
29#include "reclaim_attribute.h"
30
31
35struct Plugin
36{
40 char *library_name;
41
46};
47
48
52static struct Plugin **attr_plugins;
53
54
58static unsigned int num_plugins;
59
60
64static int initialized;
65
66
74static void
75add_plugin (void *cls, const char *library_name, void *lib_ret)
76{
77 struct GNUNET_RECLAIM_AttributePluginFunctions *api = lib_ret;
78 struct Plugin *plugin;
79
81 "Loading attribute plugin `%s'\n",
83 plugin = GNUNET_new (struct Plugin);
84 plugin->api = api;
85 plugin->library_name = GNUNET_strdup (library_name);
87}
88
89
93static void
95{
97 return;
100 "libgnunet_plugin_reclaim_attribute_",
101 NULL,
102 &add_plugin,
103 NULL);
104}
105
106
108
112void __attribute__ ((destructor))
114{
115 struct Plugin *plugin;
116
117 for (unsigned int i = 0; i < num_plugins; i++)
118 {
119 plugin = attr_plugins[i];
120 GNUNET_break (NULL ==
121 GNUNET_PLUGIN_unload (plugin->library_name,
122 plugin->api));
123 GNUNET_free (plugin->library_name);
125 }
127 attr_plugins = NULL;
128}
129
130
137uint32_t
139{
140 unsigned int i;
141 struct Plugin *plugin;
142 uint32_t ret;
143
144 init ();
145 for (i = 0; i < num_plugins; i++)
146 {
147 plugin = attr_plugins[i];
148 if (UINT32_MAX !=
149 (ret = plugin->api->typename_to_number (plugin->api->cls, typename)))
150 return ret;
151 }
152 return UINT32_MAX;
153}
154
155
162const char *
164{
165 unsigned int i;
166 struct Plugin *plugin;
167 const char *ret;
168
169 init ();
170 for (i = 0; i < num_plugins; i++)
171 {
172 plugin = attr_plugins[i];
173 if (NULL !=
174 (ret = plugin->api->number_to_typename (plugin->api->cls, type)))
175 return ret;
176 }
177 return NULL;
178}
179
180
191int
193 const char *s,
194 void **data,
195 size_t *data_size)
196{
197 unsigned int i;
198 struct Plugin *plugin;
199
200 init ();
201 for (i = 0; i < num_plugins; i++)
202 {
203 plugin = attr_plugins[i];
204 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
205 type,
206 s,
207 data,
208 data_size))
209 return GNUNET_OK;
210 }
211 return GNUNET_SYSERR;
212}
213
214
223char *
225 const void *data,
226 size_t data_size)
227{
228 unsigned int i;
229 struct Plugin *plugin;
230 char *ret;
231
232 init ();
233 for (i = 0; i < num_plugins; i++)
234 {
235 plugin = attr_plugins[i];
236 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
237 type,
238 data,
239 data_size)))
240 return ret;
241 }
242 return NULL;
243}
244
245
248 const struct
250 uint32_t type,
251 const void *data,
252 size_t data_size)
253{
254 struct GNUNET_RECLAIM_Attribute *attr;
255 char *write_ptr;
256 char *attr_name_tmp = GNUNET_strdup (attr_name);
257
258 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
259
260 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
261 + strlen (attr_name_tmp) + 1 + data_size);
262 if (NULL != credential)
263 attr->credential = *credential;
264 attr->type = type;
265 attr->data_size = data_size;
266 attr->flag = 0;
267 write_ptr = (char *) &attr[1];
268 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
269 attr->name = write_ptr;
270 write_ptr += strlen (attr->name) + 1;
271 GNUNET_memcpy (write_ptr, data, data_size);
272 attr->data = write_ptr;
273 GNUNET_free (attr_name_tmp);
274 return attr;
275}
276
277
278void
281 const char *attr_name,
283 uint32_t type,
284 const void *data,
285 size_t data_size)
286{
288
290 ale->attribute =
294 al->list_tail,
295 ale);
296}
297
298
305size_t
307 const struct GNUNET_RECLAIM_AttributeList *al)
308{
310 size_t len = 0;
311
312 for (ale = al->list_head; NULL != ale; ale = ale->next)
313 {
314 GNUNET_assert (NULL != ale->attribute);
316 }
317 return len;
318}
319
320
321size_t
323 const struct GNUNET_RECLAIM_AttributeList *attrs,
324 char *result)
325{
327 size_t len;
328 size_t total_len;
329 char *write_ptr;
330 write_ptr = result;
331 total_len = 0;
332 for (ale = attrs->list_head; NULL != ale; ale = ale->next)
333 {
334 GNUNET_assert (NULL != ale->attribute);
335 len = GNUNET_RECLAIM_attribute_serialize (ale->attribute, write_ptr);
336 total_len += len;
337 write_ptr += len;
338 }
339 return total_len;
340}
341
342
352{
355 size_t attr_len;
356 const char *read_ptr;
357 size_t left = data_size;
358
360 if (data_size < sizeof(struct Attribute))
361 return al;
362 read_ptr = data;
363 while (left >= sizeof(struct Attribute))
364 {
366 attr_len =
368 left,
369 &ale->attribute);
370 if (-1 == attr_len)
371 {
373 "Failed to deserialize malformed attribute.\n");
374 GNUNET_free (ale);
375 return al;
376 }
377 left -= attr_len;
379 read_ptr += attr_len;
380 }
381 return al;
382}
383
384
387 const struct GNUNET_RECLAIM_AttributeList *attrs)
388{
390 struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
392
394 for (ale = attrs->list_head; NULL != ale; ale = ale->next)
395 {
396 result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
397 GNUNET_assert (NULL != ale->attribute);
398 {
399 result_ale->attribute =
401 &ale->attribute->credential,
402 ale->attribute->type,
403 ale->attribute->data,
404 ale->attribute->data_size);
405
406 result_ale->attribute->id = ale->attribute->id;
407 result_ale->attribute->flag = ale->attribute->flag;
408 }
410 result->list_tail,
411 result_ale);
412 }
413 return result;
414}
415
416
417void
420{
422 struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
423
424 for (ale = al->list_head; NULL != ale;)
425 {
426 if (NULL != ale->attribute)
427 GNUNET_free (ale->attribute);
428 tmp_ale = ale;
429 ale = ale->next;
430 GNUNET_free (tmp_ale);
431 }
432 GNUNET_free (al);
433}
434
435
442size_t
444 const struct GNUNET_RECLAIM_Attribute *attr)
445{
446 return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
447}
448
449
457size_t
459 const struct GNUNET_RECLAIM_Attribute *attr,
460 char *result)
461{
462 size_t data_len_ser;
463 size_t name_len;
464 struct Attribute *attr_ser;
465 char *write_ptr;
466
467 attr_ser = (struct Attribute *) result;
468 attr_ser->attribute_type = htonl (attr->type);
469 attr_ser->attribute_flag = htonl (attr->flag);
470 attr_ser->attribute_id = attr->id;
471 attr_ser->credential_id = attr->credential;
472 name_len = strlen (attr->name);
473 attr_ser->name_len = htons (name_len);
474 write_ptr = (char *) &attr_ser[1];
475 GNUNET_memcpy (write_ptr, attr->name, name_len);
476 write_ptr += name_len;
477 // TODO plugin-ize
478 // data_len_ser = plugin->serialize_attribute_value (attr,
479 // &attr_ser[1]);
480 data_len_ser = attr->data_size;
481 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
482 attr_ser->data_size = htons (data_len_ser);
483
484 return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
485}
486
487
496ssize_t
498 struct GNUNET_RECLAIM_Attribute **attr)
499{
500 struct Attribute *attr_ser;
501 struct GNUNET_RECLAIM_Attribute *attribute;
502 size_t data_len;
503 size_t name_len;
504 char *write_ptr;
505
506 if (data_size < sizeof(struct Attribute))
507 return -1;
508
509 attr_ser = (struct Attribute *) data;
510 data_len = ntohs (attr_ser->data_size);
511 name_len = ntohs (attr_ser->name_len);
512 if (data_size < sizeof(struct Attribute) + data_len + name_len)
513 {
515 "Buffer too small to deserialize\n");
516 return -1;
517 }
518 attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
519 + data_len + name_len + 1);
520 attribute->type = ntohl (attr_ser->attribute_type);
521 attribute->flag = ntohl (attr_ser->attribute_flag);
522 attribute->id = attr_ser->attribute_id;
523 attribute->credential = attr_ser->credential_id;
524 attribute->data_size = data_len;
525
526 write_ptr = (char *) &attribute[1];
527 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
528 write_ptr[name_len] = '\0';
529 attribute->name = write_ptr;
530
531 write_ptr += name_len + 1;
532 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len,
533 attribute->data_size);
534 *attr = attribute;
535 attribute->data = write_ptr;
536 return sizeof(struct Attribute) + data_len + name_len;
537}
538
539
540/* end of reclaim_attribute.c */
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static char * data
The data to insert into the dht.
static char * name
Name (label) of the records to list.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
static char * attr_name
The attribute.
static struct GNUNET_RECLAIM_Identifier credential
Credential ID.
static int result
Global testing status.
Plugin API for reclaim attribute types.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
void GNUNET_PLUGIN_load_all(const struct GNUNET_OS_ProjectData *pd, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name.
Definition: plugin.c:401
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:277
int GNUNET_RECLAIM_attribute_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a 'claim' of an attribute to the binary representation.
void GNUNET_RECLAIM_attribute_list_destroy(struct GNUNET_RECLAIM_AttributeList *al)
Destroy claim list.
struct GNUNET_RECLAIM_AttributeList * GNUNET_RECLAIM_attribute_list_dup(const struct GNUNET_RECLAIM_AttributeList *attrs)
Make a (deep) copy of a claim list.
struct GNUNET_RECLAIM_AttributeList * GNUNET_RECLAIM_attribute_list_deserialize(const char *data, size_t data_size)
Deserialize an attribute list.
size_t GNUNET_RECLAIM_attribute_serialize_get_size(const struct GNUNET_RECLAIM_Attribute *attr)
Get required size for serialization buffer.
size_t GNUNET_RECLAIM_attribute_list_serialize(const struct GNUNET_RECLAIM_AttributeList *attrs, char *result)
Serialize an attribute list.
void GNUNET_RECLAIM_attribute_list_add(struct GNUNET_RECLAIM_AttributeList *al, const char *attr_name, const struct GNUNET_RECLAIM_Identifier *credential, uint32_t type, const void *data, size_t data_size)
Add a new attribute to a claim list.
ssize_t GNUNET_RECLAIM_attribute_deserialize(const char *data, size_t data_size, struct GNUNET_RECLAIM_Attribute **attr)
Deserialize an attribute.
char * GNUNET_RECLAIM_attribute_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the 'claim' of an attribute to a string.
struct GNUNET_RECLAIM_Attribute * GNUNET_RECLAIM_attribute_new(const char *attr_name, const struct GNUNET_RECLAIM_Identifier *credential, uint32_t type, const void *data, size_t data_size)
Create a new attribute claim.
const char * GNUNET_RECLAIM_attribute_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string.
size_t GNUNET_RECLAIM_attribute_list_serialize_get_size(const struct GNUNET_RECLAIM_AttributeList *al)
Get required size for serialization buffer.
uint32_t GNUNET_RECLAIM_attribute_typename_to_number(const char *typename)
Convert a type name to the corresponding number.
size_t GNUNET_RECLAIM_attribute_serialize(const struct GNUNET_RECLAIM_Attribute *attr, char *result)
Serialize an attribute.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
Convert the utf-8 input string to lower case.
Definition: strings.c:459
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin.
static struct Plugin ** attr_plugins
Plugins.
void RECLAIM_ATTRIBUTE_fini(void)
static unsigned int num_plugins
Number of plugins.
static int initialized
Init canary.
void __attribute__((destructor))
Dual function to init().
static void init()
Load plugins.
Serialized claim.
struct GNUNET_RECLAIM_Identifier attribute_id
Attribute ID.
uint16_t name_len
Name length.
uint16_t data_size
Data size.
uint32_t attribute_type
Attribute type.
struct GNUNET_RECLAIM_Identifier credential_id
Credential ID.
uint32_t attribute_flag
Attribute flag.
struct GNUNET_RECLAIM_Attribute * attribute
The attribute claim.
struct GNUNET_RECLAIM_AttributeListEntry * next
DLL.
A list of GNUNET_RECLAIM_Attribute structures.
struct GNUNET_RECLAIM_AttributeListEntry * list_tail
List tail.
struct GNUNET_RECLAIM_AttributeListEntry * list_head
List head.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
const char * name
The name of the attribute.
struct GNUNET_RECLAIM_Identifier credential
Referenced ID of credential (may be GNUNET_RECLAIM_ID_ZERO if self-creded)
struct GNUNET_RECLAIM_Identifier id
ID.
uint32_t type
Type of Claim.
const void * data
Binary value stored as attribute value.
size_t data_size
Number of bytes in data.
A reclaim identifier FIXME maybe put this in a different namespace.
void * cls
Closure to pass to start_testcase.
Handle for a plugin.
Definition: block.c:38
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
struct GNUNET_RECLAIM_AttributePluginFunctions * api
Plugin API.
char * library_name
Name of the shared library.
Definition: block.c:42