use std;
use crate::core::{DetectEngineState,Flow,AppLayerEventType,AppLayerDecoderEvents,AppProto};
use crate::filecontainer::FileContainer;
use crate::applayer;
use std::os::raw::{c_void,c_char,c_int};
#[repr(C)]
#[derive(Debug,PartialEq)]
pub struct AppLayerTxConfig {
log_flags: u8,
}
impl AppLayerTxConfig {
pub fn new() -> Self {
Self {
log_flags: 0,
}
}
pub fn add_log_flags(&mut self, flags: u8) {
self.log_flags |= flags;
}
pub fn set_log_flags(&mut self, flags: u8) {
self.log_flags = flags;
}
pub fn get_log_flags(&self) -> u8 {
self.log_flags
}
}
#[repr(C)]
#[derive(Debug,PartialEq)]
pub struct AppLayerTxData {
pub config: AppLayerTxConfig,
logged: LoggerFlags,
detect_flags_ts: u64,
detect_flags_tc: u64,
}
impl AppLayerTxData {
pub fn new() -> Self {
Self {
config: AppLayerTxConfig::new(),
logged: LoggerFlags::new(),
detect_flags_ts: 0,
detect_flags_tc: 0,
}
}
}
#[macro_export]
macro_rules!export_tx_data_get {
($name:ident, $type:ty) => {
#[no_mangle]
pub unsafe extern "C" fn $name(tx: *mut std::os::raw::c_void)
-> *mut crate::applayer::AppLayerTxData
{
let tx = &mut *(tx as *mut $type);
&mut tx.tx_data
}
}
}
#[repr(C)]
#[derive(Debug,PartialEq,Copy,Clone)]
pub struct AppLayerResult {
pub status: i32,
pub consumed: u32,
pub needed: u32,
}
impl AppLayerResult {
pub fn ok() -> AppLayerResult {
return AppLayerResult {
status: 0,
consumed: 0,
needed: 0,
};
}
pub fn err() -> AppLayerResult {
return AppLayerResult {
status: -1,
consumed: 0,
needed: 0,
};
}
pub fn incomplete(consumed: u32, needed: u32) -> AppLayerResult {
return AppLayerResult {
status: 1,
consumed: consumed,
needed: needed,
};
}
pub fn is_ok(self) -> bool {
self.status == 0
}
pub fn is_incomplete(self) -> bool {
self.status == 1
}
pub fn is_err(self) -> bool {
self.status == -1
}
}
impl From<bool> for AppLayerResult {
fn from(v: bool) -> Self {
if v == false {
Self::err()
} else {
Self::ok()
}
}
}
impl From<i32> for AppLayerResult {
fn from(v: i32) -> Self {
if v < 0 {
Self::err()
} else {
Self::ok()
}
}
}
#[repr(C)]
pub struct RustParser {
pub name: *const c_char,
pub default_port: *const c_char,
pub ipproto: c_int,
pub probe_ts: Option<ProbeFn>,
pub probe_tc: Option<ProbeFn>,
pub min_depth: u16,
pub max_depth: u16,
pub state_new: StateAllocFn,
pub state_free: StateFreeFn,
pub parse_ts: ParseFn,
pub parse_tc: ParseFn,
pub get_tx_count: StateGetTxCntFn,
pub get_tx: StateGetTxFn,
pub tx_free: StateTxFreeFn,
pub tx_get_comp_st: StateGetTxCompletionStatusFn,
pub tx_get_progress: StateGetProgressFn,
pub get_de_state: GetDetectStateFn,
pub set_de_state: SetDetectStateFn,
pub get_events: Option<GetEventsFn>,
pub get_eventinfo: Option<GetEventInfoFn>,
pub get_eventinfo_byid: Option<GetEventInfoByIdFn>,
pub localstorage_new: Option<LocalStorageNewFn>,
pub localstorage_free: Option<LocalStorageFreeFn>,
pub get_files: Option<GetFilesFn>,
pub get_tx_iterator: Option<GetTxIteratorFn>,
pub get_tx_data: GetTxDataFn,
pub apply_tx_config: Option<ApplyTxConfigFn>,
pub flags: u32,
pub truncate: Option<TruncateFn>,
}
#[macro_export]
macro_rules! build_slice {
($buf:ident, $len:expr) => ( unsafe{ std::slice::from_raw_parts($buf, $len) } );
}
#[macro_export]
macro_rules! cast_pointer {
($ptr:ident, $ty:ty) => ( unsafe{ &mut *($ptr as *mut $ty) } );
}
pub type ParseFn = extern "C" fn (flow: *const Flow,
state: *mut c_void,
pstate: *mut c_void,
input: *const u8,
input_len: u32,
data: *const c_void,
flags: u8) -> AppLayerResult;
pub type ProbeFn = extern "C" fn (flow: *const Flow, flags: u8, input:*const u8, input_len: u32, rdir: *mut u8) -> AppProto;
pub type StateAllocFn = extern "C" fn (*mut c_void, AppProto) -> *mut c_void;
pub type StateFreeFn = extern "C" fn (*mut c_void);
pub type StateTxFreeFn = extern "C" fn (*mut c_void, u64);
pub type StateGetTxFn = extern "C" fn (*mut c_void, u64) -> *mut c_void;
pub type StateGetTxCntFn = extern "C" fn (*mut c_void) -> u64;
pub type StateGetTxCompletionStatusFn = extern "C" fn (u8) -> c_int;
pub type StateGetProgressFn = extern "C" fn (*mut c_void, u8) -> c_int;
pub type GetDetectStateFn = extern "C" fn (*mut c_void) -> *mut DetectEngineState;
pub type SetDetectStateFn = extern "C" fn (*mut c_void, &mut DetectEngineState) -> c_int;
pub type GetEventInfoFn = extern "C" fn (*const c_char, *mut c_int, *mut AppLayerEventType) -> c_int;
pub type GetEventInfoByIdFn = extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8;
pub type GetEventsFn = extern "C" fn (*mut c_void) -> *mut AppLayerDecoderEvents;
pub type LocalStorageNewFn = extern "C" fn () -> *mut c_void;
pub type LocalStorageFreeFn = extern "C" fn (*mut c_void);
pub type GetFilesFn = extern "C" fn (*mut c_void, u8) -> *mut FileContainer;
pub type GetTxIteratorFn = extern "C" fn (ipproto: u8, alproto: AppProto,
state: *mut c_void,
min_tx_id: u64,
max_tx_id: u64,
istate: &mut u64)
-> AppLayerGetTxIterTuple;
pub type GetTxDataFn = unsafe extern "C" fn(*mut c_void) -> *mut AppLayerTxData;
pub type ApplyTxConfigFn = unsafe extern "C" fn (*mut c_void, *mut c_void, c_int, AppLayerTxConfig);
pub type TruncateFn = unsafe extern "C" fn (*mut c_void, u8);
extern {
pub fn AppLayerRegisterProtocolDetection(parser: *const RustParser, enable_default: c_int) -> AppProto;
pub fn AppLayerRegisterParser(parser: *const RustParser, alproto: AppProto) -> c_int;
}
extern {
pub fn AppLayerProtoDetectConfProtoDetectionEnabled(ipproto: *const c_char, proto: *const c_char) -> c_int;
}
pub const APP_LAYER_PARSER_EOF_TS : u8 = BIT_U8!(5);
pub const APP_LAYER_PARSER_EOF_TC : u8 = BIT_U8!(6);
pub const APP_LAYER_PARSER_NO_INSPECTION : u8 = BIT_U8!(1);
pub const APP_LAYER_PARSER_NO_REASSEMBLY : u8 = BIT_U8!(2);
pub const APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD : u8 = BIT_U8!(3);
pub const APP_LAYER_PARSER_BYPASS_READY : u8 = BIT_U8!(4);
pub const APP_LAYER_PARSER_OPT_ACCEPT_GAPS: u32 = BIT_U32!(0);
pub const APP_LAYER_PARSER_OPT_UNIDIR_TXS: u32 = BIT_U32!(1);
pub type AppLayerGetTxIteratorFn = extern "C" fn (ipproto: u8,
alproto: AppProto,
alstate: *mut c_void,
min_tx_id: u64,
max_tx_id: u64,
istate: &mut u64) -> applayer::AppLayerGetTxIterTuple;
extern {
pub fn AppLayerParserStateSetFlag(state: *mut c_void, flag: u8);
pub fn AppLayerParserStateIssetFlag(state: *mut c_void, flag: u8) -> c_int;
pub fn AppLayerParserConfParserEnabled(ipproto: *const c_char, proto: *const c_char) -> c_int;
pub fn AppLayerParserRegisterGetTxIterator(ipproto: u8, alproto: AppProto, fun: AppLayerGetTxIteratorFn);
pub fn AppLayerParserRegisterOptionFlags(ipproto: u8, alproto: AppProto, flags: u32);
}
#[repr(C)]
pub struct AppLayerGetTxIterTuple {
tx_ptr: *mut std::os::raw::c_void,
tx_id: u64,
has_next: bool,
}
impl AppLayerGetTxIterTuple {
pub fn with_values(tx_ptr: *mut std::os::raw::c_void, tx_id: u64, has_next: bool) -> AppLayerGetTxIterTuple {
AppLayerGetTxIterTuple {
tx_ptr: tx_ptr, tx_id: tx_id, has_next: has_next,
}
}
pub fn not_found() -> AppLayerGetTxIterTuple {
AppLayerGetTxIterTuple {
tx_ptr: std::ptr::null_mut(), tx_id: 0, has_next: false,
}
}
}
#[repr(C)]
#[derive(Debug,PartialEq)]
pub struct LoggerFlags {
flags: u32,
}
impl LoggerFlags {
pub fn new() -> LoggerFlags {
return LoggerFlags{
flags: 0,
}
}
pub fn get(&self) -> u32 {
self.flags
}
pub fn set(&mut self, bits: u32) {
self.flags = bits;
}
}
#[macro_export]
macro_rules!export_tx_get_detect_state {
($name:ident, $type:ty) => (
#[no_mangle]
pub extern "C" fn $name(tx: *mut std::os::raw::c_void)
-> *mut core::DetectEngineState
{
let tx = cast_pointer!(tx, $type);
match tx.de_state {
Some(ds) => {
return ds;
},
None => {
return std::ptr::null_mut();
}
}
}
)
}
#[macro_export]
macro_rules!export_tx_set_detect_state {
($name:ident, $type:ty) => (
#[no_mangle]
pub extern "C" fn $name(tx: *mut std::os::raw::c_void,
de_state: &mut core::DetectEngineState) -> std::os::raw::c_int
{
let tx = cast_pointer!(tx, $type);
tx.de_state = Some(de_state);
0
}
)
}