Skip to content

Commit cddbd0b

Browse files
catenacybervictorjulien
authored andcommitted
rust: bindgen more file functions
Ticket: 7762
1 parent 327b8b0 commit cddbd0b

7 files changed

Lines changed: 94 additions & 91 deletions

File tree

rust/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ if HAVE_BINDGEN
142142
--with-derive-default \
143143
--allowlist-type 'AppProto.*' \
144144
--allowlist-function 'AppProto.*' \
145-
--allowlist-function 'FileAppendData' \
145+
--allowlist-function 'File.*' \
146146
--allowlist-type 'SC.*' \
147147
--allowlist-function 'SC.*' \
148148
--allowlist-var 'SC.*' \

rust/src/filecontainer.rs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,16 @@
1717

1818
//! This module handles file container operations (open, append, close).
1919
20-
use std::os::raw::c_void;
2120
use std::ptr;
2221

2322
use crate::core::*;
2423

25-
#[repr(C)]
26-
#[derive(Debug)]
27-
pub struct FileContainer {
28-
head: *mut c_void,
29-
tail: *mut c_void,
30-
}
31-
32-
impl Default for FileContainer {
33-
fn default() -> Self {
34-
Self {
35-
head: ptr::null_mut(),
36-
tail: ptr::null_mut(),
37-
}
38-
}
39-
}
40-
41-
// Defined in util-file.h
42-
#[allow(unused_doc_comments)]
43-
/// cbindgen:ignore
44-
extern "C" {
45-
#[cfg(not(test))]
46-
pub fn FileContainerRecycle(file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig);
47-
#[cfg(not(test))]
48-
pub fn FileAppendGAPById(
49-
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
50-
data: *const u8, data_len: u32,
51-
) -> i32;
52-
#[cfg(not(test))]
53-
pub fn FileAppendDataById(
54-
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
55-
data: *const u8, data_len: u32,
56-
) -> i32;
57-
#[cfg(not(test))]
58-
pub fn FileCloseFileById(
59-
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
60-
data: *const u8, data_len: u32, flags: u16,
61-
) -> i32;
62-
#[cfg(not(test))]
63-
pub fn FileOpenFileWithId(
64-
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
65-
name: *const u8, name_len: u16, data: *const u8, data_len: u32, flags: u16,
66-
) -> i32;
67-
}
24+
pub use suricata_sys::sys::FileContainer;
25+
#[cfg(not(test))]
26+
use suricata_sys::sys::{
27+
FileAppendDataById, FileAppendGAPById, FileCloseFileById, FileContainerRecycle,
28+
FileOpenFileWithId,
29+
};
6830

6931
#[cfg(test)]
7032
#[allow(non_snake_case)]
@@ -103,15 +65,26 @@ pub(super) unsafe fn FileOpenFileWithId(
10365
0
10466
}
10567

106-
impl FileContainer {
107-
pub fn free(&mut self, cfg: &'static SuricataFileContext) {
68+
pub trait FileContainerWrapper {
69+
fn free(&mut self, cfg: &'static SuricataFileContext);
70+
fn file_open(
71+
&mut self, cfg: &'static SuricataFileContext, track_id: u32, name: &[u8], flags: u16,
72+
) -> i32;
73+
fn file_append(
74+
&mut self, cfg: &'static SuricataFileContext, track_id: &u32, data: &[u8], is_gap: bool,
75+
) -> i32;
76+
fn file_close(&mut self, cfg: &'static SuricataFileContext, track_id: &u32, flags: u16) -> i32;
77+
}
78+
79+
impl FileContainerWrapper for FileContainer {
80+
fn free(&mut self, cfg: &'static SuricataFileContext) {
10881
SCLogDebug!("freeing self");
10982
unsafe {
11083
FileContainerRecycle(self, cfg.files_sbcfg);
11184
}
11285
}
11386

114-
pub fn file_open(
87+
fn file_open(
11588
&mut self, cfg: &'static SuricataFileContext, track_id: u32, name: &[u8], flags: u16,
11689
) -> i32 {
11790
SCLogDebug!("FILE {:p} OPEN flags {:04X}", &self, flags);
@@ -130,7 +103,7 @@ impl FileContainer {
130103
}
131104
}
132105

133-
pub fn file_append(
106+
fn file_append(
134107
&mut self, cfg: &'static SuricataFileContext, track_id: &u32, data: &[u8], is_gap: bool,
135108
) -> i32 {
136109
SCLogDebug!("FILECONTAINER: append {}", data.len());
@@ -166,9 +139,7 @@ impl FileContainer {
166139
res
167140
}
168141

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

174145
unsafe { FileCloseFileById(self, cfg.files_sbcfg, *track_id, ptr::null(), 0u32, flags) }

rust/src/http2/http2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::conf::conf_get;
2626
use crate::core::*;
2727
use crate::direction::Direction;
2828
use crate::dns::dns::DnsVariant;
29+
use crate::filecontainer::FileContainerWrapper;
2930
use crate::filetracker::*;
3031
use crate::flow::Flow;
3132
use crate::frames::Frame;

rust/src/nfs/nfs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::core::*;
3636
use crate::direction::Direction;
3737
use crate::direction::DIR_BOTH;
3838
use crate::filetracker::*;
39+
use crate::filecontainer::FileContainerWrapper;
3940
use crate::flow::{Flow, flow_get_last_time};
4041
use crate::frames::*;
4142

rust/src/smb/smb.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::flow::{Flow, FLOW_DIR_REVERSED, flow_get_flags, flow_get_last_time, f
5050
use crate::frames::*;
5151
use crate::conf::*;
5252
use crate::applayer::{AppLayerResult, AppLayerTxData, AppLayerEvent};
53+
use crate::filecontainer::FileContainerWrapper;
5354

5455
use crate::smb::nbss_records::*;
5556
use crate::smb::smb1_records::*;

rust/sys/src/sys.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,11 @@ pub struct File_ {
897897
_unused: [u8; 0],
898898
}
899899
pub type File = File_;
900+
#[repr(C)]
901+
#[derive(Debug, Copy, Clone)]
902+
pub struct AppLayerTxData {
903+
_unused: [u8; 0],
904+
}
900905
extern "C" {
901906
#[doc = " \\brief Given a protocol name, checks if the parser is enabled in\n the conf file.\n\n \\param alproto_name Name of the app layer protocol.\n\n \\retval 1 If enabled.\n \\retval 0 If disabled."]
902907
pub fn SCAppLayerParserConfParserEnabled(
@@ -928,6 +933,9 @@ extern "C" {
928933
extern "C" {
929934
pub fn SCAppLayerParserStateIssetFlag(pstate: *mut AppLayerParserState, flag: u16) -> u16;
930935
}
936+
extern "C" {
937+
pub fn FileApplyTxFlags(txd: *const AppLayerTxData, direction: u8, file: *mut File);
938+
}
931939
extern "C" {
932940
pub fn SCAppLayerRegisterParserAlias(
933941
proto_name: *const ::std::os::raw::c_char, proto_alias: *const ::std::os::raw::c_char,
@@ -1041,6 +1049,34 @@ extern "C" {
10411049
data_len: u32,
10421050
) -> ::std::os::raw::c_int;
10431051
}
1052+
extern "C" {
1053+
#[doc = " \\brief Open a new File\n\n \\param ffc flow container\n \\param sbcfg buffer config\n \\param name filename character array\n \\param name_len filename len\n \\param data initial data\n \\param data_len initial data len\n \\param flags open flags\n\n \\retval ff flowfile object\n\n \\note filename is not a string, so it's not nul terminated.\n\n If flags contains the FILE_USE_DETECT bit, the pruning code will\n consider not just the content_stored tracker, but also content_inspected.\n It's the responsibility of the API user to make sure this tracker is\n properly updated."]
1054+
pub fn FileOpenFileWithId(
1055+
arg1: *mut FileContainer, arg2: *const StreamingBufferConfig, track_id: u32,
1056+
name: *const u8, name_len: u16, data: *const u8, data_len: u32, flags: u16,
1057+
) -> ::std::os::raw::c_int;
1058+
}
1059+
extern "C" {
1060+
pub fn FileAppendDataById(
1061+
arg1: *mut FileContainer, sbcfg: *const StreamingBufferConfig, track_id: u32,
1062+
data: *const u8, data_len: u32,
1063+
) -> ::std::os::raw::c_int;
1064+
}
1065+
extern "C" {
1066+
pub fn FileAppendGAPById(
1067+
ffc: *mut FileContainer, sbcfg: *const StreamingBufferConfig, track_id: u32,
1068+
data: *const u8, data_len: u32,
1069+
) -> ::std::os::raw::c_int;
1070+
}
1071+
extern "C" {
1072+
pub fn FileCloseFileById(
1073+
arg1: *mut FileContainer, sbcfg: *const StreamingBufferConfig, track_id: u32,
1074+
data: *const u8, data_len: u32, flags: u16,
1075+
) -> ::std::os::raw::c_int;
1076+
}
1077+
extern "C" {
1078+
pub fn FileContainerRecycle(arg1: *mut FileContainer, cfg: *const StreamingBufferConfig);
1079+
}
10441080
#[repr(C)]
10451081
#[derive(Debug, Copy, Clone)]
10461082
pub struct HttpRangeContainerBuffer {
@@ -1095,12 +1131,6 @@ extern "C" {
10951131
len: u32,
10961132
) -> ::std::os::raw::c_int;
10971133
}
1098-
extern "C" {
1099-
pub fn SCHTPFileCloseHandleRange(
1100-
sbcfg: *const StreamingBufferConfig, arg1: *mut FileContainer, arg2: u16,
1101-
arg3: *mut HttpRangeContainerBlock, arg4: *const u8, arg5: u32,
1102-
) -> bool;
1103-
}
11041134
pub type FrameId = i64;
11051135
#[repr(C)]
11061136
#[derive(Debug, Default, Copy, Clone)]

src/util-file.h

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@ typedef struct FileContainer_ {
5353
int FileAppendData(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data,
5454
uint32_t data_len);
5555

56+
/**
57+
* \brief Open a new File
58+
*
59+
* \param ffc flow container
60+
* \param sbcfg buffer config
61+
* \param name filename character array
62+
* \param name_len filename len
63+
* \param data initial data
64+
* \param data_len initial data len
65+
* \param flags open flags
66+
*
67+
* \retval ff flowfile object
68+
*
69+
* \note filename is not a string, so it's not nul terminated.
70+
*
71+
* If flags contains the FILE_USE_DETECT bit, the pruning code will
72+
* consider not just the content_stored tracker, but also content_inspected.
73+
* It's the responsibility of the API user to make sure this tracker is
74+
* properly updated.
75+
*/
76+
int FileOpenFileWithId(FileContainer *, const StreamingBufferConfig *, uint32_t track_id,
77+
const uint8_t *name, uint16_t name_len, const uint8_t *data, uint32_t data_len,
78+
uint16_t flags);
79+
int FileAppendDataById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id,
80+
const uint8_t *data, uint32_t data_len);
81+
int FileAppendGAPById(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id,
82+
const uint8_t *data, uint32_t data_len);
83+
int FileCloseFileById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id,
84+
const uint8_t *data, uint32_t data_len, uint16_t flags);
85+
86+
void FileContainerRecycle(FileContainer *, const StreamingBufferConfig *cfg);
87+
5688
#ifndef SURICATA_BINDGEN_H
5789

5890
#include "flow.h"
@@ -141,34 +173,8 @@ typedef struct File_ {
141173
FileContainer *FileContainerAlloc(void);
142174
void FileContainerFree(FileContainer *, const StreamingBufferConfig *cfg);
143175

144-
void FileContainerRecycle(FileContainer *, const StreamingBufferConfig *cfg);
145-
146176
void FileContainerAdd(FileContainer *, File *);
147177

148-
/**
149-
* \brief Open a new File
150-
*
151-
* \param ffc flow container
152-
* \param sbcfg buffer config
153-
* \param name filename character array
154-
* \param name_len filename len
155-
* \param data initial data
156-
* \param data_len initial data len
157-
* \param flags open flags
158-
*
159-
* \retval ff flowfile object
160-
*
161-
* \note filename is not a string, so it's not nul terminated.
162-
*
163-
* If flags contains the FILE_USE_DETECT bit, the pruning code will
164-
* consider not just the content_stored tracker, but also content_inspected.
165-
* It's the responsibility of the API user to make sure this tracker is
166-
* properly updated.
167-
*/
168-
int FileOpenFileWithId(FileContainer *, const StreamingBufferConfig *,
169-
uint32_t track_id, const uint8_t *name, uint16_t name_len,
170-
const uint8_t *data, uint32_t data_len, uint16_t flags);
171-
172178
/**
173179
* \brief Close a File
174180
*
@@ -182,16 +188,9 @@ int FileOpenFileWithId(FileContainer *, const StreamingBufferConfig *,
182188
*/
183189
int FileCloseFile(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data,
184190
uint32_t data_len, uint16_t flags);
185-
int FileCloseFileById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id,
186-
const uint8_t *data, uint32_t data_len, uint16_t flags);
187191
int FileCloseFilePtr(File *ff, const StreamingBufferConfig *sbcfg, const uint8_t *data,
188192
uint32_t data_len, uint16_t flags);
189193

190-
int FileAppendDataById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id,
191-
const uint8_t *data, uint32_t data_len);
192-
int FileAppendGAPById(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id,
193-
const uint8_t *data, uint32_t data_len);
194-
195194
void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min);
196195

197196
/**

0 commit comments

Comments
 (0)