GNUnet 0.22.0
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 "gnunet_util_lib.h"
31
32#define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
33
34
38struct Plugin
39{
43 char *library_name;
44
49};
50
51
55static struct Plugin **gns_plugins;
56
60static unsigned int num_plugins;
61
65static int once;
66
67
75static void
76add_plugin (void *cls,
77 const char *library_name,
78 void *lib_ret)
79{
80 struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
81 struct Plugin *plugin;
82
84 "Loading block plugin `%s'\n",
86 plugin = GNUNET_new (struct Plugin);
87 plugin->api = api;
88 plugin->library_name = GNUNET_strdup (library_name);
90}
91
92
96static void
98{
99 if (1 == once)
100 return;
101 once = 1;
102
104 "libgnunet_plugin_gnsrecord_",
105 NULL,
106 &add_plugin,
107 NULL);
108}
109
110void
112
116void __attribute__ ((destructor))
117GNSRECORD_fini (void)
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 ==
130 GNUNET_PLUGIN_unload (plugin->library_name,
131 plugin->api));
132 GNUNET_free (plugin->library_name);
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
154char *
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
176int
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
199uint32_t
200GNUNET_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
226const 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:76
void GNSRECORD_fini(void)
static unsigned int num_plugins
Size of the 'plugins' array.
Definition: gnsrecord.c:60
static struct Plugin ** gns_plugins
Array of our plugins.
Definition: gnsrecord.c:55
static int once
Global to mark if we've run the initialization.
Definition: gnsrecord.c:65
void __attribute__((destructor))
Dual function to init().
Definition: gnsrecord.c:116
static void init()
Loads all plugins (lazy initialization).
Definition: gnsrecord.c:97
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static char * data
The data to insert into the dht.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
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
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/'*'.
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
#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.
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
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
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
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.
void * cls
Closure to pass to start_testcase.
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:48
char * library_name
Name of the shared library.
Definition: block.c:42