Skip to content

Commit

Permalink
plugin: restrict rust visibility in example
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber committed Jan 25, 2025
1 parent 55484af commit b35cd6d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
3 changes: 1 addition & 2 deletions examples/plugins/altemplate/src/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ unsafe extern "C" fn template_buffer_get(
);
}

#[no_mangle]
pub unsafe extern "C" fn DetectTemplateRegister() {
pub(super) unsafe extern "C" fn detect_template_register() {
// TODO create a suricata-verify test
// Setup a keyword structure and register it
let kw = SCSigTableElmt {
Expand Down
9 changes: 4 additions & 5 deletions examples/plugins/altemplate/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ extern "C" {
}

impl JsonBuilder {
pub fn close(&mut self) -> Result<(), JsonError> {
fn close(&mut self) -> Result<(), JsonError> {
if unsafe { !jb_close(self) } {
return Err(JsonError::Memory);
}
Ok(())
}
pub fn open_object(&mut self, key: &str) -> Result<(), JsonError> {
fn open_object(&mut self, key: &str) -> Result<(), JsonError> {
let keyc = CString::new(key).unwrap();
if unsafe { !jb_open_object(self, keyc.as_ptr()) } {
return Err(JsonError::Memory);
}
Ok(())
}
pub fn set_string(&mut self, key: &str, val: &str) -> Result<(), JsonError> {
fn set_string(&mut self, key: &str, val: &str) -> Result<(), JsonError> {
let keyc = CString::new(key).unwrap();
let valc = CString::new(val.escape_default().to_string()).unwrap();
if unsafe { !jb_set_string(self, keyc.as_ptr(), valc.as_ptr()) } {
Expand All @@ -75,8 +75,7 @@ fn log_template(tx: &TemplateTransaction, js: &mut JsonBuilder) -> Result<(), Js
Ok(())
}

#[no_mangle]
pub unsafe extern "C" fn template_logger_log(
pub(super) unsafe extern "C" fn template_logger_log(
tx: *const std::os::raw::c_void, js: *mut std::os::raw::c_void,
) -> bool {
let tx = cast_pointer!(tx, TemplateTransaction);
Expand Down
2 changes: 1 addition & 1 deletion examples/plugins/altemplate/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn parse_len(input: &str) -> Result<u32, std::num::ParseIntError> {
input.parse::<u32>()
}

pub fn parse_message(i: &[u8]) -> IResult<&[u8], String> {
pub(super) fn parse_message(i: &[u8]) -> IResult<&[u8], String> {
let (i, len) = map_res(map_res(take_until(":"), std::str::from_utf8), parse_len)(i)?;
let (i, _sep) = take(1_usize)(i)?;
let (i, msg) = map_res(take(len as usize), std::str::from_utf8)(i)?;
Expand Down
4 changes: 2 additions & 2 deletions examples/plugins/altemplate/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::template::template_register_parser;
use crate::detect::DetectTemplateRegister;
use crate::detect::detect_template_register;
use crate::log::template_logger_log;
use suricata::plugin::{
SCAppLayerPlugin, SCPlugin, SCPluginRegisterAppLayer, SC_PLUGIN_API_VERSION,
Expand All @@ -16,7 +16,7 @@ extern "C" fn altemplate_plugin_init() {
confname: b"eve-log.altemplate\0".as_ptr() as *const libc::c_char,
Register: template_register_parser,
Logger: template_logger_log,
KeywordsRegister: DetectTemplateRegister,
KeywordsRegister: detect_template_register,
};
unsafe {
if SCPluginRegisterAppLayer(Box::into_raw(Box::new(plugin))) != 0 {
Expand Down
7 changes: 3 additions & 4 deletions examples/plugins/altemplate/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ enum TemplateEvent {
TooManyTransactions,
}

pub struct TemplateTransaction {
pub(super) struct TemplateTransaction {
tx_id: u64,
pub request: Option<String>,
pub response: Option<String>,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Transaction for TemplateTransaction {
}

#[derive(Default)]
pub struct TemplateState {
struct TemplateState {
state_data: AppLayerStateData,
tx_id: u64,
transactions: VecDeque<TemplateTransaction>,
Expand Down Expand Up @@ -373,8 +373,7 @@ export_state_data_get!(rs_template_get_state_data, TemplateState);
// Parser name as a C style string.
const PARSER_NAME: &[u8] = b"altemplate\0";

#[no_mangle]
pub unsafe extern "C" fn template_register_parser() {
pub(super) unsafe extern "C" fn template_register_parser() {
let default_port = CString::new("[7000]").unwrap();
let parser = RustParser {
name: PARSER_NAME.as_ptr() as *const c_char,
Expand Down
2 changes: 1 addition & 1 deletion examples/plugins/c-custom-loggers/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# But as this is an example in the Suricata source tree we'll look for
# includes in the source tree.
CPPFLAGS += -I@top_srcdir@/src -DHAVE_CONFIG_H
CPPFLAGS += -I@top_srcdir@/src -I@top_srcdir@/rust/gen -DHAVE_CONFIG_H

# Currently the Suricata logging system requires this to be even for
# plugins.
Expand Down

0 comments on commit b35cd6d

Please sign in to comment.