GNUnet  0.10.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 {
39  char *library_name;
40 
45 };
46 
47 
51 static struct Plugin **attr_plugins;
52 
53 
57 static unsigned int num_plugins;
58 
59 
63 static int initialized;
64 
65 
73 static void
74 add_plugin(void *cls, const char *library_name, void *lib_ret)
75 {
77  struct Plugin *plugin;
78 
80  "Loading attribute plugin `%s'\n",
81  library_name);
82  plugin = GNUNET_new(struct Plugin);
83  plugin->api = api;
84  plugin->library_name = GNUNET_strdup(library_name);
85  GNUNET_array_append(attr_plugins, num_plugins, plugin);
86 }
87 
88 
92 static void
94 {
95  if (GNUNET_YES == initialized)
96  return;
98  GNUNET_PLUGIN_load_all("libgnunet_plugin_reclaim_attribute_",
99  NULL,
100  &add_plugin,
101  NULL);
102 }
103 
104 
111 uint32_t
113 {
114  unsigned int i;
115  struct Plugin *plugin;
116  uint32_t ret;
117 
118  init();
119  for (i = 0; i < num_plugins; i++)
120  {
121  plugin = attr_plugins[i];
122  if (UINT32_MAX !=
123  (ret = plugin->api->typename_to_number(plugin->api->cls, typename)))
124  return ret;
125  }
126  return UINT32_MAX;
127 }
128 
129 
136 const char *
138 {
139  unsigned int i;
140  struct Plugin *plugin;
141  const char *ret;
142 
143  init();
144  for (i = 0; i < num_plugins; i++)
145  {
146  plugin = attr_plugins[i];
147  if (NULL !=
148  (ret = plugin->api->number_to_typename(plugin->api->cls, type)))
149  return ret;
150  }
151  return NULL;
152 }
153 
154 
165 int
167  const char *s,
168  void **data,
169  size_t *data_size)
170 {
171  unsigned int i;
172  struct Plugin *plugin;
173 
174  init();
175  for (i = 0; i < num_plugins; i++)
176  {
177  plugin = attr_plugins[i];
178  if (GNUNET_OK == plugin->api->string_to_value(plugin->api->cls,
179  type,
180  s,
181  data,
182  data_size))
183  return GNUNET_OK;
184  }
185  return GNUNET_SYSERR;
186 }
187 
188 
197 char *
199  const void *data,
200  size_t data_size)
201 {
202  unsigned int i;
203  struct Plugin *plugin;
204  char *ret;
205 
206  init();
207  for (i = 0; i < num_plugins; i++)
208  {
209  plugin = attr_plugins[i];
210  if (NULL != (ret = plugin->api->value_to_string(plugin->api->cls,
211  type,
212  data,
213  data_size)))
214  return ret;
215  }
216  return NULL;
217 }
218 
219 
231  uint32_t type,
232  const void *data,
233  size_t data_size)
234 {
235  struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
236  char *write_ptr;
237  char *attr_name_tmp = GNUNET_strdup(attr_name);
238 
239  GNUNET_STRINGS_utf8_tolower(attr_name, attr_name_tmp);
240 
241  attr = GNUNET_malloc(sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim) +
242  strlen(attr_name_tmp) + 1 + data_size);
243  attr->type = type;
244  attr->data_size = data_size;
245  attr->version = 0;
246  write_ptr = (char *)&attr[1];
247  GNUNET_memcpy(write_ptr, attr_name_tmp, strlen(attr_name_tmp) + 1);
248  attr->name = write_ptr;
249  write_ptr += strlen(attr->name) + 1;
250  GNUNET_memcpy(write_ptr, data, data_size);
251  attr->data = write_ptr;
252  GNUNET_free(attr_name_tmp);
253  return attr;
254 }
255 
256 
265 void
267  struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list,
268  const char *attr_name,
269  uint32_t type,
270  const void *data,
271  size_t data_size)
272 {
274 
276  le->claim =
277  GNUNET_RECLAIM_ATTRIBUTE_claim_new(attr_name, type, data, data_size);
279  claim_list->list_tail,
280  le);
281 }
282 
283 
290 size_t
292  const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
293 {
295  size_t len = 0;
296 
297  for (le = attrs->list_head; NULL != le; le = le->next)
299  return len;
300 }
301 
302 
310 size_t
312  const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
313  char *result)
314 {
316  size_t len;
317  size_t total_len;
318  char *write_ptr;
319 
320  write_ptr = result;
321  total_len = 0;
322  for (le = attrs->list_head; NULL != le; le = le->next)
323  {
324  len = GNUNET_RECLAIM_ATTRIBUTE_serialize(le->claim, write_ptr);
325  total_len += len;
326  write_ptr += len;
327  }
328  return total_len;
329 }
330 
331 
341 {
344  size_t attr_len;
345  const char *read_ptr;
346 
347  if (data_size < sizeof(struct Attribute))
348  return NULL;
349 
351  read_ptr = data;
352  while (((data + data_size) - read_ptr) >= sizeof(struct Attribute))
353  {
355  le->claim =
357  data_size - (read_ptr - data));
359  "Deserialized attribute %s\n",
360  le->claim->name);
361  GNUNET_CONTAINER_DLL_insert(attrs->list_head, attrs->list_tail, le);
363  read_ptr += attr_len;
364  }
365  return attrs;
366 }
367 
368 
376  const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
377 {
379  struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le;
381 
383  for (le = attrs->list_head; NULL != le; le = le->next)
384  {
386  result_le->claim =
388  le->claim->type,
389  le->claim->data,
390  le->claim->data_size);
391  result_le->claim->version = le->claim->version;
392  result_le->claim->id = le->claim->id;
394  result->list_tail,
395  result_le);
396  }
397  return result;
398 }
399 
400 
406 void
409 {
412 
413  for (le = attrs->list_head; NULL != le;)
414  {
415  GNUNET_free(le->claim);
416  tmp_le = le;
417  le = le->next;
418  GNUNET_free(tmp_le);
419  }
420  GNUNET_free(attrs);
421 }
422 
423 
430 size_t
432  const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
433 {
434  return sizeof(struct Attribute) + strlen(attr->name) + attr->data_size;
435 }
436 
437 
445 size_t
447  const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
448  char *result)
449 {
450  size_t data_len_ser;
451  size_t name_len;
452  struct Attribute *attr_ser;
453  char *write_ptr;
454 
455  attr_ser = (struct Attribute *)result;
456  attr_ser->attribute_type = htons(attr->type);
457  attr_ser->attribute_version = htonl(attr->version);
458  attr_ser->attribute_id = GNUNET_htonll(attr->id);
459  name_len = strlen(attr->name);
460  attr_ser->name_len = htons(name_len);
461  write_ptr = (char *)&attr_ser[1];
462  GNUNET_memcpy(write_ptr, attr->name, name_len);
463  write_ptr += name_len;
464  // TODO plugin-ize
465  // data_len_ser = plugin->serialize_attribute_value (attr,
466  // &attr_ser[1]);
467  data_len_ser = attr->data_size;
468  GNUNET_memcpy(write_ptr, attr->data, attr->data_size);
469  attr_ser->data_size = htons(data_len_ser);
470 
471  return sizeof(struct Attribute) + strlen(attr->name) + attr->data_size;
472 }
473 
474 
485 {
486  struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
487  struct Attribute *attr_ser;
488  size_t data_len;
489  size_t name_len;
490  char *write_ptr;
491 
492  if (data_size < sizeof(struct Attribute))
493  return NULL;
494 
495  attr_ser = (struct Attribute *)data;
496  data_len = ntohs(attr_ser->data_size);
497  name_len = ntohs(attr_ser->name_len);
498  if (data_size < sizeof(struct Attribute) + data_len + name_len)
499  {
501  "Buffer too small to deserialize\n");
502  return NULL;
503  }
504  attr = GNUNET_malloc(sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim) +
505  data_len + name_len + 1);
506  attr->type = ntohs(attr_ser->attribute_type);
507  attr->version = ntohl(attr_ser->attribute_version);
508  attr->id = GNUNET_ntohll(attr_ser->attribute_id);
509  attr->data_size = data_len;
510 
511  write_ptr = (char *)&attr[1];
512  GNUNET_memcpy(write_ptr, &attr_ser[1], name_len);
513  write_ptr[name_len] = '\0';
514  attr->name = write_ptr;
515 
516  write_ptr += name_len + 1;
517  GNUNET_memcpy(write_ptr, (char *)&attr_ser[1] + name_len, attr->data_size);
518  attr->data = write_ptr;
519  return attr;
520 }
521 
522 /* 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.
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 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:41
Serialized claim.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:46
const void * data
Binary value stored as attribute value.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
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.
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:370
#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.
static size_t data_size
Number of bytes in data.
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:552