GNUnet 0.22.2
plugin_namecache_flat.c File Reference

flat file-based namecache backend More...

Include dependency graph for plugin_namecache_flat.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 
struct  FlatFileEntry
 

Functions

static int database_setup (struct Plugin *plugin)
 Initialize the database connections and associated data structures (create tables and indices as needed as well). More...
 
static int store_and_free_entries (void *cls, const struct GNUNET_HashCode *key, void *value)
 Store values in hashmap in file and free data. More...
 
static void database_shutdown (struct Plugin *plugin)
 Shutdown database connection and associate data structures. More...
 
static int expire_blocks (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static void namecache_expire_blocks (struct Plugin *plugin)
 Removes any expired block. More...
 
static int namecache_cache_block (void *cls, const struct GNUNET_GNSRECORD_Block *block)
 Cache a block in the datastore. More...
 
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. More...
 
void * libgnunet_plugin_namecache_flat_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_namecache_flat_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

flat file-based namecache backend

Author
Martin Schanzenbach

Definition in file plugin_namecache_flat.c.

Function Documentation

◆ database_setup()

static int database_setup ( struct Plugin plugin)
static

Initialize the database connections and associated data structures (create tables and indices as needed as well).

Parameters
pluginthe plugin context (state for this module)
Returns
GNUNET_OK on success

Definition at line 72 of file plugin_namecache_flat.c.

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 !=
86 "namecache-flat",
87 "FILENAME",
88 &afsdir))
89 {
91 "namecache-flat", "FILENAME");
92 return GNUNET_SYSERR;
93 }
94 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
95 {
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 */
108 GNUNET_NO);
109 fh = GNUNET_DISK_file_open (afsdir,
114 if (NULL == fh)
115 {
117 _ ("Unable to initialize file: %s.\n"),
118 afsdir);
119 return GNUNET_SYSERR;
120 }
121
123 &size,
125 GNUNET_YES))
126 {
128 _ ("Unable to get filesize: %s.\n"),
129 afsdir);
131 return GNUNET_SYSERR;
132 }
133
134 if (0 == size)
135 {
137 return GNUNET_OK;
138 }
139
140 buffer = GNUNET_malloc (size + 1);
141
143 buffer,
144 size))
145 {
147 _ ("Unable to read file: %s.\n"),
148 afsdir);
149 GNUNET_free (buffer);
151 return GNUNET_SYSERR;
152 }
153 buffer[size] = '\0';
154
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));
170 &entry->query));
172 strlen (block),
173 (void **) &block_buffer);
174 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
175 if (GNUNET_OK !=
177 &entry->query,
178 entry,
180 {
181 GNUNET_free (entry);
182 GNUNET_break (0);
183 }
184 }
185 }
186 GNUNET_free (buffer);
187 return GNUNET_OK;
188}
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static char * line
Desired phone line (string to be converted to a hash).
enum GNUNET_GenericReturnValue 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_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:1234
enum GNUNET_GenericReturnValue 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:520
enum GNUNET_GenericReturnValue 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:222
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1308
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:620
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:660
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_OPEN_READWRITE
Open the file for both reading and writing.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
#define GNUNET_CRYPTO_hash_from_string(enc, result)
Convert ASCII encoding back to struct GNUNET_HashCode
enum GNUNET_GenericReturnValue 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 GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:1701
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
struct GNUNET_GNSRECORD_Block * block
Block.
struct GNUNET_HashCode query
query
Handle used to access files (and pipes).

References _, FlatFileEntry::block, GNUNET_assert, GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_hash_from_string, GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_file_size(), GNUNET_DISK_file_test(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_log_config_missing(), GNUNET_malloc, GNUNET_NO, GNUNET_OK, GNUNET_STRINGS_base64_decode(), GNUNET_SYSERR, GNUNET_YES, line, plugin, FlatFileEntry::query, and size.

Referenced by libgnunet_plugin_namecache_flat_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ store_and_free_entries()

static int store_and_free_entries ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Store values in hashmap in file and free data.

Parameters
pluginthe plugin context

Definition at line 197 of file plugin_namecache_flat.c.

200{
201 struct GNUNET_DISK_FileHandle *fh = cls;
202 struct FlatFileEntry *entry = value;
203
204 char *line;
205 char *block_b64;
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);
214 &query);
216 "%s,%s\n",
217 (char *) &query,
218 block_b64);
219
220 GNUNET_free (block_b64);
221
223 line,
224 strlen (line));
225
226 GNUNET_free (entry->block);
227 GNUNET_free (entry);
229 return GNUNET_YES;
230}
static char * value
Value of the record to add/remove.
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:697
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
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:55
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1599
0-terminated ASCII encoding of a struct GNUNET_HashCode.

References FlatFileEntry::block, GNUNET_asprintf(), GNUNET_CRYPTO_hash_to_enc(), GNUNET_DISK_file_write(), GNUNET_free, GNUNET_GNSRECORD_block_get_size(), GNUNET_STRINGS_base64_encode(), GNUNET_YES, line, FlatFileEntry::query, and value.

Referenced by database_shutdown().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ database_shutdown()

static void database_shutdown ( struct Plugin plugin)
static

Shutdown database connection and associate data structures.

Parameters
pluginthe plugin context (state for this module)

Definition at line 239 of file plugin_namecache_flat.c.

240{
241 struct GNUNET_DISK_FileHandle *fh;
242
249 if (NULL == fh)
250 {
252 _ ("Unable to initialize file: %s.\n"),
253 plugin->fn);
254 return;
255 }
256
259 fh);
262}
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static int store_and_free_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
Store values in hashmap in file and free data.

References _, GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_OPEN_TRUNCATE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, plugin, and store_and_free_entries().

Referenced by libgnunet_plugin_namecache_flat_done(), and libgnunet_plugin_namecache_flat_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expire_blocks()

static int expire_blocks ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 266 of file plugin_namecache_flat.c.

269{
270 struct Plugin *plugin = cls;
271 struct FlatFileEntry *entry = value;
272 struct GNUNET_TIME_Absolute now;
274
277
279 expiration).rel_value_us)
280 {
282 }
283 return GNUNET_YES;
284}
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_block_get_expiration(const struct GNUNET_GNSRECORD_Block *block)
Returns the expiration of a block.
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.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
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:423
Time for absolute times used by GNUnet, in microseconds.
Handle for a plugin.
Definition: block.c:38

References FlatFileEntry::block, expiration, GNUNET_CONTAINER_multihashmap_remove_all(), GNUNET_GNSRECORD_block_get_expiration(), GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_get_difference(), GNUNET_YES, key, plugin, and value.

Referenced by namecache_expire_blocks().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namecache_expire_blocks()

static void namecache_expire_blocks ( struct Plugin plugin)
static

Removes any expired block.

Parameters
pluginthe plugin

Definition at line 293 of file plugin_namecache_flat.c.

294{
297 plugin);
298}
static int expire_blocks(void *cls, const struct GNUNET_HashCode *key, void *value)

References expire_blocks(), GNUNET_CONTAINER_multihashmap_iterate(), and plugin.

Referenced by namecache_cache_block().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namecache_cache_block()

static int namecache_cache_block ( void *  cls,
const struct GNUNET_GNSRECORD_Block block 
)
static

Cache a block in the datastore.

Parameters
clsclosure (internal context for the plugin)
blockblock to cache
Returns
GNUNET_OK on success, else GNUNET_SYSERR

Definition at line 309 of file plugin_namecache_flat.c.

311{
312 struct Plugin *plugin = cls;
313 struct GNUNET_HashCode query;
314 struct FlatFileEntry *entry;
315 size_t block_size;
316
319 &query);
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);
330 if (GNUNET_OK !=
332 &query,
333 entry,
335 {
336 GNUNET_free (entry);
337 GNUNET_break (0);
338 return GNUNET_SYSERR;
339 }
341 "Caching block under derived key `%s'\n",
343 return GNUNET_OK;
344}
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_query_from_block(const struct GNUNET_GNSRECORD_Block *block, struct GNUNET_HashCode *query)
Builds the query hash from a block.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
const char * GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
static void namecache_expire_blocks(struct Plugin *plugin)
Removes any expired block.
A 512-bit hashcode.

References FlatFileEntry::block, GNUNET_break, GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_multihashmap_remove_all(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_GNSRECORD_block_get_size(), GNUNET_GNSRECORD_query_from_block(), GNUNET_h2s_full(), GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, namecache_expire_blocks(), plugin, and FlatFileEntry::query.

Referenced by libgnunet_plugin_namecache_flat_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namecache_lookup_block()

static int namecache_lookup_block ( void *  cls,
const struct GNUNET_HashCode query,
GNUNET_NAMECACHE_BlockCallback  iter,
void *  iter_cls 
)
static

Get the block for a particular zone and label in the datastore.

Will return at most one result to the iterator.

Parameters
clsclosure (internal context for the plugin)
queryhash of public key derived from the zone and the label
iterfunction to call with the result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error

Definition at line 358 of file plugin_namecache_flat.c.

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;
369 "Found block under derived key `%s'\n",
370 GNUNET_h2s_full (query));
371 iter (iter_cls, block);
372 return GNUNET_YES;
373}
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.

References GNUNET_CONTAINER_multihashmap_get(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s_full(), GNUNET_log, GNUNET_NO, GNUNET_YES, and plugin.

Referenced by libgnunet_plugin_namecache_flat_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ libgnunet_plugin_namecache_flat_init()

void * libgnunet_plugin_namecache_flat_init ( void *  cls)

Entry point for the plugin.

Parameters
clsthe "struct GNUNET_NAMECACHE_PluginEnvironment*"
Returns
NULL on error, otherwise the plugin context

Definition at line 385 of file plugin_namecache_flat.c.

386{
387 static struct Plugin plugin;
388 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
390
391 if (NULL != plugin.cfg)
392 return NULL; /* can only initialize once! */
393 memset (&plugin, 0, sizeof(struct Plugin));
394 plugin.cfg = cfg;
396 {
398 return NULL;
399 }
401 api->cls = &plugin;
405 _ ("flat plugin running\n"));
406 return api;
407}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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.
static int namecache_cache_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
struct returned by the initialization function of the plugin
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.
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
void * cls
Closure to pass to all plugin functions.

References _, GNUNET_NAMECACHE_PluginFunctions::cache_block, cfg, GNUNET_NAMECACHE_PluginFunctions::cls, database_setup(), database_shutdown(), GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_new, GNUNET_OK, GNUNET_NAMECACHE_PluginFunctions::lookup_block, namecache_cache_block(), namecache_lookup_block(), and plugin.

Here is the call graph for this function:

◆ libgnunet_plugin_namecache_flat_done()

void * libgnunet_plugin_namecache_flat_done ( void *  cls)

Exit point from the plugin.

Parameters
clsthe plugin context (as returned by "init")
Returns
always NULL

Definition at line 419 of file plugin_namecache_flat.c.

420{
422 struct Plugin *plugin = api->cls;
423
425 plugin->cfg = NULL;
428 "flat plugin is finished\n");
429 return NULL;
430}
void * cls
Closure for all of the callbacks.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47

References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_NAMECACHE_PluginFunctions::cls, database_shutdown(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, and plugin.

Here is the call graph for this function: