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;
99  GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_",
100  NULL,
101  &add_plugin,
102  NULL);
103 }
104 
105 
112 uint32_t
114 {
115  unsigned int i;
116  struct Plugin *plugin;
117  uint32_t ret;
118 
119  init ();
120  for (i = 0; i < num_plugins; i++)
121  {
122  plugin = attr_plugins[i];
123  if (UINT32_MAX !=
124  (ret = plugin->api->typename_to_number (plugin->api->cls, typename)))
125  return ret;
126  }
127  return UINT32_MAX;
128 }
129 
130 
137 const char *
139 {
140  unsigned int i;
141  struct Plugin *plugin;
142  const char *ret;
143 
144  init ();
145  for (i = 0; i < num_plugins; i++)
146  {
147  plugin = attr_plugins[i];
148  if (NULL !=
149  (ret = plugin->api->number_to_typename (plugin->api->cls, type)))
150  return ret;
151  }
152  return NULL;
153 }
154 
155 
166 int
168  const char *s,
169  void **data,
170  size_t *data_size)
171 {
172  unsigned int i;
173  struct Plugin *plugin;
174 
175  init ();
176  for (i = 0; i < num_plugins; i++)
177  {
178  plugin = attr_plugins[i];
179  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
180  type,
181  s,
182  data,
183  data_size))
184  return GNUNET_OK;
185  }
186  return GNUNET_SYSERR;
187 }
188 
189 
198 char *
200  const void *data,
201  size_t data_size)
202 {
203  unsigned int i;
204  struct Plugin *plugin;
205  char *ret;
206 
207  init ();
208  for (i = 0; i < num_plugins; i++)
209  {
210  plugin = attr_plugins[i];
211  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
212  type,
213  data,
214  data_size)))
215  return ret;
216  }
217  return NULL;
218 }
219 
220 
233  const struct
235  uint32_t type,
236  const void *data,
237  size_t data_size)
238 {
239  struct GNUNET_RECLAIM_Attribute *attr;
240  char *write_ptr;
241  char *attr_name_tmp = GNUNET_strdup (attr_name);
242 
243  GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
244 
245  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
246  + strlen (attr_name_tmp) + 1 + data_size);
247  if (NULL != credential)
248  attr->credential = *credential;
249  attr->type = type;
250  attr->data_size = data_size;
251  attr->flag = 0;
252  write_ptr = (char *) &attr[1];
253  GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
254  attr->name = write_ptr;
255  write_ptr += strlen (attr->name) + 1;
256  GNUNET_memcpy (write_ptr, data, data_size);
257  attr->data = write_ptr;
258  GNUNET_free (attr_name_tmp);
259  return attr;
260 }
261 
262 
271 void
273  struct GNUNET_RECLAIM_AttributeList *al,
274  const char *attr_name,
276  uint32_t type,
277  const void *data,
278  size_t data_size)
279 {
281 
283  ale->attribute =
284  GNUNET_RECLAIM_attribute_new (attr_name, credential,
285  type, data, data_size);
287  al->list_tail,
288  ale);
289 }
290 
291 
298 size_t
300  const struct GNUNET_RECLAIM_AttributeList *al)
301 {
303  size_t len = 0;
304 
305  for (ale = al->list_head; NULL != ale; ale = ale->next)
306  {
307  GNUNET_assert (NULL != ale->attribute);
309  }
310  return len;
311 }
312 
313 
321 size_t
323  const struct GNUNET_RECLAIM_AttributeList *al,
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 = al->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 {
353  struct GNUNET_RECLAIM_AttributeList *al;
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 
392  const struct GNUNET_RECLAIM_AttributeList *al)
393 {
395  struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
397 
398  result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
399  for (ale = al->list_head; NULL != ale; ale = ale->next)
400  {
401  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
402  GNUNET_assert (NULL != ale->attribute);
403  {
404  result_ale->attribute =
406  &ale->attribute->credential,
407  ale->attribute->type,
408  ale->attribute->data,
409  ale->attribute->data_size);
410 
411  result_ale->attribute->id = ale->attribute->id;
412  result_ale->attribute->flag = ale->attribute->flag;
413  }
415  result->list_tail,
416  result_ale);
417  }
418  return result;
419 }
420 
421 
427 void
429  struct GNUNET_RECLAIM_AttributeList *al)
430 {
432  struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
433 
434  for (ale = al->list_head; NULL != ale;)
435  {
436  if (NULL != ale->attribute)
437  GNUNET_free (ale->attribute);
438  tmp_ale = ale;
439  ale = ale->next;
440  GNUNET_free (tmp_ale);
441  }
442  GNUNET_free (al);
443 }
444 
445 
452 size_t
454  const struct GNUNET_RECLAIM_Attribute *attr)
455 {
456  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
457 }
458 
459 
467 size_t
469  const struct GNUNET_RECLAIM_Attribute *attr,
470  char *result)
471 {
472  size_t data_len_ser;
473  size_t name_len;
474  struct Attribute *attr_ser;
475  char *write_ptr;
476 
477  attr_ser = (struct Attribute *) result;
478  attr_ser->attribute_type = htons (attr->type);
479  attr_ser->attribute_flag = htonl (attr->flag);
480  attr_ser->attribute_id = attr->id;
481  attr_ser->credential_id = attr->credential;
482  name_len = strlen (attr->name);
483  attr_ser->name_len = htons (name_len);
484  write_ptr = (char *) &attr_ser[1];
485  GNUNET_memcpy (write_ptr, attr->name, name_len);
486  write_ptr += name_len;
487  // TODO plugin-ize
488  // data_len_ser = plugin->serialize_attribute_value (attr,
489  // &attr_ser[1]);
490  data_len_ser = attr->data_size;
491  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
492  attr_ser->data_size = htons (data_len_ser);
493 
494  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
495 }
496 
497 
506 ssize_t
508  struct GNUNET_RECLAIM_Attribute **attr)
509 {
510  struct Attribute *attr_ser;
511  struct GNUNET_RECLAIM_Attribute *attribute;
512  size_t data_len;
513  size_t name_len;
514  char *write_ptr;
515 
516  if (data_size < sizeof(struct Attribute))
517  return -1;
518 
519  attr_ser = (struct Attribute *) data;
520  data_len = ntohs (attr_ser->data_size);
521  name_len = ntohs (attr_ser->name_len);
522  if (data_size < sizeof(struct Attribute) + data_len + name_len)
523  {
525  "Buffer too small to deserialize\n");
526  return -1;
527  }
528  attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
529  + data_len + name_len + 1);
530  attribute->type = ntohs (attr_ser->attribute_type);
531  attribute->flag = ntohl (attr_ser->attribute_flag);
532  attribute->id = attr_ser->attribute_id;
533  attribute->credential = attr_ser->credential_id;
534  attribute->data_size = data_len;
535 
536  write_ptr = (char *) &attribute[1];
537  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
538  write_ptr[name_len] = '\0';
539  attribute->name = write_ptr;
540 
541  write_ptr += name_len + 1;
542  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len,
543  attribute->data_size);
544  *attr = attribute;
545  attribute->data = write_ptr;
546  return sizeof(struct Attribute) + data_len + name_len;
547 }
548 
549 
550 /* 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.
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.
#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.
#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.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
size_t GNUNET_RECLAIM_attribute_serialize_get_size(const struct GNUNET_RECLAIM_Attribute *attr)
Get required size for serialization buffer.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
struct GNUNET_RECLAIM_Attribute * attribute
The attribute claim.
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.
static char * plugin
Solver plugin name as string.
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.
void GNUNET_PLUGIN_load_all(const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name.
Definition: plugin.c:372
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 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.
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:568