diff options
Diffstat (limited to 'src/analysis/db/client.c')
| -rw-r--r-- | src/analysis/db/client.c | 99 | 
1 files changed, 67 insertions, 32 deletions
| diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c index 2a7aa03..309d9ed 100644 --- a/src/analysis/db/client.c +++ b/src/analysis/db/client.c @@ -353,11 +353,14 @@ bool g_db_client_start_remote(GDbClient *client, const char *host, unsigned shor  static bool g_db_client_start_common(GDbClient *client, const char *desc)  { +    packed_buffer out_pbuf;                 /* Tampon d'émission           */ +    bool status;                            /* Bilan d'une opération       */      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         */ +    packed_buffer in_pbuf;                  /* Tampon de réception         */      uint32_t data;                          /* Mot de données lues         */      DBError error;                          /* Validation de la connexion  */ @@ -372,19 +375,21 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)       * Tout ceci est à synchroniser avec la fonction g_db_server_listener().       */ -    if (!safe_send(client->fd, (uint32_t []) { htobe32(DBC_HELO) }, sizeof(uint32_t), MSG_MORE)) -        goto gdcs_error; +    init_packed_buffer(&out_pbuf); -    if (!safe_send(client->fd, (uint32_t []) { htobe32(CDB_PROTOCOL_VERSION) }, sizeof(uint32_t), MSG_MORE)) -        goto gdcs_error; +    status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_HELO }, sizeof(uint32_t), true); +    if (!status) goto gdcs_error; -    if (!send_rle_string(&client->hash, client->fd, MSG_MORE)) -        goto gdcs_error; +    status = extend_packed_buffer(&out_pbuf, (uint32_t []) { CDB_PROTOCOL_VERSION }, sizeof(uint32_t), true); +    if (!status) goto gdcs_error; + +    status = pack_rle_string(&client->hash, &out_pbuf); +    if (!status) goto gdcs_error;      init_rle_string(&user, client->author); -    if (!send_rle_string(&user, client->fd, MSG_MORE)) -        goto gdcs_error; +    status = pack_rle_string(&user, &out_pbuf); +    if (!status) goto gdcs_error;      checksum = g_checksum_new(G_CHECKSUM_MD5);      g_checksum_update(checksum, (guchar *)get_rle_string(&user), get_rle_length(&user)); @@ -399,8 +404,11 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)      RSA_free(key); -    if (!safe_send(client->fd, sig, RSA_USED_SIZE, 0)) -        goto gdcs_error; +    status = extend_packed_buffer(&out_pbuf, sig, RSA_USED_SIZE, false); +    if (!status) goto gdcs_error; + +    status = send_packed_buffer(&out_pbuf, client->fd); +    if (!status) goto gdcs_error;      /**       * Le serveur doit répondre pour un message type : @@ -409,19 +417,22 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)       *      ou 'DBE_WRONG_VERSION' ... 'DBE_LOADING_ERROR').       */ -    if (!safe_recv(client->fd, &data, sizeof(uint32_t), 0)) -        goto gdcs_error; +    status = recv_packed_buffer(&in_pbuf, client->fd); +    if (!status) goto gdsc_error; -    if (be32toh(data) != DBC_WELCOME) +    status = extract_packed_buffer(&in_pbuf, &data, sizeof(uint32_t), true); +    if (!status) goto gdsc_error; + +    if (data != DBC_WELCOME)      {          log_variadic_message(LMT_ERROR, _("The server '%s' did not welcome us!"), desc); -        goto gdcs_error; +        goto gdsc_error;      } -    if (!safe_recv(client->fd, &data, sizeof(uint32_t), 0)) -        goto gdcs_error; +    status = extract_packed_buffer(&in_pbuf, &data, sizeof(uint32_t), true); +    if (!status) goto gdsc_error; -    error = be32toh(data); +    error = data;      switch (error)      { @@ -432,25 +443,25 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)          case DBE_WRONG_VERSION:              log_variadic_message(LMT_ERROR, _("The server '%s' does not use our protocol version (0x%08x)..."),                                   desc, CDB_PROTOCOL_VERSION); -            goto gdcs_error; +            goto gdsc_error;              break;          case DBE_XML_VERSION_ERROR:              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; +            goto gdsc_error;              break;          case DBE_DB_LOADING_ERROR:              log_variadic_message(LMT_ERROR, _("The server '%s' got into troubles while loading the database...."),                                   desc); -            goto gdcs_error; +            goto gdsc_error;              break;          default:              log_variadic_message(LMT_ERROR, _("The server '%s' has run into an error (%u)..."),                                   desc, error); -            goto gdcs_error; +            goto gdsc_error;              break;      } @@ -460,13 +471,22 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)      {          log_variadic_message(LMT_ERROR, _("Failed to start a listening thread for the server '%s'!"),                               desc); -        goto gdcs_error; +        goto gdsc_error;      } +    exit_packed_buffer(&out_pbuf); +    exit_packed_buffer(&in_pbuf); +      return true; + gdsc_error: + +    exit_packed_buffer(&in_pbuf); +   gdcs_error: +    exit_packed_buffer(&out_pbuf); +      unset_rle_string(&user);      close(client->fd); @@ -493,7 +513,8 @@ static void *g_db_client_update(GDbClient *client)  {      struct pollfd fds;                      /* Surveillance des flux       */      int ret;                                /* Bilan d'un appel            */ -    uint32_t val32;                         /* Valeur sur 32 bits          */ +    packed_buffer in_pbuf;                  /* Tampon de réception         */ +    uint32_t tmp32;                         /* Valeur sur 32 bits          */      bool status;                            /* Bilan d'une opération       */      uint32_t command;                       /* Commande de la requête      */      DBError error;                          /* Bilan d'une commande passée */ @@ -515,19 +536,20 @@ static void *g_db_client_update(GDbClient *client)          if (fds.revents & (POLLIN | POLLPRI))          { -            status = safe_recv(fds.fd, &val32, sizeof(uint32_t), 0); +            status = recv_packed_buffer(&in_pbuf, fds.fd);              if (!status) goto gdcu_bad_exchange; -            command = be32toh(val32); +            status = extract_packed_buffer(&in_pbuf, &command, sizeof(uint32_t), true); +            if (!status) goto gdcu_bad_exchange;              switch (command)              {                  case DBC_SAVE: -                    status = safe_recv(fds.fd, &val32, sizeof(uint32_t), 0); +                    status = extract_packed_buffer(&in_pbuf, &tmp32, sizeof(uint32_t), true);                      if (!status) goto gdcu_bad_exchange; -                    error = be32toh(val32); +                    error = tmp32;                      if (error == DBE_NONE)                          log_variadic_message(LMT_INFO, _("Archive saved for binary '%s'"), @@ -540,13 +562,13 @@ static void *g_db_client_update(GDbClient *client)                  case DBC_COLLECTION: -                    status = safe_recv(fds.fd, &val32, sizeof(uint32_t), 0); +                    status = extract_packed_buffer(&in_pbuf, &tmp32, sizeof(uint32_t), true);                      if (!status) goto gdcu_bad_exchange; -                    collec = find_collection_in_list(client->collections, be32toh(val32)); +                    collec = find_collection_in_list(client->collections, tmp32);                      if (collec == NULL) goto gdcu_bad_exchange; -                    status = g_db_collection_recv(collec, fds.fd, NULL); +                    status = g_db_collection_unpack(collec, &in_pbuf, NULL);                      if (!status) goto gdcu_bad_exchange; @@ -558,12 +580,16 @@ static void *g_db_client_update(GDbClient *client)              } +            exit_packed_buffer(&in_pbuf); +              continue;   gdcu_bad_exchange:                  printf("Bad reception...\n"); +                exit_packed_buffer(&in_pbuf); +                  /* TODO : close conn */                  ; @@ -689,15 +715,24 @@ bool g_db_client_save(GDbClient *client)  bool g_db_client_set_last_active(GDbClient *client, timestamp_t timestamp)  {      bool result;                            /* Bilan partiel à remonter    */ +    packed_buffer out_pbuf;                 /* Tampon d'émission           */ + +    init_packed_buffer(&out_pbuf);      g_db_client_get_fd(client); -    result = safe_send(client->fd, (uint32_t []) { htobe32(DBC_SET_LAST_ACTIVE) }, sizeof(uint32_t), 0); +    result = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SET_LAST_ACTIVE }, sizeof(uint32_t), true); -    result &= send_timestamp(×tamp, client->fd, MSG_MORE); +    if (result) +        result = pack_timestamp(×tamp, &out_pbuf);      g_db_client_put_fd(client); +    if (result) +        result = send_packed_buffer(&out_pbuf, client->fd); + +    exit_packed_buffer(&out_pbuf); +      return result;  } | 
