]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Refresh fuzzy hashes when matched
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Jul 2018 11:44:56 +0000 (12:44 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Jul 2018 11:44:56 +0000 (12:44 +0100)
src/fuzzy_storage.c
src/libserver/fuzzy_wire.h

index c97756aba9f6370b4e747d9738c466a6d3ad54d2..c6947f1cf47241259535923796419a3dc06a26ad 100644 (file)
@@ -17,6 +17,7 @@
  * Rspamd fuzzy storage server
  */
 
+#include <src/libserver/fuzzy_wire.h>
 #include "config.h"
 #include "util.h"
 #include "rspamd.h"
@@ -772,6 +773,7 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud)
        struct fuzzy_session *session = ud;
        gboolean encrypted = FALSE, is_shingle = FALSE;
        struct rspamd_fuzzy_cmd *cmd = NULL;
+       const struct rspamd_shingle *shingle = NULL;
 
        switch (session->cmd_type) {
        case CMD_NORMAL:
@@ -779,6 +781,7 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud)
                break;
        case CMD_SHINGLE:
                cmd = &session->cmd.shingle.basic;
+               shingle = &session->cmd.shingle.sgl;
                is_shingle = TRUE;
                break;
        case CMD_ENCRYPTED_NORMAL:
@@ -787,12 +790,59 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud)
                break;
        case CMD_ENCRYPTED_SHINGLE:
                cmd = &session->cmd.enc_shingle.cmd.basic;
+               shingle = &session->cmd.enc_shingle.cmd.sgl;
                encrypted = TRUE;
                is_shingle = TRUE;
                break;
        }
 
        rspamd_fuzzy_make_reply (cmd, result, session, encrypted, is_shingle);
+
+       /* Refresh hash if found with strong confidence */
+       if (result->v1.prob > 0.9) {
+               struct fuzzy_peer_cmd up_cmd;
+               struct fuzzy_peer_request *up_req;
+
+               if (session->worker->index == 0 || session->ctx->peer_fd == -1) {
+                       /* Just add to the queue */
+                       memset (&up_cmd, 0, sizeof (up_cmd));
+                       up_cmd.is_shingle = is_shingle;
+                       memcpy (up_cmd.cmd.normal.digest, result->digest,
+                                       sizeof (up_cmd.cmd.normal.digest));
+                       up_cmd.cmd.normal.flag = result->v1.flag;
+                       up_cmd.cmd.normal.cmd = FUZZY_REFRESH;
+                       up_cmd.cmd.normal.shingles_count = cmd->shingles_count;
+
+                       if (is_shingle && shingle) {
+                               memcpy (&up_cmd.cmd.shingle.sgl, shingle,
+                                               sizeof (up_cmd.cmd.shingle.sgl));
+                       }
+
+                       g_array_append_val (session->ctx->updates_pending, up_cmd);
+               }
+               else {
+                       /* We need to send request to the peer */
+                       up_req = g_malloc0 (sizeof (*up_req));
+                       up_req->cmd.is_shingle = is_shingle;
+
+                       memcpy (up_req->cmd.cmd.normal.digest, result->digest,
+                                       sizeof (up_req->cmd.cmd.normal.digest));
+                       up_req->cmd.cmd.normal.flag = result->v1.flag;
+                       up_req->cmd.cmd.normal.cmd = FUZZY_REFRESH;
+                       up_req->cmd.cmd.normal.shingles_count = cmd->shingles_count;
+
+                       if (is_shingle && shingle) {
+                               memcpy (&up_req->cmd.cmd.shingle.sgl, shingle,
+                                               sizeof (up_req->cmd.cmd.shingle.sgl));
+                       }
+
+                       event_set (&up_req->io_ev, session->ctx->peer_fd, EV_WRITE,
+                                       fuzzy_peer_send_io, up_req);
+                       event_base_set (session->ctx->ev_base, &up_req->io_ev);
+                       event_add (&up_req->io_ev, NULL);
+               }
+       }
+
        REF_RELEASE (session);
 }
 
index ef9a7e4e3cb2303a6be50d37198b8ca6fa4146a0..73d4c480c46541918983a7cf34318ee3db45d75d 100644 (file)
@@ -14,7 +14,9 @@
 #define FUZZY_WRITE 1
 #define FUZZY_DEL 2
 #define FUZZY_STAT 3
-
+#define FUZZY_CLIENT_MAX 3
+/* Internal commands */
+#define FUZZY_REFRESH 100
 
 /**
  * The epoch of the fuzzy client