From: Vsevolod Stakhov Date: Thu, 12 Mar 2015 14:21:24 +0000 (+0000) Subject: Rework rolling history. X-Git-Tag: 0.9.0~512^2~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c451d5d7d3cb2247c6bec32a7f639e72ddd469c1;p=rspamd.git Rework rolling history. - Store inet_addr as string, not structure. - Add history magic for versioning and sanity. --- diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index 5edf5bea0..2f5c5ddb3 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -27,6 +27,7 @@ #include "main.h" #include "roll_history.h" +static const gchar rspamd_history_magic[] = {'r', 's', 'h', '1'}; /** * Returns new roll history @@ -108,7 +109,9 @@ rspamd_roll_history_update (struct roll_history *history, } /* Add information from task to roll history */ - memcpy (&row->from_addr, &task->from_addr, sizeof (row->from_addr)); + rspamd_strlcpy (row->from_addr, + rspamd_inet_address_to_string (task->from_addr), + sizeof (row->from_addr)); memcpy (&row->tv, &task->tv, sizeof (row->tv)); /* Strings */ @@ -161,6 +164,7 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) { gint fd; struct stat st; + gchar magic[sizeof(rspamd_history_magic)]; if (stat (filename, &st) == -1) { msg_info ("cannot load history from %s: %s", filename, @@ -168,7 +172,7 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) return FALSE; } - if (st.st_size != sizeof (history->rows)) { + if (st.st_size != sizeof (history->rows) + sizeof (rspamd_history_magic)) { msg_info ("cannot load history from %s: size mismatch", filename); return FALSE; } @@ -179,6 +183,19 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) return FALSE; } + if (read (fd, magic, sizeof (magic)) == -1) { + close (fd); + msg_info ("cannot read history from %s: %s", filename, + strerror (errno)); + return FALSE; + } + + if (memcmp (magic, rspamd_history_magic, sizeof (magic)) != 0) { + close (fd); + msg_info ("cannot read history from %s: bad magic", filename); + return FALSE; + } + if (read (fd, history->rows, sizeof (history->rows)) == -1) { close (fd); msg_info ("cannot read history from %s: %s", filename, @@ -207,6 +224,12 @@ rspamd_roll_history_save (struct roll_history *history, const gchar *filename) return FALSE; } + if (write (fd, rspamd_history_magic, sizeof (rspamd_history_magic)) == -1) { + close (fd); + msg_info ("cannot write history to %s: %s", filename, strerror (errno)); + return FALSE; + } + if (write (fd, history->rows, sizeof (history->rows)) == -1) { close (fd); msg_info ("cannot write history to %s: %s", filename, strerror (errno)); diff --git a/src/libserver/roll_history.h b/src/libserver/roll_history.h index d4034202c..2e8e1e5cd 100644 --- a/src/libserver/roll_history.h +++ b/src/libserver/roll_history.h @@ -36,6 +36,7 @@ #define HISTORY_MAX_ID 100 #define HISTORY_MAX_SYMBOLS 200 #define HISTORY_MAX_USER 20 +#define HISTORY_MAX_ADDR 32 #define HISTORY_MAX_ROWS 200 struct rspamd_task; @@ -45,7 +46,7 @@ struct roll_history_row { gchar message_id[HISTORY_MAX_ID]; gchar symbols[HISTORY_MAX_SYMBOLS]; gchar user[HISTORY_MAX_USER]; - rspamd_inet_addr_t from_addr; + gchar from_addr[HISTORY_MAX_ADDR]; gsize len; guint scan_time; gint action;