allow empty/NULL context message
[oweals/gnunet.git] / src / namestore / plugin_namestore_flat.c
index fb6ece5ecbc25bcc544d05e910e13f5beba9f5d9..ca5f3e9a3e4da52e891fed920ff09339e1141d0a 100644 (file)
@@ -1,6 +1,6 @@
  /*
   * This file is part of GNUnet
-  * Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
+  * Copyright (C) 2009-2015 GNUnet e.V.
   *
   * GNUnet is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published
@@ -92,11 +92,6 @@ struct FlatFileEntry
    */
   struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
 
-  /**
-   * Entry zone pkey
-   */
-  struct GNUNET_CRYPTO_EcdsaPublicKey *pkey;
-
   /**
    * Record cound
    */
@@ -133,10 +128,9 @@ static int
 database_setup (struct Plugin *plugin)
 {
   char *afsdir;
-  char *key_str;
+  char *key;
   char *record_data;
   char *zone_private_key;
-  char *pkey;
   char *record_data_b64;
   char *buffer;
   char *line;
@@ -145,6 +139,7 @@ database_setup (struct Plugin *plugin)
   char *record_count;
   size_t record_data_size;
   size_t size;
+  size_t key_len;
   struct GNUNET_HashCode hkey;
   struct GNUNET_DISK_FileHandle *fh;
   struct FlatFileEntry *entry; 
@@ -210,53 +205,61 @@ database_setup (struct Plugin *plugin)
   }
 
   GNUNET_DISK_file_close (fh);
-
-  line = strtok ("\n", buffer);
-  while (line != NULL) {
-    zone_private_key = strtok (",", line);
-    pkey = strtok (NULL, line);
-    rvalue = strtok (NULL, line);
-    record_count = strtok (NULL, line);
-    record_data_b64 = strtok (NULL, line);
-    label = strtok (NULL, line);
-    line = strtok ("\n", buffer);
-    entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
-    GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
-                                                strlen (pkey),
-                                                entry->pkey);
-    sscanf (rvalue, "%lu", &entry->rvalue);
-    sscanf (record_count, "%u", &entry->record_count);
-    entry->label = GNUNET_strdup (label);
-    record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
-                                                     strlen (record_data_b64),
-                                                     &record_data);
-    entry->record_data = 
-      GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
-    GNUNET_GNSRECORD_records_deserialize (record_data_size,
-                                          record_data,
-                                          entry->record_count,
-                                          entry->record_data);
-    GNUNET_free (record_data);
-    GNUNET_STRINGS_base64_decode (zone_private_key,
-                                  strlen (zone_private_key),
-                                  (char**)&entry->private_key);
-    key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-    memcpy (key_str, label, strlen (label));
-    memcpy (key_str+strlen(label),
-            entry->private_key,
-            sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-    GNUNET_CRYPTO_hash (key_str,
-                        strlen (key_str),
-                        &hkey);
-    GNUNET_free (key_str);
-    if (GNUNET_OK != 
-        GNUNET_CONTAINER_multihashmap_put (plugin->hm,
-                                           &hkey,
-                                           entry,
-                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
-    {
-      GNUNET_free (entry);
-      GNUNET_break (0);
+  if (0 < size) {
+    line = strtok (buffer, "\n");
+    while (line != NULL) {
+      zone_private_key = strtok (line, ",");
+      if (NULL == zone_private_key)
+        break;
+      rvalue = strtok (NULL, ",");
+      if (NULL == rvalue)
+        break;
+      record_count = strtok (NULL, ",");
+      if (NULL == record_count)
+        break;
+      record_data_b64 = strtok (NULL, ",");
+      if (NULL == record_data_b64)
+        break;
+      label = strtok (NULL, ",");
+      if (NULL == label)
+        break;
+      line = strtok (NULL, "\n");
+      entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
+      sscanf (rvalue, "%lu", &entry->rvalue);
+      sscanf (record_count, "%u", &entry->record_count);
+      entry->label = GNUNET_strdup (label);
+      record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
+                                                       strlen (record_data_b64),
+                                                       &record_data);
+      entry->record_data = 
+        GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
+      GNUNET_GNSRECORD_records_deserialize (record_data_size,
+                                            record_data,
+                                            entry->record_count,
+                                            entry->record_data);
+      GNUNET_free (record_data);
+      GNUNET_STRINGS_base64_decode (zone_private_key,
+                                    strlen (zone_private_key),
+                                    (char**)&entry->private_key);
+      key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
+      key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
+      memcpy (key, label, strlen (label));
+      memcpy (key+strlen(label),
+              entry->private_key,
+              sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
+      GNUNET_CRYPTO_hash (key,
+                          key_len,
+                          &hkey);
+      GNUNET_free (key);
+      if (GNUNET_OK != 
+          GNUNET_CONTAINER_multihashmap_put (plugin->hm,
+                                             &hkey,
+                                             entry,
+                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+      {
+        GNUNET_free (entry);
+        GNUNET_break (0);
+      }
     }
   }
   GNUNET_free (buffer);
@@ -278,37 +281,35 @@ store_and_free_entries (void *cls,
   struct FlatFileEntry *entry = value;
   char *line;
   char *zone_private_key;
-  char *pkey;
   char *rvalue;
   char *record_count;
-  char *record_data_buf;
   char *record_data_b64;
-  size_t record_data_len;
+  size_t data_size;
 
   GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
                                 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
                                 &zone_private_key);
-  pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (entry->pkey);
-  GNUNET_asprintf (&rvalue, "%hhu", entry->rvalue);
+  GNUNET_asprintf (&rvalue, "%lu", entry->rvalue);
   GNUNET_asprintf (&record_count, "%u", entry->record_count);
-
-  record_data_len = GNUNET_GNSRECORD_records_get_size (entry->record_count,
-                                                       entry->record_data);
-
-  record_data_buf = GNUNET_malloc (record_data_len);
-  GNUNET_GNSRECORD_records_serialize (entry->record_count,
-                                      entry->record_data,
-                                      record_data_len,
-                                      record_data_buf);
-
-  GNUNET_STRINGS_base64_encode (record_data_buf,
-                                strlen (record_data_buf),
+  
+  data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
+                                                 entry->record_data);
+  char data[data_size];
+  if (data_size != GNUNET_GNSRECORD_records_serialize (entry->record_count,
+                                                       entry->record_data,
+                                                       data_size,
+                                                       data))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_STRINGS_base64_encode (data,
+                                data_size,
                                 &record_data_b64);
 
   GNUNET_asprintf (&line,
-                   "%s,%s,%s,%s,%s,%s\n",
+                   "%s,%s,%s,%s,%s\n",
                    zone_private_key,
-                   pkey,
                    rvalue,
                    record_count,
                    record_data_b64,
@@ -316,7 +317,6 @@ store_and_free_entries (void *cls,
 
   GNUNET_free (rvalue);
   GNUNET_free (record_count);
-  GNUNET_free (record_data_buf);
   GNUNET_free (record_data_b64);
 
   GNUNET_DISK_file_write (fh,
@@ -324,7 +324,6 @@ store_and_free_entries (void *cls,
                           strlen (line));
 
   GNUNET_free (entry->private_key);
-  GNUNET_free (entry->pkey);
   GNUNET_free (entry->label);
   GNUNET_free (entry->record_data);
   GNUNET_free (entry);
@@ -342,14 +341,15 @@ database_shutdown (struct Plugin *plugin)
   struct GNUNET_DISK_FileHandle *fh;
   fh = GNUNET_DISK_file_open (plugin->fn,
                               GNUNET_DISK_OPEN_CREATE |
-                              GNUNET_DISK_OPEN_TRUNCATE,
+                              GNUNET_DISK_OPEN_TRUNCATE |
+                              GNUNET_DISK_OPEN_READWRITE,
                               GNUNET_DISK_PERM_USER_WRITE |
                               GNUNET_DISK_PERM_USER_READ);
   if (NULL == fh)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-         _("Unable to initialize file: %s.\n"),
-         plugin->fn);
+                _("Unable to initialize file: %s.\n"),
+                plugin->fn);
     return;
   }
 
@@ -380,66 +380,48 @@ namestore_store_records (void *cls,
                          const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct Plugin *plugin = cls;
-  struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
   uint64_t rvalue;
-  size_t data_size;
-  unsigned int i;
-  char *key_str;
+  size_t key_len;
+  char *key;
   struct GNUNET_HashCode hkey;
   struct FlatFileEntry *entry;
+  int i;
 
-  memset (&pkey, 0, sizeof (pkey));
-  for (i=0;i<rd_count;i++)
-    if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
-    {
-      GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
-      memcpy (&pkey,
-              rd[i].data,
-              rd[i].data_size);
-      break;
-    }
   rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
-  data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
-  if (data_size > 64 * 65536)
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
-  char data[data_size];
-
-  if (data_size != GNUNET_GNSRECORD_records_serialize (rd_count, rd,
-                                                       data_size, data))
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
-
-  key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-  memcpy (key_str, label, strlen (label));
-  memcpy (key_str+strlen(label),
+  key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
+  key = GNUNET_malloc (key_len);
+  memcpy (key, label, strlen (label));
+  memcpy (key+strlen(label),
           zone_key,
           sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-  GNUNET_CRYPTO_hash (key_str,
-                      strlen (key_str),
+  GNUNET_CRYPTO_hash (key,
+                      key_len,
                       &hkey);
 
   GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
 
-  if (0 != rd_count)
+  if (0 < rd_count)
   {
     entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
     entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-    memcpy (&entry->private_key,
+    GNUNET_asprintf (&entry->label,
+                     label,
+                     strlen (label));
+    memcpy (entry->private_key,
             zone_key,
             sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-    entry->pkey = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
-    memcpy (entry->pkey,
-            &pkey,
-            sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
     entry->rvalue = rvalue;
     entry->record_count = rd_count;
-    entry->record_data = GNUNET_malloc (data_size);
-    memcpy (&entry->record_data, data, data_size);
+    entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count);
+    for (i = 0; i < rd_count; i++)
+    {
+      entry->record_data[i].expiration_time = rd[i].expiration_time;
+      entry->record_data[i].record_type = rd[i].record_type;
+      entry->record_data[i].flags = rd[i].flags;
+      entry->record_data[i].data_size = rd[i].data_size;
+      entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
+      memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
+    }
     return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
                                               &hkey,
                                               entry,
@@ -469,22 +451,24 @@ namestore_lookup_records (void *cls,
   struct Plugin *plugin = cls;
   struct FlatFileEntry *entry;
   struct GNUNET_HashCode hkey;
-  char *key_str;
+  char *key;
+  size_t key_len;
 
   if (NULL == zone)
   {
     return GNUNET_SYSERR;
   }
-  key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-  memcpy (key_str, label, strlen (label));
-  memcpy (key_str+strlen(label),
+  key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
+  key = GNUNET_malloc (key_len);
+  memcpy (key, label, strlen (label));
+  memcpy (key+strlen(label),
           zone,
           sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-  GNUNET_CRYPTO_hash (key_str,
-                      strlen (key_str),
+  GNUNET_CRYPTO_hash (key,
+                      key_len,
                       &hkey);
-  GNUNET_free (key_str);
-
+  GNUNET_free (key);
+  
   entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
 
   if (NULL == entry)
@@ -503,15 +487,14 @@ iterate_zones (void *cls,
   struct Plugin *plugin = cls;
   struct FlatFileEntry *entry = value;
 
-
   if ((plugin->target_offset > plugin->offset) ||
-      (0 != memcmp (entry->private_key,
-                    plugin->iter_zone,
-                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))) {
+      ( (NULL != plugin->iter_zone) &&
+        (0 != memcmp (entry->private_key,
+                      plugin->iter_zone,
+                      sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
     plugin->offset++;
     return GNUNET_YES;
   }
-
   plugin->iter (plugin->iter_cls,
                 entry->private_key,
                 entry->label,
@@ -542,7 +525,7 @@ namestore_iterate_records (void *cls,
   plugin->target_offset = offset;
   plugin->offset = 0;
   plugin->iter = iter;
-  plugin->iter_cls = cls;
+  plugin->iter_cls = iter_cls;
   plugin->iter_zone = zone;
   plugin->iter_result_found = GNUNET_NO;
   GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
@@ -605,8 +588,8 @@ namestore_zone_to_name (void *cls,
   struct Plugin *plugin = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-       "Performing reverse lookup for `%s'\n",
-       GNUNET_GNSRECORD_z2s (value_zone));
+              "Performing reverse lookup for `%s'\n",
+              GNUNET_GNSRECORD_z2s (value_zone));
 
   GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
                                          &zone_to_name,
@@ -646,7 +629,7 @@ libgnunet_plugin_namestore_flat_init (void *cls)
   api->zone_to_name = &namestore_zone_to_name;
   api->lookup_records = &namestore_lookup_records;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-       _("flat file database running\n"));
+              _("flat file database running\n"));
   return api;
 }
 
@@ -667,7 +650,7 @@ libgnunet_plugin_namestore_flat_done (void *cls)
   plugin->cfg = NULL;
   GNUNET_free (api);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-       "flat file plugin is finished\n");
+              "flat file plugin is finished\n");
   return NULL;
 }