2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file gnsrecord/gnsrecord.c
23 * @brief API to access GNS record data
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_gnsrecord_plugin.h"
33 #include "gnunet_tun_lib.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__)
39 * Handle for a plugin.
44 * Name of the shared library.
51 struct GNUNET_GNSRECORD_PluginFunctions *api;
56 * Array of our plugins.
58 static struct Plugin **gns_plugins;
61 * Size of the 'plugins' array.
63 static unsigned int num_plugins;
66 * Global to mark if we've run the initialization.
72 * Add a plugin to the list managed by the block library.
75 * @param library_name name of the plugin
76 * @param lib_ret the plugin API
79 add_plugin (void *cls,
80 const char *library_name,
83 struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
84 struct Plugin *plugin;
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87 "Loading block plugin `%s'\n",
89 plugin = GNUNET_new (struct Plugin);
91 plugin->library_name = GNUNET_strdup (library_name);
92 GNUNET_array_append (gns_plugins, num_plugins, plugin);
97 * Loads all plugins (lazy initialization).
105 GNUNET_PLUGIN_load_all ("libgnunet_plugin_gnsrecord_", NULL,
111 * Dual function to #init().
113 void __attribute__ ((destructor))
116 struct Plugin *plugin;
118 for (unsigned int i = 0; i < num_plugins; i++)
120 plugin = gns_plugins[i];
121 GNUNET_break (NULL ==
122 GNUNET_PLUGIN_unload (plugin->library_name,
124 GNUNET_free (plugin->library_name);
125 GNUNET_free (plugin);
127 GNUNET_free_non_null (gns_plugins);
135 * Convert the 'value' of a record to a string.
137 * @param type type of the record
138 * @param data value in binary encoding
139 * @param data_size number of bytes in @a data
140 * @return NULL on error, otherwise human-readable representation of the value
143 GNUNET_GNSRECORD_value_to_string (uint32_t type,
147 struct Plugin *plugin;
151 for (unsigned int i = 0; i < num_plugins; i++)
153 plugin = gns_plugins[i];
154 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
165 * Convert human-readable version of a 'value' of a record to the binary
168 * @param type type of the record
169 * @param s human-readable string
170 * @param data set to value in binary encoding (will be allocated)
171 * @param data_size set to number of bytes in @a data
172 * @return #GNUNET_OK on success
175 GNUNET_GNSRECORD_string_to_value (uint32_t type,
180 struct Plugin *plugin;
183 for (unsigned int i = 0; i < num_plugins; i++)
185 plugin = gns_plugins[i];
186 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
193 return GNUNET_SYSERR;
198 * Convert a type name (i.e. "AAAA") to the corresponding number.
200 * @param dns_typename name to convert
201 * @return corresponding number, UINT32_MAX on error
204 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
206 struct Plugin *plugin;
209 if (0 == strcasecmp (dns_typename,
211 return GNUNET_GNSRECORD_TYPE_ANY;
213 for (unsigned int i = 0; i < num_plugins; i++)
215 plugin = gns_plugins[i];
216 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
225 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
227 * @param type number of a type to convert
228 * @return corresponding typestring, NULL on error
231 GNUNET_GNSRECORD_number_to_typename (uint32_t type)
233 struct Plugin *plugin;
236 if (GNUNET_GNSRECORD_TYPE_ANY == type)
239 for (unsigned int i = 0; i < num_plugins; i++)
241 plugin = gns_plugins[i];
242 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
249 /* end of gnsrecord.c */