summaryrefslogtreecommitdiff
path: root/src/analysis/db/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/analysis/db/server.c')
-rw-r--r--src/analysis/db/server.c141
1 files changed, 68 insertions, 73 deletions
diff --git a/src/analysis/db/server.c b/src/analysis/db/server.c
index c51c21e..3ef1ce0 100644
--- a/src/analysis/db/server.c
+++ b/src/analysis/db/server.c
@@ -24,6 +24,7 @@
#include "server.h"
+#include <assert.h>
#include <malloc.h>
#include <netdb.h>
#include <poll.h>
@@ -247,26 +248,14 @@ static void *g_db_server_listener(GDbServer *server)
int fd; /* Canal établi vers un client */
char source[INET6_ADDRSTRLEN]; /* Adresse du client (IPv4/6) */
const char *ip; /* Statut de la conversion */
-
-
DBError error; /* Validation de la connexion */
-
- uint32_t data; /* Mot de données lues */
-
-
+ uint32_t cmd; /* Commande initiale lue */
+ uint32_t version; /* Version du client lue */
rle_string hash; /* Empreinte du binaire visé */
rle_string user; /* Nom d'utilisateur du client */
-
GList *iter; /* Boucle de parcours */
GCdbArchive *archive; /* Destinataire final du client*/
-
-
- //cdb_client *client; /* Mémorisation pour poursuite */
-
-
-
-
fds.fd = server->fd;
fds.events = POLLIN | POLLPRI;
@@ -307,39 +296,68 @@ static void *g_db_server_listener(GDbServer *server)
* Tout ceci est à synchroniser avec la fonction g_db_client_start().
*/
- if (!safe_recv(fd, &data, sizeof(uint32_t), 0))
- goto gdsl_error;
+ 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));
+ error = DBE_BAD_EXCHANGE;
+ goto gdsl_error_sending;
+ }
- if (be32toh(data) != DBC_HELO)
+ if (!safe_recv(fd, &version, sizeof(uint32_t), 0))
{
- log_variadic_message(LMT_ERROR, _("The client from '%s:%hu' did not introduce itself!"),
+ log_variadic_message(LMT_ERROR, _("Error while getting the protocol version from '%s:%hu'..."),
source, ntohs(peer.sin_port));
- goto gdsl_error;
+ error = DBE_BAD_EXCHANGE;
+ goto gdsl_error_sending;
}
- if (!safe_recv(fd, &data, sizeof(uint32_t), 0))
- goto gdsl_error;
+ 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));
+ 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));
+ 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));
+ error = DBE_BAD_EXCHANGE;
+ goto gdsl_error_sending;
+ }
- if (be32toh(data) != CDB_PROTOCOL_VERSION)
+ 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(data), CDB_PROTOCOL_VERSION);
+ source, ntohs(peer.sin_port), be32toh(version), CDB_PROTOCOL_VERSION);
error = DBE_WRONG_VERSION;
goto gdsl_error_sending;
}
- if (!recv_rle_string(&hash, fd, 0) || is_rle_string_empty(&hash))
+ if (is_rle_string_empty(&hash))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the binary hash from '%s:%hu'..."),
+ log_variadic_message(LMT_ERROR, _("The submitted binary hash from '%s:%hu' is empty!"),
source, ntohs(peer.sin_port));
- goto gdsl_error;
+ error = DBE_BAD_EXCHANGE;
+ goto gdsl_error_sending;
}
- if (!recv_rle_string(&user, fd, 0) || is_rle_string_empty(&user))
+ if (is_rle_string_empty(&user))
{
- log_variadic_message(LMT_ERROR, _("Error while getting the user name from '%s:%hu'..."),
+ log_variadic_message(LMT_ERROR, _("No user is associated with the client from '%s:%hu'..."),
source, ntohs(peer.sin_port));
- goto gdsl_error;
+ error = DBE_BAD_EXCHANGE;
+ goto gdsl_error_sending;
}
/**
@@ -347,6 +365,8 @@ static void *g_db_server_listener(GDbServer *server)
* en cas d'échec, et être le plus précis possible dans la courte réponse.
*/
+ assert(error == DBE_NONE);
+
for (iter = g_list_first(server->archives);
iter != NULL;
iter = g_list_next(iter))
@@ -357,23 +377,13 @@ static void *g_db_server_listener(GDbServer *server)
}
if (iter == NULL)
- {
archive = g_cdb_archive_new(server->desc, &hash, &user, &error);
- if (archive != NULL)
- server->archives = g_list_append(server->archives, archive);
-
- }
-
- /*
- if (archive != NULL)
- error = g_cdb_archive_add_client(archive, fd, &user);
- */
-
/**
* Le serveur doit répondre pour un message type :
* - la commande 'DBC_WELCOME'.
- * - un identifiant d'erreur ('DBE_NONE' ou 'DBE_WRONG_VERSION').
+ * - un identifiant d'erreur ('DBE_NONE', 'DBE_BAD_EXCHANGE'
+ * ou 'DBE_WRONG_VERSION' ... 'DBE_LOADING_ERROR').
*/
gdsl_error_sending:
@@ -384,52 +394,37 @@ static void *g_db_server_listener(GDbServer *server)
if (!safe_send(fd, (uint32_t []) { htobe32(error) }, sizeof(uint32_t), 0))
goto gdsl_error;
- //if (error == DBE_NONE) continue;
-
+ /**
+ * L'ajout dans la liste des clients connectés provoque un envoie de mises à jour.
+ * L'éventuelle erreur survenant pendant l'envoi ne peut donc pas être remontée
+ * lors des échanges initiaux, car ces derniers seraient alors précédés des mises à jour...
+ */
if (archive != NULL)
+ {
+ assert(error == DBE_NONE);
+
+ server->archives = g_list_append(server->archives, archive);
error = g_cdb_archive_add_client(archive, fd, &user);
- continue;
+ exit_rle_string(&hash);
+ exit_rle_string(&user);
+ continue;
- gdsl_error:
+ }
+ assert(error != DBE_NONE);
- /* free RLE !!!! */
+ gdsl_error:
+ exit_rle_string(&hash);
+ exit_rle_string(&user);
close(fd);
}
-
-
-
-#if 0
- g_mutex_lock(&server->mutex);
-
- client = (cdb_client *)calloc(1, sizeof(cdb_client));
-
- g_object_ref(G_OBJECT(server));
- client->server = server;
-
- client->fd = fd;
- client->peer = peer;
-
- server->clients = (cdb_client **)realloc(server->clients,
- ++server->count * sizeof(cdb_client *));
- server->clients[server->count - 1] = client;
-
- client->thread = g_thread_new("cdb_process", (GThreadFunc)g_db_server_process,
- &server->clients[server->count - 1]);
-
- g_mutex_unlock(&server->mutex);
-#endif
-
-
-
-
}
return NULL;