From: Vsevolod Stakhov Date: Mon, 2 Feb 2015 22:17:48 +0000 (+0000) Subject: Update keypairs cache. X-Git-Tag: 0.9.0~780 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cb515a1e358ee8ab130c66621c0ba87e3ee59935;p=rspamd.git Update keypairs cache. --- 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 -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_ */