From a4f2f3ec4b4cf7b894d6976c884fbc446396cd00 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 11 Apr 2016 23:46:03 +0200
Subject: Distinguished the internal server from the remote one using Unix
 sockets.

---
 ChangeLog                |  18 ++++
 src/analysis/binary.c    |   4 +-
 src/analysis/db/cdb.c    |  30 +++----
 src/analysis/db/client.c | 164 +++++++++++++++++++++++++++++-----
 src/analysis/db/client.h |   7 +-
 src/analysis/db/server.c | 226 +++++++++++++++++++++++++++++++++--------------
 src/analysis/db/server.h |   7 +-
 src/main.c               |   2 +-
 8 files changed, 342 insertions(+), 116 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5822754..8f0fc98 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+16-04-11  Cyrille Bagard <nocbos@gmail.com>
+
+	* src/analysis/binary.c:
+	* src/analysis/db/cdb.c:
+	Update code.
+
+	* src/analysis/db/client.c:
+	* src/analysis/db/client.h:
+	Distinguish the internal server from the remote one using Unix sockets.
+
+	* src/analysis/db/server.c:
+	* src/analysis/db/server.h:
+	Distinguish the internal server from the remote one using Unix sockets.
+	Delete the useless cdb_client structure.
+
+	* src/main.c:
+	Update code.
+
 16-04-10  Cyrille Bagard <nocbos@gmail.com>
 
 	* src/analysis/binary.c:
diff --git a/src/analysis/binary.c b/src/analysis/binary.c
index c71bc84..c422e29 100644
--- a/src/analysis/binary.c
+++ b/src/analysis/binary.c
@@ -1047,7 +1047,7 @@ static bool g_loaded_binary_connect_internal(GLoadedBinary *binary)
         /* ... */;
 
 
-    result = g_db_client_start(binary->local, host, port);
+    result = g_db_client_start_internal(binary->local);
 
     printf("DB status :: %d\n", result);
 
@@ -1123,7 +1123,7 @@ static bool g_loaded_binary_connect_remote(GLoadedBinary *binary)
         /* ... */;
 
 
-    result = g_db_client_start(binary->local, host, port);
+    result = g_db_client_start_remote(binary->local, host, port);
 
     printf("DB status :: %d\n", result);
 
diff --git a/src/analysis/db/cdb.c b/src/analysis/db/cdb.c
index a42f838..e6a7005 100644
--- a/src/analysis/db/cdb.c
+++ b/src/analysis/db/cdb.c
@@ -49,7 +49,6 @@
 #include "../../common/extstr.h"
 #include "../../common/io.h"
 #include "../../common/pathname.h"
-#include "../../common/xdg.h"
 #include "../../common/xml.h"
 #include "../../core/collections.h"
 
@@ -256,6 +255,7 @@ static void g_cdb_archive_finalize(GCdbArchive *archive)
 {
     //void close_xml_file(xmlDocPtr, xmlXPathContextPtr);
 
+    free(archive->filename);
 
 
     G_OBJECT_CLASS(g_cdb_archive_parent_class)->finalize(G_OBJECT(archive));
@@ -265,10 +265,10 @@ static void g_cdb_archive_finalize(GCdbArchive *archive)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : owner = description humaine du serveur d'accueil.            *
-*                hash  = empreinte du binaire à représenter.                  *
-*                user  = désignation d'un éventuel nouveau créateur.          *
-*                error = indication éventuelle en cas d'échec. [OUT]          *
+*  Paramètres  : basedir = répertoire de stockage des enregistrements.        *
+*                hash    = empreinte du binaire à représenter.                *
+*                user    = désignation d'un éventuel nouveau créateur.        *
+*                error   = indication éventuelle en cas d'échec. [OUT]        *
 *                                                                             *
 *  Description : Définit ou ouvre une archive d'éléments utilisateur.         *
 *                                                                             *
@@ -279,10 +279,9 @@ static void g_cdb_archive_finalize(GCdbArchive *archive)
 *                                                                             *
 ******************************************************************************/
 
-GCdbArchive *g_cdb_archive_new(const char *owner, const rle_string *hash, const rle_string *user, DBError *error)
+GCdbArchive *g_cdb_archive_new(const char *basedir, const rle_string *hash, const rle_string *user, DBError *error)
 {
     GCdbArchive *result;                    /* Adresse à retourner         */
-    char *suffix;                           /* Fin du nom de fichier       */
     struct stat finfo;                      /* Information sur l'archive   */
     int ret;                                /* Retour d'un appel           */
 
@@ -292,17 +291,10 @@ GCdbArchive *g_cdb_archive_new(const char *owner, const rle_string *hash, const
 
     /* Chemin de l'archive */
 
-    suffix = strdup("chrysalide" G_DIR_SEPARATOR_S);
-    suffix = stradd(suffix, owner);
-    suffix = stradd(suffix, G_DIR_SEPARATOR_S);
-    suffix = stradd(suffix, hash->data);
-    suffix = stradd(suffix, ".tar.xz");
-
-    result->filename = get_xdg_config_dir(suffix);
-
-    printf("dealing with '%s'...\n", result->filename);
-
-    free(suffix);
+    result->filename = strdup(basedir);
+    result->filename = stradd(result->filename, G_DIR_SEPARATOR_S);
+    result->filename = stradd(result->filename, hash->data);
+    result->filename = stradd(result->filename, ".tar.xz");
 
     if (!mkpath(result->filename))
         goto gcan_error;
@@ -894,8 +886,6 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
 
         /* Lancement d'une phase de surveillance */
 
-        printf("(%p) POLL %d\n", archive, nfds);
-
         ret = poll(fds, nfds, -1);
         if (ret == -1)
         {
diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c
index bb900ed..0598e32 100644
--- a/src/analysis/db/client.c
+++ b/src/analysis/db/client.c
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 
 
 #include <i18n.h>
@@ -39,6 +40,7 @@
 #include "protocol.h"
 #include "misc/rlestr.h"
 #include "../../common/io.h"
+#include "../../common/xdg.h"
 #include "../../gui/panels/log.h"
 
 
@@ -80,6 +82,9 @@ static void g_db_client_init(GDbClient *);
 /* Procède à la libération totale de la mémoire. */
 static void g_db_client_finalize(GDbClient *);
 
+/* Démarre réellement la connexion à la base de données. */
+static bool g_db_client_start_common(GDbClient *, const char *);
+
 /* Assure l'accueil des nouvelles mises à jour. */
 static void *g_db_client_update(GDbClient *);
 
@@ -193,10 +198,83 @@ GDbClient *g_db_client_new(char *author, char *kfile, const char *name, const ch
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : client = client pour les accès distants à manipuler.         *
+*                                                                             *
+*  Description : Démarre la connexion à la base de données interne.           *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool g_db_client_start_internal(GDbClient *client)
+{
+    char *suffix;                           /* Suffixe pour un fichier     */
+    char *sock_path;                        /* Chemin vers le canal UNIX   */
+    struct sockaddr_un addr;                /* Adresse de transmission     */
+    int ret;                                /* Bilan d'un appel            */
+    char *desc;                             /* Description du serveur ciblé*/
+    bool status;                            /* Bilan de la connexion       */
+
+    /* Identification du serveur à contacter */
+
+    asprintf(&suffix, "chrysalide" G_DIR_SEPARATOR_S ".internal_server.%d", getpid());
+    sock_path = get_xdg_config_dir(suffix);
+    free(suffix);
+
+    memset(&addr, 0, sizeof(struct sockaddr_un));
+
+#define UNIX_PATH_MAX 108
+
+    addr.sun_family = AF_UNIX;
+    strncpy(addr.sun_path, sock_path, UNIX_PATH_MAX - 1);
+
+    free(sock_path);
+
+    /* Création d'un canal de communication */
+
+    client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (client->fd == -1)
+    {
+        perror("socket");
+        return false;
+    }
+
+    ret = connect(client->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
+    if (ret == -1)
+    {
+        perror("connect");
+        goto gdcsi_no_listening;
+    }
+
+    asprintf(&desc, "unix://.internal_server.%d", getpid());
+
+    status = g_db_client_start_common(client, desc);
+
+    free(desc);
+
+    if (!status)
+        goto gdcsi_no_listening;
+
+    return true;
+
+ gdcsi_no_listening:
+
+    close(client->fd);
+    client->fd = -1;
+
+    return false;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : client = client pour les accès distants à manipuler.         *
 *                host   = hôte à représenter pour le service.                 *
 *                port   = port de connexion pour les clients.                 *
 *                                                                             *
-*  Description : Démarre la connexion à la base de données.                   *
+*  Description : Démarre la connexion à la base de données distante.          *
 *                                                                             *
 *  Retour      : Bilan de l'opération.                                        *
 *                                                                             *
@@ -204,18 +282,13 @@ GDbClient *g_db_client_new(char *author, char *kfile, const char *name, const ch
 *                                                                             *
 ******************************************************************************/
 
-bool g_db_client_start(GDbClient *client, const char *host, unsigned short port)
+bool g_db_client_start_remote(GDbClient *client, const char *host, unsigned short port)
 {
     struct hostent *hp;                     /* Informations sur l'hôte     */
     struct sockaddr_in addr;                /* Adresse de transmission     */
     int ret;                                /* Bilan d'un appel            */
-    rle_string user;                        /* Nom d'utilisateur associé   */
-    GChecksum *checksum;                    /* Empreinte MD5 à signer      */
-    unsigned char md5_digest[16];           /* Empreinte MD5 calculée      */
-    RSA *key;                               /* Clef pour la signature      */
-    unsigned char sig[RSA_USED_SIZE];       /* Signature effectuée         */
-    uint32_t data;                          /* Mot de données lues         */
-    DBError error;                          /* Validation de la connexion  */
+    char *desc;                             /* Description du serveur ciblé*/
+    bool status;                            /* Bilan de la connexion       */
 
     /* Identification du serveur à contacter */
 
@@ -240,9 +313,54 @@ bool g_db_client_start(GDbClient *client, const char *host, unsigned short port)
     if (ret == -1)
     {
         perror("connect");
-        goto gdcs_no_listening;
+        goto gdcsr_no_listening;
     }
 
+    asprintf(&desc, "%s:%hu", host, port);
+
+    status = g_db_client_start_common(client, desc);
+
+    free(desc);
+
+    if (!status)
+        goto gdcsr_no_listening;
+
+    return true;
+
+ gdcsr_no_listening:
+
+    close(client->fd);
+    client->fd = -1;
+
+    return false;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : client = client pour les accès distants à manipuler.         *
+*                host   = hôte à représenter pour le service.                 *
+*                port   = port de connexion pour les clients.                 *
+*                                                                             *
+*  Description : Démarre réellement la connexion à la base de données.        *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static bool g_db_client_start_common(GDbClient *client, const char *desc)
+{
+    rle_string user;                        /* Nom d'utilisateur associé   */
+    GChecksum *checksum;                    /* Empreinte MD5 à signer      */
+    unsigned char md5_digest[16];           /* Empreinte MD5 calculée      */
+    RSA *key;                               /* Clef pour la signature      */
+    unsigned char sig[RSA_USED_SIZE];       /* Signature effectuée         */
+    uint32_t data;                          /* Mot de données lues         */
+    DBError error;                          /* Validation de la connexion  */
+
     /**
      * On réalise l'envoi initial ; le premier paquet doit contenir :
      *    - la commande 'DBC_HELO'.
@@ -296,7 +414,7 @@ bool g_db_client_start(GDbClient *client, const char *host, unsigned short port)
 
     if (be32toh(data) != DBC_WELCOME)
     {
-        log_variadic_message(LMT_ERROR, _("The server '%s:%hu' did not welcome us!"), host, port);
+        log_variadic_message(LMT_ERROR, _("The server '%s' did not welcome us!"), desc);
         goto gdcs_error;
     }
 
@@ -308,30 +426,30 @@ bool g_db_client_start(GDbClient *client, const char *host, unsigned short port)
     switch (error)
     {
         case DBE_NONE:
-            log_variadic_message(LMT_INFO, _("Connected to the server '%s:%hu'!"), host, port);
+            log_variadic_message(LMT_INFO, _("Connected to the server '%s'!"), desc);
             break;
 
         case DBE_WRONG_VERSION:
-            log_variadic_message(LMT_ERROR, _("The server '%s:%hu' does not use our protocol version (0x%08x)..."),
-                                 host, port, CDB_PROTOCOL_VERSION);
+            log_variadic_message(LMT_ERROR, _("The server '%s' does not use our protocol version (0x%08x)..."),
+                                 desc, CDB_PROTOCOL_VERSION);
             goto gdcs_error;
             break;
 
         case DBE_XML_VERSION_ERROR:
-            log_variadic_message(LMT_ERROR, _("The archive from the server '%s:%hu' does not use our protocol version (0x%08x)..."),
-                                 host, port, CDB_PROTOCOL_VERSION);
+            log_variadic_message(LMT_ERROR, _("The archive from the server '%s' does not use our protocol version (0x%08x)..."),
+                                 desc, CDB_PROTOCOL_VERSION);
             goto gdcs_error;
             break;
 
         case DBE_DB_LOADING_ERROR:
-            log_variadic_message(LMT_ERROR, _("The server '%s:%hu' got into troubles while loading the database...."),
-                                 host, port);
+            log_variadic_message(LMT_ERROR, _("The server '%s' got into troubles while loading the database...."),
+                                 desc);
             goto gdcs_error;
             break;
 
         default:
-            log_variadic_message(LMT_ERROR, _("The server '%s:%hu' has run into an error (%u)..."),
-                                 host, port, error);
+            log_variadic_message(LMT_ERROR, _("The server '%s' has run into an error (%u)..."),
+                                 desc, error);
             goto gdcs_error;
             break;
 
@@ -340,8 +458,8 @@ bool g_db_client_start(GDbClient *client, const char *host, unsigned short port)
     client->update = g_thread_try_new("cdb_client", (GThreadFunc)g_db_client_update, client, NULL);
     if (client->update == NULL)
     {
-        log_variadic_message(LMT_ERROR, _("Failed to start a listening thread for the server '%s:%hu'!"),
-                             host, port);
+        log_variadic_message(LMT_ERROR, _("Failed to start a listening thread for the server '%s'!"),
+                             desc);
         goto gdcs_error;
     }
 
@@ -351,8 +469,6 @@ bool g_db_client_start(GDbClient *client, const char *host, unsigned short port)
 
     unset_rle_string(&user);
 
- gdcs_no_listening:
-
     close(client->fd);
     client->fd = -1;
 
diff --git a/src/analysis/db/client.h b/src/analysis/db/client.h
index 749b8aa..e17c347 100644
--- a/src/analysis/db/client.h
+++ b/src/analysis/db/client.h
@@ -53,8 +53,11 @@ GType g_db_client_get_type(void);
 /* Prépare un client pour une connexion à une BD. */
 GDbClient *g_db_client_new(char *, char *, const char *, const char *, GList *);
 
-/* Démarre la connexion à la base de données. */
-bool g_db_client_start(GDbClient *, const char *, unsigned short);
+/* Démarre la connexion à la base de données interne. */
+bool g_db_client_start_internal(GDbClient *);
+
+/* Démarre la connexion à la base de données distante. */
+bool g_db_client_start_remote(GDbClient *, const char *, unsigned short);
 
 /* Arrête la connexion à la base de données. */
 void g_db_client_stop(GDbClient *);
diff --git a/src/analysis/db/server.c b/src/analysis/db/server.c
index 360b266..48126e8 100644
--- a/src/analysis/db/server.c
+++ b/src/analysis/db/server.c
@@ -32,6 +32,7 @@
 #include <unistd.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 
 
 #include "cdb.h"
@@ -39,6 +40,7 @@
 #include "protocol.h"
 #include "misc/rlestr.h"
 #include "../../common/io.h"
+#include "../../common/xdg.h"
 #include "../../core/params.h"
 #include "../../gui/panels/log.h"
 
@@ -52,17 +54,17 @@ typedef struct _registered_user
 
 } registered_user;
 
-/* Informations relatives à un client */
-typedef struct _cdb_client
+/* Format générique des adresses de connexion */
+typedef union _gen_sockaddr_t
 {
-    GDbServer *server;                      /* Accès facile en mémoire     */
+    struct sockaddr_in inet_addr;           /* Adresse d'écoute IPv4       */
+    struct sockaddr_un unix_addr;           /* Adresse d'écoute Unix       */
 
-    int fd;                                 /* Canal de communication      */
-    struct sockaddr_in peer;                /* Adresse distante            */
-
-    GThread *thread;                        /* Procédure de traitement     */
+} gen_sockaddr_t;
 
-} cdb_client;
+#ifndef UNIX_PATH_MAX
+#   define UNIX_PATH_MAX 108
+#endif
 
 /* Description de serveur à l'écoute (instance) */
 struct _GDbServer
@@ -73,15 +75,16 @@ struct _GDbServer
     size_t users_count;                     /* Nombre d'enregistrés        */
 
     int fd;                                 /* Canal de communication      */
-    char *hostname;                         /* Désignation humaine         */
+    int domain;                             /* Domaine du canal            */
+    gen_sockaddr_t addr;                    /* Adresse d'écoute générique  */
+    socklen_t sock_len;                     /* Taille de cette adresse     */
     char *desc;                             /* Désignation du serveur      */
-    struct sockaddr_in addr;                /* Adresse d'écoute            */
+
+    char *basedir;                          /* Répertoire de stockage      */
 
     GThread *listener;                      /* Procédure de traitement     */
 
     GList *archives;                        /* Liste des binaires ouverts  */
-    cdb_client **clients;                   /* Connexions en place         */
-    size_t count;                           /* Quantité de clients         */
     GMutex mutex;                           /* Verrou pour l'accès         */
 
 };
@@ -181,7 +184,7 @@ static void g_db_server_finalize(GDbServer *server)
 {
     g_db_server_unregister_all_user(server);
 
-    free(server->hostname);
+    free(server->desc);
 
     G_OBJECT_CLASS(g_db_server_parent_class)->finalize(G_OBJECT(server));
 
@@ -192,10 +195,8 @@ static void g_db_server_finalize(GDbServer *server)
 *                                                                             *
 *  Paramètres  : author = utilisateur à représenter via le client.            *
 *                kfile  = clef menant à sa clef publique.                     *
-*                host = hôte à représenter pour le service.                   *
-*                port = port de connexion pour les clients.                   *
 *                                                                             *
-*  Description : Prépare un serveur de BD pour les clients.                   *
+*  Description : Prépare un serveur de BD pour les clients internes.          *
 *                                                                             *
 *  Retour      : Structure mise en place ou NULL en cas d'échec.              *
 *                                                                             *
@@ -203,18 +204,97 @@ static void g_db_server_finalize(GDbServer *server)
 *                                                                             *
 ******************************************************************************/
 
-GDbServer *g_db_server_new(const char *author, char *kfile, const char *host, short port)
+GDbServer *g_db_server_new_internal(const char *author, char *kfile)
 {
     GDbServer *result;                      /* Adresse à retourner         */
+    bool ret;                               /* Bilan d'un appel            */
+    char *suffix;                           /* Suffixe pour un fichier     */
+    char *sock_path;                        /* Chemin vers le canal UNIX   */
+
+    result = g_object_new(G_TYPE_DB_SERVER, NULL);
+
+    /* Chargement du profil */
+
+    ret = g_db_server_register_user(result, author, kfile);
+    if (!ret) goto gdsni_error;
+
+    /* Détermination du point d'écoute */
+
+    result->domain = AF_UNIX;
+
+    asprintf(&suffix, "chrysalide" G_DIR_SEPARATOR_S ".internal_server.%d", getpid());
+    sock_path = get_xdg_config_dir(suffix);
+    free(suffix);
+
+    memset(&result->addr, 0, sizeof(struct sockaddr_un));
+
+    result->addr.unix_addr.sun_family = AF_UNIX;
+    strncpy(result->addr.unix_addr.sun_path, sock_path, UNIX_PATH_MAX - 1);
+
+    free(sock_path);
+
+    result->sock_len = sizeof(struct sockaddr_un);
+
+    /* Désignation humaine */
+
+    asprintf(&result->desc, "unix://.internal_server.%d", getpid());
+
+    /* Répertoire de stockage */
+
+    result->basedir = get_xdg_config_dir("chrysalide" G_DIR_SEPARATOR_S "cdbs");
+
+    return result;
+
+ gdsni_error:
+
+    g_object_unref(G_OBJECT(result));
+
+    return NULL;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : conf = fichier de configuration à interpréter.               *
+*                                                                             *
+*  Description : Prépare un serveur de BD pour les clients distants.          *
+*                                                                             *
+*  Retour      : Structure mise en place ou NULL en cas d'échec.              *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GDbServer *g_db_server_new_remote(const char *conf)
+{
+    GDbServer *result;                      /* Adresse à retourner         */
+
+    char *host;
+    short port;
+
+
     struct hostent *hp;                     /* Informations sur l'hôte     */
     size_t desclen;                         /* Taille de désignation       */
     const char *ip;                         /* Adresse IPv4 ou IPv6        */
 
     result = g_object_new(G_TYPE_DB_SERVER, NULL);
 
-    /* ... =*/g_db_server_register_user(result, author, kfile);
+    /* Chargement des profils */
 
-    result->hostname = strdup(host);
+
+    /*
+    ret = g_db_server_register_user(result, author, kfile);
+    if (!ret) goto gdsni_error;
+    */
+
+    result->domain = AF_INET;
+
+    host = "localhost";
+    port = 9999;
+
+
+    /* Détermination du point d'écoute */
 
     hp = gethostbyname(host);
     if (hp == NULL)
@@ -223,23 +303,33 @@ GDbServer *g_db_server_new(const char *author, char *kfile, const char *host, sh
         goto gdsn_error;
     }
 
-    result->addr.sin_family = hp->h_addrtype;
-    memcpy(&result->addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
+    result->addr.inet_addr.sin_family = hp->h_addrtype;
+    memcpy(&result->addr.inet_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
+
+    result->addr.inet_addr.sin_port = htons(port);
 
-    result->addr.sin_port = htons(port);
+    result->sock_len = sizeof(struct sockaddr_in);
 
     desclen = INET6_ADDRSTRLEN + 1 + 5 + 1;
     result->desc = (char *)calloc(desclen, sizeof(char));
 
-    ip = inet_ntop(AF_INET, &result->addr.sin_addr, result->desc, INET6_ADDRSTRLEN);
+    ip = inet_ntop(AF_INET, &result->addr.inet_addr.sin_addr, result->desc, INET6_ADDRSTRLEN);
     if (ip == NULL)
     {
         perror("inet_ntop");
         goto gdsn_error;
     }
 
+    /* Désignation humaine */
+
     snprintf(result->desc + strlen(ip), 1 + 5, ":%hu", port);
 
+    /* Répertoire de stockage */
+
+
+    result->basedir = strdup("/tmp/"); /* TODO */
+
+
     return result;
 
  gdsn_error:
@@ -382,10 +472,10 @@ static void *g_db_server_listener(GDbServer *server)
 {
     struct pollfd fds;                      /* Surveillance des flux       */
     int ret;                                /* Bilan d'un appel            */
-    struct sockaddr_in peer;                /* Adresse cliente             */
+    gen_sockaddr_t peer;                    /* Adresse cliente             */
     int fd;                                 /* Canal établi vers un client */
-    char source[INET6_ADDRSTRLEN];          /* Adresse du client (IPv4/6)  */
     const char *ip;                         /* Statut de la conversion     */
+    char *peer_name;                        /* Désignation du correspondant*/
     DBError error;                          /* Validation de la connexion  */
     GCdbArchive *archive;                   /* Destinataire final du client*/
     uint32_t cmd;                           /* Commande initiale lue       */
@@ -409,18 +499,31 @@ static void *g_db_server_listener(GDbServer *server)
 
         if (fds.revents & (POLLIN | POLLPRI))
         {
-            fd = accept(server->fd, &peer, (socklen_t []) { sizeof(struct sockaddr_in) });
+            fd = accept(server->fd, (struct sockaddr *)&peer, (socklen_t []) { sizeof(gen_sockaddr_t) });
             if (fd == -1)
             {
                 perror("accept");
                 continue;
             }
 
-            ip = inet_ntop(AF_INET, &peer.sin_addr, source, sizeof(source));
-            if (ip == NULL)
+            /* Construction d'une représentation */
+
+            if (*((sa_family_t *)&peer) == AF_UNIX)
+                peer_name = strdup(server->desc);
+
+            else if (*((sa_family_t *)&peer) == AF_INET)
             {
-                perror("inet_ntop");
-                goto gdsl_error;
+                peer_name = (char *)calloc(INET6_ADDRSTRLEN + 1 + 5 + 1, sizeof(char));
+
+                ip = inet_ntop(AF_INET, &peer.inet_addr.sin_addr, peer_name, INET6_ADDRSTRLEN);
+                if (ip == NULL)
+                {
+                    perror("inet_ntop");
+                    goto gdsl_error;
+                }
+
+                snprintf(peer_name + strlen(ip), 1 + 5, ":%hu", ntohs(peer.inet_addr.sin_port));
+
             }
 
             error = DBE_NONE;
@@ -439,72 +542,72 @@ static void *g_db_server_listener(GDbServer *server)
 
             if (!safe_recv(fd, &cmd, sizeof(uint32_t), 0))
             {
-                log_variadic_message(LMT_ERROR, _("Error while getting the initial command from '%s:%hu'..."),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("Error while getting the initial command from '%s'..."),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (!safe_recv(fd, &version, sizeof(uint32_t), 0))
             {
-                log_variadic_message(LMT_ERROR, _("Error while getting the protocol version from '%s:%hu'..."),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("Error while getting the protocol version from '%s'..."),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (!recv_rle_string(&hash, fd, 0))
             {
-                log_variadic_message(LMT_ERROR, _("Error while getting the binary hash from '%s:%hu'..."),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("Error while getting the binary hash from '%s'..."),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (!recv_rle_string(&user, fd, 0))
             {
-                log_variadic_message(LMT_ERROR, _("Error while getting the user name from '%s:%hu'..."),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("Error while getting the user name from '%s'..."),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (!safe_recv(fd, sig, RSA_USED_SIZE, 0))
             {
-                log_variadic_message(LMT_ERROR, _("Error while getting the signature from '%s:%hu'..."),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("Error while getting the signature from '%s'..."),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (be32toh(cmd) != DBC_HELO)
             {
-                log_variadic_message(LMT_ERROR, _("The client from '%s:%hu' did not introduce itself!"),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("The client from '%s' did not introduce itself!"),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (be32toh(version) != CDB_PROTOCOL_VERSION)
             {
-                log_variadic_message(LMT_ERROR, _("The client from '%s:%hu' does not use the same protocol: 0x%08x vs 0x%08x..."),
-                                     source, ntohs(peer.sin_port), be32toh(version), CDB_PROTOCOL_VERSION);
+                log_variadic_message(LMT_ERROR, _("The client from '%s' does not use the same protocol: 0x%08x vs 0x%08x..."),
+                                     peer_name, be32toh(version), CDB_PROTOCOL_VERSION);
                 error = DBE_WRONG_VERSION;
                 goto gdsl_error_sending;
             }
 
             if (is_rle_string_empty(&hash))
             {
-                log_variadic_message(LMT_ERROR, _("The submitted binary hash from '%s:%hu' is empty!"),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("The submitted binary hash from '%s' is empty!"),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
 
             if (is_rle_string_empty(&user))
             {
-                log_variadic_message(LMT_ERROR, _("No user is associated with the client from '%s:%hu'..."),
-                                     source, ntohs(peer.sin_port));
+                log_variadic_message(LMT_ERROR, _("No user is associated with the client from '%s'..."),
+                                     peer_name);
                 error = DBE_BAD_EXCHANGE;
                 goto gdsl_error_sending;
             }
@@ -533,7 +636,7 @@ static void *g_db_server_listener(GDbServer *server)
             }
 
             if (iter == NULL)
-                archive = g_cdb_archive_new(server->desc, &hash, &user, &error);
+                archive = g_cdb_archive_new(server->basedir, &hash, &user, &error);
 
             /**
              * Le serveur doit répondre pour un message type :
@@ -563,6 +666,8 @@ static void *g_db_server_listener(GDbServer *server)
                 server->archives = g_list_append(server->archives, archive);
                 error = g_cdb_archive_add_client(archive, fd, &user);
 
+                free(peer_name);
+
                 exit_rle_string(&hash);
                 exit_rle_string(&user);
 
@@ -574,6 +679,8 @@ static void *g_db_server_listener(GDbServer *server)
 
  gdsl_error:
 
+            free(peer_name);
+
             exit_rle_string(&hash);
             exit_rle_string(&user);
 
@@ -605,7 +712,7 @@ bool g_db_server_start(GDbServer *server)
     int ret;                                /* Bilan d'un appel            */
     int backlog;                            /* Nombre de connexions maximal*/
 
-    server->fd = socket(AF_INET, SOCK_STREAM, 0);
+    server->fd = socket(server->domain, SOCK_STREAM, 0);
     if (server->fd == -1)
     {
         perror("socket");
@@ -616,10 +723,11 @@ bool g_db_server_start(GDbServer *server)
     if (ret == -1)
     {
         perror("setsockopt");
+        exit(0);
         goto gdss_error;
     }
 
-    ret = bind(server->fd, (struct sockaddr *)&server->addr, sizeof(struct sockaddr_in));
+    ret = bind(server->fd, (struct sockaddr *)&server->addr, server->sock_len);
     if (ret == -1)
     {
         perror("bind");
@@ -638,8 +746,7 @@ bool g_db_server_start(GDbServer *server)
 
     server->listener = g_thread_new("cdb_listener", (GThreadFunc)g_db_server_listener, server);
 
-    log_variadic_message(LMT_PROCESS, _("Server started and listening at %s:%hu"),
-                         server->hostname, ntohs(server->addr.sin_port));
+    log_variadic_message(LMT_PROCESS, _("Server started and listening at %s"), server->desc);
 
     return true;
 
@@ -667,9 +774,6 @@ bool g_db_server_start(GDbServer *server)
 
 void g_db_server_stop(GDbServer *server)
 {
-    size_t i;                               /* Boucle de parcours          */
-    GThread *thread;                        /* Procédure de traitement     */
-
     if (server->fd != -1)
         return;
 
@@ -678,14 +782,6 @@ void g_db_server_stop(GDbServer *server)
 
     g_thread_join(server->listener);
 
-    for (i = 0; i < server->count; i++)
-    {
-        /* Sauvegarde de la référene, qui peut disparaître */
-        thread = server->clients[i]->thread;
-
-        close(server->clients[i]->fd);
-        g_thread_join(thread);
-
-    }
+    /* TODO : s'occuper des archives ouvertes */
 
 }
diff --git a/src/analysis/db/server.h b/src/analysis/db/server.h
index a06e89f..8d3c9b6 100644
--- a/src/analysis/db/server.h
+++ b/src/analysis/db/server.h
@@ -47,8 +47,11 @@ typedef struct _GDbServerClass GDbServerClass;
 /* Indique le type défini pour une description de serveur à l'écoute. */
 GType g_db_server_get_type(void);
 
-/* Prépare un serveur de BD pour les clients. */
-GDbServer *g_db_server_new(const char *, char *, const char *, short);
+/* Prépare un serveur de BD pour les clients internes. */
+GDbServer *g_db_server_new_internal(const char *, char *);
+
+/* Prépare un serveur de BD pour les clients distants. */
+GDbServer *g_db_server_new_remote(const char *);
 
 /* Démarre le serveur de base de données. */
 bool g_db_server_start(GDbServer *);
diff --git a/src/main.c b/src/main.c
index 09fcf42..ec379bc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -193,7 +193,7 @@ int main(int argc, char **argv)
     pub = get_xdg_config_dir("chrysalide" G_DIR_SEPARATOR_S "id_rsa.pub");
 
 
-    server = g_db_server_new(author, pub, "localhost", 1337);
+    server = g_db_server_new_internal(author, pub);
     g_db_server_start(server);
 
     /* Charge le dernier projet */
-- 
cgit v0.11.2-87-g4458