NAMESTORE/JSON: fix parsing exp and flags
[oweals/gnunet.git] / src / namestore / plugin_rest_namestore.c
index 1d72d13ffef737fb383c2dce589d4de8d790dbae..cad019292a8fa65a89350cdc0736e769e96e7212 100644 (file)
@@ -14,6 +14,8 @@
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
    */
 /**
  * @author Martin Schanzenbach
@@ -199,7 +201,7 @@ struct RequestHandle
    * Rest connection
    */
   struct GNUNET_REST_RequestHandle *rest_handle;
-  
+
   /**
    * Desired timeout for the lookup (default is no timeout).
    */
@@ -458,10 +460,7 @@ namestore_list_finished (void *cls)
   handle->list_it = NULL;
 
   if (NULL == handle->resp_object)
-  {
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
-    return;
-  }
+    handle->resp_object = json_array();
 
   result_str = json_dumps (handle->resp_object, 0);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
@@ -496,7 +495,7 @@ namestore_list_iteration (void *cls,
          (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) )
       continue;
 
-    record_obj = GNUNET_JSON_from_gns_record(rname,rd);
+    record_obj = GNUNET_JSON_from_gns_record (rname, &rd[i]);
 
     if(NULL == record_obj)
       continue;
@@ -508,6 +507,48 @@ namestore_list_iteration (void *cls,
   GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1);
 }
 
+/**
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+default_ego_get (void *cls,
+                struct GNUNET_IDENTITY_Ego *ego,
+                void **ctx,
+                const char *identifier)
+{
+  struct RequestHandle *handle = cls;
+  handle->op = NULL;
+
+  if (ego == NULL)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
+
+  handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
+                                                           handle->zone_pkey,
+                                                           &namestore_iteration_error,
+                                                           handle,
+                                                           &namestore_list_iteration,
+                                                           handle,
+                                                           &namestore_list_finished,
+                                                           handle);
+  if (NULL == handle->list_it)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+}
+
 
 /**
  * Handle namestore GET request
@@ -546,10 +587,13 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
   {
     handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
   }
+
   if (NULL == handle->zone_pkey)
   {
-    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_get,
+                                     handle);
     return;
   }
   handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
@@ -569,6 +613,48 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
 }
 
 
+/**
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+default_ego_post (void *cls,
+                 struct GNUNET_IDENTITY_Ego *ego,
+                 void **ctx,
+                 const char *identifier)
+{
+  struct RequestHandle *handle = cls;
+  handle->op = NULL;
+
+  if (ego == NULL)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
+
+  handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
+                                                  handle->zone_pkey,
+                                                  handle->record_name,
+                                                  1,
+                                                  handle->rd,
+                                                  &create_finished,
+                                                  handle);
+  if (NULL == handle->add_qe)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+}
+
+
 /**
  * Handle namestore POST request
  *
@@ -662,18 +748,62 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
     handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
   }
   if (NULL == handle->zone_pkey)
+  {
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_post,
+                                     handle);
+    return;
+  }
+  handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
+                                                  handle->zone_pkey,
+                                                  handle->record_name,
+                                                  1,
+                                                  handle->rd,
+                                                  &create_finished,
+                                                  handle);
+  if (NULL == handle->add_qe)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+}
+
+
+/**
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+default_ego_delete (void *cls,
+                   struct GNUNET_IDENTITY_Ego *ego,
+                   void **ctx,
+                   const char *identifier)
+{
+  struct RequestHandle *handle = cls;
+  handle->op = NULL;
+
+  if (ego == NULL)
   {
     handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
+  handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
+
   handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
-                                                   handle->zone_pkey,
-                                                   handle->record_name,
-                                                   1,
-                                                   handle->rd,
-                                                   &create_finished,
-                                                   handle);
+                                                   handle->zone_pkey,
+                                                   handle->record_name,
+                                                   0,
+                                                  NULL,
+                                                   &del_finished,
+                                                   handle);
   if (NULL == handle->add_qe)
   {
     handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
@@ -736,8 +866,10 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle,
 
   if (NULL == handle->zone_pkey)
   {
-    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_delete,
+                                     handle);
     return;
   }
 
@@ -811,30 +943,6 @@ init_cont (struct RequestHandle *handle)
   }
 }
 
-/**
- * @param cls closure
- * @param ego ego handle
- * @param ctx context for application to store data for this ego
- *                 (during the lifetime of this process, initially NULL)
- * @param identifier identifier assigned by the user for this ego,
- *                   NULL if the user just deleted the ego and it
- *                   must thus no longer be used
- */
-static void
-default_ego_cb (void *cls,
-                struct GNUNET_IDENTITY_Ego *ego,
-                void **ctx,
-                const char *identifier)
-{
-  struct RequestHandle *handle = cls;
-  handle->op = NULL;
-
-  if (ego != NULL)
-  {
-    handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
-  }
-}
-
 
 /**
  * This function is initially called for all egos and then again
@@ -877,17 +985,10 @@ id_connect_cb (void *cls,
   struct EgoEntry *ego_entry;
   struct GNUNET_CRYPTO_EcdsaPublicKey pk;
 
-  if ((NULL == ego) && (NULL == handle->zone_pkey))
-  {
-    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
-                                     "namestore",
-                                     &default_ego_cb,
-                                     handle);
-  }
   if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
   {
     handle->state = ID_REST_STATE_POST_INIT;
-    init_cont (handle);
+    init_cont(handle);
     return;
   }
   if (ID_REST_STATE_INIT == handle->state)
@@ -921,26 +1022,26 @@ rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
                               void *proc_cls)
 {
   struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
-  
+
   handle->response_code = 0;
   handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
   handle->proc_cls = proc_cls;
   handle->proc = proc;
   handle->rest_handle = rest_handle;
   handle->zone_pkey = NULL;
-  
+
   handle->url = GNUNET_strdup (rest_handle->url);
   if (handle->url[strlen (handle->url)-1] == '/')
     handle->url[strlen (handle->url)-1] = '\0';
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
 
-  handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle);
   handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
+  handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle);
   handle->timeout_task =
     GNUNET_SCHEDULER_add_delayed (handle->timeout,
                                   &do_error,
                                   handle);
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
 }