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  GNUNET_PLUGIN_load_all ("libgnunet_plugin_gnsrecord_", NULL,
106  &add_plugin, NULL);
107 }
108 
109 
113 void __attribute__ ((destructor))
114 GNSRECORD_fini ()
115 {
116  struct Plugin *plugin;
117 
118  for (unsigned int i = 0; i < num_plugins; i++)
119  {
120  plugin = gns_plugins[i];
121  GNUNET_break (NULL ==
123  plugin->api));
124  GNUNET_free (plugin->library_name);
125  GNUNET_free (plugin);
126  }
127  GNUNET_free_non_null (gns_plugins);
128  gns_plugins = NULL;
129  once = 0;
130  num_plugins = 0;
131 }
132 
133 
142 char *
144  const void *data,
145  size_t data_size)
146 {
147  struct Plugin *plugin;
148  char *ret;
149 
150  init ();
151  for (unsigned int i = 0; i < num_plugins; i++)
152  {
153  plugin = gns_plugins[i];
154  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
155  type,
156  data,
157  data_size)))
158  return ret;
159  }
160  return NULL;
161 }
162 
163 
174 int
176  const char *s,
177  void **data,
178  size_t *data_size)
179 {
180  struct Plugin *plugin;
181 
182  init ();
183  for (unsigned int i = 0; i < num_plugins; i++)
184  {
185  plugin = gns_plugins[i];
186  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
187  type,
188  s,
189  data,
190  data_size))
191  return GNUNET_OK;
192  }
193  return GNUNET_SYSERR;
194 }
195 
196 
203 uint32_t
204 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
205 {
206  struct Plugin *plugin;
207  uint32_t ret;
208 
209  if (0 == strcasecmp (dns_typename,
210  "ANY"))
212  init ();
213  for (unsigned int i = 0; i < num_plugins; i++)
214  {
215  plugin = gns_plugins[i];
216  if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
217  dns_typename)))
218  return ret;
219  }
220  return UINT32_MAX;
221 }
222 
223 
230 const char *
232 {
233  struct Plugin *plugin;
234  const char *ret;
235 
236  if (GNUNET_GNSRECORD_TYPE_ANY == type)
237  return "ANY";
238  init ();
239  for (unsigned int i = 0; i < num_plugins; i++)
240  {
241  plugin = gns_plugins[i];
242  if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
243  type)))
244  return ret;
245  }
246  return NULL;
247 }
248 
249 
250 /* end of gnsrecord.c */
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:204
#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
#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: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:175
static char * plugin
Solver plugin name as string.
#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:372
#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: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:113
static struct Plugin ** gns_plugins
Array of our plugins.
Definition: gnsrecord.c:58
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:143
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:231
#define GNUNET_free(ptr)
Wrapper around free.