]> source.dussan.org Git - rspamd.git/commitdiff
Rework rolling history.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 12 Mar 2015 14:21:24 +0000 (14:21 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 12 Mar 2015 14:21:24 +0000 (14:21 +0000)
- Store inet_addr as string, not structure.
- Add history magic for versioning and sanity.

src/libserver/roll_history.c
src/libserver/roll_history.h

index 5edf5bea0adbb3607e95700f2f9f6c54601bc24d..2f5c5ddb37e34ff1181d79e52cadc95f8d3f79fb 100644 (file)
@@ -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));
index d4034202c8d706b37052ec7f3e4a3bf40dbad405..2e8e1e5cdb3af4a84951dfe4ad4eb6ddad14359d 100644 (file)
@@ -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;