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 
108 void __attribute__ ((destructor))
109 RECLAIM_ATTRIBUTE_fini ()
110 {
111  struct Plugin *plugin;
114 
115  if (pd != dpd)
116  GNUNET_OS_init (dpd);
117 
118  for (unsigned int i = 0; i < num_plugins; i++)
119  {
120  plugin = attr_plugins[i];
121  GNUNET_break (NULL ==
123  plugin->api));
124  GNUNET_free (plugin->library_name);
125  GNUNET_free (plugin);
126  }
127  GNUNET_free (attr_plugins);
128 
129  if (pd != dpd)
130  GNUNET_OS_init (pd);
131 
132  attr_plugins = NULL;
133 }
134 
135 
136 
143 uint32_t
145 {
146  unsigned int i;
147  struct Plugin *plugin;
148  uint32_t ret;
149 
150  init ();
151  for (i = 0; i < num_plugins; i++)
152  {
153  plugin = attr_plugins[i];
154  if (UINT32_MAX !=
155  (ret = plugin->api->typename_to_number (plugin->api->cls, typename)))
156  return ret;
157  }
158  return UINT32_MAX;
159 }
160 
161 
168 const char *
170 {
171  unsigned int i;
172  struct Plugin *plugin;
173  const char *ret;
174 
175  init ();
176  for (i = 0; i < num_plugins; i++)
177  {
178  plugin = attr_plugins[i];
179  if (NULL !=
180  (ret = plugin->api->number_to_typename (plugin->api->cls, type)))
181  return ret;
182  }
183  return NULL;
184 }
185 
186 
197 int
199  const char *s,
200  void **data,
201  size_t *data_size)
202 {
203  unsigned int i;
204  struct Plugin *plugin;
205 
206  init ();
207  for (i = 0; i < num_plugins; i++)
208  {
209  plugin = attr_plugins[i];
210  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
211  type,
212  s,
213  data,
214  data_size))
215  return GNUNET_OK;
216  }
217  return GNUNET_SYSERR;
218 }
219 
220 
229 char *
231  const void *data,
232  size_t data_size)
233 {
234  unsigned int i;
235  struct Plugin *plugin;
236  char *ret;
237 
238  init ();
239  for (i = 0; i < num_plugins; i++)
240  {
241  plugin = attr_plugins[i];
242  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
243  type,
244  data,
245  data_size)))
246  return ret;
247  }
248  return NULL;
249 }
250 
251 
264  const struct
266  uint32_t type,
267  const void *data,
268  size_t data_size)
269 {
270  struct GNUNET_RECLAIM_Attribute *attr;
271  char *write_ptr;
272  char *attr_name_tmp = GNUNET_strdup (attr_name);
273 
274  GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
275 
276  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
277  + strlen (attr_name_tmp) + 1 + data_size);
278  if (NULL != credential)
279  attr->credential = *credential;
280  attr->type = type;
281  attr->data_size = data_size;
282  attr->flag = 0;
283  write_ptr = (char *) &attr[1];
284  GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
285  attr->name = write_ptr;
286  write_ptr += strlen (attr->name) + 1;
287  GNUNET_memcpy (write_ptr, data, data_size);
288  attr->data = write_ptr;
289  GNUNET_free (attr_name_tmp);
290  return attr;
291 }
292 
293 
302 void
304  struct GNUNET_RECLAIM_AttributeList *al,
305  const char *attr_name,
307  uint32_t type,
308  const void *data,
309  size_t data_size)
310 {
312 
314  ale->attribute =
315  GNUNET_RECLAIM_attribute_new (attr_name, credential,
316  type, data, data_size);
318  al->list_tail,
319  ale);
320 }
321 
322 
329 size_t
331  const struct GNUNET_RECLAIM_AttributeList *al)
332 {
334  size_t len = 0;
335 
336  for (ale = al->list_head; NULL != ale; ale = ale->next)
337  {
338  GNUNET_assert (NULL != ale->attribute);
340  }
341  return len;
342 }
343 
344 
352 size_t
354  const struct GNUNET_RECLAIM_AttributeList *al,
355  char *result)
356 {
358  size_t len;
359  size_t total_len;
360  char *write_ptr;
361  write_ptr = result;
362  total_len = 0;
363  for (ale = al->list_head; NULL != ale; ale = ale->next)
364  {
365  GNUNET_assert (NULL != ale->attribute);
366  len = GNUNET_RECLAIM_attribute_serialize (ale->attribute, write_ptr);
367  total_len += len;
368  write_ptr += len;
369  }
370  return total_len;
371 }
372 
373 
383 {
384  struct GNUNET_RECLAIM_AttributeList *al;
386  size_t attr_len;
387  const char *read_ptr;
388  size_t left = data_size;
389 
391  if (data_size < sizeof(struct Attribute))
392  return al;
393  read_ptr = data;
394  while (left >= sizeof(struct Attribute))
395  {
397  attr_len =
399  left,
400  &ale->attribute);
401  if (-1 == attr_len)
402  {
404  "Failed to deserialize malformed attribute.\n");
405  GNUNET_free (ale);
406  return al;
407  }
408  left -= attr_len;
410  read_ptr += attr_len;
411  }
412  return al;
413 }
414 
415 
423  const struct GNUNET_RECLAIM_AttributeList *al)
424 {
426  struct GNUNET_RECLAIM_AttributeListEntry *result_ale;
428 
429  result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
430  for (ale = al->list_head; NULL != ale; ale = ale->next)
431  {
432  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
433  GNUNET_assert (NULL != ale->attribute);
434  {
435  result_ale->attribute =
437  &ale->attribute->credential,
438  ale->attribute->type,
439  ale->attribute->data,
440  ale->attribute->data_size);
441 
442  result_ale->attribute->id = ale->attribute->id;
443  result_ale->attribute->flag = ale->attribute->flag;
444  }
446  result->list_tail,
447  result_ale);
448  }
449  return result;
450 }
451 
452 
458 void
460  struct GNUNET_RECLAIM_AttributeList *al)
461 {
463  struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale;
464 
465  for (ale = al->list_head; NULL != ale;)
466  {
467  if (NULL != ale->attribute)
468  GNUNET_free (ale->attribute);
469  tmp_ale = ale;
470  ale = ale->next;
471  GNUNET_free (tmp_ale);
472  }
473  GNUNET_free (al);
474 }
475 
476 
483 size_t
485  const struct GNUNET_RECLAIM_Attribute *attr)
486 {
487  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
488 }
489 
490 
498 size_t
500  const struct GNUNET_RECLAIM_Attribute *attr,
501  char *result)
502 {
503  size_t data_len_ser;
504  size_t name_len;
505  struct Attribute *attr_ser;
506  char *write_ptr;
507 
508  attr_ser = (struct Attribute *) result;
509  attr_ser->attribute_type = htons (attr->type);
510  attr_ser->attribute_flag = htonl (attr->flag);
511  attr_ser->attribute_id = attr->id;
512  attr_ser->credential_id = attr->credential;
513  name_len = strlen (attr->name);
514  attr_ser->name_len = htons (name_len);
515  write_ptr = (char *) &attr_ser[1];
516  GNUNET_memcpy (write_ptr, attr->name, name_len);
517  write_ptr += name_len;
518  // TODO plugin-ize
519  // data_len_ser = plugin->serialize_attribute_value (attr,
520  // &attr_ser[1]);
521  data_len_ser = attr->data_size;
522  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
523  attr_ser->data_size = htons (data_len_ser);
524 
525  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
526 }
527 
528 
537 ssize_t
539  struct GNUNET_RECLAIM_Attribute **attr)
540 {
541  struct Attribute *attr_ser;
542  struct GNUNET_RECLAIM_Attribute *attribute;
543  size_t data_len;
544  size_t name_len;
545  char *write_ptr;
546 
547  if (data_size < sizeof(struct Attribute))
548  return -1;
549 
550  attr_ser = (struct Attribute *) data;
551  data_len = ntohs (attr_ser->data_size);
552  name_len = ntohs (attr_ser->name_len);
553  if (data_size < sizeof(struct Attribute) + data_len + name_len)
554  {
556  "Buffer too small to deserialize\n");
557  return -1;
558  }
559  attribute = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute)
560  + data_len + name_len + 1);
561  attribute->type = ntohs (attr_ser->attribute_type);
562  attribute->flag = ntohl (attr_ser->attribute_flag);
563  attribute->id = attr_ser->attribute_id;
564  attribute->credential = attr_ser->credential_id;
565  attribute->data_size = data_len;
566 
567  write_ptr = (char *) &attribute[1];
568  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
569  write_ptr[name_len] = '\0';
570  attribute->name = write_ptr;
571 
572  write_ptr += name_len + 1;
573  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len,
574  attribute->data_size);
575  *attr = attribute;
576  attribute->data = write_ptr;
577  return sizeof(struct Attribute) + data_len + name_len;
578 }
579 
580 
581 /* 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.
#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.
#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_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
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.
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:397
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.
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:568