}
/// Set a key and a string value (from bytes) on an object, with a limited size
- pub fn set_string_from_bytes_limited(&mut self, key: &str, val: &[u8], limit: usize) -> Result<&mut Self, JsonError> {
+ pub fn set_string_from_bytes_limited(
+ &mut self, key: &str, val: &[u8], limit: usize,
+ ) -> Result<&mut Self, JsonError> {
let mut valtrunc = Vec::new();
let val = if val.len() > limit {
let additional_bytes = val.len() - limit;
}
fn push_float(&mut self, val: f64) -> Result<(), JsonError> {
- if val.is_nan() || val.is_infinite() {
- self.push_str("null")?;
- } else {
- self.push_str(&val.to_string())?;
- }
- Ok(())
+ if val.is_nan() || val.is_infinite() {
+ self.push_str("null")?;
+ } else {
+ self.push_str(&val.to_string())?;
+ }
+ Ok(())
}
/// Encode a string into the buffer, escaping as needed.
#[cfg(test)]
mod test {
use super::*;
+ use crate::core::Direction;
use crate::mqtt::mqtt::MQTTTransaction;
use crate::mqtt::mqtt_message::*;
use crate::mqtt::parser::FixedHeader;
- use crate::core::Direction;
use std;
#[test]
fn test_multi_unsubscribe() {
- let mut t = MQTTTransaction::new(MQTTMessage {
- header: FixedHeader {
- message_type: MQTTTypeCode::UNSUBSCRIBE,
- dup_flag: false,
- qos_level: 0,
- retain: false,
- remaining_length: 0,
+ let mut t = MQTTTransaction::new(
+ MQTTMessage {
+ header: FixedHeader {
+ message_type: MQTTTypeCode::UNSUBSCRIBE,
+ dup_flag: false,
+ qos_level: 0,
+ retain: false,
+ remaining_length: 0,
+ },
+ op: MQTTOperation::UNSUBSCRIBE(MQTTUnsubscribeData {
+ message_id: 1,
+ topics: vec!["foo".to_string(), "baar".to_string()],
+ properties: None,
+ }),
},
- op: MQTTOperation::UNSUBSCRIBE(MQTTUnsubscribeData {
- message_id: 1,
- topics: vec!["foo".to_string(), "baar".to_string()],
- properties: None,
- }),
- }, Direction::ToServer);
+ Direction::ToServer,
+ );
t.msg.push(MQTTMessage {
header: FixedHeader {
message_type: MQTTTypeCode::UNSUBSCRIBE,
#[test]
fn test_multi_subscribe() {
- let mut t = MQTTTransaction::new(MQTTMessage {
- header: FixedHeader {
- message_type: MQTTTypeCode::SUBSCRIBE,
- dup_flag: false,
- qos_level: 0,
- retain: false,
- remaining_length: 0,
+ let mut t = MQTTTransaction::new(
+ MQTTMessage {
+ header: FixedHeader {
+ message_type: MQTTTypeCode::SUBSCRIBE,
+ dup_flag: false,
+ qos_level: 0,
+ retain: false,
+ remaining_length: 0,
+ },
+ op: MQTTOperation::SUBSCRIBE(MQTTSubscribeData {
+ message_id: 1,
+ topics: vec![
+ MQTTSubscribeTopicData {
+ topic_name: "foo".to_string(),
+ qos: 0,
+ },
+ MQTTSubscribeTopicData {
+ topic_name: "baar".to_string(),
+ qos: 1,
+ },
+ ],
+ properties: None,
+ }),
},
- op: MQTTOperation::SUBSCRIBE(MQTTSubscribeData {
- message_id: 1,
- topics: vec![
- MQTTSubscribeTopicData {
- topic_name: "foo".to_string(),
- qos: 0,
- },
- MQTTSubscribeTopicData {
- topic_name: "baar".to_string(),
- qos: 1,
- },
- ],
- properties: None,
- }),
- }, Direction::ToServer);
+ Direction::ToServer,
+ );
t.msg.push(MQTTMessage {
header: FixedHeader {
message_type: MQTTTypeCode::SUBSCRIBE,
pub const MQTT_LOG_PASSWORDS: u32 = BIT_U32!(0);
#[inline]
-fn log_mqtt_topic(js: &mut JsonBuilder, t: &MQTTSubscribeTopicData, max_log_len: usize ) -> Result<(), JsonError> {
+fn log_mqtt_topic(
+ js: &mut JsonBuilder, t: &MQTTSubscribeTopicData, max_log_len: usize,
+) -> Result<(), JsonError> {
js.start_object()?;
js.set_string_limited("topic", &t.topic_name, max_log_len)?;
js.set_uint("qos", t.qos as u64)?;
connected: false,
skip_request: 0,
skip_response: 0,
- max_msg_len: unsafe { MAX_MSG_LEN},
+ max_msg_len: unsafe { MAX_MSG_LEN },
tx_index_completed: 0,
}
}
) {
let hdr = stream_slice.as_slice();
//MQTT payload has a fixed header of 2 bytes
- let _mqtt_hdr = Frame::new(flow, stream_slice, hdr, 2, MQTTFrameType::Header as u8, None);
+ let _mqtt_hdr = Frame::new(
+ flow,
+ stream_slice,
+ hdr,
+ 2,
+ MQTTFrameType::Header as u8,
+ None,
+ );
SCLogDebug!("mqtt_hdr Frame {:?}", _mqtt_hdr);
let rem_length = input.header.remaining_length as usize;
let data = &hdr[2..rem_length + 2];
#[inline]
fn parse_publish(
- protocol_version: u8,
- has_id: bool,
+ protocol_version: u8, has_id: bool,
) -> impl Fn(&[u8]) -> IResult<&[u8], MQTTPublishData> {
move |i: &[u8]| {
let (i, topic) = parse_mqtt_string(i)?;
#[inline]
fn parse_disconnect(
- remaining_len: usize,
- protocol_version: u8,
+ remaining_len: usize, protocol_version: u8,
) -> impl Fn(&[u8]) -> IResult<&[u8], MQTTDisconnectData> {
move |input: &[u8]| {
if protocol_version < 5 {
#[inline]
fn parse_remaining_message<'a>(
- full: &'a [u8],
- len: usize,
- skiplen: usize,
- header: FixedHeader,
- message_type: MQTTTypeCode,
+ full: &'a [u8], len: usize, skiplen: usize, header: FixedHeader, message_type: MQTTTypeCode,
protocol_version: u8,
) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], MQTTMessage> {
move |input: &'a [u8]| {
}
pub fn parse_message(
- input: &[u8],
- protocol_version: u8,
- max_msg_size: u32,
+ input: &[u8], protocol_version: u8, 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.
#[test]
fn test_parse_msgidonly_v5() {
let buf = [
- 0x00, 0x01, /* Message Identifier: 1 */
+ 0x00, 0x01, /* Message Identifier: 1 */
0x00, /* Reason Code: 0 */
0x00, /* Properties */
0x00, 0x61, 0x75, 0x74, 0x6f, 0x2d, 0x42, 0x34, 0x33, 0x45, 0x38, 0x30,