gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] branch master updated (713dc28 -> a365aad)


From: gnunet
Subject: [taler-anastasis] branch master updated (713dc28 -> a365aad)
Date: Fri, 25 Sep 2020 19:22:16 +0200

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a change to branch master
in repository anastasis.

    from 713dc28  fix json
     new 0a17e70  refactoring
     new a365aad  refactoring

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/cli/anastasis-cli-redux.c                      |  40 +-
 src/include/anastasis_redux.h                      |  48 +++
 src/lib/Makefile.am                                |   2 +
 src/lib/anastasis_api_backup_redux.c               | 401 +--------------------
 src/lib/anastasis_api_recovery_redux.c             | 142 ++++++++
 ...is_api_backup_redux.c => anastasis_api_redux.c} | 355 ++++++------------
 6 files changed, 314 insertions(+), 674 deletions(-)
 create mode 100644 src/lib/anastasis_api_recovery_redux.c
 copy src/lib/{anastasis_api_backup_redux.c => anastasis_api_redux.c} (70%)

diff --git a/src/cli/anastasis-cli-redux.c b/src/cli/anastasis-cli-redux.c
index bd32856..e9434eb 100644
--- a/src/cli/anastasis-cli-redux.c
+++ b/src/cli/anastasis-cli-redux.c
@@ -161,12 +161,12 @@ run (void *cls,
                                    "WARNING",
                                    NULL));
 
-  if (! (r_flag ^ b_flag ^ i_flag) || (r_flag && b_flag && i_flag))
+  if (! (r_flag ^ b_flag))
   {
     printf ("Please set one option: -b, -r, -i!\n");
     printf (
       "Example: anastasis-reducer -b prev.json action arguments.json 
new.json\n");
-    printf ("Example: anastasis-reducer -i init.json\n");
+    printf ("Example: anastasis-reducer -b -i init.json\n");
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -176,7 +176,10 @@ run (void *cls,
     if (args[0])
     {
       path_new_state = (char *) args[0];
-      new_state = ANASTASIS_backup_start (cfg);
+      if (b_flag)
+        new_state = ANASTASIS_backup_start (cfg);
+      else if (r_flag)
+        new_state = ANASTASIS_recovery_start (cfg);
       if (! new_state)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -186,9 +189,7 @@ run (void *cls,
         GNUNET_SCHEDULER_shutdown ();
         return;
       }
-      if (! (0 == json_dump_file (new_state,
-                                  path_new_state,
-                                  JSON_COMPACT)))
+      if (0 != persist_new_state ())
       {
         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                     "At %s:%u failed to dump json into file!\n",
@@ -201,7 +202,7 @@ run (void *cls,
     else
     {
       printf ("Please give path for json containing initial state!\n");
-      printf ("Example: anastasis-reducer -i init.json\n");
+      printf ("Example: anastasis-reducer -b -i init.json\n");
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
@@ -259,18 +260,6 @@ run (void *cls,
                           &rc);
   rc = GNUNET_CURL_gnunet_rc_create (ctx);
 
-  if (i_flag)
-  {
-    new_state = ANASTASIS_backup_start (cfg);
-    if (NULL != new_state)
-      if (0 == persist_new_state ())
-        return;
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to persist new state! \n");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
   if (b_flag)
     ANASTASIS_backup_action (prev_state,
                              action,
@@ -278,13 +267,12 @@ run (void *cls,
                              &action_cb,
                              cls);
 
-
-//   if (r_flag)
-//     ANASTASIS_recovery_action (prev_state,
-//                                action,
-//                                arguments,
-//                                &action_cb,
-//                                cls);
+  if (r_flag)
+    ANASTASIS_recovery_action (prev_state,
+                               action,
+                               arguments,
+                               &action_cb,
+                               cls);
 }
 
 
diff --git a/src/include/anastasis_redux.h b/src/include/anastasis_redux.h
index 1d066e4..8d2cfe9 100644
--- a/src/include/anastasis_redux.h
+++ b/src/include/anastasis_redux.h
@@ -37,6 +37,12 @@ extern json_t *redux_countries;
  */
 extern json_t *redux_id_attr;
 
+/**
+ * JSON containing anastasis providers.
+ */
+extern json_t *provider_list;
+
+
 /**
  * Returns an initial ANASTASIS backup state.
  *
@@ -55,6 +61,25 @@ json_t *
 ANASTASIS_recovery_start (const struct GNUNET_CONFIGURATION_Handle *cfg);
 
 
+/**
+ * Returns an initial ANASTASIS recovery state.
+ *
+ * @return NULL on failure
+ */
+json_t *
+ANASTASIS_recovery_start (const struct GNUNET_CONFIGURATION_Handle *cfg);
+
+
+/**
+ * Function to load json containing all countries.
+ * Returns #GNUNET_OK if operation succeeded, else #GNUNET_SYSERR.
+ *
+ * @return int
+ */
+int
+redux_countries_init ();
+
+
 /**
  * Signature of the callback passed to #ANASTASIS_backup_action and
  * #ANASTASIS_recover_action.
@@ -112,4 +137,27 @@ ANASTASIS_recovery_action (const json_t *state,
                            void *cb_cls);
 
 
+/**
+ * Operates on a state depending on given #ANASTASIS_BackupState
+ * or #ANASTASIS_RecoveryState and #ANASTASIS_BackupAction or
+ * #ANASTASIS_RecoveryAction.
+ * The new #ANASTASIS_BackupState or #ANASTASIS_RecoveryState is returned
+ * by a callback function.
+ * This function can do network access to talk to anastasis service providers.
+ *
+ * @param state input state
+ * @param action what action to perform
+ * @param arguments data for the @a action
+ * @param cb function to call with the result
+ * @param cb_cls closure for @a cb
+ * @return failure state or new state
+ */
+void
+ANASTASIS_redux_action (const json_t *state,
+                        const char *action,
+                        const json_t *arguments,
+                        ANASTASIS_ActionCallback cb,
+                        void *cb_cls);
+
+
 #endif  /* _ANASTASIS_REDUX_H */
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 809308b..ae4c20c 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -101,6 +101,8 @@ libanastasisredux_la_LDFLAGS = \
   -version-info 0:0:0 \
   -no-undefined
 libanastasisredux_la_SOURCES = \
+  anastasis_api_redux.c \
+  anastasis_api_recovery_redux.c \
   anastasis_api_backup_redux.c
 libanastasisredux_la_LIBADD = \
   -lgnunetjson \
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index 9690ece..1a32f70 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -15,7 +15,7 @@
 */
 /**
  * @file lib/anastasis_api_backup_redux.c
- * @brief anastasis reducer api
+ * @brief anastasis reducer backup api
  * @author Christian Grothoff
  * @author Dominik Meister
  * @author Dennis Neufeld
@@ -26,22 +26,6 @@
 #include "anastasis_redux.h"
 #include "anastasis_error_codes.h"
 
-/**
- * JSON containing all supported countries and additional data.
- */
-json_t *redux_countries;
-
-/**
- * JSON containing country specific identity attributes to ask the user for.
- */
-json_t *redux_id_attr;
-
-/**
- * JSON containing anastasis providers.
- */
-json_t *provider_list;
-
-
 /**
  * Callback function FIXME: Description.
  *
@@ -57,97 +41,6 @@ typedef void
                    void *cb_cls);
 
 
-/**
- * Function to load json containing all countries.
- * Returns #GNUNET_OK if operation succeeded, else #GNUNET_SYSERR.
- *
- * @return int
- */
-static int
-redux_countries_init ()
-{
-  char *dn;
-  json_error_t error;
-
-  {
-    char *path;
-
-    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
-    if (NULL == path)
-    {
-      GNUNET_break (0);
-      return GNUNET_SYSERR;
-    }
-    GNUNET_asprintf (&dn,
-                     "%s/share/anastasis/redux.countries.json",
-                     path);
-    GNUNET_free (path);
-  }
-  redux_countries = json_load_file (dn, JSON_COMPACT, &error);
-  if (NULL == redux_countries)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to parse `%s': %s at %d:%d (%d)\n",
-                dn,
-                error.text,
-                error.line,
-                error.column,
-                error.position);
-    GNUNET_free (dn);
-    return GNUNET_SYSERR;
-  }
-  GNUNET_free (dn);
-  return GNUNET_OK;
-}
-
-
-/**
- * Function to load json containing country specific
- * identity attributes. Returns #GNUNET_OK if operation
- * succeeded, else #GNUNET_SYSERR.
- *
- * @param country_code country code (e.g. "de")
- * @return int
- */
-static int
-redux_id_attr_init (const char*country_code)
-{
-  char *dn;
-  json_error_t error;
-
-  {
-    char *path;
-
-    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
-    if (NULL == path)
-    {
-      GNUNET_break (0);
-      return GNUNET_SYSERR;
-    }
-    GNUNET_asprintf (&dn,
-                     "%s/share/anastasis/redux.%s.json",
-                     path,
-                     country_code);
-    GNUNET_free (path);
-  }
-  redux_id_attr = json_load_file (dn, JSON_COMPACT, &error);
-  if (NULL == redux_id_attr)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to parse `%s': %s at %d:%d (%d)\n",
-                dn,
-                error.text,
-                error.line,
-                error.column,
-                error.position);
-    GNUNET_free (dn);
-    return GNUNET_SYSERR;
-  }
-  GNUNET_free (dn);
-  return GNUNET_OK;
-}
-
-
 /**
  * Returns an initial ANASTASIS backup state.
  *
@@ -196,213 +89,6 @@ ANASTASIS_backup_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
 }
 
 
-static void
-select_continent (const json_t *state,
-                  const json_t *arguments,
-                  ANASTASIS_ActionCallback cb,
-                  void *cb_cls)
-{
-  json_t *new_state;
-  size_t index;
-  json_t *country;
-  json_t *root = json_object_get (redux_countries, "countries");
-  json_t *countries = json_array ();
-  json_t *continent = json_object_get (arguments, "continent");
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == continent)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  json_array_foreach (root, index, country)
-  {
-    json_t *temp_continent = json_object_get (country,
-                                              "continent");
-
-    if (1 == json_equal (continent, temp_continent))
-      json_array_append_new (countries, country);
-  }
-
-  json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxContinentSelectedState"));
-
-  json_object_set_new (new_state,
-                       "selected_continent",
-                       continent);
-
-  json_object_set_new (new_state,
-                       "countries",
-                       countries);
-
-  cb (cb_cls,
-      ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
-}
-
-
-static void
-select_country (const json_t *state,
-                const json_t *arguments,
-                ANASTASIS_ActionCallback cb,
-                void *cb_cls)
-{
-  json_t *new_state;
-  json_t *root;
-  json_t *country = json_object_get (arguments, "country_code");
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == country)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (GNUNET_SYSERR ==
-      redux_id_attr_init (json_string_value (country)))
-  {
-    GNUNET_break (0);
-    return NULL;
-  }
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  root = json_object_get (redux_id_attr, "required_attributes");
-
-  json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxCountrySelectedState"));
-
-  json_object_set_new (new_state,
-                       "selected_country",
-                       country);
-
-  json_object_set_new (new_state,
-                       "required_attributes",
-                       root);
-
-  cb (cb_cls,
-      ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
-}
-
-
-static void
-enter_user_attributes (const json_t *state,
-                       const json_t *arguments,
-                       ANASTASIS_ActionCallback cb,
-                       void *cb_cls)
-{
-  json_t *new_state;
-  json_t *attributes = json_object_get (arguments, "identity");
-  json_t *root;
-  char *dn;
-  json_error_t error;
-
-  {
-    char *path;
-
-    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
-    if (NULL == path)
-    {
-      GNUNET_break (0);
-      return GNUNET_SYSERR;
-    }
-    GNUNET_asprintf (&dn,
-                     "%s/share/anastasis/provider-list.json",
-                     path);
-    GNUNET_free (path);
-  }
-  provider_list = json_load_file (dn, JSON_COMPACT, &error);
-  if (NULL == provider_list)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to parse `%s': %s at %d:%d (%d)\n",
-                dn,
-                error.text,
-                error.line,
-                error.column,
-                error.position);
-    GNUNET_free (dn);
-    return GNUNET_SYSERR;
-  }
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == attributes)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  root = json_object_get (provider_list, "anastasis-provider");
-
-  json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxUserAttributesAddedState"));
-
-  json_object_set_new (new_state,
-                       "identity_attributes",
-                       attributes);
-
-  json_object_set_new (new_state,
-                       "provider-list",
-                       root);
-
-  cb (cb_cls,
-      ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
-}
-
-
 static void
 edit_user_attributes (const json_t *state,
                       const json_t *arguments,
@@ -447,66 +133,6 @@ edit_user_attributes (const json_t *state,
 }
 
 
-static void
-change_country (const json_t *state,
-                const json_t *arguments,
-                ANASTASIS_ActionCallback cb,
-                void *cb_cls)
-{
-  json_t *new_state;
-  json_t *root;
-  json_t *country = json_object_get (arguments, "country_code");
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == country)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (GNUNET_SYSERR ==
-      redux_id_attr_init (json_string_value (country)))
-  {
-    GNUNET_break (0);
-    return NULL;
-  }
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  root = json_object_get (redux_id_attr, "required_attributes");
-
-
-  json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxCountrySelectedState"));
-
-  json_object_set_new (new_state,
-                       "selected_country",
-                       country);
-
-  json_object_set_new (new_state,
-                       "required_attributes",
-                       root);
-
-  cb (cb_cls,
-      ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
-}
-
-
 static void
 add_authentication (const json_t *state,
                     const json_t *arguments,
@@ -583,26 +209,6 @@ ANASTASIS_backup_action (const json_t *state,
     const char *backup_action;
     DispatchHandler fun;
   } dispatchers[] = {
-    {
-      "ReduxInitialBackupState",
-      "selection_continent",
-      &select_continent
-    },
-    {
-      "ReduxContinentSelectedState",
-      "selection_country",
-      &select_country
-    },
-    {
-      "ReduxCountrySelectedState",
-      "entering_user_attributes",
-      &enter_user_attributes
-    },
-    {
-      "ReduxCountrySelectedState",
-      "changing_country",
-      &change_country
-    },
     {
       "ReduxUserAttributesAddedState",
       "adding_authentication",
@@ -613,11 +219,6 @@ ANASTASIS_backup_action (const json_t *state,
       "editing_user_attributes",
       &edit_user_attributes
     },
-    {
-      "ReduxUserAttributesAddedState",
-      "changing_country",
-      &change_country
-    },
     {
       "ReduxAuthenticationAddedState",
       "adding_policy",
diff --git a/src/lib/anastasis_api_recovery_redux.c 
b/src/lib/anastasis_api_recovery_redux.c
new file mode 100644
index 0000000..459168a
--- /dev/null
+++ b/src/lib/anastasis_api_recovery_redux.c
@@ -0,0 +1,142 @@
+/*
+  This file is part of Anastasis
+  Copyright (C) 2020 Taler Systems SA
+
+  Anastasis is free software; you can redistribute it and/or modify it under 
the
+  terms of the GNU Lesser General Public License as published by the Free 
Software
+  Foundation; either version 3, or (at your option) any later version.
+
+  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along with
+  Anastasis; see the file COPYING.GPL.  If not, see 
<http://www.gnu.org/licenses/>
+*/
+/**
+ * @file lib/anastasis_api_recovery_redux.c
+ * @brief anastasis reducer recovery api
+ * @author Christian Grothoff
+ * @author Dominik Meister
+ * @author Dennis Neufeld
+ */
+
+#include <platform.h>
+#include <jansson.h>
+#include "anastasis_redux.h"
+#include "anastasis_error_codes.h"
+
+/**
+ * Callback function FIXME: Description.
+ *
+ *  @param state FIXME: Description
+ *  @param arguments FIXME: Description
+ *  @param cb FIXME: Description
+ *  @param cb_cls FIXME: Description
+ */
+typedef void
+(*DispatchHandler)(const json_t *state,
+                   const json_t *arguments,
+                   ANASTASIS_ActionCallback cb,
+                   void *cb_cls);
+
+
+/**
+ * Returns an initial ANASTASIS recovery state.
+ *
+ * @return NULL on failure
+ */
+json_t *
+ANASTASIS_recovery_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  json_t *initial_state;
+  json_t *temp_country;
+  size_t index;
+  json_t *continents = json_array ();
+  json_t *root;
+
+  if (GNUNET_SYSERR == redux_countries_init ())
+  {
+    GNUNET_break (0);
+    return NULL;
+  }
+  root = json_object_get (redux_countries, "countries");
+
+  json_array_foreach (root, index, temp_country)
+  {
+    size_t inner_index;
+    json_t *inner_continent;
+    json_t *continent = json_object_get (json_array_get (root, index),
+                                         "continent");
+
+    bool existing = false;
+    json_array_foreach (continents, inner_index, inner_continent)
+    {
+      if (1 == json_equal (continent, inner_continent))
+      {
+        existing = true;
+        break;
+      }
+    }
+    if (! existing)
+      json_array_append_new (continents, continent);
+  }
+
+  initial_state = json_pack ("{s:s, s:o}",
+                             "recovery-state", "ReduxInitialRecoveryState",
+                             "continents", continents);
+  return initial_state;
+}
+
+
+/**
+ * Operates on a recovery state depending on given #ANASTASIS_RecoveryState
+ * and #ANASTASIS_RecoveryAction. The new #ANASTASIS_RecoveryState is returned
+ * by a callback function.
+ * This function can do network access to talk to anastasis service providers.
+ *
+ * @param state input state
+ * @param action what action to perform
+ * @param arguments data for the @a action
+ * @param cb function to call with the result
+ * @param cb_cls closure for @a cb
+ * @return failure state or new state
+ */
+void
+ANASTASIS_recovery_action (const json_t *state,
+                           const char *action,
+                           const json_t *arguments,
+                           ANASTASIS_ActionCallback cb,
+                           void *cb_cls)
+{
+  struct Dispatcher
+  {
+    const char *recovery_state;
+    const char *recovery_action;
+    DispatchHandler fun;
+  } dispatchers[] = {
+    { NULL, NULL, NULL }
+  };
+  const char *s = json_string_value (json_object_get (state,
+                                                      "recovery-state"));
+  if (NULL == s)
+  {
+    GNUNET_break (0);
+    cb (cb_cls,
+        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+        NULL);
+  }
+  for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
+  {
+    if ( (0 == strcmp (s, dispatchers[i].recovery_state)) &&
+         (0 == strcmp (action, dispatchers[i].recovery_action)) )
+    {
+      dispatchers[i].fun (state, arguments, cb, cb_cls);
+      return;
+    }
+  }
+  GNUNET_break (0);
+  cb (cb_cls,
+      ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+      NULL);
+}
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_redux.c
similarity index 70%
copy from src/lib/anastasis_api_backup_redux.c
copy to src/lib/anastasis_api_redux.c
index 9690ece..3793772 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -14,7 +14,7 @@
   Anastasis; see the file COPYING.GPL.  If not, see 
<http://www.gnu.org/licenses/>
 */
 /**
- * @file lib/anastasis_api_backup_redux.c
+ * @file lib/anastasis_api_redux.c
  * @brief anastasis reducer api
  * @author Christian Grothoff
  * @author Dominik Meister
@@ -41,6 +41,11 @@ json_t *redux_id_attr;
  */
 json_t *provider_list;
 
+/**
+ * backup or recovery mode
+ */
+static char *s_mode;
+
 
 /**
  * Callback function FIXME: Description.
@@ -63,7 +68,7 @@ typedef void
  *
  * @return int
  */
-static int
+int
 redux_countries_init ()
 {
   char *dn;
@@ -148,54 +153,6 @@ redux_id_attr_init (const char*country_code)
 }
 
 
-/**
- * Returns an initial ANASTASIS backup state.
- *
- * @return NULL on failure
- */
-json_t *
-ANASTASIS_backup_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
-{
-  json_t *initial_state;
-  json_t *temp_country;
-  size_t index;
-  json_t *continents = json_array ();
-  json_t *root;
-
-  if (GNUNET_SYSERR == redux_countries_init ())
-  {
-    GNUNET_break (0);
-    return NULL;
-  }
-  root = json_object_get (redux_countries, "countries");
-
-  json_array_foreach (root, index, temp_country)
-  {
-    size_t inner_index;
-    json_t *inner_continent;
-    json_t *continent = json_object_get (json_array_get (root, index),
-                                         "continent");
-
-    bool existing = false;
-    json_array_foreach (continents, inner_index, inner_continent)
-    {
-      if (1 == json_equal (continent, inner_continent))
-      {
-        existing = true;
-        break;
-      }
-    }
-    if (! existing)
-      json_array_append_new (continents, continent);
-  }
-
-  initial_state = json_pack ("{s:s, s:o}",
-                             "backup-state", "ReduxInitialBackupState",
-                             "continents", continents);
-  return initial_state;
-}
-
-
 static void
 select_continent (const json_t *state,
                   const json_t *arguments,
@@ -242,7 +199,7 @@ select_continent (const json_t *state,
   }
 
   json_object_set (new_state,
-                   "backup-state",
+                   s_mode,
                    json_string ("ReduxContinentSelectedState"));
 
   json_object_set_new (new_state,
@@ -301,7 +258,7 @@ select_country (const json_t *state,
   root = json_object_get (redux_id_attr, "required_attributes");
 
   json_object_set (new_state,
-                   "backup-state",
+                   s_mode,
                    json_string ("ReduxCountrySelectedState"));
 
   json_object_set_new (new_state,
@@ -319,134 +276,6 @@ select_country (const json_t *state,
 }
 
 
-static void
-enter_user_attributes (const json_t *state,
-                       const json_t *arguments,
-                       ANASTASIS_ActionCallback cb,
-                       void *cb_cls)
-{
-  json_t *new_state;
-  json_t *attributes = json_object_get (arguments, "identity");
-  json_t *root;
-  char *dn;
-  json_error_t error;
-
-  {
-    char *path;
-
-    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
-    if (NULL == path)
-    {
-      GNUNET_break (0);
-      return GNUNET_SYSERR;
-    }
-    GNUNET_asprintf (&dn,
-                     "%s/share/anastasis/provider-list.json",
-                     path);
-    GNUNET_free (path);
-  }
-  provider_list = json_load_file (dn, JSON_COMPACT, &error);
-  if (NULL == provider_list)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to parse `%s': %s at %d:%d (%d)\n",
-                dn,
-                error.text,
-                error.line,
-                error.column,
-                error.position);
-    GNUNET_free (dn);
-    return GNUNET_SYSERR;
-  }
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == attributes)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  root = json_object_get (provider_list, "anastasis-provider");
-
-  json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxUserAttributesAddedState"));
-
-  json_object_set_new (new_state,
-                       "identity_attributes",
-                       attributes);
-
-  json_object_set_new (new_state,
-                       "provider-list",
-                       root);
-
-  cb (cb_cls,
-      ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
-}
-
-
-static void
-edit_user_attributes (const json_t *state,
-                      const json_t *arguments,
-                      ANASTASIS_ActionCallback cb,
-                      void *cb_cls)
-{
-  json_t *new_state;
-  json_t *attributes = json_object_get (arguments, "identity");
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == attributes)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  json_object_set_new (new_state,
-                       "identity_attributes",
-                       attributes);
-
-  cb (cb_cls,
-      ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
-}
-
-
 static void
 change_country (const json_t *state,
                 const json_t *arguments,
@@ -489,7 +318,7 @@ change_country (const json_t *state,
 
 
   json_object_set (new_state,
-                   "backup-state",
+                   s_mode,
                    json_string ("ReduxCountrySelectedState"));
 
   json_object_set_new (new_state,
@@ -508,37 +337,81 @@ change_country (const json_t *state,
 
 
 static void
-add_authentication (const json_t *state,
-                    const json_t *arguments,
-                    ANASTASIS_ActionCallback cb,
-                    void *cb_cls)
+enter_user_attributes (const json_t *state,
+                       const json_t *arguments,
+                       ANASTASIS_ActionCallback cb,
+                       void *cb_cls)
 {
   json_t *new_state;
-  json_t *methods = json_object_get (arguments, "authentication_methods");
+  json_t *attributes = json_object_get (arguments, "identity");
+  json_t *root;
+  char *dn;
+  json_error_t error;
 
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
+  {
+    char *path;
+
+    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
+    if (NULL == path)
+    {
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+    GNUNET_asprintf (&dn,
+                     "%s/share/anastasis/provider-list.json",
+                     path);
+    GNUNET_free (path);
+  }
+  provider_list = json_load_file (dn, JSON_COMPACT, &error);
+  if (NULL == provider_list)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to parse `%s': %s at %d:%d (%d)\n",
+                dn,
+                error.text,
+                error.line,
+                error.column,
+                error.position);
+    GNUNET_free (dn);
+    return GNUNET_SYSERR;
+  }
+
+  if (NULL == state)
   {
     GNUNET_break (0);
     cb (cb_cls,
         ANASTASIS_EC_INVALID, // FIXME: Define correct error code
         NULL);
   }
-  if (NULL == methods)
+  if (NULL == attributes)
+  {
+    GNUNET_break (0);
+    cb (cb_cls,
+        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+        NULL);
+  }
+
+  new_state = json_deep_copy (state);
+  if (NULL == new_state)
   {
     GNUNET_break (0);
     cb (cb_cls,
         ANASTASIS_EC_INVALID, // FIXME: Define correct error code
         NULL);
   }
+  root = json_object_get (provider_list, "anastasis-provider");
 
   json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxAuthenticationAddedState"));
+                   s_mode,
+                   json_string ("ReduxUserAttributesAddedState"));
 
   json_object_set_new (new_state,
-                       "authentication_methods",
-                       methods);
+                       "identity_attributes",
+                       attributes);
+
+  json_object_set_new (new_state,
+                       "provider-list",
+                       root);
 
   cb (cb_cls,
       ANASTASIS_EC_NONE,
@@ -547,19 +420,11 @@ add_authentication (const json_t *state,
 }
 
 
-static void
-add_policy (const json_t *state,
-            const json_t *arguments,
-            ANASTASIS_ActionCallback cb,
-            void *cb_cls)
-{
-
-}
-
-
 /**
- * Operates on a backup state depending on given #ANASTASIS_BackupState
- * and #ANASTASIS_BackupAction. The new #ANASTASIS_BackupState is returned
+ * Operates on a state depending on given #ANASTASIS_BackupState
+ * or #ANASTASIS_RecoveryState and #ANASTASIS_BackupAction or
+ * #ANASTASIS_RecoveryAction.
+ * The new #ANASTASIS_BackupState or #ANASTASIS_RecoveryState is returned
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
@@ -571,16 +436,16 @@ add_policy (const json_t *state,
  * @return failure state or new state
  */
 void
-ANASTASIS_backup_action (const json_t *state,
-                         const char *action,
-                         const json_t *arguments,
-                         ANASTASIS_ActionCallback cb,
-                         void *cb_cls)
+ANASTASIS_redux_action (const json_t *state,
+                        const char *action,
+                        const json_t *arguments,
+                        ANASTASIS_ActionCallback cb,
+                        void *cb_cls)
 {
   struct Dispatcher
   {
-    const char *backup_state;
-    const char *backup_action;
+    const char *redux_state;
+    const char *redux_action;
     DispatchHandler fun;
   } dispatchers[] = {
     {
@@ -588,6 +453,11 @@ ANASTASIS_backup_action (const json_t *state,
       "selection_continent",
       &select_continent
     },
+    {
+      "ReduxInitialRecoveryState",
+      "selection_continent",
+      &select_continent
+    },
     {
       "ReduxContinentSelectedState",
       "selection_country",
@@ -598,59 +468,48 @@ ANASTASIS_backup_action (const json_t *state,
       "entering_user_attributes",
       &enter_user_attributes
     },
-    {
-      "ReduxCountrySelectedState",
-      "changing_country",
-      &change_country
-    },
-    {
-      "ReduxUserAttributesAddedState",
-      "adding_authentication",
-      &add_authentication
-    },
-    {
-      "ReduxUserAttributesAddedState",
-      "editing_user_attributes",
-      &edit_user_attributes
-    },
     {
       "ReduxUserAttributesAddedState",
       "changing_country",
       &change_country
     },
-    {
-      "ReduxAuthenticationAddedState",
-      "adding_policy",
-      &add_policy
-    },
-    {
-      "ReduxAuthenticationAddedState",
-      "editing_user_attributes",
-      &edit_user_attributes
-    },
     { NULL, NULL, NULL }
   };
+  s_mode = "backup-state";
   const char *s = json_string_value (json_object_get (state,
                                                       "backup-state"));
   if (NULL == s)
   {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
+    s = json_string_value (json_object_get (state,
+                                            "recovery-state"));
+    s_mode = "recovery-state";
+    if (NULL == s)
+    {
+      GNUNET_break (0);
+      cb (cb_cls,
+          ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+          NULL);
+    }
   }
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
-    if ( (0 == strcmp (s, dispatchers[i].backup_state)) &&
-         (0 == strcmp (action, dispatchers[i].backup_action)) )
+    if ( (0 == strcmp (s, dispatchers[i].redux_state)) &&
+         (0 == strcmp (action, dispatchers[i].redux_action)) )
     {
       dispatchers[i].fun (state, arguments, cb, cb_cls);
       return;
     }
   }
-  GNUNET_break (0);
-  cb (cb_cls,
-      ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-      NULL);
-
+  if (0 == strcmp (s_mode, "backup-state"))
+    ANASTASIS_backup_action (state,
+                             action,
+                             arguments,
+                             cb,
+                             NULL);
+  if (0 == strcmp (s_mode, "recovery-state"))
+    ANASTASIS_recovery_action (state,
+                               action,
+                               arguments,
+                               cb,
+                               NULL);
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]