From dd66276f82c895c62e88c1e2fa0f1c8b4c748040 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Wed, 3 Jun 2026 22:02:19 +0200 Subject: [PATCH] rust: format telnet files Ticket: 3836 --- rust/src/telnet/mod.rs | 2 +- rust/src/telnet/parser.rs | 12 ++-- rust/src/telnet/telnet.rs | 116 ++++++++++++++++++-------------------- scripts/rustfmt.sh | 2 +- 4 files changed, 64 insertions(+), 68 deletions(-) diff --git a/rust/src/telnet/mod.rs b/rust/src/telnet/mod.rs index 38685c7954..6af7a8d6c3 100644 --- a/rust/src/telnet/mod.rs +++ b/rust/src/telnet/mod.rs @@ -17,5 +17,5 @@ //! Telnet application layer and parser module. -pub mod telnet; mod parser; +pub mod telnet; diff --git a/rust/src/telnet/parser.rs b/rust/src/telnet/parser.rs index 9393f9eee2..cb68ccb5c5 100644 --- a/rust/src/telnet/parser.rs +++ b/rust/src/telnet/parser.rs @@ -16,12 +16,12 @@ */ use crate::common::nom8::take_until_and_consume; -use nom8::combinator::peek; use nom8::bytes::complete::take; -use nom8::{IResult, Parser}; -use nom8::number::streaming::le_u8; use nom8::bytes::streaming::tag; -use nom8::bytes::streaming::{take_until}; +use nom8::bytes::streaming::take_until; +use nom8::combinator::peek; +use nom8::number::streaming::le_u8; +use nom8::{IResult, Parser}; pub fn peek_message_is_ctl(i: &[u8]) -> IResult<&[u8], bool> { let (i, v) = peek(le_u8).parse(i)?; @@ -33,11 +33,11 @@ pub enum TelnetMessageType<'a> { Data(&'a [u8]), } -pub fn parse_ctl_suboption<'a>(i: &'a[u8], full: &'a[u8]) -> IResult<&'a[u8], &'a[u8]> { +pub fn parse_ctl_suboption<'a>(i: &'a [u8], full: &'a [u8]) -> IResult<&'a [u8], &'a [u8]> { let (i, _sc) = le_u8(i)?; let tag: &[u8] = b"\xff\xf0"; let (i, x) = take_until(tag).parse(i)?; - let o = &full[..(x.len()+3)]; + let o = &full[..(x.len() + 3)]; Ok((i, o)) } diff --git a/rust/src/telnet/telnet.rs b/rust/src/telnet/telnet.rs index cc533acba9..71c90f8b51 100644 --- a/rust/src/telnet/telnet.rs +++ b/rust/src/telnet/telnet.rs @@ -15,18 +15,18 @@ * 02110-1301, USA. */ -use std; -use crate::core::{ALPROTO_UNKNOWN, IPPROTO_TCP}; +use super::parser; use crate::applayer::{self, *}; +use crate::core::{ALPROTO_UNKNOWN, IPPROTO_TCP}; use crate::flow::Flow; use crate::frames::*; -use std::ffi::CString; use nom8::IResult; +use std; +use std::ffi::CString; use suricata_sys::sys::{ AppLayerParserState, AppProto, SCAppLayerParserConfParserEnabled, SCAppLayerParserStateIssetFlag, SCAppLayerProtoDetectConfProtoDetectionEnabled, }; -use super::parser; static mut ALPROTO_TELNET: AppProto = ALPROTO_UNKNOWN; @@ -192,7 +192,7 @@ impl TelnetState { TelnetFrameType::Data as u8, None, ) - // app-layer-frame-documentation tag end: parse_request + // app-layer-frame-documentation tag end: parse_request }; self.request_specific_frame = f; } @@ -254,7 +254,9 @@ impl TelnetState { return AppLayerResult::ok(); } - fn parse_response(&mut self, flow: *mut Flow, stream_slice: &StreamSlice, input: &[u8]) -> AppLayerResult { + fn parse_response( + &mut self, flow: *mut Flow, stream_slice: &StreamSlice, input: &[u8], + ) -> AppLayerResult { // We're not interested in empty responses. if input.is_empty() { return AppLayerResult::ok(); @@ -274,14 +276,35 @@ impl TelnetState { let mut start = input; while !start.is_empty() { if self.response_frame.is_none() { - self.response_frame = Frame::new(flow, stream_slice, start, -1_i64, TelnetFrameType::Pdu as u8, None); + self.response_frame = Frame::new( + flow, + stream_slice, + start, + -1_i64, + TelnetFrameType::Pdu as u8, + None, + ); } if self.response_specific_frame.is_none() { if let Ok((_, is_ctl)) = parser::peek_message_is_ctl(start) { self.response_specific_frame = if is_ctl { - Frame::new(flow, stream_slice, start, -1_i64, TelnetFrameType::Ctl as u8, None) + Frame::new( + flow, + stream_slice, + start, + -1_i64, + TelnetFrameType::Ctl as u8, + None, + ) } else { - Frame::new(flow, stream_slice, start, -1_i64, TelnetFrameType::Data as u8, None) + Frame::new( + flow, + stream_slice, + start, + -1_i64, + TelnetFrameType::Data as u8, + None, + ) }; } } @@ -308,37 +331,34 @@ impl TelnetState { if let parser::TelnetMessageType::Data(d) = response { match self.state { - TelnetProtocolState::Idle | - TelnetProtocolState::AuthFail => { + TelnetProtocolState::Idle | TelnetProtocolState::AuthFail => { self.state = TelnetProtocolState::LoginSent; - }, + } TelnetProtocolState::LoginRecv => { self.state = TelnetProtocolState::PasswdSent; - }, + } TelnetProtocolState::PasswdRecv => { if let Ok(message) = std::str::from_utf8(d) { match message { "Login incorrect" => { SCLogDebug!("LOGIN FAILED"); self.state = TelnetProtocolState::AuthFail; - }, - "" => { - - }, + } + "" => {} &_ => { SCLogDebug!("LOGIN OK"); self.state = TelnetProtocolState::AuthOk; - }, + } } } - }, + } TelnetProtocolState::AuthOk => { let _message = std::str::from_utf8(d); if let Ok(_message) = _message { SCLogDebug!("<= {}", _message); } - }, - _ => {}, + } + _ => {} } } else if let parser::TelnetMessageType::Control(_c) = response { SCLogDebug!("response {:?}", _c); @@ -382,11 +402,7 @@ fn probe(input: &[u8]) -> IResult<&[u8], ()> { /// C entry point for a probing parser. unsafe extern "C" fn telnet_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() { @@ -399,7 +415,9 @@ unsafe extern "C" fn telnet_probing_parser( return ALPROTO_UNKNOWN; } -extern "C" fn telnet_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { +extern "C" fn telnet_state_new( + _orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto, +) -> *mut std::os::raw::c_void { let state = TelnetState::new(); let boxed = Box::new(state); return Box::into_raw(boxed) as *mut std::os::raw::c_void; @@ -409,20 +427,14 @@ unsafe extern "C" fn telnet_state_free(state: *mut std::os::raw::c_void) { std::mem::drop(Box::from_raw(state as *mut TelnetState)); } -unsafe extern "C" fn telnet_state_tx_free( - state: *mut std::os::raw::c_void, - tx_id: u64, -) { +unsafe extern "C" fn telnet_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, TelnetState); state.free_tx(tx_id); } unsafe extern "C" fn telnet_parse_request( - flow: *mut Flow, - state: *mut std::os::raw::c_void, - pstate: *mut AppLayerParserState, - stream_slice: StreamSlice, - _data: *mut std::os::raw::c_void + flow: *mut Flow, state: *mut std::os::raw::c_void, pstate: *mut AppLayerParserState, + stream_slice: StreamSlice, _data: *mut std::os::raw::c_void, ) -> AppLayerResult { let eof = SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0; @@ -445,11 +457,8 @@ unsafe extern "C" fn telnet_parse_request( } unsafe extern "C" fn telnet_parse_response( - flow: *mut Flow, - state: *mut std::os::raw::c_void, - pstate: *mut AppLayerParserState, - stream_slice: StreamSlice, - _data: *mut std::os::raw::c_void + flow: *mut Flow, state: *mut std::os::raw::c_void, pstate: *mut AppLayerParserState, + stream_slice: StreamSlice, _data: *mut std::os::raw::c_void, ) -> AppLayerResult { let _eof = SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0; let state = cast_pointer!(state, TelnetState); @@ -466,8 +475,7 @@ unsafe extern "C" fn telnet_parse_response( } unsafe extern "C" fn telnet_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, TelnetState); match state.get_tx(tx_id) { @@ -480,16 +488,13 @@ unsafe extern "C" fn telnet_state_get_tx( } } -unsafe extern "C" fn telnet_state_get_tx_count( - state: *mut std::os::raw::c_void, -) -> u64 { +unsafe extern "C" fn telnet_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, TelnetState); return state.tx_id; } unsafe extern "C" fn telnet_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, TelnetTransaction); // TODO @@ -524,7 +529,7 @@ pub unsafe extern "C" fn SCRegisterTelnetParser() { tx_comp_st_tc: 1, tx_get_progress: telnet_tx_get_alstate_progress, get_eventinfo: Some(TelnetEvent::get_event_info), - get_eventinfo_byid : Some(TelnetEvent::get_event_info_by_id), + get_eventinfo_byid: Some(TelnetEvent::get_event_info_by_id), localstorage_new: None, localstorage_free: None, get_tx_files: None, @@ -537,23 +542,14 @@ pub unsafe extern "C" fn SCRegisterTelnetParser() { get_frame_name_by_id: Some(TelnetFrameType::ffi_name_from_id), get_state_id_by_name: None, get_state_name_by_id: None, - }; let ip_proto_str = CString::new("tcp").unwrap(); - if SCAppLayerProtoDetectConfProtoDetectionEnabled( - ip_proto_str.as_ptr(), - parser.name, - ) != 0 - { + if SCAppLayerProtoDetectConfProtoDetectionEnabled(ip_proto_str.as_ptr(), parser.name) != 0 { let alproto = applayer_register_protocol_detection(&parser, 1); ALPROTO_TELNET = alproto; - if SCAppLayerParserConfParserEnabled( - ip_proto_str.as_ptr(), - parser.name, - ) != 0 - { + if SCAppLayerParserConfParserEnabled(ip_proto_str.as_ptr(), parser.name) != 0 { let _ = AppLayerRegisterParser(&parser, alproto); } SCLogDebug!("Rust telnet parser registered."); diff --git a/scripts/rustfmt.sh b/scripts/rustfmt.sh index bfc5584de7..8d08fbb32c 100755 --- a/scripts/rustfmt.sh +++ b/scripts/rustfmt.sh @@ -40,4 +40,4 @@ rustfmt --check rust/src/dns/*.rs rust/src/applayertemplate/*.rs rust/src/asn1/* rust/src/dhcp/*.rs rust/src/krb/*.rs rust/src/mdns/*.rs rust/src/pop3/*.rs \ rust/src/http2/*.rs rust/src/ike/*.rs rust/src/modbus/*.rs rust/src/mqtt/*.rs \ rust/src/nfs/*.rs rust/src/pgsql/*.rs rust/src/rdp/*.rs rust/src/sdp/*.rs \ - rust/src/sip/*.rs + rust/src/sip/*.rs rust/src/telnet/*.rs