Skip to content

Commit

Permalink
files: update API and callers to take stream config
Browse files Browse the repository at this point in the history
This is to allow not storing the stream buffer config in each file.
  • Loading branch information
victorjulien committed Jan 23, 2023
1 parent f7dbdb7 commit e3e5540
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 178 deletions.
7 changes: 6 additions & 1 deletion rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub enum HttpRangeContainerBlock {}
pub type SCHttpRangeFreeBlock = extern "C" fn (
c: *mut HttpRangeContainerBlock);
pub type SCHTPFileCloseHandleRange = extern "C" fn (
sbcfg: &StreamingBufferConfig,
fc: *mut FileContainer,
flags: u16,
c: *mut HttpRangeContainerBlock,
Expand All @@ -166,19 +167,23 @@ pub type SCFileOpenFileWithId = extern "C" fn (
flags: u16) -> i32;
pub type SCFileCloseFileById = extern "C" fn (
file_container: &FileContainer,
sbcfg: &StreamingBufferConfig,
track_id: u32,
data: *const u8, data_len: u32,
flags: u16) -> i32;
pub type SCFileAppendDataById = extern "C" fn (
file_container: &FileContainer,
sbcfg: &StreamingBufferConfig,
track_id: u32,
data: *const u8, data_len: u32) -> i32;
pub type SCFileAppendGAPById = extern "C" fn (
file_container: &FileContainer,
sbcfg: &StreamingBufferConfig,
track_id: u32,
data: *const u8, data_len: u32) -> i32;
pub type SCFileContainerRecycle = extern "C" fn (
file_container: &FileContainer);
file_container: &FileContainer,
sbcfg: &StreamingBufferConfig);

// A Suricata context that is passed in from C. This is alternative to
// using functions from Suricata directly, so they can be wrapped so
Expand Down
14 changes: 7 additions & 7 deletions rust/src/filecontainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ impl Default for FileContainer {
}

impl FileContainer {
pub fn free(&mut self) {
pub fn free(&mut self, cfg: &'static SuricataFileContext) {
SCLogDebug!("freeing self");
if let Some(c) = unsafe {SC} {
(c.FileContainerRecycle)(self);
(c.FileContainerRecycle)(self, cfg.files_sbcfg);
}
}

Expand All @@ -83,7 +83,7 @@ impl FileContainer {
}
}

pub fn file_append(&mut self, track_id: &u32, data: &[u8], is_gap: bool) -> i32 {
pub fn file_append(&mut self, cfg: &'static SuricataFileContext, track_id: &u32, data: &[u8], is_gap: bool) -> i32 {
SCLogDebug!("FILECONTAINER: append {}", data.len());
if data.is_empty() {
return 0
Expand All @@ -94,13 +94,13 @@ impl FileContainer {
let res = match is_gap {
false => {
SCLogDebug!("appending file data");
let r = (c.FileAppendData)(self, *track_id,
let r = (c.FileAppendData)(self, cfg.files_sbcfg, *track_id,
data.as_ptr(), data.len() as u32);
r
},
true => {
SCLogDebug!("appending GAP");
let r = (c.FileAppendGAP)(self, *track_id,
let r = (c.FileAppendGAP)(self, cfg.files_sbcfg, *track_id,
data.as_ptr(), data.len() as u32);
r
},
Expand All @@ -110,13 +110,13 @@ impl FileContainer {
}
}

pub fn file_close(&mut self, track_id: &u32, flags: u16) -> i32 {
pub fn file_close(&mut self, cfg: &'static SuricataFileContext, track_id: &u32, flags: u16) -> i32 {
SCLogDebug!("FILECONTAINER: CLOSEing");

match unsafe {SC} {
None => panic!("BUG no suricata_config"),
Some(c) => {
let res = (c.FileCloseFile)(self, *track_id, ptr::null(), 0u32, flags);
let res = (c.FileCloseFile)(self, cfg.files_sbcfg, *track_id, ptr::null(), 0u32, flags);
res
}
}
Expand Down
32 changes: 19 additions & 13 deletions rust/src/filetracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,25 @@ impl FileTransferTracker {
r
}

pub fn close(&mut self, files: &mut FileContainer, flags: u16) {
pub fn close(&mut self, config: &'static SuricataFileContext,
files: &mut FileContainer, flags: u16)
{
if !self.file_is_truncated {
SCLogDebug!("closing file with id {}", self.track_id);
files.file_close(&self.track_id, flags);
files.file_close(config, &self.track_id, flags);
}
self.file_open = false;
self.tracked = 0;
}

pub fn trunc (&mut self, files: &mut FileContainer, flags: u16) {
pub fn trunc (&mut self, config: &'static SuricataFileContext,
files: &mut FileContainer, flags: u16)
{
if self.file_is_truncated || !self.file_open {
return;
}
let myflags = flags | 1; // TODO util-file.c::FILE_TRUNCATED
files.file_close(&self.track_id, myflags);
files.file_close(config, &self.track_id, myflags);
SCLogDebug!("truncated file");
self.file_is_truncated = true;
self.chunks.clear();
Expand All @@ -127,7 +131,7 @@ impl FileTransferTracker {
{
if self.chunk_left != 0 || self.fill_bytes != 0 {
SCLogDebug!("current chunk incomplete: truncating");
self.trunc(files, flags);
self.trunc(config, files, flags);
}

SCLogDebug!("NEW CHUNK: chunk_size {} fill_bytes {}", chunk_size, fill_bytes);
Expand All @@ -139,7 +143,7 @@ impl FileTransferTracker {
SCLogDebug!("NEW CHUNK IS OOO: expected {}, got {}", self.tracked, chunk_offset);
if is_last {
SCLogDebug!("last chunk is out of order, this means we missed data before");
self.trunc(files, flags);
self.trunc(config, files, flags);
}
self.chunk_is_ooo = true;
self.cur_ooo_chunk_offset = chunk_offset;
Expand All @@ -159,7 +163,7 @@ impl FileTransferTracker {
}

if self.file_open {
let res = self.update(files, flags, data, 0);
let res = self.update(config, files, flags, data, 0);
SCLogDebug!("NEW CHUNK: update res {:?}", res);
return res;
}
Expand All @@ -170,7 +174,9 @@ impl FileTransferTracker {
/// update the file tracker
/// If gap_size > 0 'data' should not be used.
/// return how much we consumed of data
pub fn update(&mut self, files: &mut FileContainer, flags: u16, data: &[u8], gap_size: u32) -> u32 {
pub fn update(&mut self, config: &'static SuricataFileContext,
files: &mut FileContainer, flags: u16, data: &[u8], gap_size: u32) -> u32
{
if self.file_is_truncated {
let consumed = std::cmp::min(data.len() as u32, self.chunk_left);
self.chunk_left = self.chunk_left.saturating_sub(data.len() as u32);
Expand All @@ -186,7 +192,7 @@ impl FileTransferTracker {
//SCLogDebug!("UPDATE: nothing to do");
if self.chunk_is_last {
SCLogDebug!("last empty chunk, closing");
self.close(files, flags);
self.close(config, files, flags);
self.chunk_is_last = false;
}
return 0
Expand All @@ -211,7 +217,7 @@ impl FileTransferTracker {
let d = &data[0..self.chunk_left as usize];

if !self.chunk_is_ooo {
let res = files.file_append(&self.track_id, d, is_gap);
let res = files.file_append(config, &self.track_id, d, is_gap);
match res {
0 => { },
-2 => {
Expand Down Expand Up @@ -264,7 +270,7 @@ impl FileTransferTracker {
Some(c) => {
self.in_flight -= c.chunk.len() as u64;

let res = files.file_append(&self.track_id, &c.chunk, c.contains_gap);
let res = files.file_append(config, &self.track_id, &c.chunk, c.contains_gap);
match res {
0 => { },
-2 => {
Expand Down Expand Up @@ -296,15 +302,15 @@ impl FileTransferTracker {
}
if self.chunk_is_last {
SCLogDebug!("last chunk, closing");
self.close(files, flags);
self.close(config, files, flags);
self.chunk_is_last = false;
} else {
SCLogDebug!("NOT last chunk, keep going");
}

} else {
if !self.chunk_is_ooo {
let res = files.file_append(&self.track_id, data, is_gap);
let res = files.file_append(config, &self.track_id, data, is_gap);
match res {
0 => { },
-2 => {
Expand Down
38 changes: 20 additions & 18 deletions rust/src/http2/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ impl HTTP2Transaction {

pub fn free(&mut self) {
if !self.file_range.is_null() {
match unsafe { SC } {
None => panic!("BUG no suricata_config"),
Some(c) => {
if let Some(c) = unsafe { SC } {
if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } {
//TODO get a file container instead of NULL
(c.HTPFileCloseHandleRange)(
std::ptr::null_mut(),
0,
self.file_range,
std::ptr::null_mut(),
0,
);
sfcm.files_sbcfg,
std::ptr::null_mut(),
0,
self.file_range,
std::ptr::null_mut(),
0,
);
(c.HttpRangeFreeBlock)(self.file_range);
self.file_range = std::ptr::null_mut();
}
Expand Down Expand Up @@ -247,7 +247,7 @@ impl HTTP2Transaction {
if over {
range::http2_range_close(self, Direction::ToClient, decompressed)
} else {
range::http2_range_append(self.file_range, decompressed)
range::http2_range_append(sfcm, self.file_range, decompressed)
}
}
let (files, flags) = self.files.get(Direction::ToClient);
Expand Down Expand Up @@ -364,8 +364,10 @@ impl HTTP2Transaction {

impl Drop for HTTP2Transaction {
fn drop(&mut self) {
self.files.files_ts.free();
self.files.files_tc.free();
if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } {
self.files.files_ts.free(sfcm);
self.files.files_tc.free(sfcm);
}
self.free();
}
}
Expand Down Expand Up @@ -460,10 +462,10 @@ impl HTTP2State {
// but we need state's file container cf https://redmine.openinfosecfoundation.org/issues/4444
for tx in &mut self.transactions {
if !tx.file_range.is_null() {
match unsafe { SC } {
None => panic!("BUG no suricata_config"),
Some(c) => {
if let Some(c) = unsafe { SC } {
if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } {
(c.HTPFileCloseHandleRange)(
sfcm.files_sbcfg,
&mut tx.files.files_tc,
0,
tx.file_range,
Expand Down Expand Up @@ -501,10 +503,10 @@ impl HTTP2State {
// this should be in HTTP2Transaction::free
// but we need state's file container cf https://redmine.openinfosecfoundation.org/issues/4444
if !tx.file_range.is_null() {
match unsafe { SC } {
None => panic!("BUG no suricata_config"),
Some(c) => {
if let Some(c) = unsafe { SC } {
if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } {
(c.HTPFileCloseHandleRange)(
sfcm.files_sbcfg,
&mut tx.files.files_tc,
0,
tx.file_range,
Expand Down
32 changes: 19 additions & 13 deletions rust/src/http2/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::core::{
Direction, Flow, HttpRangeContainerBlock, StreamingBufferConfig, SuricataFileContext, SC,
};
use crate::http2::http2::HTTP2Transaction;
use crate::http2::http2::SURICATA_HTTP2_FILE_CONFIG;

use nom7::branch::alt;
use nom7::bytes::streaming::{take_till, take_while};
Expand Down Expand Up @@ -150,26 +151,31 @@ pub fn http2_range_open(
}
}

pub fn http2_range_append(fr: *mut HttpRangeContainerBlock, data: &[u8]) {
pub fn http2_range_append(cfg: &'static SuricataFileContext, fr: *mut HttpRangeContainerBlock, data: &[u8]) {
unsafe {
HttpRangeAppendData(fr, data.as_ptr(), data.len() as u32);
HttpRangeAppendData(cfg.files_sbcfg, fr, data.as_ptr(), data.len() as u32);
}
}

pub fn http2_range_close(
tx: &mut HTTP2Transaction, dir: Direction, data: &[u8],
) {
let added = if let Some(c) = unsafe { SC } {
let (files, flags) = tx.files.get(dir);
let added = (c.HTPFileCloseHandleRange)(
files,
flags,
tx.file_range,
data.as_ptr(),
data.len() as u32,
);
(c.HttpRangeFreeBlock)(tx.file_range);
added
if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } {
let (files, flags) = tx.files.get(dir);
let added = (c.HTPFileCloseHandleRange)(
sfcm.files_sbcfg,
files,
flags,
tx.file_range,
data.as_ptr(),
data.len() as u32,
);
(c.HttpRangeFreeBlock)(tx.file_range);
added
} else {
false
}
} else {
false
};
Expand All @@ -187,7 +193,7 @@ extern "C" {
data: *const c_uchar, data_len: u32,
) -> *mut HttpRangeContainerBlock;
pub fn HttpRangeAppendData(
c: *mut HttpRangeContainerBlock, data: *const c_uchar, data_len: u32,
cfg: *const StreamingBufferConfig, c: *mut HttpRangeContainerBlock, data: *const c_uchar, data_len: u32,
) -> std::os::raw::c_int;
}

Expand Down
20 changes: 15 additions & 5 deletions rust/src/nfs/nfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,10 @@ impl Transaction for NFSTransaction {
impl Drop for NFSTransaction {
fn drop(&mut self) {
if let Some(NFSTransactionTypeData::FILE(ref mut tdf)) = self.type_data {
tdf.files.files_ts.free();
tdf.files.files_tc.free();
if let Some(sfcm) = unsafe { SURICATA_NFS_FILE_CONFIG } {
tdf.files.files_ts.free(sfcm);
tdf.files.files_tc.free(sfcm);
}
}
self.free();
}
Expand Down Expand Up @@ -309,19 +311,27 @@ pub fn filetracker_newchunk(ft: &mut FileTransferTracker, files: &mut FileContai
fn filetracker_trunc(ft: &mut FileTransferTracker, files: &mut FileContainer,
flags: u16)
{
ft.trunc(files, flags);
if let Some(sfcm) = unsafe { SURICATA_NFS_FILE_CONFIG } {
ft.trunc(sfcm, files, flags);
}
}

pub fn filetracker_close(ft: &mut FileTransferTracker, files: &mut FileContainer,
flags: u16)
{
ft.close(files, flags);
if let Some(sfcm) = unsafe { SURICATA_NFS_FILE_CONFIG } {
ft.close(sfcm, files, flags);
}
}

fn filetracker_update(ft: &mut FileTransferTracker, files: &mut FileContainer,
flags: u16, data: &[u8], gap_size: u32) -> u32
{
ft.update(files, flags, data, gap_size)
if let Some(sfcm) = unsafe { SURICATA_NFS_FILE_CONFIG } {
ft.update(sfcm, files, flags, data, gap_size)
} else {
0
}
}


Expand Down
Loading

0 comments on commit e3e5540

Please sign in to comment.