From: Vsevolod Stakhov Date: Mon, 14 Dec 2015 11:03:31 +0000 (+0000) Subject: Add routines to use rspamd_inet_addr_t in hash tables X-Git-Tag: 1.1.0~316 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=61330ef453ae1eb997b2567928587d3c71daf87f;p=rspamd.git Add routines to use rspamd_inet_addr_t in hash tables --- diff --git a/src/libutil/addr.c b/src/libutil/addr.c index 2b5ca9854..1da69e114 100644 --- a/src/libutil/addr.c +++ b/src/libutil/addr.c @@ -25,6 +25,7 @@ #include "addr.h" #include "util.h" #include "logger.h" +#include "xxhash.h" #include "unix-std.h" /* pwd and grp */ @@ -1276,3 +1277,32 @@ rspamd_inet_address_get_af (const rspamd_inet_addr_t *addr) return addr->af; } + + +guint +rspamd_inet_address_hash (gconstpointer a) +{ + const rspamd_inet_addr_t *addr = a; + XXH64_state_t st; + + XXH64_reset (&st, rspamd_hash_seed ()); + XXH64_update (&st, &addr->af, sizeof (addr->af)); + + + if (addr->u.un) { + XXH64_update (&st, addr->u.un, sizeof (*addr->u.un)); + } + else { + XXH64_update (&st, &addr->u.in.addr, addr->slen); + } + + return XXH64_digest (&st); +} + +gboolean +rspamd_inet_address_equal (gconstpointer a, gconstpointer b) +{ + const rspamd_inet_addr_t *a1 = a, *a2 = b; + + return rspamd_inet_address_compare (a1, a2) == 0; +} diff --git a/src/libutil/addr.h b/src/libutil/addr.h index d686fa52a..fa2e4e65b 100644 --- a/src/libutil/addr.h +++ b/src/libutil/addr.h @@ -249,4 +249,15 @@ gint rspamd_inet_address_compare_ptr (gconstpointer a1, */ rspamd_inet_addr_t *rspamd_inet_address_copy (const rspamd_inet_addr_t *addr); +/** + * Returns hash for inet address + */ +guint rspamd_inet_address_hash (gconstpointer a); + + +/** + * Returns true if two address are equal + */ +gboolean rspamd_inet_address_equal (gconstpointer a, gconstpointer b); + #endif /* ADDR_H_ */