You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

radix.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RADIX_H
  17. #define RADIX_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "util.h"
  21. #define RADIX_NO_VALUE (uintptr_t)-1
  22. typedef struct radix_tree_compressed radix_compressed_t;
  23. /**
  24. * Insert new key to the radix trie
  25. * @param tree radix trie
  26. * @param key key to insert (bitstring)
  27. * @param keylen length of the key (in bytes)
  28. * @param masklen length of mask that should be applied to the key (in bits)
  29. * @param value opaque value pointer
  30. * @return previous value of the key or `RADIX_NO_VALUE`
  31. */
  32. uintptr_t
  33. radix_insert_compressed (radix_compressed_t * tree,
  34. guint8 *key, gsize keylen,
  35. gsize masklen,
  36. uintptr_t value);
  37. /**
  38. * Find a key in a radix trie
  39. * @param tree radix trie
  40. * @param key key to find (bitstring)
  41. * @param keylen length of a key
  42. * @return opaque pointer or `RADIX_NO_VALUE` if no value has been found
  43. */
  44. uintptr_t radix_find_compressed (radix_compressed_t * tree, const guint8 *key,
  45. gsize keylen);
  46. /**
  47. * Find specified address in tree (works for IPv4 or IPv6 addresses)
  48. * @param tree
  49. * @param addr
  50. * @return
  51. */
  52. uintptr_t radix_find_compressed_addr (radix_compressed_t *tree,
  53. const rspamd_inet_addr_t *addr);
  54. /**
  55. * Destroy the complete radix trie
  56. * @param tree
  57. */
  58. void radix_destroy_compressed (radix_compressed_t *tree);
  59. /**
  60. * Create new radix trie
  61. * @return
  62. */
  63. radix_compressed_t *radix_create_compressed (void);
  64. radix_compressed_t *radix_create_compressed_with_pool (rspamd_mempool_t *pool);
  65. /**
  66. * Insert list of ip addresses and masks to the radix tree
  67. * @param list string line of addresses
  68. * @param separators string of characters used as separators
  69. * @param tree target tree
  70. * @return number of elements inserted
  71. */
  72. gint rspamd_radix_add_iplist (const gchar *list, const gchar *separators,
  73. radix_compressed_t *tree, gconstpointer value, gboolean resolve);
  74. /**
  75. * Generic version of @see rspamd_radix_add_iplist. This function creates tree
  76. * if `tree` is NULL.
  77. */
  78. gboolean radix_add_generic_iplist (const gchar *ip_list,
  79. radix_compressed_t **tree, gboolean resolve);
  80. /**
  81. * Returns number of elements in the tree
  82. * @param tree
  83. * @return
  84. */
  85. gsize radix_get_size (radix_compressed_t *tree);
  86. /**
  87. * Return string that describes this radix tree (memory, nodes, compression etc)
  88. * @param tree
  89. * @return constant string
  90. */
  91. const gchar * radix_get_info (radix_compressed_t *tree);
  92. /**
  93. * Returns memory pool associated with the radix tree
  94. */
  95. rspamd_mempool_t* radix_get_pool (radix_compressed_t *tree);
  96. #endif