]> source.dussan.org Git - rspamd.git/commitdiff
Use keypairs cache.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 2 Feb 2015 22:53:26 +0000 (22:53 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 2 Feb 2015 22:53:26 +0000 (22:53 +0000)
src/client/rspamdclient.c
src/libutil/http.c
src/libutil/http.h
src/libutil/map.c
src/lua/lua_http.c
src/worker.c

index 1df619437c6dc7b2000afd90621404d06a10b135..da873630f5a96914290692757770a6b5621b0f0f 100644 (file)
@@ -24,6 +24,7 @@
 #include "rspamdclient.h"
 #include "util.h"
 #include "http.h"
+#include "keypairs_cache.h"
 
 #ifdef HAVE_FETCH_H
 #include <fetch.h>
@@ -46,6 +47,7 @@ struct rspamd_client_connection {
        struct rspamd_http_connection *http_conn;
        gboolean req_sent;
        struct rspamd_client_request *req;
+       struct rspamd_keypair_cache *keys_cache;
 };
 
 struct rspamd_client_request {
@@ -148,11 +150,13 @@ rspamd_client_init (struct event_base *ev_base, const gchar *name,
        conn->ev_base = ev_base;
        conn->fd = fd;
        conn->req_sent = FALSE;
+       conn->keys_cache = rspamd_keypair_cache_new (32);
        conn->http_conn = rspamd_http_connection_new (rspamd_client_body_handler,
                        rspamd_client_error_handler,
                        rspamd_client_finish_handler,
                        0,
-                       RSPAMD_HTTP_CLIENT);
+                       RSPAMD_HTTP_CLIENT,
+                       conn->keys_cache);
 
        conn->server_name = g_string_new (name);
        if (port != 0) {
index b2a33b97f5b0a561495c119a47d96e8308fe1418..6852e7232740b7a02413ef2ea2c81d8328b7aef9 100644 (file)
@@ -397,7 +397,8 @@ rspamd_http_parse_date (const gchar *header, gsize len)
 }
 
 static void
-rspamd_http_parse_key (GString *data, struct rspamd_http_connection_private *priv)
+rspamd_http_parse_key (GString *data, struct rspamd_http_connection *conn,
+               struct rspamd_http_connection_private *priv)
 {
        guchar *decoded_id, *decoded_key;
        const gchar *eq_pos;
@@ -422,6 +423,11 @@ rspamd_http_parse_key (GString *data, struct rspamd_http_connection_private *pri
                                                        RSPAMD_HTTP_KEY_ID_LEN) == 0) {
                                                priv->msg->peer_key =
                                                        rspamd_http_connection_make_peer_key (eq_pos + 1);
+
+                                               if (conn->cache && priv->msg->peer_key) {
+                                                       rspamd_keypair_cache_process (conn->cache,
+                                                                       priv->msg->peer_key, priv->local_key);
+                                               }
                                        }
                                }
                        }
@@ -433,14 +439,15 @@ rspamd_http_parse_key (GString *data, struct rspamd_http_connection_private *pri
 }
 
 static inline void
-rspamd_http_check_special_header (struct rspamd_http_connection_private *priv)
+rspamd_http_check_special_header (struct rspamd_http_connection *conn,
+               struct rspamd_http_connection_private *priv)
 {
        if (g_ascii_strcasecmp (priv->header->name->str, date_header) == 0) {
                priv->msg->date = rspamd_http_parse_date (priv->header->value->str,
                                priv->header->value->len);
        }
        else if (g_ascii_strcasecmp (priv->header->name->str, key_header) == 0) {
-               rspamd_http_parse_key (priv->header->value, priv);
+               rspamd_http_parse_key (priv->header->value, conn, priv);
        }
 }
 
@@ -495,7 +502,7 @@ rspamd_http_on_header_field (http_parser * parser,
        }
        else if (priv->new_header) {
                DL_APPEND (priv->msg->headers, priv->header);
-               rspamd_http_check_special_header (priv);
+               rspamd_http_check_special_header (conn, priv);
                priv->header = g_slice_alloc (sizeof (struct rspamd_http_header));
                priv->header->name = g_string_sized_new (32);
                priv->header->value = g_string_sized_new (32);
@@ -540,7 +547,7 @@ rspamd_http_on_headers_complete (http_parser * parser)
 
        if (priv->header != NULL) {
                DL_APPEND (priv->msg->headers, priv->header);
-               rspamd_http_check_special_header (priv);
+               rspamd_http_check_special_header (conn, priv);
                priv->header = NULL;
        }
 
@@ -856,7 +863,8 @@ rspamd_http_connection_new (rspamd_http_body_handler_t body_handler,
        rspamd_http_error_handler_t error_handler,
        rspamd_http_finish_handler_t finish_handler,
        unsigned opts,
-       enum rspamd_http_connection_type type)
+       enum rspamd_http_connection_type type,
+       struct rspamd_keypair_cache *cache)
 {
        struct rspamd_http_connection *new;
        struct rspamd_http_connection_private *priv;
@@ -874,6 +882,7 @@ rspamd_http_connection_new (rspamd_http_body_handler_t body_handler,
        new->fd = -1;
        new->ref = 1;
        new->finished = FALSE;
+       new->cache = cache;
 
        /* Init priv */
        priv = g_slice_alloc0 (sizeof (struct rspamd_http_connection_private));
@@ -1012,10 +1021,15 @@ rspamd_http_connection_write_message (struct rspamd_http_connection *conn,
        priv->buf->data = g_string_sized_new (128);
        buf = priv->buf->data;
 
-       if (priv->peer_key) {
+       if (priv->peer_key && priv->local_key) {
                priv->msg->peer_key = priv->peer_key;
                priv->peer_key = NULL;
                priv->encrypted = TRUE;
+
+               if (conn->cache && priv->msg->peer_key) {
+                       rspamd_keypair_cache_process (conn->cache,
+                                       priv->msg->peer_key, priv->local_key);
+               }
        }
 
        if (msg->method < HTTP_SYMBOLS) {
@@ -1575,6 +1589,7 @@ rspamd_http_router_new (rspamd_http_router_error_handler_t eh,
        }
 
        new->default_fs_path = NULL;
+
        if (default_fs_path != NULL) {
                if (stat (default_fs_path, &st) == -1) {
                        msg_err ("cannot stat %s", default_fs_path);
@@ -1589,6 +1604,9 @@ rspamd_http_router_new (rspamd_http_router_error_handler_t eh,
                }
        }
 
+       /* XXX: stupid default value, should be configurable */
+       new->cache = rspamd_keypair_cache_new (256);
+
        return new;
 }
 
@@ -1633,7 +1651,7 @@ rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router,
                        rspamd_http_router_error_handler,
                        rspamd_http_router_finish_handler,
                        0,
-                       RSPAMD_HTTP_SERVER);
+                       RSPAMD_HTTP_SERVER, router->cache);
 
        if (router->key) {
                rspamd_http_connection_set_key (conn->conn, router->key);
@@ -1661,6 +1679,10 @@ rspamd_http_router_free (struct rspamd_http_connection_router *router)
                        REF_RELEASE (kp);
                }
 
+               if (router->cache) {
+                       rspamd_keypair_cache_destroy (router->cache);
+               }
+
                if (router->default_fs_path != NULL) {
                        g_free (router->default_fs_path);
                }
index 59ac5ef2056414580836f0ff3512a6d598122358..1c79fb45c9745f7013120b7147f252454ef4b7cc 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "config.h"
 #include "http_parser.h"
+#include "keypairs_cache.h"
 
 enum rspamd_http_connection_type {
        RSPAMD_HTTP_SERVER,
@@ -109,6 +110,7 @@ struct rspamd_http_connection {
        rspamd_http_body_handler_t body_handler;
        rspamd_http_error_handler_t error_handler;
        rspamd_http_finish_handler_t finish_handler;
+       struct rspamd_keypair_cache *cache;
        gpointer ud;
        unsigned opts;
        enum rspamd_http_connection_type type;
@@ -131,6 +133,7 @@ struct rspamd_http_connection_router {
        struct timeval tv;
        struct timeval *ptv;
        struct event_base *ev_base;
+       struct rspamd_keypair_cache *cache;
        gchar *default_fs_path;
        gpointer key;
        rspamd_http_router_error_handler_t error_handler;
@@ -148,7 +151,8 @@ struct rspamd_http_connection * rspamd_http_connection_new (
        rspamd_http_error_handler_t error_handler,
        rspamd_http_finish_handler_t finish_handler,
        unsigned opts,
-       enum rspamd_http_connection_type type);
+       enum rspamd_http_connection_type type,
+       struct rspamd_keypair_cache *cache);
 
 /**
  * Load the encryption keypair
index 18c33e63d27b7e8fbd494a2f0a88115950b44e68..5161871dc93e41d31fecd25fa31fa17232b5dcb9 100644 (file)
@@ -588,7 +588,7 @@ rspamd_map_add (struct rspamd_config *cfg,
                hdata->conn = rspamd_http_connection_new (http_map_read, http_map_error,
                        http_map_finish,
                        RSPAMD_HTTP_BODY_PARTIAL | RSPAMD_HTTP_CLIENT_SIMPLE,
-                       RSPAMD_HTTP_CLIENT);
+                       RSPAMD_HTTP_CLIENT, NULL);
                new_map->map_data = hdata;
        }
        /* Temp pool */
index 1d5e3bcf89c7e820cbdf69b6c97207fcef15eeee..ae7682b056a9f2e0cb9aaf812d1533715de77adf 100644 (file)
@@ -158,7 +158,8 @@ lua_http_make_connection (struct lua_http_cbdata *cbd)
        }
        cbd->fd = fd;
        cbd->conn = rspamd_http_connection_new (NULL, lua_http_error_handler,
-                       lua_http_finish_handler, RSPAMD_HTTP_CLIENT_SIMPLE, RSPAMD_HTTP_CLIENT);
+                       lua_http_finish_handler, RSPAMD_HTTP_CLIENT_SIMPLE,
+                       RSPAMD_HTTP_CLIENT, NULL);
 
        rspamd_http_connection_write_message (cbd->conn, cbd->msg,
                        NULL, NULL, cbd, fd, &cbd->tv, cbd->ev_base);
index e55483dafa245ee1f6ac0c7002059ed139507b1f..7a94ee77e9d4faa1f07164dba52366f2b10030fd 100644 (file)
@@ -36,6 +36,7 @@
 #include "libserver/dns.h"
 #include "libmime/message.h"
 #include "main.h"
+#include "keypairs_cache.h"
 
 #include "lua/lua_common.h"
 
@@ -88,6 +89,8 @@ struct rspamd_worker_ctx {
        struct event_base *ev_base;
        /* Encryption key */
        gpointer key;
+       /* Keys cache */
+       struct rspamd_keypair_cache *keys_cache;
 };
 
 /*
@@ -237,7 +240,8 @@ accept_socket (gint fd, short what, void *arg)
                rspamd_worker_error_handler,
                rspamd_worker_finish_handler,
                0,
-               RSPAMD_HTTP_SERVER);
+               RSPAMD_HTTP_SERVER,
+               ctx->keys_cache);
        new_task->ev_base = ctx->ev_base;
        ctx->tasks++;
        rspamd_mempool_add_destructor (new_task->task_pool,
@@ -352,6 +356,9 @@ start_worker (struct rspamd_worker *worker)
                }
        }
 
+       /* XXX: stupid default */
+       ctx->keys_cache = rspamd_keypair_cache_new (256);
+
        event_base_loop (ctx->ev_base, 0);
 
        g_mime_shutdown ();
@@ -361,6 +368,8 @@ start_worker (struct rspamd_worker *worker)
                rspamd_http_connection_key_unref (ctx->key);
        }
 
+       rspamd_keypair_cache_destroy (ctx->keys_cache);
+
        exit (EXIT_SUCCESS);
 }