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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. typedef struct radix_tree_compressed radix_compressed_t;
  26. /**
  27. * Insert new key to the radix trie
  28. * @param tree radix trie
  29. * @param key key to insert (bitstring)
  30. * @param keylen length of the key (in bytes)
  31. * @param masklen length of mask that should be applied to the key (in bits)
  32. * @param value opaque value pointer
  33. * @return previous value of the key or `RADIX_NO_VALUE`
  34. */
  35. uintptr_t
  36. radix_insert_compressed (radix_compressed_t *tree,
  37. guint8 *key, gsize keylen,
  38. gsize masklen,
  39. uintptr_t value);
  40. /**
  41. * Find a key in a radix trie
  42. * @param tree radix trie
  43. * @param key key to find (bitstring)
  44. * @param keylen length of a key
  45. * @return opaque pointer or `RADIX_NO_VALUE` if no value has been found
  46. */
  47. uintptr_t radix_find_compressed (radix_compressed_t *tree, const guint8 *key,
  48. gsize keylen);
  49. /**
  50. * Find specified address in tree (works for IPv4 or IPv6 addresses)
  51. * @param tree
  52. * @param addr
  53. * @return
  54. */
  55. uintptr_t radix_find_compressed_addr (radix_compressed_t *tree,
  56. const rspamd_inet_addr_t *addr);
  57. /**
  58. * Destroy the complete radix trie
  59. * @param tree
  60. */
  61. void radix_destroy_compressed (radix_compressed_t *tree);
  62. /**
  63. * Create new radix trie
  64. * @return
  65. */
  66. radix_compressed_t *radix_create_compressed (const gchar *tree_name);
  67. radix_compressed_t *radix_create_compressed_with_pool (rspamd_mempool_t *pool, const gchar *tree_name);
  68. /**
  69. * Insert list of ip addresses and masks to the radix tree
  70. * @param list string line of addresses
  71. * @param separators string of characters used as separators
  72. * @param tree target tree
  73. * @return number of elements inserted
  74. */
  75. gint rspamd_radix_add_iplist (const gchar *list, const gchar *separators,
  76. radix_compressed_t *tree, gconstpointer value,
  77. gboolean resolve, const gchar *tree_name);
  78. /**
  79. * Generic version of @see rspamd_radix_add_iplist. This function creates tree
  80. * if `tree` is NULL.
  81. */
  82. gboolean
  83. radix_add_generic_iplist (const gchar *ip_list,
  84. radix_compressed_t **tree,
  85. gboolean resolve,
  86. const gchar *tree_name);
  87. /**
  88. * Returns number of elements in the tree
  89. * @param tree
  90. * @return
  91. */
  92. gsize radix_get_size (radix_compressed_t *tree);
  93. /**
  94. * Return string that describes this radix tree (memory, nodes, compression etc)
  95. * @param tree
  96. * @return constant string
  97. */
  98. const gchar *radix_get_info (radix_compressed_t *tree);
  99. /**
  100. * Returns memory pool associated with the radix tree
  101. */
  102. rspamd_mempool_t *radix_get_pool (radix_compressed_t *tree);
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #endif