rust: bindgen SCLogMessage

Ticket: 7667
pull/13884/head
Philippe Antoine 5 months ago committed by Victor Julien
parent cc845dccdb
commit 828a6bdbf8

@ -17,8 +17,7 @@
//! This module exposes items from the core "C" code to Rust. //! This module exposes items from the core "C" code to Rust.
use std; use suricata_sys::sys::{AppProto, AppProtoEnum};
use suricata_sys::sys::{AppProto, AppProtoEnum, SCLogLevel};
#[cfg(not(test))] #[cfg(not(test))]
use suricata_sys::sys::SCAppLayerParserTriggerRawStreamInspection; use suricata_sys::sys::SCAppLayerParserTriggerRawStreamInspection;
@ -68,16 +67,6 @@ macro_rules!BIT_U64 {
// //
// Function types for calls into C. // 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 {} pub enum StreamingBufferConfig {}
// Opaque flow type (defined in C) // Opaque flow type (defined in C)
@ -129,8 +118,6 @@ pub type SCFileContainerRecycle = extern "C" fn (
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[repr(C)] #[repr(C)]
pub struct SuricataContext { pub struct SuricataContext {
pub SCLogMessage: SCLogMessageFunc,
pub HttpRangeFreeBlock: SCHttpRangeFreeBlock, pub HttpRangeFreeBlock: SCHttpRangeFreeBlock,
pub HTPFileCloseHandleRange: SCHTPFileCloseHandleRange, pub HTPFileCloseHandleRange: SCHTPFileCloseHandleRange,

@ -19,9 +19,9 @@
use std::{ffi::CString, path::Path}; use std::{ffi::CString, path::Path};
use crate::core::SC;
use suricata_sys::sys::{SCFatalErrorOnInitStatic, SCLogLevel}; use suricata_sys::sys::{SCFatalErrorOnInitStatic, SCLogLevel};
#[cfg(not(test))]
use suricata_sys::sys::{SCError, SCLogMessage};
pub static mut LEVEL: SCLogLevel = SCLogLevel::SC_LOG_NOTSET; 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 /// SCLogMessage wrapper. If the Suricata C context is not registered
/// a more basic log format will be used (for example, when running /// a more basic log format will be used (for example, when running
/// Rust unit tests). /// Rust unit tests).
#[cfg(not(test))]
pub fn sc_log_message( pub fn sc_log_message(
level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str, level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str,
message: &str, message: &str,
) -> std::os::raw::c_int { ) -> SCError {
unsafe { unsafe {
if let Some(c) = SC { return SCLogMessage(
return (c.SCLogMessage)( level,
level, to_safe_cstring(filename).as_ptr(),
to_safe_cstring(filename).as_ptr(), line,
line, to_safe_cstring(function).as_ptr(),
to_safe_cstring(function).as_ptr(), to_safe_cstring(module).as_ptr(),
to_safe_cstring(module).as_ptr(), to_safe_cstring(message).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 // Fall back if the Suricata C context is not registered which is
// the case when Rust unit tests are running. // the case when Rust unit tests are running.
// //

@ -435,6 +435,17 @@ extern "C" {
s: *mut Signature, alproto: AppProto, s: *mut Signature, alproto: AppProto,
) -> ::std::os::raw::c_int; ) -> ::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)] #[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."] #[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)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
@ -450,6 +461,13 @@ pub enum SCLogLevel {
SC_LOG_DEBUG = 7, SC_LOG_DEBUG = 7,
SC_LOG_LEVEL_MAX = 8, 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" { extern "C" {
pub fn SCFatalErrorOnInitStatic(arg1: *const ::std::os::raw::c_char); pub fn SCFatalErrorOnInitStatic(arg1: *const ::std::os::raw::c_char);
} }

@ -25,8 +25,6 @@
#include "util-var.h" #include "util-var.h"
const SuricataContext suricata_context = { const SuricataContext suricata_context = {
SCLogMessage,
HttpRangeFreeBlock, HttpRangeFreeBlock,
HTPFileCloseHandleRange, HTPFileCloseHandleRange,

@ -25,9 +25,7 @@
#include "app-layer-ike.h" //IKEState, IKETransaction #include "app-layer-ike.h" //IKEState, IKETransaction
#include "app-layer-tftp.h" //TFTPState, TFTPTransaction #include "app-layer-tftp.h" //TFTPState, TFTPTransaction
#include "util-debug.h"
#include "util-file.h" #include "util-file.h"
#include "util-var.h"
// hack for include orders cf SCSha256 // hack for include orders cf SCSha256
typedef struct HttpRangeContainerBlock HttpRangeContainerBlock; typedef struct HttpRangeContainerBlock HttpRangeContainerBlock;
@ -35,9 +33,6 @@ typedef struct HttpRangeContainerBlock HttpRangeContainerBlock;
struct AppLayerParser; struct AppLayerParser;
typedef struct SuricataContext_ { typedef struct SuricataContext_ {
SCError (*SCLogMessage)(const SCLogLevel, const char *, const unsigned int, const char *,
const char *, const char *message);
void (*HttpRangeFreeBlock)(HttpRangeContainerBlock *); void (*HttpRangeFreeBlock)(HttpRangeContainerBlock *);
bool (*HTPFileCloseHandleRange)(const StreamingBufferConfig *sbcfg, FileContainer *, bool (*HTPFileCloseHandleRange)(const StreamingBufferConfig *sbcfg, FileContainer *,
const uint16_t, HttpRangeContainerBlock *, const uint8_t *, uint32_t); const uint16_t, HttpRangeContainerBlock *, const uint8_t *, uint32_t);

@ -24,6 +24,8 @@
#ifndef SURICATA_UTIL_DEBUG_H #ifndef SURICATA_UTIL_DEBUG_H
#define SURICATA_UTIL_DEBUG_H #define SURICATA_UTIL_DEBUG_H
#include "util-error.h"
/** /**
* \brief The various log levels * \brief The various log levels
* NOTE: when adding new level, don't forget to update SCLogMapLogLevelToSyslogLevel() * NOTE: when adding new level, don't forget to update SCLogMapLogLevelToSyslogLevel()
@ -42,11 +44,13 @@ typedef enum {
SC_LOG_LEVEL_MAX, SC_LOG_LEVEL_MAX,
} SCLogLevel; } SCLogLevel;
SCError SCLogMessage(const SCLogLevel, const char *, const unsigned int, const char *, const char *,
const char *message);
#ifndef SURICATA_BINDGEN_H #ifndef SURICATA_BINDGEN_H
#include "suricata-common.h" #include "suricata-common.h"
#include "threads.h" #include "threads.h"
#include "util-error.h"
#include "util-debug-filters.h" #include "util-debug-filters.h"
/** /**
@ -542,9 +546,6 @@ void SCLogInitLogModule(SCLogInitData *);
void SCLogDeInitLogModule(void); void SCLogDeInitLogModule(void);
SCError SCLogMessage(const SCLogLevel, const char *, const unsigned int, const char *, const char *,
const char *message);
SCLogOPBuffer *SCLogAllocLogOPBuffer(void); SCLogOPBuffer *SCLogAllocLogOPBuffer(void);
int SCLogDebugEnabled(void); int SCLogDebugEnabled(void);

@ -23,7 +23,7 @@
#define SURICATA_UTIL_ERROR_H #define SURICATA_UTIL_ERROR_H
/* different error types */ /* different error types */
typedef enum { typedef enum SCError {
SC_OK, SC_OK,
SC_ENOMEM, SC_ENOMEM,
@ -35,8 +35,10 @@ typedef enum {
SC_ERR_MAX SC_ERR_MAX
} SCError; } SCError;
#ifndef SURICATA_BINDGEN_H
#include "threads.h" #include "threads.h"
extern thread_local SCError sc_errno; extern thread_local SCError sc_errno;
#endif
#endif /* SURICATA_UTIL_ERROR_H */ #endif /* SURICATA_UTIL_ERROR_H */

Loading…
Cancel
Save