GNUnet  0.19.5
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 
34 #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
35 
36 
40 struct Plugin
41 {
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;
92 }
93 
94 
98 static void
99 init ()
100 {
101  if (1 == once)
102  return;
103  once = 1;
104 
106  "libgnunet_plugin_gnsrecord_",
107  NULL,
108  &add_plugin,
109  NULL);
110 }
111 
112 
116 void __attribute__ ((destructor))
117 GNSRECORD_fini ()
118 {
119  struct Plugin *plugin;
122 
123  if (pd != dpd)
124  GNUNET_OS_init (dpd);
125 
126  for (unsigned int i = 0; i < num_plugins; i++)
127  {
128  plugin = gns_plugins[i];
129  GNUNET_break (NULL ==
131  plugin->api));
134  }
136 
137  if (pd != dpd)
138  GNUNET_OS_init (pd);
139 
140  gns_plugins = NULL;
141  once = 0;
142  num_plugins = 0;
143 }
144 
145 
154 char *
156  const void *data,
157  size_t data_size)
158 {
159  struct Plugin *plugin;
160  char *ret;
161 
162  init ();
163  for (unsigned int i = 0; i < num_plugins; i++)
164  {
165  plugin = gns_plugins[i];
166  if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
167  type,
168  data,
169  data_size)))
170  return ret;
171  }
172  return NULL;
173 }
174 
175 
176 int
178  const char *s,
179  void **data,
180  size_t *data_size)
181 {
182  struct Plugin *plugin;
183 
184  init ();
185  for (unsigned int i = 0; i < num_plugins; i++)
186  {
187  plugin = gns_plugins[i];
188  if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
189  type,
190  s,
191  data,
192  data_size))
193  return GNUNET_OK;
194  }
195  return GNUNET_SYSERR;
196 }
197 
198 
199 uint32_t
200 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
201 {
202  struct Plugin *plugin;
203  uint32_t ret;
204 
205  if (0 == strcasecmp (dns_typename,
206  "ANY"))
208  init ();
209  for (unsigned int i = 0; i < num_plugins; i++)
210  {
211  plugin = gns_plugins[i];
212  if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
213  dns_typename)))
214  return ret;
215  }
216  return UINT32_MAX;
217 }
218 
219 
226 const char *
228 {
229  struct Plugin *plugin;
230  const char *ret;
231 
233  return "ANY";
234  init ();
235  for (unsigned int i = 0; i < num_plugins; i++)
236  {
237  plugin = gns_plugins[i];
238  if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
239  type)))
240  return ret;
241  }
242  return NULL;
243 }
244 
245 
248 {
249  struct Plugin *plugin;
250 
252  return GNUNET_NO;
253  init ();
254  for (unsigned int i = 0; i < num_plugins; i++)
255  {
256  plugin = gns_plugins[i];
257  if (NULL == plugin->api->is_critical)
258  continue;
259  if (GNUNET_NO == plugin->api->is_critical (plugin->api->cls, type))
260  continue;
261  return GNUNET_YES;
262  }
263  return GNUNET_NO;
264 }
265 
266 
267 /* end of gnsrecord.c */
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 unsigned int num_plugins
Size of the 'plugins' array.
Definition: gnsrecord.c:62
static struct Plugin ** gns_plugins
Array of our plugins.
Definition: gnsrecord.c:57
static int once
Global to mark if we've run the initialization.
Definition: gnsrecord.c:67
void __attribute__((destructor))
Dual function to init().
Definition: gnsrecord.c:116
static void init()
Loads all plugins (lazy initialization).
Definition: gnsrecord.c:99
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
struct TestcasePlugin * plugin
The process handle to the testbed service.
uint32_t data
The data value.
API that can be used to manipulate GNS record data.
Plugin API for GNS record types.
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number to the corresponding type string (e.g.
Definition: gnsrecord.c:227
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the 'value' of a record to a string.
Definition: gnsrecord.c:155
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g.
Definition: gnsrecord.c:200
int GNUNET_GNSRECORD_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of the value s of a record of type type to the respective binary repre...
Definition: gnsrecord.c:177
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_is_critical(uint32_t type)
Check if this type is a critical record.
Definition: gnsrecord.c:247
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/'*'.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_default(void)
Return default project data used by 'libgnunetutil' for GNUnet.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:242
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:386
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
Project-specific data used to help the OS subsystem find installation paths.
Handle for a plugin.
Definition: block.c:38
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
struct GNUNET_GNSRECORD_PluginFunctions * api
Plugin API.
Definition: gnsrecord.c:50
char * library_name
Name of the shared library.
Definition: block.c:42
char * library_name
Name of the shared library.
Definition: testing.h:98
struct GNUNET_TESTING_PluginFunctions * api
Plugin API.
Definition: testing.h:103
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model