Skip to content

Commit

Permalink
mqtt: Move conf code to rust
Browse files Browse the repository at this point in the history
Issue: 6387

This commit moves the configuration logic to Rust.
  • Loading branch information
jlucovsky authored and victorjulien committed Dec 28, 2023
1 parent b453eea commit f12e026
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 108 deletions.
19 changes: 12 additions & 7 deletions rust/src/mqtt/mqtt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2020-2022 Open Information Security Foundation
/* Copyright (C) 2020-2023 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
Expand All @@ -21,7 +21,7 @@ use super::mqtt_message::*;
use super::parser::*;
use crate::applayer::*;
use crate::applayer::{self, LoggerFlags};
use crate::conf::conf_get;
use crate::conf::{conf_get, get_memval};
use crate::core::*;
use crate::frames::*;
use nom7::Err;
Expand Down Expand Up @@ -112,7 +112,7 @@ pub struct MQTTState {
connected: bool,
skip_request: usize,
skip_response: usize,
max_msg_len: usize,
max_msg_len: u32,
tx_index_completed: usize,
}

Expand Down Expand Up @@ -142,7 +142,7 @@ impl MQTTState {
connected: false,
skip_request: 0,
skip_response: 0,
max_msg_len: unsafe { MAX_MSG_LEN as usize },
max_msg_len: unsafe { MAX_MSG_LEN},
tx_index_completed: 0,
}
}
Expand Down Expand Up @@ -778,10 +778,8 @@ export_tx_data_get!(rs_mqtt_get_tx_data, MQTTTransaction);
export_state_data_get!(rs_mqtt_get_state_data, MQTTState);

#[no_mangle]
pub unsafe extern "C" fn rs_mqtt_register_parser(cfg_max_msg_len: u32) {
pub unsafe extern "C" fn SCMqttRegisterParser() {
let default_port = CString::new("[1883]").unwrap();
let max_msg_len = &mut MAX_MSG_LEN;
*max_msg_len = cfg_max_msg_len;
let parser = RustParser {
name: PARSER_NAME.as_ptr() as *const std::os::raw::c_char,
default_port: default_port.as_ptr(),
Expand Down Expand Up @@ -830,6 +828,13 @@ pub unsafe extern "C" fn rs_mqtt_register_parser(cfg_max_msg_len: u32) {
SCLogError!("Invalid value for mqtt.max-tx");
}
}
if let Some(val) = conf_get("app-layer.protocols.mqtt.max-msg-length") {
if let Ok(v) = get_memval(val) {
MAX_MSG_LEN = v as u32;
} else {
SCLogError!("Invalid value for mqtt.max-msg-length: {}", val);
}
}
} else {
SCLogDebug!("Protocol detector and parser disabled for MQTT.");
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/mqtt/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ fn parse_remaining_message<'a>(
pub fn parse_message(
input: &[u8],
protocol_version: u8,
max_msg_size: usize,
max_msg_size: u32,
) -> IResult<&[u8], MQTTMessage> {
// Parse the fixed header first. This is identical across versions and can
// be between 2 and 5 bytes long.
Expand All @@ -652,7 +652,7 @@ pub fn parse_message(
// limit, we return a special truncation message type, containing
// no parsed metadata but just the skipped length and the message
// type.
if len > max_msg_size {
if len > max_msg_size as usize {
let msg = MQTTMessage {
header,
op: MQTTOperation::TRUNCATED(MQTTTruncatedData {
Expand Down
2 changes: 0 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ noinst_HEADERS = \
app-layer-krb5.h \
app-layer-modbus.h \
app-layer-quic.h \
app-layer-mqtt.h \
app-layer-nfs-tcp.h \
app-layer-nfs-udp.h \
app-layer-ntp.h \
Expand Down Expand Up @@ -655,7 +654,6 @@ libsuricata_c_a_SOURCES = \
app-layer-krb5.c \
app-layer-modbus.c \
app-layer-quic.c \
app-layer-mqtt.c \
app-layer-nfs-tcp.c \
app-layer-nfs-udp.c \
app-layer-ntp.c \
Expand Down
64 changes: 0 additions & 64 deletions src/app-layer-mqtt.c

This file was deleted.

30 changes: 0 additions & 30 deletions src/app-layer-mqtt.h

This file was deleted.

3 changes: 1 addition & 2 deletions src/app-layer-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#include "app-layer-krb5.h"
#include "app-layer-sip.h"
#include "app-layer-rfb.h"
#include "app-layer-mqtt.h"
#include "app-layer-snmp.h"
#include "app-layer-quic.h"
#include "app-layer-rdp.h"
Expand Down Expand Up @@ -1766,7 +1765,7 @@ void AppLayerParserRegisterProtocolParsers(void)
RegisterQuicParsers();
rs_template_register_parser();
RegisterRFBParsers();
RegisterMQTTParsers();
SCMqttRegisterParser();
rs_pgsql_register_parser();
RegisterRdpParsers();
RegisterHTTP2Parsers();
Expand Down
1 change: 0 additions & 1 deletion src/output-json-mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "app-layer.h"
#include "app-layer-parser.h"

#include "app-layer-mqtt.h"
#include "output-json-mqtt.h"
#include "rust.h"

Expand Down

0 comments on commit f12e026

Please sign in to comment.