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.

re_cache.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 RSPAMD_RE_CACHE_H
  17. #define RSPAMD_RE_CACHE_H
  18. #include "config.h"
  19. #include "libutil/regexp.h"
  20. struct rspamd_re_cache;
  21. struct rspamd_re_runtime;
  22. struct rspamd_task;
  23. struct rspamd_config;
  24. enum rspamd_re_type {
  25. RSPAMD_RE_HEADER,
  26. RSPAMD_RE_RAWHEADER,
  27. RSPAMD_RE_ALLHEADER,
  28. RSPAMD_RE_MIMEHEADER,
  29. RSPAMD_RE_MIME,
  30. RSPAMD_RE_RAWMIME,
  31. RSPAMD_RE_URL,
  32. RSPAMD_RE_BODY, /* full in SA */
  33. RSPAMD_RE_SABODY, /* body in SA */
  34. RSPAMD_RE_SARAWBODY, /* rawbody in SA */
  35. RSPAMD_RE_SELECTOR, /* use lua selector to process regexp */
  36. RSPAMD_RE_MAX
  37. };
  38. struct rspamd_re_cache_stat {
  39. guint64 bytes_scanned;
  40. guint64 bytes_scanned_pcre;
  41. guint regexp_checked;
  42. guint regexp_matched;
  43. guint regexp_total;
  44. guint regexp_fast_cached;
  45. };
  46. /**
  47. * Initialize re_cache persistent structure
  48. */
  49. struct rspamd_re_cache *rspamd_re_cache_new (void);
  50. /**
  51. * Add the existing regexp to the cache
  52. * @param cache cache object
  53. * @param re regexp object
  54. * @param type type of object
  55. * @param type_data associated data with the type (e.g. header name)
  56. * @param datalen associated data length
  57. */
  58. rspamd_regexp_t *
  59. rspamd_re_cache_add (struct rspamd_re_cache *cache, rspamd_regexp_t *re,
  60. enum rspamd_re_type type,
  61. gconstpointer type_data, gsize datalen);
  62. /**
  63. * Replace regexp in the cache with another regexp
  64. * @param cache cache object
  65. * @param what re to replace
  66. * @param with regexp object to replace the origin
  67. */
  68. void rspamd_re_cache_replace (struct rspamd_re_cache *cache,
  69. rspamd_regexp_t *what,
  70. rspamd_regexp_t *with);
  71. /**
  72. * Initialize and optimize re cache structure
  73. */
  74. void rspamd_re_cache_init (struct rspamd_re_cache *cache,
  75. struct rspamd_config *cfg);
  76. /**
  77. * Returns true when hyperscan is loaded
  78. * @param cache
  79. * @return
  80. */
  81. gboolean rspamd_re_cache_is_hs_loaded (struct rspamd_re_cache *cache);
  82. /**
  83. * Get runtime data for a cache
  84. */
  85. struct rspamd_re_runtime* rspamd_re_cache_runtime_new (struct rspamd_re_cache *cache);
  86. /**
  87. * Get runtime statistics
  88. */
  89. const struct rspamd_re_cache_stat *
  90. rspamd_re_cache_get_stat (struct rspamd_re_runtime *rt);
  91. /**
  92. * Process regexp runtime and return the result for a specific regexp
  93. * @param task task object
  94. * @param rt cache runtime object
  95. * @param re regexp object
  96. * @param type type of object
  97. * @param type_data associated data with the type (e.g. header name)
  98. * @param datalen associated data length
  99. * @param is_strong use case sensitive match when looking for headers
  100. */
  101. gint rspamd_re_cache_process (struct rspamd_task *task,
  102. rspamd_regexp_t *re,
  103. enum rspamd_re_type type,
  104. gconstpointer type_data,
  105. gsize datalen,
  106. gboolean is_strong);
  107. int rspamd_re_cache_process_ffi (void *ptask,
  108. void *pre,
  109. int type,
  110. void *type_data,
  111. int is_strong);
  112. /**
  113. * Destroy runtime data
  114. */
  115. void rspamd_re_cache_runtime_destroy (struct rspamd_re_runtime *rt);
  116. /**
  117. * Unref re cache
  118. */
  119. void rspamd_re_cache_unref (struct rspamd_re_cache *cache);
  120. /**
  121. * Retain reference to re cache
  122. */
  123. struct rspamd_re_cache *rspamd_re_cache_ref (struct rspamd_re_cache *cache);
  124. /**
  125. * Set limit for all regular expressions in the cache, returns previous limit
  126. */
  127. guint rspamd_re_cache_set_limit (struct rspamd_re_cache *cache, guint limit);
  128. /**
  129. * Convert re type to a human readable string (constant one)
  130. */
  131. const gchar * rspamd_re_cache_type_to_string (enum rspamd_re_type type);
  132. /**
  133. * Convert re type string to the type enum
  134. */
  135. enum rspamd_re_type rspamd_re_cache_type_from_string (const char *str);
  136. /**
  137. * Compile expressions to the hyperscan tree and store in the `cache_dir`
  138. */
  139. gint rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
  140. const char *cache_dir, gdouble max_time, gboolean silent,
  141. GError **err);
  142. /**
  143. * Returns TRUE if the specified file is valid hyperscan cache
  144. */
  145. gboolean rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache,
  146. const char *path, gboolean silent, gboolean try_load);
  147. /**
  148. * Loads all hyperscan regexps precompiled
  149. */
  150. gboolean rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache,
  151. const char *cache_dir);
  152. /**
  153. * Registers lua selector in the cache
  154. */
  155. void rspamd_re_cache_add_selector (struct rspamd_re_cache *cache,
  156. const gchar *sname, gint ref);
  157. #endif