More fixes for #3522
[oweals/gnunet.git] / src / namestore / gnunet-namestore-fcfsd.c
index 14f95c911f35e028fe9663df2d8347b4bf78c376..5286b905667b8e79cb2ae9ec18b34fd0e94736d2 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012-2013 Christian Grothoff (and other contributing authors)
+     (C) 2012-2014 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -131,6 +131,11 @@ struct Request
    */
   struct GNUNET_NAMESTORE_QueueEntry *qe;
 
+  /**
+   * Active iteration with the namestore.
+   */
+  struct GNUNET_NAMESTORE_ZoneIterator *zi;
+
   /**
    * Current processing phase.
    */
@@ -146,6 +151,8 @@ struct Request
    */
   char public_key[128];
 
+  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
+
 };
 
 /**
@@ -241,12 +248,22 @@ run_httpd_now ()
 }
 
 
+/**
+ * Process a record that was stored in the namestore, adding
+ * the information to the HTML.
+ *
+ * @param cls closure with the `struct ZoneinfoRequest *`
+ * @param zone_key private key of the zone; NULL on disconnect
+ * @param name label of the records; NULL on disconnect
+ * @param rd_len number of entries in @a rd array, 0 if label was deleted
+ * @param rd array of records with data to store
+ */
 static void
 iterate_cb (void *cls,
            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
            const char *name,
            unsigned int rd_len,
-           const struct GNUNET_NAMESTORE_RecordData *rd)
+           const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct ZoneinfoRequest *zr = cls;
   struct MHD_Response *response;
@@ -287,17 +304,22 @@ iterate_cb (void *cls,
     return;
   }
 
-  if (GNUNET_NAMESTORE_TYPE_PKEY != rd->record_type)
+  if (GNUNET_GNSRECORD_TYPE_PKEY != rd->record_type)
   {
     GNUNET_NAMESTORE_zone_iterator_next (zr->list_it);
     return;
   }
 
   bytes_free = zr->buf_len - zr->write_offset;
-  pkey = GNUNET_NAMESTORE_value_to_string (rd->record_type,
+  pkey = GNUNET_GNSRECORD_value_to_string (rd->record_type,
                                            rd->data,
                                            rd->data_size);
-
+  if (NULL == pkey)
+  {
+    GNUNET_break (0);
+    GNUNET_NAMESTORE_zone_iterator_next (zr->list_it);
+    return;
+  }
   if (bytes_free < (strlen (name) + strlen (pkey) + 40))
   {
     new_buf = GNUNET_malloc (zr->buf_len * 2);
@@ -316,7 +338,6 @@ iterate_cb (void *cls,
 }
 
 
-
 /**
  * Handler that returns FCFS zoneinfo page.
  *
@@ -460,16 +481,14 @@ post_iterator (void *cls,
 }
 
 
-
-
 /**
  * Continuation called to notify client about result of the
  * operation.
  *
  * @param cls closure
- * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
- *                GNUNET_NO if content was already there
- *                GNUNET_YES (or other positive value) on success
+ * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
+ *                #GNUNET_NO if content was already there
+ *                #GNUNET_YES (or other positive value) on success
  * @param emsg NULL on success, otherwise an error message
  */
 static void
@@ -500,7 +519,7 @@ put_continuation (void *cls,
  * @param cls closure
  * @param zone_key public key of the zone
  * @param name name that is being mapped (at most 255 characters long)
- * @param rd_count number of entries in 'rd' array
+ * @param rd_count number of entries in @a rd array
  * @param rd array of records with data to store
  */
 static void
@@ -508,14 +527,13 @@ zone_to_name_cb (void *cls,
                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
                 const char *name,
                 unsigned int rd_count,
-                const struct GNUNET_NAMESTORE_RecordData *rd)
+                const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct Request *request = cls;
-  struct GNUNET_NAMESTORE_RecordData r;
-  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
-
+  struct GNUNET_GNSRECORD_Data r;
   request->qe = NULL;
-  if (NULL != name)
+
+  if (0 != rd_count)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("Found existing name `%s' for the given key\n"),
@@ -524,13 +542,20 @@ zone_to_name_cb (void *cls,
     run_httpd_now ();
     return;
   }
-  GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
-                                                 &pub);
-  r.data = &pub;
-  r.data_size = sizeof (pub);
+  if (NULL == zone_key)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Error when mapping zone to name\n"));
+    request->phase = RP_FAIL;
+    run_httpd_now ();
+    return;
+  }
+
+  r.data = &request->pub;
+  r.data_size = sizeof (request->pub);
   r.expiration_time = UINT64_MAX;
-  r.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
-  r.flags = GNUNET_NAMESTORE_RF_NONE;
+  r.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
+  r.flags = GNUNET_GNSRECORD_RF_NONE;
   request->qe = GNUNET_NAMESTORE_records_store (ns,
                                                &fcfs_zone_pkey,
                                                request->domain_name,
@@ -540,85 +565,54 @@ zone_to_name_cb (void *cls,
 }
 
 
-/**
- * Process a record that was stored in the namestore.  Used to check if
- * the requested name already exists in the namestore.  If not,
- * proceed to check if the requested key already exists.
- *
- * @param cls closure
- * @param rd_count number of entries in 'rd' array
- * @param rd array of records with data to store
- */
-static void
-lookup_result_processor (void *cls,
-                        unsigned int rd_count,
-                        const struct GNUNET_NAMESTORE_RecordData *rd)
-{
-  struct Request *request = cls;
-  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
-
-  if (0 != rd_count)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-               _("Found %u existing records for domain `%s'\n"),
-               rd_count,
-               request->domain_name);
-    request->phase = RP_FAIL;
-    run_httpd_now ();
-    return;
-  }
-  if (GNUNET_OK !=
-      GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
-                                               strlen (request->public_key),
-                                               &pub))
-  {
-    GNUNET_break (0);
-    request->phase = RP_FAIL;
-    run_httpd_now ();
-    return;
-  }
-  request->qe = GNUNET_NAMESTORE_zone_to_name (ns,
-                                              &fcfs_zone_pkey,
-                                              &pub,
-                                              &zone_to_name_cb,
-                                              request);
-}
-
-
 /**
  * We got a block back from the namestore.  Decrypt it
  * and continue to process the result.
  *
  * @param cls the 'struct Request' we are processing
- * @param block block returned form namestore, NULL on error
+ * @param zone private key of the zone; NULL on disconnect
+ * @param label label of the records; NULL on disconnect
+ * @param rd_count number of entries in @a rd array, 0 if label was deleted
+ * @param rd array of records with data to store
  */
 static void
 lookup_block_processor (void *cls,
-                       const struct GNUNET_NAMESTORE_Block *block)
+                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
+                        const char *label,
+                        unsigned int rd_count,
+                        const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct Request *request = cls;
-  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
 
   request->qe = NULL;
-  if (NULL == block)
-  {
-    lookup_result_processor (request, 0, NULL);
-    return;
-  }
-  GNUNET_CRYPTO_ecdsa_key_get_public (&fcfs_zone_pkey,
-                                   &pub);
-  if (GNUNET_OK !=
-      GNUNET_NAMESTORE_block_decrypt (block,
-                                     &pub,
-                                     request->domain_name,
-                                     &lookup_result_processor,
-                                     request))
+  if (0 == rd_count)
   {
-    GNUNET_break (0);
-    request->phase = RP_FAIL;
-    run_httpd_now ();
+
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
+                                                    strlen (request->public_key),
+                                                    &request->pub))
+    {
+      GNUNET_break (0);
+      request->phase = RP_FAIL;
+      run_httpd_now ();
+      return;
+    }
+    request->qe = GNUNET_NAMESTORE_zone_to_name (ns,
+                                                 &fcfs_zone_pkey,
+                                                 &request->pub,
+                                                 &zone_to_name_cb,
+                                                 request);
     return;
   }
+  GNUNET_break (0 != rd_count);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("Found %u existing records for domain `%s'\n"),
+              rd_count,
+              request->domain_name);
+  request->phase = RP_FAIL;
+  run_httpd_now ();
+  return;
 }
 
 
@@ -638,7 +632,7 @@ lookup_block_processor (void *cls,
  *        data *will* be made available incrementally in
  *        upload_data)
  * @param upload_data_size set initially to the size of the
- *        upload_data provided; the method must update this
+ *        @a upload_data provided; the method must update this
  *        value to the number of bytes NOT processed;
  * @param ptr pointer to location where we store the 'struct Request'
  * @return MHD_YES if the connection was handled successfully,
@@ -657,9 +651,8 @@ create_response (void *cls,
 {
   struct MHD_Response *response;
   struct Request *request;
-  int ret;
   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
-  struct GNUNET_HashCode query;
+  int ret;
 
   if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
        (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
@@ -679,7 +672,7 @@ create_response (void *cls,
       request = *ptr;
       if (NULL == request)
       {
-       request = GNUNET_malloc (sizeof (struct Request));
+       request = GNUNET_new (struct Request);
        *ptr = request;
        request->pp = MHD_create_post_processor (connection, 1024,
                                                 &post_iterator, request);
@@ -709,8 +702,8 @@ create_response (void *cls,
       }
       if (GNUNET_OK !=
          GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
-                                                   strlen (request->public_key),
-                                                   &pub))
+                                                      strlen (request->public_key),
+                                                      &pub))
       {
        /* parse error */
        return fill_s_reply ("Failed to parse given public key",
@@ -736,15 +729,11 @@ create_response (void *cls,
                                 request, connection);
          }
          request->phase = RP_LOOKUP;
-         GNUNET_CRYPTO_ecdsa_key_get_public (&fcfs_zone_pkey,
-                                           &pub);
-         GNUNET_NAMESTORE_query_from_public_key (&pub,
-                                                 request->domain_name,
-                                                 &query);
-         request->qe = GNUNET_NAMESTORE_lookup_block (ns,
-                                                      &query,
-                                                      &lookup_block_processor,
-                                                      request);
+         request->qe = GNUNET_NAMESTORE_records_lookup (ns,
+                                                       &fcfs_zone_pkey,
+                                                       request->domain_name,
+                                                       &lookup_block_processor,
+                                                       request);
          break;
        case RP_LOOKUP:
          break;
@@ -997,6 +986,12 @@ run (void *cls, char *const *args, const char *cfgfile,
     }
   identity = GNUNET_IDENTITY_connect (cfg,
                                      NULL, NULL);
+  if (NULL == identity)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Failed to connect to identity\n"));
+    return;
+  }
   id_op = GNUNET_IDENTITY_get (identity, "fcfsd",
                               &identity_cb, NULL);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,