Skip to content

Commit

Permalink
files: remove the need for state in callbacks
Browse files Browse the repository at this point in the history
As files now belong to transactions
  • Loading branch information
catenacyber authored and victorjulien committed Jun 6, 2024
1 parent e8438fd commit 0d267e2
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 45 deletions.
2 changes: 1 addition & 1 deletion rust/src/applayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ pub type GetEventInfoFn = unsafe extern "C" fn (*const c_char, *mut c_int, *
pub type GetEventInfoByIdFn = unsafe extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8;
pub type LocalStorageNewFn = extern "C" fn () -> *mut c_void;
pub type LocalStorageFreeFn = extern "C" fn (*mut c_void);
pub type GetTxFilesFn = unsafe extern "C" fn (*mut c_void, *mut c_void, u8) -> AppLayerGetFileState;
pub type GetTxFilesFn = unsafe extern "C" fn (*mut c_void, u8) -> AppLayerGetFileState;
pub type GetTxIteratorFn = unsafe extern "C" fn (ipproto: u8, alproto: AppProto,
state: *mut c_void,
min_tx_id: u64,
Expand Down
1 change: 0 additions & 1 deletion rust/src/http2/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,6 @@ pub unsafe extern "C" fn rs_http2_tx_get_alstate_progress(

#[no_mangle]
pub unsafe extern "C" fn rs_http2_getfiles(
_state: *mut std::os::raw::c_void,
tx: *mut std::os::raw::c_void, direction: u8,
) -> AppLayerGetFileState {
let tx = cast_pointer!(tx, HTTP2Transaction);
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nfs/nfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl NFSTransactionFile {
}

#[no_mangle]
pub unsafe extern "C" fn rs_nfs_gettxfiles(_state: *mut std::ffi::c_void, tx: *mut std::ffi::c_void, direction: u8) -> AppLayerGetFileState {
pub unsafe extern "C" fn rs_nfs_gettxfiles(tx: *mut std::ffi::c_void, direction: u8) -> AppLayerGetFileState {
let tx = cast_pointer!(tx, NFSTransaction);
if let Some(NFSTransactionTypeData::FILE(ref mut tdf)) = tx.type_data {
let tx_dir : u8 = tdf.direction.into();
Expand Down
2 changes: 1 addition & 1 deletion rust/src/smb/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl SMBState {

use crate::applayer::AppLayerGetFileState;
#[no_mangle]
pub unsafe extern "C" fn rs_smb_gettxfiles(_state: *mut std::ffi::c_void, tx: *mut std::ffi::c_void, direction: u8) -> AppLayerGetFileState {
pub unsafe extern "C" fn rs_smb_gettxfiles(tx: *mut std::ffi::c_void, direction: u8) -> AppLayerGetFileState {
let tx = cast_pointer!(tx, SMBTransaction);
if let Some(SMBTransactionTypeData::FILE(ref mut tdf)) = tx.type_data {
let tx_dir : u8 = tdf.direction.into();
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ static int FTPDataGetAlstateProgress(void *tx, uint8_t direction)
return FTPDATA_STATE_FINISHED;
}

static AppLayerGetFileState FTPDataStateGetTxFiles(void *_state, void *tx, uint8_t direction)
static AppLayerGetFileState FTPDataStateGetTxFiles(void *tx, uint8_t direction)
{
FtpDataState *ftpdata_state = (FtpDataState *)tx;
AppLayerGetFileState files = { .fc = NULL, .cfg = &sbcfg };
Expand Down
4 changes: 2 additions & 2 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,7 @@ void AppLayerHtpPrintStats(void)
* \param direction flow direction
* \retval files files ptr
*/
static AppLayerGetFileState HTPGetTxFiles(void *state, void *txv, uint8_t direction)
static AppLayerGetFileState HTPGetTxFiles(void *txv, uint8_t direction)
{
AppLayerGetFileState files = { .fc = NULL, .cfg = &htp_sbcfg };
htp_tx_t *tx = (htp_tx_t *)txv;
Expand Down Expand Up @@ -6502,7 +6502,7 @@ libhtp:\n\
void *tx_ptr = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx_ptr);

AppLayerGetFileState files = HTPGetTxFiles(http_state, tx_ptr, STREAM_TOCLIENT);
AppLayerGetFileState files = HTPGetTxFiles(tx_ptr, STREAM_TOCLIENT);
FileContainer *ffc = files.fc;
FAIL_IF_NULL(ffc);

Expand Down
13 changes: 6 additions & 7 deletions src/app-layer-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ typedef struct AppLayerParserProtoCtx_

/** get FileContainer reference from the TX. MUST return a non-NULL reference if the TX
* has or may have files in the requested direction at some point. */
AppLayerGetFileState (*GetTxFiles)(void *, void *, uint8_t);
AppLayerGetFileState (*GetTxFiles)(void *, uint8_t);

int (*StateGetProgress)(void *alstate, uint8_t direction);
uint64_t (*StateGetTxCnt)(void *alstate);
Expand Down Expand Up @@ -441,8 +441,8 @@ void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto,
SCReturn;
}

void AppLayerParserRegisterGetTxFilesFunc(uint8_t ipproto, AppProto alproto,
AppLayerGetFileState (*GetTxFiles)(void *, void *, uint8_t))
void AppLayerParserRegisterGetTxFilesFunc(
uint8_t ipproto, AppProto alproto, AppLayerGetFileState (*GetTxFiles)(void *, uint8_t))
{
SCEnter();

Expand Down Expand Up @@ -868,13 +868,12 @@ AppLayerDecoderEvents *AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alp
SCReturnPtr(ptr, "AppLayerDecoderEvents *");
}

AppLayerGetFileState AppLayerParserGetTxFiles(
const Flow *f, void *state, void *tx, const uint8_t direction)
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
{
SCEnter();

if (alp_ctx.ctxs[f->protomap][f->alproto].GetTxFiles != NULL) {
return alp_ctx.ctxs[f->protomap][f->alproto].GetTxFiles(state, tx, direction);
return alp_ctx.ctxs[f->protomap][f->alproto].GetTxFiles(tx, direction);
}

AppLayerGetFileState files = { .fc = NULL, .cfg = NULL };
Expand All @@ -884,7 +883,7 @@ AppLayerGetFileState AppLayerParserGetTxFiles(
static void AppLayerParserFileTxHousekeeping(
const Flow *f, void *tx, const uint8_t pkt_dir, const bool trunc)
{
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, FlowGetAppState(f), tx, pkt_dir);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, tx, pkt_dir);
if (files.fc) {
FilesPrune(files.fc, files.cfg, trunc);
}
Expand Down
7 changes: 3 additions & 4 deletions src/app-layer-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto proto,
void *(*LocalStorageAlloc)(void), void (*LocalStorageFree)(void *));
// void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto proto,
// AppLayerDecoderEvents *(*StateGetEvents)(void *) __attribute__((nonnull)));
void AppLayerParserRegisterGetTxFilesFunc(uint8_t ipproto, AppProto alproto,
AppLayerGetFileState (*GetTxFiles)(void *, void *, uint8_t));
void AppLayerParserRegisterGetTxFilesFunc(
uint8_t ipproto, AppProto alproto, AppLayerGetFileState (*GetTxFiles)(void *, uint8_t));
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto);
void AppLayerParserRegisterLoggerBits(uint8_t ipproto, AppProto alproto, LoggerId bits);
void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto,
Expand Down Expand Up @@ -235,8 +235,7 @@ void AppLayerParserSetTransactionInspectId(const Flow *f, AppLayerParserState *p

AppLayerDecoderEvents *AppLayerParserGetDecoderEvents(AppLayerParserState *pstate);
AppLayerDecoderEvents *AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alproto, void *tx);
AppLayerGetFileState AppLayerParserGetTxFiles(
const Flow *f, void *state, void *tx, const uint8_t direction);
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction);
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto,
void *alstate, uint8_t direction);
uint64_t AppLayerParserGetTxCnt(const Flow *, void *alstate);
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-register.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ typedef struct AppLayerParser {
void *(*LocalStorageAlloc)(void);
void (*LocalStorageFree)(void *);

AppLayerGetFileState (*GetTxFiles)(void *, void *, uint8_t);
AppLayerGetFileState (*GetTxFiles)(void *, uint8_t);

AppLayerGetTxIterTuple (*GetTxIterator)(const uint8_t ipproto,
const AppProto alproto, void *alstate, uint64_t min_tx_id,
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-smtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@ static int SMTPStateGetAlstateProgress(void *vtx, uint8_t direction)
return tx->done;
}

static AppLayerGetFileState SMTPGetTxFiles(void *state, void *txv, uint8_t direction)
static AppLayerGetFileState SMTPGetTxFiles(void *txv, uint8_t direction)
{
AppLayerGetFileState files = { .fc = NULL, .cfg = &smtp_config.sbcfg };
SMTPTransaction *tx = (SMTPTransaction *)txv;
Expand Down
2 changes: 1 addition & 1 deletion src/detect-engine-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ uint8_t DetectFileInspectGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx
DEBUG_VALIDATE_BUG_ON(f->alstate != alstate);

const uint8_t direction = flags & (STREAM_TOSERVER|STREAM_TOCLIENT);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, alstate, tx, direction);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, tx, direction);
FileContainer *ffc = files.fc;
SCLogDebug("tx %p tx_id %" PRIu64 " ffc %p ffc->head %p sid %u", tx, tx_id, ffc,
ffc ? ffc->head : NULL, s->id);
Expand Down
22 changes: 11 additions & 11 deletions src/detect-engine-state.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ static int DeStateSigTest03(void)
SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
FAIL_IF(!(PacketAlertCheck(p, 1)));

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);

Expand Down Expand Up @@ -791,7 +791,7 @@ static int DeStateSigTest04(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand Down Expand Up @@ -866,7 +866,7 @@ static int DeStateSigTest05(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand Down Expand Up @@ -952,7 +952,7 @@ static int DeStateSigTest06(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand Down Expand Up @@ -1040,7 +1040,7 @@ static int DeStateSigTest07(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand Down Expand Up @@ -1139,7 +1139,7 @@ static int DeStateSigTest08(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand All @@ -1166,7 +1166,7 @@ static int DeStateSigTest08(void)
tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
fc = files.fc;
FAIL_IF_NULL(fc);
file = fc->head;
Expand Down Expand Up @@ -1267,7 +1267,7 @@ static int DeStateSigTest09(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand All @@ -1294,7 +1294,7 @@ static int DeStateSigTest09(void)
tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
fc = files.fc;
FAIL_IF_NULL(fc);
file = fc->head;
Expand Down Expand Up @@ -1393,7 +1393,7 @@ static int DeStateSigTest10(void)
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
FileContainer *fc = files.fc;
FAIL_IF_NULL(fc);
File *file = fc->head;
Expand All @@ -1420,7 +1420,7 @@ static int DeStateSigTest10(void)
tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);

files = AppLayerParserGetTxFiles(p->flow, http_state, tx, STREAM_TOSERVER);
files = AppLayerParserGetTxFiles(p->flow, tx, STREAM_TOSERVER);
fc = files.fc;
FAIL_IF_NULL(fc);
file = fc->head;
Expand Down
4 changes: 2 additions & 2 deletions src/detect-file-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ uint8_t DetectEngineInspectFiledata(DetectEngineCtx *de_ctx, DetectEngineThreadC
transforms = engine->v2.transforms;
}

AppLayerGetFileState files = AppLayerParserGetTxFiles(f, alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
FileContainer *ffc = files.fc;
if (ffc == NULL) {
return DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES;
Expand Down Expand Up @@ -448,7 +448,7 @@ static void PrefilterTxFiledata(DetectEngineThreadCtx *det_ctx, const void *pect
const MpmCtx *mpm_ctx = ctx->mpm_ctx;
const int list_id = ctx->list_id;

AppLayerGetFileState files = AppLayerParserGetTxFiles(f, f->alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
FileContainer *ffc = files.fc;
if (ffc != NULL) {
int local_file_id = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/detect-filemagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static uint8_t DetectEngineInspectFilemagic(DetectEngineCtx *de_ctx, DetectEngin
transforms = engine->v2.transforms;
}

AppLayerGetFileState files = AppLayerParserGetTxFiles(f, alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
FileContainer *ffc = files.fc;
if (ffc == NULL) {
return DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES;
Expand Down Expand Up @@ -358,7 +358,7 @@ static void PrefilterTxFilemagic(DetectEngineThreadCtx *det_ctx, const void *pec
const MpmCtx *mpm_ctx = ctx->mpm_ctx;
const int list_id = ctx->list_id;

AppLayerGetFileState files = AppLayerParserGetTxFiles(f, f->alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
FileContainer *ffc = files.fc;
if (ffc != NULL) {
int local_file_id = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/detect-filename.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static uint8_t DetectEngineInspectFilename(DetectEngineCtx *de_ctx, DetectEngine
transforms = engine->v2.transforms;
}

AppLayerGetFileState files = AppLayerParserGetTxFiles(f, alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
FileContainer *ffc = files.fc;
if (ffc == NULL) {
return DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES;
Expand Down Expand Up @@ -295,7 +295,7 @@ static void PrefilterTxFilename(DetectEngineThreadCtx *det_ctx, const void *pect
const MpmCtx *mpm_ctx = ctx->mpm_ctx;
const int list_id = ctx->list_id;

AppLayerGetFileState files = AppLayerParserGetTxFiles(f, f->alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
FileContainer *ffc = files.fc;
if (ffc != NULL) {
int local_file_id = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/detect-filestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ static int DetectFilestorePostMatch(DetectEngineThreadCtx *det_ctx,
p->flow->proto, p->flow->alproto, alstate, det_ctx->filestore[u].tx_id);
DEBUG_VALIDATE_BUG_ON(txv == NULL);
if (txv) {
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, alstate, txv, flags);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, txv, flags);
FileContainer *ffc_tx = files.fc;
DEBUG_VALIDATE_BUG_ON(ffc_tx == NULL);
if (ffc_tx) {
Expand Down
3 changes: 1 addition & 2 deletions src/output-json-alert.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ static void AlertAddFiles(const Packet *p, JsonBuilder *jb, const uint64_t tx_id
if (p->flow->alstate != NULL) {
void *tx = AppLayerParserGetTx(p->flow->proto, p->flow->alproto, p->flow->alstate, tx_id);
if (tx) {
AppLayerGetFileState files =
AppLayerParserGetTxFiles(p->flow, p->flow->alstate, tx, direction);
AppLayerGetFileState files = AppLayerParserGetTxFiles(p->flow, tx, direction);
ffc = files.fc;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/output-tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ static inline void OutputTxLogFiles(ThreadVars *tv, OutputFileLoggerThreadData *
opposing_dir == STREAM_TOSERVER ? "TOSERVER" : "TOCLIENT", packet_dir_ready,
opposing_dir_ready);

AppLayerGetFileState app_files =
AppLayerParserGetTxFiles(f, FlowGetAppState(f), tx, packet_dir);
AppLayerGetFileState app_files = AppLayerParserGetTxFiles(f, tx, packet_dir);
FileContainer *ffc = app_files.fc;
AppLayerGetFileState app_files_opposing =
AppLayerParserGetTxFiles(f, FlowGetAppState(f), tx, opposing_dir);
AppLayerGetFileState app_files_opposing = AppLayerParserGetTxFiles(f, tx, opposing_dir);
FileContainer *ffc_opposing = app_files_opposing.fc;

/* see if opposing side is finished: if no file support in this direction, of is not
Expand Down

0 comments on commit 0d267e2

Please sign in to comment.