GNUnet  0.11.x
reclaim_attestation.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_attestation.h"
30 
31 
35 struct Plugin
36 {
40  char *library_name;
41 
46 };
47 
48 
52 static struct Plugin **attest_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 attestation 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 (attest_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_attestation_",
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  init ();
119  for (i = 0; i < num_plugins; i++)
120  {
121  plugin = attest_plugins[i];
122  if (UINT32_MAX !=
123  (ret = plugin->api->typename_to_number (plugin->api->cls,
124  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 = attest_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 = attest_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 = attest_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_Attestation *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_Attestation)
243  + strlen (attr_name_tmp) + 1 + data_size);
244  attr->type = type;
245  attr->data_size = data_size;
246  attr->flag = 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 
264 size_t
266  const struct GNUNET_RECLAIM_AttestationList *attestations)
267 {
269  size_t len = 0;
270 
271  for (le = attestations->list_head; NULL != le; le = le->next)
272  {
273  GNUNET_assert (NULL != le->attestation);
275  len += sizeof(struct GNUNET_RECLAIM_AttestationListEntry);
276  }
277  return len;
278 }
279 
280 
288 size_t
290  const struct GNUNET_RECLAIM_AttestationList *attestations,
291  char *result)
292 {
294  size_t len;
295  size_t total_len;
296  char *write_ptr;
297  write_ptr = result;
298  total_len = 0;
299  for (le = attestations->list_head; NULL != le; le = le->next)
300  {
301  GNUNET_assert (NULL != le->attestation);
302  len = GNUNET_RECLAIM_attestation_serialize (le->attestation, write_ptr);
303  total_len += len;
304  write_ptr += len;
305  }
306  return total_len;
307 }
308 
309 
319 {
322  size_t att_len;
323  const char *read_ptr;
324 
326 
327  if ((data_size < sizeof(struct
328  Attestation)
329  + sizeof(struct GNUNET_RECLAIM_AttestationListEntry)))
330  return al;
331 
332  read_ptr = data;
333  while (((data + data_size) - read_ptr) >= sizeof(struct Attestation))
334  {
336  ale->attestation =
338  data_size - (read_ptr - data));
341  read_ptr += att_len;
342  }
343  return al;
344 }
345 
346 
354  const struct GNUNET_RECLAIM_AttestationList *al)
355 {
357  struct GNUNET_RECLAIM_AttestationListEntry *result_ale;
359 
360  result = GNUNET_new (struct GNUNET_RECLAIM_AttestationList);
361  for (ale = al->list_head; NULL != ale; ale = ale->next)
362  {
363  result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttestationListEntry);
364  GNUNET_assert (NULL != ale->attestation);
365  result_ale->attestation =
367  ale->attestation->type,
368  ale->attestation->data,
369  ale->attestation->data_size);
370  result_ale->attestation->id = ale->attestation->id;
372  result->list_tail,
373  result_ale);
374  }
375  return result;
376 }
377 
378 
384 void
387 {
389  struct GNUNET_RECLAIM_AttestationListEntry *tmp_ale;
390 
391  for (ale = al->list_head; NULL != ale;)
392  {
393  if (NULL != ale->attestation)
394  GNUNET_free (ale->attestation);
395  tmp_ale = ale;
396  ale = ale->next;
397  GNUNET_free (tmp_ale);
398  }
399  GNUNET_free (al);
400 }
401 
402 
409 size_t
412 {
413  return sizeof(struct Attestation) + strlen (attestation->name)
414  + attestation->data_size;
415 }
416 
417 
425 size_t
428  char *result)
429 {
430  size_t data_len_ser;
431  size_t name_len;
432  struct Attestation *atts;
433  char *write_ptr;
434 
435  atts = (struct Attestation *) result;
436  atts->attestation_type = htons (attestation->type);
437  atts->attestation_flag = htonl (attestation->flag);
438  atts->attestation_id = attestation->id;
439  name_len = strlen (attestation->name);
440  atts->name_len = htons (name_len);
441  write_ptr = (char *) &atts[1];
442  GNUNET_memcpy (write_ptr, attestation->name, name_len);
443  write_ptr += name_len;
444  // TODO plugin-ize
445  // data_len_ser = plugin->serialize_attribute_value (attr,
446  // &attr_ser[1]);
447  data_len_ser = attestation->data_size;
448  GNUNET_memcpy (write_ptr, attestation->data, attestation->data_size);
449  atts->data_size = htons (data_len_ser);
450 
451  return sizeof(struct Attestation) + strlen (attestation->name)
452  + attestation->data_size;
453 }
454 
455 
466 {
468  struct Attestation *atts;
469  size_t data_len;
470  size_t name_len;
471  char *write_ptr;
472 
473  if (data_size < sizeof(struct Attestation))
474  return NULL;
475 
476  atts = (struct Attestation *) data;
477  data_len = ntohs (atts->data_size);
478  name_len = ntohs (atts->name_len);
479  if (data_size < sizeof(struct Attestation) + data_len + name_len)
480  {
482  "Buffer too small to deserialize\n");
483  return NULL;
484  }
485  attestation = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attestation)
486  + data_len + name_len + 1);
487  attestation->type = ntohs (atts->attestation_type);
488  attestation->flag = ntohl (atts->attestation_flag);
489  attestation->id = atts->attestation_id;
490  attestation->data_size = data_len;
491 
492  write_ptr = (char *) &attestation[1];
493  GNUNET_memcpy (write_ptr, &atts[1], name_len);
494  write_ptr[name_len] = '\0';
495  attestation->name = write_ptr;
496 
497  write_ptr += name_len + 1;
498  GNUNET_memcpy (write_ptr, (char *) &atts[1] + name_len,
499  attestation->data_size);
500  attestation->data = write_ptr;
501  return attestation;
502 }
503 
504 
508 {
509  unsigned int i;
510  struct Plugin *plugin;
512  init ();
513  for (i = 0; i < num_plugins; i++)
514  {
515  plugin = attest_plugins[i];
516  if (NULL !=
517  (ret = plugin->api->get_attributes (plugin->api->cls,
518  attest)))
519  return ret;
520  }
521  return NULL;
522 }
523 
524 
525 char*
528 {
529  unsigned int i;
530  struct Plugin *plugin;
531  char *ret;
532  init ();
533  for (i = 0; i < num_plugins; i++)
534  {
535  plugin = attest_plugins[i];
536  if (NULL !=
537  (ret = plugin->api->get_issuer (plugin->api->cls,
538  attest)))
539  return ret;
540  }
541  return NULL;
542 }
543 
544 
545 int
548  struct GNUNET_TIME_Absolute* exp)
549 {
550  unsigned int i;
551  struct Plugin *plugin;
552  init ();
553  for (i = 0; i < num_plugins; i++)
554  {
555  plugin = attest_plugins[i];
556  if (GNUNET_OK != plugin->api->get_expiration (plugin->api->cls,
557  attest,
558  exp))
559  continue;
560  return GNUNET_OK;
561  }
562  return GNUNET_SYSERR;
563 }
size_t GNUNET_RECLAIM_attestation_list_serialize(const struct GNUNET_RECLAIM_AttestationList *attestations, char *result)
Serialize an attribute list.
static struct Plugin ** attest_plugins
Plugins.
struct GNUNET_RECLAIM_AttributeList * GNUNET_RECLAIM_attestation_get_attributes(const struct GNUNET_RECLAIM_Attestation *attest)
Convert an attestation type name to the corresponding number.
struct GNUNET_RECLAIM_AttestationList * GNUNET_RECLAIM_attestation_list_dup(const struct GNUNET_RECLAIM_AttestationList *al)
Make a (deep) copy of the attestation list.
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
size_t GNUNET_RECLAIM_attestation_serialize(const struct GNUNET_RECLAIM_Attestation *attestation, char *result)
Serialize an attestation.
void GNUNET_RECLAIM_attestation_list_destroy(struct GNUNET_RECLAIM_AttestationList *al)
Destroy attestation list.
uint32_t attestation_type
Attestation type.
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.
static int initialized
Init canary.
uint32_t name_len
Name length.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct GNUNET_RECLAIM_Attestation * attestation
The attestation.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
A list of GNUNET_RECLAIM_Attribute structures.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
A list of GNUNET_RECLAIM_Attestation 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
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
uint32_t GNUNET_RECLAIM_attestation_typename_to_number(const char *typename)
Convert an attestation type name to the corresponding number.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
char * GNUNET_RECLAIM_attestation_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;claim&#39; of an attestation to a string.
static void init()
Load plugins.
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.
int GNUNET_RECLAIM_attestation_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 attestation to the binary representation.
const char * GNUNET_RECLAIM_attestation_number_to_typename(uint32_t type)
Convert an attestation type number to the corresponding attestation type string.
static int result
Global testing status.
int GNUNET_RECLAIM_attestation_get_expiration(const struct GNUNET_RECLAIM_Attestation *attest, struct GNUNET_TIME_Absolute *exp)
struct GNUNET_RECLAIM_AttestationPluginFunctions * api
Plugin API.
static unsigned int num_plugins
Number of plugins.
size_t data_size
Number of bytes in data.
struct GNUNET_RECLAIM_Attestation * GNUNET_RECLAIM_attestation_deserialize(const char *data, size_t data_size)
Deserialize an attestation.
struct GNUNET_RECLAIM_Attestation * GNUNET_RECLAIM_attestation_new(const char *attr_name, uint32_t type, const void *data, size_t data_size)
Create a new attestation.
size_t GNUNET_RECLAIM_attestation_serialize_get_size(const struct GNUNET_RECLAIM_Attestation *attestation)
Get required size for serialization buffer.
static char * plugin
Solver plugin name as string.
const char * name
The name of the attribute.
struct GNUNET_RECLAIM_Identifier id
ID.
uint32_t type
Type/Format of Claim.
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
struct GNUNET_RECLAIM_AttestationListEntry * list_head
List head.
uint32_t attestation_flag
Attestation flag.
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
const void * data
Binary value stored as attribute value.
#define GNUNET_array_append(arr, size, element)
Append an element to a list (growing the list by one).
static struct GNUNET_RECLAIM_Identifier attestation
Attestation ID.
struct GNUNET_RECLAIM_AttestationListEntry * list_tail
List tail.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_RECLAIM_Identifier attestation_id
Attestation ID.
#define GNUNET_log(kind,...)
char * GNUNET_RECLAIM_attestation_get_issuer(const struct GNUNET_RECLAIM_Attestation *attest)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:85
struct GNUNET_RECLAIM_AttestationList * GNUNET_RECLAIM_attestation_list_deserialize(const char *data, size_t data_size)
Deserialize an attestation list.
Serialized attestation claim.
struct GNUNET_RECLAIM_AttestationListEntry * next
DLL.
void * cls
Closure for all of the callbacks.
uint32_t data_size
Data size.
uint32_t data
The data value.
size_t GNUNET_RECLAIM_attestation_list_serialize_get_size(const struct GNUNET_RECLAIM_AttestationList *attestations)
Get required size for serialization buffer.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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