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.

rspamd_control.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright 2024 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_RSPAMD_CONTROL_H
  17. #define RSPAMD_RSPAMD_CONTROL_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "contrib/libev/ev.h"
  21. G_BEGIN_DECLS
  22. struct rspamd_main;
  23. struct rspamd_worker;
  24. enum rspamd_control_type {
  25. RSPAMD_CONTROL_STAT = 0,
  26. RSPAMD_CONTROL_RELOAD,
  27. RSPAMD_CONTROL_RERESOLVE,
  28. RSPAMD_CONTROL_RECOMPILE,
  29. RSPAMD_CONTROL_HYPERSCAN_LOADED,
  30. RSPAMD_CONTROL_LOG_PIPE,
  31. RSPAMD_CONTROL_FUZZY_STAT,
  32. RSPAMD_CONTROL_FUZZY_SYNC,
  33. RSPAMD_CONTROL_MONITORED_CHANGE,
  34. RSPAMD_CONTROL_CHILD_CHANGE,
  35. RSPAMD_CONTROL_FUZZY_BLOCKED,
  36. RSPAMD_CONTROL_MAX
  37. };
  38. enum rspamd_srv_type {
  39. RSPAMD_SRV_SOCKETPAIR = 0,
  40. RSPAMD_SRV_HYPERSCAN_LOADED,
  41. RSPAMD_SRV_MONITORED_CHANGE,
  42. RSPAMD_SRV_LOG_PIPE,
  43. RSPAMD_SRV_ON_FORK,
  44. RSPAMD_SRV_HEARTBEAT,
  45. RSPAMD_SRV_HEALTH,
  46. RSPAMD_SRV_NOTICE_HYPERSCAN_CACHE,
  47. RSPAMD_SRV_FUZZY_BLOCKED, /* Used to notify main process about a blocked ip */
  48. };
  49. enum rspamd_log_pipe_type {
  50. RSPAMD_LOG_PIPE_SYMBOLS = 0,
  51. };
  52. #define CONTROL_PATHLEN MIN(PATH_MAX, PIPE_BUF - sizeof(int) * 2 - sizeof(int64_t) * 2)
  53. struct rspamd_control_command {
  54. enum rspamd_control_type type;
  55. union {
  56. struct {
  57. unsigned int unused;
  58. } stat;
  59. struct {
  60. unsigned int unused;
  61. } reload;
  62. struct {
  63. unsigned int unused;
  64. } reresolve;
  65. struct {
  66. unsigned int unused;
  67. } recompile;
  68. struct {
  69. gboolean forced;
  70. char cache_dir[CONTROL_PATHLEN];
  71. } hs_loaded;
  72. struct {
  73. char tag[32];
  74. gboolean alive;
  75. pid_t sender;
  76. } monitored_change;
  77. struct {
  78. enum rspamd_log_pipe_type type;
  79. } log_pipe;
  80. struct {
  81. unsigned int unused;
  82. } fuzzy_stat;
  83. struct {
  84. unsigned int unused;
  85. } fuzzy_sync;
  86. struct {
  87. enum {
  88. rspamd_child_offline,
  89. rspamd_child_online,
  90. rspamd_child_terminated,
  91. } what;
  92. pid_t pid;
  93. unsigned int additional;
  94. } child_change;
  95. struct {
  96. union {
  97. struct sockaddr sa;
  98. struct sockaddr_in s4;
  99. struct sockaddr_in6 s6;
  100. } addr;
  101. sa_family_t af;
  102. } fuzzy_blocked;
  103. } cmd;
  104. };
  105. struct rspamd_control_reply {
  106. enum rspamd_control_type type;
  107. union {
  108. struct {
  109. unsigned int conns;
  110. double uptime;
  111. double utime;
  112. double systime;
  113. gulong maxrss;
  114. } stat;
  115. struct {
  116. unsigned int status;
  117. } reload;
  118. struct {
  119. unsigned int status;
  120. } reresolve;
  121. struct {
  122. unsigned int status;
  123. } recompile;
  124. struct {
  125. unsigned int status;
  126. } hs_loaded;
  127. struct {
  128. unsigned int status;
  129. } monitored_change;
  130. struct {
  131. unsigned int status;
  132. } log_pipe;
  133. struct {
  134. unsigned int status;
  135. char storage_id[MEMPOOL_UID_LEN];
  136. } fuzzy_stat;
  137. struct {
  138. unsigned int status;
  139. } fuzzy_sync;
  140. struct {
  141. unsigned int status;
  142. } fuzzy_blocked;
  143. } reply;
  144. };
  145. #define PAIR_ID_LEN 16
  146. struct rspamd_srv_command {
  147. enum rspamd_srv_type type;
  148. uint64_t id;
  149. union {
  150. struct {
  151. int af;
  152. char pair_id[PAIR_ID_LEN];
  153. unsigned int pair_num;
  154. } spair;
  155. struct {
  156. gboolean forced;
  157. char cache_dir[CONTROL_PATHLEN];
  158. } hs_loaded;
  159. struct {
  160. char tag[32];
  161. gboolean alive;
  162. pid_t sender;
  163. } monitored_change;
  164. struct {
  165. enum rspamd_log_pipe_type type;
  166. } log_pipe;
  167. struct {
  168. pid_t ppid;
  169. pid_t cpid;
  170. enum {
  171. child_create = 0,
  172. child_dead,
  173. } state;
  174. } on_fork;
  175. struct {
  176. unsigned int status;
  177. /* TODO: add more fields */
  178. } heartbeat;
  179. struct {
  180. unsigned int status;
  181. } health;
  182. /* Used when a worker loads a valid hyperscan file */
  183. struct {
  184. char path[CONTROL_PATHLEN];
  185. } hyperscan_cache_file;
  186. /* Send when one worker has blocked some IP address */
  187. struct {
  188. union {
  189. struct sockaddr sa;
  190. struct sockaddr_in s4;
  191. struct sockaddr_in6 s6;
  192. } addr;
  193. sa_family_t af;
  194. } fuzzy_blocked;
  195. } cmd;
  196. };
  197. struct rspamd_srv_reply {
  198. enum rspamd_srv_type type;
  199. uint64_t id;
  200. union {
  201. struct {
  202. int code;
  203. } spair;
  204. struct {
  205. int forced;
  206. } hs_loaded;
  207. struct {
  208. int status;
  209. };
  210. struct {
  211. enum rspamd_log_pipe_type type;
  212. } log_pipe;
  213. struct {
  214. int status;
  215. } on_fork;
  216. struct {
  217. int status;
  218. } heartbeat;
  219. struct {
  220. unsigned int status;
  221. unsigned int workers_count;
  222. unsigned int scanners_count;
  223. unsigned int workers_hb_lost;
  224. } health;
  225. struct {
  226. int unused;
  227. } hyperscan_cache_file;
  228. struct {
  229. int unused;
  230. } fuzzy_blocked;
  231. } reply;
  232. };
  233. typedef gboolean (*rspamd_worker_control_handler)(struct rspamd_main *rspamd_main,
  234. struct rspamd_worker *worker,
  235. int fd,
  236. int attached_fd,
  237. struct rspamd_control_command *cmd,
  238. gpointer ud);
  239. typedef void (*rspamd_srv_reply_handler)(struct rspamd_worker *worker,
  240. struct rspamd_srv_reply *rep, int rep_fd,
  241. gpointer ud);
  242. /**
  243. * Process client socket connection
  244. */
  245. void rspamd_control_process_client_socket(struct rspamd_main *rspamd_main,
  246. int fd, rspamd_inet_addr_t *addr);
  247. /**
  248. * Register default handlers for a worker
  249. */
  250. void rspamd_control_worker_add_default_cmd_handlers(struct rspamd_worker *worker,
  251. struct ev_loop *ev_base);
  252. /**
  253. * Register custom handler for a specific control command for this worker
  254. */
  255. void rspamd_control_worker_add_cmd_handler(struct rspamd_worker *worker,
  256. enum rspamd_control_type type,
  257. rspamd_worker_control_handler handler,
  258. gpointer ud);
  259. /**
  260. * Start watching on srv pipe
  261. */
  262. void rspamd_srv_start_watching(struct rspamd_main *srv,
  263. struct rspamd_worker *worker,
  264. struct ev_loop *ev_base);
  265. /**
  266. * Send command to srv pipe and read reply calling the specified callback at the
  267. * end
  268. */
  269. void rspamd_srv_send_command(struct rspamd_worker *worker,
  270. struct ev_loop *ev_base,
  271. struct rspamd_srv_command *cmd,
  272. int attached_fd,
  273. rspamd_srv_reply_handler handler,
  274. gpointer ud);
  275. /**
  276. * Broadcast srv cmd from rspamd_main to workers
  277. * @param rspamd_main
  278. * @param cmd
  279. * @param except_pid
  280. */
  281. void rspamd_control_broadcast_srv_cmd(struct rspamd_main *rspamd_main,
  282. struct rspamd_control_command *cmd,
  283. pid_t except_pid);
  284. /**
  285. * Returns command from a specified string (case insensitive)
  286. * @param str
  287. * @return
  288. */
  289. enum rspamd_control_type rspamd_control_command_from_string(const char *str);
  290. /**
  291. * Returns command name from it's type
  292. * @param cmd
  293. * @return
  294. */
  295. const char *rspamd_control_command_to_string(enum rspamd_control_type cmd);
  296. const char *rspamd_srv_command_to_string(enum rspamd_srv_type cmd);
  297. /**
  298. * Used to cleanup pending events
  299. * @param p
  300. */
  301. void rspamd_pending_control_free(gpointer p);
  302. G_END_DECLS
  303. #endif