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/>.
20 * @file gnsrecord/gnsrecord.c
21 * @brief API to access GNS record data
22 * @author Martin Schanzenbach
23 * @author Matthias Wachs
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_gnsrecord_plugin.h"
31 #include "gnunet_tun_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__)
37 * Handle for a plugin.
42 * Name of the shared library.
49 struct GNUNET_GNSRECORD_PluginFunctions *api;
54 * Array of our plugins.
56 static struct Plugin **gns_plugins;
59 * Size of the 'plugins' array.
61 static unsigned int num_plugins;
64 * Global to mark if we've run the initialization.
70 * Add a plugin to the list managed by the block library.
73 * @param library_name name of the plugin
74 * @param lib_ret the plugin API
77 add_plugin (void *cls,
78 const char *library_name,
81 struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
82 struct Plugin *plugin;
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Loading block plugin `%s'\n",
87 plugin = GNUNET_new (struct Plugin);
89 plugin->library_name = GNUNET_strdup (library_name);
90 GNUNET_array_append (gns_plugins, num_plugins, plugin);
95 * Loads all plugins (lazy initialization).
103 GNUNET_PLUGIN_load_all ("libgnunet_plugin_gnsrecord_", NULL,
109 * Dual function to #init().
111 void __attribute__ ((destructor))
114 struct Plugin *plugin;
116 for (unsigned int i = 0; i < num_plugins; i++)
118 plugin = gns_plugins[i];
119 GNUNET_break (NULL ==
120 GNUNET_PLUGIN_unload (plugin->library_name,
122 GNUNET_free (plugin->library_name);
123 GNUNET_free (plugin);
125 GNUNET_free_non_null (gns_plugins);
133 * Convert the 'value' of a record to a string.
135 * @param type type of the record
136 * @param data value in binary encoding
137 * @param data_size number of bytes in @a data
138 * @return NULL on error, otherwise human-readable representation of the value
141 GNUNET_GNSRECORD_value_to_string (uint32_t type,
145 struct Plugin *plugin;
149 for (unsigned int i = 0; i < num_plugins; i++)
151 plugin = gns_plugins[i];
152 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
163 * Convert human-readable version of a 'value' of a record to the binary
166 * @param type type of the record
167 * @param s human-readable string
168 * @param data set to value in binary encoding (will be allocated)
169 * @param data_size set to number of bytes in @a data
170 * @return #GNUNET_OK on success
173 GNUNET_GNSRECORD_string_to_value (uint32_t type,
178 struct Plugin *plugin;
181 for (unsigned int i = 0; i < num_plugins; i++)
183 plugin = gns_plugins[i];
184 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
191 return GNUNET_SYSERR;
196 * Convert a type name (i.e. "AAAA") to the corresponding number.
198 * @param dns_typename name to convert
199 * @return corresponding number, UINT32_MAX on error
202 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
204 struct Plugin *plugin;
207 if (0 == strcasecmp (dns_typename,
209 return GNUNET_GNSRECORD_TYPE_ANY;
211 for (unsigned int i = 0; i < num_plugins; i++)
213 plugin = gns_plugins[i];
214 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
223 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
225 * @param type number of a type to convert
226 * @return corresponding typestring, NULL on error
229 GNUNET_GNSRECORD_number_to_typename (uint32_t type)
231 struct Plugin *plugin;
234 if (GNUNET_GNSRECORD_TYPE_ANY == type)
237 for (unsigned int i = 0; i < num_plugins; i++)
239 plugin = gns_plugins[i];
240 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
247 /* end of gnsrecord.c */