fix ICMP IPv4/IPv6 type conversion logic
[oweals/gnunet.git] / src / peerstore / plugin_peerstore_flat.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2015 Christian Grothoff (and other contributing authors)
4  *
5  * GNUnet is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your
8  * 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with GNUnet; see the file COPYING.  If not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file peerstore/plugin_peerstore_flat.c
23  * @brief flat file-based peerstore backend
24  * @author Martin Schanzenbach
25  */
26
27 #include "platform.h"
28 #include "gnunet_peerstore_plugin.h"
29 #include "gnunet_peerstore_service.h"
30 #include "peerstore.h"
31
32 /**
33  * Context for all functions in this plugin.
34  */
35 struct Plugin
36 {
37
38   /**
39    * Configuration handle
40    */
41   const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43   /**
44    * HashMap
45    */
46   struct GNUNET_CONTAINER_MultiHashMap *hm;
47
48   /**
49    * Iterator
50    */
51   GNUNET_PEERSTORE_Processor iter;
52
53   /**
54    * Iterator cls
55    */
56   void *iter_cls;
57
58   /**
59    * iterator key
60    */
61   const char *iter_key;
62
63   /**
64    * Iterator peer
65    */
66   const struct GNUNET_PeerIdentity *iter_peer;
67
68   /**
69    * Iterator subsystem
70    */
71   const char *iter_sub_system;
72
73   /**
74    * Iterator time
75    */
76   struct GNUNET_TIME_Absolute iter_now;
77
78   /**
79    * Deleted entries
80    */
81   uint64_t deleted_entries;
82
83   /**
84    * Expired entries
85    */
86   uint64_t exp_changes;
87
88   /**
89    * Database filename.
90    */
91   char *fn;
92
93   /**
94    * Result found bool
95    */
96   int iter_result_found;
97
98 };
99
100
101 static int
102 delete_entries (void *cls,
103                 const struct GNUNET_HashCode *key,
104                 void *value)
105 {
106   struct Plugin *plugin = cls;
107   struct GNUNET_PEERSTORE_Record *entry = value;
108   if (0 != strcmp (plugin->iter_key, entry->key))
109     return GNUNET_YES;
110   if (0 != memcmp (plugin->iter_peer, entry->peer, sizeof (struct GNUNET_PeerIdentity)))
111     return GNUNET_YES;
112   if (0 != strcmp (plugin->iter_sub_system, entry->sub_system))
113     return GNUNET_YES;
114
115   GNUNET_CONTAINER_multihashmap_remove (plugin->hm, key, value);
116   plugin->deleted_entries++;
117   return GNUNET_YES;
118 }
119
120
121 /**
122  * Delete records with the given key
123  *
124  * @param cls closure (internal context for the plugin)
125  * @param sub_system name of sub system
126  * @param peer Peer identity (can be NULL)
127  * @param key entry key string (can be NULL)
128  * @return number of deleted records
129  */
130 static int
131 peerstore_flat_delete_records (void *cls, const char *sub_system,
132                                const struct GNUNET_PeerIdentity *peer,
133                                const char *key)
134 {
135   struct Plugin *plugin = cls;
136
137   plugin->iter_sub_system = sub_system;
138   plugin->iter_peer = peer;
139   plugin->iter_key = key;
140   plugin->deleted_entries = 0;
141
142   GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
143                                          &delete_entries,
144                                          plugin);
145   return plugin->deleted_entries;
146 }
147
148 static int
149 expire_entries (void *cls,
150                 const struct GNUNET_HashCode *key,
151                 void *value)
152 {
153   struct Plugin *plugin = cls;
154   struct GNUNET_PEERSTORE_Record *entry = value;
155
156   if (entry->expiry->abs_value_us < plugin->iter_now.abs_value_us)
157   {
158     GNUNET_CONTAINER_multihashmap_remove (plugin->hm, key, value);
159     plugin->exp_changes++;
160   }
161   return GNUNET_YES;
162 }
163
164
165
166 /**
167  * Delete expired records (expiry < now)
168  *
169  * @param cls closure (internal context for the plugin)
170  * @param now time to use as reference
171  * @param cont continuation called with the number of records expired
172  * @param cont_cls continuation closure
173  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and cont is not
174  * called
175  */
176 static int
177 peerstore_flat_expire_records (void *cls, struct GNUNET_TIME_Absolute now,
178                                GNUNET_PEERSTORE_Continuation cont,
179                                void *cont_cls)
180 {
181   struct Plugin *plugin = cls;
182   plugin->exp_changes = 0;
183   plugin->iter_now = now;
184
185   GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
186                                          &expire_entries,
187                                          plugin);
188   if (NULL != cont)
189   {
190     cont (cont_cls, plugin->exp_changes);
191   }
192   return GNUNET_OK;
193
194 }
195
196
197 static int
198 iterate_entries (void *cls,
199                  const struct GNUNET_HashCode *key,
200                  void *value)
201 {
202   struct Plugin *plugin = cls;
203   struct GNUNET_PEERSTORE_Record *entry = value;
204
205   if ((NULL != plugin->iter_peer) &&
206       (0 != memcmp (plugin->iter_peer,
207                     entry->peer,
208                     sizeof (struct GNUNET_PeerIdentity))))
209   {
210     return GNUNET_YES;
211   }
212   if ((NULL != plugin->iter_key) &&
213       (0 != strcmp (plugin->iter_key,
214                     entry->key)))
215   {
216     return GNUNET_YES;
217   }
218   if (NULL != plugin->iter)
219     plugin->iter (plugin->iter_cls, entry, NULL);
220   plugin->iter_result_found = GNUNET_YES;
221   return GNUNET_YES;
222 }
223
224 /**
225  * Iterate over the records given an optional peer id
226  * and/or key.
227  *
228  * @param cls closure (internal context for the plugin)
229  * @param sub_system name of sub system
230  * @param peer Peer identity (can be NULL)
231  * @param key entry key string (can be NULL)
232  * @param iter function to call asynchronously with the results, terminated
233  * by a NULL result
234  * @param iter_cls closure for @a iter
235  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and iter is not
236  * called
237  */
238 static int
239 peerstore_flat_iterate_records (void *cls, const char *sub_system,
240                                 const struct GNUNET_PeerIdentity *peer,
241                                 const char *key,
242                                 GNUNET_PEERSTORE_Processor iter,
243                                 void *iter_cls)
244 {
245   struct Plugin *plugin = cls;
246   plugin->iter = iter;
247   plugin->iter_cls = iter_cls;
248   plugin->iter_peer = peer;
249   plugin->iter_sub_system = sub_system;
250   plugin->iter_key = key;
251
252   GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
253                                          &iterate_entries,
254                                          plugin);
255   if (NULL != iter)
256     iter (iter_cls, NULL, NULL);
257   return GNUNET_OK;
258 }
259
260
261 /**
262  * Store a record in the peerstore.
263  * Key is the combination of sub system and peer identity.
264  * One key can store multiple values.
265  *
266  * @param cls closure (internal context for the plugin)
267  * @param sub_system name of the GNUnet sub system responsible
268  * @param peer peer identity
269  * @param key record key string
270  * @param value value to be stored
271  * @param size size of value to be stored
272  * @param expiry absolute time after which the record is (possibly) deleted
273  * @param options options related to the store operation
274  * @param cont continuation called when record is stored
275  * @param cont_cls continuation closure
276  * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called
277  */
278 static int
279 peerstore_flat_store_record (void *cls, const char *sub_system,
280                              const struct GNUNET_PeerIdentity *peer,
281                              const char *key, const void *value, size_t size,
282                              struct GNUNET_TIME_Absolute expiry,
283                              enum GNUNET_PEERSTORE_StoreOption options,
284                              GNUNET_PEERSTORE_Continuation cont,
285                              void *cont_cls)
286 {
287   struct Plugin *plugin = cls;
288   struct GNUNET_HashCode hkey;
289   struct GNUNET_PEERSTORE_Record *entry;
290   const char *peer_id;
291
292
293   entry = GNUNET_new (struct GNUNET_PEERSTORE_Record);
294   entry->sub_system = GNUNET_strdup (sub_system);
295   entry->key = GNUNET_strdup (key);
296   entry->value = GNUNET_malloc (size);
297   GNUNET_memcpy (entry->value, value, size);
298   entry->value_size = size;
299   entry->peer = GNUNET_new (struct GNUNET_PeerIdentity);
300   GNUNET_memcpy (entry->peer, peer, sizeof (struct GNUNET_PeerIdentity));
301   entry->expiry = GNUNET_new (struct GNUNET_TIME_Absolute);
302   entry->expiry->abs_value_us = expiry.abs_value_us;
303
304   peer_id = GNUNET_i2s (peer);
305   GNUNET_CRYPTO_hash (peer_id,
306                       strlen (peer_id),
307                       &hkey);
308
309   if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options)
310   {
311     peerstore_flat_delete_records (cls, sub_system, peer, key);
312   }
313
314   GNUNET_CONTAINER_multihashmap_put (plugin->hm,
315                                      &hkey,
316                                      entry,
317                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
318   if (NULL != cont)
319   {
320     cont (cont_cls, GNUNET_OK);
321   }
322   return GNUNET_OK;
323 }
324
325
326 /**
327  * Initialize the database connections and associated
328  * data structures (create tables and indices
329  * as needed as well).
330  *
331  * @param plugin the plugin context (state for this module)
332  * @return GNUNET_OK on success
333  */
334 static int
335 database_setup (struct Plugin *plugin)
336 {
337   char *afsdir;
338   char *key;
339   char *sub_system;
340   const char *peer_id;
341   char *peer;
342   char *value;
343   char *expiry;
344   struct GNUNET_DISK_FileHandle *fh;
345   struct GNUNET_PEERSTORE_Record *entry;
346   struct GNUNET_HashCode hkey;
347   size_t size;
348   char *buffer;
349   char *line;
350
351   if (GNUNET_OK !=
352       GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-flat",
353                                                "FILENAME", &afsdir))
354   {
355     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "peerstore-flat",
356                                "FILENAME");
357     return GNUNET_SYSERR;
358   }
359   if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
360   {
361     if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
362     {
363       GNUNET_break (0);
364       GNUNET_free (afsdir);
365       return GNUNET_SYSERR;
366     }
367   }
368   /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
369   plugin->fn = afsdir;
370
371   fh = GNUNET_DISK_file_open (afsdir,
372                               GNUNET_DISK_OPEN_CREATE |
373                               GNUNET_DISK_OPEN_READWRITE,
374                               GNUNET_DISK_PERM_USER_WRITE |
375                               GNUNET_DISK_PERM_USER_READ);
376   if (NULL == fh)
377   {
378     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379                 _("Unable to initialize file: %s.\n"),
380                 afsdir);
381     return GNUNET_SYSERR;
382   }
383
384   /* Load data from file into hashmap */
385   plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
386                                                      GNUNET_NO);
387
388   if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
389                                               &size,
390                                               GNUNET_YES,
391                                               GNUNET_YES))
392   {
393     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394                 _("Unable to get filesize: %s.\n"),
395                 afsdir);
396     return GNUNET_SYSERR;
397   }
398
399   buffer = GNUNET_malloc (size + 1);
400
401   if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
402                                               buffer,
403                                               size))
404   {
405     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
406                 _("Unable to read file: %s.\n"),
407                 afsdir);
408     GNUNET_DISK_file_close (fh);
409     GNUNET_free (buffer);
410     return GNUNET_SYSERR;
411   }
412   
413   buffer[size] = '\0';
414   GNUNET_DISK_file_close (fh);
415   if (0 < size) {
416     line = strtok (buffer, "\n");
417     while (line != NULL) {
418       sub_system = strtok (line, ",");
419       if (NULL == sub_system)
420         break;
421       peer = strtok (NULL, ",");
422       if (NULL == peer)
423         break;
424       key = strtok (NULL, ",");
425       if (NULL == key)
426         break;
427       value = strtok (NULL, ",");
428       if (NULL == value)
429         break;
430       expiry = strtok (NULL, ",");
431       if (NULL == expiry)
432         break;
433       entry = GNUNET_new (struct GNUNET_PEERSTORE_Record);
434       entry->sub_system = GNUNET_strdup (sub_system);
435       entry->key = GNUNET_strdup (key);
436       GNUNET_STRINGS_base64_decode (peer,
437                                     strlen (peer),
438                                     (char**)&entry->peer);
439       entry->value_size = GNUNET_STRINGS_base64_decode (value,
440                                                         strlen (value),
441                                                         (char**)&entry->value);
442       if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_absolute (expiry,
443                                              entry->expiry))
444       {
445         GNUNET_free (entry->sub_system);
446         GNUNET_free (entry->key);
447         GNUNET_free (entry->peer);
448         GNUNET_free (entry);
449         break;
450       }
451       peer_id = GNUNET_i2s (entry->peer);
452       GNUNET_CRYPTO_hash (peer_id,
453                           strlen (peer_id),
454                           &hkey);
455
456       GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->hm,
457                                          &hkey,
458                                          entry,
459                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
460
461     }
462   }
463   GNUNET_free (buffer);
464   return GNUNET_OK;
465 }
466
467 static int
468 store_and_free_entries (void *cls,
469                         const struct GNUNET_HashCode *key,
470                         void *value)
471 {
472   struct GNUNET_DISK_FileHandle *fh = cls;
473   struct GNUNET_PEERSTORE_Record *entry = value;
474   char *line;
475   char *peer;
476   const char *expiry;
477   char *val;
478
479   GNUNET_STRINGS_base64_encode (entry->value,
480                                 entry->value_size,
481                                 &val);
482   expiry = GNUNET_STRINGS_absolute_time_to_string (*entry->expiry);
483   GNUNET_STRINGS_base64_encode ((char*)entry->peer,
484                                 sizeof (struct GNUNET_PeerIdentity),
485                                 &peer);
486   GNUNET_asprintf (&line,
487                    "%s,%s,%s,%s,%s",
488                    entry->sub_system,
489                    peer,
490                    entry->key,
491                    val,
492                    expiry);
493   GNUNET_free (val);
494   GNUNET_free (peer);
495   GNUNET_DISK_file_write (fh,
496                           line,
497                           strlen (line));
498   GNUNET_free (entry->sub_system);
499   GNUNET_free (entry->peer);
500   GNUNET_free (entry->key);
501   GNUNET_free (entry->value);
502   GNUNET_free (entry->expiry);
503   GNUNET_free (entry);
504   GNUNET_free (line);
505   return GNUNET_YES;
506
507 }
508
509 /**
510  * Shutdown database connection and associate data
511  * structures.
512  * @param plugin the plugin context (state for this module)
513  */
514 static void
515 database_shutdown (struct Plugin *plugin)
516 {
517   struct GNUNET_DISK_FileHandle *fh;
518   fh = GNUNET_DISK_file_open (plugin->fn,
519                               GNUNET_DISK_OPEN_CREATE |
520                               GNUNET_DISK_OPEN_TRUNCATE |
521                               GNUNET_DISK_OPEN_READWRITE,
522                               GNUNET_DISK_PERM_USER_WRITE |
523                               GNUNET_DISK_PERM_USER_READ);
524   if (NULL == fh)
525   {
526     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
527                 _("Unable to initialize file: %s.\n"),
528                 plugin->fn);
529     return;
530   }
531   GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
532                                          &store_and_free_entries,
533                                          fh);
534   GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
535   GNUNET_DISK_file_close (fh);
536 }
537
538
539 /**
540  * Entry point for the plugin.
541  *
542  * @param cls The struct GNUNET_CONFIGURATION_Handle.
543  * @return NULL on error, otherwise the plugin context
544  */
545 void *
546 libgnunet_plugin_peerstore_flat_init (void *cls)
547 {
548   static struct Plugin plugin;
549   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
550   struct GNUNET_PEERSTORE_PluginFunctions *api;
551
552   if (NULL != plugin.cfg)
553     return NULL;                /* can only initialize once! */
554   memset (&plugin, 0, sizeof (struct Plugin));
555   plugin.cfg = cfg;
556   if (GNUNET_OK != database_setup (&plugin))
557   {
558     database_shutdown (&plugin);
559     return NULL;
560   }
561   api = GNUNET_new (struct GNUNET_PEERSTORE_PluginFunctions);
562   api->cls = &plugin;
563   api->store_record = &peerstore_flat_store_record;
564   api->iterate_records = &peerstore_flat_iterate_records;
565   api->expire_records = &peerstore_flat_expire_records;
566   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is running\n");
567   return api;
568 }
569
570
571 /**
572  * Exit point from the plugin.
573  *
574  * @param cls The plugin context (as returned by "init")
575  * @return Always NULL
576  */
577 void *
578 libgnunet_plugin_peerstore_flat_done (void *cls)
579 {
580   struct GNUNET_PEERSTORE_PluginFunctions *api = cls;
581   struct Plugin *plugin = api->cls;
582
583   database_shutdown (plugin);
584   plugin->cfg = NULL;
585   GNUNET_free (api);
586   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is finished\n");
587   return NULL;
588 }
589
590 /* end of plugin_peerstore_sqlite.c */