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 6.1KB

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