• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23352517976

20 Mar 2026 04:32PM UTC coverage: 65.866% (-13.4%) from 79.315%
23352517976

Pull #15072

github

web-flow
Merge abcd1935f into 6587e363a
Pull Request #15072: Stack 8001 v16.3

41 of 70 new or added lines in 10 files covered. (58.57%)

18894 existing lines in 577 files now uncovered.

143735 of 218225 relevant lines covered (65.87%)

4342818.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.54
/src/app-layer-parser.c
1
/* Copyright (C) 2007-2026 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \file
20
 *
21
 * \author Victor Julien <victor@inliniac.net>
22
 *
23
 * Generic App-layer parsing functions.
24
 */
25

26
#include "suricata-common.h"
27
#include "app-layer-parser.h"
28

29
#include "flow.h"
30
#include "flow-private.h"
31
#include "flow-util.h"
32

33
#include "app-layer-frames.h"
34
#include "app-layer-events.h"
35

36
#include "stream-tcp.h"
37

38
#include "util-validate.h"
39
#include "util-config.h"
40

41
#include "app-layer.h"
42
#include "app-layer-detect-proto.h"
43

44
#include "app-layer-ftp.h"
45
#include "app-layer-smtp.h"
46

47
#include "app-layer-smb.h"
48
#include "app-layer-htp.h"
49
#include "app-layer-ssl.h"
50
#include "app-layer-ssh.h"
51
#include "app-layer-modbus.h"
52
#include "app-layer-dnp3.h"
53
#include "app-layer-nfs-tcp.h"
54
#include "app-layer-nfs-udp.h"
55
#include "app-layer-tftp.h"
56
#include "app-layer-ike.h"
57
#include "app-layer-http2.h"
58
#include "app-layer-imap.h"
59

60
struct AppLayerParserThreadCtx_ {
61
    void *(*alproto_local_storage)[FLOW_PROTO_MAX];
62
};
63

64

65
/**
66
 * \brief App layer protocol parser context.
67
 */
68
typedef struct AppLayerParserProtoCtx_
69
{
70
    /* 0 - to_server, 1 - to_client. */
71
    AppLayerParserFPtr Parser[2];
72

73
    bool logger;
74

75
    /* Indicates the direction the parser is ready to see the data
76
     * the first time for a flow.  Values accepted -
77
     * STREAM_TOSERVER, STREAM_TOCLIENT */
78
    uint8_t first_data_dir;
79

80
    uint32_t logger_bits;   /**< registered loggers for this proto */
81

82
    void *(*StateAlloc)(void *, AppProto);
83
    void (*StateFree)(void *);
84
    void (*StateTransactionFree)(void *, uint64_t);
85
    void *(*LocalStorageAlloc)(void);
86
    void (*LocalStorageFree)(void *);
87

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

92
    int (*StateGetProgress)(void *alstate, uint8_t direction);
93
    uint64_t (*StateGetTxCnt)(void *alstate);
94
    void *(*StateGetTx)(void *alstate, uint64_t tx_id);
95
    AppLayerGetTxIteratorFunc StateGetTxIterator;
96
    int complete_ts;
97
    int complete_tc;
98
    int (*StateGetEventInfoById)(
99
            uint8_t event_id, const char **event_name, AppLayerEventType *event_type);
100
    int (*StateGetEventInfo)(
101
            const char *event_name, uint8_t *event_id, AppLayerEventType *event_type);
102

103
    AppLayerStateData *(*GetStateData)(void *state);
104
    AppLayerTxData *(*GetTxData)(void *tx);
105
    void (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig);
106

107
    void (*SetStreamDepthFlag)(void *tx, uint8_t flags);
108

109
    AppLayerParserGetFrameIdByNameFn GetFrameIdByName;
110
    AppLayerParserGetFrameNameByIdFn GetFrameNameById;
111

112
    AppLayerParserGetStateIdByNameFn GetStateIdByName;
113
    AppLayerParserGetStateNameByIdFn GetStateNameById;
114

115
    /* each app-layer has its own value */
116
    uint32_t stream_depth;
117

118
    /* Option flags such as supporting gaps or not. */
119
    uint32_t option_flags;
120
    /* coccinelle: AppLayerParserProtoCtx:option_flags:APP_LAYER_PARSER_OPT_ */
121

122
    uint32_t internal_flags;
123
    /* coccinelle: AppLayerParserProtoCtx:internal_flags:APP_LAYER_PARSER_INT_ */
124

125
#ifdef UNITTESTS
126
    void (*RegisterUnittests)(void);
127
#endif
128
} AppLayerParserProtoCtx;
129

130
typedef struct AppLayerParserCtx_ {
131
    AppLayerParserProtoCtx (*ctxs)[FLOW_PROTO_MAX];
132
    size_t ctxs_len;
133
} AppLayerParserCtx;
134

135
struct AppLayerParserState_ {
136
    /* coccinelle: AppLayerParserState:flags:APP_LAYER_PARSER_ */
137
    uint16_t flags;
138

139
    /* Indicates the current transaction that is being inspected.
140
     * We have a var per direction. */
141
    uint64_t inspect_id[2];
142
    /* Indicates the current transaction being logged.  Unlike inspect_id,
143
     * we don't need a var per direction since we don't log a transaction
144
     * unless we have the entire transaction. */
145
    uint64_t log_id;
146

147
    uint64_t min_id;
148

149
    /* Used to store decoder events. */
150
    AppLayerDecoderEvents *decoder_events;
151

152
    FramesContainer *frames;
153
};
154

155
enum ExceptionPolicy g_applayerparser_error_policy = EXCEPTION_POLICY_NOT_SET;
156

157
static void AppLayerConfig(void)
158
{
2,213✔
159
    g_applayerparser_error_policy = ExceptionPolicyParse("app-layer.error-policy", true);
2,213✔
160
}
2,213✔
161

162
enum ExceptionPolicy AppLayerErrorGetExceptionPolicy(void)
163
{
178✔
164
    return g_applayerparser_error_policy;
178✔
165
}
178✔
166

167
static void AppLayerParserFramesFreeContainer(FramesContainer *frames)
168
{
8,632✔
169
    if (frames != NULL) {
8,632✔
170
        FramesFree(&frames->toserver);
40✔
171
        FramesFree(&frames->toclient);
40✔
172
        SCFree(frames);
40✔
173
    }
40✔
174
}
8,632✔
175

176
void AppLayerFramesFreeContainer(Flow *f)
177
{
2✔
178
    if (f == NULL || f->alparser == NULL || f->alparser->frames == NULL)
2✔
179
        return;
×
180
    AppLayerParserFramesFreeContainer(f->alparser->frames);
2✔
181
    f->alparser->frames = NULL;
2✔
182
}
2✔
183

184
FramesContainer *AppLayerFramesGetContainer(const Flow *f)
185
{
1,442,004✔
186
    if (f == NULL || f->alparser == NULL)
1,442,129✔
187
        return NULL;
23,576✔
188
    return f->alparser->frames;
1,418,428✔
189
}
1,442,004✔
190

191
FramesContainer *AppLayerFramesSetupContainer(Flow *f)
192
{
4,729✔
193
#ifdef UNITTESTS
194
    if (f == NULL || f->alparser == NULL || (f->proto == IPPROTO_TCP && f->protoctx == NULL))
195
        return NULL;
196
#endif
197
    DEBUG_VALIDATE_BUG_ON(f == NULL || f->alparser == NULL);
4,729✔
198
    if (f->alparser->frames == NULL) {
4,729✔
199
        f->alparser->frames = SCCalloc(1, sizeof(FramesContainer));
40✔
200
        if (f->alparser->frames == NULL) {
40✔
201
            return NULL;
×
202
        }
×
203
#ifdef DEBUG
204
        f->alparser->frames->toserver.ipproto = f->proto;
205
        f->alparser->frames->toserver.alproto = f->alproto;
206
        f->alparser->frames->toclient.ipproto = f->proto;
207
        f->alparser->frames->toclient.alproto = f->alproto;
208
#endif
209
    }
40✔
210
    return f->alparser->frames;
4,729✔
211
}
4,729✔
212

213
#ifdef UNITTESTS
214
void UTHAppLayerParserStateGetIds(void *ptr, uint64_t *i1, uint64_t *i2, uint64_t *log, uint64_t *min)
215
{
216
    struct AppLayerParserState_ *s = ptr;
217
    *i1 = s->inspect_id[0];
218
    *i2 = s->inspect_id[1];
219
    *log = s->log_id;
220
    *min = s->min_id;
221
}
222
#endif
223

224
/* Static global version of the parser context.
225
 * Post 2.0 let's look at changing this to move it out to app-layer.c. */
226
static AppLayerParserCtx alp_ctx;
227

228
int AppLayerParserProtoIsRegistered(uint8_t ipproto, AppProto alproto)
229
{
488,924✔
230
    uint8_t ipproto_map = FlowGetProtoMapping(ipproto);
488,924✔
231

232
    return (alp_ctx.ctxs[alproto][ipproto_map].StateAlloc != NULL) ? 1 : 0;
488,924✔
233
}
488,924✔
234

235
AppLayerParserState *AppLayerParserStateAlloc(void)
236
{
8,626✔
237
    SCEnter();
8,626✔
238

239
    AppLayerParserState *pstate = (AppLayerParserState *)SCCalloc(1, sizeof(*pstate));
8,626✔
240
    if (pstate == NULL)
8,626✔
241
        goto end;
×
242

243
 end:
8,628✔
244
    SCReturnPtr(pstate, "AppLayerParserState");
8,628✔
245
}
8,626✔
246

247
void AppLayerParserStateFree(AppLayerParserState *pstate)
248
{
8,630✔
249
    SCEnter();
8,630✔
250

251
    if (pstate->decoder_events != NULL)
8,630✔
252
        SCAppLayerDecoderEventsFreeEvents(&pstate->decoder_events);
×
253
    AppLayerParserFramesFreeContainer(pstate->frames);
8,630✔
254
    SCFree(pstate);
8,630✔
255

256
    SCReturn;
8,630✔
257
}
8,630✔
258

259
int AppLayerParserSetup(void)
260
{
2,213✔
261
    SCEnter();
2,213✔
262
    // initial allocation that will later be grown using realloc,
263
    // when new protocols register themselves and make g_alproto_max grow
264
    alp_ctx.ctxs = SCCalloc(g_alproto_max, sizeof(AppLayerParserProtoCtx[FLOW_PROTO_MAX]));
2,213✔
265
    if (unlikely(alp_ctx.ctxs == NULL)) {
2,213✔
266
        FatalError("Unable to alloc alp_ctx.ctxs.");
×
267
    }
×
268
    alp_ctx.ctxs_len = g_alproto_max;
2,213✔
269
    SCReturnInt(0);
2,213✔
270
}
2,213✔
271

272
void AppLayerParserPostStreamSetup(void)
273
{
2,196✔
274
    /* lets set a default value for stream_depth */
275
    for (int flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
8,784✔
276
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
270,105✔
277
            if (!(alp_ctx.ctxs[alproto][flow_proto].internal_flags &
263,517✔
278
                        APP_LAYER_PARSER_INT_STREAM_DEPTH_SET)) {
263,517✔
279
                alp_ctx.ctxs[alproto][flow_proto].stream_depth = stream_config.reassembly_depth;
259,829✔
280
            }
259,829✔
281
        }
263,517✔
282
    }
6,588✔
283
}
2,196✔
284

285
int AppLayerParserDeSetup(void)
286
{
2,132✔
287
    SCEnter();
2,132✔
288

289
    SCFree(alp_ctx.ctxs);
2,132✔
290

291
    FTPParserCleanup();
2,132✔
292
    SMTPParserCleanup();
2,132✔
293

294
    SCReturnInt(0);
2,132✔
295
}
2,132✔
296

297
AppLayerParserThreadCtx *AppLayerParserThreadCtxAlloc(void)
298
{
18,018✔
299
    SCEnter();
18,018✔
300

301
    AppLayerParserThreadCtx *tctx = SCCalloc(1, sizeof(*tctx));
18,018✔
302
    if (tctx == NULL)
18,018✔
303
        goto end;
×
304

305
    tctx->alproto_local_storage = SCCalloc(g_alproto_max, sizeof(void *[FLOW_PROTO_MAX]));
18,018✔
306
    if (unlikely(tctx->alproto_local_storage == NULL)) {
18,018✔
307
        SCFree(tctx);
×
308
        tctx = NULL;
×
309
        goto end;
×
310
    }
×
311
    for (uint8_t flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
72,072✔
312
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
2,216,187✔
313
            uint8_t ipproto = FlowGetReverseProtoMapping(flow_proto);
2,162,133✔
314

315
            tctx->alproto_local_storage[alproto][flow_proto] =
2,162,133✔
316
                    AppLayerParserGetProtocolParserLocalStorage(ipproto, alproto);
2,162,133✔
317
        }
2,162,133✔
318
    }
54,054✔
319

320
 end:
18,018✔
321
    SCReturnPtr(tctx, "void *");
18,018✔
322
}
18,018✔
323

324
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
325
{
18,018✔
326
    SCEnter();
18,018✔
327

328
    for (uint8_t flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
72,072✔
329
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
2,216,187✔
330
            uint8_t ipproto = FlowGetReverseProtoMapping(flow_proto);
2,162,133✔
331

332
            AppLayerParserDestroyProtocolParserLocalStorage(
2,162,133✔
333
                    ipproto, alproto, tctx->alproto_local_storage[alproto][flow_proto]);
2,162,133✔
334
        }
2,162,133✔
335
    }
54,054✔
336

337
    SCFree(tctx->alproto_local_storage);
18,018✔
338
    SCFree(tctx);
18,018✔
339
    SCReturn;
18,018✔
340
}
18,018✔
341

342
/** \brief check if a parser is enabled in the config
343
 *  Returns enabled always if: were running unittests
344
 */
345
int SCAppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
346
{
81,750✔
347
    SCEnter();
81,750✔
348

349
    char param[100];
81,750✔
350
    SCConfNode *g_proto, *i_proto;
81,750✔
351
    bool g_enabled = false;
81,750✔
352
    bool i_enabled = false;
81,750✔
353
    int r;
81,750✔
354

355
    if (RunmodeIsUnittests())
81,750✔
UNCOV
356
        SCReturnInt(1);
×
357

358
    r = snprintf(param, sizeof(param), "%s%s%s%s%s", "app-layer.protocols.", alproto_name, ".",
81,750✔
359
            ipproto, ".enabled");
81,750✔
360
    if (r < 0) {
81,750✔
361
        FatalError("snprintf failure.");
×
362
    } else if (r > (int)sizeof(param)) {
81,750✔
363
        FatalError("buffer not big enough to write param.");
×
364
    }
×
365
    SCLogDebug("Looking for %s", param);
81,750✔
366

367
    i_proto = SCConfGetNode(param);
81,750✔
368
    if (i_proto && i_proto->val) {
81,750✔
369
        if (SCConfValIsTrue(i_proto->val)) {
6,008✔
370
            i_enabled = true;
6,008✔
371
        } else if (SCConfValIsFalse(i_proto->val)) {
6,008✔
372
            i_enabled = false;
×
373
        } else if (strcasecmp(i_proto->val, "detection-only") == 0) {
×
374
            i_enabled = false;
×
375
        } else {
×
376
            FatalError("Invalid value found for %s.", param);
×
377
        }
×
378
    }
6,008✔
379

380
    r = snprintf(param, sizeof(param), "%s%s%s", "app-layer.protocols.", alproto_name, ".enabled");
81,750✔
381
    if (r < 0) {
81,750✔
382
        FatalError("snprintf failure.");
×
383
    } else if (r > (int)sizeof(param)) {
81,750✔
384
        FatalError("buffer not big enough to write param.");
×
385
    }
×
386

387
    SCLogDebug("Looking for %s", param);
81,750✔
388
    g_proto = SCConfGetNode(param);
81,750✔
389
    if (g_proto && g_proto->val) {
81,750✔
390
        if (SCConfValIsTrue(g_proto->val)) {
42,051✔
391
            g_enabled = true;
42,043✔
392
        } else if (SCConfValIsFalse(g_proto->val)) {
42,043✔
393
            g_enabled = false;
4✔
394
        } else if (strcasecmp(g_proto->val, "detection-only") == 0) {
4✔
395
            g_enabled = false;
4✔
396
        } else {
4✔
397
            FatalError("Invalid value found for %s", param);
×
398
        }
×
399
    }
42,051✔
400

401
    if ((i_proto && g_proto) && (i_enabled ^ g_enabled)) {
81,750✔
402
        /* these checks are also performed for detection-only, no need to issue double warning */
403
        SCLogDebug("Inconsistent global (%s) and respective ipproto (%s) settings found for "
4✔
404
                   "alproto %s and ipproto %s",
4✔
405
                g_enabled ? "TRUE" : "FALSE", i_enabled ? "TRUE" : "FALSE", alproto_name, ipproto);
4✔
406
    }
4✔
407

408
    if (i_proto) {
81,750✔
409
        SCReturnInt(i_enabled);
6,008✔
410
    }
6,008✔
411
    if (g_proto) {
75,742✔
412
        SCReturnInt(g_enabled);
42,043✔
413
    }
42,043✔
414

415
    SCReturnInt(1);
75,742✔
416
}
75,742✔
417

418
/***** Parser related registration *****/
419

420
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto,
421
                      uint8_t direction,
422
                      AppLayerParserFPtr Parser)
423
{
163,654✔
424
    SCEnter();
163,654✔
425

426
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)]
163,654✔
427
            .Parser[(direction & STREAM_TOSERVER) ? 0 : 1] = Parser;
163,654✔
428

429
    SCReturnInt(0);
163,654✔
430
}
163,654✔
431

432
void SCAppLayerParserRegisterParserAcceptableDataDirection(
433
        uint8_t ipproto, AppProto alproto, uint8_t direction)
434
{
10,292✔
435
    SCEnter();
10,292✔
436

437
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].first_data_dir |=
10,292✔
438
            (direction & (STREAM_TOSERVER | STREAM_TOCLIENT));
10,292✔
439

440
    SCReturn;
10,292✔
441
}
10,292✔
442

443
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto,
444
        uint32_t flags)
445
{
19,136✔
446
    SCEnter();
19,136✔
447

448
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].option_flags |= flags;
19,136✔
449

450
    SCReturn;
19,136✔
451
}
19,136✔
452

453
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto,
454
        void *(*StateAlloc)(void *, AppProto), void (*StateFree)(void *))
455
{
81,827✔
456
    SCEnter();
81,827✔
457

458
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateAlloc = StateAlloc;
81,827✔
459
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateFree = StateFree;
81,827✔
460

461
    SCReturn;
81,827✔
462
}
81,827✔
463

464
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto,
465
                                 void *(*LocalStorageAlloc)(void),
466
                                 void (*LocalStorageFree)(void *))
467
{
4,426✔
468
    SCEnter();
4,426✔
469

470
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].LocalStorageAlloc = LocalStorageAlloc;
4,426✔
471
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].LocalStorageFree = LocalStorageFree;
4,426✔
472

473
    SCReturn;
4,426✔
474
}
4,426✔
475

476
void AppLayerParserRegisterGetTxFilesFunc(
477
        uint8_t ipproto, AppProto alproto, AppLayerGetFileState (*GetTxFiles)(void *, uint8_t))
478
{
17,677✔
479
    SCEnter();
17,677✔
480

481
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetTxFiles = GetTxFiles;
17,677✔
482

483
    SCReturn;
17,677✔
484
}
17,677✔
485

486
void AppLayerParserRegisterLoggerBits(uint8_t ipproto, AppProto alproto, LoggerId bits)
487
{
85,568✔
488
    SCEnter();
85,568✔
489

490
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].logger_bits = bits;
85,568✔
491

492
    SCReturn;
85,568✔
493
}
85,568✔
494

495
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
496
{
69,432✔
497
    SCEnter();
69,432✔
498

499
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].logger = true;
69,432✔
500

501
    SCReturn;
69,432✔
502
}
69,432✔
503

504
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto,
505
    int (*StateGetProgress)(void *alstate, uint8_t direction))
506
{
81,827✔
507
    SCEnter();
81,827✔
508

509
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetProgress = StateGetProgress;
81,827✔
510

511
    SCReturn;
81,827✔
512
}
81,827✔
513

514
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto,
515
                           void (*StateTransactionFree)(void *, uint64_t))
516
{
81,827✔
517
    SCEnter();
81,827✔
518

519
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateTransactionFree = StateTransactionFree;
81,827✔
520

521
    SCReturn;
81,827✔
522
}
81,827✔
523

524
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto,
525
                         uint64_t (*StateGetTxCnt)(void *alstate))
526
{
81,827✔
527
    SCEnter();
81,827✔
528

529
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetTxCnt = StateGetTxCnt;
81,827✔
530

531
    SCReturn;
81,827✔
532
}
81,827✔
533

534
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto,
535
                      void *(StateGetTx)(void *alstate, uint64_t tx_id))
536
{
81,827✔
537
    SCEnter();
81,827✔
538

539
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetTx = StateGetTx;
81,827✔
540

541
    SCReturn;
81,827✔
542
}
81,827✔
543

544
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto,
545
                      AppLayerGetTxIteratorFunc Func)
546
{
72,975✔
547
    SCEnter();
72,975✔
548
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetTxIterator = Func;
72,975✔
549
    SCReturn;
72,975✔
550
}
72,975✔
551

552
void AppLayerParserRegisterStateProgressCompletionStatus(
553
        AppProto alproto, const int ts, const int tc)
554
{
81,827✔
555
    BUG_ON(ts == 0);
81,827✔
556
    BUG_ON(tc == 0);
81,827✔
557
    BUG_ON(!AppProtoIsValid(alproto));
81,827✔
558
    BUG_ON(alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_ts != 0 &&
81,827✔
559
            alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_ts != ts);
81,827✔
560
    BUG_ON(alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_tc != 0 &&
81,827✔
561
            alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_tc != tc);
81,827✔
562

563
    alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_ts = ts;
81,827✔
564
    alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_tc = tc;
81,827✔
565
}
81,827✔
566

567
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto,
568
        int (*StateGetEventInfoById)(
569
                uint8_t event_id, const char **event_name, AppLayerEventType *event_type))
570
{
70,762✔
571
    SCEnter();
70,762✔
572

573
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetEventInfoById =
70,762✔
574
            StateGetEventInfoById;
70,762✔
575

576
    SCReturn;
70,762✔
577
}
70,762✔
578

579
void AppLayerParserRegisterGetStateFuncs(uint8_t ipproto, AppProto alproto,
580
        AppLayerParserGetStateIdByNameFn GetIdByNameFunc,
581
        AppLayerParserGetStateNameByIdFn GetNameByIdFunc)
582
{
6,639✔
583
    SCEnter();
6,639✔
584
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateIdByName = GetIdByNameFunc;
6,639✔
585
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateNameById = GetNameByIdFunc;
6,639✔
586
    SCReturn;
6,639✔
587
}
6,639✔
588

589
void AppLayerParserRegisterGetFrameFuncs(uint8_t ipproto, AppProto alproto,
590
        AppLayerParserGetFrameIdByNameFn GetIdByNameFunc,
591
        AppLayerParserGetFrameNameByIdFn GetNameByIdFunc)
592
{
47,875✔
593
    SCEnter();
47,875✔
594
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetFrameIdByName = GetIdByNameFunc;
47,875✔
595
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetFrameNameById = GetNameByIdFunc;
47,875✔
596
    SCReturn;
47,875✔
597
}
47,875✔
598

599
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto,
600
        int (*StateGetEventInfo)(
601
                const char *event_name, uint8_t *event_id, AppLayerEventType *event_type))
602
{
72,975✔
603
    SCEnter();
72,975✔
604

605
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetEventInfo = StateGetEventInfo;
72,975✔
606

607
    SCReturn;
72,975✔
608
}
72,975✔
609

610
void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto,
611
        AppLayerTxData *(*GetTxData)(void *tx))
612
{
81,827✔
613
    SCEnter();
81,827✔
614

615
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetTxData = GetTxData;
81,827✔
616

617
    SCReturn;
81,827✔
618
}
81,827✔
619

620
void AppLayerParserRegisterStateDataFunc(
621
        uint8_t ipproto, AppProto alproto, AppLayerStateData *(*GetStateData)(void *state))
622
{
81,827✔
623
    SCEnter();
81,827✔
624

625
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateData = GetStateData;
81,827✔
626

627
    SCReturn;
81,827✔
628
}
81,827✔
629

630
void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto,
631
        void (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig))
632
{
4,417✔
633
    SCEnter();
4,417✔
634

635
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].ApplyTxConfig = ApplyTxConfig;
4,417✔
636

637
    SCReturn;
4,417✔
638
}
4,417✔
639

640
void AppLayerParserRegisterSetStreamDepthFlag(uint8_t ipproto, AppProto alproto,
641
        void (*SetStreamDepthFlag)(void *tx, uint8_t flags))
642
{
2,213✔
643
    SCEnter();
2,213✔
644

645
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].SetStreamDepthFlag = SetStreamDepthFlag;
2,213✔
646

647
    SCReturn;
2,213✔
648
}
2,213✔
649

650
/***** Get and transaction functions *****/
651

652
void *AppLayerParserGetProtocolParserLocalStorage(uint8_t ipproto, AppProto alproto)
653
{
2,162,133✔
654
    SCEnter();
2,162,133✔
655
    void * r = NULL;
2,162,133✔
656

657
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].LocalStorageAlloc != NULL) {
2,162,133✔
658
        r = alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].LocalStorageAlloc();
36,036✔
659
    }
36,036✔
660

661
    SCReturnPtr(r, "void *");
2,162,133✔
662
}
2,162,133✔
663

664
void AppLayerParserDestroyProtocolParserLocalStorage(uint8_t ipproto, AppProto alproto,
665
                                          void *local_data)
666
{
2,162,133✔
667
    SCEnter();
2,162,133✔
668

669
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].LocalStorageFree != NULL) {
2,162,133✔
670
        alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].LocalStorageFree(local_data);
36,036✔
671
    }
36,036✔
672

673
    SCReturn;
2,162,133✔
674
}
2,162,133✔
675

676
/** \brief default tx iterator
677
 *
678
 *  Used if the app layer parser doesn't register its own iterator.
679
 *  Simply walks the tx_id space until it finds a tx. Uses 'state' to
680
 *  keep track of where it left off.
681
 *
682
 *  \retval txptr or NULL if no more txs in list
683
 */
684
static AppLayerGetTxIterTuple AppLayerDefaultGetTxIterator(
685
        const uint8_t ipproto, const AppProto alproto,
686
        void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
687
        AppLayerGetTxIterState *state)
688
{
250,147✔
689
    uint64_t ustate = *(uint64_t *)state;
250,147✔
690
    uint64_t tx_id = MAX(min_tx_id, ustate);
250,147✔
691
    for ( ; tx_id < max_tx_id; tx_id++) {
2,147,483,647✔
692
        void *tx_ptr = AppLayerParserGetTx(ipproto, alproto, alstate, tx_id);
195,624✔
693
        if (tx_ptr != NULL) {
195,631✔
694
            ustate = tx_id + 1;
195,631✔
695
            *state = *(AppLayerGetTxIterState *)&ustate;
195,631✔
696
            AppLayerGetTxIterTuple tuple = {
195,631✔
697
                .tx_ptr = tx_ptr,
195,631✔
698
                .tx_id = tx_id,
195,631✔
699
                .has_next = (tx_id + 1 < max_tx_id),
195,631✔
700
            };
195,631✔
701
            SCLogDebug("tuple: %p/%"PRIu64"/%s", tuple.tx_ptr, tuple.tx_id,
195,631✔
702
                    tuple.has_next ? "true" : "false");
195,631✔
703
            return tuple;
195,631✔
704
        }
195,631✔
705
    }
195,624✔
706

707
    AppLayerGetTxIterTuple no_tuple = { NULL, 0, false };
54,516✔
708
    return no_tuple;
54,516✔
709
}
250,147✔
710

711
AppLayerGetTxIteratorFunc AppLayerGetTxIterator(const uint8_t ipproto,
712
        const AppProto alproto)
713
{
720,003✔
714
    AppLayerGetTxIteratorFunc Func =
720,003✔
715
            alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetTxIterator;
720,003✔
716
    return Func ? Func : AppLayerDefaultGetTxIterator;
720,003✔
717
}
720,003✔
718

719
uint64_t AppLayerParserGetTransactionLogId(AppLayerParserState *pstate)
720
{
155,690✔
721
    SCEnter();
155,690✔
722

723
    SCReturnCT((pstate == NULL) ? 0 : pstate->log_id, "uint64_t");
155,690✔
724
}
155,690✔
725

726
uint64_t AppLayerParserGetMinId(AppLayerParserState *pstate)
727
{
16✔
728
    SCEnter();
16✔
729

730
    SCReturnCT((pstate == NULL) ? 0 : pstate->min_id, "uint64_t");
16✔
731
}
16✔
732

733
void AppLayerParserSetTransactionLogId(AppLayerParserState *pstate, uint64_t tx_id)
734
{
29,134✔
735
    SCEnter();
29,134✔
736

737
    if (pstate != NULL)
29,134✔
738
        pstate->log_id = tx_id;
29,134✔
739

740
    SCReturn;
29,134✔
741
}
29,134✔
742

743
uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction)
744
{
1,239,708✔
745
    SCEnter();
1,239,708✔
746

747
    if (pstate != NULL)
1,239,708✔
748
        SCReturnCT(pstate->inspect_id[(direction & STREAM_TOSERVER) ? 0 : 1], "uint64_t");
1,239,730✔
749

750
    DEBUG_VALIDATE_BUG_ON(1);
2,147,483,647✔
751
    SCReturnCT(0ULL, "uint64_t");
2,147,483,647✔
752
}
1,239,708✔
753

754
inline uint8_t AppLayerParserGetTxDetectProgress(AppLayerTxData *txd, const uint8_t dir)
755
{
×
756
    uint8_t p = (dir & STREAM_TOSERVER) ? txd->detect_progress_ts : txd->detect_progress_tc;
×
757
    return p;
×
758
}
×
759

760
static inline uint32_t GetTxLogged(AppLayerTxData *txd)
761
{
200,579✔
762
    return txd->logged;
200,579✔
763
}
200,579✔
764

765
void SCAppLayerTxDataCleanup(AppLayerTxData *txd)
766
{
60,145✔
767
    if (txd->de_state) {
60,145✔
768
        SCDetectEngineStateFree(txd->de_state);
2,360✔
769
    }
2,360✔
770
    if (txd->events) {
60,145✔
771
        SCAppLayerDecoderEventsFreeEvents(&txd->events);
635✔
772
    }
635✔
773
    if (txd->txbits) {
60,145✔
774
        SCGenericVarFree(txd->txbits);
8✔
775
    }
8✔
776
}
60,145✔
777

778
void AppLayerParserSetTransactionInspectId(const Flow *f, AppLayerParserState *pstate,
779
                                           void *alstate, const uint8_t flags,
780
                                           bool tag_txs_as_inspected)
781
{
140,859✔
782
    SCEnter();
140,859✔
783

784
    const int direction = (flags & STREAM_TOSERVER) ? 0 : 1;
140,859✔
785
    const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
140,859✔
786
    uint64_t idx = AppLayerParserGetTransactionInspectId(pstate, flags);
140,859✔
787
    const int state_done_progress = AppLayerParserGetStateProgressCompletionStatus(f->alproto, flags);
140,859✔
788
    const uint8_t ipproto = f->proto;
140,859✔
789
    const AppProto alproto = f->alproto;
140,859✔
790

791
    AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
140,859✔
792
    AppLayerGetTxIterState state = { 0 };
140,859✔
793

794
    SCLogDebug("called: %s, tag_txs_as_inspected %s",direction==0?"toserver":"toclient",
140,859✔
795
            tag_txs_as_inspected?"true":"false");
140,859✔
796

797
    /* mark all txs as inspected if the applayer progress is
798
     * at the 'end state'. */
799
    while (1) {
146,996✔
800
        AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, idx, total_txs, &state);
146,973✔
801
        if (ires.tx_ptr == NULL)
146,973✔
802
            break;
12,324✔
803

804
        void *tx = ires.tx_ptr;
134,649✔
805
        idx = ires.tx_id;
134,649✔
806

807
        int state_progress = AppLayerParserGetStateProgress(ipproto, alproto, tx, flags);
134,649✔
808
        if (state_progress < state_done_progress)
134,649✔
809
            break;
103,935✔
810

811
        AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx);
30,714✔
812
        if (tag_txs_as_inspected) {
30,714✔
813
            const uint8_t inspected_flag = (flags & STREAM_TOSERVER) ? APP_LAYER_TX_INSPECTED_TS
63✔
814
                                                                     : APP_LAYER_TX_INSPECTED_TC;
63✔
815
            if (txd->flags & inspected_flag) {
63✔
816
                txd->flags |= inspected_flag;
×
817
                SCLogDebug("%p/%" PRIu64 " in-order tx is done for direction %s. Flags %02x", tx,
×
818
                        idx, flags & STREAM_TOSERVER ? "toserver" : "toclient", txd->flags);
×
819
            }
×
820
        }
63✔
821
        idx++;
30,714✔
822
        if (!ires.has_next)
30,714✔
823
            break;
24,577✔
824
    }
30,714✔
825
    pstate->inspect_id[direction] = idx;
140,859✔
826
    SCLogDebug("inspect_id now %"PRIu64, pstate->inspect_id[direction]);
140,859✔
827

828
    /* if necessary we flag all txs that are complete as 'inspected'
829
     * also move inspect_id forward. */
830
    if (tag_txs_as_inspected) {
140,859✔
831
        /* continue at idx */
832
        while (1) {
2,121✔
833
            AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, idx, total_txs, &state);
2,121✔
834
            if (ires.tx_ptr == NULL)
2,121✔
835
                break;
2,057✔
836

837
            void *tx = ires.tx_ptr;
64✔
838
            /* if we got a higher id than the minimum we requested, we
839
             * skipped a bunch of 'null-txs'. Lets see if we can up the
840
             * inspect tracker */
841
            if (ires.tx_id > idx && pstate->inspect_id[direction] == idx) {
64✔
UNCOV
842
                pstate->inspect_id[direction] = ires.tx_id;
×
UNCOV
843
            }
×
844
            idx = ires.tx_id;
64✔
845

846
            const int state_progress = AppLayerParserGetStateProgress(ipproto, alproto, tx, flags);
64✔
847
            if (state_progress < state_done_progress)
64✔
848
                break;
64✔
849

850
            /* txd can be NULL for HTTP sessions where the user data alloc failed */
UNCOV
851
            AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx);
×
UNCOV
852
            const uint8_t inspected_flag = (flags & STREAM_TOSERVER) ? APP_LAYER_TX_INSPECTED_TS
×
UNCOV
853
                                                                     : APP_LAYER_TX_INSPECTED_TC;
×
UNCOV
854
            if (txd->flags & inspected_flag) {
×
855
                txd->flags |= inspected_flag;
×
856
                SCLogDebug("%p/%" PRIu64 " out of order tx is done for direction %s. Flag %02x", tx,
×
857
                        idx, flags & STREAM_TOSERVER ? "toserver" : "toclient", txd->flags);
×
858

859
                SCLogDebug("%p/%" PRIu64 " out of order tx. Update inspect_id? %" PRIu64, tx, idx,
×
860
                        pstate->inspect_id[direction]);
×
861
                if (pstate->inspect_id[direction] + 1 == idx)
×
862
                    pstate->inspect_id[direction] = idx;
×
863
            }
×
UNCOV
864
            if (!ires.has_next)
×
UNCOV
865
                break;
×
UNCOV
866
            idx++;
×
UNCOV
867
        }
×
868
    }
2,121✔
869

870
    SCReturn;
140,859✔
871
}
140,859✔
872

873
AppLayerDecoderEvents *AppLayerParserGetDecoderEvents(AppLayerParserState *pstate)
874
{
1,561,251✔
875
    SCEnter();
1,561,251✔
876

877
    SCReturnPtr(pstate->decoder_events,
1,561,251✔
878
                "AppLayerDecoderEvents *");
1,561,251✔
879
}
1,561,251✔
880

881
AppLayerDecoderEvents *AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alproto,
882
                                        void *tx)
883
{
160,132✔
884
    SCEnter();
160,132✔
885

886
    AppLayerDecoderEvents *ptr = NULL;
160,132✔
887

888
    /* Access events via the tx_data. */
889
    AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx);
160,132✔
890
    if (txd->events != NULL) {
160,132✔
891
        ptr = txd->events;
25,303✔
892
    }
25,303✔
893

894
    SCReturnPtr(ptr, "AppLayerDecoderEvents *");
160,132✔
895
}
160,132✔
896

897
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
898
{
536,370✔
899
    SCEnter();
536,370✔
900

901
    if (alp_ctx.ctxs[f->alproto][f->protomap].GetTxFiles != NULL) {
536,370✔
902
        return alp_ctx.ctxs[f->alproto][f->protomap].GetTxFiles(tx, direction);
532,621✔
903
    }
532,621✔
904

905
    AppLayerGetFileState files = { .fc = NULL, .cfg = NULL };
3,749✔
906
    return files;
3,749✔
907
}
536,370✔
908

909
static void AppLayerParserFileTxHousekeeping(
910
        const Flow *f, void *tx, const uint8_t pkt_dir, const bool trunc)
911
{
112,614✔
912
    AppLayerGetFileState files = AppLayerParserGetTxFiles(f, tx, pkt_dir);
112,614✔
913
    if (files.fc) {
112,614✔
914
        FilesPrune(files.fc, files.cfg, trunc);
112,614✔
915
    }
112,614✔
916
}
112,614✔
917

918
#define IS_DISRUPTED(flags) ((flags) & (STREAM_DEPTH | STREAM_GAP))
200,880,644✔
919

920
extern int g_detect_disabled;
921
extern bool g_file_logger_enabled;
922
extern bool g_filedata_logger_enabled;
923

924
/**
925
 * \brief remove obsolete (inspected and logged) transactions
926
 */
927
void AppLayerParserTransactionsCleanup(Flow *f, const uint8_t pkt_dir)
928
{
287,876✔
929
    SCEnter();
287,876✔
930
    DEBUG_ASSERT_FLOW_LOCKED(f);
287,876✔
931

932
    AppLayerParserProtoCtx *p = &alp_ctx.ctxs[f->alproto][f->protomap];
287,876✔
933
    if (unlikely(p->StateTransactionFree == NULL))
287,876✔
934
        SCReturn;
2,864✔
935

936
    const bool has_tx_detect_flags = !g_detect_disabled;
285,012✔
937
    const uint8_t ipproto = f->proto;
285,012✔
938
    const AppProto alproto = f->alproto;
285,012✔
939
    void * const alstate = f->alstate;
285,012✔
940
    AppLayerParserState * const alparser = f->alparser;
285,012✔
941

942
    if (alstate == NULL || alparser == NULL)
285,012✔
943
        SCReturn;
6✔
944

945
    const uint64_t min = alparser->min_id;
285,006✔
946
    const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
285,006✔
947
    const LoggerId logger_expectation = AppLayerParserProtocolGetLoggerBits(ipproto, alproto);
285,006✔
948
    const int tx_end_state_ts = AppLayerParserGetStateProgressCompletionStatus(alproto, STREAM_TOSERVER);
285,006✔
949
    const int tx_end_state_tc = AppLayerParserGetStateProgressCompletionStatus(alproto, STREAM_TOCLIENT);
285,006✔
950
    const uint8_t ts_disrupt_flags = FlowGetDisruptionFlags(f, STREAM_TOSERVER);
285,006✔
951
    const uint8_t tc_disrupt_flags = FlowGetDisruptionFlags(f, STREAM_TOCLIENT);
285,006✔
952

953
    int pkt_dir_trunc = -1;
285,006✔
954

955
    AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
285,006✔
956
    AppLayerGetTxIterState state;
285,006✔
957
    memset(&state, 0, sizeof(state));
285,006✔
958
    uint64_t i = min;
285,006✔
959
    uint64_t new_min = min;
285,006✔
960
    SCLogDebug("start min %"PRIu64, min);
285,006✔
961
    bool skipped = false;
285,006✔
962
    // const bool support_files = AppLayerParserSupportsFiles(f->proto, f->alproto);
963

964
    while (1) {
50,664,450✔
965
        AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, i, total_txs, &state);
50,664,450✔
966
        if (ires.tx_ptr == NULL)
50,664,450✔
967
            break;
105,873✔
968

969
        bool tx_skipped = false;
50,664,450✔
970
        void *tx = ires.tx_ptr;
50,558,577✔
971
        i = ires.tx_id; // actual tx id for the tx the IterFunc returned
50,558,577✔
972

973
        SCLogDebug("%p/%"PRIu64" checking", tx, i);
50,558,577✔
974
        AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx);
50,558,577✔
975
        if (AppLayerParserHasFilesInDir(txd, pkt_dir)) {
50,558,577✔
976
            if (pkt_dir_trunc == -1)
112,614✔
977
                pkt_dir_trunc = IS_DISRUPTED(
26,264✔
978
                        (pkt_dir == STREAM_TOSERVER) ? ts_disrupt_flags : tc_disrupt_flags);
112,614✔
979
            AppLayerParserFileTxHousekeeping(f, tx, pkt_dir, (bool)pkt_dir_trunc);
112,614✔
980
        }
112,614✔
981
        // should be reset by parser next time it updates the tx
982
        if (pkt_dir & STREAM_TOSERVER) {
50,558,577✔
983
            txd->updated_ts = false;
50,287,123✔
984
        } else {
50,287,123✔
985
            txd->updated_tc = false;
271,454✔
986
        }
271,454✔
987
        const int tx_progress_tc =
50,558,577✔
988
                AppLayerParserGetStateProgress(ipproto, alproto, tx, tc_disrupt_flags);
50,558,577✔
989
        if (tx_progress_tc < tx_end_state_tc) {
50,558,577✔
990
            SCLogDebug("%p/%"PRIu64" skipping: tc parser not done", tx, i);
303,445✔
991
            skipped = true;
303,445✔
992
            goto next;
303,445✔
993
        }
303,445✔
994
        const int tx_progress_ts =
50,255,132✔
995
                AppLayerParserGetStateProgress(ipproto, alproto, tx, ts_disrupt_flags);
50,255,132✔
996
        if (tx_progress_ts < tx_end_state_ts) {
50,255,132✔
997
            SCLogDebug("%p/%"PRIu64" skipping: ts parser not done", tx, i);
33,802✔
998
            skipped = true;
33,802✔
999
            goto next;
33,802✔
1000
        }
33,802✔
1001

1002
        if (has_tx_detect_flags) {
50,221,330✔
1003
            if (!IS_DISRUPTED(ts_disrupt_flags) &&
50,213,595✔
1004
                    (f->sgh_toserver != NULL || (f->flags & FLOW_SGH_TOSERVER) == 0)) {
50,213,595✔
1005
                if ((txd->flags & (APP_LAYER_TX_INSPECTED_TS | APP_LAYER_TX_SKIP_INSPECT_TS)) ==
50,206,140✔
1006
                        0) {
50,206,140✔
1007
                    SCLogDebug("%p/%" PRIu64 " skipping: TS inspect not done: ts:%02x", tx, i,
1,186✔
1008
                            txd->flags);
1,186✔
1009
                    tx_skipped = true;
1,186✔
1010
                }
1,186✔
1011
            }
50,206,140✔
1012
            if (!IS_DISRUPTED(tc_disrupt_flags) &&
50,213,595✔
1013
                    (f->sgh_toclient != NULL || (f->flags & FLOW_SGH_TOCLIENT) == 0)) {
50,213,595✔
1014
                if ((txd->flags & (APP_LAYER_TX_INSPECTED_TC | APP_LAYER_TX_SKIP_INSPECT_TC)) ==
50,170,874✔
1015
                        0) {
50,170,874✔
1016
                    SCLogDebug("%p/%" PRIu64 " skipping: TC inspect not done: ts:%02x", tx, i,
50,016,109✔
1017
                            txd->flags);
50,016,109✔
1018
                    tx_skipped = true;
50,016,109✔
1019
                }
50,016,109✔
1020
            }
50,170,874✔
1021
        }
50,213,595✔
1022

1023
        if (tx_skipped) {
50,221,330✔
1024
            SCLogDebug("%p/%" PRIu64 " tx_skipped", tx, i);
50,017,235✔
1025
            skipped = true;
50,017,235✔
1026
            goto next;
50,017,235✔
1027
        }
50,017,235✔
1028

1029
        if (logger_expectation != 0) {
204,095✔
1030
            LoggerId tx_logged = GetTxLogged(txd);
200,579✔
1031
            if (tx_logged != logger_expectation) {
200,579✔
1032
                SCLogDebug("%p/%"PRIu64" skipping: logging not done: want:%"PRIx32", have:%"PRIx32,
530✔
1033
                        tx, i, logger_expectation, tx_logged);
530✔
1034
                skipped = true;
530✔
1035
                goto next;
530✔
1036
            }
530✔
1037
        }
200,579✔
1038

1039
        /* if file logging is enabled, we keep a tx active while some of the files aren't
1040
         * logged yet. */
1041
        SCLogDebug("files_opened %u files_logged %u files_stored %u", txd->files_opened,
203,565✔
1042
                txd->files_logged, txd->files_stored);
203,565✔
1043

1044
        if (txd->files_opened) {
203,565✔
1045
            if (g_file_logger_enabled && txd->files_opened != txd->files_logged) {
175,478✔
1046
                skipped = true;
13✔
1047
                goto next;
13✔
1048
            }
13✔
1049
            if (g_filedata_logger_enabled && txd->files_opened != txd->files_stored) {
175,465✔
1050
                skipped = true;
172,815✔
1051
                goto next;
172,815✔
1052
            }
172,815✔
1053
        }
175,465✔
1054

1055
        /* if we are here, the tx can be freed. */
1056
        p->StateTransactionFree(alstate, i);
30,737✔
1057
        SCLogDebug("%p/%"PRIu64" freed", tx, i);
30,737✔
1058

1059
        /* if we didn't skip any tx so far, up the minimum */
1060
        SCLogDebug("skipped? %s i %"PRIu64", new_min %"PRIu64, skipped ? "true" : "false", i, new_min);
30,737✔
1061
        if (!skipped)
30,737✔
1062
            new_min = i + 1;
23,696✔
1063
        SCLogDebug("final i %"PRIu64", new_min %"PRIu64, i, new_min);
30,737✔
1064

1065
next:
50,558,556✔
1066
        if (!ires.has_next) {
50,558,556✔
1067
            /* this was the last tx. See if we skipped any. If not
1068
             * we removed all and can update the minimum to the max
1069
             * id. */
1070
            SCLogDebug("no next: cur tx i %"PRIu64", total %"PRIu64, i, total_txs);
179,129✔
1071
            if (!skipped) {
179,129✔
1072
                new_min = total_txs;
18,555✔
1073
                SCLogDebug("no next: cur tx i %"PRIu64", total %"PRIu64": "
18,555✔
1074
                        "new_min updated to %"PRIu64, i, total_txs, new_min);
18,555✔
1075
            }
18,555✔
1076
            break;
179,129✔
1077
        }
179,129✔
1078
        i++;
50,379,427✔
1079
    }
50,379,427✔
1080

1081
    /* see if we need to bring all trackers up to date. */
1082
    SCLogDebug("update f->alparser->min_id? %"PRIu64" vs %"PRIu64, new_min, alparser->min_id);
284,985✔
1083
    if (new_min > alparser->min_id) {
284,985✔
1084
        const uint64_t next_id = new_min;
23,335✔
1085
        alparser->min_id = next_id;
23,335✔
1086
        alparser->inspect_id[0] = MAX(alparser->inspect_id[0], next_id);
23,335✔
1087
        alparser->inspect_id[1] = MAX(alparser->inspect_id[1], next_id);
23,335✔
1088
        alparser->log_id = MAX(alparser->log_id, next_id);
23,335✔
1089
        SCLogDebug("updated f->alparser->min_id %"PRIu64, alparser->min_id);
23,335✔
1090
    }
23,335✔
1091
    SCReturn;
284,985✔
1092
}
285,006✔
1093

1094
static inline int StateGetProgressCompletionStatus(const AppProto alproto, const uint8_t flags)
1095
{
1,508,530✔
1096
    if (flags & STREAM_TOSERVER) {
1,508,530✔
1097
        return alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_ts;
757,458✔
1098
    } else if (flags & STREAM_TOCLIENT) {
757,458✔
1099
        return alp_ctx.ctxs[alproto][FLOW_PROTO_DEFAULT].complete_tc;
751,994✔
1100
    } else {
2,147,484,848✔
1101
        DEBUG_VALIDATE_BUG_ON(1);
2,147,483,647✔
1102
        return 0;
2,147,483,647✔
1103
    }
2,147,483,647✔
1104
}
1,508,530✔
1105

1106
/**
1107
 *  \brief get the progress value for a tx/protocol
1108
 *
1109
 *  If the stream is disrupted, we return the 'completion' value.
1110
 */
1111
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto,
1112
                        void *alstate, uint8_t flags)
1113
{
106,975,870✔
1114
    SCEnter();
106,975,870✔
1115
    int r;
106,975,870✔
1116
    if (unlikely(IS_DISRUPTED(flags))) {
106,975,870✔
1117
        r = StateGetProgressCompletionStatus(alproto, flags);
83,518✔
1118
    } else {
106,892,352✔
1119
        uint8_t direction = flags & (STREAM_TOCLIENT | STREAM_TOSERVER);
106,892,352✔
1120
        r = alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetProgress(
106,892,352✔
1121
                alstate, direction);
106,892,352✔
1122
    }
106,892,352✔
1123
    SCReturnInt(r);
106,975,870✔
1124
}
106,975,870✔
1125

1126
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
1127
{
1,105,874✔
1128
    SCEnter();
1,105,874✔
1129
    uint64_t r = alp_ctx.ctxs[f->alproto][f->protomap].StateGetTxCnt(alstate);
1,105,874✔
1130
    SCReturnCT(r, "uint64_t");
1,105,874✔
1131
}
1,105,874✔
1132

1133
void *AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
1134
{
215,313✔
1135
    SCEnter();
215,313✔
1136
    void *r = alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].StateGetTx(alstate, tx_id);
215,313✔
1137
    SCReturnPtr(r, "void *");
215,313✔
1138
}
215,313✔
1139

1140
int AppLayerParserGetStateProgressCompletionStatus(AppProto alproto,
1141
                                                   uint8_t direction)
1142
{
1,424,929✔
1143
    SCEnter();
1,424,929✔
1144
    int r = StateGetProgressCompletionStatus(alproto, direction);
1,424,929✔
1145
    SCReturnInt(r);
1,424,929✔
1146
}
1,424,929✔
1147

1148
int AppLayerParserGetEventInfo(uint8_t ipproto, AppProto alproto, const char *event_name,
1149
        uint8_t *event_id, AppLayerEventType *event_type)
1150
{
2,341✔
1151
    SCEnter();
2,341✔
1152
    const int ipproto_map = FlowGetProtoMapping(ipproto);
2,341✔
1153
    int r = (alp_ctx.ctxs[alproto][ipproto_map].StateGetEventInfo == NULL)
2,341✔
1154
                    ? -1
2,341✔
1155
                    : alp_ctx.ctxs[alproto][ipproto_map].StateGetEventInfo(
2,341✔
1156
                              event_name, event_id, event_type);
2,341✔
1157
    SCReturnInt(r);
2,341✔
1158
}
2,341✔
1159

1160
int AppLayerParserGetEventInfoById(uint8_t ipproto, AppProto alproto, uint8_t event_id,
1161
        const char **event_name, AppLayerEventType *event_type)
1162
{
841✔
1163
    SCEnter();
841✔
1164
    const int ipproto_map = FlowGetProtoMapping(ipproto);
841✔
1165
    *event_name = (const char *)NULL;
841✔
1166
    int r = (alp_ctx.ctxs[alproto][ipproto_map].StateGetEventInfoById == NULL)
841✔
1167
                    ? -1
841✔
1168
                    : alp_ctx.ctxs[alproto][ipproto_map].StateGetEventInfoById(
841✔
1169
                              event_id, event_name, event_type);
841✔
1170
    SCReturnInt(r);
841✔
1171
}
841✔
1172

1173
uint8_t AppLayerParserGetFirstDataDir(uint8_t ipproto, AppProto alproto)
1174
{
5,845✔
1175
    SCEnter();
5,845✔
1176
    uint8_t r = alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].first_data_dir;
5,845✔
1177
    SCReturnCT(r, "uint8_t");
5,845✔
1178
}
5,845✔
1179

1180
uint64_t AppLayerParserGetTransactionActive(const Flow *f,
1181
        AppLayerParserState *pstate, uint8_t direction)
1182
{
15,761✔
1183
    SCEnter();
15,761✔
1184

1185
    uint64_t active_id;
15,761✔
1186
    uint64_t log_id = pstate->log_id;
15,761✔
1187
    uint64_t inspect_id = pstate->inspect_id[(direction & STREAM_TOSERVER) ? 0 : 1];
15,761✔
1188
    if (alp_ctx.ctxs[f->alproto][f->protomap].logger) {
15,761✔
1189
        active_id = MIN(log_id, inspect_id);
10,500✔
1190
    } else {
10,500✔
1191
        active_id = inspect_id;
5,261✔
1192
    }
5,261✔
1193

1194
    SCReturnCT(active_id, "uint64_t");
15,761✔
1195
}
15,761✔
1196

1197
bool AppLayerParserSupportsFiles(uint8_t ipproto, AppProto alproto)
1198
{
345,591✔
1199
    // Custom case for only signature-only protocol so far
1200
    if (alproto == ALPROTO_HTTP) {
345,591✔
1201
        return AppLayerParserSupportsFiles(ipproto, ALPROTO_HTTP1) ||
11,310✔
1202
               AppLayerParserSupportsFiles(ipproto, ALPROTO_HTTP2);
11,310✔
1203
    }
11,310✔
1204
    return alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetTxFiles != NULL;
334,281✔
1205
}
345,591✔
1206

1207
AppLayerTxData *AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
1208
{
56,366,109✔
1209
    SCEnter();
56,366,109✔
1210
    AppLayerTxData *d = alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetTxData(tx);
56,366,109✔
1211
    SCReturnPtr(d, "AppLayerTxData");
56,366,109✔
1212
}
56,366,109✔
1213

1214
AppLayerStateData *AppLayerParserGetStateData(uint8_t ipproto, AppProto alproto, void *state)
1215
{
9,428✔
1216
    SCEnter();
9,428✔
1217
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateData) {
9,428✔
1218
        AppLayerStateData *d =
9,427✔
1219
                alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateData(state);
9,427✔
1220
        SCReturnPtr(d, "AppLayerStateData");
9,427✔
1221
    }
9,427✔
1222
    SCReturnPtr(NULL, "AppLayerStateData");
9,428✔
1223
}
9,428✔
1224

1225
void AppLayerParserApplyTxConfig(uint8_t ipproto, AppProto alproto,
1226
        void *state, void *tx, enum ConfigAction mode, AppLayerTxConfig config)
1227
{
1✔
1228
    SCEnter();
1✔
1229
    const int ipproto_map = FlowGetProtoMapping(ipproto);
1✔
1230
    if (alp_ctx.ctxs[alproto][ipproto_map].ApplyTxConfig) {
1✔
1231
        alp_ctx.ctxs[alproto][ipproto_map].ApplyTxConfig(state, tx, mode, config);
1✔
1232
    }
1✔
1233
    SCReturn;
1✔
1234
}
1✔
1235

1236
/***** General *****/
1237

1238
static inline void SetEOFFlags(AppLayerParserState *pstate, const uint8_t flags)
1239
{
186,443✔
1240
    if ((flags & (STREAM_EOF|STREAM_TOSERVER)) == (STREAM_EOF|STREAM_TOSERVER)) {
186,443✔
1241
        SCLogDebug("setting APP_LAYER_PARSER_EOF_TS");
4,876✔
1242
        SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_EOF_TS);
4,876✔
1243
    } else if ((flags & (STREAM_EOF|STREAM_TOCLIENT)) == (STREAM_EOF|STREAM_TOCLIENT)) {
181,567✔
1244
        SCLogDebug("setting APP_LAYER_PARSER_EOF_TC");
4,590✔
1245
        SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_EOF_TC);
4,590✔
1246
    }
4,590✔
1247
}
186,443✔
1248

1249
/** \internal
1250
 *  \brief create/close stream frames
1251
 *  On first invocation of TCP parser in a direction, create a <alproto>.stream frame.
1252
 *  On STREAM_EOF, set the final length. */
1253
static void HandleStreamFrames(Flow *f, StreamSlice stream_slice, const uint8_t *input,
1254
        const uint32_t input_len, const uint8_t flags)
1255
{
186,413✔
1256
    const uint8_t direction = (flags & STREAM_TOSERVER) ? 0 : 1;
186,413✔
1257
    AppLayerParserState *pstate = f->alparser;
186,413✔
1258

1259
    /* setup the generic stream frame */
1260
    if (((direction == 0 && (pstate->flags & APP_LAYER_PARSER_SFRAME_TS) == 0) ||
186,413✔
1261
                (direction == 1 && (pstate->flags & APP_LAYER_PARSER_SFRAME_TC) == 0)) &&
186,413✔
1262
            input != NULL && f->proto == IPPROTO_TCP) {
186,413✔
1263
        Frame *frame = AppLayerFrameGetLastOpenByType(f, direction, FRAME_STREAM_TYPE);
12,503✔
1264
        if (frame == NULL) {
12,503✔
1265
            int64_t frame_len = -1;
12,503✔
1266
            if (flags & STREAM_EOF)
12,503✔
1267
                frame_len = input_len;
220✔
1268

1269
            frame = AppLayerFrameNewByAbsoluteOffset(
12,503✔
1270
                    f, &stream_slice, stream_slice.offset, frame_len, direction, FRAME_STREAM_TYPE);
12,503✔
1271
            if (frame) {
12,503✔
1272
                SCLogDebug("opened: frame %p id %" PRIi64, frame, frame->id);
28✔
1273
                frame->flags = FRAME_FLAG_ENDS_AT_EOF; // TODO logic is not yet implemented
28✔
1274
                DEBUG_VALIDATE_BUG_ON(
28✔
1275
                        frame->id != 1); // should always be the first frame that is created
28✔
1276
            }
28✔
1277
            if (direction == 0) {
12,503✔
1278
                pstate->flags |= APP_LAYER_PARSER_SFRAME_TS;
6,571✔
1279
            } else {
6,571✔
1280
                pstate->flags |= APP_LAYER_PARSER_SFRAME_TC;
5,932✔
1281
            }
5,932✔
1282
        }
12,503✔
1283
    } else if (flags & STREAM_EOF) {
173,910✔
1284
        Frame *frame = AppLayerFrameGetLastOpenByType(f, direction, FRAME_STREAM_TYPE);
9,246✔
1285
        SCLogDebug("EOF closing: frame %p", frame);
9,246✔
1286
        if (frame) {
9,246✔
1287
            /* calculate final frame length */
1288
            int64_t slice_o = (int64_t)stream_slice.offset - (int64_t)frame->offset;
26✔
1289
            int64_t frame_len = slice_o + (int64_t)input_len;
26✔
1290
            SCLogDebug("%s: EOF frame->offset %" PRIu64 " -> %" PRIi64 ": o %" PRIi64,
26✔
1291
                    AppProtoToString(f->alproto), frame->offset, frame_len, slice_o);
26✔
1292
            frame->len = frame_len;
26✔
1293
        }
26✔
1294
    }
9,246✔
1295
}
186,413✔
1296

1297
static void Setup(Flow *f, const uint8_t direction, const uint8_t *input, uint32_t input_len,
1298
        const uint8_t flags, StreamSlice *as)
1299
{
186,436✔
1300
    memset(as, 0, sizeof(*as));
186,436✔
1301
    as->input = input;
186,436✔
1302
    as->input_len = input_len;
186,436✔
1303
    as->flags = flags;
186,436✔
1304

1305
    if (f->proto == IPPROTO_TCP && f->protoctx != NULL) {
186,436✔
1306
        TcpSession *ssn = f->protoctx;
163,999✔
1307
        TcpStream *stream = (direction & STREAM_TOSERVER) ? &ssn->client : &ssn->server;
163,999✔
1308
        as->offset = STREAM_APP_PROGRESS(stream);
163,999✔
1309
    }
163,999✔
1310
}
186,436✔
1311

1312
/** \retval int -1 in case of unrecoverable error. App-layer tracking stops for this flow.
1313
 *  \retval int 0 ok: we did not update app_progress
1314
 *  \retval int 1 ok: we updated app_progress */
1315
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto,
1316
                        uint8_t flags, const uint8_t *input, uint32_t input_len)
1317
{
189,047✔
1318
    SCEnter();
189,047✔
1319
#ifdef DEBUG_VALIDATION
1320
    BUG_ON(f->protomap != FlowGetProtoMapping(f->proto));
1321
#endif
1322
    AppLayerParserState *pstate = f->alparser;
189,047✔
1323
    AppLayerParserProtoCtx *p = &alp_ctx.ctxs[alproto][f->protomap];
189,047✔
1324
    StreamSlice stream_slice;
189,047✔
1325
    void *alstate = NULL;
189,047✔
1326
    uint64_t p_tx_cnt = 0;
189,047✔
1327
    uint32_t consumed = input_len;
189,047✔
1328
    const uint8_t direction = (flags & STREAM_TOSERVER) ? 0 : 1;
189,047✔
1329

1330
    /* we don't have the parser registered for this protocol */
1331
    if (p->StateAlloc == NULL) {
189,047✔
1332
        if (f->proto == IPPROTO_TCP) {
2,055✔
1333
            StreamTcpDisableAppLayer(f);
3✔
1334
        }
3✔
1335
        goto end;
2,055✔
1336
    }
2,055✔
1337

1338
    if (flags & STREAM_GAP) {
186,992✔
1339
        if (!(p->option_flags & APP_LAYER_PARSER_OPT_ACCEPT_GAPS)) {
582✔
1340
            SCLogDebug("app-layer parser does not accept gaps");
479✔
1341
            if (f->alstate != NULL && !FlowChangeProto(f)) {
479✔
1342
                SCAppLayerParserTriggerRawStreamInspection(f, direction);
479✔
1343
            }
479✔
1344
            AppLayerIncGapErrorCounter(tv, f);
479✔
1345
            goto error;
479✔
1346
        }
479✔
1347
    }
582✔
1348

1349
    /* Get the parser state (if any) */
1350
    if (pstate == NULL) {
186,513✔
1351
        f->alparser = pstate = AppLayerParserStateAlloc();
8,628✔
1352
        if (pstate == NULL) {
8,628✔
1353
            AppLayerIncAllocErrorCounter(tv, f);
×
1354
            goto error;
×
1355
        }
×
1356
    }
8,628✔
1357

1358
    SetEOFFlags(pstate, flags);
186,513✔
1359

1360
    alstate = f->alstate;
186,513✔
1361
    if (alstate == NULL || FlowChangeProto(f)) {
186,513✔
1362
        f->alstate = alstate = p->StateAlloc(alstate, f->alproto_orig);
8,625✔
1363
        if (alstate == NULL) {
8,625✔
1364
            AppLayerIncAllocErrorCounter(tv, f);
×
1365
            goto error;
×
1366
        }
×
1367
        SCLogDebug("alloced new app layer state %p (name %s)",
8,625✔
1368
                   alstate, AppLayerGetProtoName(f->alproto));
8,625✔
1369

1370
        /* set flow flags to state */
1371
        if (f->file_flags != 0) {
8,625✔
1372
            AppLayerStateData *sd = AppLayerParserGetStateData(f->proto, f->alproto, f->alstate);
6,609✔
1373
            if (sd != NULL) {
6,609✔
1374
                if ((sd->file_flags & f->file_flags) != f->file_flags) {
6,607✔
1375
                    SCLogDebug("state data: updating file_flags %04x with flow file_flags %04x",
6,607✔
1376
                            sd->file_flags, f->file_flags);
6,607✔
1377
                    sd->file_flags |= f->file_flags;
6,607✔
1378
                }
6,607✔
1379
            }
6,607✔
1380
        }
6,609✔
1381
    } else {
177,888✔
1382
        SCLogDebug("using existing app layer state %p (name %s))",
177,888✔
1383
                   alstate, AppLayerGetProtoName(f->alproto));
177,888✔
1384
    }
177,888✔
1385

1386
    p_tx_cnt = AppLayerParserGetTxCnt(f, f->alstate);
186,513✔
1387

1388
    /* invoke the recursive parser, but only on data. We may get empty msgs on EOF */
1389
    if (input_len > 0 || (flags & STREAM_EOF)) {
186,513✔
1390
        Setup(f, flags & (STREAM_TOSERVER | STREAM_TOCLIENT), input, input_len, flags,
186,441✔
1391
                &stream_slice);
186,441✔
1392
        HandleStreamFrames(f, stream_slice, input, input_len, flags);
186,441✔
1393

1394
#ifdef QA_SIMULATION
1395
        if (((stream_slice.flags & STREAM_TOSERVER) &&
1396
                    stream_slice.offset >= g_eps_applayer_error_offset_ts)) {
1397
            SCLogNotice("putting parser %s into an error state from toserver offset %" PRIu64,
1398
                    AppProtoToString(alproto), g_eps_applayer_error_offset_ts);
1399
            AppLayerIncParserErrorCounter(tv, f);
1400
            goto error;
1401
        }
1402
        if (((stream_slice.flags & STREAM_TOCLIENT) &&
1403
                    stream_slice.offset >= g_eps_applayer_error_offset_tc)) {
1404
            SCLogNotice("putting parser %s into an error state from toclient offset %" PRIu64,
1405
                    AppProtoToString(alproto), g_eps_applayer_error_offset_tc);
1406
            AppLayerIncParserErrorCounter(tv, f);
1407
            goto error;
1408
        }
1409
#endif
1410
        /* invoke the parser */
1411
        AppLayerResult res = p->Parser[direction](f, alstate, pstate, stream_slice,
186,441✔
1412
                alp_tctx->alproto_local_storage[alproto][f->protomap]);
186,441✔
1413
        if (res.status < 0) {
186,441✔
1414
            AppLayerIncParserErrorCounter(tv, f);
394✔
1415
            goto error;
394✔
1416
        } else if (res.status > 0) {
186,047✔
1417
            DEBUG_VALIDATE_BUG_ON(res.consumed > input_len);
2,535✔
1418
            DEBUG_VALIDATE_BUG_ON(res.needed < input_len - res.consumed);
2,535✔
1419
            DEBUG_VALIDATE_BUG_ON(res.needed == 0);
2,535✔
1420
            /* incomplete is only supported for TCP */
1421
            DEBUG_VALIDATE_BUG_ON(f->proto != IPPROTO_TCP);
2,535✔
1422

1423
            /* put protocol in error state on improper use of the
1424
             * return codes. */
1425
            if (res.consumed > input_len || res.needed + res.consumed < input_len) {
2,535✔
1426
                AppLayerIncInternalErrorCounter(tv, f);
×
1427
                goto error;
×
1428
            }
×
1429

1430
            if (f->proto == IPPROTO_TCP && f->protoctx != NULL) {
2,535✔
1431
                TcpSession *ssn = f->protoctx;
2,535✔
1432
                SCLogDebug("direction %d/%s", direction,
2,535✔
1433
                        (flags & STREAM_TOSERVER) ? "toserver" : "toclient");
2,535✔
1434
                if (direction == 0) {
2,535✔
1435
                    /* parser told us how much data it needs on top of what it
1436
                     * consumed. So we need tell stream engine how much we need
1437
                     * before the next call */
1438
                    ssn->client.data_required = res.needed;
256✔
1439
                    SCLogDebug("setting data_required %u", ssn->client.data_required);
256✔
1440
                } else {
2,279✔
1441
                    /* parser told us how much data it needs on top of what it
1442
                     * consumed. So we need tell stream engine how much we need
1443
                     * before the next call */
1444
                    ssn->server.data_required = res.needed;
2,279✔
1445
                    SCLogDebug("setting data_required %u", ssn->server.data_required);
2,279✔
1446
                }
2,279✔
1447
            }
2,535✔
1448
            consumed = res.consumed;
2,535✔
1449
        }
2,535✔
1450
    }
186,441✔
1451

1452
    /* set the packets to no inspection and reassembly if required */
1453
    if (pstate->flags & APP_LAYER_PARSER_NO_INSPECTION) {
186,119✔
1454
        AppLayerParserSetEOF(pstate);
435✔
1455

1456
        if (f->proto == IPPROTO_TCP) {
435✔
1457
            StreamTcpDisableAppLayer(f);
23✔
1458

1459
            /* Set the no reassembly flag for both the stream in this TcpSession */
1460
            if (pstate->flags & APP_LAYER_PARSER_NO_REASSEMBLY) {
23✔
1461
                /* Used only if it's TCP */
1462
                TcpSession *ssn = f->protoctx;
4✔
1463
                if (ssn != NULL) {
4✔
1464
                    StreamTcpSetSessionNoReassemblyFlag(ssn, 0);
4✔
1465
                    StreamTcpSetSessionNoReassemblyFlag(ssn, 1);
4✔
1466
                }
4✔
1467
            }
4✔
1468
            /* Set the bypass flag for both the stream in this TcpSession */
1469
            if (pstate->flags & APP_LAYER_PARSER_BYPASS_READY) {
23✔
1470
                /* Used only if it's TCP */
1471
                TcpSession *ssn = f->protoctx;
4✔
1472
                if (ssn != NULL) {
4✔
1473
                    StreamTcpSetSessionBypassFlag(ssn);
4✔
1474
                }
4✔
1475
            }
4✔
1476
        } else {
412✔
1477
            // for TCP, this is set after flushing
1478
            FlowSetNoPayloadInspectionFlag(f);
412✔
1479
        }
412✔
1480
    }
435✔
1481

1482
    /* In cases like HeartBleed for TLS we need to inspect AppLayer but not Payload */
1483
    if (!(f->flags & FLOW_NOPAYLOAD_INSPECTION) && pstate->flags & APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD) {
186,119✔
1484
        FlowSetNoPayloadInspectionFlag(f);
1,808✔
1485
        /* Set the no reassembly flag for both the stream in this TcpSession */
1486
        if (f->proto == IPPROTO_TCP) {
1,809✔
1487
            /* Used only if it's TCP */
1488
            TcpSession *ssn = f->protoctx;
1,809✔
1489
            if (ssn != NULL) {
1,809✔
1490
                StreamTcpSetDisableRawReassemblyFlag(ssn, 0);
1,809✔
1491
                StreamTcpSetDisableRawReassemblyFlag(ssn, 1);
1,809✔
1492
            }
1,809✔
1493
        }
1,809✔
1494
    }
1,808✔
1495

1496
    /* get the diff in tx cnt for stats keeping */
1497
    uint64_t cur_tx_cnt = AppLayerParserGetTxCnt(f, f->alstate);
186,119✔
1498
    if (cur_tx_cnt > p_tx_cnt && tv) {
186,119✔
1499
        AppLayerIncTxCounter(tv, f, cur_tx_cnt - p_tx_cnt);
39,453✔
1500
    }
39,453✔
1501

1502
 end:
188,174✔
1503
    /* update app progress */
1504
    if (consumed != input_len && f->proto == IPPROTO_TCP && f->protoctx != NULL) {
188,174✔
1505
        TcpSession *ssn = f->protoctx;
2,525✔
1506
        StreamTcpUpdateAppLayerProgress(ssn, direction, consumed);
2,525✔
1507
        SCReturnInt(1);
2,525✔
1508
    }
2,525✔
1509

1510
    SCReturnInt(0);
188,174✔
1511
 error:
873✔
1512
    /* Set the no app layer inspection flag for both
1513
     * the stream in this Flow */
1514
    if (f->proto == IPPROTO_TCP) {
873✔
1515
        StreamTcpDisableAppLayer(f);
566✔
1516
    }
566✔
1517
    AppLayerParserSetEOF(pstate);
873✔
1518
    SCReturnInt(-1);
873✔
1519
}
188,174✔
1520

1521
void AppLayerParserSetEOF(AppLayerParserState *pstate)
1522
{
1,308✔
1523
    SCEnter();
1,308✔
1524

1525
    if (pstate == NULL)
1,308✔
UNCOV
1526
        goto end;
×
1527

1528
    SCLogDebug("setting APP_LAYER_PARSER_EOF_TC and APP_LAYER_PARSER_EOF_TS");
1,308✔
1529
    SCAppLayerParserStateSetFlag(pstate, (APP_LAYER_PARSER_EOF_TS | APP_LAYER_PARSER_EOF_TC));
1,308✔
1530

1531
 end:
1,308✔
1532
    SCReturn;
1,308✔
1533
}
1,308✔
1534

1535
/* return true if there are app parser decoder events. These are
1536
 * only the ones that are set during protocol detection. */
1537
bool AppLayerParserHasDecoderEvents(AppLayerParserState *pstate)
1538
{
2,024,974✔
1539
    SCEnter();
2,024,974✔
1540

1541
    if (pstate == NULL)
2,024,974✔
1542
        return false;
463,525✔
1543

1544
    const AppLayerDecoderEvents *decoder_events = AppLayerParserGetDecoderEvents(pstate);
1,561,449✔
1545
    if (decoder_events && decoder_events->cnt)
1,561,449✔
1546
        return true;
×
1547

1548
    /* if we have reached here, we don't have events */
1549
    return false;
1,561,449✔
1550
}
1,561,449✔
1551

1552
/** \brief simple way to globally test if a alproto is registered
1553
 *         and fully enabled in the configuration.
1554
 */
1555
int AppLayerParserIsEnabled(AppProto alproto)
1556
{
40,791✔
1557
    for (int i = 0; i < FLOW_PROTO_APPLAYER_MAX; i++) {
51,233✔
1558
        if (alp_ctx.ctxs[alproto][i].StateGetProgress != NULL) {
51,230✔
1559
            return 1;
40,788✔
1560
        }
40,788✔
1561
    }
51,230✔
1562
    return 0;
3✔
1563
}
40,791✔
1564

1565
int AppLayerParserProtocolHasLogger(uint8_t ipproto, AppProto alproto)
1566
{
106,124✔
1567
    SCEnter();
106,124✔
1568
    int ipproto_map = FlowGetProtoMapping(ipproto);
106,124✔
1569
    int r = (!alp_ctx.ctxs[alproto][ipproto_map].logger) ? 0 : 1;
106,124✔
1570
    SCReturnInt(r);
106,124✔
1571
}
106,124✔
1572

1573
LoggerId AppLayerParserProtocolGetLoggerBits(uint8_t ipproto, AppProto alproto)
1574
{
441,236✔
1575
    SCEnter();
441,236✔
1576
    const int ipproto_map = FlowGetProtoMapping(ipproto);
441,236✔
1577
    LoggerId r = alp_ctx.ctxs[alproto][ipproto_map].logger_bits;
441,236✔
1578
    SCReturnUInt(r);
441,236✔
1579
}
441,236✔
1580

1581
void SCAppLayerParserTriggerRawStreamInspection(Flow *f, int direction)
1582
{
90,680✔
1583
    SCEnter();
90,680✔
1584

1585
    SCLogDebug("f %p tcp %p direction %d", f, f ? f->protoctx : NULL, direction);
90,680✔
1586
    if (f != NULL && f->protoctx != NULL)
90,680✔
1587
        StreamTcpReassembleTriggerRawInspection(f->protoctx, direction);
90,277✔
1588

1589
    SCReturn;
90,680✔
1590
}
90,680✔
1591

1592
void SCAppLayerParserSetStreamDepth(uint8_t ipproto, AppProto alproto, uint32_t stream_depth)
1593
{
3,707✔
1594
    SCEnter();
3,707✔
1595

1596
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].stream_depth = stream_depth;
3,707✔
1597
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].internal_flags |=
3,707✔
1598
            APP_LAYER_PARSER_INT_STREAM_DEPTH_SET;
3,707✔
1599

1600
    SCReturn;
3,707✔
1601
}
3,707✔
1602

1603
uint32_t AppLayerParserGetStreamDepth(const Flow *f)
1604
{
12,506✔
1605
    SCReturnInt(alp_ctx.ctxs[f->alproto][f->protomap].stream_depth);
12,506✔
1606
}
12,506✔
1607

1608
void AppLayerParserSetStreamDepthFlag(uint8_t ipproto, AppProto alproto, void *state, uint64_t tx_id, uint8_t flags)
1609
{
34✔
1610
    SCEnter();
34✔
1611
    void *tx = NULL;
34✔
1612
    if (state != NULL) {
34✔
1613
        if ((tx = AppLayerParserGetTx(ipproto, alproto, state, tx_id)) != NULL) {
34✔
1614
            if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].SetStreamDepthFlag != NULL) {
34✔
1615
                alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].SetStreamDepthFlag(tx, flags);
18✔
1616
            }
18✔
1617
        }
34✔
1618
    }
34✔
1619
    SCReturn;
34✔
1620
}
34✔
1621

1622
/**
1623
 *  \param id progress value id to get the name for
1624
 *  \param direction STREAM_TOSERVER/STREAM_TOCLIENT
1625
 */
1626
int AppLayerParserGetStateIdByName(
1627
        uint8_t ipproto, AppProto alproto, const char *name, const uint8_t direction)
1628
{
200✔
1629
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateIdByName != NULL) {
200✔
1630
        return alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateIdByName(
200✔
1631
                name, direction);
200✔
1632
    } else {
200✔
UNCOV
1633
        return -1;
×
UNCOV
1634
    }
×
1635
}
200✔
1636

1637
/**
1638
 *  \param id progress value id to get the name for
1639
 *  \param direction STREAM_TOSERVER/STREAM_TOCLIENT
1640
 */
1641
const char *AppLayerParserGetStateNameById(
1642
        uint8_t ipproto, AppProto alproto, const int id, const uint8_t direction)
1643
{
412,300✔
1644
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateNameById != NULL) {
412,300✔
1645
        return alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetStateNameById(id, direction);
75,292✔
1646
    } else {
337,008✔
1647
        return NULL;
337,008✔
1648
    }
337,008✔
1649
}
412,300✔
1650

1651
int AppLayerParserGetFrameIdByName(uint8_t ipproto, AppProto alproto, const char *name)
1652
{
108✔
1653
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetFrameIdByName != NULL) {
108✔
1654
        return alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetFrameIdByName(name);
108✔
1655
    } else {
108✔
UNCOV
1656
        return -1;
×
UNCOV
1657
    }
×
1658
}
108✔
1659

1660
const char *AppLayerParserGetFrameNameById(uint8_t ipproto, AppProto alproto, const uint8_t id)
1661
{
1,523✔
1662
    if (alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetFrameNameById != NULL) {
1,523✔
1663
        return alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].GetFrameNameById(id);
1,484✔
1664
    } else {
1,484✔
1665
        return NULL;
39✔
1666
    }
39✔
1667
}
1,523✔
1668

1669
/***** Cleanup *****/
1670

1671
void AppLayerParserStateProtoCleanup(
1672
        uint8_t protomap, AppProto alproto, void *alstate, AppLayerParserState *pstate)
1673
{
12,541✔
1674
    SCEnter();
12,541✔
1675

1676
    AppLayerParserProtoCtx *ctx = &alp_ctx.ctxs[alproto][protomap];
12,541✔
1677

1678
    if (ctx->StateFree != NULL && alstate != NULL)
12,541✔
1679
        ctx->StateFree(alstate);
8,630✔
1680

1681
    /* free the app layer parser api state */
1682
    if (pstate != NULL)
12,541✔
1683
        AppLayerParserStateFree(pstate);
8,630✔
1684

1685
    SCReturn;
12,541✔
1686
}
12,541✔
1687

1688
void AppLayerParserStateCleanup(const Flow *f, void *alstate, AppLayerParserState *pstate)
1689
{
11,033✔
1690
    AppLayerParserStateProtoCleanup(f->protomap, f->alproto, alstate, pstate);
11,033✔
1691
}
11,033✔
1692

1693
static void ValidateParserProtoDump(AppProto alproto, uint8_t ipproto)
1694
{
×
1695
    uint8_t map = FlowGetProtoMapping(ipproto);
×
1696
    const AppLayerParserProtoCtx *ctx = &alp_ctx.ctxs[alproto][map];
×
1697
    printf("ERROR: incomplete app-layer registration\n");
×
1698
    printf("AppLayer protocol %s ipproto %u\n", AppProtoToString(alproto), ipproto);
×
1699
    printf("- option flags %"PRIx32"\n", ctx->option_flags);
×
1700
    printf("- first_data_dir %"PRIx8"\n", ctx->first_data_dir);
×
1701
    printf("Mandatory:\n");
×
1702
    printf("- Parser[0] %p Parser[1] %p\n", ctx->Parser[0], ctx->Parser[1]);
×
1703
    printf("- StateAlloc %p StateFree %p\n", ctx->StateAlloc, ctx->StateFree);
×
1704
    printf("- StateGetTx %p StateGetTxCnt %p StateTransactionFree %p\n",
×
1705
            ctx->StateGetTx, ctx->StateGetTxCnt, ctx->StateTransactionFree);
×
1706
    printf("- GetTxData %p\n", ctx->GetTxData);
×
1707
    printf("- GetStateData %p\n", ctx->GetStateData);
×
1708
    printf("- StateGetProgress %p\n", ctx->StateGetProgress);
×
1709
    printf("Optional:\n");
×
1710
    printf("- LocalStorageAlloc %p LocalStorageFree %p\n", ctx->LocalStorageAlloc, ctx->LocalStorageFree);
×
1711
    printf("- StateGetEventInfo %p StateGetEventInfoById %p\n", ctx->StateGetEventInfo,
×
1712
            ctx->StateGetEventInfoById);
×
1713
}
×
1714

1715
#define BOTH_SET(a, b) ((a) != NULL && (b) != NULL)
159,232✔
1716
#define BOTH_SET_OR_BOTH_UNSET(a, b) (((a) == NULL && (b) == NULL) || ((a) != NULL && (b) != NULL))
79,616✔
1717
#define THREE_SET(a, b, c) ((a) != NULL && (b) != NULL && (c) != NULL)
79,616✔
1718

1719
static void ValidateParserProto(AppProto alproto, uint8_t ipproto)
1720
{
177,038✔
1721
    uint8_t map = FlowGetProtoMapping(ipproto);
177,038✔
1722
    const AppLayerParserProtoCtx *ctx = &alp_ctx.ctxs[alproto][map];
177,038✔
1723

1724
    if (ctx->Parser[0] == NULL && ctx->Parser[1] == NULL)
177,038✔
1725
        return;
97,422✔
1726

1727
    if (!(BOTH_SET(ctx->Parser[0], ctx->Parser[1]))) {
79,616✔
1728
        goto bad;
×
1729
    }
×
1730
    if (!(BOTH_SET(ctx->StateFree, ctx->StateAlloc))) {
79,616✔
1731
        goto bad;
×
1732
    }
×
1733
    if (!(THREE_SET(ctx->StateGetTx, ctx->StateGetTxCnt, ctx->StateTransactionFree))) {
79,616✔
1734
        goto bad;
×
1735
    }
×
1736
    if (ctx->StateGetProgress == NULL) {
79,616✔
1737
        goto bad;
×
1738
    }
×
1739
    /* local storage is optional, but needs both set if used */
1740
    if (!(BOTH_SET_OR_BOTH_UNSET(ctx->LocalStorageAlloc, ctx->LocalStorageFree))) {
79,616✔
1741
        goto bad;
×
1742
    }
×
1743
    if (ctx->GetTxData == NULL) {
79,616✔
1744
        goto bad;
×
1745
    }
×
1746
    if (ctx->GetStateData == NULL) {
79,616✔
1747
        goto bad;
×
1748
    }
×
1749
    return;
79,616✔
1750
bad:
79,616✔
1751
    ValidateParserProtoDump(alproto, ipproto);
×
1752
    exit(EXIT_FAILURE);
×
1753
}
79,616✔
1754
#undef BOTH_SET
1755
#undef BOTH_SET_OR_BOTH_UNSET
1756
#undef THREE_SET
1757

1758
static void ValidateParser(AppProto alproto)
1759
{
88,519✔
1760
    ValidateParserProto(alproto, IPPROTO_TCP);
88,519✔
1761
    ValidateParserProto(alproto, IPPROTO_UDP);
88,519✔
1762
}
88,519✔
1763

1764
static void ValidateParsers(void)
1765
{
2,213✔
1766
    AppProto p = 0;
2,213✔
1767
    for (; p < g_alproto_max; p++) {
90,732✔
1768
        ValidateParser(p);
88,519✔
1769
    }
88,519✔
1770
}
2,213✔
1771

1772
#define ARRAY_CAP_STEP 16
4,424✔
1773
static void (**PreRegisteredCallbacks)(void) = NULL;
1774
static size_t preregistered_callbacks_nb = 0;
1775
static size_t preregistered_callbacks_cap = 0;
1776

1777
int SCAppLayerParserReallocCtx(AppProto alproto)
1778
{
2,212✔
1779
    if (alp_ctx.ctxs_len <= alproto && alproto < g_alproto_max) {
2,212✔
1780
        /* Realloc alp_ctx.ctxs, so that dynamic alproto can be treated as real/normal ones.
1781
         * In case we need to turn off dynamic alproto. */
1782
        void *tmp = SCRealloc(alp_ctx.ctxs, sizeof(AppLayerParserProtoCtx[FLOW_PROTO_MAX]) *
2,212✔
1783
                                                    (alp_ctx.ctxs_len + ARRAY_CAP_STEP));
2,212✔
1784
        if (unlikely(tmp == NULL)) {
2,212✔
1785
            FatalError("Unable to realloc alp_ctx.ctxs.");
×
1786
        }
×
1787
        alp_ctx.ctxs = tmp;
2,212✔
1788
        memset(&alp_ctx.ctxs[alp_ctx.ctxs_len], 0,
2,212✔
1789
                sizeof(AppLayerParserProtoCtx[FLOW_PROTO_MAX]) * ARRAY_CAP_STEP);
2,212✔
1790
        alp_ctx.ctxs_len += ARRAY_CAP_STEP;
2,212✔
1791
    }
2,212✔
1792
    return 0;
2,212✔
1793
}
2,212✔
1794

1795
int AppLayerParserPreRegister(void (*Register)(void))
1796
{
×
1797
    if (preregistered_callbacks_nb == preregistered_callbacks_cap) {
×
1798
        void *tmp = SCRealloc(PreRegisteredCallbacks,
×
1799
                sizeof(void *) * (preregistered_callbacks_cap + ARRAY_CAP_STEP));
×
1800
        if (tmp == NULL) {
×
1801
            return 1;
×
1802
        }
×
1803
        preregistered_callbacks_cap += ARRAY_CAP_STEP;
×
1804
        PreRegisteredCallbacks = tmp;
×
1805
    }
×
1806
    PreRegisteredCallbacks[preregistered_callbacks_nb] = Register;
×
1807
    preregistered_callbacks_nb++;
×
1808
    return 0;
×
1809
}
×
1810

1811
void AppLayerParserRegisterProtocolParsers(void)
1812
{
2,213✔
1813
    SCEnter();
2,213✔
1814

1815
    AppLayerConfig();
2,213✔
1816

1817
    RegisterHTPParsers();
2,213✔
1818
    RegisterSSLParsers();
2,213✔
1819
    SCRegisterDcerpcParser();
2,213✔
1820
    SCRegisterDcerpcUdpParser();
2,213✔
1821
    RegisterSMBParsers();
2,213✔
1822
    RegisterFTPParsers();
2,213✔
1823
    RegisterSSHParsers();
2,213✔
1824
    RegisterSMTPParsers();
2,213✔
1825
    SCRegisterDnsUdpParser();
2,213✔
1826
    SCRegisterDnsTcpParser();
2,213✔
1827
    SCRegisterBittorrentDhtUdpParser();
2,213✔
1828
    RegisterModbusParsers();
2,213✔
1829
    SCEnipRegisterParsers();
2,213✔
1830
    RegisterDNP3Parsers();
2,213✔
1831
    RegisterNFSTCPParsers();
2,213✔
1832
    RegisterNFSUDPParsers();
2,213✔
1833
    SCRegisterNtpParser();
2,213✔
1834
    RegisterTFTPParsers();
2,213✔
1835
    RegisterIKEParsers();
2,213✔
1836
    SCRegisterKrb5Parser();
2,213✔
1837
    SCRegisterDhcpParser();
2,213✔
1838
    SCRegisterSnmpParser();
2,213✔
1839
    SCRegisterSipParser();
2,213✔
1840
    SCRegisterQuicParser();
2,213✔
1841
    SCRegisterWebSocketParser();
2,213✔
1842
    SCRegisterLdapTcpParser();
2,213✔
1843
    SCRegisterLdapUdpParser();
2,213✔
1844
    SCRegisterMdnsParser();
2,213✔
1845
    SCRegisterTemplateParser();
2,213✔
1846
    SCRfbRegisterParser();
2,213✔
1847
    SCMqttRegisterParser();
2,213✔
1848
    SCRegisterPgsqlParser();
2,213✔
1849
    SCRegisterPop3Parser();
2,213✔
1850
    SCRegisterRdpParser();
2,213✔
1851
    RegisterHTTP2Parsers();
2,213✔
1852
    SCRegisterTelnetParser();
2,213✔
1853
    RegisterIMAPParsers();
2,213✔
1854

1855
    for (size_t i = 0; i < preregistered_callbacks_nb; i++) {
2,213✔
1856
        PreRegisteredCallbacks[i]();
×
1857
    }
×
1858

1859
    ValidateParsers();
2,213✔
1860
}
2,213✔
1861

1862
/* coccinelle: SCAppLayerParserStateSetFlag():2,2:APP_LAYER_PARSER_ */
1863
void SCAppLayerParserStateSetFlag(AppLayerParserState *pstate, uint16_t flag)
1864
{
94,703✔
1865
    SCEnter();
94,703✔
1866
    pstate->flags |= flag;
94,703✔
1867
    SCReturn;
94,703✔
1868
}
94,703✔
1869

1870
/* coccinelle: SCAppLayerParserStateIssetFlag():2,2:APP_LAYER_PARSER_ */
1871
uint16_t SCAppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag)
1872
{
2,312,790✔
1873
    SCEnter();
2,312,790✔
1874
    SCReturnUInt(pstate->flags & flag);
2,312,790✔
1875
}
2,312,790✔
1876

1877
/***** Unittests *****/
1878

1879
#ifdef UNITTESTS
1880
#include "util-unittest-helper.h"
1881

1882
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto,
1883
                                  void (*RegisterUnittests)(void))
1884
{
1885
    SCEnter();
1886
    alp_ctx.ctxs[alproto][FlowGetProtoMapping(ipproto)].RegisterUnittests = RegisterUnittests;
1887
    SCReturn;
1888
}
1889

1890
void AppLayerParserRegisterUnittests(void)
1891
{
1892
    SCEnter();
1893

1894
    int ip;
1895
    AppProto alproto;
1896
    AppLayerParserProtoCtx *ctx;
1897

1898
    for (ip = 0; ip < FLOW_PROTO_DEFAULT; ip++) {
1899
        for (alproto = 0; alproto < g_alproto_max; alproto++) {
1900
            ctx = &alp_ctx.ctxs[alproto][ip];
1901
            if (ctx->RegisterUnittests == NULL)
1902
                continue;
1903
            ctx->RegisterUnittests();
1904
        }
1905
    }
1906

1907
    SCReturn;
1908
}
1909

1910
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc