attempt fix fcfs
authorSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>
Sun, 24 Jun 2018 11:03:41 +0000 (13:03 +0200)
committerSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>
Sun, 24 Jun 2018 11:03:41 +0000 (13:03 +0200)
src/namestore/gnunet-namestore-fcfsd.c

index f4cdf26b47441f474463a2f2161d08c2922cf783..ddd719e737913c14b08a779d8842c34b42b630d1 100644 (file)
@@ -129,6 +129,11 @@ struct Request
    */
   struct GNUNET_NAMESTORE_QueueEntry *qe;
 
+  /**
+   * Active lookup iterator
+   * TODO: deprecate or fix lookup by label and use above member
+   */
+  struct GNUNET_NAMESTORE_ZoneIterator *lookup_it;
   /**
    * Active iteration with the namestore.
    */
@@ -637,7 +642,7 @@ zone_to_name_cb (void *cls,
  * We encountered an error in the name lookup.
  */
 static void
-lookup_block_error (void *cls)
+lookup_it_error (void *cls)
 {
   struct Request *request = cls;
 
@@ -658,7 +663,7 @@ lookup_block_error (void *cls)
  * @param rd array of records with data to store
  */
 static void
-lookup_block_processor (void *cls,
+lookup_it_processor (void *cls,
                         const struct GNUNET_CRYPTO_EcdsaPrivateKey *zonekey,
                         const char *label,
                         unsigned int rd_count,
@@ -670,38 +675,44 @@ lookup_block_processor (void *cls,
   (void) rd;
   (void) zonekey;
   request->qe = NULL;
-  if (0 == rd_count)
+  if (0 == strcmp (label, request->domain_name)) {
+    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;
+  }
+}
+
+static void
+lookup_it_finished (void *cls)
+{
+  struct Request *request = cls;
+
+  if (RP_FAIL == request->phase) {
+    run_httpd_now ();
+    return;
+  }
+  if (GNUNET_OK !=
+      GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
+                                                  strlen (request->public_key),
+                                                  &request->pub))
   {
-    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_error,
-                                                 request,
-                                                 &zone_to_name_cb,
-                                                 request);
+    GNUNET_break (0);
+    request->phase = RP_FAIL;
+    run_httpd_now ();
     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;
+  request->qe = GNUNET_NAMESTORE_zone_to_name (ns,
+                                               &fcfs_zone_pkey,
+                                               &request->pub,
+                                               &zone_to_name_error,
+                                               request,
+                                               &zone_to_name_cb,
+                                               request);
 }
 
-
 /**
  * Main MHD callback for handling requests.
  *
@@ -727,13 +738,13 @@ lookup_block_processor (void *cls,
  */
 static int
 create_response (void *cls,
-                struct MHD_Connection *connection,
-                const char *url,
-                const char *method,
-                const char *version,
-                const char *upload_data,
-                size_t *upload_data_size,
-                void **ptr)
+                 struct MHD_Connection *connection,
+                 const char *url,
+                 const char *method,
+                 const char *version,
+                 const char *upload_data,
+                 size_t *upload_data_size,
+                 void **ptr)
 {
   struct MHD_Response *response;
   struct Request *request;
@@ -744,114 +755,115 @@ create_response (void *cls,
   (void) version;
   if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
        (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
-    {
-      if (0 == strcmp (url, FCFS_ZONEINFO_URL))
-        ret = serve_zoneinfo_page (connection);
-      else
-        ret = serve_main_page (connection);
-      if (ret != MHD_YES)
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                   _("Failed to create page for `%s'\n"),
-                   url);
-      return ret;
-    }
+  {
+    if (0 == strcmp (url, FCFS_ZONEINFO_URL))
+      ret = serve_zoneinfo_page (connection);
+    else
+      ret = serve_main_page (connection);
+    if (ret != MHD_YES)
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  _("Failed to create page for `%s'\n"),
+                  url);
+    return ret;
+  }
   if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
+  {
+    request = *ptr;
+    if (NULL == request)
     {
-      request = *ptr;
-      if (NULL == request)
-      {
-       request = GNUNET_new (struct Request);
-       *ptr = request;
-       request->pp = MHD_create_post_processor (connection,
-                                                1024,
-                                                &post_iterator,
-                                                request);
-       if (NULL == request->pp)
-         {
-           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                       _("Failed to setup post processor for `%s'\n"),
-                       url);
-           return MHD_NO; /* internal error */
-         }
-       return MHD_YES;
-      }
-      if (NULL != request->pp)
+      request = GNUNET_new (struct Request);
+      *ptr = request;
+      request->pp = MHD_create_post_processor (connection,
+                                               1024,
+                                               &post_iterator,
+                                               request);
+      if (NULL == request->pp)
       {
-       /* evaluate POST data */
-       MHD_post_process (request->pp,
-                         upload_data,
-                         *upload_data_size);
-       if (0 != *upload_data_size)
-         {
-           *upload_data_size = 0;
-           return MHD_YES;
-         }
-       /* done with POST data, serve response */
-       MHD_destroy_post_processor (request->pp);
-       request->pp = NULL;
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    _("Failed to setup post processor for `%s'\n"),
+                    url);
+        return MHD_NO; /* internal error */
       }
-      if (GNUNET_OK !=
-         GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
-                                                      strlen (request->public_key),
-                                                      &pub))
+      return MHD_YES;
+    }
+    if (NULL != request->pp)
+    {
+      /* evaluate POST data */
+      MHD_post_process (request->pp,
+                        upload_data,
+                        *upload_data_size);
+      if (0 != *upload_data_size)
       {
-       /* parse error */
-       return fill_s_reply ("Failed to parse given public key",
-                            request, connection);
+        *upload_data_size = 0;
+        return MHD_YES;
       }
-      switch (request->phase)
-       {
-       case RP_START:
-         if (NULL != strchr (request->domain_name, (int) '.'))
-         {
-           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                       _("Domain name must not contain `.'\n"));
-           request->phase = RP_FAIL;
-           return fill_s_reply ("Domain name must not contain `.', sorry.",
-                                request,
-                                connection);
-         }
-         if (NULL != strchr (request->domain_name, (int) '+'))
-         {
-           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                       _("Domain name must not contain `+'\n"));
-           request->phase = RP_FAIL;
-           return fill_s_reply ("Domain name must not contain `+', sorry.",
-                                request, connection);
-         }
-         request->phase = RP_LOOKUP;
-         request->qe
-           = GNUNET_NAMESTORE_records_lookup (ns,
-                                              &fcfs_zone_pkey,
-                                              request->domain_name,
-                                              &lookup_block_error,
-                                              request,
-                                              &lookup_block_processor,
-                                              request);
-         break;
-       case RP_LOOKUP:
-         break;
-       case RP_PUT:
-         break;
-       case RP_FAIL:
-         return fill_s_reply ("Request failed, sorry.",
-                              request, connection);
-       case RP_SUCCESS:
-         return fill_s_reply ("Success.",
-                              request, connection);
-       default:
-         GNUNET_break (0);
-         return MHD_NO;
-       }
-       return MHD_YES; /* will have a reply later... */
+      /* done with POST data, serve response */
+      MHD_destroy_post_processor (request->pp);
+      request->pp = NULL;
     }
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
+                                                    strlen (request->public_key),
+                                                    &pub))
+    {
+      /* parse error */
+      return fill_s_reply ("Failed to parse given public key",
+                           request, connection);
+    }
+    switch (request->phase)
+    {
+      case RP_START:
+        if (NULL != strchr (request->domain_name, (int) '.'))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                      _("Domain name must not contain `.'\n"));
+          request->phase = RP_FAIL;
+          return fill_s_reply ("Domain name must not contain `.', sorry.",
+                               request,
+                               connection);
+        }
+        if (NULL != strchr (request->domain_name, (int) '+'))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                      _("Domain name must not contain `+'\n"));
+          request->phase = RP_FAIL;
+          return fill_s_reply ("Domain name must not contain `+', sorry.",
+                               request, connection);
+        }
+        request->phase = RP_LOOKUP;
+        request->lookup_it
+          = GNUNET_NAMESTORE_zone_iteration_start (ns,
+                                                   &fcfs_zone_pkey,
+                                                   &lookup_it_error,
+                                                   request,
+                                                   &lookup_it_processor,
+                                                   request,
+                                                   &lookup_it_finished,
+                                                   request);
+        break;
+      case RP_LOOKUP:
+        break;
+      case RP_PUT:
+        break;
+      case RP_FAIL:
+        return fill_s_reply ("Request failed, sorry.",
+                             request, connection);
+      case RP_SUCCESS:
+        return fill_s_reply ("Success.",
+                             request, connection);
+      default:
+        GNUNET_break (0);
+        return MHD_NO;
+    }
+    return MHD_YES; /* will have a reply later... */
+  }
   /* unsupported HTTP method */
   response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
-                                             (void *) METHOD_ERROR,
-                                             MHD_RESPMEM_PERSISTENT);
+                                              (void *) METHOD_ERROR,
+                                              MHD_RESPMEM_PERSISTENT);
   ret = MHD_queue_response (connection,
-                           MHD_HTTP_NOT_ACCEPTABLE,
-                           response);
+                            MHD_HTTP_NOT_ACCEPTABLE,
+                            response);
   MHD_destroy_response (response);
   return ret;
 }
@@ -868,9 +880,9 @@ create_response (void *cls,
  */
 static void
 request_completed_callback (void *cls,
-                           struct MHD_Connection *connection,
-                           void **con_cls,
-                           enum MHD_RequestTerminationCode toe)
+                            struct MHD_Connection *connection,
+                            void **con_cls,
+                            enum MHD_RequestTerminationCode toe)
 {
   struct Request *request = *con_cls;
 
@@ -915,33 +927,33 @@ run_httpd ()
   wws = GNUNET_NETWORK_fdset_create ();
   max = -1;
   GNUNET_assert (MHD_YES ==
-                MHD_get_fdset (httpd,
-                               &rs,
-                               &ws,
-                               &es,
-                               &max));
+                 MHD_get_fdset (httpd,
+                                &rs,
+                                &ws,
+                                &es,
+                                &max));
   haveto = MHD_get_timeout (httpd,
-                           &timeout);
+                            &timeout);
   if (haveto == MHD_YES)
     tv.rel_value_us = (uint64_t) timeout * 1000LL;
   else
     tv = GNUNET_TIME_UNIT_FOREVER_REL;
   GNUNET_NETWORK_fdset_copy_native (wrs,
-                                   &rs,
-                                   max + 1);
+                                    &rs,
+                                    max + 1);
   GNUNET_NETWORK_fdset_copy_native (wws,
-                                   &ws,
-                                   max + 1);
+                                    &ws,
+                                    max + 1);
   GNUNET_NETWORK_fdset_copy_native (wes,
-                                   &es,
-                                   max + 1);
+                                    &es,
+                                    max + 1);
   httpd_task =
-      GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
-                                   tv,
-                                  wrs,
-                                  wws,
-                                   &do_httpd,
-                                  NULL);
+    GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
+                                 tv,
+                                 wrs,
+                                 wws,
+                                 &do_httpd,
+                                 NULL);
   GNUNET_NETWORK_fdset_destroy (wrs);
   GNUNET_NETWORK_fdset_destroy (wws);
   GNUNET_NETWORK_fdset_destroy (wes);
@@ -1026,9 +1038,9 @@ do_shutdown (void *cls)
  */
 static void
 identity_cb (void *cls,
-            struct GNUNET_IDENTITY_Ego *ego,
-            void **ctx,
-            const char *name)
+             struct GNUNET_IDENTITY_Ego *ego,
+             void **ctx,
+             const char *name)
 {
   int options;
 
@@ -1037,12 +1049,12 @@ identity_cb (void *cls,
   if (NULL == name)
     return;
   if (0 != strcmp (name,
-                  zone))
+                   zone))
     return;
   if (NULL == ego)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("No ego configured for `fcfsd` subsystem\n"));
+                _("No ego configured for `fcfsd` subsystem\n"));
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -1050,27 +1062,27 @@ identity_cb (void *cls,
 
   options = MHD_USE_DUAL_STACK | MHD_USE_DEBUG;
   do
-    {
-      httpd = MHD_start_daemon (options,
-                               (uint16_t) port,
-                               NULL, NULL,
-                               &create_response, NULL,
-                               MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
-                               MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
-                               MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
-                               MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024),
-                               MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
-                               MHD_OPTION_END);
-      if (MHD_USE_DEBUG == options)
-       break;
-      options = MHD_USE_DEBUG;
-    }
+  {
+    httpd = MHD_start_daemon (options,
+                              (uint16_t) port,
+                              NULL, NULL,
+                              &create_response, NULL,
+                              MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
+                              MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
+                              MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+                              MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024),
+                              MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
+                              MHD_OPTION_END);
+    if (MHD_USE_DEBUG == options)
+      break;
+    options = MHD_USE_DEBUG;
+  }
   while (NULL == httpd);
   if (NULL == httpd)
   {
 
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("Failed to start HTTP server\n"));
+                _("Failed to start HTTP server\n"));
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -1097,24 +1109,24 @@ run (void *cls,
   (void) cfgfile;
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg,
-                                            "fcfsd",
-                                            "HTTPPORT",
-                                            &port))
+                                             "fcfsd",
+                                             "HTTPPORT",
+                                             &port))
   {
     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
-                              "fcfsd", "HTTPPORT");
+                               "fcfsd", "HTTPPORT");
     return;
   }
   ns = GNUNET_NAMESTORE_connect (cfg);
   if (NULL == ns)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Failed to connect to namestore\n"));
-      return;
-    }
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Failed to connect to namestore\n"));
+    return;
+  }
   identity = GNUNET_IDENTITY_connect (cfg,
-                                     &identity_cb,
-                                     NULL);
+                                      &identity_cb,
+                                      NULL);
   if (NULL == identity)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1122,9 +1134,9 @@ run (void *cls,
     return;
   }
   uzp_task = GNUNET_SCHEDULER_add_now (&update_zoneinfo_page,
-                                      NULL);
+                                       NULL);
   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
-                                NULL);
+                                 NULL);
 }
 
 
@@ -1141,31 +1153,31 @@ main (int argc,
 {
   struct GNUNET_GETOPT_CommandLineOption options[] = {
     GNUNET_GETOPT_option_mandatory 
-    (GNUNET_GETOPT_option_string ('z',
-                                 "zone",
-                                 "EGO",
-                                 gettext_noop ("name of the zone that is to be managed by FCFSD"),
-                                 &zone)),
+      (GNUNET_GETOPT_option_string ('z',
+                                    "zone",
+                                    "EGO",
+                                    gettext_noop ("name of the zone that is to be managed by FCFSD"),
+                                    &zone)),
     GNUNET_GETOPT_OPTION_END
   };
   int ret;
 
   if (GNUNET_OK !=
       GNUNET_STRINGS_get_utf8_args (argc, argv,
-                                   &argc, &argv))
+                                    &argc, &argv))
     return 2;
 
   GNUNET_log_setup ("fcfsd",
-                   "WARNING",
-                   NULL);
+                    "WARNING",
+                    NULL);
   ret =
-      (GNUNET_OK ==
-       GNUNET_PROGRAM_run (argc,
-                          argv,
-                          "gnunet-namestore-fcfsd",
-                           _("GNU Name System First Come First Serve name registration service"),
-                          options,
-                           &run, NULL)) ? 0 : 1;
+    (GNUNET_OK ==
+     GNUNET_PROGRAM_run (argc,
+                         argv,
+                         "gnunet-namestore-fcfsd",
+                         _("GNU Name System First Come First Serve name registration service"),
+                         options,
+                         &run, NULL)) ? 0 : 1;
   GNUNET_free ((void*) argv);
   GNUNET_CRYPTO_ecdsa_key_clear (&fcfs_zone_pkey);
   return ret;