GNUnet  0.10.x
Data Structures | Functions
plugin_namestore_flat.c File Reference

file-based namestore backend More...

#include "platform.h"
#include "gnunet_namestore_plugin.h"
#include "gnunet_namestore_service.h"
#include "gnunet_gnsrecord_lib.h"
#include "namestore.h"
Include dependency graph for plugin_namestore_flat.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 
struct  FlatFileEntry
 
struct  IterateContext
 Closure for iterate_zones. More...
 
struct  ZoneToNameContext
 Closure for zone_to_name. More...
 

Functions

static void hash_pkey_and_label (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, struct GNUNET_HashCode *h)
 Hash contactenation of pkey and label into h. More...
 
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 namestore_flat_store_records (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
 Store a record in the datastore. More...
 
static int namestore_flat_lookup_records (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
 Lookup records in the datastore for which we are the authority. More...
 
static int iterate_zones (void *cls, const struct GNUNET_HashCode *key, void *value)
 Helper function for namestore_flat_iterate_records(). More...
 
static int namestore_flat_iterate_records (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, uint64_t serial, uint64_t limit, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
 Iterate over the results for a particular key and zone in the datastore. More...
 
static int zone_to_name (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static int namestore_flat_zone_to_name (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
 Look for an existing PKEY delegation record for a given public key. More...
 
void * libgnunet_plugin_namestore_flat_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_namestore_flat_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

file-based namestore backend

Author
Martin Schanzenbach
Christian Grothoff

Definition in file plugin_namestore_flat.c.

Function Documentation

◆ hash_pkey_and_label()

static void hash_pkey_and_label ( const struct GNUNET_CRYPTO_EcdsaPrivateKey pkey,
const char *  label,
struct GNUNET_HashCode h 
)
static

Hash contactenation of pkey and label into h.

Parameters
pkeya key
labela label
h[out]initialized hash

Definition at line 92 of file plugin_namestore_flat.c.

References GNUNET_CRYPTO_hash(), GNUNET_free, GNUNET_malloc, GNUNET_memcpy, and Plugin::key.

Referenced by database_setup(), namestore_flat_lookup_records(), and namestore_flat_store_records().

95 {
96  char *key;
97  size_t label_len;
98  size_t key_len;
99 
100  label_len = strlen (label);
101  key_len = label_len + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
102  key = GNUNET_malloc (key_len);
103  GNUNET_memcpy (key,
104  label,
105  label_len);
106  GNUNET_memcpy (key + label_len,
107  pkey,
108  sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
109  GNUNET_CRYPTO_hash (key,
110  key_len,
111  h);
112  GNUNET_free (key);
113 }
Private ECC key encoded for transmission.
#define GNUNET_memcpy(dst, src, n)
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
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 125 of file plugin_namestore_flat.c.

References _, Plugin::cfg, fh, Plugin::fn, GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_close(), GNUNET_DISK_file_map(), GNUNET_DISK_file_open(), GNUNET_DISK_file_size(), GNUNET_DISK_file_test(), GNUNET_DISK_file_unmap(), GNUNET_DISK_MAP_TYPE_READ, 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_GNSRECORD_records_deserialize(), GNUNET_log, GNUNET_log_config_missing(), GNUNET_log_strerror, GNUNET_new, GNUNET_new_array, GNUNET_NO, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_base64_decode(), GNUNET_SYSERR, GNUNET_YES, hash_pkey_and_label(), Plugin::hm, FlatFileEntry::label, line, mh, FlatFileEntry::private_key, FlatFileEntry::record_count, record_count, FlatFileEntry::record_data, FlatFileEntry::rvalue, Plugin::size, and SIZE_MAX.

Referenced by libgnunet_plugin_namestore_flat_init().

126 {
127  char *flatdbfile;
128  char *record_data;
129  char *zone_private_key;
130  char *record_data_b64;
131  char *buffer;
132  char *line;
133  char *label;
134  char *rvalue;
135  char *record_count;
136  size_t record_data_size;
137  uint64_t size;
138  struct GNUNET_HashCode hkey;
139  struct GNUNET_DISK_FileHandle *fh;
140  struct FlatFileEntry *entry;
141  struct GNUNET_DISK_MapHandle *mh;
142 
143  if (GNUNET_OK !=
145  "namestore-flat",
146  "FILENAME",
147  &flatdbfile))
148  {
150  "namestore-flat",
151  "FILENAME");
152  return GNUNET_SYSERR;
153  }
154  if (GNUNET_OK !=
155  GNUNET_DISK_file_test (flatdbfile))
156  {
157  if (GNUNET_OK !=
159  {
160  GNUNET_break (0);
161  GNUNET_free (flatdbfile);
162  return GNUNET_SYSERR;
163  }
164  }
165  /* flatdbfile should be UTF-8-encoded. If it isn't, it's a bug */
166  plugin->fn = flatdbfile;
167 
168  /* Load data from file into hashmap */
170  GNUNET_NO);
171  fh = GNUNET_DISK_file_open (flatdbfile,
176  if (NULL == fh)
177  {
179  _("Unable to initialize file: %s.\n"),
180  flatdbfile);
181  return GNUNET_SYSERR;
182  }
183  if (GNUNET_SYSERR ==
184  GNUNET_DISK_file_size (flatdbfile,
185  &size,
186  GNUNET_YES,
187  GNUNET_YES))
188  {
190  _("Unable to get filesize: %s.\n"),
191  flatdbfile);
193  return GNUNET_SYSERR;
194  }
195  if (size > SIZE_MAX)
196  {
198  _("File too big to map: %llu bytes.\n"),
199  (unsigned long long) size);
201  return GNUNET_SYSERR;
202  }
203  if (0 == size)
204  {
206  return GNUNET_OK;
207  }
208  buffer = GNUNET_DISK_file_map (fh,
209  &mh,
211  size);
212  if (NULL == buffer)
213  {
215  "mmap");
217  return GNUNET_SYSERR;
218  }
219  if ('\0' != buffer[size-1])
220  {
222  _("Namestore database file `%s' malformed\n"),
223  flatdbfile);
226  return GNUNET_SYSERR;
227  }
228 
229  line = strtok (buffer, "\n");
230  while (NULL != line)
231  {
232  zone_private_key = strtok (line, ",");
233  if (NULL == zone_private_key)
234  break;
235  rvalue = strtok (NULL, ",");
236  if (NULL == rvalue)
237  break;
238  record_count = strtok (NULL, ",");
239  if (NULL == record_count)
240  break;
241  record_data_b64 = strtok (NULL, ",");
242  if (NULL == record_data_b64)
243  break;
244  label = strtok (NULL, ",");
245  if (NULL == label)
246  break;
247  line = strtok (NULL, "\n");
248  entry = GNUNET_new (struct FlatFileEntry);
249  {
250  unsigned long long ll;
251 
252  if (1 != sscanf (rvalue,
253  "%llu",
254  &ll))
255  {
257  "Error parsing entry\n");
258  GNUNET_free (entry);
259  break;
260  }
261  entry->rvalue = (uint64_t) ll;
262  }
263  {
264  unsigned int ui;
265 
266  if (1 != sscanf (record_count,
267  "%u",
268  &ui))
269  {
271  "Error parsing entry\n");
272  GNUNET_free (entry);
273  break;
274  }
275  entry->record_count = (uint32_t) ui;
276  }
277  entry->label = GNUNET_strdup (label);
278  record_data_size
279  = GNUNET_STRINGS_base64_decode (record_data_b64,
280  strlen (record_data_b64),
281  (void **) &record_data);
282  entry->record_data =
284  struct GNUNET_GNSRECORD_Data);
285  if (GNUNET_OK !=
286  GNUNET_GNSRECORD_records_deserialize (record_data_size,
287  record_data,
288  entry->record_count,
289  entry->record_data))
290  {
292  "Unable to deserialize record %s\n",
293  label);
294  GNUNET_free (entry->label);
295  GNUNET_free (entry);
296  GNUNET_free (record_data);
297  break;
298  }
299  GNUNET_free (record_data);
300 
301  {
302  struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
303 
304  GNUNET_STRINGS_base64_decode (zone_private_key,
305  strlen (zone_private_key),
306  (void**)&private_key);
307  entry->private_key = *private_key;
308  GNUNET_free (private_key);
309  }
310 
312  label,
313  &hkey);
314  if (GNUNET_OK !=
316  &hkey,
317  entry,
319  {
320  GNUNET_free (entry);
321  GNUNET_break (0);
322  }
323  }
326  return GNUNET_OK;
327 }
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:669
static void hash_pkey_and_label(const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, struct GNUNET_HashCode *h)
Hash contactenation of pkey and label into h.
Create file if it doesn't exist.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
void * GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m, enum GNUNET_DISK_MapType access, size_t len)
Map a file into memory.
Definition: disk.c:2020
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
uint64_t rvalue
Rvalue.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:833
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Private ECC key encoded for transmission.
int GNUNET_GNSRECORD_records_deserialize(size_t len, const char *src, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *dest)
Deserialize the given records to the given destination.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define SIZE_MAX
Definition: platform.h:250
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static uint64_t record_count
Record count.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
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.
Read-only memory map.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
char * label
Label.
static int fh
Handle to the unique file.
A 512-bit hashcode.
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:2067
There must only be one value per key; storing a value should fail if a value under the same key alrea...
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
int GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:2100
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
uint32_t record_count
Record cound.
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.
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:289
char * fn
Filename used for the DB.
struct GNUNET_GNSRECORD_Data * record_data
Record data.
#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.
#define GNUNET_YES
Definition: gnunet_common.h:80
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:1673
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1985
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
Entry zone.
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
keykey in the map
valuea struct FlatFileEntry

Definition at line 338 of file plugin_namestore_flat.c.

References data, data_size, fh, GNUNET_asprintf(), GNUNET_break, GNUNET_DISK_file_write(), GNUNET_free, GNUNET_GNSRECORD_records_get_size(), GNUNET_GNSRECORD_records_serialize(), GNUNET_STRINGS_base64_encode(), GNUNET_SYSERR, GNUNET_YES, FlatFileEntry::label, line, FlatFileEntry::private_key, FlatFileEntry::record_count, FlatFileEntry::record_data, ret, FlatFileEntry::rvalue, and value.

Referenced by database_shutdown().

341 {
342  struct GNUNET_DISK_FileHandle *fh = cls;
343  struct FlatFileEntry *entry = value;
344  char *line;
345  char *zone_private_key;
346  char *record_data_b64;
347  ssize_t data_size;
348 
349  (void) key;
351  sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
352  &zone_private_key);
354  entry->record_data);
355  if (data_size < 0)
356  {
357  GNUNET_break (0);
358  GNUNET_free (zone_private_key);
359  return GNUNET_SYSERR;
360  }
361  if (data_size >= UINT16_MAX)
362  {
363  GNUNET_break (0);
364  GNUNET_free (zone_private_key);
365  return GNUNET_SYSERR;
366  }
367  {
368  char data[data_size];
369  ssize_t ret;
370 
372  entry->record_data,
373  data_size,
374  data);
375  if ( (ret < 0) ||
376  (data_size != ret) )
377  {
378  GNUNET_break (0);
379  GNUNET_free (zone_private_key);
380  return GNUNET_SYSERR;
381  }
383  data_size,
384  &record_data_b64);
385  }
386  GNUNET_asprintf (&line,
387  "%s,%llu,%u,%s,%s\n",
388  zone_private_key,
389  (unsigned long long) entry->rvalue,
390  (unsigned int) entry->record_count,
391  record_data_b64,
392  entry->label);
393  GNUNET_free (record_data_b64);
394  GNUNET_free (zone_private_key);
395 
397  line,
398  strlen (line));
399 
400  GNUNET_free (line);
401  GNUNET_free (entry->label);
402  GNUNET_free (entry->record_data);
403  GNUNET_free (entry);
404  return GNUNET_YES;
405 }
GNUNET_NETWORK_STRUCT_END ssize_t GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Calculate how many bytes we will need to serialize the given records.
ssize_t GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, size_t dest_size, char *dest)
Serialize the given records to the given destination buffer.
uint64_t rvalue
Rvalue.
Private ECC key encoded for transmission.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
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).
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:1049
char * label
Label.
static int fh
Handle to the unique file.
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:2004
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
uint32_t record_count
Record cound.
struct GNUNET_GNSRECORD_Data * record_data
Record data.
#define GNUNET_YES
Definition: gnunet_common.h:80
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
Handle used to access files (and pipes).
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
Entry zone.
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 414 of file plugin_namestore_flat.c.

References _, fh, Plugin::fn, GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_write(), 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::hm, and store_and_free_entries().

Referenced by libgnunet_plugin_namestore_flat_done(), and libgnunet_plugin_namestore_flat_init().

415 {
416  struct GNUNET_DISK_FileHandle *fh;
417 
418  fh = GNUNET_DISK_file_open (plugin->fn,
424  if (NULL == fh)
425  {
427  _("Unable to initialize file: %s.\n"),
428  plugin->fn);
429  return;
430  }
431 
434  fh);
436  /* append 0-terminator */
438  "",
439  1);
441 }
Create file if it doesn&#39;t exist.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_HashMapIterator it, void *it_cls)
Iterate over all entries in the map.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
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:1049
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static int fh
Handle to the unique file.
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
char * fn
Filename used for the DB.
#define GNUNET_log(kind,...)
Open the file for both reading and writing.
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:1673
Handle used to access files (and pipes).
static int store_and_free_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
Store values in hashmap in file and free data.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ namestore_flat_store_records()

static int namestore_flat_store_records ( void *  cls,
const struct GNUNET_CRYPTO_EcdsaPrivateKey zone_key,
const char *  label,
unsigned int  rd_count,
const struct GNUNET_GNSRECORD_Data rd 
)
static

Store a record in the datastore.

Removes any existing record in the same zone with the same name.

Parameters
clsclosure (internal context for the plugin)
zone_keyprivate key of the zone
labelname that is being mapped (at most 255 characters long)
rd_countnumber of entries in rd array
rdarray of records with data to store
Returns
GNUNET_OK on success, else GNUNET_SYSERR

Definition at line 456 of file plugin_namestore_flat.c.

References GNUNET_GNSRECORD_Data::data, data_size, GNUNET_GNSRECORD_Data::data_size, GNUNET_GNSRECORD_Data::expiration_time, GNUNET_GNSRECORD_Data::flags, GNUNET_asprintf(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_multihashmap_remove_all(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log_from, GNUNET_malloc, GNUNET_memcpy, GNUNET_new, GNUNET_new_array, GNUNET_OK, hash_pkey_and_label(), Plugin::hm, FlatFileEntry::label, plugin, FlatFileEntry::private_key, FlatFileEntry::record_count, FlatFileEntry::record_data, GNUNET_GNSRECORD_Data::record_type, and FlatFileEntry::rvalue.

Referenced by libgnunet_plugin_namestore_flat_init().

461 {
462  struct Plugin *plugin = cls;
463  uint64_t rvalue;
464  struct GNUNET_HashCode hkey;
465  struct FlatFileEntry *entry;
466 
468  UINT64_MAX);
469  hash_pkey_and_label (zone_key,
470  label,
471  &hkey);
473  &hkey);
474  if (0 == rd_count)
475  {
477  "sqlite",
478  "Record deleted\n");
479  return GNUNET_OK;
480  }
481  entry = GNUNET_new (struct FlatFileEntry);
482  GNUNET_asprintf (&entry->label,
483  label,
484  strlen (label));
485  GNUNET_memcpy (&entry->private_key,
486  zone_key,
487  sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
488  entry->rvalue = rvalue;
489  entry->record_count = rd_count;
490  entry->record_data = GNUNET_new_array (rd_count,
491  struct GNUNET_GNSRECORD_Data);
492  for (unsigned int i = 0; i < rd_count; i++)
493  {
494  entry->record_data[i].expiration_time = rd[i].expiration_time;
495  entry->record_data[i].record_type = rd[i].record_type;
496  entry->record_data[i].flags = rd[i].flags;
497  entry->record_data[i].data_size = rd[i].data_size;
498  entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
499  GNUNET_memcpy ((char*)entry->record_data[i].data,
500  rd[i].data,
501  rd[i].data_size);
502  }
503  return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
504  &hkey,
505  entry,
507 }
static void hash_pkey_and_label(const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, struct GNUNET_HashCode *h)
Hash contactenation of pkey and label into h.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
uint64_t rvalue
Rvalue.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
size_t data_size
Number of bytes in data.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Private ECC key encoded for transmission.
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.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_memcpy(dst, src, n)
const void * data
Binary value stored in the DNS record.
uint64_t expiration_time
Expiration time for the DNS record.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
char * label
Label.
A 512-bit hashcode.
There must only be one value per key; storing a value should fail if a value under the same key alrea...
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
uint32_t record_count
Record cound.
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 GNUNET_GNSRECORD_Data * record_data
Record data.
uint32_t record_type
Type of the GNS/DNS record.
Handle for a plugin.
Definition: block.c:37
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
#define GNUNET_log_from(kind, comp,...)
static size_t data_size
Number of bytes in data.
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_malloc(size)
Wrapper around malloc.
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
Entry zone.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ namestore_flat_lookup_records()

static int namestore_flat_lookup_records ( void *  cls,
const struct GNUNET_CRYPTO_EcdsaPrivateKey zone,
const char *  label,
GNUNET_NAMESTORE_RecordIterator  iter,
void *  iter_cls 
)
static

Lookup records in the datastore for which we are the authority.

Parameters
clsclosure (internal context for the plugin)
zoneprivate key of the zone
labelname of the record in the zone
iterfunction to call with the result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_NO for no results, else GNUNET_SYSERR

Definition at line 521 of file plugin_namestore_flat.c.

References GNUNET_break, GNUNET_CONTAINER_multihashmap_get(), GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, hash_pkey_and_label(), Plugin::hm, Plugin::iter, FlatFileEntry::label, plugin, FlatFileEntry::private_key, FlatFileEntry::record_count, and FlatFileEntry::record_data.

Referenced by libgnunet_plugin_namestore_flat_init().

526 {
527  struct Plugin *plugin = cls;
528  struct FlatFileEntry *entry;
529  struct GNUNET_HashCode hkey;
530 
531  if (NULL == zone)
532  {
533  GNUNET_break (0);
534  return GNUNET_SYSERR;
535  }
536  hash_pkey_and_label (zone,
537  label,
538  &hkey);
539  entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm,
540  &hkey);
541 
542  if (NULL == entry)
543  return GNUNET_NO;
544  if (NULL != iter)
545  iter (iter_cls,
546  1, /* zero is illegal */
547  &entry->private_key,
548  entry->label,
549  entry->record_count,
550  entry->record_data);
551  return GNUNET_YES;
552 }
static void hash_pkey_and_label(const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, struct GNUNET_HashCode *h)
Hash contactenation of pkey and label into h.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
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.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
char * label
Label.
A 512-bit hashcode.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
uint32_t record_count
Record cound.
struct GNUNET_GNSRECORD_Data * record_data
Record data.
Handle for a plugin.
Definition: block.c:37
#define GNUNET_YES
Definition: gnunet_common.h:80
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
Entry zone.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate_zones()

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

Helper function for namestore_flat_iterate_records().

Parameters
clsa struct IterateContext
keyunused
valuea struct FlatFileEntry
Returns
GNUNET_YES to continue the iteration

Definition at line 603 of file plugin_namestore_flat.c.

References GNUNET_memcmp, GNUNET_NO, GNUNET_YES, IterateContext::iter, IterateContext::iter_cls, FlatFileEntry::label, IterateContext::limit, IterateContext::offset, IterateContext::pos, FlatFileEntry::private_key, FlatFileEntry::record_count, FlatFileEntry::record_data, value, and IterateContext::zone.

Referenced by namestore_flat_iterate_records().

606 {
607  struct IterateContext *ic = cls;
608  struct FlatFileEntry *entry = value;
609 
610  (void) key;
611  if (0 == ic->limit)
612  return GNUNET_NO;
613  if ( (NULL != ic->zone) &&
614  (0 != GNUNET_memcmp (&entry->private_key,
615  ic->zone)) )
616  return GNUNET_YES;
617  ic->pos++;
618  if (ic->offset > 0)
619  {
620  ic->offset--;
621  return GNUNET_YES;
622  }
623  ic->iter (ic->iter_cls,
624  ic->pos,
625  (NULL == ic->zone)
626  ? &entry->private_key
627  : ic->zone,
628  entry->label,
629  entry->record_count,
630  entry->record_data);
631  ic->limit--;
632  if (0 == ic->limit)
633  return GNUNET_NO;
634  return GNUNET_YES;
635 }
uint64_t offset
How many more records should we skip before returning results?
const struct GNUNET_CRYPTO_EcdsaPrivateKey * zone
Target zone.
#define GNUNET_NO
Definition: gnunet_common.h:81
Closure for iterate_zones.
uint64_t limit
How many more records should we return?
GNUNET_NAMESTORE_RecordIterator iter
Function to call on each record.
static char * value
Value of the record to add/remove.
char * label
Label.
uint32_t record_count
Record cound.
void * iter_cls
Closure for iter.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
struct GNUNET_GNSRECORD_Data * record_data
Record data.
#define GNUNET_YES
Definition: gnunet_common.h:80
uint64_t pos
What is the position of the current entry, counting starts from 1.
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
Entry zone.
Here is the caller graph for this function:

◆ namestore_flat_iterate_records()

static int namestore_flat_iterate_records ( void *  cls,
const struct GNUNET_CRYPTO_EcdsaPrivateKey zone,
uint64_t  serial,
uint64_t  limit,
GNUNET_NAMESTORE_RecordIterator  iter,
void *  iter_cls 
)
static

Iterate over the results for a particular key and zone in the datastore.

Will return at most one result to the iterator.

Parameters
clsclosure (internal context for the plugin)
zonehash of public key of the zone, NULL to iterate over all zones
serialserial number to exclude in the list of all matching records
limitmaximum number of results to return to iter
iterfunction to call with the result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_NO if there were no more results, GNUNET_SYSERR on error

Definition at line 651 of file plugin_namestore_flat.c.

References GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_NO, GNUNET_OK, Plugin::hm, Plugin::iter, IterateContext::iter, Plugin::iter_cls, IterateContext::iter_cls, iterate_zones(), IterateContext::limit, IterateContext::offset, plugin, IterateContext::pos, zone, and IterateContext::zone.

Referenced by libgnunet_plugin_namestore_flat_init().

657 {
658  struct Plugin *plugin = cls;
659  struct IterateContext ic;
660 
661  ic.offset = serial;
662  ic.pos = 0;
663  ic.limit = limit;
664  ic.iter = iter;
665  ic.iter_cls = iter_cls;
666  ic.zone = zone;
668  &iterate_zones,
669  &ic);
670  return (0 == ic.limit) ? GNUNET_OK : GNUNET_NO;
671 }
static int iterate_zones(void *cls, const struct GNUNET_HashCode *key, void *value)
Helper function for namestore_flat_iterate_records().
uint64_t offset
How many more records should we skip before returning results?
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_HashMapIterator it, void *it_cls)
Iterate over all entries in the map.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Closure for iterate_zones.
uint64_t limit
How many more records should we return?
static char * zone
Name of the zone we manage.
GNUNET_NAMESTORE_RecordIterator iter
Function to call on each record.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
void * iter_cls
Closure for iter.
Handle for a plugin.
Definition: block.c:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ zone_to_name()

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

Definition at line 689 of file plugin_namestore_flat.c.

References GNUNET_GNSRECORD_Data::data, GNUNET_GNSRECORD_TYPE_PKEY, GNUNET_memcmp, GNUNET_YES, ZoneToNameContext::iter, ZoneToNameContext::iter_cls, FlatFileEntry::label, FlatFileEntry::private_key, FlatFileEntry::record_count, FlatFileEntry::record_data, GNUNET_GNSRECORD_Data::record_type, ZoneToNameContext::result_found, value, ZoneToNameContext::value_zone, and ZoneToNameContext::zone.

Referenced by run().

692 {
693  struct ZoneToNameContext *ztn = cls;
694  struct FlatFileEntry *entry = value;
695 
696  (void) key;
697  if (0 != GNUNET_memcmp (&entry->private_key,
698  ztn->zone))
699  return GNUNET_YES;
700 
701  for (unsigned int i = 0; i < entry->record_count; i++)
702  {
704  continue;
705  if (0 == memcmp (ztn->value_zone,
706  entry->record_data[i].data,
707  sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
708  {
709  ztn->iter (ztn->iter_cls,
710  i + 1, /* zero is illegal! */
711  &entry->private_key,
712  entry->label,
713  entry->record_count,
714  entry->record_data);
715  ztn->result_found = GNUNET_YES;
716  }
717  }
718  return GNUNET_YES;
719 }
GNUNET_NAMESTORE_RecordIterator iter
const struct GNUNET_CRYPTO_EcdsaPublicKey * value_zone
const void * data
Binary value stored in the DNS record.
static char * value
Value of the record to add/remove.
#define GNUNET_GNSRECORD_TYPE_PKEY
Record type for GNS zone transfer ("PKEY").
char * label
Label.
Closure for zone_to_name.
uint32_t record_count
Record cound.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
const struct GNUNET_CRYPTO_EcdsaPrivateKey * zone
struct GNUNET_GNSRECORD_Data * record_data
Record data.
uint32_t record_type
Type of the GNS/DNS record.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_YES
Definition: gnunet_common.h:80
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
Entry zone.
Here is the caller graph for this function:

◆ namestore_flat_zone_to_name()

static int namestore_flat_zone_to_name ( void *  cls,
const struct GNUNET_CRYPTO_EcdsaPrivateKey zone,
const struct GNUNET_CRYPTO_EcdsaPublicKey value_zone,
GNUNET_NAMESTORE_RecordIterator  iter,
void *  iter_cls 
)
static

Look for an existing PKEY delegation record for a given public key.

Returns at most one result to the iterator.

Parameters
clsclosure (internal context for the plugin)
zoneprivate key of the zone to look up in, never NULL
value_zonepublic key of the target zone (value), never NULL
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 734 of file plugin_namestore_flat.c.

References GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_GNSRECORD_z2s(), GNUNET_log, GNUNET_NO, Plugin::hm, Plugin::iter, ZoneToNameContext::iter, Plugin::iter_cls, plugin, ZoneToNameContext::result_found, ZoneToNameContext::value_zone, zone, and Plugin::zone_to_name.

Referenced by libgnunet_plugin_namestore_flat_init().

739 {
740  struct Plugin *plugin = cls;
741  struct ZoneToNameContext ztn = {
742  .iter = iter,
743  .iter_cls = iter_cls,
744  .zone = zone,
745  .value_zone = value_zone,
746  .result_found = GNUNET_NO
747  };
748 
750  "Performing reverse lookup for `%s'\n",
751  GNUNET_GNSRECORD_z2s (value_zone));
753  &zone_to_name,
754  &ztn);
755  return ztn.result_found;
756 }
GNUNET_NAMESTORE_RecordIterator iter
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_HashMapIterator it, void *it_cls)
Iterate over all entries in the map.
#define GNUNET_NO
Definition: gnunet_common.h:81
static char * zone
Name of the zone we manage.
const struct GNUNET_CRYPTO_EcdsaPublicKey * value_zone
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
Closure for zone_to_name.
const char * GNUNET_GNSRECORD_z2s(const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
Convert a zone key to a string (for printing debug messages).
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
static int zone_to_name(void *cls, const struct GNUNET_HashCode *key, void *value)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ libgnunet_plugin_namestore_flat_init()

void* libgnunet_plugin_namestore_flat_init ( void *  cls)

Entry point for the plugin.

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

Definition at line 766 of file plugin_namestore_flat.c.

References _, Plugin::api, Plugin::cfg, GNUNET_NAMESTORE_PluginFunctions::cls, database_setup(), database_shutdown(), GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_new, GNUNET_OK, GNUNET_NAMESTORE_PluginFunctions::iterate_records, GNUNET_NAMESTORE_PluginFunctions::lookup_records, namestore_flat_iterate_records(), namestore_flat_lookup_records(), namestore_flat_store_records(), namestore_flat_zone_to_name(), plugin, GNUNET_NAMESTORE_PluginFunctions::store_records, and GNUNET_NAMESTORE_PluginFunctions::zone_to_name.

767 {
768  static struct Plugin plugin;
769  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
771 
772  if (NULL != plugin.cfg)
773  return NULL; /* can only initialize once! */
774  memset (&plugin,
775  0,
776  sizeof (struct Plugin));
777  plugin.cfg = cfg;
778  if (GNUNET_OK != database_setup (&plugin))
779  {
781  return NULL;
782  }
784  api->cls = &plugin;
790  _("Flat file database running\n"));
791  return api;
792 }
static int namestore_flat_lookup_records(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Lookup records in the datastore for which we are the authority.
int(* zone_to_name)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Look for an existing PKEY delegation record for a given public key.
static int namestore_flat_iterate_records(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, uint64_t serial, uint64_t limit, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Iterate over the results for a particular key and zone in the datastore.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static int namestore_flat_store_records(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Store a record in the datastore.
void * cls
Closure to pass to all plugin functions.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
int(* lookup_records)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Lookup records in the datastore for which we are the authority.
configuration data
Definition: configuration.c:85
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
static int namestore_flat_zone_to_name(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Look for an existing PKEY delegation record for a given public key.
int(* store_records)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Store a record in the datastore for which we are the authority.
struct returned by the initialization function of the plugin
int(* iterate_records)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, uint64_t serial, uint64_t limit, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Iterate over the results for a particular zone in the datastore.
Here is the call graph for this function:

◆ libgnunet_plugin_namestore_flat_done()

void* libgnunet_plugin_namestore_flat_done ( void *  cls)

Exit point from the plugin.

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

Definition at line 802 of file plugin_namestore_flat.c.

References Plugin::api, Plugin::cfg, GNUNET_NAMESTORE_PluginFunctions::cls, database_shutdown(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, and plugin.

803 {
805  struct Plugin *plugin = api->cls;
806 
807  database_shutdown (plugin);
808  plugin->cfg = NULL;
809  GNUNET_free (api);
811  "Flat file plugin is finished\n");
812  return NULL;
813 }
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
void * cls
Closure to pass to all plugin functions.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
struct returned by the initialization function of the plugin
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function: