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.

protocol.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  17. * @file protocol.h
  18. * Rspamd protocol definition
  19. */
  20. #ifndef RSPAMD_PROTOCOL_H
  21. #define RSPAMD_PROTOCOL_H
  22. #include "config.h"
  23. #include "scan_result.h"
  24. #include "libserver/http/http_connection.h"
  25. #include "task.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define RSPAMD_BASE_ERROR 500
  30. #define RSPAMD_FILTER_ERROR RSPAMD_BASE_ERROR + 1
  31. #define RSPAMD_NETWORK_ERROR RSPAMD_BASE_ERROR + 2
  32. #define RSPAMD_PROTOCOL_ERROR RSPAMD_BASE_ERROR + 3
  33. #define RSPAMD_LENGTH_ERROR RSPAMD_BASE_ERROR + 4
  34. #define RSPAMD_STATFILE_ERROR RSPAMD_BASE_ERROR + 5
  35. struct rspamd_protocol_log_symbol_result {
  36. uint32_t id;
  37. float score;
  38. };
  39. struct rspamd_protocol_log_message_sum {
  40. uint32_t nresults;
  41. uint32_t nextra;
  42. uint32_t settings_id;
  43. double score;
  44. double required_score;
  45. struct rspamd_protocol_log_symbol_result results[];
  46. };
  47. struct rspamd_metric;
  48. /**
  49. * Process headers into HTTP message and set appropriate task fields
  50. * @param task
  51. * @param msg
  52. * @return
  53. */
  54. gboolean rspamd_protocol_handle_headers(struct rspamd_task *task,
  55. struct rspamd_http_message *msg);
  56. /**
  57. * Process control chunk and update task structure accordingly
  58. * @param task
  59. * @param control
  60. * @return
  61. */
  62. gboolean rspamd_protocol_handle_control(struct rspamd_task *task,
  63. const ucl_object_t *control);
  64. /**
  65. * Process HTTP request to the task structure
  66. * @param task
  67. * @param msg
  68. * @return
  69. */
  70. gboolean rspamd_protocol_handle_request(struct rspamd_task *task,
  71. struct rspamd_http_message *msg);
  72. /**
  73. * Write task results to http message
  74. * @param msg
  75. * @param task
  76. */
  77. void rspamd_protocol_http_reply(struct rspamd_http_message *msg,
  78. struct rspamd_task *task, ucl_object_t **pobj,
  79. int how);
  80. /**
  81. * Write data to log pipes
  82. * @param task
  83. */
  84. void rspamd_protocol_write_log_pipe(struct rspamd_task *task);
  85. enum rspamd_protocol_flags {
  86. RSPAMD_PROTOCOL_BASIC = 1 << 0,
  87. RSPAMD_PROTOCOL_METRICS = 1 << 1,
  88. RSPAMD_PROTOCOL_MESSAGES = 1 << 2,
  89. RSPAMD_PROTOCOL_RMILTER = 1 << 3,
  90. RSPAMD_PROTOCOL_DKIM = 1 << 4,
  91. RSPAMD_PROTOCOL_URLS = 1 << 5,
  92. RSPAMD_PROTOCOL_EXTRA = 1 << 6,
  93. };
  94. #define RSPAMD_PROTOCOL_DEFAULT (RSPAMD_PROTOCOL_BASIC | \
  95. RSPAMD_PROTOCOL_METRICS | \
  96. RSPAMD_PROTOCOL_MESSAGES | \
  97. RSPAMD_PROTOCOL_RMILTER | \
  98. RSPAMD_PROTOCOL_DKIM | \
  99. RSPAMD_PROTOCOL_EXTRA)
  100. /**
  101. * Write reply to ucl object filling log buffer
  102. * @param task
  103. * @param logbuf
  104. * @return
  105. */
  106. ucl_object_t *rspamd_protocol_write_ucl(struct rspamd_task *task,
  107. enum rspamd_protocol_flags flags);
  108. /**
  109. * Write reply for specified task command
  110. * @param task task object
  111. * @return 0 if we wrote reply and -1 if there was some error
  112. */
  113. void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout);
  114. /**
  115. * Convert rspamd output to legacy protocol reply
  116. * @param task
  117. * @param top
  118. * @param out
  119. */
  120. void rspamd_ucl_torspamc_output(const ucl_object_t *top,
  121. rspamd_fstring_t **out);
  122. void rspamd_ucl_tospamc_output(const ucl_object_t *top,
  123. rspamd_fstring_t **out);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif