]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: export constants via cbindgen
authorPhilippe Antoine <contact@catenacyber.fr>
Tue, 30 Nov 2021 08:19:20 +0000 (09:19 +0100)
committerVictor Julien <vjulien@oisf.net>
Tue, 7 Dec 2021 06:56:36 +0000 (07:56 +0100)
so that constants are not defined twice in Rust anc C
So that we are sure they have the same value

rust/cbindgen.toml
rust/src/core.rs
rust/src/mime/mod.rs
src/detect-engine-payload.c
src/stream-tcp-list.c
src/stream.h
src/suricata-common.h
src/util-decode-mime.c
src/util-logopenfile.c

index 9e58721f369a847d3c29dd8a19e3b9fc919cde24..2be76398b264f69dc92de4904218f9d46a05d387 100644 (file)
@@ -104,6 +104,8 @@ exclude = [
     "TFTPState",
     "TFTPTransaction",
     "free",
+    "IPPROTO_TCP",
+    "IPPROTO_UDP",
 ]
 
 # Types of items that we'll generate. If empty, then all types of item are emitted.
@@ -119,7 +121,7 @@ exclude = [
 # * "functions":
 #
 # default: []
-item_types = ["enums","structs","opaque","functions"]
+item_types = ["enums","structs","opaque","functions","constants"]
 
 # Whether applying rules in export.rename prevents export.prefix from applying.
 #
index c47f936cbf7116564af4b801d4efc6be01136157..245cfd900f0547768e642d5324bde9f14ff97b3b 100644 (file)
@@ -31,12 +31,14 @@ pub type AppLayerEventType = std::os::raw::c_int;
 pub const APP_LAYER_EVENT_TYPE_TRANSACTION : i32 = 1;
 pub const APP_LAYER_EVENT_TYPE_PACKET      : i32 = 2;
 
-// From stream.h.
 pub const STREAM_START:    u8 = 0x01;
 pub const STREAM_EOF:      u8 = 0x02;
+pub const STREAM_TOSERVER: u8 = 0x04;
+pub const STREAM_TOCLIENT: u8 = 0x08;
 pub const STREAM_GAP:      u8 = 0x10;
 pub const STREAM_DEPTH:    u8 = 0x20;
 pub const STREAM_MIDSTREAM:u8 = 0x40;
+pub const STREAM_FLUSH:    u8 = 0x80;
 pub const DIR_BOTH:        u8 = 0b0000_1100;
 const DIR_TOSERVER:        u8 = 0b0000_0100;
 const DIR_TOCLIENT:        u8 = 0b0000_1000;
index 045b542411e8f56b513409dde97dda6c34f65ee6..59799c34c4bc1e00d32da9c441933ee90e47d371 100644 (file)
@@ -125,9 +125,7 @@ fn mime_find_header_token<'a>(
     }
 }
 
-// TODO ? export with "constants" in cbindgen
-// and use in outbuf definition for rs_mime_find_header_token
-// but other constants are now defined twice in rust and in C
+// used on the C side
 pub const RS_MIME_MAX_TOKEN_LEN: usize = 255;
 
 #[no_mangle]
index 57e09cd17e842e4d5b055dc15915a8a25689fae4..bbe46addc7fe271a119a2fe553086d30d91fa4d3 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "suricata-common.h"
 #include "suricata.h"
+#include "rust.h"
 
 #include "decode.h"
 
index 1ac580f33f440df03d9e5c050b8bb00c8fb9f5b7..47fde89cb5286d354a68e0d7e22efe1580f2f173 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include "suricata-common.h"
+#include "rust.h"
 #include "stream-tcp-private.h"
 #include "stream-tcp.h"
 #include "stream-tcp-reassemble.h"
index 3ba96ede35ba694cfd787b61c2fd48c4f987f65b..78fbfcf1fec535f6be2975cec872f2c61c578359 100644 (file)
 
 #include "flow.h"
 
-#define STREAM_START        BIT_U8(0)
-#define STREAM_EOF          BIT_U8(1)
-#define STREAM_TOSERVER     BIT_U8(2)
-#define STREAM_TOCLIENT     BIT_U8(3)
-#define STREAM_GAP          BIT_U8(4)   /**< data gap encountered */
-#define STREAM_DEPTH        BIT_U8(5)   /**< depth reached */
-#define STREAM_MIDSTREAM    BIT_U8(6)
-#define STREAM_FLUSH        BIT_U8(7)
-
 #define STREAM_FLAGS_FOR_PACKET(p) PKT_IS_TOSERVER((p)) ? STREAM_TOSERVER : STREAM_TOCLIENT
 
 typedef int (*StreamSegmentCallback)(const Packet *, void *, const uint8_t *, uint32_t);
index 85303f9fe068251f62ac9524800c54f0915c6bac..6af3a0b0ce252dce5d47c80755933c6c4da67057 100644 (file)
@@ -530,9 +530,5 @@ extern int g_ut_covered;
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
 
-#ifndef NAME_MAX
-#define NAME_MAX 255
-#endif
-
 #endif /* __SURICATA_COMMON_H__ */
 
index b3598658d7107f5f36a235133280c4c26b257e8b..437335d9031f08af8a363b78fb8c065654abd275 100644 (file)
@@ -1842,7 +1842,7 @@ static int ProcessMimeHeaders(const uint8_t *buf, uint32_t len,
     uint8_t *rptr = NULL;
     uint32_t blen = 0;
     MimeDecEntity *entity = (MimeDecEntity *) state->stack->top->data;
-    uint8_t bptr[NAME_MAX];
+    uint8_t bptr[RS_MIME_MAX_TOKEN_LEN];
 
     /* Look for mime header in current line */
     ret = FindMimeHeader(buf, len, state);
@@ -1871,14 +1871,13 @@ static int ProcessMimeHeaders(const uint8_t *buf, uint32_t len,
         field = MimeDecFindField(entity, CTNT_DISP_STR);
         if (field != NULL) {
             bool truncated_name = false;
-            // NAME_MAX is RS_MIME_MAX_TOKEN_LEN on the rust side
             if (rs_mime_find_header_token(field->value, field->value_len,
                         (const uint8_t *)"filename", strlen("filename"), &bptr, &blen)) {
                 SCLogDebug("File attachment found in disposition");
                 entity->ctnt_flags |= CTNT_IS_ATTACHMENT;
 
-                if (blen > NAME_MAX) {
-                    blen = NAME_MAX;
+                if (blen > RS_MIME_MAX_TOKEN_LEN) {
+                    blen = RS_MIME_MAX_TOKEN_LEN;
                     truncated_name = true;
                 }
 
@@ -1902,7 +1901,7 @@ static int ProcessMimeHeaders(const uint8_t *buf, uint32_t len,
         field = MimeDecFindField(entity, CTNT_TYPE_STR);
         if (field != NULL) {
             /* Check if child entity boundary definition found */
-            // NAME_MAX is RS_MIME_MAX_TOKEN_LEN on the rust side
+            // RS_MIME_MAX_TOKEN_LEN is RS_MIME_MAX_TOKEN_LEN on the rust side
             if (rs_mime_find_header_token(field->value, field->value_len,
                         (const uint8_t *)"boundary", strlen("boundary"), &bptr, &blen)) {
                 state->found_child = 1;
@@ -1926,14 +1925,13 @@ static int ProcessMimeHeaders(const uint8_t *buf, uint32_t len,
             /* Look for file name (if not already found) */
             if (!(entity->ctnt_flags & CTNT_IS_ATTACHMENT)) {
                 bool truncated_name = false;
-                // NAME_MAX is RS_MIME_MAX_TOKEN_LEN on the rust side
                 if (rs_mime_find_header_token(field->value, field->value_len,
                             (const uint8_t *)"name", strlen("name"), &bptr, &blen)) {
                     SCLogDebug("File attachment found");
                     entity->ctnt_flags |= CTNT_IS_ATTACHMENT;
 
-                    if (blen > NAME_MAX) {
-                        blen = NAME_MAX;
+                    if (blen > RS_MIME_MAX_TOKEN_LEN) {
+                        blen = RS_MIME_MAX_TOKEN_LEN;
                         truncated_name = true;
                     }
 
@@ -3014,7 +3012,7 @@ static int MimeDecParseLongFilename01(void)
     FAIL_IF_NOT(msg);
 
     FAIL_IF_NOT(msg->anomaly_flags & ANOM_LONG_FILENAME);
-    FAIL_IF_NOT(msg->filename_len == NAME_MAX);
+    FAIL_IF_NOT(msg->filename_len == RS_MIME_MAX_TOKEN_LEN);
 
     MimeDecFreeEntity(msg);
 
index c7ef24ff96ef90eb7d7e72a7474ec699f163ca2b..58bccd533588d7b5dac3cfdf786ad8aa7c500400 100644 (file)
@@ -41,6 +41,8 @@
 #include "util-log-redis.h"
 #endif /* HAVE_LIBHIREDIS */
 
+#define LOGFILE_NAME_MAX 255
+
 static bool LogFileNewThreadedCtx(LogFileCtx *parent_ctx, const char *log_path, const char *append, int i);
 
 // Threaded eve.json identifier
@@ -765,7 +767,7 @@ static bool LogFileNewThreadedCtx(LogFileCtx *parent_ctx, const char *log_path,
 
     *thread = *parent_ctx;
     if (parent_ctx->type == LOGFILE_TYPE_FILE) {
-        char fname[NAME_MAX];
+        char fname[LOGFILE_NAME_MAX];
         if (!LogFileThreadedName(log_path, fname, sizeof(fname), SC_ATOMIC_ADD(eve_file_id, 1))) {
             SCLogError(SC_ERR_MEM_ALLOC, "Unable to create threaded filename for log");
             goto error;