GNUnet  0.10.x
plugin_namecache_flat.c
Go to the documentation of this file.
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 
27 #include "platform.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "namecache.h"
32 
36 struct Plugin {
37  const struct GNUNET_CONFIGURATION_Handle *cfg;
38 
42  char *fn;
43 
48 };
49 
50 struct FlatFileEntry {
55 
59  struct GNUNET_HashCode query;
60 };
61 
70 static int
72 {
73  char *afsdir;
74  char* block_buffer;
75  char* buffer;
76  char* line;
77  char* query;
78  char* block;
79  size_t size;
80  struct FlatFileEntry *entry;
81  struct GNUNET_DISK_FileHandle *fh;
82 
83  if (GNUNET_OK !=
85  "namecache-flat",
86  "FILENAME",
87  &afsdir))
88  {
90  "namecache-flat", "FILENAME");
91  return GNUNET_SYSERR;
92  }
93  if (GNUNET_OK != GNUNET_DISK_file_test(afsdir))
94  {
96  {
97  GNUNET_break(0);
98  GNUNET_free(afsdir);
99  return GNUNET_SYSERR;
100  }
101  }
102  /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
103  plugin->fn = afsdir;
104 
105  /* Load data from file into hashmap */
107  GNUNET_NO);
108  fh = GNUNET_DISK_file_open(afsdir,
113  if (NULL == fh)
114  {
116  _("Unable to initialize file: %s.\n"),
117  afsdir);
118  return GNUNET_SYSERR;
119  }
120 
121  if (GNUNET_SYSERR == GNUNET_DISK_file_size(afsdir,
122  &size,
123  GNUNET_YES,
124  GNUNET_YES))
125  {
127  _("Unable to get filesize: %s.\n"),
128  afsdir);
130  return GNUNET_SYSERR;
131  }
132 
133  if (0 == size)
134  {
136  return GNUNET_OK;
137  }
138 
139  buffer = GNUNET_malloc(size + 1);
140 
142  buffer,
143  size))
144  {
146  _("Unable to read file: %s.\n"),
147  afsdir);
148  GNUNET_free(buffer);
150  return GNUNET_SYSERR;
151  }
152  buffer[size] = '\0';
153 
155  if (0 < size)
156  {
157  line = strtok(buffer, "\n");
158  while (line != NULL)
159  {
160  query = strtok(line, ",");
161  if (NULL == query)
162  break;
163  block = strtok(NULL, ",");
164  if (NULL == block)
165  break;
166  line = strtok(NULL, "\n");
167  entry = GNUNET_malloc(sizeof(struct FlatFileEntry));
169  &entry->query));
171  strlen(block),
172  (void**)&block_buffer);
173  entry->block = (struct GNUNET_GNSRECORD_Block *)block_buffer;
174  if (GNUNET_OK !=
176  &entry->query,
177  entry,
179  {
180  GNUNET_free(entry);
181  GNUNET_break(0);
182  }
183  }
184  }
185  GNUNET_free(buffer);
186  return GNUNET_OK;
187 }
188 
194 static int
196  const struct GNUNET_HashCode *key,
197  void *value)
198 {
199  struct GNUNET_DISK_FileHandle *fh = cls;
200  struct FlatFileEntry *entry = value;
201 
202  char *line;
203  char *block_b64;
204  struct GNUNET_CRYPTO_HashAsciiEncoded query;
205  size_t block_size;
206 
207  block_size = ntohl(entry->block->purpose.size) +
208  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
209  sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
210 
211  GNUNET_STRINGS_base64_encode((char*)entry->block,
212  block_size,
213  &block_b64);
215  &query);
216  GNUNET_asprintf(&line,
217  "%s,%s\n",
218  (char*)&query,
219  block_b64);
220 
221  GNUNET_free(block_b64);
222 
224  line,
225  strlen(line));
226 
227  GNUNET_free(entry->block);
228  GNUNET_free(entry);
229  GNUNET_free(line);
230  return GNUNET_YES;
231 }
232 
238 static void
240 {
241  struct GNUNET_DISK_FileHandle *fh;
242 
243  fh = GNUNET_DISK_file_open(plugin->fn,
249  if (NULL == fh)
250  {
252  _("Unable to initialize file: %s.\n"),
253  plugin->fn);
254  return;
255  }
256 
259  fh);
262 }
263 
264 static int
265 expire_blocks(void *cls,
266  const struct GNUNET_HashCode *key,
267  void *value)
268 {
269  struct Plugin *plugin = cls;
270  struct FlatFileEntry *entry = value;
271  struct GNUNET_TIME_Absolute now;
272  struct GNUNET_TIME_Absolute expiration;
273 
274  now = GNUNET_TIME_absolute_get();
275  expiration = GNUNET_TIME_absolute_ntoh(entry->block->expiration_time);
276 
278  expiration).rel_value_us)
279  {
281  }
282  return GNUNET_YES;
283 }
284 
285 
286 
292 static void
294 {
296  &expire_blocks,
297  plugin);
298 }
299 
300 
308 static int
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);
319  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
320  &query);
321  block_size = ntohl(block->purpose.size) +
322  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
323  sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
324  if (block_size > 64 * 65536)
325  {
326  GNUNET_break(0);
327  return GNUNET_SYSERR;
328  }
329  entry = GNUNET_malloc(sizeof(struct FlatFileEntry));
330  entry->block = GNUNET_malloc(block_size);
331  GNUNET_memcpy(entry->block, block, block_size);
333  if (GNUNET_OK !=
335  &query,
336  entry,
338  {
339  GNUNET_free(entry);
340  GNUNET_break(0);
341  return GNUNET_SYSERR;
342  }
344  "Caching block under derived key `%s'\n",
345  GNUNET_h2s_full(&query));
346  return GNUNET_OK;
347 }
348 
349 
360 static int
362  const struct GNUNET_HashCode *query,
364 {
365  struct Plugin *plugin = cls;
366  const struct GNUNET_GNSRECORD_Block *block;
367 
368  block = GNUNET_CONTAINER_multihashmap_get(plugin->hm, query);
369  if (NULL == block)
370  return GNUNET_NO;
372  "Found block under derived key `%s'\n",
373  GNUNET_h2s_full(query));
374  iter(iter_cls, block);
375  return GNUNET_YES;
376 }
377 
378 
385 void *
387 {
388  static struct Plugin plugin;
389  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
391 
392  if (NULL != plugin.cfg)
393  return NULL; /* can only initialize once! */
394  memset(&plugin, 0, sizeof(struct Plugin));
395  plugin.cfg = cfg;
396  if (GNUNET_OK != database_setup(&plugin))
397  {
398  database_shutdown(&plugin);
399  return NULL;
400  }
402  api->cls = &plugin;
406  _("flat plugin running\n"));
407  return api;
408 }
409 
410 
417 void *
419 {
421  struct Plugin *plugin = api->cls;
422 
423  database_shutdown(plugin);
424  plugin->cfg = NULL;
425  GNUNET_free(api);
427  "flat plugin is finished\n");
428  return NULL;
429 }
430 
431 /* end of plugin_namecache_sqlite.c */
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:541
void(* GNUNET_NAMECACHE_BlockCallback)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Function called for matching blocks.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
Create file if it doesn&#39;t exist.
void * cls
Closure to pass to all plugin functions.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:671
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
struct GNUNET_GNSRECORD_Block * block
Block.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:729
static int namecache_cache_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_HashCode query
query
char * key
TLS key.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:681
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
int GNUNET_CONTAINER_multihashmap_remove_all(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Remove all entries for the given key from the map.
sqlite3_stmt * expire_blocks
Precompiled SQL for removing expired blocks.
Information we have in an encrypted block with record data (i.e.
Internal representation of the hash map.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
void * libgnunet_plugin_namecache_flat_init(void *cls)
Entry point for the plugin.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:46
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static char * line
Desired phone line (string to be converted to a hash).
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
static char * value
Value of the record to add/remove.
int(* lookup_block)(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
Truncate file if it exists.
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:817
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static int fh
Handle to the unique file.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
static void namecache_expire_blocks(struct Plugin *plugin)
Removes any expired block.
static int store_and_free_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
Store values in hashmap in file and free data.
A 512-bit hashcode.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:1920
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1856
There must only be one value per key; storing a value should fail if a value under the same key alrea...
void GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
Convert hash to ASCII encoding.
Definition: crypto_hash.c:69
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
void * iter_cls
Iterator cls.
const char * GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
struct returned by the initialization function of the plugin
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:254
0-terminated ASCII encoding of a struct GNUNET_HashCode.
char * fn
Filename used for the DB.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
unsigned long long size
Size of all values we&#39;re storing.
configuration data
Definition: configuration.c:83
Handle for a plugin.
Definition: block.c:37
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_log(kind,...)
Open the file for both reading and writing.
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
static int namecache_lookup_block(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute end)
Compute the time difference between the given start and end times.
Definition: time.c:353
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1262
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
GNUNET_PEERSTORE_Processor iter
Iterator.
#define GNUNET_CRYPTO_hash_from_string(enc, result)
Convert ASCII encoding back to struct GNUNET_HashCode
common internal definitions for namecache service
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
Handle used to access files (and pipes).
void * libgnunet_plugin_namecache_flat_done(void *cls)
Exit point from the plugin.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.