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.

map_helpers.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*-
  2. * Copyright 2018 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 RSPAMD_MAP_HELPERS_H
  17. #define RSPAMD_MAP_HELPERS_H
  18. #include "config.h"
  19. #include "map.h"
  20. #include "addr.h"
  21. /**
  22. * @file map_helpers.h
  23. *
  24. * Defines helper structures to deal with different map types
  25. */
  26. /**
  27. * Common structures, abstract for simplicity
  28. */
  29. struct rspamd_radix_map_helper;
  30. struct rspamd_hash_map_helper;
  31. struct rspamd_regexp_map_helper;
  32. struct rspamd_map_helper_value;
  33. enum rspamd_regexp_map_flags {
  34. RSPAMD_REGEXP_MAP_FLAG_UTF = (1u << 0),
  35. RSPAMD_REGEXP_MAP_FLAG_MULTIPLE = (1u << 1),
  36. RSPAMD_REGEXP_MAP_FLAG_GLOB = (1u << 2),
  37. };
  38. typedef void (*insert_func) (gpointer st, gconstpointer key,
  39. gconstpointer value);
  40. /**
  41. * Radix list is a list like ip/mask
  42. */
  43. gchar * rspamd_radix_read (
  44. gchar *chunk,
  45. gint len,
  46. struct map_cb_data *data,
  47. gboolean final);
  48. void rspamd_radix_fin (struct map_cb_data *data);
  49. void rspamd_radix_dtor (struct map_cb_data *data);
  50. /**
  51. * Kv list is an ordinal list of keys and values separated by whitespace
  52. */
  53. gchar * rspamd_kv_list_read (
  54. gchar *chunk,
  55. gint len,
  56. struct map_cb_data *data,
  57. gboolean final);
  58. void rspamd_kv_list_fin (struct map_cb_data *data);
  59. void rspamd_kv_list_dtor (struct map_cb_data *data);
  60. /**
  61. * Regexp list is a list of regular expressions
  62. */
  63. gchar * rspamd_regexp_list_read_single (
  64. gchar *chunk,
  65. gint len,
  66. struct map_cb_data *data,
  67. gboolean final);
  68. gchar * rspamd_regexp_list_read_multiple (
  69. gchar *chunk,
  70. gint len,
  71. struct map_cb_data *data,
  72. gboolean final);
  73. gchar * rspamd_glob_list_read_single (
  74. gchar *chunk,
  75. gint len,
  76. struct map_cb_data *data,
  77. gboolean final);
  78. gchar * rspamd_glob_list_read_multiple (
  79. gchar *chunk,
  80. gint len,
  81. struct map_cb_data *data,
  82. gboolean final);
  83. void rspamd_regexp_list_fin (struct map_cb_data *data);
  84. void rspamd_regexp_list_dtor (struct map_cb_data *data);
  85. /**
  86. * FSM for lists parsing (support comments, blank lines and partial replies)
  87. */
  88. gchar *
  89. rspamd_parse_kv_list (
  90. gchar * chunk,
  91. gint len,
  92. struct map_cb_data *data,
  93. insert_func func,
  94. const gchar *default_value,
  95. gboolean final);
  96. /**
  97. * Find a single (any) matching regexp for the specified text or NULL if
  98. * no matches found
  99. * @param map
  100. * @param in
  101. * @param len
  102. * @return
  103. */
  104. gconstpointer rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map,
  105. const gchar *in, gsize len);
  106. /**
  107. * Find a multiple (all) matching regexp for the specified text or NULL if
  108. * no matches found. Returns GPtrArray that *must* be freed by a caller if not NULL
  109. * @param map
  110. * @param in
  111. * @param len
  112. * @return
  113. */
  114. GPtrArray* rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map,
  115. const gchar *in, gsize len);
  116. /**
  117. * Find value matching specific key in a hash map
  118. * @param map
  119. * @param in
  120. * @param len
  121. * @return
  122. */
  123. gconstpointer rspamd_match_hash_map (struct rspamd_hash_map_helper *map,
  124. const gchar *in);
  125. /**
  126. * Find value matching specific key in a hash map
  127. * @param map
  128. * @param in raw ip address
  129. * @param inlen ip address length (4 for IPv4 and 16 for IPv6)
  130. * @return
  131. */
  132. gconstpointer rspamd_match_radix_map (struct rspamd_radix_map_helper *map,
  133. const guchar *in, gsize inlen);
  134. gconstpointer rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map,
  135. const rspamd_inet_addr_t *addr);
  136. /**
  137. * Creates radix map helper
  138. * @param map
  139. * @return
  140. */
  141. struct rspamd_radix_map_helper *rspamd_map_helper_new_radix (struct rspamd_map *map);
  142. /**
  143. * Inserts new value into radix map
  144. * @param st
  145. * @param key
  146. * @param value
  147. */
  148. void rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer value);
  149. /**
  150. * Inserts new value into radix map performing synchronous resolving
  151. * @param st
  152. * @param key
  153. * @param value
  154. */
  155. void rspamd_map_helper_insert_radix_resolve (gpointer st, gconstpointer key,
  156. gconstpointer value);
  157. /**
  158. * Destroys radix map helper
  159. * @param r
  160. */
  161. void rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r);
  162. /**
  163. * Creates hash map helper
  164. * @param map
  165. * @return
  166. */
  167. struct rspamd_hash_map_helper *rspamd_map_helper_new_hash (struct rspamd_map *map);
  168. /**
  169. * Inserts a new value into a hash map
  170. * @param st
  171. * @param key
  172. * @param value
  173. */
  174. void rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer value);
  175. /**
  176. * Destroys hash map helper
  177. * @param r
  178. */
  179. void rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r);
  180. /**
  181. * Create new regexp map
  182. * @param map
  183. * @param flags
  184. * @return
  185. */
  186. struct rspamd_regexp_map_helper * rspamd_map_helper_new_regexp (struct rspamd_map *map,
  187. enum rspamd_regexp_map_flags flags);
  188. /**
  189. * Inserts a new regexp into regexp map
  190. * @param st
  191. * @param key
  192. * @param value
  193. */
  194. void rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value);
  195. /**
  196. * Destroy regexp map
  197. * @param re_map
  198. */
  199. void rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map);
  200. #endif