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;
107 
108  if (pd != dpd)
109  GNUNET_OS_init (dpd);
110  GNUNET_PLUGIN_load_all ("libgnunet_plugin_gnsrecord_", NULL,
111  &add_plugin, NULL);
112  if (pd != dpd)
113  GNUNET_OS_init (pd);
114 }
115 
116 
120 void __attribute__ ((destructor))
121 GNSRECORD_fini ()
122 {
123  struct Plugin *plugin;
126 
127  if (pd != dpd)
128  GNUNET_OS_init (dpd);
129 
130  for (unsigned int i = 0; i < num_plugins; i++)
131  {
132  plugin = gns_plugins[i];
133  GNUNET_break (NULL ==
135  plugin->api));
136  GNUNET_free (plugin->library_name);
137  GNUNET_free (plugin);
138  }
139  GNUNET_free (gns_plugins);
140 
141  if (pd != dpd)
142  GNUNET_OS_init (pd);
143 
144  gns_plugins = NULL;
145  once = 0;
146  num_plugins = 0;
147 }
148 
149 
158 char *
160  const void *data,
161  size_t data_size)
162 {
163  struct Plugin *plugin;
164  char *ret;
165 
166  init ();
167  for (unsigned int i = 0; i < num_plugins; i++)
168  {
169  plugin = gns_plugins[i];
170  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
171  type,
172  data,
173  data_size)))
174  return ret;
175  }
176  return NULL;
177 }
178 
179 
190 int
192  const char *s,
193  void **data,
194  size_t *data_size)
195 {
196  struct Plugin *plugin;
197 
198  init ();
199  for (unsigned int i = 0; i < num_plugins; i++)
200  {
201  plugin = gns_plugins[i];
202  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
203  type,
204  s,
205  data,
206  data_size))
207  return GNUNET_OK;
208  }
209  return GNUNET_SYSERR;
210 }
211 
212 
219 uint32_t
220 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
221 {
222  struct Plugin *plugin;
223  uint32_t ret;
224 
225  if (0 == strcasecmp (dns_typename,
226  "ANY"))
228  init ();
229  for (unsigned int i = 0; i < num_plugins; i++)
230  {
231  plugin = gns_plugins[i];
232  if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
233  dns_typename)))
234  return ret;
235  }
236  return UINT32_MAX;
237 }
238 
239 
246 const char *
248 {
249  struct Plugin *plugin;
250  const char *ret;
251 
252  if (GNUNET_GNSRECORD_TYPE_ANY == type)
253  return "ANY";
254  init ();
255  for (unsigned int i = 0; i < num_plugins; i++)
256  {
257  plugin = gns_plugins[i];
258  if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
259  type)))
260  return ret;
261  }
262  return NULL;
263 }
264 
265 
266 /* 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 (i.e.
Definition: gnsrecord.c:220
#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
#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:256
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:191
static char * plugin
Solver plugin name as string.
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
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:120
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:159
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:247
#define GNUNET_free(ptr)
Wrapper around free.