GNUnet  0.10.x
gnsrecord.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013 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 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_tun_lib.h"
34 
35 #define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__)
36 
37 
41 struct Plugin {
45  char *library_name;
46 
51 };
52 
53 
57 static struct Plugin **gns_plugins;
58 
62 static unsigned int num_plugins;
63 
67 static int once;
68 
69 
77 static void
78 add_plugin(void *cls,
79  const char *library_name,
80  void *lib_ret)
81 {
82  struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
83  struct Plugin *plugin;
84 
86  "Loading block plugin `%s'\n",
87  library_name);
88  plugin = GNUNET_new(struct Plugin);
89  plugin->api = api;
90  plugin->library_name = GNUNET_strdup(library_name);
91  GNUNET_array_append(gns_plugins, num_plugins, plugin);
92 }
93 
94 
98 static void
100 {
101  if (1 == once)
102  return;
103  once = 1;
104  GNUNET_PLUGIN_load_all("libgnunet_plugin_gnsrecord_", NULL,
105  &add_plugin, NULL);
106 }
107 
108 
112 void __attribute__ ((destructor))
113 GNSRECORD_fini()
114 {
115  struct Plugin *plugin;
116 
117  for (unsigned int i = 0; i < num_plugins; i++)
118  {
119  plugin = gns_plugins[i];
120  GNUNET_break(NULL ==
122  plugin->api));
123  GNUNET_free(plugin->library_name);
124  GNUNET_free(plugin);
125  }
126  GNUNET_free_non_null(gns_plugins);
127  gns_plugins = NULL;
128  once = 0;
129  num_plugins = 0;
130 }
131 
132 
141 char *
143  const void *data,
144  size_t data_size)
145 {
146  struct Plugin *plugin;
147  char *ret;
148 
149  init();
150  for (unsigned int i = 0; i < num_plugins; i++)
151  {
152  plugin = gns_plugins[i];
153  if (NULL != (ret = plugin->api->value_to_string(plugin->api->cls,
154  type,
155  data,
156  data_size)))
157  return ret;
158  }
159  return NULL;
160 }
161 
162 
173 int
175  const char *s,
176  void **data,
177  size_t *data_size)
178 {
179  struct Plugin *plugin;
180 
181  init();
182  for (unsigned int i = 0; i < num_plugins; i++)
183  {
184  plugin = gns_plugins[i];
185  if (GNUNET_OK == plugin->api->string_to_value(plugin->api->cls,
186  type,
187  s,
188  data,
189  data_size))
190  return GNUNET_OK;
191  }
192  return GNUNET_SYSERR;
193 }
194 
195 
202 uint32_t
203 GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
204 {
205  struct Plugin *plugin;
206  uint32_t ret;
207 
208  if (0 == strcasecmp(dns_typename,
209  "ANY"))
211  init();
212  for (unsigned int i = 0; i < num_plugins; i++)
213  {
214  plugin = gns_plugins[i];
215  if (UINT32_MAX != (ret = plugin->api->typename_to_number(plugin->api->cls,
216  dns_typename)))
217  return ret;
218  }
219  return UINT32_MAX;
220 }
221 
222 
229 const char *
231 {
232  struct Plugin *plugin;
233  const char * ret;
234 
235  if (GNUNET_GNSRECORD_TYPE_ANY == type)
236  return "ANY";
237  init();
238  for (unsigned int i = 0; i < num_plugins; i++)
239  {
240  plugin = gns_plugins[i];
241  if (NULL != (ret = plugin->api->number_to_typename(plugin->api->cls,
242  type)))
243  return ret;
244  }
245  return NULL;
246 }
247 
248 /* end of gnsrecord.c */
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (i.e.
Definition: gnsrecord.c:203
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/&#39;*&#39;.
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin to the list managed by the block library.
Definition: gnsrecord.c:78
static void init()
Loads all plugins (lazy initialization).
Definition: gnsrecord.c:99
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#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
static int ret
Final status code.
Definition: gnunet-arm.c:89
#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...
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:46
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:255
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
static int once
Global to mark if we&#39;ve run the initialization.
Definition: gnsrecord.c:67
static unsigned int num_plugins
Size of the &#39;plugins&#39; array.
Definition: gnsrecord.c:62
int GNUNET_GNSRECORD_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a &#39;value&#39; of a record to the binary representation.
Definition: gnsrecord.c:174
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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).
struct GNUNET_GNSRECORD_PluginFunctions * api
Plugin API.
Definition: gnsrecord.c:50
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
void __attribute__((destructor))
Dual function to init().
Definition: gnsrecord.c:112
static struct Plugin ** gns_plugins
Array of our plugins.
Definition: gnsrecord.c:57
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
void * cls
Closure for all of the callbacks.
uint32_t data
The data value.
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;value&#39; of a record to a string.
Definition: gnsrecord.c:142
static size_t data_size
Number of bytes in data.
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:230
#define GNUNET_free(ptr)
Wrapper around free.