GNUnet  0.19.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"
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 
285 void
287  struct GNUNET_RECLAIM_AttributeList *al,
288  const char *attr_name,
290  uint32_t type,
291  const void *data,
292  size_t data_size)
293 {
295 
297  ale->attribute =
299  type, data, data_size);
301  al->list_tail,
302  ale);
303 }
304 
305 
312 size_t
314  const struct GNUNET_RECLAIM_AttributeList *al)
315 {
317  size_t len = 0;
318 
319  for (ale = al->list_head; NULL != ale; ale = ale->next)
320  {
321  GNUNET_assert (NULL != ale->attribute);
323  }
324  return len;
325 }
326 
327 
328 size_t
330  const struct GNUNET_RECLAIM_AttributeList *attrs,
331  char *result)
332 {
334  size_t len;
335  size_t total_len;
336  char *write_ptr;
337  write_ptr = result;
338  total_len = 0;
339  for (ale = attrs->list_head; NULL != ale; ale = ale->next)
340  {
341  GNUNET_assert (NULL != ale->attribute);
343  total_len += len;
344  write_ptr += len;
345  }
346  return total_len;
347 }
348 
349 
359 {
360  struct GNUNET_RECLAIM_AttributeList *al;
362  size_t attr_len;
363  const char *read_ptr;
364  size_t left = data_size;
365 
367  if (data_size < sizeof(struct Attribute))
368  return al;
369  read_ptr = data;
370  while (left >= sizeof(struct Attribute))
371  {
373  attr_len =
375  left,
376  &ale->attribute);
377  if (-1 == attr_len)
378  {
380  "Failed to deserialize malformed attribute.\n");
381  GNUNET_free (ale);
382  return al;
383  }
384  left -= attr_len;
386  read_ptr += attr_len;
387  }
388  return al;
389 }
390 
391 
394  const struct GNUNET_RECLAIM_AttributeList *attrs)
395 {
397  struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
399 
401  for (ale = attrs->list_head; NULL != ale; ale = ale->next)
402  {
403  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
404  GNUNET_assert (NULL != ale->attribute);
405  {
406  result_ale->attribute =
408  &ale->attribute->credential,
409  ale->attribute->type,
410  ale->attribute->data,
411  ale->attribute->data_size);
412 
413  result_ale->attribute->id = ale->attribute->id;
414  result_ale->attribute->flag = ale->attribute->flag;
415  }
417  result->list_tail,
418  result_ale);
419  }
420  return result;
421 }
422 
423 
424 void
426  struct GNUNET_RECLAIM_AttributeList *al)
427 {
429  struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
430 
431  for (ale = al->list_head; NULL != ale;)
432  {
433  if (NULL != ale->attribute)
434  GNUNET_free (ale->attribute);
435  tmp_ale = ale;
436  ale = ale->next;
437  GNUNET_free (tmp_ale);
438  }
439  GNUNET_free (al);
440 }
441 
442 
449 size_t
451  const struct GNUNET_RECLAIM_Attribute *attr)
452 {
453  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
454 }
455 
456 
464 size_t
466  const struct GNUNET_RECLAIM_Attribute *attr,
467  char *result)
468 {
469  size_t data_len_ser;
470  size_t name_len;
471  struct Attribute *attr_ser;
472  char *write_ptr;
473 
474  attr_ser = (struct Attribute *) result;
475  attr_ser->attribute_type = htonl (attr->type);
476  attr_ser->attribute_flag = htonl (attr->flag);
477  attr_ser->attribute_id = attr->id;
478  attr_ser->credential_id = attr->credential;
479  name_len = strlen (attr->name);
480  attr_ser->name_len = htons (name_len);
481  write_ptr = (char *) &attr_ser[1];
482  GNUNET_memcpy (write_ptr, attr->name, name_len);
483  write_ptr += name_len;
484  // TODO plugin-ize
485  // data_len_ser = plugin->serialize_attribute_value (attr,
486  // &attr_ser[1]);
487  data_len_ser = attr->data_size;
488  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
489  attr_ser->data_size = htons (data_len_ser);
490 
491  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
492 }
493 
494 
503 ssize_t
505  struct GNUNET_RECLAIM_Attribute **attr)
506 {
507  struct Attribute *attr_ser;
508  struct GNUNET_RECLAIM_Attribute *attribute;
509  size_t data_len;
510  size_t name_len;
511  char *write_ptr;
512 
513  if (data_size < sizeof(struct Attribute))
514  return -1;
515 
516  attr_ser = (struct Attribute *) data;
517  data_len = ntohs (attr_ser->data_size);
518  name_len = ntohs (attr_ser->name_len);
519  if (data_size < sizeof(struct Attribute) + data_len + name_len)
520  {
522  "Buffer too small to deserialize\n");
523  return -1;
524  }
525  attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
526  + data_len + name_len + 1);
527  attribute->type = ntohl (attr_ser->attribute_type);
528  attribute->flag = ntohl (attr_ser->attribute_flag);
529  attribute->id = attr_ser->attribute_id;
530  attribute->credential = attr_ser->credential_id;
531  attribute->data_size = data_len;
532 
533  write_ptr = (char *) &attribute[1];
534  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
535  write_ptr[name_len] = '\0';
536  attribute->name = write_ptr;
537 
538  write_ptr += name_len + 1;
539  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len,
540  attribute->data_size);
541  *attr = attribute;
542  attribute->data = write_ptr;
543  return sizeof(struct Attribute) + data_len + name_len;
544 }
545 
546 
547 /* 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 TestcasePlugin * 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
@ 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_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:242
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:386
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 *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.
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 *attrs)
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:449
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.
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.
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
char * library_name
Name of the shared library.
Definition: testing.h:98
struct GNUNET_TESTING_PluginFunctions * api
Plugin API.
Definition: testing.h:103
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model