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 
234  uint32_t type,
235  const void *data,
236  size_t data_size)
237 {
238  struct GNUNET_RECLAIM_Attribute *attr;
239  char *write_ptr;
240  char *attr_name_tmp = GNUNET_strdup (attr_name);
241 
242  GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
243 
244  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
245  + strlen (attr_name_tmp) + 1 + data_size);
246  if (NULL != attestation)
247  attr->attestation = *attestation;
248  attr->type = type;
249  attr->data_size = data_size;
250  attr->flag = 0;
251  write_ptr = (char *) &attr[1];
252  GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
253  attr->name = write_ptr;
254  write_ptr += strlen (attr->name) + 1;
255  GNUNET_memcpy (write_ptr, data, data_size);
256  attr->data = write_ptr;
257  GNUNET_free (attr_name_tmp);
258  return attr;
259 }
260 
261 
270 void
272  struct GNUNET_RECLAIM_AttributeList *al,
273  const char *attr_name,
275  uint32_t type,
276  const void *data,
277  size_t data_size)
278 {
280 
282  ale->attribute =
283  GNUNET_RECLAIM_attribute_new (attr_name, attestation,
284  type, data, data_size);
286  al->list_tail,
287  ale);
288 }
289 
290 
297 size_t
299  const struct GNUNET_RECLAIM_AttributeList *al)
300 {
302  size_t len = 0;
303 
304  for (ale = al->list_head; NULL != ale; ale = ale->next)
305  {
306  GNUNET_assert (NULL != ale->attribute);
308  len += sizeof(struct GNUNET_RECLAIM_AttributeListEntry);
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 
359  if (data_size < sizeof(struct Attribute) + sizeof(struct
361  return al;
362  read_ptr = data;
363  while (((data + data_size) - read_ptr) >= sizeof(struct Attribute))
364  {
366  ale->attribute =
368  data_size - (read_ptr - data));
369  if (NULL == ale->attribute)
370  {
372  "Failed to deserialize malformed attribute.\n");
373  GNUNET_free (ale);
374  return al;
375  }
378  read_ptr += attr_len;
379  }
380  return al;
381 }
382 
383 
391  const struct GNUNET_RECLAIM_AttributeList *al)
392 {
394  struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
396 
397  result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
398  for (ale = al->list_head; NULL != ale; ale = ale->next)
399  {
400  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
401  GNUNET_assert (NULL != ale->attribute);
402  {
403  result_ale->attribute =
405  &ale->attribute->attestation,
406  ale->attribute->type,
407  ale->attribute->data,
408  ale->attribute->data_size);
409 
410  result_ale->attribute->id = ale->attribute->id;
411  result_ale->attribute->flag = ale->attribute->flag;
412  }
414  result->list_tail,
415  result_ale);
416  }
417  return result;
418 }
419 
420 
426 void
428  struct GNUNET_RECLAIM_AttributeList *al)
429 {
431  struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
432 
433  for (ale = al->list_head; NULL != ale;)
434  {
435  if (NULL != ale->attribute)
436  GNUNET_free (ale->attribute);
437  tmp_ale = ale;
438  ale = ale->next;
439  GNUNET_free (tmp_ale);
440  }
441  GNUNET_free (al);
442 }
443 
444 
451 size_t
453  const struct GNUNET_RECLAIM_Attribute *attr)
454 {
455  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
456 }
457 
458 
466 size_t
468  const struct GNUNET_RECLAIM_Attribute *attr,
469  char *result)
470 {
471  size_t data_len_ser;
472  size_t name_len;
473  struct Attribute *attr_ser;
474  char *write_ptr;
475 
476  attr_ser = (struct Attribute *) result;
477  attr_ser->attribute_type = htons (attr->type);
478  attr_ser->attribute_flag = htonl (attr->flag);
479  attr_ser->attribute_id = attr->id;
480  attr_ser->attestation_id = attr->attestation;
481  name_len = strlen (attr->name);
482  attr_ser->name_len = htons (name_len);
483  write_ptr = (char *) &attr_ser[1];
484  GNUNET_memcpy (write_ptr, attr->name, name_len);
485  write_ptr += name_len;
486  // TODO plugin-ize
487  // data_len_ser = plugin->serialize_attribute_value (attr,
488  // &attr_ser[1]);
489  data_len_ser = attr->data_size;
490  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
491  attr_ser->data_size = htons (data_len_ser);
492 
493  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
494 }
495 
496 
507 {
508  struct GNUNET_RECLAIM_Attribute *attr;
509  struct Attribute *attr_ser;
510  size_t data_len;
511  size_t name_len;
512  char *write_ptr;
513 
514  if (data_size < sizeof(struct Attribute))
515  return NULL;
516 
517  attr_ser = (struct Attribute *) data;
518  data_len = ntohs (attr_ser->data_size);
519  name_len = ntohs (attr_ser->name_len);
520  if (data_size < sizeof(struct Attribute) + data_len + name_len)
521  {
523  "Buffer too small to deserialize\n");
524  return NULL;
525  }
526  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
527  + data_len + name_len + 1);
528  attr->type = ntohs (attr_ser->attribute_type);
529  attr->flag = ntohl (attr_ser->attribute_flag);
530  attr->id = attr_ser->attribute_id;
531  attr->attestation = attr_ser->attestation_id;
532  attr->data_size = data_len;
533 
534  write_ptr = (char *) &attr[1];
535  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
536  write_ptr[name_len] = '\0';
537  attr->name = write_ptr;
538 
539  write_ptr += name_len + 1;
540  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size);
541  attr->data = write_ptr;
542  return attr;
543 }
544 
545 
546 /* end of reclaim_attribute.c */
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.
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_.
void GNUNET_RECLAIM_attribute_list_add(struct GNUNET_RECLAIM_AttributeList *al, const char *attr_name, const struct GNUNET_RECLAIM_Identifier *attestation, uint32_t type, const void *data, size_t data_size)
Add a new attribute to a claim list.
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.
struct GNUNET_RECLAIM_Identifier attestation
Referenced ID of Attestation (may be 0 if self-attested)
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_Attribute * GNUNET_RECLAIM_attribute_deserialize(const char *data, size_t data_size)
Deserialize an 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.
struct GNUNET_RECLAIM_Identifier attestation_id
Attestation ID.
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.
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.
struct GNUNET_RECLAIM_Attribute * GNUNET_RECLAIM_attribute_new(const char *attr_name, const struct GNUNET_RECLAIM_Identifier *attestation, uint32_t type, const void *data, size_t data_size)
Create a new attribute.
static struct GNUNET_RECLAIM_Identifier attestation
Attestation 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.
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:567