diff options
Diffstat (limited to 'src/gnsrecord/gnsrecord.c')
-rw-r--r-- | src/gnsrecord/gnsrecord.c | 284 |
1 files changed, 0 insertions, 284 deletions
diff --git a/src/gnsrecord/gnsrecord.c b/src/gnsrecord/gnsrecord.c deleted file mode 100644 index 52c480ef6..000000000 --- a/src/gnsrecord/gnsrecord.c +++ /dev/null | |||
@@ -1,284 +0,0 @@ | |||
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 | |||
21 | /** | ||
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 | ||
27 | */ | ||
28 | #include "platform.h" | ||
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" | ||
34 | |||
35 | #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__) | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Handle for a plugin. | ||
40 | */ | ||
41 | struct Plugin | ||
42 | { | ||
43 | /** | ||
44 | * Name of the shared library. | ||
45 | */ | ||
46 | char *library_name; | ||
47 | |||
48 | /** | ||
49 | * Plugin API. | ||
50 | */ | ||
51 | struct GNUNET_GNSRECORD_PluginFunctions *api; | ||
52 | }; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Array of our plugins. | ||
57 | */ | ||
58 | static struct Plugin **gns_plugins; | ||
59 | |||
60 | /** | ||
61 | * Size of the 'plugins' array. | ||
62 | */ | ||
63 | static unsigned int num_plugins; | ||
64 | |||
65 | /** | ||
66 | * Global to mark if we've run the initialization. | ||
67 | */ | ||
68 | static int once; | ||
69 | |||
70 | |||
71 | /** | ||
72 | * Add a plugin to the list managed by the block library. | ||
73 | * | ||
74 | * @param cls NULL | ||
75 | * @param library_name name of the plugin | ||
76 | * @param lib_ret the plugin API | ||
77 | */ | ||
78 | static void | ||
79 | add_plugin (void *cls, | ||
80 | const char *library_name, | ||
81 | void *lib_ret) | ||
82 | { | ||
83 | struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret; | ||
84 | struct Plugin *plugin; | ||
85 | |||
86 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
87 | "Loading block plugin `%s'\n", | ||
88 | library_name); | ||
89 | plugin = GNUNET_new (struct Plugin); | ||
90 | plugin->api = api; | ||
91 | plugin->library_name = GNUNET_strdup (library_name); | ||
92 | GNUNET_array_append (gns_plugins, num_plugins, plugin); | ||
93 | } | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Loads all plugins (lazy initialization). | ||
98 | */ | ||
99 | static void | ||
100 | init () | ||
101 | { | ||
102 | if (1 == once) | ||
103 | return; | ||
104 | once = 1; | ||
105 | |||
106 | GNUNET_PLUGIN_load_all_in_context (GNUNET_OS_project_data_default (), | ||
107 | "libgnunet_plugin_gnsrecord_", | ||
108 | NULL, | ||
109 | &add_plugin, | ||
110 | NULL); | ||
111 | } | ||
112 | |||
113 | |||
114 | /** | ||
115 | * Dual function to #init(). | ||
116 | */ | ||
117 | void __attribute__ ((destructor)) | ||
118 | GNSRECORD_fini () | ||
119 | { | ||
120 | struct Plugin *plugin; | ||
121 | const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get (); | ||
122 | const struct GNUNET_OS_ProjectData *dpd = GNUNET_OS_project_data_default (); | ||
123 | |||
124 | if (pd != dpd) | ||
125 | GNUNET_OS_init (dpd); | ||
126 | |||
127 | for (unsigned int i = 0; i < num_plugins; i++) | ||
128 | { | ||
129 | plugin = gns_plugins[i]; | ||
130 | GNUNET_break (NULL == | ||
131 | GNUNET_PLUGIN_unload (plugin->library_name, | ||
132 | plugin->api)); | ||
133 | GNUNET_free (plugin->library_name); | ||
134 | GNUNET_free (plugin); | ||
135 | } | ||
136 | GNUNET_free (gns_plugins); | ||
137 | |||
138 | if (pd != dpd) | ||
139 | GNUNET_OS_init (pd); | ||
140 | |||
141 | gns_plugins = NULL; | ||
142 | once = 0; | ||
143 | num_plugins = 0; | ||
144 | } | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Convert the 'value' of a record to a string. | ||
149 | * | ||
150 | * @param type type of the record | ||
151 | * @param data value in binary encoding | ||
152 | * @param data_size number of bytes in @a data | ||
153 | * @return NULL on error, otherwise human-readable representation of the value | ||
154 | */ | ||
155 | char * | ||
156 | GNUNET_GNSRECORD_value_to_string (uint32_t type, | ||
157 | const void *data, | ||
158 | size_t data_size) | ||
159 | { | ||
160 | struct Plugin *plugin; | ||
161 | char *ret; | ||
162 | |||
163 | init (); | ||
164 | for (unsigned int i = 0; i < num_plugins; i++) | ||
165 | { | ||
166 | plugin = gns_plugins[i]; | ||
167 | if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, | ||
168 | type, | ||
169 | data, | ||
170 | data_size))) | ||
171 | return ret; | ||
172 | } | ||
173 | return NULL; | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Convert human-readable version of a 'value' of a record to the binary | ||
179 | * representation. | ||
180 | * | ||
181 | * @param type type of the record | ||
182 | * @param s human-readable string | ||
183 | * @param data set to value in binary encoding (will be allocated) | ||
184 | * @param data_size set to number of bytes in @a data | ||
185 | * @return #GNUNET_OK on success | ||
186 | */ | ||
187 | int | ||
188 | GNUNET_GNSRECORD_string_to_value (uint32_t type, | ||
189 | const char *s, | ||
190 | void **data, | ||
191 | size_t *data_size) | ||
192 | { | ||
193 | struct Plugin *plugin; | ||
194 | |||
195 | init (); | ||
196 | for (unsigned int i = 0; i < num_plugins; i++) | ||
197 | { | ||
198 | plugin = gns_plugins[i]; | ||
199 | if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, | ||
200 | type, | ||
201 | s, | ||
202 | data, | ||
203 | data_size)) | ||
204 | return GNUNET_OK; | ||
205 | } | ||
206 | return GNUNET_SYSERR; | ||
207 | } | ||
208 | |||
209 | |||
210 | /** | ||
211 | * Convert a type name (e.g "AAAA") to the corresponding number. | ||
212 | * | ||
213 | * @param dns_typename name to convert | ||
214 | * @return corresponding number, UINT32_MAX on error | ||
215 | */ | ||
216 | uint32_t | ||
217 | GNUNET_GNSRECORD_typename_to_number (const char *dns_typename) | ||
218 | { | ||
219 | struct Plugin *plugin; | ||
220 | uint32_t ret; | ||
221 | |||
222 | if (0 == strcasecmp (dns_typename, | ||
223 | "ANY")) | ||
224 | return GNUNET_GNSRECORD_TYPE_ANY; | ||
225 | init (); | ||
226 | for (unsigned int i = 0; i < num_plugins; i++) | ||
227 | { | ||
228 | plugin = gns_plugins[i]; | ||
229 | if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls, | ||
230 | dns_typename))) | ||
231 | return ret; | ||
232 | } | ||
233 | return UINT32_MAX; | ||
234 | } | ||
235 | |||
236 | |||
237 | /** | ||
238 | * Convert a type number to the corresponding type string (e.g. 1 to "A") | ||
239 | * | ||
240 | * @param type number of a type to convert | ||
241 | * @return corresponding typestring, NULL on error | ||
242 | */ | ||
243 | const char * | ||
244 | GNUNET_GNSRECORD_number_to_typename (uint32_t type) | ||
245 | { | ||
246 | struct Plugin *plugin; | ||
247 | const char *ret; | ||
248 | |||
249 | if (GNUNET_GNSRECORD_TYPE_ANY == type) | ||
250 | return "ANY"; | ||
251 | init (); | ||
252 | for (unsigned int i = 0; i < num_plugins; i++) | ||
253 | { | ||
254 | plugin = gns_plugins[i]; | ||
255 | if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls, | ||
256 | type))) | ||
257 | return ret; | ||
258 | } | ||
259 | return NULL; | ||
260 | } | ||
261 | |||
262 | |||
263 | enum GNUNET_GenericReturnValue | ||
264 | GNUNET_GNSRECORD_is_critical (uint32_t type) | ||
265 | { | ||
266 | struct Plugin *plugin; | ||
267 | |||
268 | if (GNUNET_GNSRECORD_TYPE_ANY == type) | ||
269 | return GNUNET_NO; | ||
270 | init (); | ||
271 | for (unsigned int i = 0; i < num_plugins; i++) | ||
272 | { | ||
273 | plugin = gns_plugins[i]; | ||
274 | if (NULL == plugin->api->is_critical) | ||
275 | continue; | ||
276 | if (GNUNET_NO == plugin->api->is_critical (plugin->api->cls, type)) | ||
277 | continue; | ||
278 | return GNUNET_YES; | ||
279 | } | ||
280 | return GNUNET_NO; | ||
281 | } | ||
282 | |||
283 | |||
284 | /* end of gnsrecord.c */ | ||