Skip to content

Commit

Permalink
rust: remove unneeded mut for tx in loggers
Browse files Browse the repository at this point in the history
Loggers do not change transactions, they read only.
  • Loading branch information
catenacyber authored and victorjulien committed Jan 16, 2025
1 parent 5ee6e7c commit af4e1e8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 38 deletions.
4 changes: 2 additions & 2 deletions rust/src/dns/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ pub extern "C" fn SCDnsLogJsonQuery(
/// "dns" object.
///
/// This logger implements V3 style DNS logging.
fn log_json(tx: &mut DNSTransaction, flags: u64, jb: &mut JsonBuilder) -> Result<(), JsonError> {
fn log_json(tx: &DNSTransaction, flags: u64, jb: &mut JsonBuilder) -> Result<(), JsonError> {
jb.open_object("dns")?;
jb.set_int("version", 3)?;

Expand Down Expand Up @@ -921,7 +921,7 @@ fn log_json(tx: &mut DNSTransaction, flags: u64, jb: &mut JsonBuilder) -> Result

/// FFI wrapper around the common V3 style DNS logger.
#[no_mangle]
pub extern "C" fn SCDnsLogJson(tx: &mut DNSTransaction, flags: u64, jb: &mut JsonBuilder) -> bool {
pub extern "C" fn SCDnsLogJson(tx: &DNSTransaction, flags: u64, jb: &mut JsonBuilder) -> bool {
log_json(tx, flags, jb).is_ok()
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/krb/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::jsonbuilder::{JsonBuilder, JsonError};
use crate::krb::krb5::{KRB5Transaction,test_weak_encryption};

fn krb5_log_response(jsb: &mut JsonBuilder, tx: &mut KRB5Transaction) -> Result<(), JsonError>
fn krb5_log_response(jsb: &mut JsonBuilder, tx: &KRB5Transaction) -> Result<(), JsonError>
{
jsb.open_object("krb5")?;
match tx.error_code {
Expand Down Expand Up @@ -70,7 +70,7 @@ fn krb5_log_response(jsb: &mut JsonBuilder, tx: &mut KRB5Transaction) -> Result<
}

#[no_mangle]
pub extern "C" fn rs_krb5_log_json_response(tx: &mut KRB5Transaction, jsb: &mut JsonBuilder) -> bool
pub extern "C" fn rs_krb5_log_json_response(tx: &KRB5Transaction, jsb: &mut JsonBuilder) -> bool
{
krb5_log_response(jsb, tx).is_ok()
}
28 changes: 13 additions & 15 deletions rust/src/mime/smtp_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use digest::Update;
use md5::Md5;
use std::ffi::CStr;

fn log_subject_md5(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> Result<(), JsonError> {
fn log_subject_md5(js: &mut JsonBuilder, ctx: &MimeStateSMTP) -> Result<(), JsonError> {
for h in &ctx.headers[..ctx.main_headers_nb] {
if mime::slice_equals_lowercase(&h.name, b"subject") {
let hash = format!("{:x}", Md5::new().chain(&h.value).finalize());
Expand All @@ -36,12 +36,12 @@ fn log_subject_md5(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> Result<(),

#[no_mangle]
pub unsafe extern "C" fn SCMimeSmtpLogSubjectMd5(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP,
js: &mut JsonBuilder, ctx: &MimeStateSMTP,
) -> bool {
return log_subject_md5(js, ctx).is_ok();
}

fn log_body_md5(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> Result<(), JsonError> {
fn log_body_md5(js: &mut JsonBuilder, ctx: &MimeStateSMTP) -> Result<(), JsonError> {
if ctx.md5_state == MimeSmtpMd5State::MimeSmtpMd5Completed {
let hash = format!("{:x}", ctx.md5_result);
js.set_string("body_md5", &hash)?;
Expand All @@ -50,14 +50,12 @@ fn log_body_md5(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> Result<(), Jso
}

#[no_mangle]
pub unsafe extern "C" fn SCMimeSmtpLogBodyMd5(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP,
) -> bool {
pub unsafe extern "C" fn SCMimeSmtpLogBodyMd5(js: &mut JsonBuilder, ctx: &MimeStateSMTP) -> bool {
return log_body_md5(js, ctx).is_ok();
}

fn log_field_array(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, c: &str, e: &str,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, c: &str, e: &str,
) -> Result<(), JsonError> {
let mark = js.get_mark();
let mut found = false;
Expand All @@ -81,7 +79,7 @@ fn log_field_array(

#[no_mangle]
pub unsafe extern "C" fn SCMimeSmtpLogFieldArray(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, email: *const std::os::raw::c_char,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, email: *const std::os::raw::c_char,
config: *const std::os::raw::c_char,
) -> bool {
let e: &CStr = CStr::from_ptr(email); //unsafe
Expand All @@ -101,7 +99,7 @@ enum FieldCommaState {
}

fn log_field_comma(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, c: &str, e: &str,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, c: &str, e: &str,
) -> Result<(), JsonError> {
for h in &ctx.headers[..ctx.main_headers_nb] {
if mime::slice_equals_lowercase(&h.name, e.as_bytes()) {
Expand Down Expand Up @@ -158,7 +156,7 @@ fn log_field_comma(

#[no_mangle]
pub unsafe extern "C" fn SCMimeSmtpLogFieldComma(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, email: *const std::os::raw::c_char,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, email: *const std::os::raw::c_char,
config: *const std::os::raw::c_char,
) -> bool {
let e: &CStr = CStr::from_ptr(email); //unsafe
Expand All @@ -172,7 +170,7 @@ pub unsafe extern "C" fn SCMimeSmtpLogFieldComma(
}

fn log_field_string(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, c: &str, e: &str,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, c: &str, e: &str,
) -> Result<(), JsonError> {
for h in &ctx.headers[..ctx.main_headers_nb] {
if mime::slice_equals_lowercase(&h.name, e.as_bytes()) {
Expand All @@ -185,7 +183,7 @@ fn log_field_string(

#[no_mangle]
pub unsafe extern "C" fn SCMimeSmtpLogFieldString(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, email: *const std::os::raw::c_char,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, email: *const std::os::raw::c_char,
config: *const std::os::raw::c_char,
) -> bool {
let e: &CStr = CStr::from_ptr(email); //unsafe
Expand All @@ -199,7 +197,7 @@ pub unsafe extern "C" fn SCMimeSmtpLogFieldString(
}

fn log_data_header(
js: &mut JsonBuilder, ctx: &mut MimeStateSMTP, hname: &str,
js: &mut JsonBuilder, ctx: &MimeStateSMTP, hname: &str,
) -> Result<(), JsonError> {
for h in &ctx.headers[..ctx.main_headers_nb] {
if mime::slice_equals_lowercase(&h.name, hname.as_bytes()) {
Expand All @@ -210,7 +208,7 @@ fn log_data_header(
return Ok(());
}

fn log_data(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> Result<(), JsonError> {
fn log_data(js: &mut JsonBuilder, ctx: &MimeStateSMTP) -> Result<(), JsonError> {
log_data_header(js, ctx, "from")?;
log_field_comma(js, ctx, "to", "to")?;
log_field_comma(js, ctx, "cc", "cc")?;
Expand All @@ -236,6 +234,6 @@ fn log_data(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> Result<(), JsonErr
}

#[no_mangle]
pub unsafe extern "C" fn SCMimeSmtpLogData(js: &mut JsonBuilder, ctx: &mut MimeStateSMTP) -> bool {
pub unsafe extern "C" fn SCMimeSmtpLogData(js: &mut JsonBuilder, ctx: &MimeStateSMTP) -> bool {
return log_data(js, ctx).is_ok();
}
2 changes: 1 addition & 1 deletion rust/src/modbus/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::jsonbuilder::{JsonBuilder, JsonError};
use sawp_modbus::{Data, Message, Read, Write};

#[no_mangle]
pub extern "C" fn rs_modbus_to_json(tx: &mut ModbusTransaction, js: &mut JsonBuilder) -> bool {
pub extern "C" fn rs_modbus_to_json(tx: &ModbusTransaction, js: &mut JsonBuilder) -> bool {
log(tx, js).is_ok()
}

Expand Down
8 changes: 4 additions & 4 deletions rust/src/nfs/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crc::crc32;

#[no_mangle]
pub extern "C" fn rs_nfs_tx_logging_is_filtered(state: &mut NFSState,
tx: &mut NFSTransaction)
tx: &NFSTransaction)
-> u8
{
// TODO probably best to make this configurable
Expand Down Expand Up @@ -119,7 +119,7 @@ fn nfs_log_request(state: &NFSState, tx: &NFSTransaction, js: &mut JsonBuilder)
}

#[no_mangle]
pub extern "C" fn rs_nfs_log_json_request(state: &mut NFSState, tx: &mut NFSTransaction,
pub extern "C" fn rs_nfs_log_json_request(state: &mut NFSState, tx: &NFSTransaction,
js: &mut JsonBuilder) -> bool
{
nfs_log_request(state, tx, js).is_ok()
Expand Down Expand Up @@ -152,7 +152,7 @@ fn nfs_log_response(state: &NFSState, tx: &NFSTransaction, js: &mut JsonBuilder)
}

#[no_mangle]
pub extern "C" fn rs_nfs_log_json_response(state: &mut NFSState, tx: &mut NFSTransaction,
pub extern "C" fn rs_nfs_log_json_response(state: &mut NFSState, tx: &NFSTransaction,
js: &mut JsonBuilder) -> bool
{
nfs_log_response(state, tx, js).is_ok()
Expand All @@ -173,7 +173,7 @@ fn rpc_log_response(tx: &NFSTransaction, js: &mut JsonBuilder)
}

#[no_mangle]
pub extern "C" fn rs_rpc_log_json_response(tx: &mut NFSTransaction,
pub extern "C" fn rs_rpc_log_json_response(tx: &NFSTransaction,
js: &mut JsonBuilder) -> bool
{
rpc_log_response(tx, js).is_ok()
Expand Down
4 changes: 2 additions & 2 deletions rust/src/rdp/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use super::rdp::{RdpTransaction, RdpTransactionItem};
use crate::jsonbuilder::{JsonBuilder, JsonError};
use crate::rdp::parser::*;
use crate::rdp::windows;
use x509_parser::prelude::{X509Certificate, FromDer};
use x509_parser::prelude::{FromDer, X509Certificate};

#[no_mangle]
pub extern "C" fn rs_rdp_to_json(tx: &mut RdpTransaction, js: &mut JsonBuilder) -> bool {
pub extern "C" fn rs_rdp_to_json(tx: &RdpTransaction, js: &mut JsonBuilder) -> bool {
log(tx, js).is_ok()
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/sip/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ fn log(tx: &SIPTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> {
}

#[no_mangle]
pub extern "C" fn rs_sip_log_json(tx: &mut SIPTransaction, js: &mut JsonBuilder) -> bool {
pub extern "C" fn rs_sip_log_json(tx: &SIPTransaction, js: &mut JsonBuilder) -> bool {
log(tx, js).is_ok()
}
4 changes: 2 additions & 2 deletions rust/src/smb/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,13 @@ fn smb_common_header(jsb: &mut JsonBuilder, state: &SMBState, tx: &SMBTransactio
}

#[no_mangle]
pub extern "C" fn rs_smb_log_json_request(jsb: &mut JsonBuilder, state: &mut SMBState, tx: &mut SMBTransaction) -> bool
pub extern "C" fn rs_smb_log_json_request(jsb: &mut JsonBuilder, state: &mut SMBState, tx: &SMBTransaction) -> bool
{
smb_common_header(jsb, state, tx).is_ok()
}

#[no_mangle]
pub extern "C" fn rs_smb_log_json_response(jsb: &mut JsonBuilder, state: &mut SMBState, tx: &mut SMBTransaction) -> bool
pub extern "C" fn rs_smb_log_json_response(jsb: &mut JsonBuilder, state: &mut SMBState, tx: &SMBTransaction) -> bool
{
smb_common_header(jsb, state, tx).is_ok()
}
Expand Down
12 changes: 3 additions & 9 deletions rust/src/tftp/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
use crate::jsonbuilder::{JsonBuilder, JsonError};
use crate::tftp::tftp::TFTPTransaction;

fn tftp_log_request(tx: &mut TFTPTransaction,
jb: &mut JsonBuilder)
-> Result<(), JsonError>
{
fn tftp_log_request(tx: &TFTPTransaction, jb: &mut JsonBuilder) -> Result<(), JsonError> {
jb.open_object("tftp")?;
match tx.opcode {
1 => jb.set_string("packet", "read")?,
2 => jb.set_string("packet", "write")?,
_ => jb.set_string("packet", "error")?
_ => jb.set_string("packet", "error")?,
};
jb.set_string("file", tx.filename.as_str())?;
jb.set_string("mode", tx.mode.as_str())?;
Expand All @@ -37,9 +34,6 @@ fn tftp_log_request(tx: &mut TFTPTransaction,
}

#[no_mangle]
pub extern "C" fn rs_tftp_log_json_request(tx: &mut TFTPTransaction,
jb: &mut JsonBuilder)
-> bool
{
pub extern "C" fn rs_tftp_log_json_request(tx: &TFTPTransaction, jb: &mut JsonBuilder) -> bool {
tftp_log_request(tx, jb).is_ok()
}

0 comments on commit af4e1e8

Please sign in to comment.