/src/suricata8/rust/htp/src/c_api/log.rs
Line | Count | Source |
1 | | #![deny(missing_docs)] |
2 | | use crate::log::{HtpLogCode, Log}; |
3 | | use std::{ffi::CString, os::raw::c_char}; |
4 | | |
5 | | /// Get the log's message string |
6 | | /// |
7 | | /// Returns the log message as a cstring or NULL on error |
8 | | /// The caller must free this result with htp_free_cstring |
9 | | /// # Safety |
10 | | /// When calling this method, you have to ensure that log is either properly initialized or NULL |
11 | | #[no_mangle] |
12 | 801k | pub unsafe extern "C" fn htp_log_message(log: *const Log) -> *mut c_char { |
13 | 801k | log.as_ref() |
14 | 801k | .and_then(|log| CString::new(log.msg.msg.clone()).ok()) |
15 | 801k | .map(|msg| msg.into_raw()) |
16 | 801k | .unwrap_or(std::ptr::null_mut()) |
17 | 801k | } |
18 | | |
19 | | /// Get a log's message code |
20 | | /// |
21 | | /// Returns a code or HTP_LOG_CODE_ERROR on error |
22 | | /// # Safety |
23 | | /// When calling this method, you have to ensure that log is either properly initialized or NULL |
24 | | #[no_mangle] |
25 | 801k | pub unsafe extern "C" fn htp_log_code(log: *const Log) -> HtpLogCode { |
26 | 801k | log.as_ref() |
27 | 801k | .map(|log| log.msg.code) |
28 | 801k | .unwrap_or(HtpLogCode::ERROR) |
29 | 801k | } |
30 | | |
31 | | /// Free log |
32 | | /// # Safety |
33 | | /// This function is unsafe because improper use may lead to memory problems. For example, a double-free may occur if the function is called twice on the same raw pointer. |
34 | | #[no_mangle] |
35 | 801k | pub unsafe extern "C" fn htp_log_free(log: *mut Log) { |
36 | 801k | if !log.is_null() { |
37 | 801k | drop(Box::from_raw(log)); |
38 | 801k | } |
39 | 801k | } |