Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindgen: use bindgen to provide Rust bindings to C - v7 #12461

Closed
wants to merge 12 commits into from
Closed
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,9 @@ fi
fi
fi

AC_PATH_PROG([BINDGEN], [bindgen], [no])
AM_CONDITIONAL([HAVE_BINDGEN], [test "x$BINDGEN" != "xno"])

AC_PATH_PROG(CBINDGEN, cbindgen, "no")
if test "x$CBINDGEN" != "xno"; then
cbindgen_version=$(cbindgen --version 2>&1 | cut -d' ' -f2-)
Expand Down
31 changes: 28 additions & 3 deletions rust/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ if HAVE_CYGPATH
CARGO_ENV = @rustup_home@ \
CARGO_HOME="$(CARGO_HOME)" \
CARGO_TARGET_DIR="$(e_rustdir)/target" \
SURICATA_LUA_SYS_HEADER_DST="$(e_rustdir)/gen"
SURICATA_LUA_SYS_HEADER_DST="$(e_rustdir)/gen" \
TOP_BUILDDIR=$(abs_top_builddir) \
TOP_SRCDIR=$(abs_top_srcdir) \
RUST_GENDIR=$(e_rustdir)/gen
else
CARGO_ENV = @rustup_home@ \
CARGO_HOME="$(CARGO_HOME)" \
CARGO_TARGET_DIR="$(abs_top_builddir)/rust/target" \
SURICATA_LUA_SYS_HEADER_DST="$(abs_top_builddir)/rust/gen"
SURICATA_LUA_SYS_HEADER_DST="$(abs_top_builddir)/rust/gen" \
TOP_BUILDDIR=$(abs_top_builddir) \
TOP_SRCDIR=$(abs_top_srcdir) \
RUST_GENDIR=$(abs_top_builddir)/rust/gen
endif

all-local: Cargo.toml
Expand Down Expand Up @@ -83,6 +89,22 @@ check:
vendor:
$(CARGO_ENV) $(CARGO) vendor

update-bindings:
if HAVE_BINDGEN
$(BINDGEN) \
-o src/_sys.rs \
--disable-header-comment \
--allowlist-type 'SCAppLayerEventType' \
--rustified-enum 'SCAppLayerEventType' \
--allowlist-type 'SCAppLayerStateGetEventInfoByIdFn' \
$(abs_top_srcdir)/src/bindgen.h \
-- \
-DHAVE_CONFIG_H -I../src $(CPPFLAGS)
else
@echo "error: bindgen not installed, can't update bindings"
exit 1
endif

if HAVE_CBINDGEN
gen/rust-bindings.h: $(RUST_SURICATA_LIB)
cd $(abs_top_srcdir)/rust && \
Expand All @@ -93,7 +115,10 @@ gen/rust-bindings.h:
endif

doc:
CARGO_HOME=$(CARGO_HOME) $(CARGO) doc --all-features --no-deps
CARGO_HOME=$(CARGO_HOME) \
$(CARGO_ENV) \
SURICATA_LUA_SYS_HEADER_DST="" $(CARGO) doc \
--all-features --no-deps

if HAVE_CBINDGEN
dist/rust-bindings.h:
Expand Down
4 changes: 2 additions & 2 deletions rust/derive/src/applayerevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ pub fn derive_app_layer_event(input: TokenStream) -> TokenStream {
unsafe extern "C" fn get_event_info(
event_name: *const std::os::raw::c_char,
event_id: *mut u8,
event_type: *mut #crate_id::core::AppLayerEventType,
event_type: *mut #crate_id::sys::SCAppLayerEventType,
) -> std::os::raw::c_int {
#crate_id::applayer::get_event_info::<#name>(event_name, event_id, event_type)
}

unsafe extern "C" fn get_event_info_by_id(
event_id: u8,
event_name: *mut *const std::os::raw::c_char,
event_type: *mut #crate_id::core::AppLayerEventType,
event_type: *mut #crate_id::sys::SCAppLayerEventType,
) -> std::os::raw::c_int {
#crate_id::applayer::get_event_info_by_id::<#name>(event_id, event_name, event_type)
}
Expand Down
13 changes: 13 additions & 0 deletions rust/src/_sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum SCAppLayerEventType {
APP_LAYER_EVENT_TYPE_TRANSACTION = 1,
APP_LAYER_EVENT_TYPE_PACKET = 2,
}
pub type SCAppLayerStateGetEventInfoByIdFn = ::std::option::Option<
unsafe extern "C" fn(
event_id: u8,
event_name: *mut *const ::std::os::raw::c_char,
event_type: *mut SCAppLayerEventType,
) -> ::std::os::raw::c_int,
>;
21 changes: 11 additions & 10 deletions rust/src/applayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
//! Parser registration functions and common interface module.

use std;
use crate::core::{self,DetectEngineState,AppLayerEventType,AppProto};
use crate::core::{self,DetectEngineState,AppProto};
use crate::direction::Direction;
use crate::filecontainer::FileContainer;
use crate::flow::Flow;
use crate::sys::SCAppLayerEventType;
use std::os::raw::{c_void,c_char,c_int};
use crate::core::SC;
use std::ffi::CStr;
Expand Down Expand Up @@ -387,7 +388,7 @@ pub struct RustParser {
/// Function to get an event id from a description
pub get_eventinfo: Option<GetEventInfoFn>,
/// Function to get an event description from an event id
pub get_eventinfo_byid: Option<GetEventInfoByIdFn>,
pub get_eventinfo_byid: crate::sys::SCAppLayerStateGetEventInfoByIdFn,

/// Function to allocate local storage
pub localstorage_new: Option<LocalStorageNewFn>,
Expand Down Expand Up @@ -457,8 +458,8 @@ pub type StateTxFreeFn = unsafe extern "C" fn (*mut c_void, u64);
pub type StateGetTxFn = unsafe extern "C" fn (*mut c_void, u64) -> *mut c_void;
pub type StateGetTxCntFn = unsafe extern "C" fn (*mut c_void) -> u64;
pub type StateGetProgressFn = unsafe extern "C" fn (*mut c_void, u8) -> c_int;
pub type GetEventInfoFn = unsafe extern "C" fn (*const c_char, event_id: *mut u8, *mut AppLayerEventType) -> c_int;
pub type GetEventInfoByIdFn = unsafe extern "C" fn (event_id: u8, *mut *const c_char, *mut AppLayerEventType) -> c_int;
pub type GetEventInfoFn = unsafe extern "C" fn (*const c_char, event_id: *mut u8, *mut SCAppLayerEventType) -> c_int;
pub type GetEventInfoByIdFn = unsafe extern "C" fn (event_id: u8, *mut *const c_char, *mut SCAppLayerEventType) -> c_int;
pub type LocalStorageNewFn = extern "C" fn () -> *mut c_void;
pub type LocalStorageFreeFn = extern "C" fn (*mut c_void);
pub type GetTxFilesFn = unsafe extern "C" fn (*mut c_void, u8) -> AppLayerGetFileState;
Expand Down Expand Up @@ -596,13 +597,13 @@ pub trait AppLayerEvent {
unsafe extern "C" fn get_event_info(
event_name: *const std::os::raw::c_char,
event_id: *mut u8,
event_type: *mut core::AppLayerEventType,
event_type: *mut SCAppLayerEventType,
) -> std::os::raw::c_int;

unsafe extern "C" fn get_event_info_by_id(
event_id: u8,
event_name: *mut *const std::os::raw::c_char,
event_type: *mut core::AppLayerEventType,
event_type: *mut SCAppLayerEventType,
) -> std::os::raw::c_int;
}

Expand All @@ -625,7 +626,7 @@ pub trait AppLayerEvent {
pub unsafe fn get_event_info<T: AppLayerEvent>(
event_name: *const std::os::raw::c_char,
event_id: *mut u8,
event_type: *mut core::AppLayerEventType,
event_type: *mut SCAppLayerEventType,
) -> std::os::raw::c_int {
if event_name.is_null() {
return -1;
Expand All @@ -637,7 +638,7 @@ pub unsafe fn get_event_info<T: AppLayerEvent>(
return -1;
}
};
*event_type = core::AppLayerEventType::APP_LAYER_EVENT_TYPE_TRANSACTION;
*event_type = SCAppLayerEventType::APP_LAYER_EVENT_TYPE_TRANSACTION;
*event_id = event;
return 0;
}
Expand All @@ -648,11 +649,11 @@ pub unsafe fn get_event_info<T: AppLayerEvent>(
pub unsafe fn get_event_info_by_id<T: AppLayerEvent>(
event_id: u8,
event_name: *mut *const std::os::raw::c_char,
event_type: *mut core::AppLayerEventType,
event_type: *mut SCAppLayerEventType,
) -> std::os::raw::c_int {
if let Some(e) = T::from_id(event_id) {
*event_name = e.to_cstring().as_ptr() as *const std::os::raw::c_char;
*event_type = core::AppLayerEventType::APP_LAYER_EVENT_TYPE_TRANSACTION;
*event_type = SCAppLayerEventType::APP_LAYER_EVENT_TYPE_TRANSACTION;
return 0;
}
return -1;
Expand Down
8 changes: 0 additions & 8 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ use crate::flow::Flow;
pub enum DetectEngineState {}
pub enum AppLayerDecoderEvents {}

#[repr(C)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum AppLayerEventType {
APP_LAYER_EVENT_TYPE_TRANSACTION = 1,
APP_LAYER_EVENT_TYPE_PACKET = 2,
}

pub const STREAM_START: u8 = 0x01;
pub const STREAM_EOF: u8 = 0x02;
pub const STREAM_TOSERVER: u8 = 0x04;
Expand Down
6 changes: 3 additions & 3 deletions rust/src/ftp/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* 02110-1301, USA.
*/

use crate::core::AppLayerEventType;
use crate::sys::SCAppLayerEventType;
use std::os::raw::{c_char, c_int};

#[derive(Debug, PartialEq, Eq, AppLayerEvent)]
Expand All @@ -33,7 +33,7 @@ pub enum FtpEvent {
/// Unsafe as called from C.
#[no_mangle]
pub unsafe extern "C" fn ftp_get_event_info(
event_name: *const c_char, event_id: *mut u8, event_type: *mut AppLayerEventType,
event_name: *const c_char, event_id: *mut u8, event_type: *mut SCAppLayerEventType,
) -> c_int {
crate::applayer::get_event_info::<FtpEvent>(event_name, event_id, event_type)
}
Expand All @@ -44,7 +44,7 @@ pub unsafe extern "C" fn ftp_get_event_info(
/// Unsafe as called from C.
#[no_mangle]
pub unsafe extern "C" fn ftp_get_event_info_by_id(
event_id: u8, event_name: *mut *const c_char, event_type: *mut AppLayerEventType,
event_id: u8, event_name: *mut *const c_char, event_type: *mut SCAppLayerEventType,
) -> c_int {
crate::applayer::get_event_info_by_id::<FtpEvent>(event_id, event_name, event_type) as c_int
}
4 changes: 4 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,7 @@ pub mod direction;

#[allow(unused_imports)]
pub use suricata_lua_sys;

// Generated Rust bindings from C.
/// cbindgen:ignore
pub mod sys;
5 changes: 3 additions & 2 deletions rust/src/smb/smb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use crate::smb::session::*;
use crate::smb::events::*;
use crate::smb::files::*;
use crate::smb::smb2_ioctl::*;
use crate::sys::SCAppLayerEventType;

#[derive(AppLayerFrameType)]
pub enum SMBFrameType {
Expand Down Expand Up @@ -2267,7 +2268,7 @@ pub unsafe extern "C" fn rs_smb_get_tx_data(
pub unsafe extern "C" fn rs_smb_state_get_event_info_by_id(
event_id: u8,
event_name: *mut *const std::os::raw::c_char,
event_type: *mut AppLayerEventType,
event_type: *mut SCAppLayerEventType,
) -> std::os::raw::c_int {
SMBEvent::get_event_info_by_id(event_id, event_name, event_type)
}
Expand All @@ -2276,7 +2277,7 @@ pub unsafe extern "C" fn rs_smb_state_get_event_info_by_id(
pub unsafe extern "C" fn rs_smb_state_get_event_info(
event_name: *const std::os::raw::c_char,
event_id: *mut u8,
event_type: *mut AppLayerEventType,
event_type: *mut SCAppLayerEventType,
) -> std::os::raw::c_int {
SMBEvent::get_event_info(event_name, event_id, event_type)
}
Expand Down
21 changes: 21 additions & 0 deletions rust/src/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright (C) 2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

// We do this as an include so we can allow non_camel_case_types.
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!("_sys.rs");
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ noinst_HEADERS = \
app-layer-ssh.h \
app-layer-ssl.h \
app-layer-tftp.h \
app-layer-types.h \
app-layer-imap.h \
build-info.h \
conf.h \
Expand Down
4 changes: 2 additions & 2 deletions src/app-layer-dnp3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ static int DNP3GetAlstateProgress(void *tx, uint8_t direction)
* \brief App-layer support.
*/
static int DNP3StateGetEventInfo(
const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
const char *event_name, uint8_t *event_id, SCAppLayerEventType *event_type)
{
if (SCAppLayerGetEventIdByName(event_name, dnp3_decoder_event_table, event_id) == 0) {
*event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
Expand All @@ -1447,7 +1447,7 @@ static int DNP3StateGetEventInfo(
* \brief App-layer support.
*/
static int DNP3StateGetEventInfoById(
uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
uint8_t event_id, const char **event_name, SCAppLayerEventType *event_type)
{
*event_name = SCMapEnumValueToName(event_id, dnp3_decoder_event_table);
if (*event_name == NULL) {
Expand Down
5 changes: 3 additions & 2 deletions src/app-layer-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include "app-layer-events.h"
#include "rust.h"
#include "util-enum.h"

int SCAppLayerGetEventIdByName(const char *event_name, SCEnumCharMap *table, uint8_t *event_id)
Expand Down Expand Up @@ -61,7 +62,7 @@ SCEnumCharMap app_layer_event_pkt_table[ ] = {
};

int AppLayerGetEventInfoById(
uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
uint8_t event_id, const char **event_name, SCAppLayerEventType *event_type)
{
*event_name = SCMapEnumValueToName(event_id, app_layer_event_pkt_table);
if (*event_name == NULL) {
Expand Down Expand Up @@ -165,7 +166,7 @@ SCEnumCharMap det_ctx_event_table[] = {
};

int DetectEngineGetEventInfo(
const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
const char *event_name, uint8_t *event_id, SCAppLayerEventType *event_type)
{
if (SCAppLayerGetEventIdByName(event_name, det_ctx_event_table, event_id) == 0) {
*event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
Expand Down
12 changes: 6 additions & 6 deletions src/app-layer-events.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
#define SURICATA_APP_LAYER_EVENTS_H

/* contains fwd declaration of AppLayerDecoderEvents_ */
#include "decode.h"
#include "rust.h"
#include "suricata-common.h"
#include "app-layer-types.h"
#include "util-enum.h"

/**
* \brief Data structure to store app layer decoder events.
*/
struct AppLayerDecoderEvents_ {
typedef struct AppLayerDecoderEvents_ {
/* array of events */
uint8_t *events;
/* number of events in the above buffer */
Expand All @@ -42,7 +42,7 @@ struct AppLayerDecoderEvents_ {
uint8_t events_buffer_size;
/* last logged */
uint8_t event_last_logged;
};
} AppLayerDecoderEvents;

/* app layer pkt level events */
enum {
Expand All @@ -57,7 +57,7 @@ enum {
int AppLayerGetPktEventInfo(const char *event_name, uint8_t *event_id);

int AppLayerGetEventInfoById(
uint8_t event_id, const char **event_name, AppLayerEventType *event_type);
uint8_t event_id, const char **event_name, SCAppLayerEventType *event_type);
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event);

static inline int AppLayerDecoderEventsIsEventSet(
Expand All @@ -78,7 +78,7 @@ static inline int AppLayerDecoderEventsIsEventSet(
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events);
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events);
int DetectEngineGetEventInfo(
const char *event_name, uint8_t *event_id, AppLayerEventType *event_type);
const char *event_name, uint8_t *event_id, SCAppLayerEventType *event_type);
int SCAppLayerGetEventIdByName(const char *event_name, SCEnumCharMap *table, uint8_t *event_id);

#endif /* SURICATA_APP_LAYER_EVENTS_H */
4 changes: 2 additions & 2 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2728,7 +2728,7 @@ void *HtpGetTxForH2(void *alstate)
}

static int HTPStateGetEventInfo(
const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
const char *event_name, uint8_t *event_id, SCAppLayerEventType *event_type)
{
if (SCAppLayerGetEventIdByName(event_name, http_decoder_event_table, event_id) == 0) {
*event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
Expand All @@ -2738,7 +2738,7 @@ static int HTPStateGetEventInfo(
}

static int HTPStateGetEventInfoById(
uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
uint8_t event_id, const char **event_name, SCAppLayerEventType *event_type)
{
*event_name = SCMapEnumValueToName(event_id, http_decoder_event_table);
if (*event_name == NULL) {
Expand Down
Loading