Skip to content

Instantly share code, notes, and snippets.

@jasonish
Last active January 3, 2024 17:22
Show Gist options
  • Save jasonish/43f6672af27fc541352285853cd64edc to your computer and use it in GitHub Desktop.
Save jasonish/43f6672af27fc541352285853cd64edc to your computer and use it in GitHub Desktop.
diff --git a/rust/derive/src/stringenum.rs b/rust/derive/src/stringenum.rs
index b8fa727c59..e49311bef8 100644
--- a/rust/derive/src/stringenum.rs
+++ b/rust/derive/src/stringenum.rs
@@ -89,21 +89,6 @@ pub fn derive_enum_string<T: std::str::FromStr + quote::ToTokens>(input: TokenSt
_ => None
}
}
- fn to_detect_ctx(s: &str) -> Option<DetectUintData<#utype_str>> {
- if let Ok((_, ctx)) = detect_parse_uint::<#utype_str>(s) {
- return Some(ctx);
- }
- if let Some(arg1) = #name::from_str(s) {
- let arg1 = #name::into_u(&arg1);
- let ctx = DetectUintData::<#utype_str> {
- arg1,
- arg2: 0,
- mode: DetectUintMode::DetectUintModeEqual,
- };
- return Some(ctx);
- }
- return None;
- }
}
};
diff --git a/rust/src/detect/mod.rs b/rust/src/detect/mod.rs
index 7c6b370f97..1bde318d2a 100644
--- a/rust/src/detect/mod.rs
+++ b/rust/src/detect/mod.rs
@@ -26,8 +26,6 @@ pub mod uint;
pub mod uri;
pub mod requires;
-use crate::detect::uint::DetectUintData;
-
/// Enum trait that will be implemented on enums that
/// derive StringEnum.
pub trait Enum<T> {
@@ -42,7 +40,4 @@ pub trait Enum<T> {
/// Get an enum variant from parsing a string.
fn from_str(s: &str) -> Option<Self> where Self: Sized;
-
- /// Get a detect context for integer keyword.
- fn to_detect_ctx(s: &str) -> Option<DetectUintData<T>>;
}
diff --git a/rust/src/detect/uint.rs b/rust/src/detect/uint.rs
index 7dc3d91748..b0d7c829f3 100644
--- a/rust/src/detect/uint.rs
+++ b/rust/src/detect/uint.rs
@@ -24,6 +24,9 @@ use nom7::Err;
use nom7::IResult;
use std::ffi::CStr;
+use std::str::FromStr;
+
+use super::Enum;
#[derive(PartialEq, Eq, Clone, Debug)]
#[repr(u8)]
@@ -47,6 +50,30 @@ pub struct DetectUintData<T> {
pub mode: DetectUintMode,
}
+impl<T: Clone> From<&dyn Enum<T>> for DetectUintData<T> {
+ fn from(value: &dyn Enum<T>) -> Self {
+ let arg = value.into_u();
+ let ctx = DetectUintData::<T> {
+ arg1: arg.clone(),
+ arg2: arg,
+ mode: DetectUintMode::DetectUintModeEqual,
+ };
+ ctx
+ }
+}
+
+impl<T: DetectIntType> FromStr for DetectUintData<T> {
+ type Err = ();
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ if let Ok((_, v)) = detect_parse_uint::<T>(s) {
+ Ok(v)
+ } else {
+ Err(())
+ }
+ }
+}
+
pub trait DetectIntType:
std::str::FromStr
+ std::cmp::PartialOrd
diff --git a/rust/src/websocket/detect.rs b/rust/src/websocket/detect.rs
index a665bde540..168f73b6f6 100644
--- a/rust/src/websocket/detect.rs
+++ b/rust/src/websocket/detect.rs
@@ -15,10 +15,10 @@
* 02110-1301, USA.
*/
+use super::parser::WebSocketOpcode;
use super::websocket::WebSocketTransaction;
-use crate::detect::uint::{detect_parse_uint, DetectUintData, DetectUintMode};
use crate::detect::Enum;
-use crate::websocket::parser::WebSocketOpcode;
+use crate::detect::uint::{detect_parse_uint, DetectUintData, DetectUintMode};
use nom7::branch::alt;
use nom7::bytes::complete::{is_a, tag};
@@ -64,10 +64,12 @@ pub unsafe extern "C" fn SCWebSocketParseOpcode(
) -> *mut DetectUintData<u8> {
let ft_name: &CStr = CStr::from_ptr(ustr); //unsafe
if let Ok(s) = ft_name.to_str() {
- if let Some(ctx) = WebSocketOpcode::to_detect_ctx(s) {
- let boxed = Box::new(ctx);
- return Box::into_raw(boxed) as *mut _;
- }
+ if let Some(opcode) = WebSocketOpcode::from_str(s) {
+ let opcode: &dyn Enum<u8> = &opcode;
+ let ctx: DetectUintData<u8> = opcode.into();
+ let boxed = Box::new(ctx);
+ return Box::into_raw(boxed) as *mut _;
+ }
}
return std::ptr::null_mut();
}
diff --git a/rust/src/websocket/parser.rs b/rust/src/websocket/parser.rs
index 9aa6f20c6e..dac2adde1f 100644
--- a/rust/src/websocket/parser.rs
+++ b/rust/src/websocket/parser.rs
@@ -15,7 +15,6 @@
* 02110-1301, USA.
*/
-use crate::detect::uint::{detect_parse_uint, DetectUintData, DetectUintMode};
use nom7::bytes::streaming::take;
use nom7::combinator::cond;
use nom7::number::streaming::{be_u16, be_u32, be_u64, be_u8};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment