]> git.ipfire.org Git - people/ms/suricata.git/commitdiff
rust/ffi: provide AppLayerRegisterParser in context
authorJason Ish <jason.ish@oisf.net>
Thu, 26 Nov 2020 22:21:46 +0000 (16:21 -0600)
committerVictor Julien <victor@inliniac.net>
Fri, 12 Feb 2021 13:52:33 +0000 (14:52 +0100)
AppLayerRegisterParser was creating a link error when attempting
to use a convenience library for the Suricata C code, then linking
the library of C code with the library of Rust code into a final
Suricata executable, or use with fuzz targets.

By moving AppLayerRegisterParser to the context structure and
calling it like a callback the circular reference is removed
allowing the convenience libraries to work again.

This is also a stepping block to proving a Suricata library
as a single .a or .so file.

rust/src/applayer.rs
rust/src/core.rs
src/rust-context.h
src/suricata.c

index 476ea1cb361f8e2fe96ac3fd273ae6e68950eb88..684d0f27596a6f0e063f5eb90a9444e42dfa279a 100644 (file)
@@ -22,6 +22,7 @@ use crate::core::{DetectEngineState,Flow,AppLayerEventType,AppLayerDecoderEvents
 use crate::filecontainer::FileContainer;
 use crate::applayer;
 use std::os::raw::{c_void,c_char,c_int};
+use crate::core::SC;
 
 #[repr(C)]
 #[derive(Debug,PartialEq)]
@@ -289,7 +290,11 @@ pub type TruncateFn = unsafe extern "C" fn (*mut c_void, u8);
 // Defined in app-layer-register.h
 extern {
     pub fn AppLayerRegisterProtocolDetection(parser: *const RustParser, enable_default: c_int) -> AppProto;
-    pub fn AppLayerRegisterParser(parser: *const RustParser, alproto: AppProto) -> c_int;
+}
+
+#[allow(non_snake_case)]
+pub unsafe fn AppLayerRegisterParser(parser: *const RustParser, alproto: AppProto) -> c_int {
+    (SC.unwrap().AppLayerRegisterParser)(parser, alproto)
 }
 
 // Defined in app-layer-detect-proto.h
index db929303217abe8caec3ed35225e09facdd1aa1f..69ed658a234752c692d10dcf99ea0f748ca54eeb 100644 (file)
@@ -141,6 +141,8 @@ pub struct SuricataContext {
     pub FileContainerRecycle: SCFileContainerRecycle,
     pub FilePrune: SCFilePrune,
     pub FileSetTx: SCFileSetTx,
+
+    pub AppLayerRegisterParser: extern fn(parser: *const crate::applayer::RustParser, alproto: AppProto) -> std::os::raw::c_int,
 }
 
 #[allow(non_snake_case)]
index 8efd2678a3d44d6d73e4fa1134050520e4db7db4..f0005314f7cfa3fe156c57ad0cb87f6b95486174 100644 (file)
@@ -25,6 +25,8 @@
 #include "app-layer-snmp.h" //SNMPState, SNMPTransaction
 #include "app-layer-tftp.h" //TFTPState, TFTPTransaction
 
+struct AppLayerParser;
+
 typedef struct SuricataContext_ {
     SCError (*SCLogMessage)(const SCLogLevel, const char *, const unsigned int,
             const char *, const SCError, const char *message);
@@ -46,6 +48,8 @@ typedef struct SuricataContext_ {
     void (*FilePrune)(FileContainer *ffc);
     void (*FileSetTx)(FileContainer *, uint64_t);
 
+    int (*AppLayerRegisterParser)(const struct AppLayerParser *p, AppProto alproto);
+
 } SuricataContext;
 
 extern SuricataContext suricata_context;
index 5b80e974a0c6a0642658a6cf96c45746ff7da526..f386dc4159c884332691eaef904046785cb81d68 100644 (file)
 
 #include "app-layer.h"
 #include "app-layer-parser.h"
+#include "app-layer-register.h"
 #include "app-layer-htp.h"
 #include "app-layer-ssl.h"
 #include "app-layer-ssh.h"
@@ -2661,6 +2662,8 @@ int InitGlobal(void) {
     suricata_context.FilePrune = FilePrune;
     suricata_context.FileSetTx = FileContainerSetTx;
 
+    suricata_context.AppLayerRegisterParser = AppLayerRegisterParser;
+
     rs_init(&suricata_context);
 
     SC_ATOMIC_INIT(engine_stage);