From: Philippe Antoine Date: Tue, 30 Nov 2021 08:19:20 +0000 (+0100) Subject: rust: export constants via cbindgen X-Git-Tag: suricata-7.0.0-beta1~1130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=87d9c44ec504f459578fdb551b16a1015b8239c7;p=thirdparty%2Fsuricata.git rust: export constants via cbindgen so that constants are not defined twice in Rust anc C So that we are sure they have the same value --- diff --git a/rust/cbindgen.toml b/rust/cbindgen.toml index 9e58721f36..2be76398b2 100644 --- a/rust/cbindgen.toml +++ b/rust/cbindgen.toml @@ -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. # diff --git a/rust/src/core.rs b/rust/src/core.rs index c47f936cbf..245cfd900f 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -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; diff --git a/rust/src/mime/mod.rs b/rust/src/mime/mod.rs index 045b542411..59799c34c4 100644 --- a/rust/src/mime/mod.rs +++ b/rust/src/mime/mod.rs @@ -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] diff --git a/src/detect-engine-payload.c b/src/detect-engine-payload.c index 57e09cd17e..bbe46addc7 100644 --- a/src/detect-engine-payload.c +++ b/src/detect-engine-payload.c @@ -25,6 +25,7 @@ #include "suricata-common.h" #include "suricata.h" +#include "rust.h" #include "decode.h" diff --git a/src/stream-tcp-list.c b/src/stream-tcp-list.c index 1ac580f33f..47fde89cb5 100644 --- a/src/stream-tcp-list.c +++ b/src/stream-tcp-list.c @@ -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" diff --git a/src/stream.h b/src/stream.h index 3ba96ede35..78fbfcf1fe 100644 --- a/src/stream.h +++ b/src/stream.h @@ -26,15 +26,6 @@ #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); diff --git a/src/suricata-common.h b/src/suricata-common.h index 85303f9fe0..6af3a0b0ce 100644 --- a/src/suricata-common.h +++ b/src/suricata-common.h @@ -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__ */ diff --git a/src/util-decode-mime.c b/src/util-decode-mime.c index b3598658d7..437335d903 100644 --- a/src/util-decode-mime.c +++ b/src/util-decode-mime.c @@ -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); diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index c7ef24ff96..58bccd5335 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -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;