diff --git a/rust/src/bittorrent_dht/logger.rs b/rust/src/bittorrent_dht/logger.rs index 74ea7c59ba57..b4ec5af9f4a3 100644 --- a/rust/src/bittorrent_dht/logger.rs +++ b/rust/src/bittorrent_dht/logger.rs @@ -92,7 +92,7 @@ fn log_bittorrent_dht( js.start_object()?; js.set_hex("id", &node.id)?; js.set_string("ip", &print_ip_addr(&node.ip))?; - js.set_uint("port", node.port.into())?; + js.set_uint("port", node.port)?; js.close()?; } js.close()?; @@ -105,7 +105,7 @@ fn log_bittorrent_dht( js.start_object()?; js.set_hex("id", &node.id)?; js.set_string("ip", &print_ip_addr(&node.ip))?; - js.set_uint("port", node.port.into())?; + js.set_uint("port", node.port)?; js.close()?; } js.close()?; @@ -116,7 +116,7 @@ fn log_bittorrent_dht( for value in values { js.start_object()?; js.set_string("ip", &print_ip_addr(&value.ip))?; - js.set_uint("port", value.port.into())?; + js.set_uint("port", value.port)?; js.close()?; } js.close()?; diff --git a/rust/src/detect/tojson/mod.rs b/rust/src/detect/tojson/mod.rs index 1a1f0cee8028..747a1b398b11 100644 --- a/rust/src/detect/tojson/mod.rs +++ b/rust/src/detect/tojson/mod.rs @@ -24,47 +24,49 @@ pub fn detect_uint_to_json( where u64: From, { + let arg1: u64 = du.arg1.into(); + let arg2: u64 = du.arg2.into(); match du.mode { DetectUintMode::DetectUintModeEqual => { - js.set_uint("equal", du.arg1.into())?; + js.set_uint("equal", arg1)?; } DetectUintMode::DetectUintModeNe => { - js.set_uint("diff", du.arg1.into())?; + js.set_uint("diff", arg1)?; } DetectUintMode::DetectUintModeLt => { - js.set_uint("lt", du.arg1.into())?; + js.set_uint("lt", arg1)?; } DetectUintMode::DetectUintModeLte => { - js.set_uint("lte", du.arg1.into())?; + js.set_uint("lte", arg1)?; } DetectUintMode::DetectUintModeGt => { - js.set_uint("gt", du.arg1.into())?; + js.set_uint("gt", arg1)?; } DetectUintMode::DetectUintModeGte => { - js.set_uint("gte", du.arg1.into())?; + js.set_uint("gte", arg1)?; } DetectUintMode::DetectUintModeRange => { js.open_object("range")?; - js.set_uint("min", du.arg1.into())?; - js.set_uint("max", du.arg2.into())?; + js.set_uint("min", arg1)?; + js.set_uint("max", arg2)?; js.close()?; } DetectUintMode::DetectUintModeNegRg => { js.open_object("negated_range")?; - js.set_uint("min", du.arg1.into())?; - js.set_uint("max", du.arg2.into())?; + js.set_uint("min", arg1)?; + js.set_uint("max", arg2)?; js.close()?; } DetectUintMode::DetectUintModeBitmask => { js.open_object("bitmask")?; - js.set_uint("mask", du.arg1.into())?; - js.set_uint("value", du.arg2.into())?; + js.set_uint("mask", arg1)?; + js.set_uint("value", arg2)?; js.close()?; } DetectUintMode::DetectUintModeNegBitmask => { js.open_object("negated_bitmask")?; - js.set_uint("mask", du.arg1.into())?; - js.set_uint("value", du.arg2.into())?; + js.set_uint("mask", arg1)?; + js.set_uint("value", arg2)?; js.close()?; } } @@ -83,4 +85,4 @@ pub unsafe extern "C" fn SCDetectU32ToJson( js: &mut JsonBuilder, du: &DetectUintData, ) -> bool { return detect_uint_to_json(js, du).is_ok(); -} \ No newline at end of file +} diff --git a/rust/src/enip/logger.rs b/rust/src/enip/logger.rs index e3f85a47a06c..57f8d2aaf0ab 100644 --- a/rust/src/enip/logger.rs +++ b/rust/src/enip/logger.rs @@ -37,7 +37,7 @@ fn log_enip_header(h: &EnipHeader, js: &mut JsonBuilder) -> Result<(), JsonError js.set_string("status", &format!("unknown-{}", h.status))?; } if h.options != 0 { - js.set_uint("options", h.options.into())?; + js.set_uint("options", h.options)?; } Ok(()) } @@ -1707,7 +1707,7 @@ fn log_cip_path_segment(c: &EnipCipPathSegment) -> Result Result<(), JsonError> } EnipPayload::RegisterSession(rs) => { js.open_object("register_session")?; - js.set_uint("protocol_version", rs.protocol_version.into())?; - js.set_uint("options", rs.options.into())?; + js.set_uint("protocol_version", rs.protocol_version)?; + js.set_uint("options", rs.options)?; js.close()?; } _ => {} @@ -1833,8 +1833,8 @@ fn log_enip(tx: &EnipTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> match &response.payload { EnipPayload::RegisterSession(rs) => { js.open_object("register_session")?; - js.set_uint("protocol_version", rs.protocol_version.into())?; - js.set_uint("options", rs.options.into())?; + js.set_uint("protocol_version", rs.protocol_version)?; + js.set_uint("options", rs.options)?; js.close()?; } EnipPayload::Cip(cip) => { @@ -1843,8 +1843,8 @@ fn log_enip(tx: &EnipTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> EnipPayload::ListServices(lsp) if !lsp.is_empty() => { if let EnipItemPayload::Services(ls) = &lsp[0].payload { js.open_object("list_services")?; - js.set_uint("protocol_version", ls.protocol_version.into())?; - js.set_uint("capabilities", ls.capabilities.into())?; + js.set_uint("protocol_version", ls.protocol_version)?; + js.set_uint("capabilities", ls.capabilities)?; js.set_string("service_name", &String::from_utf8_lossy(&ls.service_name))?; js.close()?; } @@ -1852,7 +1852,7 @@ fn log_enip(tx: &EnipTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> EnipPayload::ListIdentity(lip) if !lip.is_empty() => { if let EnipItemPayload::Identity(li) = &lip[0].payload { js.open_object("identity")?; - js.set_uint("protocol_version", li.protocol_version.into())?; + js.set_uint("protocol_version", li.protocol_version)?; js.set_string( "revision", &format!("{}.{}", li.revision_major, li.revision_minor), @@ -1873,11 +1873,11 @@ fn log_enip(tx: &EnipTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> js.set_string("device_type", &format!("unknown-{}", li.device_type))?; } } - js.set_uint("product_code", li.product_code.into())?; - js.set_uint("status", li.status.into())?; - js.set_uint("serial", li.serial.into())?; + js.set_uint("product_code", li.product_code)?; + js.set_uint("status", li.status)?; + js.set_uint("serial", li.serial)?; js.set_string("product_name", &String::from_utf8_lossy(&li.product_name))?; - js.set_uint("state", li.state.into())?; + js.set_uint("state", li.state)?; js.close()?; } } diff --git a/rust/src/jsonbuilder.rs b/rust/src/jsonbuilder.rs index 56fe91fcbd93..0d9bc916a770 100644 --- a/rust/src/jsonbuilder.rs +++ b/rust/src/jsonbuilder.rs @@ -19,12 +19,13 @@ #![allow(clippy::missing_safety_doc)] +use base64::{engine::general_purpose::STANDARD, Engine}; +use num_traits::Unsigned; use std::cmp::max; use std::collections::TryReserveError; use std::ffi::CStr; use std::os::raw::c_char; use std::str::Utf8Error; -use base64::{Engine, engine::general_purpose::STANDARD}; const INIT_SIZE: usize = 4096; @@ -637,7 +638,11 @@ impl JsonBuilder { } /// Set a key and an unsigned integer type on an object. - pub fn set_uint(&mut self, key: &str, val: u64) -> Result<&mut Self, JsonError> { + pub fn set_uint(&mut self, key: &str, val: T) -> Result<&mut Self, JsonError> + where + T: Unsigned + Into, + { + let val: u64 = val.into(); match self.current_state() { State::ObjectNth => { self.push(',')?; @@ -1204,9 +1209,11 @@ mod test { assert_eq!(js.current_state(), State::ObjectNth); assert_eq!(js.buf, r#"{"one":"one","two":"two""#); + js.set_uint("three", 3u8)?; + js.close()?; assert_eq!(js.current_state(), State::None); - assert_eq!(js.buf, r#"{"one":"one","two":"two"}"#); + assert_eq!(js.buf, r#"{"one":"one","two":"two","three":3}"#); Ok(()) } diff --git a/rust/src/ldap/logger.rs b/rust/src/ldap/logger.rs index f126f97ef231..d57f8d80527f 100644 --- a/rust/src/ldap/logger.rs +++ b/rust/src/ldap/logger.rs @@ -28,7 +28,7 @@ fn log_ldap(tx: &LdapTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> if let Some(req) = &tx.request { let protocol_op_str = req.protocol_op.to_string(); js.open_object("request")?; - js.set_uint("message_id", req.message_id.0.into())?; + js.set_uint("message_id", req.message_id.0)?; js.set_string("operation", &protocol_op_str)?; match &req.protocol_op { @@ -59,7 +59,7 @@ fn log_ldap(tx: &LdapTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> js.set_string("operation", &protocol_op_str)?; if tx.request.is_none() { - js.set_uint("message_id", response.message_id.0.into())?; + js.set_uint("message_id", response.message_id.0)?; } match &response.protocol_op { @@ -88,10 +88,10 @@ fn log_ldap(tx: &LdapTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> fn log_search_request(msg: &SearchRequest, js: &mut JsonBuilder) -> Result<(), JsonError> { js.open_object("search_request")?; js.set_string("base_object", &msg.base_object.0)?; - js.set_uint("scope", msg.scope.0.into())?; - js.set_uint("deref_alias", msg.deref_aliases.0.into())?; - js.set_uint("size_limit", msg.size_limit.into())?; - js.set_uint("time_limit", msg.time_limit.into())?; + js.set_uint("scope", msg.scope.0)?; + js.set_uint("deref_alias", msg.deref_aliases.0)?; + js.set_uint("size_limit", msg.size_limit)?; + js.set_uint("time_limit", msg.time_limit)?; js.set_bool("types_only", msg.types_only)?; if let Filter::Present(val) = &msg.filter { js.open_object("filter")?; @@ -113,7 +113,7 @@ fn log_search_request(msg: &SearchRequest, js: &mut JsonBuilder) -> Result<(), J fn log_bind_request(msg: &BindRequest, js: &mut JsonBuilder) -> Result<(), JsonError> { js.open_object("bind_request")?; - js.set_uint("version", msg.version.into())?; + js.set_uint("version", msg.version)?; js.set_string("name", &msg.name.0)?; if let AuthenticationChoice::Sasl(sasl) = &msg.authentication { js.open_object("sasl")?; diff --git a/rust/src/modbus/log.rs b/rust/src/modbus/log.rs index 6724291de786..24e703c245f7 100644 --- a/rust/src/modbus/log.rs +++ b/rust/src/modbus/log.rs @@ -47,10 +47,10 @@ fn log(tx: &ModbusTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { } fn log_message(msg: &Message, js: &mut JsonBuilder) -> Result<(), JsonError> { - js.set_uint("transaction_id", msg.transaction_id.into())?; - js.set_uint("protocol_id", msg.protocol_id.into())?; - js.set_uint("unit_id", msg.unit_id.into())?; - js.set_uint("function_raw", msg.function.raw.into())?; + js.set_uint("transaction_id", msg.transaction_id)?; + js.set_uint("protocol_id", msg.protocol_id)?; + js.set_uint("unit_id", msg.unit_id)?; + js.set_uint("function_raw", msg.function.raw)?; js.set_string("function_code", &msg.function.code.to_string())?; js.set_string("access_type", &msg.access_type.to_string())?; js.set_string("category", &msg.category.to_string())?; @@ -59,20 +59,20 @@ fn log_message(msg: &Message, js: &mut JsonBuilder) -> Result<(), JsonError> { match &msg.data { Data::Exception(exc) => { js.open_object("exception")?; - js.set_uint("raw", exc.raw.into())?; + js.set_uint("raw", exc.raw)?; js.set_string("code", &exc.code.to_string())?; js.close()?; } Data::Diagnostic { func, data } => { js.open_object("diagnostic")?; - js.set_uint("raw", func.raw.into())?; + js.set_uint("raw", func.raw)?; js.set_string("code", &func.code.to_string())?; js.set_string_from_bytes("data", data)?; js.close()?; } Data::MEI { mei_type, data } => { js.open_object("mei")?; - js.set_uint("raw", mei_type.raw.into())?; + js.set_uint("raw", mei_type.raw)?; js.set_string("code", &mei_type.code.to_string())?; js.set_string_from_bytes("data", data)?; js.close()?; @@ -107,8 +107,8 @@ fn log_message(msg: &Message, js: &mut JsonBuilder) -> Result<(), JsonError> { fn log_read(read: &Read, js: &mut JsonBuilder) -> Result<(), JsonError> { match read { Read::Request { address, quantity } => { - js.set_uint("address", (*address).into())?; - js.set_uint("quantity", (*quantity).into())?; + js.set_uint("address", *address)?; + js.set_uint("quantity", *quantity)?; } Read::Response(data) => { js.set_string_from_bytes("data", data)?; @@ -125,8 +125,8 @@ fn log_write(write: &Write, js: &mut JsonBuilder) -> Result<(), JsonError> { quantity, data, } => { - js.set_uint("address", (*address).into())?; - js.set_uint("quantity", (*quantity).into())?; + js.set_uint("address", *address)?; + js.set_uint("quantity", *quantity)?; js.set_string_from_bytes("data", data)?; } Write::Mask { @@ -134,13 +134,13 @@ fn log_write(write: &Write, js: &mut JsonBuilder) -> Result<(), JsonError> { and_mask, or_mask, } => { - js.set_uint("address", (*address).into())?; - js.set_uint("and_mask", (*and_mask).into())?; - js.set_uint("or_mask", (*or_mask).into())?; + js.set_uint("address", *address)?; + js.set_uint("and_mask", *and_mask)?; + js.set_uint("or_mask", *or_mask)?; } Write::Other { address, data } => { - js.set_uint("address", (*address).into())?; - js.set_uint("data", (*data).into())?; + js.set_uint("address", *address)?; + js.set_uint("data", *data)?; } } diff --git a/rust/src/pgsql/logger.rs b/rust/src/pgsql/logger.rs index a306e6bee698..9d78ffdc318e 100644 --- a/rust/src/pgsql/logger.rs +++ b/rust/src/pgsql/logger.rs @@ -99,8 +99,8 @@ fn log_request(req: &PgsqlFEMessage, flags: u32) -> Result { js.set_string("message", "cancel_request")?; - js.set_uint("process_id", (*pid).into())?; - js.set_uint("secret_key", (*backend_key).into())?; + js.set_uint("process_id", *pid)?; + js.set_uint("secret_key", *backend_key)?; } PgsqlFEMessage::Terminate(TerminationMessage { identifier: _, @@ -214,8 +214,8 @@ fn log_response(res: &PgsqlBEMessage, jb: &mut JsonBuilder) -> Result<(), JsonEr backend_pid, secret_key, }) => { - jb.set_uint("process_id", (*backend_pid).into())?; - jb.set_uint("secret_key", (*secret_key).into())?; + jb.set_uint("process_id", *backend_pid)?; + jb.set_uint("secret_key", *secret_key)?; } PgsqlBEMessage::ReadyForQuery(ReadyForQueryMessage { identifier: _, @@ -230,7 +230,7 @@ fn log_response(res: &PgsqlBEMessage, jb: &mut JsonBuilder) -> Result<(), JsonEr field_count, fields: _, }) => { - jb.set_uint("field_count", (*field_count).into())?; + jb.set_uint("field_count", *field_count)?; } PgsqlBEMessage::ConsolidatedDataRow(ConsolidatedDataRowPacket { identifier: _, @@ -247,7 +247,7 @@ fn log_response(res: &PgsqlBEMessage, jb: &mut JsonBuilder) -> Result<(), JsonEr channel_name, payload, }) => { - jb.set_uint("pid", (*pid).into())?; + jb.set_uint("pid", *pid)?; jb.set_string_from_bytes("channel_name", channel_name)?; jb.set_string_from_bytes("payload", payload)?; } diff --git a/rust/src/quic/logger.rs b/rust/src/quic/logger.rs index 2bb92f3be01d..a8362c55d9c4 100644 --- a/rust/src/quic/logger.rs +++ b/rust/src/quic/logger.rs @@ -135,7 +135,7 @@ fn log_quic(tx: &QuicTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> if let Some(s) = quic_tls_extension_name(etype) { js.set_string("name", &s)?; } - js.set_uint("type", etype.into())?; + js.set_uint("type", etype)?; if !e.values.is_empty() { js.open_array("values")?; diff --git a/rust/src/smb/log.rs b/rust/src/smb/log.rs index e242d02e486b..01d3d9e13846 100644 --- a/rust/src/smb/log.rs +++ b/rust/src/smb/log.rs @@ -253,10 +253,10 @@ fn smb_common_header(jsb: &mut JsonBuilder, state: &SMBState, tx: &SMBTransactio jsb.set_string("server_guid", &guid_to_string(&x.server_guid))?; if state.max_read_size > 0 { - jsb.set_uint("max_read_size", state.max_read_size.into())?; + jsb.set_uint("max_read_size", state.max_read_size)?; } if state.max_write_size > 0 { - jsb.set_uint("max_write_size", state.max_write_size.into())?; + jsb.set_uint("max_write_size", state.max_write_size)?; } }, Some(SMBTransactionTypeData::TREECONNECT(ref x)) => { diff --git a/rust/src/websocket/logger.rs b/rust/src/websocket/logger.rs index 189d3ab7d9a6..09103f4167e7 100644 --- a/rust/src/websocket/logger.rs +++ b/rust/src/websocket/logger.rs @@ -27,7 +27,7 @@ fn log_websocket( js.open_object("websocket")?; js.set_bool("fin", tx.pdu.fin)?; if let Some(xorkey) = tx.pdu.mask { - js.set_uint("mask", xorkey.into())?; + js.set_uint("mask", xorkey)?; } if let Some(opcode) = WebSocketOpcode::from_u(tx.pdu.opcode) { js.set_string("opcode", opcode.to_str())?;