GNUnet  0.17.6
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"
28 #include "gnunet_reclaim_plugin.h"
29 #include "reclaim_attribute.h"
30 
31 
35 struct Plugin
36 {
40  char *library_name;
41 
46 };
47 
48 
52 static struct Plugin **attr_plugins;
53 
54 
58 static unsigned int num_plugins;
59 
60 
64 static int initialized;
65 
66 
74 static void
75 add_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",
82  library_name);
83  plugin = GNUNET_new (struct Plugin);
84  plugin->api = api;
87 }
88 
89 
93 static void
94 init ()
95 {
96  if (GNUNET_YES == initialized)
97  return;
100  "libgnunet_plugin_reclaim_attribute_",
101  NULL,
102  &add_plugin,
103  NULL);
104 }
105 
109 void __attribute__ ((destructor))
110 RECLAIM_ATTRIBUTE_fini ()
111 {
112  struct Plugin *plugin;
115 
116  if (pd != dpd)
117  GNUNET_OS_init (dpd);
118 
119  for (unsigned int i = 0; i < num_plugins; i++)
120  {
121  plugin = attr_plugins[i];
122  GNUNET_break (NULL ==
124  plugin->api));
127  }
129 
130  if (pd != dpd)
131  GNUNET_OS_init (pd);
132 
133  attr_plugins = NULL;
134 }
135 
136 
137 
144 uint32_t
146 {
147  unsigned int i;
148  struct Plugin *plugin;
149  uint32_t ret;
150 
151  init ();
152  for (i = 0; i < num_plugins; i++)
153  {
154  plugin = attr_plugins[i];
155  if (UINT32_MAX !=
156  (ret = plugin->api->typename_to_number (plugin->api->cls, typename)))
157  return ret;
158  }
159  return UINT32_MAX;
160 }
161 
162 
169 const char *
171 {
172  unsigned int i;
173  struct Plugin *plugin;
174  const char *ret;
175 
176  init ();
177  for (i = 0; i < num_plugins; i++)
178  {
179  plugin = attr_plugins[i];
180  if (NULL !=
181  (ret = plugin->api->number_to_typename (plugin->api->cls, type)))
182  return ret;
183  }
184  return NULL;
185 }
186 
187 
198 int
200  const char *s,
201  void **data,
202  size_t *data_size)
203 {
204  unsigned int i;
205  struct Plugin *plugin;
206 
207  init ();
208  for (i = 0; i < num_plugins; i++)
209  {
210  plugin = attr_plugins[i];
211  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
212  type,
213  s,
214  data,
215  data_size))
216  return GNUNET_OK;
217  }
218  return GNUNET_SYSERR;
219 }
220 
221 
230 char *
232  const void *data,
233  size_t data_size)
234 {
235  unsigned int i;
236  struct Plugin *plugin;
237  char *ret;
238 
239  init ();
240  for (i = 0; i < num_plugins; i++)
241  {
242  plugin = attr_plugins[i];
243  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
244  type,
245  data,
246  data_size)))
247  return ret;
248  }
249  return NULL;
250 }
251 
252 
255  const struct
257  uint32_t type,
258  const void *data,
259  size_t data_size)
260 {
261  struct GNUNET_RECLAIM_Attribute *attr;
262  char *write_ptr;
263  char *attr_name_tmp = GNUNET_strdup (attr_name);
264 
265  GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
266 
267  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
268  + strlen (attr_name_tmp) + 1 + data_size);
269  if (NULL != credential)
270  attr->credential = *credential;
271  attr->type = type;
272  attr->data_size = data_size;
273  attr->flag = 0;
274  write_ptr = (char *) &attr[1];
275  GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
276  attr->name = write_ptr;
277  write_ptr += strlen (attr->name) + 1;
278  GNUNET_memcpy (write_ptr, data, data_size);
279  attr->data = write_ptr;
280  GNUNET_free (attr_name_tmp);
281  return attr;
282 }
283 
284 
293 void
295  struct GNUNET_RECLAIM_AttributeList *al,
296  const char *attr_name,
298  uint32_t type,
299  const void *data,
300  size_t data_size)
301 {
303 
305  ale->attribute =
307  type, data, data_size);
309  al->list_tail,
310  ale);
311 }
312 
313 
320 size_t
322  const struct GNUNET_RECLAIM_AttributeList *al)
323 {
325  size_t len = 0;
326 
327  for (ale = al->list_head; NULL != ale; ale = ale->next)
328  {
329  GNUNET_assert (NULL != ale->attribute);
331  }
332  return len;
333 }
334 
335 
343 size_t
345  const struct GNUNET_RECLAIM_AttributeList *al,
346  char *result)
347 {
349  size_t len;
350  size_t total_len;
351  char *write_ptr;
352  write_ptr = result;
353  total_len = 0;
354  for (ale = al->list_head; NULL != ale; ale = ale->next)
355  {
356  GNUNET_assert (NULL != ale->attribute);
358  total_len += len;
359  write_ptr += len;
360  }
361  return total_len;
362 }
363 
364 
374 {
375  struct GNUNET_RECLAIM_AttributeList *al;
377  size_t attr_len;
378  const char *read_ptr;
379  size_t left = data_size;
380 
382  if (data_size < sizeof(struct Attribute))
383  return al;
384  read_ptr = data;
385  while (left >= sizeof(struct Attribute))
386  {
388  attr_len =
390  left,
391  &ale->attribute);
392  if (-1 == attr_len)
393  {
395  "Failed to deserialize malformed attribute.\n");
396  GNUNET_free (ale);
397  return al;
398  }
399  left -= attr_len;
401  read_ptr += attr_len;
402  }
403  return al;
404 }
405 
406 
414  const struct GNUNET_RECLAIM_AttributeList *al)
415 {
417  struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
419 
421  for (ale = al->list_head; NULL != ale; ale = ale->next)
422  {
423  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
424  GNUNET_assert (NULL != ale->attribute);
425  {
426  result_ale->attribute =
428  &ale->attribute->credential,
429  ale->attribute->type,
430  ale->attribute->data,
431  ale->attribute->data_size);
432 
433  result_ale->attribute->id = ale->attribute->id;
434  result_ale->attribute->flag = ale->attribute->flag;
435  }
437  result->list_tail,
438  result_ale);
439  }
440  return result;
441 }
442 
443 
449 void
451  struct GNUNET_RECLAIM_AttributeList *al)
452 {
454  struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
455 
456  for (ale = al->list_head; NULL != ale;)
457  {
458  if (NULL != ale->attribute)
459  GNUNET_free (ale->attribute);
460  tmp_ale = ale;
461  ale = ale->next;
462  GNUNET_free (tmp_ale);
463  }
464  GNUNET_free (al);
465 }
466 
467 
474 size_t
476  const struct GNUNET_RECLAIM_Attribute *attr)
477 {
478  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
479 }
480 
481 
489 size_t
491  const struct GNUNET_RECLAIM_Attribute *attr,
492  char *result)
493 {
494  size_t data_len_ser;
495  size_t name_len;
496  struct Attribute *attr_ser;
497  char *write_ptr;
498 
499  attr_ser = (struct Attribute *) result;
500  attr_ser->attribute_type = htons (attr->type);
501  attr_ser->attribute_flag = htonl (attr->flag);
502  attr_ser->attribute_id = attr->id;
503  attr_ser->credential_id = attr->credential;
504  name_len = strlen (attr->name);
505  attr_ser->name_len = htons (name_len);
506  write_ptr = (char *) &attr_ser[1];
507  GNUNET_memcpy (write_ptr, attr->name, name_len);
508  write_ptr += name_len;
509  // TODO plugin-ize
510  // data_len_ser = plugin->serialize_attribute_value (attr,
511  // &attr_ser[1]);
512  data_len_ser = attr->data_size;
513  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
514  attr_ser->data_size = htons (data_len_ser);
515 
516  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
517 }
518 
519 
528 ssize_t
530  struct GNUNET_RECLAIM_Attribute **attr)
531 {
532  struct Attribute *attr_ser;
533  struct GNUNET_RECLAIM_Attribute *attribute;
534  size_t data_len;
535  size_t name_len;
536  char *write_ptr;
537 
538  if (data_size < sizeof(struct Attribute))
539  return -1;
540 
541  attr_ser = (struct Attribute *) data;
542  data_len = ntohs (attr_ser->data_size);
543  name_len = ntohs (attr_ser->name_len);
544  if (data_size < sizeof(struct Attribute) + data_len + name_len)
545  {
547  "Buffer too small to deserialize\n");
548  return -1;
549  }
550  attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
551  + data_len + name_len + 1);
552  attribute->type = ntohs (attr_ser->attribute_type);
553  attribute->flag = ntohl (attr_ser->attribute_flag);
554  attribute->id = attr_ser->attribute_id;
555  attribute->credential = attr_ser->credential_id;
556  attribute->data_size = data_len;
557 
558  write_ptr = (char *) &attribute[1];
559  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
560  write_ptr[name_len] = '\0';
561  attribute->name = write_ptr;
562 
563  write_ptr += name_len + 1;
564  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len,
565  attribute->data_size);
566  *attr = attribute;
567  attribute->data = write_ptr;
568  return sizeof(struct Attribute) + data_len + name_len;
569 }
570 
571 
572 /* end of reclaim_attribute.c */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
struct Plugin * plugin
The process handle to the testbed service.
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#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_default(void)
Return default project data used by 'libgnunetutil' for GNUnet.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:241
void GNUNET_PLUGIN_load_all_in_context(const struct GNUNET_OS_ProjectData *ctx, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name while inside the given context (i....
Definition: plugin.c:385
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.
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_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 *al, 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.
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.
struct GNUNET_RECLAIM_AttributeList * GNUNET_RECLAIM_attribute_list_dup(const struct GNUNET_RECLAIM_AttributeList *al)
Make a (deep) copy of a claim list.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
Convert the utf-8 input string to lower case.
Definition: strings.c:450
const char * name
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin.
static struct Plugin ** attr_plugins
Plugins.
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.
uint32_t attribute_type
Attribute type.
uint32_t name_len
Name length.
struct GNUNET_RECLAIM_Identifier credential_id
Credential ID.
uint32_t data_size
Data size.
uint32_t attribute_flag
Attribute flag.
void * cls
Closure for all of the callbacks.
Project-specific data used to help the OS subsystem find installation paths.
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.
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
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model