GNUnet  0.11.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
42 {
46  char *library_name;
47 
52 };
53 
54 
58 static struct Plugin **gns_plugins;
59 
63 static unsigned int num_plugins;
64 
68 static int once;
69 
70 
78 static void
79 add_plugin (void *cls,
80  const char *library_name,
81  void *lib_ret)
82 {
83  struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
84  struct Plugin *plugin;
85 
87  "Loading block plugin `%s'\n",
88  library_name);
89  plugin = GNUNET_new (struct Plugin);
90  plugin->api = api;
91  plugin->library_name = GNUNET_strdup (library_name);
92  GNUNET_array_append (gns_plugins, num_plugins, plugin);
93 }
94 
95 
99 static void
101 {
102  if (1 == once)
103  return;
104  once = 1;
105 
107  "libgnunet_plugin_gnsrecord_",
108  NULL,
109  &add_plugin,
110  NULL);
111 }
112 
113 
117 void __attribute__ ((destructor))
118 GNSRECORD_fini ()
119 {
120  struct Plugin *plugin;
123 
124  if (pd != dpd)
125  GNUNET_OS_init (dpd);
126 
127  for (unsigned int i = 0; i < num_plugins; i++)
128  {
129  plugin = gns_plugins[i];
130  GNUNET_break (NULL ==
132  plugin->api));
133  GNUNET_free (plugin->library_name);
134  GNUNET_free (plugin);
135  }
136  GNUNET_free (gns_plugins);
137 
138  if (pd != dpd)
139  GNUNET_OS_init (pd);
140 
141  gns_plugins = NULL;
142  once = 0;
143  num_plugins = 0;
144 }
145 
146 
155 char *
157  const void *data,
158  size_t data_size)
159 {
160  struct Plugin *plugin;
161  char *ret;
162 
163  init ();
164  for (unsigned int i = 0; i < num_plugins; i++)
165  {
166  plugin = gns_plugins[i];
167  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
168  type,
169  data,
170  data_size)))
171  return ret;
172  }
173  return NULL;
174 }
175 
176 
187 int
189  const char *s,
190  void **data,
191  size_t *data_size)
192 {
193  struct Plugin *plugin;
194 
195  init ();
196  for (unsigned int i = 0; i < num_plugins; i++)
197  {
198  plugin = gns_plugins[i];
199  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
200  type,
201  s,
202  data,
203  data_size))
204  return GNUNET_OK;
205  }
206  return GNUNET_SYSERR;
207 }
208 
209 
216 uint32_t
217 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
218 {
219  struct Plugin *plugin;
220  uint32_t ret;
221 
222  if (0 == strcasecmp (dns_typename,
223  "ANY"))
225  init ();
226  for (unsigned int i = 0; i < num_plugins; i++)
227  {
228  plugin = gns_plugins[i];
229  if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
230  dns_typename)))
231  return ret;
232  }
233  return UINT32_MAX;
234 }
235 
236 
243 const char *
245 {
246  struct Plugin *plugin;
247  const char *ret;
248 
249  if (GNUNET_GNSRECORD_TYPE_ANY == type)
250  return "ANY";
251  init ();
252  for (unsigned int i = 0; i < num_plugins; i++)
253  {
254  plugin = gns_plugins[i];
255  if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
256  type)))
257  return ret;
258  }
259  return NULL;
260 }
261 
262 
263 /* end of gnsrecord.c */
Project-specific data used to help the OS subsystem find installation paths.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g "AAAA") to the corresponding number.
Definition: gnsrecord.c:217
#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:79
static void init()
Loads all plugins (lazy initialization).
Definition: gnsrecord.c:100
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_default(void)
Return default project data used by &#39;libgnunetutil&#39; for GNUnet.
#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
void GNUNET_PLUGIN_load_all_in_context(const struct GNUNET_OS_ProjectData *ctx, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name while inside the given context (i...
Definition: plugin.c:443
#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:47
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
static int once
Global to mark if we&#39;ve run the initialization.
Definition: gnsrecord.c:68
static unsigned int num_plugins
Size of the &#39;plugins&#39; array.
Definition: gnsrecord.c:63
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:188
static char * plugin
Solver plugin name as string.
struct GNUNET_GNSRECORD_PluginFunctions * api
Plugin API.
Definition: gnsrecord.c:51
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:117
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
static struct Plugin ** gns_plugins
Array of our plugins.
Definition: gnsrecord.c:58
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
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:156
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string (e.g.
Definition: gnsrecord.c:244
#define GNUNET_free(ptr)
Wrapper around free.