rust: integrate bindgen to generate Rust bindings to C

Bindgen works by processing a header file which includes all other
header files it should generate bindings for. For this I've created
bindgen.h which just includes app-layer-protos.h for now as an
example.

These bindings are then generated and saved in the "suricata-sys"
crate and become availale as "suricata_sys::sys".

Ticket: #7341
pull/12590/head
Jason Ish 6 months ago committed by Victor Julien
parent 8f22e55678
commit 21ccc4f307

@ -79,14 +79,49 @@ clean-local:
distclean-local:
rm -rf vendor dist
check-bindgen-bindings:
if HAVE_BINDGEN
if test "$(top_srcdir)" = "$(top_builddir)"; then \
cp sys/src/sys.rs sys/src/sys.rs.orig; \
$(MAKE) update-bindings; \
if diff sys/src/sys.rs sys/src/sys.rs.orig > /dev/null 2>&1; then \
rm -f sys/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 sys/src/sys.rs \
--rust-target 1.68 \
--disable-header-comment \
--default-enum-style rust \
--allowlist-type 'AppProto.*' \
--allowlist-function 'AppProto.*' \
$(abs_top_srcdir)/src/bindgen.h \
-- \
-DHAVE_CONFIG_H -I../src -I../rust/gen $(CPPFLAGS)
else
@echo "error: bindgen not installed, can't update bindings"
exit 1
endif
if HAVE_CBINDGEN
gen/rust-bindings.h: $(RUST_SURICATA_LIB) cbindgen.toml
cd $(abs_top_srcdir)/rust && \

@ -14,3 +14,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(clippy::all)]
pub mod sys;

@ -0,0 +1,54 @@
#[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;
extern "C" {
#[doc = " \\brief Maps the ALPROTO_*, to its string equivalent.\n\n \\param alproto App layer protocol id.\n\n \\retval String equivalent for the alproto."]
pub fn AppProtoToString(alproto: AppProto) -> *const ::std::os::raw::c_char;
}
extern "C" {
pub fn AppProtoRegisterProtoString(
alproto: AppProto, proto_name: *const ::std::os::raw::c_char,
);
}

@ -43,6 +43,7 @@ noinst_HEADERS = \
app-layer-ssl.h \
app-layer-tftp.h \
app-layer-imap.h \
bindgen.h \
build-info.h \
conf.h \
conf-yaml-loader.h \

@ -0,0 +1,34 @@
/* Copyright (C) 2025 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.
*/
/**
* \file Input to bindgen to generate Rust bindings.
*
* This file should include every header that should have Rust
* bindings generated for it. It is then used by bindgen to generate
* the Rust bindings.
*/
#ifndef SURICATA_BINDGEN_H
#define SURICATA_BINDGEN_H
#include "stdint.h"
#include "stdbool.h"
#include "app-layer-protos.h"
#endif
Loading…
Cancel
Save