]> git.ipfire.org Git - people/ms/suricata.git/commitdiff
applayerevent: derive get_event_info and get_event_info_by_id
authorJason Ish <jason.ish@oisf.net>
Mon, 19 Oct 2020 17:17:48 +0000 (11:17 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 1 Sep 2021 06:33:52 +0000 (08:33 +0200)
Add generation of wrapper functions for get_event_info
and get_event_info_by_id to the derive macro. Eliminates
the need for the wrapper method to be created by the parser
author.

rust/derive/src/applayerevent.rs
rust/src/applayer.rs

index 999fa2ea9cd93b3d9d0d934bcbea9aafe750fd82..88339d6dd844d98fc425034798cd022d1a6cce11 100644 (file)
@@ -70,6 +70,23 @@ pub fn derive_app_layer_event(input: TokenStream) -> TokenStream {
                     _ => None
                 }
             }
+
+            unsafe extern "C" fn get_event_info(
+                event_name: *const std::os::raw::c_char,
+                event_id: *mut std::os::raw::c_int,
+                event_type: *mut crate::core::AppLayerEventType,
+            ) -> std::os::raw::c_int {
+                crate::applayer::get_event_info::<#name>(event_name, event_id, event_type)
+            }
+
+            unsafe extern "C" fn get_event_info_by_id(
+                event_id: std::os::raw::c_int,
+                event_name: *mut *const std::os::raw::c_char,
+                event_type: *mut crate::core::AppLayerEventType,
+            ) -> i8 {
+                crate::applayer::get_event_info_by_id::<#name>(event_id, event_name, event_type)
+            }
+
         }
     };
 
index 008610dec2f8cf3498a5e8e11f16ea52849dae97..5568fb71d5a8763e50218bd250173445b510b077 100644 (file)
@@ -431,6 +431,18 @@ pub trait AppLayerEvent {
 
     /// Return the ID value of the enum variant.
     fn as_i32(&self) -> i32;
+
+    unsafe extern "C" fn get_event_info(
+        event_name: *const std::os::raw::c_char,
+        event_id: *mut std::os::raw::c_int,
+        event_type: *mut core::AppLayerEventType,
+    ) -> std::os::raw::c_int;
+
+    unsafe extern "C" fn get_event_info_by_id(
+        event_id: std::os::raw::c_int,
+        event_name: *mut *const std::os::raw::c_char,
+        event_type: *mut core::AppLayerEventType,
+    ) -> i8;
 }
 
 /// Generic `get_info_info` implementation for enums implementing
@@ -447,7 +459,7 @@ pub trait AppLayerEvent {
 ///
 /// get_event_info::<AppEvent>(...)
 #[inline(always)]
-pub fn get_event_info<T: AppLayerEvent>(
+pub unsafe fn get_event_info<T: AppLayerEvent>(
     event_name: *const std::os::raw::c_char,
     event_id: *mut std::os::raw::c_int,
     event_type: *mut core::AppLayerEventType,
@@ -456,30 +468,26 @@ pub fn get_event_info<T: AppLayerEvent>(
         return -1;
     }
 
-    unsafe {
-        let event = match CStr::from_ptr(event_name).to_str().map(T::from_string) {
-            Ok(Some(event)) => event.as_i32(),
-            _ => -1,
-        };
-        *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION;
-        *event_id = event as std::os::raw::c_int;
-        return 0;
-    }
+    let event = match CStr::from_ptr(event_name).to_str().map(T::from_string) {
+        Ok(Some(event)) => event.as_i32(),
+        _ => -1,
+    };
+    *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION;
+    *event_id = event as std::os::raw::c_int;
+    return 0;
 }
 
 /// Generic `get_info_info_by_id` implementation for enums implementing
 /// AppLayerEvent.
 #[inline(always)]
-pub fn get_event_info_by_id<T: AppLayerEvent>(
+pub unsafe fn get_event_info_by_id<T: AppLayerEvent>(
     event_id: std::os::raw::c_int,
     event_name: *mut *const std::os::raw::c_char,
     event_type: *mut core::AppLayerEventType,
 ) -> i8 {
     if let Some(e) = T::from_id(event_id as i32) {
-        unsafe {
-            *event_name = e.to_cstring().as_ptr() as *const std::os::raw::c_char;
-            *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION;
-        }
+        *event_name = e.to_cstring().as_ptr() as *const std::os::raw::c_char;
+        *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION;
         return 0;
     }
     return -1;