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"
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 
232  uint32_t type,
233  const void *data,
234  size_t data_size)
235 {
236  struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
237  char *write_ptr;
238  char *attr_name_tmp = GNUNET_strdup (attr_name);
239 
240  GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
241 
242  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
243  + strlen (attr_name_tmp) + 1 + data_size);
244  attr->type = type;
245  attr->data_size = data_size;
246  attr->version = 0;
247  write_ptr = (char *) &attr[1];
248  GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
249  attr->name = write_ptr;
250  write_ptr += strlen (attr->name) + 1;
251  GNUNET_memcpy (write_ptr, data, data_size);
252  attr->data = write_ptr;
253  GNUNET_free (attr_name_tmp);
254  return attr;
255 }
256 
257 
266 void
268  struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list,
269  const char *attr_name,
270  uint32_t type,
271  const void *data,
272  size_t data_size)
273 {
275 
277  le->claim =
278  GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size);
280  claim_list->list_tail,
281  le);
282 }
283 
284 
291 size_t
293  const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
294 {
296  size_t len = 0;
297 
298  for (le = attrs->list_head; NULL != le; le = le->next)
300  return len;
301 }
302 
303 
311 size_t
313  const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
314  char *result)
315 {
317  size_t len;
318  size_t total_len;
319  char *write_ptr;
320 
321  write_ptr = result;
322  total_len = 0;
323  for (le = attrs->list_head; NULL != le; le = le->next)
324  {
325  len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr);
326  total_len += len;
327  write_ptr += len;
328  }
329  return total_len;
330 }
331 
332 
342 {
345  size_t attr_len;
346  const char *read_ptr;
347 
348  if (data_size < sizeof(struct Attribute))
349  return NULL;
350 
352  read_ptr = data;
353  while (((data + data_size) - read_ptr) >= sizeof(struct Attribute))
354  {
356  le->claim =
358  data_size - (read_ptr - data));
360  "Deserialized attribute %s\n",
361  le->claim->name);
362  GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
364  read_ptr += attr_len;
365  }
366  return attrs;
367 }
368 
369 
377  const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
378 {
380  struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le;
382 
384  for (le = attrs->list_head; NULL != le; le = le->next)
385  {
387  result_le->claim =
389  le->claim->type,
390  le->claim->data,
391  le->claim->data_size);
392  result_le->claim->version = le->claim->version;
393  result_le->claim->id = le->claim->id;
395  result->list_tail,
396  result_le);
397  }
398  return result;
399 }
400 
401 
407 void
410 {
413 
414  for (le = attrs->list_head; NULL != le;)
415  {
416  GNUNET_free (le->claim);
417  tmp_le = le;
418  le = le->next;
419  GNUNET_free (tmp_le);
420  }
421  GNUNET_free (attrs);
422 }
423 
424 
431 size_t
433  const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
434 {
435  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
436 }
437 
438 
446 size_t
448  const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
449  char *result)
450 {
451  size_t data_len_ser;
452  size_t name_len;
453  struct Attribute *attr_ser;
454  char *write_ptr;
455 
456  attr_ser = (struct Attribute *) result;
457  attr_ser->attribute_type = htons (attr->type);
458  attr_ser->attribute_version = htonl (attr->version);
459  attr_ser->attribute_id = GNUNET_htonll (attr->id);
460  name_len = strlen (attr->name);
461  attr_ser->name_len = htons (name_len);
462  write_ptr = (char *) &attr_ser[1];
463  GNUNET_memcpy (write_ptr, attr->name, name_len);
464  write_ptr += name_len;
465  // TODO plugin-ize
466  // data_len_ser = plugin->serialize_attribute_value (attr,
467  // &attr_ser[1]);
468  data_len_ser = attr->data_size;
469  GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
470  attr_ser->data_size = htons (data_len_ser);
471 
472  return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
473 }
474 
475 
486 {
487  struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
488  struct Attribute *attr_ser;
489  size_t data_len;
490  size_t name_len;
491  char *write_ptr;
492 
493  if (data_size < sizeof(struct Attribute))
494  return NULL;
495 
496  attr_ser = (struct Attribute *) data;
497  data_len = ntohs (attr_ser->data_size);
498  name_len = ntohs (attr_ser->name_len);
499  if (data_size < sizeof(struct Attribute) + data_len + name_len)
500  {
502  "Buffer too small to deserialize\n");
503  return NULL;
504  }
505  attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
506  + data_len + name_len + 1);
507  attr->type = ntohs (attr_ser->attribute_type);
508  attr->version = ntohl (attr_ser->attribute_version);
509  attr->id = GNUNET_ntohll (attr_ser->attribute_id);
510  attr->data_size = data_len;
511 
512  write_ptr = (char *) &attr[1];
513  GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
514  write_ptr[name_len] = '\0';
515  attr->name = write_ptr;
516 
517  write_ptr += name_len + 1;
518  GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size);
519  attr->data = write_ptr;
520  return attr;
521 }
522 
523 
524 /* end of reclaim_attribute.c */
const char * name
The name of the attribute.
struct GNUNET_RECLAIM_ATTRIBUTE_Claim * claim
The attribute claim.
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.
static unsigned int num_plugins
Number of plugins.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
size_t data_size
Number of bytes in data.
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry * list_tail
List tail.
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t GNUNET_RECLAIM_ATTRIBUTE_typename_to_number(const char *typename)
Convert a type name to the corresponding number.
#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.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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.
#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.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
const void * data
Binary value stored as attribute value.
void GNUNET_RECLAIM_ATTRIBUTE_list_destroy(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
Destroy claim list.
GNUnet reclaim identity attributes.
const char * GNUNET_RECLAIM_ATTRIBUTE_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string.
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
static char * attr_name
The attribute.
struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions * api
Plugin API.
uint32_t data_size
Data size.
static int result
Global testing status.
static char * plugin
Solver plugin name as string.
uint32_t attribute_version
Attribute version.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct Plugin ** attr_plugins
Plugins.
uint64_t attribute_id
Attribute ID.
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
#define GNUNET_array_append(arr, size, element)
Append an element to a list (growing the list by one).
size_t GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size(const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
Get required size for serialization buffer.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_RECLAIM_ATTRIBUTE_Claim * GNUNET_RECLAIM_ATTRIBUTE_claim_new(const char *attr_name, uint32_t type, const void *data, size_t data_size)
Create a new attribute.
A list of GNUNET_RECLAIM_ATTRIBUTE_Claim structures.
#define GNUNET_log(kind,...)
uint32_t name_len
Name length.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
size_t GNUNET_RECLAIM_ATTRIBUTE_serialize(const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr, char *result)
Serialize an attribute.
#define GNUNET_YES
Definition: gnunet_common.h:77
void GNUNET_RECLAIM_ATTRIBUTE_list_add(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list, const char *attr_name, uint32_t type, const void *data, size_t data_size)
Add a new attribute to a claim list.
void * cls
Closure for all of the callbacks.
uint32_t data
The data value.
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry * next
DLL.
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * GNUNET_RECLAIM_ATTRIBUTE_list_dup(const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
Make a (deep) copy of a claim list.
uint32_t attribute_type
Attribute type.
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry * list_head
List head.
size_t GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size(const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
Get required size for serialization buffer.
struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * GNUNET_RECLAIM_ATTRIBUTE_list_deserialize(const char *data, size_t data_size)
Deserialize an attribute list.
size_t GNUNET_RECLAIM_ATTRIBUTE_list_serialize(const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, char *result)
Serialize an attribute list.
struct GNUNET_RECLAIM_ATTRIBUTE_Claim * GNUNET_RECLAIM_ATTRIBUTE_deserialize(const char *data, size_t data_size)
Deserialize an attribute.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:48
#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:553