Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ jobs:
runs-on: ubuntu-latest
container: almalinux:9
steps:
- name: Cache rust
uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2
with:
path: ~/.cargo
key: check-rust

- name: Install system packages
run: |
dnf -y install dnf-plugins-core
dnf -y install dnf-plugins-core epel-release
dnf config-manager --set-enabled crb
dnf -y install \
autoconf \
automake \
cargo-vendor \
bindgen \
cargo \
cbindgen \
clang-devel \
clippy \
diffutils \
numactl-devel \
dpdk-devel \
Expand All @@ -61,21 +59,28 @@ jobs:
python3-devel \
python3-sphinx \
python3-yaml \
rust \
rustfmt \
sudo \
which \
zlib-devel

- name: Installing Rust
run: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- name: Install cbindgen
run: cargo install --debug cbindgen
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- run: git config --global --add safe.directory /__w/suricata/suricata
- run: ./scripts/bundle.sh
- run: ./autogen.sh
- run: ./configure --enable-warnings
- name: Checking bindgen output
working-directory: rust
run: |
bindgen --version
make update-bindings
diff=$(git diff src/_sys.rs)
if [ "${diff}" ]; then
echo "${diff}"
echo "::error ::Bindgen bindings appear to be out of date"
exit 1
fi
- run: cargo clippy --all-features --fix --allow-no-vcs
working-directory: rust
- run: |
Expand Down
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
52 changes: 49 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 @@ -75,14 +81,51 @@ clean-local:
distclean-local:
rm -rf vendor dist

check-bindgen-bindings:
if HAVE_BINDGEN
if test "$(top_srcdir)" = "$(top_builddir)"; then \
cp src/_sys.rs src/_sys.rs.orig; \
$(MAKE) update-bindings; \
if diff src/_sys.rs src/_sys.rs.orig > /dev/null 2>&1; then \
rm -f src/_sys.rs.orig; \
else \
echo "WARNING: bindgen bindings may be out of date"; \
fi \
else \
echo "Not checking bindings for out of tree build"; \
fi
else
@echo "Unable to check bindgen bindings: bindgen not found"
endif

check:
cd $(abs_top_srcdir)/rust && \
$(CARGO_ENV) \
$(CARGO) test --all $(RELEASE) --features "$(RUST_FEATURES)"
$(MAKE) check-bindgen-bindings

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' \
--allowlist-type 'AppProto' \
--allowlist-type 'AppProtoEnum' \
--rustified-enum 'AppProtoEnum' \
$(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 +136,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
58 changes: 58 additions & 0 deletions rust/src/_sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum AppProtoEnum {
ALPROTO_UNKNOWN = 0,
ALPROTO_FAILED = 1,
ALPROTO_HTTP1 = 2,
ALPROTO_FTP = 3,
ALPROTO_SMTP = 4,
ALPROTO_TLS = 5,
ALPROTO_SSH = 6,
ALPROTO_IMAP = 7,
ALPROTO_JABBER = 8,
ALPROTO_SMB = 9,
ALPROTO_DCERPC = 10,
ALPROTO_IRC = 11,
ALPROTO_DNS = 12,
ALPROTO_MODBUS = 13,
ALPROTO_ENIP = 14,
ALPROTO_DNP3 = 15,
ALPROTO_NFS = 16,
ALPROTO_NTP = 17,
ALPROTO_FTPDATA = 18,
ALPROTO_TFTP = 19,
ALPROTO_IKE = 20,
ALPROTO_KRB5 = 21,
ALPROTO_QUIC = 22,
ALPROTO_DHCP = 23,
ALPROTO_SNMP = 24,
ALPROTO_SIP = 25,
ALPROTO_RFB = 26,
ALPROTO_MQTT = 27,
ALPROTO_PGSQL = 28,
ALPROTO_TELNET = 29,
ALPROTO_WEBSOCKET = 30,
ALPROTO_LDAP = 31,
ALPROTO_DOH2 = 32,
ALPROTO_TEMPLATE = 33,
ALPROTO_RDP = 34,
ALPROTO_HTTP2 = 35,
ALPROTO_BITTORRENT_DHT = 36,
ALPROTO_POP3 = 37,
ALPROTO_HTTP = 38,
ALPROTO_MAX_STATIC = 39,
}
pub type AppProto = u16;
#[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
17 changes: 4 additions & 13 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ use std;
use crate::filecontainer::*;
use crate::flow::Flow;

pub use crate::sys::{AppProto, AppProtoEnum};

/// Opaque C types.
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 All @@ -41,11 +35,8 @@ pub const STREAM_GAP: u8 = 0x10;
pub const STREAM_DEPTH: u8 = 0x20;
pub const STREAM_MIDSTREAM:u8 = 0x40;

// Application layer protocol identifiers (app-layer-protos.h)
pub type AppProto = u16;

pub const ALPROTO_UNKNOWN : AppProto = 0;
pub const ALPROTO_FAILED : AppProto = 1;
pub const ALPROTO_UNKNOWN : AppProto = AppProtoEnum::ALPROTO_UNKNOWN as u16;
pub const ALPROTO_FAILED : AppProto = AppProtoEnum::ALPROTO_FAILED as u16;

pub const IPPROTO_TCP : u8 = 6;
pub const IPPROTO_UDP : u8 = 17;
Expand Down
8 changes: 4 additions & 4 deletions rust/src/dhcp/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ use crate::applayer::{self, *};
use crate::core::{ALPROTO_UNKNOWN, AppProto, IPPROTO_UDP};
use crate::dhcp::parser::*;
use crate::flow::Flow;
use crate::sys::AppProtoEnum;
use std;
use std::ffi::CString;

pub(super) static mut ALPROTO_DHCP: AppProto = ALPROTO_UNKNOWN;
pub(super) static ALPROTO_DHCP: AppProto = AppProtoEnum::ALPROTO_DHCP as AppProto;

static DHCP_MIN_FRAME_LEN: u32 = 232;

Expand Down Expand Up @@ -305,10 +306,9 @@ pub unsafe extern "C" fn rs_dhcp_register_parser() {
let ip_proto_str = CString::new("udp").unwrap();

if AppLayerProtoDetectConfProtoDetectionEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
let alproto = AppLayerRegisterProtocolDetection(&parser, 1);
ALPROTO_DHCP = alproto;
AppLayerRegisterProtocolDetection(&parser, 1);
if AppLayerParserConfParserEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
let _ = AppLayerRegisterParser(&parser, alproto);
let _ = AppLayerRegisterParser(&parser, ALPROTO_DHCP);
}
} else {
SCLogDebug!("Protocol detector and parser disabled for DHCP.");
Expand Down
Loading
Loading