]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: integrate bindgen to generate Rust bindings to C
authorJason Ish <jason.ish@oisf.net>
Thu, 23 Jan 2025 22:26:25 +0000 (16:26 -0600)
committerVictor Julien <victor@inliniac.net>
Mon, 17 Feb 2025 06:31:29 +0000 (07:31 +0100)
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

rust/Makefile.am
rust/sys/src/lib.rs
rust/sys/src/sys.rs [new file with mode: 0644]
src/Makefile.am
src/bindgen.h [new file with mode: 0644]

index 7abd37d2208fc6d54922f8c6177e17acd593fa43..c96724f01f1bc79cf5fe769420f68122bf71bcd6 100644 (file)
@@ -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 && \
index 48917a9d99c7cfb17f943aabac619bdc6cb19ce1..470f6e7a37ee94c2e007863856cbe53c52c7fad6 100644 (file)
@@ -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;
diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs
new file mode 100644 (file)
index 0000000..61c35d8
--- /dev/null
@@ -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,
+    );
+}
index f99b8f83556cf95271990c812c9f1f1e37498cc1..c3b1e9d237f9bdad63c455cbb9a090279b038187 100755 (executable)
@@ -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 \
diff --git a/src/bindgen.h b/src/bindgen.h
new file mode 100644 (file)
index 0000000..9db5d0f
--- /dev/null
@@ -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