sip: use pure rust function

For internal function that is not part of C FFI
pull/11829/head
Philippe Antoine 2 years ago committed by Victor Julien
parent c55c7d6c27
commit 521928e2a3

@ -24,8 +24,7 @@ use crate::detect::{
DetectSignatureSetAppProto, SCSigTableElmt, SIGMATCH_NOOPT, DetectSignatureSetAppProto, SCSigTableElmt, SIGMATCH_NOOPT,
}; };
use crate::sip::sip::{SIPTransaction, ALPROTO_SIP}; use crate::sip::sip::{SIPTransaction, ALPROTO_SIP};
use std::ffi::CStr; use std::os::raw::{c_int, c_void};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr; use std::ptr;
static mut G_SIP_PROTOCOL_BUFFER_ID: c_int = 0; static mut G_SIP_PROTOCOL_BUFFER_ID: c_int = 0;
@ -309,31 +308,22 @@ unsafe extern "C" fn sip_response_line_get_data(
return false; return false;
} }
unsafe extern "C" fn sip_get_header_value( fn sip_get_header_value<'a>(
tx: *const c_void, i: u32, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32, tx: &'a SIPTransaction, i: u32, direction: Direction, s: &str,
strname: *const c_char, ) -> Option<&'a str> {
) -> bool { let headers = match direction {
let tx = cast_pointer!(tx, SIPTransaction); Direction::ToServer => tx.request.as_ref().map(|r| &r.headers),
let hname: &CStr = CStr::from_ptr(strname); Direction::ToClient => tx.response.as_ref().map(|r| &r.headers),
if let Ok(s) = hname.to_str() { };
let headers = match direction.into() { if let Some(headers) = headers {
Direction::ToServer => tx.request.as_ref().map(|r| &r.headers), if let Some(header_vals) = headers.get(s) {
Direction::ToClient => tx.response.as_ref().map(|r| &r.headers), if (i as usize) < header_vals.len() {
}; let value = &header_vals[i as usize];
if let Some(headers) = headers { return Some(value);
if let Some(header_vals) = headers.get(s) {
if (i as usize) < header_vals.len() {
let value = &header_vals[i as usize];
*buffer = value.as_ptr();
*buffer_len = value.len() as u32;
return true;
}
} }
} }
} }
*buffer = ptr::null(); return None;
*buffer_len = 0;
return false;
} }
unsafe extern "C" fn sip_from_hdr_setup( unsafe extern "C" fn sip_from_hdr_setup(
@ -367,14 +357,15 @@ unsafe extern "C" fn sip_from_hdr_get(
unsafe extern "C" fn sip_from_hdr_get_data( unsafe extern "C" fn sip_from_hdr_get_data(
tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32, tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool { ) -> bool {
sip_get_header_value( let tx = cast_pointer!(tx, SIPTransaction);
tx, if let Some(value) = sip_get_header_value(tx, local_id, flow_flags.into(), "From") {
local_id, *buffer = value.as_ptr();
flow_flags, *buffer_len = value.len() as u32;
buffer, return true;
buffer_len, }
"From\0".as_ptr() as *const c_char, *buffer = ptr::null();
) *buffer_len = 0;
return false;
} }
unsafe extern "C" fn sip_to_hdr_setup( unsafe extern "C" fn sip_to_hdr_setup(
@ -408,14 +399,15 @@ unsafe extern "C" fn sip_to_hdr_get(
unsafe extern "C" fn sip_to_hdr_get_data( unsafe extern "C" fn sip_to_hdr_get_data(
tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32, tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool { ) -> bool {
sip_get_header_value( let tx = cast_pointer!(tx, SIPTransaction);
tx, if let Some(value) = sip_get_header_value(tx, local_id, flow_flags.into(), "To") {
local_id, *buffer = value.as_ptr();
flow_flags, *buffer_len = value.len() as u32;
buffer, return true;
buffer_len, }
"To\0".as_ptr() as *const c_char, *buffer = ptr::null();
) *buffer_len = 0;
return false;
} }
unsafe extern "C" fn sip_via_hdr_setup( unsafe extern "C" fn sip_via_hdr_setup(
@ -449,14 +441,15 @@ unsafe extern "C" fn sip_via_hdr_get(
unsafe extern "C" fn sip_via_hdr_get_data( unsafe extern "C" fn sip_via_hdr_get_data(
tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32, tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool { ) -> bool {
sip_get_header_value( let tx = cast_pointer!(tx, SIPTransaction);
tx, if let Some(value) = sip_get_header_value(tx, local_id, flow_flags.into(), "Via") {
local_id, *buffer = value.as_ptr();
flow_flags, *buffer_len = value.len() as u32;
buffer, return true;
buffer_len, }
"Via\0".as_ptr() as *const c_char, *buffer = ptr::null();
) *buffer_len = 0;
return false;
} }
unsafe extern "C" fn sip_ua_hdr_setup( unsafe extern "C" fn sip_ua_hdr_setup(
@ -490,14 +483,15 @@ unsafe extern "C" fn sip_ua_hdr_get(
unsafe extern "C" fn sip_ua_hdr_get_data( unsafe extern "C" fn sip_ua_hdr_get_data(
tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32, tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool { ) -> bool {
sip_get_header_value( let tx = cast_pointer!(tx, SIPTransaction);
tx, if let Some(value) = sip_get_header_value(tx, local_id, flow_flags.into(), "User-Agent") {
local_id, *buffer = value.as_ptr();
flow_flags, *buffer_len = value.len() as u32;
buffer, return true;
buffer_len, }
"User-Agent\0".as_ptr() as *const c_char, *buffer = ptr::null();
) *buffer_len = 0;
return false;
} }
unsafe extern "C" fn sip_content_type_hdr_setup( unsafe extern "C" fn sip_content_type_hdr_setup(
@ -531,14 +525,15 @@ unsafe extern "C" fn sip_content_type_hdr_get(
unsafe extern "C" fn sip_content_type_hdr_get_data( unsafe extern "C" fn sip_content_type_hdr_get_data(
tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32, tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool { ) -> bool {
sip_get_header_value( let tx = cast_pointer!(tx, SIPTransaction);
tx, if let Some(value) = sip_get_header_value(tx, local_id, flow_flags.into(), "Content-Type") {
local_id, *buffer = value.as_ptr();
flow_flags, *buffer_len = value.len() as u32;
buffer, return true;
buffer_len, }
"Content-Type\0".as_ptr() as *const c_char, *buffer = ptr::null();
) *buffer_len = 0;
return false;
} }
unsafe extern "C" fn sip_content_length_hdr_setup( unsafe extern "C" fn sip_content_length_hdr_setup(
@ -572,14 +567,15 @@ unsafe extern "C" fn sip_content_length_hdr_get(
unsafe extern "C" fn sip_content_length_hdr_get_data( unsafe extern "C" fn sip_content_length_hdr_get_data(
tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32, tx: *const c_void, flow_flags: u8, local_id: u32, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool { ) -> bool {
sip_get_header_value( let tx = cast_pointer!(tx, SIPTransaction);
tx, if let Some(value) = sip_get_header_value(tx, local_id, flow_flags.into(), "Content-Length") {
local_id, *buffer = value.as_ptr();
flow_flags, *buffer_len = value.len() as u32;
buffer, return true;
buffer_len, }
"Content-Length\0".as_ptr() as *const c_char, *buffer = ptr::null();
) *buffer_len = 0;
return false;
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn ScDetectSipRegister() { pub unsafe extern "C" fn ScDetectSipRegister() {

Loading…
Cancel
Save