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.

fuzzy_wire.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_FUZZY_STORAGE_H
  17. #define RSPAMD_FUZZY_STORAGE_H
  18. #include "config.h"
  19. #include "rspamd.h"
  20. #include "shingles.h"
  21. #include "cryptobox.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define RSPAMD_FUZZY_VERSION 4
  26. #define RSPAMD_FUZZY_KEYLEN 8
  27. #define RSPAMD_FUZZY_FLAG_WEAK (1u << 7u)
  28. /* Use lower 4 bits for the version */
  29. #define RSPAMD_FUZZY_VERSION_MASK 0x0fu
  30. /* Commands for fuzzy storage */
  31. #define FUZZY_CHECK 0
  32. #define FUZZY_WRITE 1
  33. #define FUZZY_DEL 2
  34. #define FUZZY_STAT 3
  35. #define FUZZY_PING 4
  36. #define FUZZY_CLIENT_MAX 4
  37. /* Internal commands */
  38. #define FUZZY_REFRESH 100 /* Update expire */
  39. #define FUZZY_DUP 101 /* Skip duplicate in update queue */
  40. /**
  41. * The epoch of the fuzzy client
  42. */
  43. enum rspamd_fuzzy_epoch {
  44. RSPAMD_FUZZY_EPOCH10, /**< 1.0+ encryption */
  45. RSPAMD_FUZZY_EPOCH11, /**< 1.7+ extended reply */
  46. RSPAMD_FUZZY_EPOCH_MAX
  47. };
  48. RSPAMD_PACKED(rspamd_fuzzy_cmd)
  49. {
  50. guint8 version;
  51. guint8 cmd;
  52. guint8 shingles_count;
  53. guint8 flag;
  54. int32_t value;
  55. uint32_t tag;
  56. gchar digest[rspamd_cryptobox_HASHBYTES];
  57. };
  58. RSPAMD_PACKED(rspamd_fuzzy_shingle_cmd)
  59. {
  60. struct rspamd_fuzzy_cmd basic;
  61. struct rspamd_shingle sgl;
  62. };
  63. RSPAMD_PACKED(rspamd_fuzzy_reply_v1)
  64. {
  65. int32_t value;
  66. uint32_t flag;
  67. uint32_t tag;
  68. float prob;
  69. };
  70. RSPAMD_PACKED(rspamd_fuzzy_reply)
  71. {
  72. struct rspamd_fuzzy_reply_v1 v1;
  73. gchar digest[rspamd_cryptobox_HASHBYTES];
  74. uint32_t ts;
  75. guchar reserved[12];
  76. };
  77. RSPAMD_PACKED(rspamd_fuzzy_encrypted_req_hdr)
  78. {
  79. guchar magic[4];
  80. guchar key_id[RSPAMD_FUZZY_KEYLEN];
  81. guchar pubkey[32];
  82. guchar nonce[rspamd_cryptobox_MAX_NONCEBYTES];
  83. guchar mac[rspamd_cryptobox_MAX_MACBYTES];
  84. };
  85. RSPAMD_PACKED(rspamd_fuzzy_encrypted_cmd)
  86. {
  87. struct rspamd_fuzzy_encrypted_req_hdr hdr;
  88. struct rspamd_fuzzy_cmd cmd;
  89. };
  90. RSPAMD_PACKED(rspamd_fuzzy_encrypted_shingle_cmd)
  91. {
  92. struct rspamd_fuzzy_encrypted_req_hdr hdr;
  93. struct rspamd_fuzzy_shingle_cmd cmd;
  94. };
  95. RSPAMD_PACKED(rspamd_fuzzy_encrypted_rep_hdr)
  96. {
  97. guchar nonce[rspamd_cryptobox_MAX_NONCEBYTES];
  98. guchar mac[rspamd_cryptobox_MAX_MACBYTES];
  99. };
  100. RSPAMD_PACKED(rspamd_fuzzy_encrypted_reply)
  101. {
  102. struct rspamd_fuzzy_encrypted_rep_hdr hdr;
  103. struct rspamd_fuzzy_reply rep;
  104. };
  105. static const guchar fuzzy_encrypted_magic[4] = {'r', 's', 'f', 'e'};
  106. enum rspamd_fuzzy_extension_type {
  107. RSPAMD_FUZZY_EXT_SOURCE_DOMAIN = 'd',
  108. RSPAMD_FUZZY_EXT_SOURCE_IP4 = '4',
  109. RSPAMD_FUZZY_EXT_SOURCE_IP6 = '6',
  110. };
  111. struct rspamd_fuzzy_cmd_extension {
  112. enum rspamd_fuzzy_extension_type ext;
  113. guint length;
  114. struct rspamd_fuzzy_cmd_extension *next;
  115. guchar *payload;
  116. };
  117. struct rspamd_fuzzy_stat_entry {
  118. const gchar *name;
  119. uint64_t fuzzy_cnt;
  120. };
  121. RSPAMD_PACKED(fuzzy_peer_cmd)
  122. {
  123. int32_t is_shingle;
  124. union {
  125. struct rspamd_fuzzy_cmd normal;
  126. struct rspamd_fuzzy_shingle_cmd shingle;
  127. } cmd;
  128. };
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif