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.
17 * @file gnsrecord/gnsrecord.c
18 * @brief API to access GNS record data
19 * @author Martin Schanzenbach
20 * @author Matthias Wachs
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_constants.h"
26 #include "gnunet_gnsrecord_lib.h"
27 #include "gnunet_gnsrecord_plugin.h"
28 #include "gnunet_tun_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__)
35 * Handle for a plugin.
40 * Name of the shared library.
47 struct GNUNET_GNSRECORD_PluginFunctions *api;
52 * Array of our plugins.
54 static struct Plugin **gns_plugins;
57 * Size of the 'plugins' array.
59 static unsigned int num_plugins;
62 * Global to mark if we've run the initialization.
68 * Add a plugin to the list managed by the block library.
71 * @param library_name name of the plugin
72 * @param lib_ret the plugin API
75 add_plugin (void *cls,
76 const char *library_name,
79 struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
80 struct Plugin *plugin;
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83 "Loading block plugin `%s'\n",
85 plugin = GNUNET_new (struct Plugin);
87 plugin->library_name = GNUNET_strdup (library_name);
88 GNUNET_array_append (gns_plugins, num_plugins, plugin);
93 * Loads all plugins (lazy initialization).
101 GNUNET_PLUGIN_load_all ("libgnunet_plugin_gnsrecord_", NULL,
107 * Dual function to #init().
109 void __attribute__ ((destructor))
112 struct Plugin *plugin;
114 for (unsigned int i = 0; i < num_plugins; i++)
116 plugin = gns_plugins[i];
117 GNUNET_break (NULL ==
118 GNUNET_PLUGIN_unload (plugin->library_name,
120 GNUNET_free (plugin->library_name);
121 GNUNET_free (plugin);
123 GNUNET_free_non_null (gns_plugins);
131 * Convert the 'value' of a record to a string.
133 * @param type type of the record
134 * @param data value in binary encoding
135 * @param data_size number of bytes in @a data
136 * @return NULL on error, otherwise human-readable representation of the value
139 GNUNET_GNSRECORD_value_to_string (uint32_t type,
143 struct Plugin *plugin;
147 for (unsigned int i = 0; i < num_plugins; i++)
149 plugin = gns_plugins[i];
150 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
161 * Convert human-readable version of a 'value' of a record to the binary
164 * @param type type of the record
165 * @param s human-readable string
166 * @param data set to value in binary encoding (will be allocated)
167 * @param data_size set to number of bytes in @a data
168 * @return #GNUNET_OK on success
171 GNUNET_GNSRECORD_string_to_value (uint32_t type,
176 struct Plugin *plugin;
179 for (unsigned int i = 0; i < num_plugins; i++)
181 plugin = gns_plugins[i];
182 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
189 return GNUNET_SYSERR;
194 * Convert a type name (i.e. "AAAA") to the corresponding number.
196 * @param dns_typename name to convert
197 * @return corresponding number, UINT32_MAX on error
200 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
202 struct Plugin *plugin;
205 if (0 == strcasecmp (dns_typename,
207 return GNUNET_GNSRECORD_TYPE_ANY;
209 for (unsigned int i = 0; i < num_plugins; i++)
211 plugin = gns_plugins[i];
212 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls,
221 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
223 * @param type number of a type to convert
224 * @return corresponding typestring, NULL on error
227 GNUNET_GNSRECORD_number_to_typename (uint32_t type)
229 struct Plugin *plugin;
232 if (GNUNET_GNSRECORD_TYPE_ANY == type)
235 for (unsigned int i = 0; i < num_plugins; i++)
237 plugin = gns_plugins[i];
238 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls,
246 /* end of gnsrecord.c */