From 9e58f9ae7208eeaf09a4b0af2f60996341d6af50 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 3 Dec 2018 16:25:23 +0100
Subject: Selected the right storage for each loaded binary.

---
 src/analysis/binary.c    | 88 +++++++++++++++++-------------------------------
 src/analysis/db/client.c |  1 -
 src/core/params.c        | 12 -------
 src/core/params.h        |  4 ---
 4 files changed, 31 insertions(+), 74 deletions(-)

diff --git a/src/analysis/binary.c b/src/analysis/binary.c
index 6cc3c74..4d6c8ba 100644
--- a/src/analysis/binary.c
+++ b/src/analysis/binary.c
@@ -48,7 +48,6 @@
 #include "../core/logs.h"
 #include "../core/params.h"
 #include "../core/processors.h"
-//#include "../glibext/chrysamarshal.h"
 #include "../glibext/gbinarycursor.h"
 #include "../glibext/gloadedpanel.h"
 #include "../gtkext/easygtk.h"
@@ -429,7 +428,6 @@ static bool g_loaded_binary_load_storage(GLoadedBinary *binary, xmlXPathContext
     char *value;                            /* Valeur lue à partie du XML  */
     char *access;                           /* Chemin d'accès à un élément */
     unsigned short port;                    /* Port de communication       */
-    bool use;                               /* Usage d'un serveur distant  */
     DBFeatures i;                           /* Boucle de parcours          */
 
     result = true;
@@ -522,6 +520,9 @@ static bool g_loaded_binary_load_storage(GLoadedBinary *binary, xmlXPathContext
 
     }
 
+    if (binary->use_remote)
+        g_loaded_binary_set_remote_storage_usage(binary, true);
+
  glbls_no_storage_config:
 
     free(storage_path);
@@ -705,10 +706,10 @@ void g_loaded_binary_set_remote_storage_usage(GLoadedBinary *binary, bool use)
 {
     binary->use_remote = use;
 
+    g_clear_object(&binary->remote);
+
     if (use)
-        /* TODO : reload conn ! */;
-    else
-        /* TODO : stop conn ! */;
+        g_loaded_binary_connect_remote(binary);
 
 }
 
@@ -821,13 +822,6 @@ static bool g_loaded_binary_connect_internal(GLoadedBinary *binary)
     GBinContent *content;                   /* Contenu bianire manipulé    */
     const gchar *checksum;                  /* Identifiant de binaire      */
 
-
-
-    char *host;                             /* Nom du serveur à contacter  */
-    int port;                               /* Numéro du port associé      */
-
-
-
     result = false;
 
     /* Utilisateur représenté */
@@ -854,19 +848,8 @@ static bool g_loaded_binary_connect_internal(GLoadedBinary *binary)
                                     checksum,
                                     binary->collections);
 
-
-
-
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_LOCAL_HOST, &host))
-        /* ... */;
-
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_LOCAL_PORT, &port))
-        /* ... */;
-
-
     result = g_db_client_start_internal(binary->local);
 
-
  glbcl_exit:
 
     return result;
@@ -895,15 +878,10 @@ static bool g_loaded_binary_connect_remote(GLoadedBinary *binary)
     GBinContent *content;                   /* Contenu bianire manipulé    */
     const gchar *checksum;                  /* Identifiant de binaire      */
 
-
-
-    char *host;                             /* Nom du serveur à contacter  */
-    int port;                               /* Numéro du port associé      */
-
-
-
     result = false;
 
+    assert(binary->remote == NULL);
+
     /* Utilisateur représenté */
 
     if (!g_generic_config_get_value(get_main_configuration(), MPK_AUTHOR_NAME, &tmp))
@@ -923,23 +901,21 @@ static bool g_loaded_binary_connect_remote(GLoadedBinary *binary)
 
     /* Tentative de connexion */
 
-    binary->local = g_db_client_new(author, priv,
-                                    g_loaded_binary_get_name(binary, false),
-                                    checksum,
-                                    binary->collections);
-
-
+    binary->remote = g_db_client_new(author, priv,
+                                     g_loaded_binary_get_name(binary, false),
+                                     checksum,
+                                     binary->collections);
 
+    result = g_db_client_start_remote(binary->local, binary->remote_host, binary->remote_port);
 
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_LOCAL_HOST, &host))
-        /* ... */;
-
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_LOCAL_PORT, &port))
-        /* ... */;
-
+    if (!result)
+    {
+        log_variadic_message(LMT_ERROR, _("Failed to connect to remote host '%s:%hu'"),
+                             binary->remote_host, binary->remote_port);
 
-    result = g_db_client_start_remote(binary->local, host, port);
+        g_clear_object(&binary->remote);
 
+    }
 
  glbcl_exit:
 
@@ -1129,10 +1105,13 @@ bool _g_loaded_binary_add_to_collection(GLoadedBinary *binary, GDbItem *item, bo
     {
         storage = g_loaded_binary_get_storage(binary, feature);
 
+        if (storage == DBS_ALL_REMOTE)
+            client = binary->remote;
+        else
+            client = NULL;
 
-        /* TODO : sélection du bon client... */
-        client = binary->local;
-
+        if (client == NULL)
+            client = binary->local;
 
         init_packed_buffer(&out_pbuf);
 
@@ -1147,8 +1126,6 @@ bool _g_loaded_binary_add_to_collection(GLoadedBinary *binary, GDbItem *item, bo
 
         exit_packed_buffer(&out_pbuf);
 
-
-
     }
 
     g_object_unref(G_OBJECT(collec));
@@ -1159,10 +1136,6 @@ bool _g_loaded_binary_add_to_collection(GLoadedBinary *binary, GDbItem *item, bo
 }
 
 
-
-
-
-
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : binary  = élément binaire à consulter.                       *
@@ -1199,10 +1172,13 @@ bool _g_loaded_binary_remove_from_collection(GLoadedBinary *binary, DBFeatures f
     {
         storage = g_loaded_binary_get_storage(binary, feature);
 
+        if (storage == DBS_ALL_REMOTE)
+            client = binary->remote;
+        else
+            client = NULL;
 
-        /* TODO : sélection du bon client... */
-        client = binary->local;
-
+        if (client == NULL)
+            client = binary->local;
 
         init_packed_buffer(&out_pbuf);
 
@@ -1217,8 +1193,6 @@ bool _g_loaded_binary_remove_from_collection(GLoadedBinary *binary, DBFeatures f
 
         exit_packed_buffer(&out_pbuf);
 
-
-
     }
 
     g_object_unref(G_OBJECT(collec));
diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c
index f97335c..25fe64d 100644
--- a/src/analysis/db/client.c
+++ b/src/analysis/db/client.c
@@ -164,7 +164,6 @@ static void g_db_client_init(GDbClient *client)
 static void g_db_client_finalize(GDbClient *client)
 {
     free(client->author);
-    free(client->key_file);
 
     unset_rle_string(&client->hash);
 
diff --git a/src/core/params.c b/src/core/params.c
index d71a0eb..8d9a035 100644
--- a/src/core/params.c
+++ b/src/core/params.c
@@ -159,18 +159,6 @@ bool load_main_config_parameters(void)
     param = g_generic_config_create_param(config, MPK_IDENTITY_VALIDITY, CPT_ULONG, 10 * 365 * 24 * 60 * 60);
     if (param == NULL) return false;
 
-    param = g_generic_config_create_param(config, MPK_REMOTE_HOST, CPT_STRING, "localhost");
-    if (param == NULL) return false;
-
-    param = g_generic_config_create_param(config, MPK_REMOTE_PORT, CPT_INTEGER, 9999);
-    if (param == NULL) return false;
-
-    param = g_generic_config_create_param(config, MPK_LOCAL_HOST, CPT_STRING, "localhost");
-    if (param == NULL) return false;
-
-    param = g_generic_config_create_param(config, MPK_LOCAL_PORT, CPT_INTEGER, 1337);
-    if (param == NULL) return false;
-
     param = g_generic_config_create_param(config, MPK_SERVER_BACKLOG, CPT_INTEGER, 20);
     if (param == NULL) return false;
 
diff --git a/src/core/params.h b/src/core/params.h
index da9dcb9..21fffd7 100644
--- a/src/core/params.h
+++ b/src/core/params.h
@@ -47,10 +47,6 @@
 #define MPK_IDENTITY_SERVER_OU  "cdb.identity.server.organisational_unit"
 #define MPK_IDENTITY_SERVER_CN  "cdb.identity.server.common_name"
 #define MPK_IDENTITY_VALIDITY   "cdb.identity.validity"
-#define MPK_REMOTE_HOST         "cdb.default.network.remote.server"
-#define MPK_REMOTE_PORT         "cdb.default.network.remote.port"
-#define MPK_LOCAL_HOST          "cdb.network.local.server"
-#define MPK_LOCAL_PORT          "cdb.network.local.port"
 #define MPK_SERVER_BACKLOG      "cdb.network.server.backlog"
 #define MPK_FORMAT_NO_NAME      "format.symbols.use_phy_instead_of_virt"
 #define MPK_INTERNAL_THEME      "gui.editor.theme"
-- 
cgit v0.11.2-87-g4458