From 511877239e385e46ad81a22d6c87c48517693a8d Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 21 Apr 2015 18:19:40 +0100 Subject: [PATCH] Implement the concept of fuzzy protocol epoches. --- src/fuzzy_storage.c | 33 ++++++++++++++++++++++++--------- src/main.h | 12 ++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index 22b61256a..4f86fbd94 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -160,7 +160,8 @@ rspamd_fuzzy_write_reply (struct fuzzy_session *session, } static void -rspamd_fuzzy_process_command (struct fuzzy_session *session) +rspamd_fuzzy_process_command (struct fuzzy_session *session, + enum rspamd_fuzzy_epoch epoch) { struct rspamd_fuzzy_reply rep = {0, 0, 0, 0.0}; gboolean res = FALSE; @@ -168,6 +169,12 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) if (session->cmd->cmd == FUZZY_CHECK) { rep = rspamd_fuzzy_backend_check (session->ctx->backend, session->cmd, session->ctx->expire); + /* XXX: actually, these updates are not atomic, but we don't care */ + server_stat->fuzzy_hashes_checked[epoch] ++; + + if (rep.prob > 0.5) { + server_stat->fuzzy_hashes_found[epoch] ++; + } } else { rep.flag = session->cmd->flag; @@ -201,17 +208,21 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) } -static gboolean +static enum rspamd_fuzzy_epoch rspamd_fuzzy_command_valid (struct rspamd_fuzzy_cmd *cmd, gint r) { + enum rspamd_fuzzy_epoch ret = RSPAMD_FUZZY_EPOCH_MAX; + if (cmd->version == RSPAMD_FUZZY_VERSION) { if (cmd->shingles_count > 0) { if (r == sizeof (struct rspamd_fuzzy_shingle_cmd)) { - return TRUE; + ret = RSPAMD_FUZZY_EPOCH9; } } else { - return (r == sizeof (*cmd)); + if (r == sizeof (*cmd)) { + ret = RSPAMD_FUZZY_EPOCH9; + } } } else if (cmd->version == 2) { @@ -221,15 +232,15 @@ rspamd_fuzzy_command_valid (struct rspamd_fuzzy_cmd *cmd, gint r) */ if (cmd->shingles_count > 0) { if (r == sizeof (struct rspamd_fuzzy_shingle_cmd)) { - return TRUE; + ret = RSPAMD_FUZZY_EPOCH8; } } else { - return (r == sizeof (*cmd)); + ret = RSPAMD_FUZZY_EPOCH8; } } - return FALSE; + return ret; } /* * Accept new connection and construct task @@ -243,6 +254,7 @@ accept_fuzzy_socket (gint fd, short what, void *arg) guint8 buf[2048]; struct rspamd_fuzzy_cmd *cmd = NULL, lcmd; struct legacy_fuzzy_cmd *l; + enum rspamd_fuzzy_epoch epoch = RSPAMD_FUZZY_EPOCH_MAX; session.worker = worker; session.fd = fd; @@ -273,14 +285,17 @@ accept_fuzzy_socket (gint fd, short what, void *arg) lcmd.value = l->value; lcmd.tag = 0; cmd = &lcmd; + epoch = RSPAMD_FUZZY_EPOCH6; } else if ((guint)r >= sizeof (struct rspamd_fuzzy_cmd)) { /* Check shingles count sanity */ session.legacy = FALSE; cmd = (struct rspamd_fuzzy_cmd *)buf; - if (!rspamd_fuzzy_command_valid (cmd, r)) { + epoch = rspamd_fuzzy_command_valid (cmd, r); + if (epoch == RSPAMD_FUZZY_EPOCH_MAX) { /* Bad input */ msg_debug ("invalid fuzzy command of size %d received", r); + cmd = NULL; } } else { @@ -289,7 +304,7 @@ accept_fuzzy_socket (gint fd, short what, void *arg) } if (cmd != NULL) { session.cmd = cmd; - rspamd_fuzzy_process_command (&session); + rspamd_fuzzy_process_command (&session, epoch); } rspamd_inet_address_destroy (session.addr); diff --git a/src/main.h b/src/main.h index d33e4ed1a..29096b86f 100644 --- a/src/main.h +++ b/src/main.h @@ -83,6 +83,16 @@ struct mime_part; struct rspamd_dns_resolver; struct rspamd_task; +/** + * The epoch of the fuzzy client + */ +enum rspamd_fuzzy_epoch { + RSPAMD_FUZZY_EPOCH6 = 0, /**< pre 0.6.x */ + RSPAMD_FUZZY_EPOCH8, /**< 0.8 till 0.9 */ + RSPAMD_FUZZY_EPOCH9, /**< 0.9 + */ + RSPAMD_FUZZY_EPOCH_MAX +}; + /** * Server statistics */ @@ -94,6 +104,8 @@ struct rspamd_stat { guint messages_learned; /**< messages learned */ guint fuzzy_hashes; /**< number of fuzzy hashes stored */ guint fuzzy_hashes_expired; /**< number of fuzzy hashes expired */ + guint64 fuzzy_hashes_checked[RSPAMD_FUZZY_EPOCH_MAX]; /**< ammount of check requests for each epoch */ + guint64 fuzzy_hashes_found[RSPAMD_FUZZY_EPOCH_MAX]; /**< amount of hashes found by epoch */ }; /** -- 2.39.5