Skip to content

Commit

Permalink
rust/ffi: provide AppLayerRegisterParser in context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jasonish committed Feb 4, 2021
1 parent 13700b6 commit ef53224
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rust/src/applayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 4 additions & 0 deletions src/rust-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/suricata.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@

#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"
Expand Down Expand Up @@ -2669,6 +2670,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);
Expand Down

0 comments on commit ef53224

Please sign in to comment.