summaryrefslogtreecommitdiff
path: root/src/analysis/db/client.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2016-04-11 21:46:03 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2016-04-11 21:46:03 (GMT)
commita4f2f3ec4b4cf7b894d6976c884fbc446396cd00 (patch)
tree0ef60529d585eb0e90b2df7eae60bdf4b1e327d3 /src/analysis/db/client.c
parent5ad85cf30b2355ca727904d1a0d25240283813b3 (diff)
Distinguished the internal server from the remote one using Unix sockets.
Diffstat (limited to 'src/analysis/db/client.c')
-rw-r--r--src/analysis/db/client.c164
1 files changed, 140 insertions, 24 deletions
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;