aboutsummaryrefslogtreecommitdiff
path: root/src/plugin/namecache/plugin_namecache_flat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin/namecache/plugin_namecache_flat.c')
-rw-r--r--src/plugin/namecache/plugin_namecache_flat.c429
1 files changed, 429 insertions, 0 deletions
diff --git a/src/plugin/namecache/plugin_namecache_flat.c b/src/plugin/namecache/plugin_namecache_flat.c
new file mode 100644
index 000000000..ba118bf02
--- /dev/null
+++ b/src/plugin/namecache/plugin_namecache_flat.c
@@ -0,0 +1,429 @@
1/*
2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015 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 namecache/plugin_namecache_flat.c
23 * @brief flat file-based namecache backend
24 * @author Martin Schanzenbach
25 */
26
27#include "platform.h"
28#include "gnunet_namecache_plugin.h"
29#include "gnunet_namecache_service.h"
30#include "gnunet_gnsrecord_lib.h"
31
32/**
33 * Context for all functions in this plugin.
34 */
35struct Plugin
36{
37 const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 /**
40 * Database filename.
41 */
42 char *fn;
43
44 /**
45 * HashMap
46 */
47 struct GNUNET_CONTAINER_MultiHashMap *hm;
48};
49
50struct FlatFileEntry
51{
52 /**
53 * Block
54 */
55 struct GNUNET_GNSRECORD_Block *block;
56
57 /**
58 * query
59 */
60 struct GNUNET_HashCode query;
61};
62
63/**
64 * Initialize the database connections and associated
65 * data structures (create tables and indices
66 * as needed as well).
67 *
68 * @param plugin the plugin context (state for this module)
69 * @return #GNUNET_OK on success
70 */
71static int
72database_setup (struct Plugin *plugin)
73{
74 char *afsdir;
75 char*block_buffer;
76 char*buffer;
77 char*line;
78 char*query;
79 char*block;
80 uint64_t size;
81 struct FlatFileEntry *entry;
82 struct GNUNET_DISK_FileHandle *fh;
83
84 if (GNUNET_OK !=
85 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
86 "namecache-flat",
87 "FILENAME",
88 &afsdir))
89 {
90 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
91 "namecache-flat", "FILENAME");
92 return GNUNET_SYSERR;
93 }
94 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
95 {
96 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
97 {
98 GNUNET_break (0);
99 GNUNET_free (afsdir);
100 return GNUNET_SYSERR;
101 }
102 }
103 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
104 plugin->fn = afsdir;
105
106 /* Load data from file into hashmap */
107 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
108 GNUNET_NO);
109 fh = GNUNET_DISK_file_open (afsdir,
110 GNUNET_DISK_OPEN_CREATE
111 | GNUNET_DISK_OPEN_READWRITE,
112 GNUNET_DISK_PERM_USER_WRITE
113 | GNUNET_DISK_PERM_USER_READ);
114 if (NULL == fh)
115 {
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 _ ("Unable to initialize file: %s.\n"),
118 afsdir);
119 return GNUNET_SYSERR;
120 }
121
122 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
123 &size,
124 GNUNET_YES,
125 GNUNET_YES))
126 {
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 _ ("Unable to get filesize: %s.\n"),
129 afsdir);
130 GNUNET_DISK_file_close (fh);
131 return GNUNET_SYSERR;
132 }
133
134 if (0 == size)
135 {
136 GNUNET_DISK_file_close (fh);
137 return GNUNET_OK;
138 }
139
140 buffer = GNUNET_malloc (size + 1);
141
142 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
143 buffer,
144 size))
145 {
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 _ ("Unable to read file: %s.\n"),
148 afsdir);
149 GNUNET_free (buffer);
150 GNUNET_DISK_file_close (fh);
151 return GNUNET_SYSERR;
152 }
153 buffer[size] = '\0';
154
155 GNUNET_DISK_file_close (fh);
156 if (0 < size)
157 {
158 line = strtok (buffer, "\n");
159 while (line != NULL)
160 {
161 query = strtok (line, ",");
162 if (NULL == query)
163 break;
164 block = strtok (NULL, ",");
165 if (NULL == block)
166 break;
167 line = strtok (NULL, "\n");
168 entry = GNUNET_malloc (sizeof(struct FlatFileEntry));
169 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (query,
170 &entry->query));
171 GNUNET_STRINGS_base64_decode (block,
172 strlen (block),
173 (void **) &block_buffer);
174 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
175 if (GNUNET_OK !=
176 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
177 &entry->query,
178 entry,
179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
180 {
181 GNUNET_free (entry);
182 GNUNET_break (0);
183 }
184 }
185 }
186 GNUNET_free (buffer);
187 return GNUNET_OK;
188}
189
190
191/**
192 * Store values in hashmap in file and free data
193 *
194 * @param plugin the plugin context
195 */
196static int
197store_and_free_entries (void *cls,
198 const struct GNUNET_HashCode *key,
199 void *value)
200{
201 struct GNUNET_DISK_FileHandle *fh = cls;
202 struct FlatFileEntry *entry = value;
203
204 char *line;
205 char *block_b64;
206 struct GNUNET_CRYPTO_HashAsciiEncoded query;
207 size_t block_size;
208
209 block_size = GNUNET_GNSRECORD_block_get_size (entry->block);
210 GNUNET_STRINGS_base64_encode ((char *) entry->block,
211 block_size,
212 &block_b64);
213 GNUNET_CRYPTO_hash_to_enc (&entry->query,
214 &query);
215 GNUNET_asprintf (&line,
216 "%s,%s\n",
217 (char *) &query,
218 block_b64);
219
220 GNUNET_free (block_b64);
221
222 GNUNET_DISK_file_write (fh,
223 line,
224 strlen (line));
225
226 GNUNET_free (entry->block);
227 GNUNET_free (entry);
228 GNUNET_free (line);
229 return GNUNET_YES;
230}
231
232
233/**
234 * Shutdown database connection and associate data
235 * structures.
236 * @param plugin the plugin context (state for this module)
237 */
238static void
239database_shutdown (struct Plugin *plugin)
240{
241 struct GNUNET_DISK_FileHandle *fh;
242
243 fh = GNUNET_DISK_file_open (plugin->fn,
244 GNUNET_DISK_OPEN_CREATE
245 | GNUNET_DISK_OPEN_TRUNCATE
246 | GNUNET_DISK_OPEN_READWRITE,
247 GNUNET_DISK_PERM_USER_WRITE
248 | GNUNET_DISK_PERM_USER_READ);
249 if (NULL == fh)
250 {
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 _ ("Unable to initialize file: %s.\n"),
253 plugin->fn);
254 return;
255 }
256
257 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
258 &store_and_free_entries,
259 fh);
260 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
261 GNUNET_DISK_file_close (fh);
262}
263
264
265static int
266expire_blocks (void *cls,
267 const struct GNUNET_HashCode *key,
268 void *value)
269{
270 struct Plugin *plugin = cls;
271 struct FlatFileEntry *entry = value;
272 struct GNUNET_TIME_Absolute now;
273 struct GNUNET_TIME_Absolute expiration;
274
275 now = GNUNET_TIME_absolute_get ();
276 expiration = GNUNET_GNSRECORD_block_get_expiration (entry->block);
277
278 if (0 == GNUNET_TIME_absolute_get_difference (now,
279 expiration).rel_value_us)
280 {
281 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, key);
282 }
283 return GNUNET_YES;
284}
285
286
287/**
288 * Removes any expired block.
289 *
290 * @param plugin the plugin
291 */
292static void
293namecache_expire_blocks (struct Plugin *plugin)
294{
295 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
296 &expire_blocks,
297 plugin);
298}
299
300
301/**
302 * Cache a block in the datastore.
303 *
304 * @param cls closure (internal context for the plugin)
305 * @param block block to cache
306 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
307 */
308static int
309namecache_cache_block (void *cls,
310 const struct GNUNET_GNSRECORD_Block *block)
311{
312 struct Plugin *plugin = cls;
313 struct GNUNET_HashCode query;
314 struct FlatFileEntry *entry;
315 size_t block_size;
316
317 namecache_expire_blocks (plugin);
318 GNUNET_GNSRECORD_query_from_block (block,
319 &query);
320 block_size = GNUNET_GNSRECORD_block_get_size (block);
321 if (block_size > 64 * 65536)
322 {
323 GNUNET_break (0);
324 return GNUNET_SYSERR;
325 }
326 entry = GNUNET_malloc (sizeof(struct FlatFileEntry));
327 entry->block = GNUNET_malloc (block_size);
328 GNUNET_memcpy (entry->block, block, block_size);
329 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &query);
330 if (GNUNET_OK !=
331 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
332 &query,
333 entry,
334 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
335 {
336 GNUNET_free (entry);
337 GNUNET_break (0);
338 return GNUNET_SYSERR;
339 }
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Caching block under derived key `%s'\n",
342 GNUNET_h2s_full (&query));
343 return GNUNET_OK;
344}
345
346
347/**
348 * Get the block for a particular zone and label in the
349 * datastore. Will return at most one result to the iterator.
350 *
351 * @param cls closure (internal context for the plugin)
352 * @param query hash of public key derived from the zone and the label
353 * @param iter function to call with the result
354 * @param iter_cls closure for @a iter
355 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
356 */
357static int
358namecache_lookup_block (void *cls,
359 const struct GNUNET_HashCode *query,
360 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
361{
362 struct Plugin *plugin = cls;
363 const struct GNUNET_GNSRECORD_Block *block;
364
365 block = GNUNET_CONTAINER_multihashmap_get (plugin->hm, query);
366 if (NULL == block)
367 return GNUNET_NO;
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Found block under derived key `%s'\n",
370 GNUNET_h2s_full (query));
371 iter (iter_cls, block);
372 return GNUNET_YES;
373}
374
375
376/**
377 * Entry point for the plugin.
378 *
379 * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
380 * @return NULL on error, otherwise the plugin context
381 */
382void *
383libgnunet_plugin_namecache_flat_init (void *cls)
384{
385 static struct Plugin plugin;
386 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
387 struct GNUNET_NAMECACHE_PluginFunctions *api;
388
389 if (NULL != plugin.cfg)
390 return NULL; /* can only initialize once! */
391 memset (&plugin, 0, sizeof(struct Plugin));
392 plugin.cfg = cfg;
393 if (GNUNET_OK != database_setup (&plugin))
394 {
395 database_shutdown (&plugin);
396 return NULL;
397 }
398 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
399 api->cls = &plugin;
400 api->cache_block = &namecache_cache_block;
401 api->lookup_block = &namecache_lookup_block;
402 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
403 _ ("flat plugin running\n"));
404 return api;
405}
406
407
408/**
409 * Exit point from the plugin.
410 *
411 * @param cls the plugin context (as returned by "init")
412 * @return always NULL
413 */
414void *
415libgnunet_plugin_namecache_flat_done (void *cls)
416{
417 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
418 struct Plugin *plugin = api->cls;
419
420 database_shutdown (plugin);
421 plugin->cfg = NULL;
422 GNUNET_free (api);
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "flat plugin is finished\n");
425 return NULL;
426}
427
428
429/* end of plugin_namecache_sqlite.c */