GNUnet 0.22.2
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;
103 "libgnunet_plugin_gnsrecord_",
104 NULL,
105 &add_plugin,
106 NULL);
107}
108
109
110void
112
113
117void __attribute__ ((destructor))
118GNSRECORD_fini (void)
119{
120 struct Plugin *plugin;
121
122 for (unsigned int i = 0; i < num_plugins; i++)
123 {
124 plugin = gns_plugins[i];
125 GNUNET_break (NULL ==
126 GNUNET_PLUGIN_unload (plugin->library_name,
127 plugin->api));
128 GNUNET_free (plugin->library_name);
130 }
132 gns_plugins = NULL;
133 once = 0;
134 num_plugins = 0;
135}
136
137
146char *
148 const void *data,
149 size_t data_size)
150{
151 struct Plugin *plugin;
152 char *ret;
153
154 init ();
155 for (unsigned int i = 0; i < num_plugins; i++)
156 {
157 plugin = gns_plugins[i];
158 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
159 type,
160 data,
161 data_size)))
162 return ret;
163 }
164 return NULL;
165}
166
167
168int
170 const char *s,
171 void **data,
172 size_t *data_size)
173{
174 struct Plugin *plugin;
175
176 init ();
177 for (unsigned int i = 0; i < num_plugins; i++)
178 {
179 plugin = gns_plugins[i];
180 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
181 type,
182 s,
183 data,
184 data_size))
185 return GNUNET_OK;
186 }
187 return GNUNET_SYSERR;
188}
189
190
191uint32_t
192GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
193{
194 struct Plugin *plugin;
195 uint32_t ret;
196
197 if (0 == strcasecmp (dns_typename,
198 "ANY"))
200 init ();
201 for (unsigned int i = 0; i < num_plugins; i++)
202 {
203 plugin = gns_plugins[i];
204 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
205 dns_typename)))
206 return ret;
207 }
208 return UINT32_MAX;
209}
210
211
218const char *
220{
221 struct Plugin *plugin;
222 const char *ret;
223
225 return "ANY";
226 init ();
227 for (unsigned int i = 0; i < num_plugins; i++)
228 {
229 plugin = gns_plugins[i];
230 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
231 type)))
232 return ret;
233 }
234 return NULL;
235}
236
237
240{
241 struct Plugin *plugin;
242
244 return GNUNET_NO;
245 init ();
246 for (unsigned int i = 0; i < num_plugins; i++)
247 {
248 plugin = gns_plugins[i];
249 if (NULL == plugin->api->is_critical)
250 continue;
251 if (GNUNET_NO == plugin->api->is_critical (plugin->api->cls, type))
252 continue;
253 return GNUNET_YES;
254 }
255 return GNUNET_NO;
256}
257
258
259/* 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:117
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:219
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g.
Definition: gnsrecord.c:192
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:169
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_is_critical(uint32_t type)
Check if this type is a critical record.
Definition: gnsrecord.c:239
#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:147
#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_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
void GNUNET_PLUGIN_load_all(const struct GNUNET_OS_ProjectData *pd, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name.
Definition: plugin.c:401
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:277
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
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