summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2019-09-08 07:47:41 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2019-09-08 07:47:41 (GMT)
commitfbb80d00d8ac456451963d52af24fcccbbc1d751 (patch)
tree232d2f63378bf30db17c33c399cedc28fc13d4f9 /src
parent1a85f36e0505d75a51ab7b7f2c5078da7ef6bd98 (diff)
Updated the database protocol for bookmarks.
Diffstat (limited to 'src')
-rw-r--r--src/analysis/binary.c20
-rw-r--r--src/analysis/db/cdb.c10
-rw-r--r--src/analysis/db/client.c44
-rw-r--r--src/analysis/db/client.h5
-rw-r--r--src/analysis/db/items/bookmark.c59
-rw-r--r--src/analysis/db/items/bookmark.h30
-rw-r--r--src/analysis/db/protocol.h47
-rw-r--r--src/common/packed.c29
-rw-r--r--src/common/packed.h3
9 files changed, 169 insertions, 78 deletions
diff --git a/src/analysis/binary.c b/src/analysis/binary.c
index cf680c3..639d6cc 100644
--- a/src/analysis/binary.c
+++ b/src/analysis/binary.c
@@ -1068,7 +1068,7 @@ bool _g_loaded_binary_add_to_collection(GLoadedBinary *binary, GDbItem *item, bo
DBStorage storage; /* Forme d'enregistrement */
GHubClient *client; /* Liaison à utiliser */
packed_buffer out_pbuf; /* Tampon d'émission */
- int fd; /* Identifiant du canal de com.*/
+ SSL *tls_fd; /* Canal de communication SSL */
feature = g_db_item_get_feature(item);
@@ -1099,14 +1099,14 @@ bool _g_loaded_binary_add_to_collection(GLoadedBinary *binary, GDbItem *item, bo
{
init_packed_buffer(&out_pbuf);
- fd = g_hub_client_get_fd(client);
+ tls_fd = g_hub_client_get_ssl_fd(client);
result = g_db_collection_pack(collec, &out_pbuf, DBA_ADD_ITEM, item);
- g_hub_client_put_fd(client);
-
if (result)
- result = send_packed_buffer(&out_pbuf, fd);
+ result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
+
+ g_hub_client_put_ssl_fd(client, tls_fd);
exit_packed_buffer(&out_pbuf);
@@ -1144,7 +1144,7 @@ bool _g_loaded_binary_remove_from_collection(GLoadedBinary *binary, DBFeatures f
DBStorage storage; /* Forme d'enregistrement */
GHubClient *client; /* Liaison à utiliser */
packed_buffer out_pbuf; /* Tampon d'émission */
- int fd; /* Identifiant du canal de com.*/
+ SSL *tls_fd; /* Canal de communication SSL */
collec = g_loaded_binary_find_collection(binary, feature);
if (collec == NULL) return false;
@@ -1168,14 +1168,14 @@ bool _g_loaded_binary_remove_from_collection(GLoadedBinary *binary, DBFeatures f
init_packed_buffer(&out_pbuf);
- fd = g_hub_client_get_fd(client);
+ tls_fd = g_hub_client_get_ssl_fd(client);
result = g_db_collection_pack(collec, &out_pbuf, DBA_REM_ITEM, item);
- g_hub_client_put_fd(client);
-
if (result)
- result = send_packed_buffer(&out_pbuf, fd);
+ result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
+
+ g_hub_client_put_ssl_fd(client, tls_fd);
exit_packed_buffer(&out_pbuf);
diff --git a/src/analysis/db/cdb.c b/src/analysis/db/cdb.c
index 74e8501..597242f 100644
--- a/src/analysis/db/cdb.c
+++ b/src/analysis/db/cdb.c
@@ -939,9 +939,19 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
sizeof(uint32_t), true);
if (!status) goto gcap_bad_reply;
+ status = extend_packed_buffer(&out_pbuf, (uint8_t []) { 0x1 }, sizeof(uint8_t), true);
+ if (!status) goto gcap_bad_reply;
+
status = pack_all_collection_updates(archive->collections, &out_pbuf);
if (!status) goto gcap_bad_reply;
+ status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SET_ALL_ITEMS },
+ sizeof(uint32_t), true);
+ if (!status) goto gcap_bad_reply;
+
+ status = extend_packed_buffer(&out_pbuf, (uint8_t []) { 0x0 }, sizeof(uint8_t), true);
+ if (!status) goto gcap_bad_reply;
+
status = ssl_send_packed_buffer(&out_pbuf, archive->clients[i].ssl_fd);
if (!status) goto gcap_bad_reply;
diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c
index 9252ccc..2bfa978 100644
--- a/src/analysis/db/client.c
+++ b/src/analysis/db/client.c
@@ -31,7 +31,6 @@
#include <string.h>
#include <unistd.h>
#include <openssl/err.h>
-#include <openssl/ssl.h>
#include <i18n.h>
@@ -682,6 +681,7 @@ static void *g_hub_client_update(GHubClient *client)
uint32_t command; /* Commande de la requête */
DBError error; /* Bilan d'une commande passée */
GDbCollection *collec; /* Collection visée au final */
+ uint8_t tmp8; /* Valeur sur 8 bits */
char *msg; /* Message d'erreur à imprimer */
/**
@@ -740,6 +740,8 @@ static void *g_hub_client_update(GHubClient *client)
status = ssl_recv_packed_buffer(&in_pbuf, client->tls_fd);
if (!status) goto gdcu_bad_exchange;
+ next_command:
+
status = extract_packed_buffer(&in_pbuf, &command, sizeof(uint32_t), true);
if (!status) goto gdcu_bad_exchange;
@@ -785,11 +787,18 @@ static void *g_hub_client_update(GHubClient *client)
break;
case DBC_SET_ALL_ITEMS:
- client->can_get_updates = true;
+
+ status = extract_packed_buffer(&in_pbuf, &tmp8, sizeof(uint8_t), true);
+ if (!status) goto gdcu_bad_exchange;
+
+ client->can_get_updates = (tmp8 == 0x1);
break;
}
+ if (has_more_data_in_packed_buffer(&in_pbuf))
+ goto next_command;
+
continue;
gdcu_bad_exchange:
@@ -882,11 +891,25 @@ void g_hub_client_stop(GHubClient *client)
* *
******************************************************************************/
-int g_hub_client_get_fd(GHubClient *client)
+SSL *g_hub_client_get_ssl_fd(GHubClient *client)
{
+ SSL *result; /* Canal à retourner */
+#ifndef NDEBUG
+ int ret; /* Validation de transmission */
+#endif
+
g_mutex_lock(&client->sending_lock);
- return client->fd;
+ result = client->tls_fd;
+
+#ifndef NDEBUG
+ ret = SSL_up_ref(result);
+ assert(ret == 1);
+#else
+ SSL_up_ref(result);
+#endif
+
+ return result;
}
@@ -894,6 +917,7 @@ int g_hub_client_get_fd(GHubClient *client)
/******************************************************************************
* *
* Paramètres : client = client pour les accès distants à manipuler. *
+* tls_fd = canal de communication SSL. *
* *
* Description : Marque le canal de communication comme disponible. *
* *
@@ -903,10 +927,12 @@ int g_hub_client_get_fd(GHubClient *client)
* *
******************************************************************************/
-void g_hub_client_put_fd(GHubClient *client)
+void g_hub_client_put_ssl_fd(GHubClient *client, SSL *tls_fd)
{
g_mutex_unlock(&client->sending_lock);
+ SSL_free(tls_fd);
+
}
@@ -927,13 +953,13 @@ bool g_hub_client_save(GHubClient *client)
bool result; /* Bilan partiel à remonter */
int sent; /* Quantité de données traitées*/
- g_hub_client_get_fd(client);
+ g_hub_client_get_ssl_fd(client);
sent = SSL_write(client->tls_fd, (uint32_t []) { htobe32(DBC_SAVE) }, sizeof(uint32_t));
result = (sent == sizeof(uint32_t));
- g_hub_client_put_fd(client);
+ g_hub_client_put_ssl_fd(client, client->tls_fd);
return result;
@@ -960,14 +986,14 @@ bool g_hub_client_set_last_active(GHubClient *client, timestamp_t timestamp)
init_packed_buffer(&out_pbuf);
- g_hub_client_get_fd(client);
+ g_hub_client_get_ssl_fd(client);
result = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SET_LAST_ACTIVE }, sizeof(uint32_t), true);
if (result)
result = pack_timestamp(&timestamp, &out_pbuf);
- g_hub_client_put_fd(client);
+ g_hub_client_put_ssl_fd(client, client->tls_fd);
if (result)
result = ssl_send_packed_buffer(&out_pbuf, client->tls_fd);
diff --git a/src/analysis/db/client.h b/src/analysis/db/client.h
index 66ca6ab..2915f77 100644
--- a/src/analysis/db/client.h
+++ b/src/analysis/db/client.h
@@ -27,6 +27,7 @@
#include <glib-object.h>
#include <stdbool.h>
+#include <openssl/ssl.h>
#include "collection.h"
@@ -64,10 +65,10 @@ bool g_hub_client_start_remote(GHubClient *, const char *, const char *, bool);
void g_hub_client_stop(GHubClient *);
/* Identifie le canal de communication pour envois au serveur. */
-int g_hub_client_get_fd(GHubClient *);
+SSL *g_hub_client_get_ssl_fd(GHubClient *);
/* Marque le canal de communication comme disponible. */
-void g_hub_client_put_fd(GHubClient *);
+void g_hub_client_put_ssl_fd(GHubClient *, SSL *);
/* Effectue une demande de sauvegarde de l'état courant. */
bool g_hub_client_save(GHubClient *);
diff --git a/src/analysis/db/items/bookmark.c b/src/analysis/db/items/bookmark.c
index 1dc299b..266b04c 100644
--- a/src/analysis/db/items/bookmark.c
+++ b/src/analysis/db/items/bookmark.c
@@ -266,12 +266,47 @@ static void g_db_bookmark_finalize(GDbBookmark *bookmark)
GDbBookmark *g_db_bookmark_new(const vmpa2t *addr, const char *comment)
{
GDbBookmark *result; /* Instance à retourner */
+ bool status; /* Bilan de l'initialisation */
result = g_object_new(G_TYPE_DB_BOOKMARK, NULL);
- copy_vmpa(&result->addr, addr);
+ status = g_db_bookmark_fill(result, addr, comment);
+ if (!status) goto error;
- g_db_bookmark_set_comment(result, comment);
+ return result;
+
+ error:
+
+ g_object_unref(G_OBJECT(result));
+
+ return NULL;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : bookmark = signet à initialiser. *
+* addr = adresse inamovible localisant une position donnée.*
+* comment = commentaire construit ou NULL. *
+* *
+* Description : Initialise la définition d'un signet dans une zone de texte. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_db_bookmark_fill(GDbBookmark *bookmark, const vmpa2t *addr, const char *comment)
+{
+ bool result; /* Bilan à retourner */
+
+ result = true;
+
+ copy_vmpa(&bookmark->addr, addr);
+
+ dup_into_rle_string(&bookmark->comment, comment);
return result;
@@ -601,26 +636,6 @@ const char *g_db_bookmark_get_comment(const GDbBookmark *bookmark)
}
-/******************************************************************************
-* *
-* Paramètres : bookmark = informations à consulter. *
-* comment = commentaire construit ou NULL. *
-* *
-* Description : Définit le commentaire associé à un signet. *
-* *
-* Retour : - *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-void g_db_bookmark_set_comment(GDbBookmark *bookmark, const char *comment)
-{
- dup_into_rle_string(&bookmark->comment, comment);
-
-}
-
-
/* ---------------------------------------------------------------------------------- */
/* DEFINITION DE LA COLLECTION ASSOCIEE */
diff --git a/src/analysis/db/items/bookmark.h b/src/analysis/db/items/bookmark.h
index a8526f7..47a7955 100644
--- a/src/analysis/db/items/bookmark.h
+++ b/src/analysis/db/items/bookmark.h
@@ -36,12 +36,12 @@
/* --------------------- ELABORATION D'UN ELEMENT DE COLLECTION --------------------- */
-#define G_TYPE_DB_BOOKMARK g_db_bookmark_get_type()
-#define G_DB_BOOKMARK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_db_bookmark_get_type(), GDbBookmark))
-#define G_IS_DB_BOOKMARK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_db_bookmark_get_type()))
-#define G_DB_BOOKMARK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_DB_BOOKMARK, GDbBookmarkClass))
-#define G_IS_DB_BOOKMARK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_DB_BOOKMARK))
-#define G_DB_BOOKMARK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_DB_BOOKMARK, GDbBookmarkClass))
+#define G_TYPE_DB_BOOKMARK g_db_bookmark_get_type()
+#define G_DB_BOOKMARK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_DB_BOOKMARK, GDbBookmark))
+#define G_IS_DB_BOOKMARK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_DB_BOOKMARK))
+#define G_DB_BOOKMARK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_DB_BOOKMARK, GDbBookmarkClass))
+#define G_IS_DB_BOOKMARK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_DB_BOOKMARK))
+#define G_DB_BOOKMARK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_DB_BOOKMARK, GDbBookmarkClass))
/* Signet à l'intérieur d'une zone de texte (instance) */
@@ -57,26 +57,26 @@ GType g_db_bookmark_get_type(void);
/* Crée une définition d'un signet dans une zone de texte. */
GDbBookmark *g_db_bookmark_new(const vmpa2t *, const char *);
+/* Initialise la définition d'un signet dans une zone de texte. */
+bool g_db_bookmark_fill(GDbBookmark *, const vmpa2t *, const char *);
+
/* Fournit l'adresse associée à un signet. */
const vmpa2t *g_db_bookmark_get_address(GDbBookmark *);
/* Fournit le commentaire associé à un signet. */
const char *g_db_bookmark_get_comment(const GDbBookmark *);
-/* Définit le commentaire associé à un signet. */
-void g_db_bookmark_set_comment(GDbBookmark *, const char *);
-
/* ---------------------- DEFINITION DE LA COLLECTION ASSOCIEE ---------------------- */
-#define G_TYPE_BM_COLLECTION g_bookmark_collection_get_type()
-#define G_BM_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_bookmark_collection_get_type(), GBookmarkCollection))
-#define G_IS_BM_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_bookmark_collection_get_type()))
-#define G_BM_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_BM_COLLECTION, GBookmarkCollectionClass))
-#define G_IS_BM_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_BM_COLLECTION))
-#define G_BM_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_BM_COLLECTION, GBookmarkCollectionClass))
+#define G_TYPE_BM_COLLECTION g_bookmark_collection_get_type()
+#define G_BM_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_BM_COLLECTION, GBookmarkCollection))
+#define G_IS_BM_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_BM_COLLECTION))
+#define G_BM_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_BM_COLLECTION, GBookmarkCollectionClass))
+#define G_IS_BM_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_BM_COLLECTION))
+#define G_BM_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_BM_COLLECTION, GBookmarkCollectionClass))
/* Collection dédiée aux signets (instance) */
diff --git a/src/analysis/db/protocol.h b/src/analysis/db/protocol.h
index 1c03d62..5c3eed6 100644
--- a/src/analysis/db/protocol.h
+++ b/src/analysis/db/protocol.h
@@ -114,7 +114,28 @@ typedef enum _DBCommand
{
DBC_HELO, /* Connexion initiale C -> S */
DBC_WELCOME, /* Réponse initiale S -> C */
+
+ /**
+ * Gestion de la commande 'DBC_SAVE'.
+ *
+ * Le client connecté envoie un paquet de la forme suivante :
+ *
+ * [ Ordre de sauvegarde : DBC_SAVE ]
+ *
+ * Le serveur s'exécute et renvoie un bilan :
+ *
+ * [ Ordre de sauvegarde : DBC_SAVE ]
+ * [ Statut d'exécution ; cf. DBError ]
+ *
+ * Les traitements se réalisent dans :
+ * - g_db_client_save() pour la partie client en émission.
+ * - g_cdb_archive_process() pour la partie serveur.
+ * - g_db_client_update() pour la partie client en réception.
+ *
+ */
+
DBC_SAVE, /* Enregistrement de l'archive */
+
DBC_COLLECTION, /* Implication d'une collection*/
/**
@@ -131,6 +152,7 @@ typedef enum _DBCommand
* De son côté, le serveur répond par une requête :
*
* [ Notification de maj : DBC_SET_ALL_ITEMS ]
+ * [ marqueur de démarrage : octet 0x1 ]
*
* Dans la foulée, il enverra ensuite les éléments avec des paquets classiques :
*
@@ -138,6 +160,11 @@ typedef enum _DBCommand
* [ Action : DBA_ADD_ITEM ]
* ...
*
+ * La séquence se termine par une requête finale :
+ *
+ * [ Notification de maj : DBC_SET_ALL_ITEMS ]
+ * [ marqueur de fin : octet 0x0 ]
+ *
* Les traitements se réalisent dans :
* - g_db_client_update() pour la partie client.
* - g_cdb_archive_process() pour la partie serveur.
@@ -202,26 +229,6 @@ typedef enum _DBError
-/**
- * Gestion de la commande 'DBC_SAVE'.
- *
- * Le client connecté envoie un paquet de la forme suivante :
- *
- * [ Ordre de sauvegarde : DBC_SAVE ]
- *
- * Le serveur s'exécute et renvoie un bilan :
- *
- * [ Ordre de sauvegarde : DBC_SAVE ]
- * [ Statut d'exécution ; cf. DBError ]
- *
- * Les traitements se réalisent dans :
- * - g_db_client_save() pour la partie client en émission.
- * - g_cdb_archive_process() pour la partie serveur.
- * - g_db_client_update() pour la partie client en réception.
- *
- */
-
-
diff --git a/src/common/packed.c b/src/common/packed.c
index e06e4bb..163ed3b 100644
--- a/src/common/packed.c
+++ b/src/common/packed.c
@@ -30,6 +30,9 @@
#include <string.h>
+#include "../core/logs.h"
+
+
/* Taille d'allocation en cas de besoin */
#define PACKET_BLOCK_SIZE 1000
@@ -124,6 +127,29 @@ size_t get_packed_buffer_payload_length(const packed_buffer *pbuf)
/******************************************************************************
* *
+* Paramètres : pbuf = paquet de données à consulter. *
+* *
+* Description : Détermine si des données sont disponibles en lecture. *
+* *
+* Retour : true si des données peuvent être dépilées, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool has_more_data_in_packed_buffer(const packed_buffer *pbuf)
+{
+ bool result; /* Bilan à retourner */
+
+ result = (pbuf->pos < pbuf->used);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
* Paramètres : pbuf = paquet de données à compléter. *
* buf = nouvelles données à ajouter. *
* len = quantité de ces données. *
@@ -430,6 +456,7 @@ bool ssl_recv_packed_buffer(packed_buffer *pbuf, SSL *fd)
int got; /* Quantité de données traitées*/
got = SSL_read(fd, &used, sizeof(uint32_t));
+ if (got <= 0) LOG_ERROR_OPENSSL;
result = (got == sizeof(uint32_t));
@@ -446,6 +473,7 @@ bool ssl_recv_packed_buffer(packed_buffer *pbuf, SSL *fd)
pbuf->used = used;
got = SSL_read(fd, pbuf->data + pbuf->pos, used);
+ if (got <= 0) LOG_ERROR_OPENSSL;
result = (got == used);
@@ -480,6 +508,7 @@ bool ssl_send_packed_buffer(packed_buffer *pbuf, SSL *fd)
quantity = sizeof(uint32_t) + pbuf->used;
sent = SSL_write(fd, pbuf->data, quantity);
+ if (sent <= 0) LOG_ERROR_OPENSSL;
result = (quantity == sent);
diff --git a/src/common/packed.h b/src/common/packed.h
index 5c724e0..4403ad0 100644
--- a/src/common/packed.h
+++ b/src/common/packed.h
@@ -59,6 +59,9 @@ void exit_packed_buffer(packed_buffer *);
/* Indique le nombre d'octets de la charge utile d'un paquet. */
size_t get_packed_buffer_payload_length(const packed_buffer *);
+/* Détermine si des données sont disponibles en lecture. */
+bool has_more_data_in_packed_buffer(const packed_buffer *);
+
/* Ajoute des données à un paquet en amont à un envoi. */
bool extend_packed_buffer(packed_buffer *, const void *, size_t, bool);