#include "filter.h"
#include "lua/lua_common.h"
#include "map.h"
+#include "map_helpers.h"
#include "map_private.h"
#include "dynamic_cfg.h"
#include "utlist.h"
rspamd_config_radix_from_ucl (struct rspamd_config *cfg,
const ucl_object_t *obj,
const gchar *description,
- radix_compressed_t **target,
+ struct rspamd_radix_map_helper **target,
GError **err)
{
ucl_type_t type;
}
else {
/* Just a list */
- if (!radix_add_generic_iplist (str, target, TRUE)) {
- g_set_error (err, g_quark_from_static_string ("rspamd-config"),
- EINVAL, "bad map definition %s for %s", str,
- ucl_object_key (obj));
- return FALSE;
- }
+ *target = rspamd_map_helper_new_radix (NULL);
+ rspamd_map_helper_insert_radix (*target, str, "");
}
break;
case UCL_OBJECT:
while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
str = ucl_object_tostring (cur);
- if (str == NULL || !radix_add_generic_iplist (str, target, TRUE)) {
- g_set_error (err, g_quark_from_static_string ("rspamd-config"),
- EINVAL, "bad map element %s for %s", str,
- ucl_object_key (obj));
-
- if (*target) {
- radix_destroy_compressed (*target);
- }
-
- ucl_object_iterate_free (it);
-
- return FALSE;
+ if (!*target) {
+ *target = rspamd_map_helper_new_radix (NULL);
}
+
+ rspamd_map_helper_insert_radix (*target, str, "");
}
ucl_object_iterate_free (it);
khash_t(rspamd_map_hash) *htb;
};
-enum rspamd_regexp_map_flags {
- RSPAMD_REGEXP_FLAG_UTF = (1u << 0),
- RSPAMD_REGEXP_FLAG_MULTIPLE = (1u << 1),
- RSPAMD_REGEXP_FLAG_GLOB = (1u << 2),
-};
-
struct rspamd_regexp_map_helper {
rspamd_mempool_t *pool;
struct rspamd_map *map;
/**
* Radix tree helper function
*/
-static void
-radix_tree_insert_helper (gpointer st, gconstpointer key, gconstpointer value)
+void
+rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer value)
{
struct rspamd_radix_map_helper *r = (struct rspamd_radix_map_helper *)st;
struct rspamd_map_helper_value *val;
rspamd_radix_add_iplist (key, ",", r->trie, val, FALSE);
}
-static void
-hash_insert_helper (gpointer st, gconstpointer key, gconstpointer value)
+void
+rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer value)
{
struct rspamd_hash_map_helper *ht = st;
struct rspamd_map_helper_value *val;
kh_value (ht->htb, k) = val;
}
-static void
-rspamd_re_map_insert_helper (gpointer st, gconstpointer key, gconstpointer value)
+void
+rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value)
{
struct rspamd_regexp_map_helper *re_map = st;
struct rspamd_map *map;
map = re_map->map;
- if (re_map->map_flags & RSPAMD_REGEXP_FLAG_GLOB) {
+ if (re_map->map_flags & RSPAMD_REGEXP_MAP_FLAG_GLOB) {
escaped = rspamd_str_regexp_escape (key, strlen (key), &escaped_len,
TRUE);
re = rspamd_regexp_new (escaped, NULL, &err);
#ifndef WITH_PCRE2
if (pcre_flags & PCRE_FLAG(UTF8)) {
- re_map->map_flags |= RSPAMD_REGEXP_FLAG_UTF;
+ re_map->map_flags |= RSPAMD_REGEXP_MAP_FLAG_UTF;
}
#else
if (pcre_flags & PCRE_FLAG(UTF)) {
- re_map->map_flags |= RSPAMD_REGEXP_FLAG_UTF;
+ re_map->map_flags |= RSPAMD_REGEXP_MAP_FLAG_UTF;
}
#endif
}
-static struct rspamd_hash_map_helper *
+struct rspamd_hash_map_helper *
rspamd_map_helper_new_hash (struct rspamd_map *map)
{
struct rspamd_hash_map_helper *htb;
rspamd_mempool_t *pool;
- pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
- map->tag);
+ if (map) {
+ pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
+ map->tag);
+ }
+ else {
+ pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
+ NULL);
+ }
+
htb = rspamd_mempool_alloc0 (pool, sizeof (*htb));
htb->htb = kh_init (rspamd_map_hash);
htb->pool = pool;
return htb;
}
-static struct rspamd_radix_map_helper *
+void
+rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r)
+{
+ kh_destroy (rspamd_map_hash, r->htb);
+ rspamd_mempool_delete (r->pool);
+}
+
+struct rspamd_radix_map_helper *
rspamd_map_helper_new_radix (struct rspamd_map *map)
{
struct rspamd_radix_map_helper *r;
rspamd_mempool_t *pool;
- pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
- map->tag);
+ if (map) {
+ pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
+ map->tag);
+ }
+ else {
+ pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
+ NULL);
+ }
+
r = rspamd_mempool_alloc0 (pool, sizeof (*r));
r->trie = radix_create_compressed_with_pool (pool);
r->htb = kh_init (rspamd_map_hash);
return r;
}
-static struct rspamd_regexp_map_helper *
-rspamd_regexp_map_create (struct rspamd_map *map,
+void
+rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r)
+{
+ kh_destroy (rspamd_map_hash, r->htb);
+ rspamd_mempool_delete (r->pool);
+}
+
+struct rspamd_regexp_map_helper *
+rspamd_map_helper_new_regexp (struct rspamd_map *map,
enum rspamd_regexp_map_flags flags)
{
struct rspamd_regexp_map_helper *re_map;
}
-static void
-rspamd_regexp_map_destroy (struct rspamd_regexp_map_helper *re_map)
+void
+rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map)
{
rspamd_regexp_t *re;
guint i;
chunk,
len,
data,
- hash_insert_helper,
+ rspamd_map_helper_insert_hash,
"",
final);
}
if (data->prev_data) {
htb = (struct rspamd_hash_map_helper *)data->prev_data;
- kh_destroy (rspamd_map_hash, htb->htb);
- rspamd_mempool_delete (htb->pool);
+ rspamd_map_helper_destroy_hash (htb);
}
if (data->cur_data) {
chunk,
len,
data,
- radix_tree_insert_helper,
+ rspamd_map_helper_insert_radix,
hash_fill,
final);
}
if (data->prev_data) {
r = (struct rspamd_radix_map_helper *)data->prev_data;
- kh_destroy (rspamd_map_hash, r->htb);
- rspamd_mempool_delete (r->pool);
+ rspamd_map_helper_destroy_radix (r);
}
if (data->cur_data) {
struct rspamd_regexp_map_helper *re_map;
if (data->cur_data == NULL) {
- re_map = rspamd_regexp_map_create (data->map, 0);
+ re_map = rspamd_map_helper_new_regexp (data->map, 0);
data->cur_data = re_map;
}
chunk,
len,
data,
- rspamd_re_map_insert_helper,
+ rspamd_map_helper_insert_re,
hash_fill,
final);
}
struct rspamd_regexp_map_helper *re_map;
if (data->cur_data == NULL) {
- re_map = rspamd_regexp_map_create (data->map, RSPAMD_REGEXP_FLAG_GLOB);
+ re_map = rspamd_map_helper_new_regexp (data->map, RSPAMD_REGEXP_MAP_FLAG_GLOB);
data->cur_data = re_map;
}
chunk,
len,
data,
- rspamd_re_map_insert_helper,
+ rspamd_map_helper_insert_re,
hash_fill,
final);
}
struct rspamd_regexp_map_helper *re_map;
if (data->cur_data == NULL) {
- re_map = rspamd_regexp_map_create (data->map, RSPAMD_REGEXP_FLAG_MULTIPLE);
+ re_map = rspamd_map_helper_new_regexp (data->map, RSPAMD_REGEXP_MAP_FLAG_MULTIPLE);
data->cur_data = re_map;
}
chunk,
len,
data,
- rspamd_re_map_insert_helper,
+ rspamd_map_helper_insert_re,
hash_fill,
final);
}
struct rspamd_map *map = data->map;
if (data->prev_data) {
- rspamd_regexp_map_destroy (data->prev_data);
+ rspamd_map_helper_destroy_regexp (data->prev_data);
}
if (data->cur_data) {
re_map = data->cur_data;
return NULL;
}
- if (map->map_flags & RSPAMD_REGEXP_FLAG_UTF) {
+ if (map->map_flags & RSPAMD_REGEXP_MAP_FLAG_UTF) {
if (g_utf8_validate (in, len, NULL)) {
validated = TRUE;
}
return NULL;
}
- if (map->map_flags & RSPAMD_REGEXP_FLAG_UTF) {
+ if (map->map_flags & RSPAMD_REGEXP_MAP_FLAG_UTF) {
if (g_utf8_validate (in, len, NULL)) {
validated = TRUE;
}
val = (struct rspamd_map_helper_value *)radix_find_compressed (map->trie,
in, inlen);
- if (val) {
+ if (val != (gconstpointer)RADIX_NO_VALUE) {
+ val->hits ++;
+
+ return val->value;
+ }
+
+ return NULL;
+}
+
+gconstpointer
+rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map,
+ const rspamd_inet_addr_t *addr)
+{
+ struct rspamd_map_helper_value *val;
+
+ val = (struct rspamd_map_helper_value *)radix_find_compressed_addr (map->trie, addr);
+
+ if (val != (gconstpointer)RADIX_NO_VALUE) {
val->hits ++;
return val->value;
#include "config.h"
#include "map.h"
+#include "addr.h"
/**
* @file map_helpers.h
struct rspamd_radix_map_helper;
struct rspamd_hash_map_helper;
struct rspamd_regexp_map_helper;
+struct rspamd_map_helper_value;
+
+enum rspamd_regexp_map_flags {
+ RSPAMD_REGEXP_MAP_FLAG_UTF = (1u << 0),
+ RSPAMD_REGEXP_MAP_FLAG_MULTIPLE = (1u << 1),
+ RSPAMD_REGEXP_MAP_FLAG_GLOB = (1u << 2),
+};
typedef void (*insert_func) (gpointer st, gconstpointer key,
gconstpointer value);
gconstpointer rspamd_match_radix_map (struct rspamd_radix_map_helper *map,
const guchar *in, gsize inlen);
+gconstpointer rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map,
+ const rspamd_inet_addr_t *addr);
+
+/**
+ * Creates radix map helper
+ * @param map
+ * @return
+ */
+struct rspamd_radix_map_helper *rspamd_map_helper_new_radix (struct rspamd_map *map);
+/**
+ * Inserts new value into radix map
+ * @param st
+ * @param key
+ * @param value
+ */
+void rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer value);
+/**
+ * Destroys radix map helper
+ * @param r
+ */
+void rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r);
+
+
+/**
+ * Creates hash map helper
+ * @param map
+ * @return
+ */
+struct rspamd_hash_map_helper *rspamd_map_helper_new_hash (struct rspamd_map *map);
+/**
+ * Inserts a new value into a hash map
+ * @param st
+ * @param key
+ * @param value
+ */
+void rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer value);
+/**
+ * Destroys hash map helper
+ * @param r
+ */
+void rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r);
+
+/**
+ * Create new regexp map
+ * @param map
+ * @param flags
+ * @return
+ */
+struct rspamd_regexp_map_helper * rspamd_map_helper_new_regexp (struct rspamd_map *map,
+ enum rspamd_regexp_map_flags flags);
+/**
+ * Inserts a new regexp into regexp map
+ * @param st
+ * @param key
+ * @param value
+ */
+void rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value);
+/**
+ * Destroy regexp map
+ * @param re_map
+ */
+void rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map);
+
#endif