aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-02-02 22:17:48 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-02-02 22:17:48 +0000
commitcb515a1e358ee8ab130c66621c0ba87e3ee59935 (patch)
tree7e33ab4a2aabfe8b6f49f2cbbaa738b2dbf53415
parent4e5107dd39adb721d95cbb10bf17a16002ceaffb (diff)
downloadrspamd-cb515a1e358ee8ab130c66621c0ba87e3ee59935.tar.gz
rspamd-cb515a1e358ee8ab130c66621c0ba87e3ee59935.zip
Update keypairs cache.
-rw-r--r--src/libutil/hash.c7
-rw-r--r--src/libutil/http.c9
-rw-r--r--src/libutil/keypair_private.h1
-rw-r--r--src/libutil/keypairs_cache.c55
-rw-r--r--src/libutil/keypairs_cache.h26
5 files changed, 90 insertions, 8 deletions
diff --git a/src/libutil/hash.c b/src/libutil/hash.c
index ec952e87b..9a9f98e2e 100644
--- a/src/libutil/hash.c
+++ b/src/libutil/hash.c
@@ -151,6 +151,10 @@ rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gpointer key, time_t now)
return NULL;
}
}
+ else {
+ res->store_time = now;
+ }
+
return res->data;
}
@@ -188,6 +192,9 @@ rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value,
}
}
}
+ else {
+ /* XXX: use binary heap here */
+ }
if (removed == 0) {
rspamd_lru_hash_destroy_node (hash->elements);
}
diff --git a/src/libutil/http.c b/src/libutil/http.c
index 9b8e2a98c..b2a33b97f 100644
--- a/src/libutil/http.c
+++ b/src/libutil/http.c
@@ -31,16 +31,9 @@
#include "tweetnacl.h"
#include "blake2.h"
#include "ottery.h"
-
+#include "keypair_private.h"
#include <limits.h>
-struct rspamd_http_keypair {
- guchar pk[crypto_box_PUBLICKEYBYTES];
- guchar sk[crypto_box_SECRETKEYBYTES];
- guchar id[BLAKE2B_OUTBYTES];
- ref_entry_t ref;
-};
-
struct rspamd_http_connection_private {
struct _rspamd_http_privbuf {
GString *data;
diff --git a/src/libutil/keypair_private.h b/src/libutil/keypair_private.h
index 66d04ad7a..287a302b3 100644
--- a/src/libutil/keypair_private.h
+++ b/src/libutil/keypair_private.h
@@ -34,6 +34,7 @@ struct rspamd_http_keypair {
guchar pk[crypto_box_PUBLICKEYBYTES];
guchar sk[crypto_box_SECRETKEYBYTES];
guchar id[BLAKE2B_OUTBYTES];
+ guchar nm[crypto_box_BEFORENMBYTES];
ref_entry_t ref;
};
diff --git a/src/libutil/keypairs_cache.c b/src/libutil/keypairs_cache.c
index 91478bf12..1e747028f 100644
--- a/src/libutil/keypairs_cache.c
+++ b/src/libutil/keypairs_cache.c
@@ -23,3 +23,58 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "config.h"
+#include "main.h"
+#include "keypairs_cache.h"
+#include "keypair_private.h"
+#include "hash.h"
+
+struct rspamd_keypair_elt {
+ guchar nm[crypto_box_BEFORENMBYTES];
+};
+
+struct rspamd_keypair_cache {
+ rspamd_lru_hash_t *hash;
+};
+
+struct rspamd_keypair_cache *
+rspamd_keypair_cache_new (guint max_items)
+{
+ struct rspamd_keypair_cache *c;
+
+ g_assert (max_items > 0);
+
+ c = g_slice_alloc (sizeof (*c));
+ c->hash = rspamd_lru_hash_new (max_items, -1, g_free, g_free);
+
+ return c;
+}
+
+void
+rspamd_keypair_cache_process (struct rspamd_keypair_cache *c,
+ gpointer lk, gpointer rk)
+{
+ struct rspamd_http_keypair *kp_local = (struct rspamd_http_keypair *)lk,
+ *kp_remote = (struct rspamd_http_keypair *)rk;
+ guchar nm[crypto_box_BEFORENMBYTES];
+
+ g_assert (kp_local != NULL);
+ g_assert (kp_remote != NULL);
+
+ /*
+ * XXX: at this point we do nothing, since LRU hash is completely broken
+ * and useless for our purposes
+ */
+ crypto_box_beforenm (nm, kp_remote->pk, kp_local->sk);
+ memcpy (kp_remote->nm, nm, sizeof (nm));
+ memcpy (kp_local->nm, nm, sizeof (nm));
+}
+
+void
+rspamd_keypair_cache_destroy (struct rspamd_keypair_cache *c)
+{
+ if (c != NULL) {
+ rspamd_lru_hash_destroy (c->hash);
+ g_slice_free1 (sizeof (*c), c);
+ }
+}
diff --git a/src/libutil/keypairs_cache.h b/src/libutil/keypairs_cache.h
index 88bc8ba32..544d73456 100644
--- a/src/libutil/keypairs_cache.h
+++ b/src/libutil/keypairs_cache.h
@@ -25,6 +25,32 @@
#ifndef KEYPAIRS_CACHE_H_
#define KEYPAIRS_CACHE_H_
+#include "config.h"
+
+struct rspamd_keypair_cache;
+
+/**
+ * Create new keypair cache of the specified size
+ * @param max_items defines maximum count of elements in the cache
+ * @return new cache
+ */
+struct rspamd_keypair_cache * rspamd_keypair_cache_new (guint max_items);
+
+
+/**
+ * Process local and remote keypair setting beforenm value as appropriate
+ * @param c cache of keypairs
+ * @param lk local key
+ * @param rk remote key
+ */
+void rspamd_keypair_cache_process (struct rspamd_keypair_cache *c,
+ gpointer lk, gpointer rk);
+
+/**
+ * Destroy old keypair cache
+ * @param c cache object
+ */
+void rspamd_keypair_cache_destroy (struct rspamd_keypair_cache *c);
#endif /* KEYPAIRS_CACHE_H_ */