GNUnet  0.11.x
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 {
78  struct Plugin *plugin;
79 
81  "Loading attribute plugin `%s'\n",
82  library_name);
83  plugin = GNUNET_new (struct Plugin);
84  plugin->api = api;
85  plugin->library_name = GNUNET_strdup (library_name);
86  GNUNET_array_append (attr_plugins, num_plugins, plugin);
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));
125  GNUNET_free (plugin->library_name);
126  GNUNET_free (plugin);
127  }
128  GNUNET_free (attr_plugins);
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 
265  const struct
267  uint32_t type,
268  const void *data,
269  size_t data_size)
270 {
271  struct GNUNET_RECLAIM_Attribute *attr;
272  char *write_ptr;
273  char *attr_name_tmp = GNUNET_strdup (attr_name);
274 
275  GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
276 
277  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
278  + strlen (attr_name_tmp) + 1 + data_size);
279  if (NULL != credential)
280  attr->credential = *credential;
281  attr->type = type;
282  attr->data_size = data_size;
283  attr->flag = 0;
284  write_ptr = (char *) &attr[1];
285  GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
286  attr->name = write_ptr;
287  write_ptr += strlen (attr->name) + 1;
288  GNUNET_memcpy (write_ptr, data, data_size);
289  attr->data = write_ptr;
290  GNUNET_free (attr_name_tmp);
291  return attr;
292 }
293 
294 
303 void
305  struct GNUNET_RECLAIM_AttributeList *al,
306  const char *attr_name,
308  uint32_t type,
309  const void *data,
310  size_t data_size)
311 {
313 
315  ale->attribute =
316  GNUNET_RECLAIM_attribute_new (attr_name, credential,
317  type, data, data_size);
319  al->list_tail,
320  ale);
321 }
322 
323 
330 size_t
332  const struct GNUNET_RECLAIM_AttributeList *al)
333 {
335  size_t len = 0;
336 
337  for (ale = al->list_head; NULL != ale; ale = ale->next)
338  {
339  GNUNET_assert (NULL != ale->attribute);
341  }
342  return len;
343 }
344 
345 
353 size_t
355  const struct GNUNET_RECLAIM_AttributeList *al,
356  char *result)
357 {
359  size_t len;
360  size_t total_len;
361  char *write_ptr;
362  write_ptr = result;
363  total_len = 0;
364  for (ale = al->list_head; NULL != ale; ale = ale->next)
365  {
366  GNUNET_assert (NULL != ale->attribute);
367  len = GNUNET_RECLAIM_attribute_serialize (ale->attribute, write_ptr);
368  total_len += len;
369  write_ptr += len;
370  }
371  return total_len;
372 }
373 
374 
384 {
385  struct GNUNET_RECLAIM_AttributeList *al;
387  size_t attr_len;
388  const char *read_ptr;
389  size_t left = data_size;
390 
392  if (data_size < sizeof(struct Attribute))
393  return al;
394  read_ptr = data;
395  while (left >= sizeof(struct Attribute))
396  {
398  attr_len =
400  left,
401  &ale->attribute);
402  if (-1 == attr_len)
403  {
405  "Failed to deserialize malformed attribute.\n");
406  GNUNET_free (ale);
407  return al;
408  }
409  left -= attr_len;
411  read_ptr += attr_len;
412  }
413  return al;
414 }
415 
416 
424  const struct GNUNET_RECLAIM_AttributeList *al)
425 {
427  struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
429 
430  result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
431  for (ale = al->list_head; NULL != ale; ale = ale->next)
432  {
433  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
434  GNUNET_assert (NULL != ale->attribute);
435  {
436  result_ale->attribute =
438  &ale->attribute->credential,
439  ale->attribute->type,
440  ale->attribute->data,
441  ale->attribute->data_size);
442 
443  result_ale->attribute->id = ale->attribute->id;
444  result_ale->attribute->flag = ale->attribute->flag;
445  }
447  result->list_tail,
448  result_ale);
449  }
450  return result;
451 }
452 
453 
459 void
461  struct GNUNET_RECLAIM_AttributeList *al)
462 {
464  struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
465 
466  for (ale = al->list_head; NULL != ale;)
467  {
468  if (NULL != ale->attribute)
469  GNUNET_free (ale->attribute);
470  tmp_ale = ale;
471  ale = ale->next;
472  GNUNET_free (tmp_ale);
473  }
474  GNUNET_free (al);
475 }
476 
477 
484 size_t
486  const struct GNUNET_RECLAIM_Attribute *attr)
487 {
488  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
489 }
490 
491 
499 size_t
501  const struct GNUNET_RECLAIM_Attribute *attr,
502  char *result)
503 {
504  size_t data_len_ser;
505  size_t name_len;
506  struct Attribute *attr_ser;
507  char *write_ptr;
508 
509  attr_ser = (struct Attribute *) result;
510  attr_ser->attribute_type = htons (attr->type);
511  attr_ser->attribute_flag = htonl (attr->flag);
512  attr_ser->attribute_id = attr->id;
513  attr_ser->credential_id = attr->credential;
514  name_len = strlen (attr->name);
515  attr_ser->name_len = htons (name_len);
516  write_ptr = (char *) &attr_ser[1];
517  GNUNET_memcpy (write_ptr, attr->name, name_len);
518  write_ptr += name_len;
519  // TODO plugin-ize
520  // data_len_ser = plugin->serialize_attribute_value (attr,
521  // &attr_ser[1]);
522  data_len_ser = attr->data_size;
523  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
524  attr_ser->data_size = htons (data_len_ser);
525 
526  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
527 }
528 
529 
538 ssize_t
540  struct GNUNET_RECLAIM_Attribute **attr)
541 {
542  struct Attribute *attr_ser;
543  struct GNUNET_RECLAIM_Attribute *attribute;
544  size_t data_len;
545  size_t name_len;
546  char *write_ptr;
547 
548  if (data_size < sizeof(struct Attribute))
549  return -1;
550 
551  attr_ser = (struct Attribute *) data;
552  data_len = ntohs (attr_ser->data_size);
553  name_len = ntohs (attr_ser->name_len);
554  if (data_size < sizeof(struct Attribute) + data_len + name_len)
555  {
557  "Buffer too small to deserialize\n");
558  return -1;
559  }
560  attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
561  + data_len + name_len + 1);
562  attribute->type = ntohs (attr_ser->attribute_type);
563  attribute->flag = ntohl (attr_ser->attribute_flag);
564  attribute->id = attr_ser->attribute_id;
565  attribute->credential = attr_ser->credential_id;
566  attribute->data_size = data_len;
567 
568  write_ptr = (char *) &attribute[1];
569  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
570  write_ptr[name_len] = '\0';
571  attribute->name = write_ptr;
572 
573  write_ptr += name_len + 1;
574  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len,
575  attribute->data_size);
576  *attr = attribute;
577  attribute->data = write_ptr;
578  return sizeof(struct Attribute) + data_len + name_len;
579 }
580 
581 
582 /* end of reclaim_attribute.c */
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.
struct GNUNET_RECLAIM_AttributePluginFunctions * api
Plugin API.
Project-specific data used to help the OS subsystem find installation paths.
static unsigned int num_plugins
Number of plugins.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
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 *al)
Make a (deep) copy of a claim list.
const char * name
The name of the attribute.
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_RECLAIM_AttributeList * GNUNET_RECLAIM_attribute_list_deserialize(const char *data, size_t data_size)
Deserialize an attribute list.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void init()
Load plugins.
A list of GNUNET_RECLAIM_Attribute structures.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_default(void)
Return default project data used by &#39;libgnunetutil&#39; for GNUnet.
void __attribute__((destructor))
Dual function to init().
#define GNUNET_new(type)
Allocate a struct or union of the given type.
char * library_name
Name of the shared library.
Definition: block.c:42
Serialized claim.
struct GNUNET_RECLAIM_Identifier credential
Referenced ID of credential (may be GNUNET_RECLAIM_ID_ZERO if self-creded)
size_t GNUNET_RECLAIM_attribute_serialize(const struct GNUNET_RECLAIM_Attribute *attr, char *result)
Serialize an attribute.
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:443
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
size_t GNUNET_RECLAIM_attribute_serialize_get_size(const struct GNUNET_RECLAIM_Attribute *attr)
Get required size for serialization buffer.
struct GNUNET_RECLAIM_Attribute * attribute
The attribute claim.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:269
uint32_t attribute_flag
Attribute flag.
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin.
static char * attr_name
The attribute.
struct GNUNET_RECLAIM_AttributeListEntry * list_tail
List tail.
uint32_t data_size
Data size.
uint32_t GNUNET_RECLAIM_attribute_typename_to_number(const char *typename)
Convert a type name to the corresponding number.
static int result
Global testing status.
struct GNUNET_RECLAIM_Identifier id
ID.
size_t data_size
Number of bytes in data.
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 &#39;claim&#39; of an attribute to the binary representation.
A reclaim identifier FIXME maybe put this in a different namespace.
struct GNUNET_RECLAIM_Identifier credential_id
Credential ID.
size_t GNUNET_RECLAIM_attribute_list_serialize(const struct GNUNET_RECLAIM_AttributeList *al, char *result)
Serialize an attribute list.
static struct Plugin ** attr_plugins
Plugins.
struct GNUNET_RECLAIM_AttributeListEntry * next
DLL.
static struct GNUNET_RECLAIM_Identifier credential
Credential ID.
const char * GNUNET_RECLAIM_attribute_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string.
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
uint32_t name_len
Name length.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
const void * data
Binary value stored as attribute value.
char * GNUNET_RECLAIM_attribute_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;claim&#39; of an attribute to a string.
size_t GNUNET_RECLAIM_attribute_list_serialize_get_size(const struct GNUNET_RECLAIM_AttributeList *al)
Get required size for serialization buffer.
uint32_t type
Type of Claim.
struct Plugin * plugin
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
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.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
struct GNUNET_RECLAIM_Identifier attribute_id
Attribute ID.
void * cls
Closure for all of the callbacks.
uint32_t data
The data value.
uint32_t attribute_type
Attribute type.
struct GNUNET_RECLAIM_AttributeListEntry * list_head
List head.
#define GNUNET_malloc(size)
Wrapper around malloc.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
#define GNUNET_free(ptr)
Wrapper around free.
static int initialized
Init canary.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
void GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
Convert the utf-8 input string to lower case.
Definition: strings.c:444