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.

upstream.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef UPSTREAM_H
  2. #define UPSTREAM_H
  3. #include "config.h"
  4. #include "util.h"
  5. #include "rdns.h"
  6. #include "ucl.h"
  7. enum rspamd_upstream_rotation {
  8. RSPAMD_UPSTREAM_RANDOM = 0,
  9. RSPAMD_UPSTREAM_HASHED,
  10. RSPAMD_UPSTREAM_ROUND_ROBIN,
  11. RSPAMD_UPSTREAM_MASTER_SLAVE,
  12. RSPAMD_UPSTREAM_SEQUENTIAL,
  13. RSPAMD_UPSTREAM_UNDEF
  14. };
  15. enum rspamd_upstream_flag {
  16. RSPAMD_UPSTREAM_FLAG_NORESOLVE = (1 << 0),
  17. };
  18. struct rspamd_config;
  19. /* Opaque upstream structures */
  20. struct upstream;
  21. struct upstream_list;
  22. struct upstream_ctx;
  23. /**
  24. * Init upstreams library
  25. * @param resolver
  26. */
  27. struct upstream_ctx* rspamd_upstreams_library_init (void);
  28. /**
  29. * Remove reference from upstreams library
  30. */
  31. void rspamd_upstreams_library_unref (struct upstream_ctx *ctx);
  32. /**
  33. * Configure attributes of upstreams library
  34. * @param cfg
  35. */
  36. void rspamd_upstreams_library_config (struct rspamd_config *cfg,
  37. struct upstream_ctx *ctx, struct event_base *ev_base,
  38. struct rdns_resolver *resolver);
  39. /**
  40. * Upstream error logic
  41. * 1. During error time we count upstream_ok and upstream_fail
  42. * 2. If failcount is more then maxerrors then we mark upstream as unavailable for dead time
  43. * 3. After dead time we mark upstream as alive and go to the step 1
  44. * 4. If all upstreams are dead, marks every upstream as alive
  45. */
  46. /**
  47. * Add an error to an upstream
  48. */
  49. void rspamd_upstream_fail (struct upstream *up, gboolean addr_failure);
  50. /**
  51. * Increase upstream successes count
  52. */
  53. void rspamd_upstream_ok (struct upstream *up);
  54. /**
  55. * Set weight for an upstream
  56. * @param up
  57. */
  58. void rspamd_upstream_set_weight (struct upstream *up, guint weight);
  59. /**
  60. * Create new list of upstreams
  61. * @return
  62. */
  63. struct upstream_list* rspamd_upstreams_create (struct upstream_ctx *ctx);
  64. /**
  65. * Sets specific flag to the upstream list
  66. * @param ups
  67. * @param flags
  68. */
  69. void rspamd_upstreams_set_flags (struct upstream_list *ups,
  70. enum rspamd_upstream_flag flags);
  71. /**
  72. * Sets custom limits for upstreams
  73. * @param ups
  74. * @param revive_time
  75. * @param revive_jitter
  76. * @param error_time
  77. * @param dns_timeout
  78. * @param max_errors
  79. * @param dns_retransmits
  80. */
  81. void rspamd_upstreams_set_limits (struct upstream_list *ups,
  82. gdouble revive_time,
  83. gdouble revive_jitter,
  84. gdouble error_time,
  85. gdouble dns_timeout,
  86. guint max_errors,
  87. guint dns_retransmits);
  88. /**
  89. * Sets rotation policy for upstreams list
  90. * @param ups
  91. * @param rot
  92. */
  93. void rspamd_upstreams_set_rotation (struct upstream_list *ups,
  94. enum rspamd_upstream_rotation rot);
  95. /**
  96. * Destroy list of upstreams
  97. * @param ups
  98. */
  99. void rspamd_upstreams_destroy (struct upstream_list *ups);
  100. /**
  101. * Returns count of upstreams in a list
  102. * @param ups
  103. * @return
  104. */
  105. gsize rspamd_upstreams_count (struct upstream_list *ups);
  106. /**
  107. * Returns the number of upstreams in the list
  108. * @param ups
  109. * @return
  110. */
  111. gsize rspamd_upstreams_alive (struct upstream_list *ups);
  112. enum rspamd_upstream_parse_type {
  113. RSPAMD_UPSTREAM_PARSE_DEFAULT = 0,
  114. RSPAMD_UPSTREAM_PARSE_NAMESERVER,
  115. };
  116. /**
  117. * Add upstream from the string
  118. * @param ups upstream list
  119. * @param str string in format "name[:port[:priority]]"
  120. * @param def_port default port number
  121. * @param data optional userdata
  122. * @return TRUE if upstream has been added
  123. */
  124. gboolean rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str,
  125. guint16 def_port, enum rspamd_upstream_parse_type parse_type,
  126. void *data);
  127. /**
  128. * Add multiple upstreams from comma, semicolon or space separated line
  129. * @param ups upstream list
  130. * @param str string in format "(<ups>([<sep>+]<ups>)*)+"
  131. * @param def_port default port number
  132. * @param data optional userdata
  133. * @return TRUE if **any** of upstreams has been added
  134. */
  135. gboolean rspamd_upstreams_parse_line (struct upstream_list *ups,
  136. const gchar *str, guint16 def_port, void *data);
  137. /**
  138. * Parse upstreams list from the UCL object
  139. * @param ups
  140. * @param in
  141. * @param def_port
  142. * @param data
  143. * @return
  144. */
  145. gboolean rspamd_upstreams_from_ucl (struct upstream_list *ups,
  146. const ucl_object_t *in, guint16 def_port, void *data);
  147. typedef void (*rspamd_upstream_traverse_func) (struct upstream *up, guint idx,
  148. void *ud);
  149. /**
  150. * Traverse upstreams list calling the function specified
  151. * @param ups
  152. * @param cb
  153. * @param ud
  154. */
  155. void rspamd_upstreams_foreach (struct upstream_list *ups,
  156. rspamd_upstream_traverse_func cb, void *ud);
  157. enum rspamd_upstreams_watch_event {
  158. RSPAMD_UPSTREAM_WATCH_SUCCESS = 1u << 0,
  159. RSPAMD_UPSTREAM_WATCH_FAILURE = 1u << 1,
  160. RSPAMD_UPSTREAM_WATCH_OFFLINE = 1u << 2,
  161. RSPAMD_UPSTREAM_WATCH_ONLINE = 1u << 3,
  162. RSPAMD_UPSTREAM_WATCH_ALL = (1u << 0) | (1u << 1) | (1u << 2) | (1u << 3),
  163. };
  164. typedef void (*rspamd_upstream_watch_func) (struct upstream *up,
  165. enum rspamd_upstreams_watch_event event,
  166. guint cur_errors,
  167. void *ud);
  168. /**
  169. * Adds new watcher to the upstreams list
  170. * @param ups
  171. * @param events
  172. * @param func
  173. * @param ud
  174. */
  175. void rspamd_upstreams_add_watch_callback (struct upstream_list *ups,
  176. enum rspamd_upstreams_watch_event events,
  177. rspamd_upstream_watch_func func,
  178. GFreeFunc free_func,
  179. gpointer ud);
  180. /**
  181. * Returns the current IP address of the upstream
  182. * @param up
  183. * @return
  184. */
  185. rspamd_inet_addr_t* rspamd_upstream_addr (struct upstream *up);
  186. /**
  187. * Add custom address for an upstream (ownership of addr is transferred to upstream)
  188. * @param up
  189. * @return
  190. */
  191. gboolean rspamd_upstream_add_addr (struct upstream *up,
  192. rspamd_inet_addr_t *addr);
  193. /**
  194. * Returns the symbolic name of the upstream
  195. * @param up
  196. * @return
  197. */
  198. const gchar* rspamd_upstream_name (struct upstream *up);
  199. /**
  200. * Sets opaque user data associated with this upstream
  201. * @param up
  202. * @param data
  203. * @return old data
  204. */
  205. gpointer rspamd_upstream_set_data (struct upstream *up, gpointer data);
  206. /**
  207. * Gets opaque user data associated with this upstream
  208. * @param up
  209. * @return
  210. */
  211. gpointer rspamd_upstream_get_data (struct upstream *up);
  212. /**
  213. * Get new upstream from the list
  214. * @param ups upstream list
  215. * @param type type of rotation algorithm, for `RSPAMD_UPSTREAM_HASHED` it is required to specify `key` and `keylen` as arguments
  216. * @return
  217. */
  218. struct upstream* rspamd_upstream_get (struct upstream_list *ups,
  219. enum rspamd_upstream_rotation default_type,
  220. const guchar *key, gsize keylen);
  221. /**
  222. * Get new upstream from the list
  223. * @param ups upstream list
  224. * @param type type of rotation algorithm, for `RSPAMD_UPSTREAM_HASHED` it is required to specify `key` and `keylen` as arguments
  225. * @return
  226. */
  227. struct upstream* rspamd_upstream_get_forced (struct upstream_list *ups,
  228. enum rspamd_upstream_rotation forced_type,
  229. const guchar *key, gsize keylen);
  230. /**
  231. * Re-resolve addresses for all upstreams registered
  232. */
  233. void rspamd_upstream_reresolve (struct upstream_ctx *ctx);
  234. #endif /* UPSTREAM_H */
  235. /*
  236. * vi:ts=4
  237. */