]> source.dussan.org Git - rspamd.git/commitdiff
* Various bugfixes in map and radix code
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Thu, 3 Sep 2009 09:59:12 +0000 (13:59 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Thu, 3 Sep 2009 09:59:12 +0000 (13:59 +0400)
src/map.c
src/mem_pool.c
src/radix.c
src/view.c

index 0202f448f25f91c94d00e4b4944598043f8ea10e..f19b822b6d00ea21f0f053e42b991f5b5f24dba1 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -317,7 +317,7 @@ add_map (const char *map_line, map_cb_t read_callback, map_fin_cb_t fin_callback
                def = map_line + sizeof ("file://") - 1;
        }
        else {
-               msg_err ("add_map: invalid map fetching protocol: %s", map_line);
+               msg_debug ("add_map: invalid map fetching protocol: %s", map_line);
                return FALSE;
        }
        /* Constant pool */
@@ -419,7 +419,7 @@ abstract_parse_list (memory_pool_t *pool, u_char *chunk, size_t len, struct map_
                                if (*p == '#') {
                                        if (s != str) {
                                                *s = '\0';
-                                               s = memory_pool_strdup (pool, str);
+                                               s = memory_pool_strdup (pool, g_strstrip (str));
                                                if (strlen (s) > 0) {
                                                        func (data->cur_data, s, hash_fill);
                                                }
@@ -431,7 +431,7 @@ abstract_parse_list (memory_pool_t *pool, u_char *chunk, size_t len, struct map_
                                else if (*p == '\r' || *p == '\n') {
                                        if (s != str) {
                                                *s = '\0';
-                                               s = memory_pool_strdup (pool, str);
+                                               s = memory_pool_strdup (pool, g_strstrip (str));
                                                if (strlen (s) > 0) {
                                                        func (data->cur_data, s, hash_fill);
                                                }
@@ -442,9 +442,6 @@ abstract_parse_list (memory_pool_t *pool, u_char *chunk, size_t len, struct map_
                                                p ++;
                                        }
                                }
-                               else if (g_ascii_isspace (*p)) {
-                                       p ++;
-                               }
                                else {
                                        *s = *p;
                                        s ++;
@@ -480,38 +477,52 @@ radix_tree_insert_helper (gpointer st, gconstpointer key, gpointer value)
 
        uint32_t mask = 0xFFFFFFFF;
        uint32_t ip;
-       char *token, *ipnet;
+       char *token, *ipnet, *err_str, **strv, **cur;
        struct in_addr ina;
        int k;
        
-       k = strlen ((char *)key) + 1;
-       ipnet = alloca (k);
-       g_strlcpy (ipnet, key, k);
-       token = strsep (&ipnet, "/");
-
-       if (ipnet != NULL) {
-               k = atoi (ipnet);
-               if (k > 32 || k < 0) {
-                       msg_warn ("radix_tree_insert_helper: invalid netmask value: %d", k);
-                       k = 32;
+    strv = g_strsplit_set ((char *)key, " ,;", 0);
+       cur = strv;
+    while (*cur) {
+               if (**cur == '\0') {
+                       cur ++;
+                       continue;
+               }
+               ipnet = *cur;
+               token = strsep (&ipnet, "/");
+
+               if (ipnet != NULL) {
+                       errno = 0;
+                       k = strtoul (ipnet, &err_str, 10);
+                       if (errno != 0) {
+                               msg_warn ("radix_tree_insert_helper: invalid netmask, error detected on symbol: %s, erorr: %s", err_str, strerror (errno));
+                               k = 32;
+                       }
+                       else if (k > 32 || k < 0) {
+                               msg_warn ("radix_tree_insert_helper: invalid netmask value: %d", k);
+                               k = 32;
+                       }
+                       k = 32 - k;
+                       mask = mask << k;
                }
-               k = 32 - k;
-               mask = mask << k;
-       }
 
-       if (inet_aton (token, &ina) == 0) {
-               msg_err ("radix_tree_insert_helper: invalid ip address: %s", token);
-               return;
-       }
+               if (inet_aton (token, &ina) == 0) {
+                       msg_err ("radix_tree_insert_helper: invalid ip address: %s", token);
+                       return;
+               }
 
-       ip = ntohl ((uint32_t)ina.s_addr);
-       k = radix32tree_insert (tree, ip, mask, 1);
-       if (k == -1) {
-               msg_warn ("radix_tree_insert_helper: cannot insert ip to tree: %s, mask %X", inet_ntoa (ina), mask);
-       }
-       else if (k == 1) {
-               msg_warn ("add_ip_radix: ip %s, mask %X, value already exists", inet_ntoa (ina), mask);
+               ip = ntohl ((uint32_t)ina.s_addr);
+               k = radix32tree_insert (tree, ip, mask, 1);
+               if (k == -1) {
+                       msg_warn ("radix_tree_insert_helper: cannot insert ip to tree: %s, mask %X", inet_ntoa (ina), mask);
+               }
+               else if (k == 1) {
+                       msg_warn ("add_ip_radix: ip %s, mask %X, value already exists", inet_ntoa (ina), mask);
+               }
+               cur ++;
        }
+
+       g_strfreev (strv);
 }
 
 u_char *
index e8be6a95c2a9e65fbaec94afbd063d34a14bbcf5..ce6eab86e4066eb6b861a8e6fa0c8fe3de9bfa33 100644 (file)
@@ -60,7 +60,13 @@ pool_chain_new (memory_pool_ssize_t size)
        g_assert (size > 0);
 
        chain = g_slice_alloc (sizeof (struct _pool_chain));
+
+    g_assert (chain != NULL);
+
        chain->begin = g_slice_alloc (size);
+
+    g_assert (chain->begin != NULL);
+
        chain->len = size;
        chain->pos = chain->begin;
        chain->next = NULL;
@@ -78,10 +84,9 @@ pool_chain_new_shared (memory_pool_ssize_t size)
 
 #if defined(HAVE_MMAP_ANON)
        chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
+       g_assert (chain != MAP_FAILED);
        chain->begin = ((u_char *)chain) + sizeof (struct _pool_chain_shared);
-       if (chain == MAP_FAILED) {
-               return NULL;
-       }
+       g_assert (chain->begin != MAP_FAILED);
 #elif defined(HAVE_MMAP_ZERO)
        int fd;
 
@@ -90,10 +95,9 @@ pool_chain_new_shared (memory_pool_ssize_t size)
                return NULL;
        }
        chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+       g_assert (chain != MAP_FAILED);
        chain->begin = ((u_char *)chain) + sizeof (struct _pool_chain_shared);
-       if (chain == MAP_FAILED) {
-               return NULL;
-       }
+       g_assert (chain->begin != MAP_FAILED);
 #else
 #      error No mmap methods are defined
 #endif
@@ -138,6 +142,8 @@ memory_pool_new (memory_pool_ssize_t size)
        }
 
        new = g_slice_alloc (sizeof (memory_pool_t));
+    g_assert (new != NULL);
+
        new->cur_pool = pool_chain_new (size);
        new->shared_pool = NULL;
        new->first_pool = new->cur_pool;
index 8b9df48a7a2a758bae112e0f0aaf11a96360ce2b..e55e9e5bae66b3b7c10938249452652c54542685 100644 (file)
 static void *radix_alloc (radix_tree_t *tree);
 
 radix_tree_t *
-radix_tree_create (memory_pool_t *pool)
+radix_tree_create ()
 {
        radix_tree_t  *tree;
 
-       tree = memory_pool_alloc (pool, sizeof(radix_tree_t));
+       tree = g_malloc (sizeof(radix_tree_t));
        if (tree == NULL) {
                return NULL;
        }
-
+    
+    tree->pool = memory_pool_new (memory_pool_get_size ());
        tree->size = 0;
 
        tree->root = radix_alloc (tree);
@@ -50,7 +51,6 @@ radix_tree_create (memory_pool_t *pool)
        tree->root->left = NULL;
        tree->root->parent = NULL;
        tree->root->value = RADIX_NO_VALUE;
-       tree->pool = pool;
        
        return tree;
 }
@@ -228,37 +228,10 @@ radix_alloc (radix_tree_t *tree)
 void
 radix_tree_free (radix_tree_t *tree) 
 {
-       radix_node_t  *node, *tmp;
-
-       node = tree->root;
-
-       for (;;) {
-               /* We are at the trie root and we have no more leaves, end of algorithm */
-               if (!node->left && !node->right && !node->parent) {
-                       break;
-               }
-
-               /* Traverse to the end of trie */
-               while (node->left || node->right) {
-                       if (node->left) {
-                               node = node->left;
-                       }
-                       else {
-                               node = node->right;
-                       }
-               }
-               /* Found leaf node, free it */
-               if (node->parent->right == node) {
-                       node->parent->right = NULL;
-
-               } else {
-                       node->parent->left = NULL;
-               }
-
-               tmp = node;
-               /* Go up */
-               node = node->parent;
-       }
+    
+       g_return_if_fail (tree != NULL);
+       memory_pool_delete (tree->pool);
+       g_free (tree);
 }
 
 /* 
index ec10eb120aba8fc1b0cb00f91b8df2c3a96dd96d..efad70666db4502c5e9ff6b1cca07637071ac560 100644 (file)
@@ -93,7 +93,7 @@ add_view_symbols (struct rspamd_view *view, char *line)
 gboolean 
 add_view_ip (struct rspamd_view *view, char *line)
 {
-       if (add_map (line, read_radix_list, fin_radix_list, (void **)&view->symbols_hash)) {
+       if (add_map (line, read_radix_list, fin_radix_list, (void **)&view->ip_tree)) {
                return TRUE;
        }