From: Jason Ish Date: Thu, 17 Nov 2022 06:28:59 +0000 (-0600) Subject: rust/app-layer-template: rustfmt X-Git-Tag: suricata-7.0.0-rc1~308 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38321a213f9502f2c7e58953e9560e974c31d3fe;p=thirdparty%2Fsuricata.git rust/app-layer-template: rustfmt --- diff --git a/rust/src/applayertemplate/logger.rs b/rust/src/applayertemplate/logger.rs index ef4b616f0e..0105526fc0 100644 --- a/rust/src/applayertemplate/logger.rs +++ b/rust/src/applayertemplate/logger.rs @@ -15,9 +15,9 @@ * 02110-1301, USA. */ -use std; -use crate::jsonbuilder::{JsonBuilder, JsonError}; use super::template::TemplateTransaction; +use crate::jsonbuilder::{JsonBuilder, JsonError}; +use std; fn log_template(tx: &TemplateTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { if let Some(ref request) = tx.request { @@ -30,7 +30,9 @@ fn log_template(tx: &TemplateTransaction, js: &mut JsonBuilder) -> Result<(), Js } #[no_mangle] -pub unsafe extern "C" fn rs_template_logger_log(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { +pub unsafe extern "C" fn rs_template_logger_log( + tx: *mut std::os::raw::c_void, js: &mut JsonBuilder, +) -> bool { let tx = cast_pointer!(tx, TemplateTransaction); log_template(tx, js).is_ok() } diff --git a/rust/src/applayertemplate/mod.rs b/rust/src/applayertemplate/mod.rs index d78388f720..63f4ed139e 100644 --- a/rust/src/applayertemplate/mod.rs +++ b/rust/src/applayertemplate/mod.rs @@ -15,8 +15,8 @@ * 02110-1301, USA. */ -pub mod template; mod parser; +pub mod template; /* TEMPLATE_START_REMOVE */ pub mod logger; /* TEMPLATE_END_REMOVE */ diff --git a/rust/src/applayertemplate/parser.rs b/rust/src/applayertemplate/parser.rs index d781e9891d..dc096eb0c6 100644 --- a/rust/src/applayertemplate/parser.rs +++ b/rust/src/applayertemplate/parser.rs @@ -56,11 +56,9 @@ mod tests { Err(Err::Incomplete(_)) => { panic!("Result should not have been incomplete."); } - Err(Err::Error(err)) | - Err(Err::Failure(err)) => { + Err(Err::Error(err)) | Err(Err::Failure(err)) => { panic!("Result should not be an error: {:?}.", err); } } } - } diff --git a/rust/src/applayertemplate/template.rs b/rust/src/applayertemplate/template.rs index 317f2e7ade..cd3bd8803f 100644 --- a/rust/src/applayertemplate/template.rs +++ b/rust/src/applayertemplate/template.rs @@ -15,13 +15,13 @@ * 02110-1301, USA. */ +use super::parser; +use crate::applayer::{self, *}; +use crate::core::{AppProto, Flow, ALPROTO_UNKNOWN, IPPROTO_TCP}; +use nom7 as nom; use std; use std::collections::VecDeque; -use crate::core::{ALPROTO_UNKNOWN, AppProto, Flow, IPPROTO_TCP}; -use crate::applayer::{self, *}; use std::ffi::CString; -use nom7 as nom; -use super::parser; static mut ALPROTO_TEMPLATE: AppProto = ALPROTO_UNKNOWN; @@ -154,7 +154,7 @@ impl TemplateState { let mut tx = self.new_tx(); tx.request = Some(request); self.transactions.push_back(tx); - }, + } Err(nom::Err::Incomplete(_)) => { // Not enough data. This parser doesn't give us a good indication // of how much data is missing so just ask for one more byte so the @@ -162,10 +162,10 @@ impl TemplateState { let consumed = input.len() - start.len(); let needed = start.len() + 1; return AppLayerResult::incomplete(consumed as u32, needed as u32); - }, + } Err(_) => { return AppLayerResult::err(); - }, + } } } @@ -250,11 +250,7 @@ fn probe(input: &[u8]) -> nom::IResult<&[u8], ()> { /// C entry point for a probing parser. #[no_mangle] pub unsafe extern "C" fn rs_template_probing_parser( - _flow: *const Flow, - _direction: u8, - input: *const u8, - input_len: u32, - _rdir: *mut u8 + _flow: *const Flow, _direction: u8, input: *const u8, input_len: u32, _rdir: *mut u8, ) -> AppProto { // Need at least 2 bytes. if input_len > 1 && !input.is_null() { @@ -267,7 +263,9 @@ pub unsafe extern "C" fn rs_template_probing_parser( } #[no_mangle] -pub extern "C" fn rs_template_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { +pub extern "C" fn rs_template_state_new( + _orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto, +) -> *mut std::os::raw::c_void { let state = TemplateState::new(); let boxed = Box::new(state); return Box::into_raw(boxed) as *mut std::os::raw::c_void; @@ -279,21 +277,15 @@ pub unsafe extern "C" fn rs_template_state_free(state: *mut std::os::raw::c_void } #[no_mangle] -pub unsafe extern "C" fn rs_template_state_tx_free( - state: *mut std::os::raw::c_void, - tx_id: u64, -) { +pub unsafe extern "C" fn rs_template_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, TemplateState); state.free_tx(tx_id); } #[no_mangle] pub unsafe extern "C" fn rs_template_parse_request( - _flow: *const Flow, - state: *mut std::os::raw::c_void, - pstate: *mut std::os::raw::c_void, - stream_slice: StreamSlice, - _data: *const std::os::raw::c_void + _flow: *const Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, + stream_slice: StreamSlice, _data: *const std::os::raw::c_void, ) -> AppLayerResult { let eof = AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0; @@ -317,11 +309,8 @@ pub unsafe extern "C" fn rs_template_parse_request( #[no_mangle] pub unsafe extern "C" fn rs_template_parse_response( - _flow: *const Flow, - state: *mut std::os::raw::c_void, - pstate: *mut std::os::raw::c_void, - stream_slice: StreamSlice, - _data: *const std::os::raw::c_void + _flow: *const Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, + stream_slice: StreamSlice, _data: *const std::os::raw::c_void, ) -> AppLayerResult { let _eof = AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0; let state = cast_pointer!(state, TemplateState); @@ -339,8 +328,7 @@ pub unsafe extern "C" fn rs_template_parse_response( #[no_mangle] pub unsafe extern "C" fn rs_template_state_get_tx( - state: *mut std::os::raw::c_void, - tx_id: u64, + state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, TemplateState); match state.get_tx(tx_id) { @@ -354,17 +342,14 @@ pub unsafe extern "C" fn rs_template_state_get_tx( } #[no_mangle] -pub unsafe extern "C" fn rs_template_state_get_tx_count( - state: *mut std::os::raw::c_void, -) -> u64 { +pub unsafe extern "C" fn rs_template_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, TemplateState); return state.tx_id; } #[no_mangle] pub unsafe extern "C" fn rs_template_tx_get_alstate_progress( - tx: *mut std::os::raw::c_void, - _direction: u8, + tx: *mut std::os::raw::c_void, _direction: u8, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, TemplateTransaction); @@ -381,11 +366,8 @@ pub unsafe extern "C" fn rs_template_tx_get_alstate_progress( /// pointer to the request buffer from C for detection. #[no_mangle] pub unsafe extern "C" fn rs_template_get_request_buffer( - tx: *mut std::os::raw::c_void, - buf: *mut *const u8, - len: *mut u32, -) -> u8 -{ + tx: *mut std::os::raw::c_void, buf: *mut *const u8, len: *mut u32, +) -> u8 { let tx = cast_pointer!(tx, TemplateTransaction); if let Some(ref request) = tx.request { if !request.is_empty() { @@ -400,11 +382,8 @@ pub unsafe extern "C" fn rs_template_get_request_buffer( /// Get the response buffer for a transaction from C. #[no_mangle] pub unsafe extern "C" fn rs_template_get_response_buffer( - tx: *mut std::os::raw::c_void, - buf: *mut *const u8, - len: *mut u32, -) -> u8 -{ + tx: *mut std::os::raw::c_void, buf: *mut *const u8, len: *mut u32, +) -> u8 { let tx = cast_pointer!(tx, TemplateTransaction); if let Some(ref response) = tx.response { if !response.is_empty() { @@ -450,11 +429,13 @@ pub unsafe extern "C" fn rs_template_register_parser() { tx_comp_st_tc: 1, tx_get_progress: rs_template_tx_get_alstate_progress, get_eventinfo: Some(TemplateEvent::get_event_info), - get_eventinfo_byid : Some(TemplateEvent::get_event_info_by_id), + get_eventinfo_byid: Some(TemplateEvent::get_event_info_by_id), localstorage_new: None, localstorage_free: None, get_tx_files: None, - get_tx_iterator: Some(applayer::state_get_tx_iterator::), + get_tx_iterator: Some( + applayer::state_get_tx_iterator::, + ), get_tx_data: rs_template_get_tx_data, get_state_data: rs_template_get_state_data, apply_tx_config: None, @@ -466,18 +447,10 @@ pub unsafe extern "C" fn rs_template_register_parser() { let ip_proto_str = CString::new("tcp").unwrap(); - if AppLayerProtoDetectConfProtoDetectionEnabled( - ip_proto_str.as_ptr(), - parser.name, - ) != 0 - { + if AppLayerProtoDetectConfProtoDetectionEnabled(ip_proto_str.as_ptr(), parser.name) != 0 { let alproto = AppLayerRegisterProtocolDetection(&parser, 1); ALPROTO_TEMPLATE = alproto; - if AppLayerParserConfParserEnabled( - ip_proto_str.as_ptr(), - parser.name, - ) != 0 - { + if AppLayerParserConfParserEnabled(ip_proto_str.as_ptr(), parser.name) != 0 { let _ = AppLayerRegisterParser(&parser, alproto); } SCLogNotice!("Rust template parser registered."); @@ -504,20 +477,55 @@ mod test { let buf = b"5:Hello3:bye"; let r = state.parse_request(&buf[0..0]); - assert_eq!(r, AppLayerResult{ status: 0, consumed: 0, needed: 0}); + assert_eq!( + r, + AppLayerResult { + status: 0, + consumed: 0, + needed: 0 + } + ); let r = state.parse_request(&buf[0..1]); - assert_eq!(r, AppLayerResult{ status: 1, consumed: 0, needed: 2}); + assert_eq!( + r, + AppLayerResult { + status: 1, + consumed: 0, + needed: 2 + } + ); let r = state.parse_request(&buf[0..2]); - assert_eq!(r, AppLayerResult{ status: 1, consumed: 0, needed: 3}); + assert_eq!( + r, + AppLayerResult { + status: 1, + consumed: 0, + needed: 3 + } + ); // This is the first message and only the first message. let r = state.parse_request(&buf[0..7]); - assert_eq!(r, AppLayerResult{ status: 0, consumed: 0, needed: 0}); + assert_eq!( + r, + AppLayerResult { + status: 0, + consumed: 0, + needed: 0 + } + ); // The first message and a portion of the second. let r = state.parse_request(&buf[0..9]); - assert_eq!(r, AppLayerResult{ status: 1, consumed: 7, needed: 3}); + assert_eq!( + r, + AppLayerResult { + status: 1, + consumed: 7, + needed: 3 + } + ); } }