diff --git a/rust/src/core.rs b/rust/src/core.rs index d127bb4a9f..efb891efdc 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -17,8 +17,7 @@ //! This module exposes items from the core "C" code to Rust. -use std; -use suricata_sys::sys::{AppProto, AppProtoEnum, SCLogLevel}; +use suricata_sys::sys::{AppProto, AppProtoEnum}; #[cfg(not(test))] use suricata_sys::sys::SCAppLayerParserTriggerRawStreamInspection; @@ -68,16 +67,6 @@ macro_rules!BIT_U64 { // // Function types for calls into C. // - -#[allow(non_snake_case)] -pub type SCLogMessageFunc = - extern "C" fn(level: SCLogLevel, - filename: *const std::os::raw::c_char, - line: std::os::raw::c_uint, - function: *const std::os::raw::c_char, - subsystem: *const std::os::raw::c_char, - message: *const std::os::raw::c_char) -> std::os::raw::c_int; - pub enum StreamingBufferConfig {} // Opaque flow type (defined in C) @@ -129,8 +118,6 @@ pub type SCFileContainerRecycle = extern "C" fn ( #[allow(non_snake_case)] #[repr(C)] pub struct SuricataContext { - pub SCLogMessage: SCLogMessageFunc, - pub HttpRangeFreeBlock: SCHttpRangeFreeBlock, pub HTPFileCloseHandleRange: SCHTPFileCloseHandleRange, diff --git a/rust/src/debug.rs b/rust/src/debug.rs index d31508bde3..d51453387c 100644 --- a/rust/src/debug.rs +++ b/rust/src/debug.rs @@ -19,9 +19,9 @@ use std::{ffi::CString, path::Path}; -use crate::core::SC; - use suricata_sys::sys::{SCFatalErrorOnInitStatic, SCLogLevel}; +#[cfg(not(test))] +use suricata_sys::sys::{SCError, SCLogMessage}; pub static mut LEVEL: SCLogLevel = SCLogLevel::SC_LOG_NOTSET; @@ -59,23 +59,28 @@ pub fn sclog(level: SCLogLevel, file: &str, line: u32, function: &str, message: /// SCLogMessage wrapper. If the Suricata C context is not registered /// a more basic log format will be used (for example, when running /// Rust unit tests). +#[cfg(not(test))] pub fn sc_log_message( level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str, message: &str, -) -> std::os::raw::c_int { +) -> SCError { unsafe { - if let Some(c) = SC { - return (c.SCLogMessage)( - level, - to_safe_cstring(filename).as_ptr(), - line, - to_safe_cstring(function).as_ptr(), - to_safe_cstring(module).as_ptr(), - to_safe_cstring(message).as_ptr(), - ); - } + return SCLogMessage( + level, + to_safe_cstring(filename).as_ptr(), + line, + to_safe_cstring(function).as_ptr(), + to_safe_cstring(module).as_ptr(), + to_safe_cstring(message).as_ptr(), + ); } +} +#[cfg(test)] +pub fn sc_log_message( + level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, _function: &str, _module: &str, + message: &str, +) -> i32 { // Fall back if the Suricata C context is not registered which is // the case when Rust unit tests are running. // diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index bd04c3a7d8..f9ee53bcb3 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -435,6 +435,17 @@ extern "C" { s: *mut Signature, alproto: AppProto, ) -> ::std::os::raw::c_int; } +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum SCError { + SC_OK = 0, + SC_ENOMEM = 1, + SC_EINVAL = 2, + SC_ELIMIT = 3, + SC_EEXIST = 4, + SC_ENOENT = 5, + SC_ERR_MAX = 6, +} #[repr(i32)] #[doc = " \\brief The various log levels\n NOTE: when adding new level, don't forget to update SCLogMapLogLevelToSyslogLevel()\n or it may result in logging to syslog with LOG_EMERG priority."] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -450,6 +461,13 @@ pub enum SCLogLevel { SC_LOG_DEBUG = 7, SC_LOG_LEVEL_MAX = 8, } +extern "C" { + pub fn SCLogMessage( + arg1: SCLogLevel, arg2: *const ::std::os::raw::c_char, arg3: ::std::os::raw::c_uint, + arg4: *const ::std::os::raw::c_char, arg5: *const ::std::os::raw::c_char, + message: *const ::std::os::raw::c_char, + ) -> SCError; +} extern "C" { pub fn SCFatalErrorOnInitStatic(arg1: *const ::std::os::raw::c_char); } diff --git a/src/rust-context.c b/src/rust-context.c index 475fc65e9f..84e0e6bcce 100644 --- a/src/rust-context.c +++ b/src/rust-context.c @@ -25,8 +25,6 @@ #include "util-var.h" const SuricataContext suricata_context = { - SCLogMessage, - HttpRangeFreeBlock, HTPFileCloseHandleRange, diff --git a/src/rust-context.h b/src/rust-context.h index dd8a35ea27..2ac4394e49 100644 --- a/src/rust-context.h +++ b/src/rust-context.h @@ -25,9 +25,7 @@ #include "app-layer-ike.h" //IKEState, IKETransaction #include "app-layer-tftp.h" //TFTPState, TFTPTransaction -#include "util-debug.h" #include "util-file.h" -#include "util-var.h" // hack for include orders cf SCSha256 typedef struct HttpRangeContainerBlock HttpRangeContainerBlock; @@ -35,9 +33,6 @@ typedef struct HttpRangeContainerBlock HttpRangeContainerBlock; struct AppLayerParser; typedef struct SuricataContext_ { - SCError (*SCLogMessage)(const SCLogLevel, const char *, const unsigned int, const char *, - const char *, const char *message); - void (*HttpRangeFreeBlock)(HttpRangeContainerBlock *); bool (*HTPFileCloseHandleRange)(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t, HttpRangeContainerBlock *, const uint8_t *, uint32_t); diff --git a/src/util-debug.h b/src/util-debug.h index 2279b85327..791388a302 100644 --- a/src/util-debug.h +++ b/src/util-debug.h @@ -24,6 +24,8 @@ #ifndef SURICATA_UTIL_DEBUG_H #define SURICATA_UTIL_DEBUG_H +#include "util-error.h" + /** * \brief The various log levels * NOTE: when adding new level, don't forget to update SCLogMapLogLevelToSyslogLevel() @@ -42,11 +44,13 @@ typedef enum { SC_LOG_LEVEL_MAX, } SCLogLevel; +SCError SCLogMessage(const SCLogLevel, const char *, const unsigned int, const char *, const char *, + const char *message); + #ifndef SURICATA_BINDGEN_H #include "suricata-common.h" #include "threads.h" -#include "util-error.h" #include "util-debug-filters.h" /** @@ -542,9 +546,6 @@ void SCLogInitLogModule(SCLogInitData *); void SCLogDeInitLogModule(void); -SCError SCLogMessage(const SCLogLevel, const char *, const unsigned int, const char *, const char *, - const char *message); - SCLogOPBuffer *SCLogAllocLogOPBuffer(void); int SCLogDebugEnabled(void); diff --git a/src/util-error.h b/src/util-error.h index 440f0ae8ee..81b482bf6d 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -23,7 +23,7 @@ #define SURICATA_UTIL_ERROR_H /* different error types */ -typedef enum { +typedef enum SCError { SC_OK, SC_ENOMEM, @@ -35,8 +35,10 @@ typedef enum { SC_ERR_MAX } SCError; +#ifndef SURICATA_BINDGEN_H #include "threads.h" extern thread_local SCError sc_errno; +#endif #endif /* SURICATA_UTIL_ERROR_H */