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

OISF / suricata / 22586342403

02 Mar 2026 04:53PM UTC coverage: 73.68% (-0.007%) from 73.687%
22586342403

Pull #14930

github

web-flow
Merge 1d6dc53fc into 90823fa90
Pull Request #14930: Sslproxy/v16

38328 of 77468 branches covered (49.48%)

Branch coverage included in aggregate %.

336 of 431 new or added lines in 14 files covered. (77.96%)

66 existing lines in 15 files now uncovered.

265724 of 335196 relevant lines covered (79.27%)

5207223.13 hits per line

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

93.04
/src/app-layer.c
1
/* Copyright (C) 2007-2024 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
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23
 *
24
 * Generic App-layer functions
25
 */
26

27
#include "suricata-common.h"
28
#include "suricata.h"
29
#include "app-layer.h"
30
#include "app-layer-parser.h"
31
#include "app-layer-protos.h"
32
#include "app-layer-expectation.h"
33
#include "app-layer-ftp.h"
34
#include "app-layer-htp-range.h"
35
#include "app-layer-detect-proto.h"
36
#include "app-layer-frames.h"
37
#include "app-layer-events.h"
38
#include "stream-tcp-reassemble.h"
39
#include "stream-tcp-private.h"
40
#include "stream-tcp-inline.h"
41
#include "stream-tcp.h"
42
#include "flow.h"
43
#include "flow-util.h"
44
#include "flow-private.h"
45
#include "ippair.h"
46
#include "util-debug.h"
47
#include "util-print.h"
48
#include "util-profiling.h"
49
#include "util-validate.h"
50
#include "decode-events.h"
51
#include "app-layer-htp-mem.h"
52
#include "util-exception-policy.h"
53

54
extern bool g_stats_eps_per_app_proto_errors;
55
/**
56
 * \brief This is for the app layer in general and it contains per thread
57
 *        context relevant to both the alpd and alp.
58
 */
59
struct AppLayerThreadCtx_ {
60
    /* App layer protocol detection thread context, from AppLayerProtoDetectGetCtxThread(). */
61
    AppLayerProtoDetectThreadCtx *alpd_tctx;
62
    /* App layer parser thread context, from AppLayerParserThreadCtxAlloc(). */
63
    AppLayerParserThreadCtx *alp_tctx;
64

65
#ifdef PROFILING
66
    uint64_t ticks_start;
67
    uint64_t ticks_end;
68
    uint64_t ticks_spent;
69
    AppProto alproto;
70
    uint64_t proto_detect_ticks_start;
71
    uint64_t proto_detect_ticks_end;
72
    uint64_t proto_detect_ticks_spent;
73
#endif
74
};
75

76
#define FLOW_PROTO_CHANGE_MAX_DEPTH 4096
2,200✔
77

78
#define MAX_COUNTER_SIZE 64
79
typedef struct AppLayerCounterNames_ {
80
    char name[MAX_COUNTER_SIZE];
81
    char tx_name[MAX_COUNTER_SIZE];
82
    char gap_error[MAX_COUNTER_SIZE];
83
    char parser_error[MAX_COUNTER_SIZE];
84
    char internal_error[MAX_COUNTER_SIZE];
85
    char alloc_error[MAX_COUNTER_SIZE];
86
    char eps_name[EXCEPTION_POLICY_MAX][MAX_COUNTER_SIZE];
87
} AppLayerCounterNames;
88

89
typedef struct AppLayerCounters_ {
90
    StatsCounterId counter_id;
91
    StatsCounterId counter_tx_id;
92
    StatsCounterId gap_error_id;
93
    StatsCounterId parser_error_id;
94
    StatsCounterId internal_error_id;
95
    StatsCounterId alloc_error_id;
96
    ExceptionPolicyCounters eps_error;
97
} AppLayerCounters;
98

99
/* counter names. Only used at init. */
100
AppLayerCounterNames (*applayer_counter_names)[FLOW_PROTO_APPLAYER_MAX];
101
/* counter id's. Used that runtime. */
102
AppLayerCounters (*applayer_counters)[FLOW_PROTO_APPLAYER_MAX];
103
/* Exception policy global counters ids */
104
ExceptionPolicyCounters eps_error_summary;
105

106
/* Settings order as in the enum */
107
// clang-format off
108
ExceptionPolicyStatsSetts app_layer_error_eps_stats = {
109
    .valid_settings_ids = {
110
       /* EXCEPTION_POLICY_NOT_SET */      false,
111
       /* EXCEPTION_POLICY_AUTO */         false,
112
       /* EXCEPTION_POLICY_PASS_PACKET */  true,
113
       /* EXCEPTION_POLICY_PASS_FLOW */    true,
114
       /* EXCEPTION_POLICY_BYPASS_FLOW */  true,
115
       /* EXCEPTION_POLICY_DROP_PACKET */  false,
116
       /* EXCEPTION_POLICY_DROP_FLOW */    false,
117
       /* EXCEPTION_POLICY_REJECT */       true,
118
       /* EXCEPTION_POLICY_REJECT_BOTH */  true,
119
    },
120
    .valid_settings_ips = {
121
       /* EXCEPTION_POLICY_NOT_SET */      false,
122
       /* EXCEPTION_POLICY_AUTO */         false,
123
       /* EXCEPTION_POLICY_PASS_PACKET */  true,
124
       /* EXCEPTION_POLICY_PASS_FLOW */    true,
125
       /* EXCEPTION_POLICY_BYPASS_FLOW */  true,
126
       /* EXCEPTION_POLICY_DROP_PACKET */  true,
127
       /* EXCEPTION_POLICY_DROP_FLOW */    true,
128
       /* EXCEPTION_POLICY_REJECT */       true,
129
       /* EXCEPTION_POLICY_REJECT_BOTH */  true,
130
    },
131
};
132
// clang-format on
133

134
void AppLayerSetupCounters(void);
135
void AppLayerDeSetupCounters(void);
136

137
/***** L7 layer dispatchers *****/
138

139
static inline int ProtoDetectDone(const Flow *f, const TcpSession *ssn, uint8_t direction) {
31,192✔
140
    const TcpStream *stream = (direction & STREAM_TOSERVER) ? &ssn->client : &ssn->server;
31,192✔
141
    return ((stream->flags & STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_COMPLETED) ||
31,192✔
142
            (FLOW_IS_PM_DONE(f, direction) && FLOW_IS_PP_DONE(f, direction)));
31,192✔
143
}
31,192✔
144

145
/**
146
 * \note id can be 0 if protocol parser is disabled but detection
147
 *       is enabled.
148
 */
149
static void AppLayerIncFlowCounter(ThreadVars *tv, Flow *f)
150
{
56,051✔
151
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].counter_id;
56,051✔
152
    if (likely(tv && id.id > 0)) {
56,055✔
153
        StatsCounterIncr(&tv->stats, id);
56,043✔
154
    }
56,043✔
155
}
56,051✔
156

157
void AppLayerIncTxCounter(ThreadVars *tv, Flow *f, int64_t step)
158
{
249,597✔
159
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].counter_tx_id;
249,597✔
160
    if (likely(tv && id.id > 0)) {
249,598✔
161
        StatsCounterAddI64(&tv->stats, id, step);
249,583✔
162
    }
249,583✔
163
}
249,597✔
164

165
void AppLayerIncGapErrorCounter(ThreadVars *tv, Flow *f)
166
{
1,281✔
167
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].gap_error_id;
1,281✔
168
    if (likely(tv && id.id > 0)) {
1,281!
169
        StatsCounterIncr(&tv->stats, id);
1,281✔
170
    }
1,281✔
171
}
1,281✔
172

173
void AppLayerIncAllocErrorCounter(ThreadVars *tv, Flow *f)
174
{
×
175
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].alloc_error_id;
×
176
    if (likely(tv && id.id > 0)) {
×
177
        StatsCounterIncr(&tv->stats, id);
×
178
    }
×
179
}
×
180

181
void AppLayerIncParserErrorCounter(ThreadVars *tv, Flow *f)
182
{
5,428✔
183
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].parser_error_id;
5,428✔
184
    if (likely(tv && id.id > 0)) {
5,428!
185
        StatsCounterIncr(&tv->stats, id);
4,794✔
186
    }
4,794✔
187
}
5,428✔
188

189
void AppLayerIncInternalErrorCounter(ThreadVars *tv, Flow *f)
190
{
×
191
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].internal_error_id;
×
192
    if (likely(tv && id.id > 0)) {
×
193
        StatsCounterIncr(&tv->stats, id);
×
194
    }
×
195
}
×
196

197
static void AppLayerIncrErrorExcPolicyCounter(ThreadVars *tv, Flow *f, enum ExceptionPolicy policy)
198
{
6,075✔
199
#ifdef UNITTESTS
200
    if (tv == NULL) {
201
        return;
202
    }
203
#endif
204
    StatsCounterId id = applayer_counters[f->alproto][f->protomap].eps_error.eps_id[policy];
6,075✔
205
    /* for the summary values */
206
    StatsCounterId g_id = eps_error_summary.eps_id[policy];
6,075✔
207

208
    if (likely(id.id > 0)) {
6,075!
209
        StatsCounterIncr(&tv->stats, id);
×
210
    }
×
211
    if (likely(g_id.id > 0)) {
6,075✔
212
        StatsCounterIncr(&tv->stats, g_id);
2✔
213
    }
2✔
214
}
6,075✔
215

216
/* in IDS mode protocol detection is done in reverse order:
217
 * when TCP data is ack'd. We want to flag the correct packet,
218
 * so in this case we set a flag in the flow so that the first
219
 * packet in the correct direction can be tagged.
220
 *
221
 * For IPS we update packet and flow. */
222
static inline void FlagPacketFlow(Packet *p, Flow *f, uint8_t flags)
223
{
124,588✔
224
    if (p->proto != IPPROTO_TCP || EngineModeIsIPS()) {
124,588✔
225
        if (flags & STREAM_TOSERVER) {
62,193✔
226
            if (p->flowflags & FLOW_PKT_TOSERVER) {
31,114✔
227
                p->flags |= PKT_PROTO_DETECT_TS_DONE;
22,985✔
228
                f->flags |= FLOW_PROTO_DETECT_TS_DONE;
22,985✔
229
            } else {
22,990✔
230
                f->flags |= FLOW_PROTO_DETECT_TS_DONE;
8,129✔
231
            }
8,129✔
232
        } else {
31,115✔
233
            if (p->flowflags & FLOW_PKT_TOCLIENT) {
31,079✔
234
                p->flags |= PKT_PROTO_DETECT_TC_DONE;
8,258✔
235
                f->flags |= FLOW_PROTO_DETECT_TC_DONE;
8,258✔
236
            } else {
22,834✔
237
                f->flags |= FLOW_PROTO_DETECT_TC_DONE;
22,821✔
238
            }
22,821✔
239
        }
31,079✔
240
    } else {
67,169✔
241
        if (flags & STREAM_TOSERVER) {
62,395✔
242
            f->flags |= FLOW_PROTO_DETECT_TS_DONE;
33,925✔
243
        } else {
33,927✔
244
            f->flags |= FLOW_PROTO_DETECT_TC_DONE;
28,470✔
245
        }
28,470✔
246
    }
62,395✔
247
}
124,588✔
248

249
static void DisableAppLayer(ThreadVars *tv, Flow *f, Packet *p)
250
{
1,971✔
251
    SCLogDebug("disable app layer for flow %p alproto %u ts %u tc %u",
1,971!
252
            f, f->alproto, f->alproto_ts, f->alproto_tc);
1,971✔
253
    FlowCleanupAppLayer(f);
1,971✔
254
    StreamTcpDisableAppLayer(f);
1,971✔
255
    TcpSession *ssn = f->protoctx;
1,971✔
256
    ssn->data_first_seen_dir = APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER;
1,971✔
257
    f->alproto = ALPROTO_FAILED;
1,971✔
258
    AppLayerIncFlowCounter(tv, f);
1,971✔
259

260
    if (f->alproto_tc != ALPROTO_FAILED) {
1,971✔
261
        if (f->alproto_tc == ALPROTO_UNKNOWN) {
1,970✔
262
            f->alproto_tc = ALPROTO_FAILED;
1,733✔
263
        }
1,733✔
264
        FlagPacketFlow(p, f, STREAM_TOCLIENT);
1,970✔
265
    }
1,970✔
266
    if (f->alproto_ts != ALPROTO_FAILED) {
1,971✔
267
        if (f->alproto_ts == ALPROTO_UNKNOWN) {
1,970✔
268
            f->alproto_ts = ALPROTO_FAILED;
1,952✔
269
        }
1,952✔
270
        FlagPacketFlow(p, f, STREAM_TOSERVER);
1,970✔
271
    }
1,970✔
272
    SCLogDebug("disabled app layer for flow %p alproto %u ts %u tc %u",
1,971!
273
            f, f->alproto, f->alproto_ts, f->alproto_tc);
1,971✔
274
}
1,971✔
275

276
/* See if we're going to have to give up:
277
 *
278
 * If we're getting a lot of data in one direction and the
279
 * proto for this direction is unknown, proto detect will
280
 * hold up segments in the segment list in the stream.
281
 * They are held so that if we detect the protocol on the
282
 * opposing stream, we can still parse this side of the stream
283
 * as well. However, some sessions are very unbalanced. FTP
284
 * data channels, large PUT/POST request and many others, can
285
 * lead to cases where we would have to store many megabytes
286
 * worth of segments before we see the opposing stream. This
287
 * leads to risks of resource starvation.
288
 *
289
 * Here a cutoff point is enforced. If we've stored 100k in
290
 * one direction and we've seen no data in the other direction,
291
 * we give up.
292
 *
293
 * Giving up means we disable applayer an set an applayer event
294
 */
295
static void TCPProtoDetectCheckBailConditions(ThreadVars *tv,
296
        Flow *f, TcpSession *ssn, Packet *p)
297
{
27,095✔
298
    if (ssn->state < TCP_ESTABLISHED) {
27,095✔
299
        SCLogDebug("skip as long as TCP is not ESTABLISHED (TCP fast open)");
532!
300
        return;
532✔
301
    }
532✔
302

303
    const uint32_t size_ts = StreamDataAvailableForProtoDetect(&ssn->client);
26,563✔
304
    const uint32_t size_tc = StreamDataAvailableForProtoDetect(&ssn->server);
26,563✔
305
    SCLogDebug("size_ts %" PRIu32 ", size_tc %" PRIu32, size_ts, size_tc);
26,563!
306

307
    /* at least 100000 whatever the conditions
308
     * and can be more if window is bigger and if configuration allows it */
309
    const uint32_t size_tc_limit =
26,563✔
310
            MAX(100000, MIN(ssn->client.window, stream_config.reassembly_depth));
26,563✔
311
    const uint32_t size_ts_limit =
26,563✔
312
            MAX(100000, MIN(ssn->server.window, stream_config.reassembly_depth));
26,563✔
313

314
    if (ProtoDetectDone(f, ssn, STREAM_TOSERVER) &&
26,563✔
315
        ProtoDetectDone(f, ssn, STREAM_TOCLIENT))
26,563✔
316
    {
185✔
317
        goto failure;
185✔
318

319
        /* we bail out whatever the pp and pm states if
320
         * we received too much data */
321
    } else if (size_tc > 2 * size_tc_limit || size_ts > 2 * size_ts_limit) {
26,379!
322
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
×
323
        goto failure;
×
324

325
    } else if (FLOW_IS_PM_DONE(f, STREAM_TOSERVER) && FLOW_IS_PP_DONE(f, STREAM_TOSERVER) &&
26,378✔
326
               size_ts > size_ts_limit && size_tc == 0) {
26,378!
327
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
2✔
328
        goto failure;
2✔
329

330
    } else if (FLOW_IS_PM_DONE(f, STREAM_TOCLIENT) && FLOW_IS_PP_DONE(f, STREAM_TOCLIENT) &&
26,376✔
331
               size_tc > size_tc_limit && size_ts == 0) {
26,376!
UNCOV
332
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
×
UNCOV
333
        goto failure;
×
334

335
    /* little data in ts direction, pp done, pm not done (max
336
     * depth not reached), ts direction done, lots of data in
337
     * tc direction. */
338
    } else if (size_tc > size_tc_limit && FLOW_IS_PP_DONE(f, STREAM_TOSERVER) &&
26,376!
339
               !(FLOW_IS_PM_DONE(f, STREAM_TOSERVER)) && FLOW_IS_PM_DONE(f, STREAM_TOCLIENT) &&
26,376!
340
               FLOW_IS_PP_DONE(f, STREAM_TOCLIENT)) {
26,376!
341
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
×
342
        goto failure;
×
343

344
    /* little data in tc direction, pp done, pm not done (max
345
     * depth not reached), tc direction done, lots of data in
346
     * ts direction. */
347
    } else if (size_ts > size_ts_limit && FLOW_IS_PP_DONE(f, STREAM_TOCLIENT) &&
26,376!
348
               !(FLOW_IS_PM_DONE(f, STREAM_TOCLIENT)) && FLOW_IS_PM_DONE(f, STREAM_TOSERVER) &&
26,376!
349
               FLOW_IS_PP_DONE(f, STREAM_TOSERVER)) {
26,376!
350
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
×
351
        goto failure;
×
352
    }
×
353
    return;
26,376✔
354

355
failure:
26,376✔
356
    DisableAppLayer(tv, f, p);
187✔
357
}
187✔
358

359
static int TCPProtoDetectTriggerOpposingSide(ThreadVars *tv, TcpReassemblyThreadCtx *ra_ctx,
360
        Packet *p, TcpSession *ssn, const TcpStream *stream)
361
{
1,215✔
362
    TcpStream *opposing_stream = NULL;
1,215✔
363
    if (stream == &ssn->client) {
1,215✔
364
        opposing_stream = &ssn->server;
436✔
365
    } else {
784✔
366
        opposing_stream = &ssn->client;
779✔
367
    }
779✔
368

369
    /* if the opposing side is not going to work, then
370
     * we just have to give up. */
371
    if (opposing_stream->flags & STREAMTCP_STREAM_FLAG_NOREASSEMBLY) {
1,215!
372
        SCLogDebug("opposing dir has STREAMTCP_STREAM_FLAG_NOREASSEMBLY set");
11!
373
        return -1;
11✔
374
    }
11✔
375

376
    enum StreamUpdateDir dir = StreamTcpInlineMode() ?
1,204✔
377
                                                UPDATE_DIR_OPPOSING :
12✔
378
                                                UPDATE_DIR_PACKET;
1,204✔
379
    int ret = StreamTcpReassembleAppLayer(tv, ra_ctx, ssn,
1,204✔
380
            opposing_stream, p, dir);
1,204✔
381
    return ret;
1,204✔
382
}
1,215✔
383

384
extern enum ExceptionPolicy g_applayerparser_error_policy;
385

386
/** \todo data const
387
 *  \retval int -1 error
388
 *  \retval int 0 ok
389
 */
390
static int TCPProtoDetect(ThreadVars *tv, TcpReassemblyThreadCtx *ra_ctx,
391
        AppLayerThreadCtx *app_tctx, Packet *p, Flow *f, TcpSession *ssn, TcpStream **stream,
392
        const uint8_t *data, uint32_t data_len, uint8_t flags, enum StreamUpdateDir app_update_dir)
393
{
92,015✔
394
    AppProto *alproto;
92,015✔
395
    AppProto *alproto_otherdir;
92,015✔
396
    uint8_t direction = (flags & STREAM_TOSERVER) ? 0 : 1;
92,015✔
397

398
    if (flags & STREAM_TOSERVER) {
92,015✔
399
        alproto = &f->alproto_ts;
47,696✔
400
        alproto_otherdir = &f->alproto_tc;
47,696✔
401
    } else {
47,696✔
402
        alproto = &f->alproto_tc;
44,319✔
403
        alproto_otherdir = &f->alproto_ts;
44,319✔
404
    }
44,319✔
405

406
    SCLogDebug("Stream initializer (len %" PRIu32 ")", data_len);
92,015!
407
#ifdef PRINT
408
    if (data_len > 0) {
409
        printf("=> Init Stream Data (app layer) -- start %s%s\n",
410
                flags & STREAM_TOCLIENT ? "toclient" : "",
411
                flags & STREAM_TOSERVER ? "toserver" : "");
412
        PrintRawDataFp(stdout, data, data_len);
413
        printf("=> Init Stream Data -- end\n");
414
    }
415
#endif
416

417
    bool reverse_flow = false;
92,015✔
418
    DEBUG_VALIDATE_BUG_ON(data == NULL && data_len > 0);
92,015✔
419
    PACKET_PROFILING_APP_PD_START(app_tctx);
92,015✔
420
    *alproto = AppLayerProtoDetectGetProto(app_tctx->alpd_tctx,
92,015✔
421
            f, data, data_len,
92,015✔
422
            IPPROTO_TCP, flags, &reverse_flow);
92,015✔
423
    PACKET_PROFILING_APP_PD_END(app_tctx);
92,015✔
424
    SCLogDebug("alproto %u rev %s", *alproto, reverse_flow ? "true" : "false");
92,015!
425

426
    if (*alproto != ALPROTO_UNKNOWN) {
92,015✔
427
        if (*alproto_otherdir != ALPROTO_UNKNOWN && *alproto_otherdir != *alproto) {
57,897✔
428
            SCAppLayerDecoderEventsSetEventRaw(
51✔
429
                    &p->app_layer_events, APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS);
51✔
430

431
            if (ssn->data_first_seen_dir == APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER) {
51!
432
                /* if we already invoked the parser, we go with that proto */
433
                f->alproto = *alproto_otherdir;
51✔
434
            } else {
51✔
435
                /* no data sent to parser yet, we can still choose
436
                 * we're trusting the server more. */
437
                if (flags & STREAM_TOCLIENT)
×
438
                    f->alproto = *alproto;
×
439
                else
×
440
                    f->alproto = *alproto_otherdir;
×
441
            }
×
442
        } else {
57,846✔
443
            f->alproto = *alproto;
57,846✔
444
        }
57,846✔
445

446
        StreamTcpSetStreamFlagAppProtoDetectionCompleted(*stream);
57,897✔
447
        TcpSessionSetReassemblyDepth(ssn,
57,897✔
448
                AppLayerParserGetStreamDepth(f));
57,897✔
449
        FlagPacketFlow(p, f, flags);
57,897✔
450

451
        /* if protocol detection indicated that we need to reverse
452
         * the direction of the flow, do it now. We flip the flow,
453
         * packet and the direction flags */
454
        if (reverse_flow &&
57,897✔
455
                ((ssn->flags & (STREAMTCP_FLAG_MIDSTREAM | STREAMTCP_FLAG_MIDSTREAM_SYNACK)) ==
57,897✔
456
                        STREAMTCP_FLAG_MIDSTREAM)) {
2,060✔
457
            /* but only if we didn't already detect it on the other side. */
458
            if (*alproto_otherdir == ALPROTO_UNKNOWN) {
1,530!
459
                SCLogDebug("reversing flow after proto detect told us so");
1,498!
460
                PacketSwap(p);
1,498✔
461
                FlowSwap(f);
1,498✔
462
                // Will reset signature groups in DetectRunSetup
463
                f->de_ctx_version = UINT32_MAX;
1,498✔
464
                SWAP_FLAGS(flags, STREAM_TOSERVER, STREAM_TOCLIENT);
1,498✔
465
                if (*stream == &ssn->client) {
1,498!
466
                    *stream = &ssn->server;
1,376✔
467
                } else {
1,376✔
468
                    *stream = &ssn->client;
122✔
469
                }
122✔
470
                direction = 1 - direction;
1,498✔
471
            } else {
1,498✔
472
                // TODO event, error?
473
            }
32✔
474
        }
1,530✔
475

476
        /* account flow if we have both sides */
477
        if (*alproto_otherdir != ALPROTO_UNKNOWN) {
57,897✔
478
            AppLayerIncFlowCounter(tv, f);
25,985✔
479
        }
25,985✔
480

481
        /* if we have seen data from the other direction first, send
482
         * data for that direction first to the parser.  This shouldn't
483
         * be an issue, since each stream processing happens
484
         * independently of the other stream direction.  At this point of
485
         * call, you need to know that this function's already being
486
         * called by the very same StreamReassembly() function that we
487
         * will now call shortly for the opposing direction. */
488
        if ((ssn->data_first_seen_dir & (STREAM_TOSERVER | STREAM_TOCLIENT)) &&
57,897✔
489
                !(flags & ssn->data_first_seen_dir))
57,897✔
490
        {
1,215✔
491
            SCLogDebug("protocol %s needs first data in other direction",
1,215!
492
                    AppProtoToString(*alproto));
1,215✔
493

494
            if (TCPProtoDetectTriggerOpposingSide(tv, ra_ctx,
1,215!
495
                        p, ssn, *stream) != 0)
1,215✔
496
            {
11✔
497
                goto detect_error;
11✔
498
            }
11✔
499
            if (FlowChangeProto(f)) {
1,204!
500
                /* We have the first data which requested a protocol change from P1 to P2
501
                 * even if it was not recognized at first as being P1
502
                 * As the second data was recognized as P1, the protocol did not change !
503
                 */
504
                FlowUnsetChangeProtoFlag(f);
×
505
                SCAppLayerDecoderEventsSetEventRaw(
×
506
                        &p->app_layer_events, APPLAYER_UNEXPECTED_PROTOCOL);
×
507
            }
×
508
        }
1,204✔
509

510
        /* if the parser operates such that it needs to see data from
511
         * a particular direction first, we check if we have seen
512
         * data from that direction first for the flow.  IF it is not
513
         * the same, we set an event and exit.
514
         *
515
         * \todo We need to figure out a more robust solution for this,
516
         *       as this can lead to easy evasion tactics, where the
517
         *       attacker can first send some dummy data in the wrong
518
         *       direction first to mislead our proto detection process.
519
         *       While doing this we need to update the parsers as well,
520
         *       since the parsers must be robust to see such wrong
521
         *       direction data.
522
         *       Either ways the moment we see the
523
         *       APPLAYER_WRONG_DIRECTION_FIRST_DATA event set for the
524
         *       flow, it shows something's fishy.
525
         */
526
        if (ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER) {
57,886✔
527
            uint8_t first_data_dir;
30,994✔
528
            first_data_dir = AppLayerParserGetFirstDataDir(f->proto, f->alproto);
30,994✔
529

530
            if (first_data_dir && !(first_data_dir & ssn->data_first_seen_dir)) {
30,994✔
531
                SCAppLayerDecoderEventsSetEventRaw(
215✔
532
                        &p->app_layer_events, APPLAYER_WRONG_DIRECTION_FIRST_DATA);
215✔
533
                goto detect_error;
215✔
534
            }
215✔
535
            /* This can happen if the current direction is not the
536
             * right direction, and the data from the other(also
537
             * the right direction) direction is available to be sent
538
             * to the app layer, but it is not ack'ed yet and hence
539
             * the forced call to STreamTcpAppLayerReassemble still
540
             * hasn't managed to send data from the other direction
541
             * to the app layer. */
542
            if (first_data_dir && !(first_data_dir & flags)) {
30,779!
543
                FlowCleanupAppLayer(f);
13✔
544
                StreamTcpResetStreamFlagAppProtoDetectionCompleted(*stream);
13✔
545
                FLOW_RESET_PP_DONE(f, flags);
13!
546
                FLOW_RESET_PM_DONE(f, flags);
13!
547
                FLOW_RESET_PE_DONE(f, flags);
13!
548
                SCReturnInt(-1);
13✔
549
            }
13✔
550
        }
30,779✔
551

552
        /* Set a value that is neither STREAM_TOSERVER, nor STREAM_TOCLIENT */
553
        ssn->data_first_seen_dir = APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER;
57,658✔
554

555
        /* finally, invoke the parser */
556
        PACKET_PROFILING_APP_START(app_tctx, f->alproto);
57,658✔
557
        int r = AppLayerParserParse(tv, app_tctx->alp_tctx, f, f->alproto,
57,658✔
558
                flags, data, data_len);
57,658✔
559
        PACKET_PROFILING_APP_END(app_tctx);
57,658✔
560
        p->app_update_direction = (uint8_t)app_update_dir;
57,658✔
561
        if (r <= 0) {
57,658✔
562
            StreamTcpUpdateAppLayerProgress(ssn, direction, data_len);
54,660✔
563
        }
54,660✔
564
        if (r == 0) {
57,658✔
565
            if (*alproto_otherdir == ALPROTO_UNKNOWN) {
53,377✔
566
                TcpStream *opposing_stream;
29,357✔
567
                if (*stream == &ssn->client) {
29,357✔
568
                    opposing_stream = &ssn->server;
26,568✔
569
                } else {
26,568✔
570
                    opposing_stream = &ssn->client;
2,789✔
571
                }
2,789✔
572
                if (StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(opposing_stream)) {
29,357✔
573
                    // can happen in detection-only
574
                    AppLayerIncFlowCounter(tv, f);
82✔
575
                }
82✔
576
            }
29,357✔
577
        }
53,377✔
578
        if (r < 0) {
57,658✔
579
            goto parser_error;
1,281✔
580
        }
1,281✔
581
        SCReturnInt(r);
57,658✔
582
    } else {
57,659✔
583
        /* if the ssn is midstream, we may end up with a case where the
584
         * start of an HTTP request is missing. We won't detect HTTP based
585
         * on the request. However, the reply is fine, so we detect
586
         * HTTP anyway. This leads to passing the incomplete request to
587
         * the htp parser.
588
         *
589
         * This has been observed, where the http parser then saw many
590
         * bogus requests in the incomplete data.
591
         *
592
         * To counter this case, a midstream session MUST find it's
593
         * protocol in the toserver direction. If not, we assume the
594
         * start of the request/toserver is incomplete and no reliable
595
         * detection and parsing is possible. So we give up.
596
         */
597
        if ((ssn->flags & STREAMTCP_FLAG_MIDSTREAM) &&
34,118✔
598
                !(ssn->flags & STREAMTCP_FLAG_MIDSTREAM_SYNACK))
34,118✔
599
        {
15,166✔
600
            if (FLOW_IS_PM_DONE(f, STREAM_TOSERVER) && FLOW_IS_PP_DONE(f, STREAM_TOSERVER)) {
15,166✔
601
                SCLogDebug("midstream end pd %p", ssn);
1,012!
602
                /* midstream and toserver detection failed: give up */
603
                DisableAppLayer(tv, f, p);
1,012✔
604
                SCReturnInt(0);
1,012✔
605
            }
1,012✔
606
        }
15,166✔
607

608
        if (*alproto_otherdir != ALPROTO_UNKNOWN) {
33,106✔
609
            uint8_t first_data_dir;
4,911✔
610
            first_data_dir = AppLayerParserGetFirstDataDir(f->proto, *alproto_otherdir);
4,911✔
611

612
            /* this would handle this test case -
613
             * http parser which says it wants to see toserver data first only.
614
             * tcp handshake
615
             * toclient data first received. - RUBBISH DATA which
616
             *                                 we don't detect as http
617
             * toserver data next sent - we detect this as http.
618
             * at this stage we see that toclient is the first data seen
619
             * for this session and we try and redetect the app protocol,
620
             * but we are unable to detect the app protocol like before.
621
             * But since we have managed to detect the protocol for the
622
             * other direction as http, we try to use that.  At this
623
             * stage we check if the direction of this stream matches
624
             * to that acceptable by the app parser.  If it is not the
625
             * acceptable direction we error out.
626
             */
627
            if ((ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER) &&
4,911✔
628
                    (first_data_dir) && !(first_data_dir & flags))
4,911!
629
            {
3✔
630
                goto detect_error;
3✔
631
            }
3✔
632

633
            /* if protocol detection is marked done for our direction we
634
             * pass our data on. We're only succeeded in finding one
635
             * direction: the opposing stream
636
             *
637
             * If PD was not yet complete, we don't do anything.
638
             */
639
            if (FLOW_IS_PM_DONE(f, flags) && FLOW_IS_PP_DONE(f, flags)) {
4,908✔
640
                if (data_len > 0)
1,011!
641
                    ssn->data_first_seen_dir = APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER;
1,011✔
642

643
                if (*alproto_otherdir != ALPROTO_FAILED) {
1,011!
644
                    PACKET_PROFILING_APP_START(app_tctx, f->alproto);
1,011✔
645
                    int r = AppLayerParserParse(tv, app_tctx->alp_tctx, f,
1,011✔
646
                            f->alproto, flags,
1,011✔
647
                            data, data_len);
1,011✔
648
                    PACKET_PROFILING_APP_END(app_tctx);
1,011✔
649
                    p->app_update_direction = (uint8_t)app_update_dir;
1,011✔
650
                    if (r != 1) {
1,011✔
651
                        StreamTcpUpdateAppLayerProgress(ssn, direction, data_len);
768✔
652
                    }
768✔
653

654
                    SCAppLayerDecoderEventsSetEventRaw(
1,011✔
655
                            &p->app_layer_events, APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION);
1,011✔
656
                    TcpSessionSetReassemblyDepth(ssn,
1,011✔
657
                            AppLayerParserGetStreamDepth(f));
1,011✔
658
                    AppLayerIncFlowCounter(tv, f);
1,011✔
659

660
                    *alproto = *alproto_otherdir;
1,011✔
661
                    SCLogDebug("packet %" PRIu64 ": pd done(us %u them %u), parser called (r==%d), "
1,011!
662
                               "APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION set",
1,011✔
663
                            PcapPacketCntGet(p), *alproto, *alproto_otherdir, r);
1,011✔
664
                    if (r < 0) {
1,011!
665
                        goto parser_error;
102✔
666
                    }
102✔
667
                }
1,011✔
668
                *alproto = ALPROTO_FAILED;
909✔
669
                StreamTcpSetStreamFlagAppProtoDetectionCompleted(*stream);
909✔
670
                FlagPacketFlow(p, f, flags);
909✔
671

672
            } else if (flags & STREAM_EOF) {
3,899✔
673
                *alproto = f->alproto;
1,731✔
674
                StreamTcpSetStreamFlagAppProtoDetectionCompleted(*stream);
1,731✔
675
                AppLayerIncFlowCounter(tv, f);
1,731✔
676
            }
1,731✔
677
        } else {
28,195✔
678
            /* both sides unknown, let's see if we need to give up */
679
            if (FlowChangeProto(f)) {
28,195✔
680
                /* TCPProtoDetectCheckBailConditions does not work well because
681
                 * size_tc from STREAM_RIGHT_EDGE is not reset to zero
682
                 * so, we set a lower limit to the data we inspect
683
                 * We could instead have set ssn->server.sb.stream_offset = 0;
684
                 */
685
                if (data_len >= FLOW_PROTO_CHANGE_MAX_DEPTH || (flags & STREAM_EOF)) {
1,100!
686
                    DisableAppLayer(tv, f, p);
543✔
687
                }
543✔
688
            } else {
27,095✔
689
                TCPProtoDetectCheckBailConditions(tv, f, ssn, p);
27,095✔
690
            }
27,095✔
691
        }
28,195✔
692
    }
33,106✔
693
    SCReturnInt(0);
92,015✔
694
parser_error:
1,383✔
695
    ExceptionPolicyApply(p, g_applayerparser_error_policy, PKT_DROP_REASON_APPLAYER_ERROR);
1,383✔
696
    AppLayerIncrErrorExcPolicyCounter(tv, f, g_applayerparser_error_policy);
1,383✔
697
    SCReturnInt(-1);
1,383✔
698
detect_error:
229✔
699
    DisableAppLayer(tv, f, p);
229✔
700
    SCReturnInt(-2);
229✔
701
}
92,015✔
702

703
/** \brief handle TCP data for the app-layer.
704
 *
705
 *  First run protocol detection and then when the protocol is known invoke
706
 *  the app layer parser.
707
 *
708
 *  \param stream ptr-to-ptr to stream object. Might change if flow dir is
709
 *                reversed.
710
 */
711
int AppLayerHandleTCPData(ThreadVars *tv, TcpReassemblyThreadCtx *ra_ctx, Packet *p, Flow *f,
712
        TcpSession *ssn, TcpStream **stream, uint8_t *data, uint32_t data_len, uint8_t flags,
713
        enum StreamUpdateDir app_update_dir)
714
{
741,467✔
715
    SCEnter();
741,467✔
716

717
    DEBUG_ASSERT_FLOW_LOCKED(f);
741,467✔
718
    DEBUG_VALIDATE_BUG_ON(data_len > (uint32_t)INT_MAX);
741,467✔
719

720
    AppLayerThreadCtx *app_tctx = ra_ctx->app_tctx;
741,467✔
721
    AppProto alproto;
741,467✔
722
    int r = 0;
741,467✔
723

724
    SCLogDebug("data_len %u flags %02X", data_len, flags);
741,467!
725
    if (ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED) {
741,467!
726
        SCLogDebug("STREAMTCP_FLAG_APP_LAYER_DISABLED is set");
7!
727
        goto end;
7✔
728
    }
7✔
729

730
    const uint8_t direction = (flags & STREAM_TOSERVER) ? 0 : 1;
741,460✔
731

732
    if (flags & STREAM_TOSERVER) {
741,460✔
733
        alproto = f->alproto_ts;
385,013✔
734
    } else {
402,986✔
735
        alproto = f->alproto_tc;
356,447✔
736
    }
356,447✔
737

738
    /* If a gap notification, relay the notification on to the
739
     * app-layer if known. */
740
    if (flags & STREAM_GAP) {
741,460✔
741
        SCLogDebug("GAP of size %u", data_len);
13,868!
742
        if (alproto == ALPROTO_UNKNOWN) {
13,868✔
743
            StreamTcpSetStreamFlagAppProtoDetectionCompleted(*stream);
6,069✔
744
            SCLogDebug("ALPROTO_UNKNOWN flow %p, due to GAP in stream start", f);
6,069!
745
            /* if the other side didn't already find the proto, we're done */
746
            if (f->alproto == ALPROTO_UNKNOWN) {
6,069✔
747
                goto failure;
3,732✔
748
            }
3,732✔
749
            AppLayerIncFlowCounter(tv, f);
2,337✔
750
        }
2,337✔
751
        if (FlowChangeProto(f)) {
10,136!
752
            FlowUnsetChangeProtoFlag(f);
256✔
753
            SCLogDebug("Cannot handle gap while changing protocol");
256!
754
            goto failure;
256✔
755
        }
256✔
756
        PACKET_PROFILING_APP_START(app_tctx, f->alproto);
9,880✔
757
        r = AppLayerParserParse(tv, app_tctx->alp_tctx, f, f->alproto,
9,880✔
758
                flags, data, data_len);
9,880✔
759
        PACKET_PROFILING_APP_END(app_tctx);
9,880✔
760
        p->app_update_direction = (uint8_t)app_update_dir;
9,880✔
761
        /* ignore parser result for gap */
762
        StreamTcpUpdateAppLayerProgress(ssn, direction, data_len);
9,880✔
763
        if (r < 0) {
9,880✔
764
            ExceptionPolicyApply(p, g_applayerparser_error_policy, PKT_DROP_REASON_APPLAYER_ERROR);
1,331✔
765
            AppLayerIncrErrorExcPolicyCounter(tv, f, g_applayerparser_error_policy);
1,331✔
766
            SCReturnInt(-1);
1,331✔
767
        }
1,331✔
768
        goto end;
8,549✔
769
    }
9,880✔
770

771
    /* if we don't know the proto yet and we have received a stream
772
     * initializer message, we run proto detection.
773
     * We receive 2 stream init msgs (one for each direction), we
774
     * only run the proto detection for both and emit an event
775
     * in the case protocols mismatch. */
776
    if (alproto == ALPROTO_UNKNOWN && (flags & STREAM_START)) {
727,592✔
777
        DEBUG_VALIDATE_BUG_ON(FlowChangeProto(f));
88,777✔
778
        /* run protocol detection */
779
        int rd = TCPProtoDetect(
88,777✔
780
                tv, ra_ctx, app_tctx, p, f, ssn, stream, data, data_len, flags, app_update_dir);
88,777✔
781
        if (rd < 0) {
88,777✔
782
            goto failure;
1,621✔
783
        }
1,621✔
784
        if (rd == 2) {
87,156!
NEW
785
            SCReturnInt(1);
×
NEW
786
        }
×
787
    } else if (alproto != ALPROTO_UNKNOWN && FlowChangeProto(f)) {
638,819✔
788
        SCLogDebug("protocol change, old %s", AppProtoToString(f->alproto_orig));
3,236!
789
        void *alstate_orig = f->alstate;
3,236✔
790
        AppLayerParserState *alparser = f->alparser;
3,236✔
791
        // we delay AppLayerParserStateCleanup because we may need previous parser state
792
        AppLayerProtoDetectReset(f);
3,236✔
793
        StreamTcpResetStreamFlagAppProtoDetectionCompleted(&ssn->client);
3,236✔
794
        StreamTcpResetStreamFlagAppProtoDetectionCompleted(&ssn->server);
3,236✔
795
        /* rerun protocol detection */
796
        int rd = TCPProtoDetect(
3,236✔
797
                tv, ra_ctx, app_tctx, p, f, ssn, stream, data, data_len, flags, app_update_dir);
3,236✔
798
        if (f->alproto == ALPROTO_UNKNOWN) {
3,236✔
799
            DEBUG_VALIDATE_BUG_ON(alstate_orig != f->alstate);
557✔
800
            // not enough data, revert AppLayerProtoDetectReset to rerun detection
801
            f->alparser = alparser;
557✔
802
            f->alproto = f->alproto_orig;
557✔
803
            f->alproto_tc = f->alproto_orig;
557✔
804
            f->alproto_ts = f->alproto_orig;
557✔
805
        } else {
2,679✔
806
            FlowUnsetChangeProtoFlag(f);
2,679✔
807
            AppLayerParserStateProtoCleanup(f->protomap, f->alproto_orig, alstate_orig, alparser);
2,679✔
808
            if (alstate_orig == f->alstate) {
2,679!
809
                // we just freed it
810
                f->alstate = NULL;
×
811
            }
×
812
        }
2,679✔
813
        if (rd != 0) {
3,236✔
814
            SCLogDebug("proto detect failure");
8!
815
            goto failure;
8✔
816
        }
8✔
817
        SCLogDebug("protocol change, old %s, new %s",
3,228!
818
                AppProtoToString(f->alproto_orig), AppProtoToString(f->alproto));
3,228✔
819

820
        if (f->alproto_expect != ALPROTO_UNKNOWN && f->alproto != ALPROTO_UNKNOWN &&
3,228!
821
                f->alproto != f->alproto_expect) {
3,228✔
822
            SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_UNEXPECTED_PROTOCOL);
571✔
823

824
            if (f->alproto_expect == ALPROTO_TLS && f->alproto != ALPROTO_TLS) {
571!
825
                SCAppLayerDecoderEventsSetEventRaw(
17✔
826
                        &p->app_layer_events, APPLAYER_NO_TLS_AFTER_STARTTLS);
17✔
827
            }
17✔
828
        }
571✔
829
    } else {
635,579✔
830
        SCLogDebug("stream data (len %" PRIu32 " alproto "
635,579!
831
                   "%"PRIu16" (flow %p)", data_len, f->alproto, f);
635,579✔
832
#ifdef PRINT
833
        if (data_len > 0) {
834
            printf("=> Stream Data (app layer) -- start %s%s\n",
835
                   flags & STREAM_TOCLIENT ? "toclient" : "",
836
                   flags & STREAM_TOSERVER ? "toserver" : "");
837
            PrintRawDataFp(stdout, data, data_len);
838
            printf("=> Stream Data -- end\n");
839
        }
840
#endif
841
        /* if we don't have a data object here we are not getting it
842
         * a start msg should have gotten us one */
843
        if (f->alproto != ALPROTO_UNKNOWN) {
635,600✔
844
            PACKET_PROFILING_APP_START(app_tctx, f->alproto);
635,509✔
845
            r = AppLayerParserParse(tv, app_tctx->alp_tctx, f, f->alproto,
635,509✔
846
                                    flags, data, data_len);
635,509✔
847
            PACKET_PROFILING_APP_END(app_tctx);
635,509✔
848
            p->app_update_direction = (uint8_t)app_update_dir;
635,509✔
849
            if (r != 1) {
635,509✔
850
                StreamTcpUpdateAppLayerProgress(ssn, direction, data_len);
626,407✔
851
                if (r < 0) {
626,407✔
852
                    ExceptionPolicyApply(
425✔
853
                            p, g_applayerparser_error_policy, PKT_DROP_REASON_APPLAYER_ERROR);
425✔
854
                    AppLayerIncrErrorExcPolicyCounter(tv, f, g_applayerparser_error_policy);
425✔
855
                    SCReturnInt(-1);
425✔
856
                }
425✔
857
            }
626,407✔
858
        }
635,509✔
859
    }
635,579✔
860

861
    goto end;
725,538✔
862
 failure:
725,538✔
863
    r = -1;
5,617✔
864
 end:
739,761✔
865
    SCReturnInt(r);
739,761✔
866
}
5,617✔
867

868
/**
869
 *  \brief Handle a app layer UDP message
870
 *
871
 *  If the protocol is yet unknown, the proto detection code is run first.
872
 *
873
 *  \param dp_ctx Thread app layer detect context
874
 *  \param f *locked* flow
875
 *  \param p UDP packet
876
 *
877
 *  \retval 0 ok
878
 *  \retval -1 error
879
 */
880
int AppLayerHandleUdp(ThreadVars *tv, AppLayerThreadCtx *tctx, Packet *p, Flow *f)
881
{
114,152✔
882
    SCEnter();
114,152✔
883
    AppProto *alproto;
114,152✔
884
    AppProto *alproto_otherdir;
114,152✔
885

886
    if (f->alproto_ts == ALPROTO_FAILED && f->alproto_tc == ALPROTO_FAILED) {
114,152✔
887
        SCReturnInt(0);
4,282✔
888
    }
4,282✔
889

890
    int r = 0;
109,870✔
891
    uint8_t flags = 0;
109,870✔
892
    if (p->flowflags & FLOW_PKT_TOSERVER) {
109,870✔
893
        flags |= STREAM_TOSERVER;
91,972✔
894
        alproto = &f->alproto_ts;
91,972✔
895
        alproto_otherdir = &f->alproto_tc;
91,972✔
896
    } else {
91,972✔
897
        flags |= STREAM_TOCLIENT;
17,898✔
898
        alproto = &f->alproto_tc;
17,898✔
899
        alproto_otherdir = &f->alproto_ts;
17,898✔
900
    }
17,898✔
901

902
    AppLayerProfilingReset(tctx);
109,870✔
903

904
    /* if the protocol is still unknown, run detection */
905
    if (*alproto == ALPROTO_UNKNOWN) {
109,870✔
906
        SCLogDebug("Detecting AL proto on udp mesg (len %" PRIu32 ")",
30,923!
907
                   p->payload_len);
30,923✔
908

909
        bool reverse_flow = false;
30,923✔
910
        PACKET_PROFILING_APP_PD_START(tctx);
30,923✔
911
        *alproto = AppLayerProtoDetectGetProto(
30,923✔
912
                tctx->alpd_tctx, f, p->payload, p->payload_len, IPPROTO_UDP, flags, &reverse_flow);
30,923✔
913
        PACKET_PROFILING_APP_PD_END(tctx);
30,923✔
914

915
        switch (*alproto) {
30,923✔
916
            case ALPROTO_UNKNOWN:
13,497✔
917
                if (*alproto_otherdir != ALPROTO_UNKNOWN) {
13,497✔
918
                    // Use recognized side
919
                    f->alproto = *alproto_otherdir;
2,705✔
920
                    // do not keep ALPROTO_UNKNOWN for this side so as not to loop
921
                    *alproto = *alproto_otherdir;
2,705✔
922
                    if (*alproto_otherdir == ALPROTO_FAILED) {
2,705✔
923
                        SCLogDebug("ALPROTO_UNKNOWN flow %p", f);
2,113!
924
                    }
2,113✔
925
                } else {
10,792✔
926
                    // First side of protocol is unknown
927
                    *alproto = ALPROTO_FAILED;
10,792✔
928
                }
10,792✔
929
                break;
13,497✔
930
            case ALPROTO_FAILED:
×
931
                if (*alproto_otherdir != ALPROTO_UNKNOWN) {
×
932
                    // Use recognized side
933
                    f->alproto = *alproto_otherdir;
×
934
                    if (*alproto_otherdir == ALPROTO_FAILED) {
×
935
                        SCLogDebug("ALPROTO_UNKNOWN flow %p", f);
×
936
                    }
×
937
                }
×
938
                // else wait for second side of protocol
939
                break;
×
940
            default:
17,425✔
941
                if (*alproto_otherdir != ALPROTO_UNKNOWN && *alproto_otherdir != ALPROTO_FAILED) {
17,425✔
942
                    if (*alproto_otherdir != *alproto) {
5,142!
943
                        SCAppLayerDecoderEventsSetEventRaw(
1✔
944
                                &p->app_layer_events, APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS);
1✔
945
                        // data already sent to parser, we cannot change the protocol to use the one
946
                        // of the server
947
                    }
1✔
948
                } else {
12,283✔
949
                    f->alproto = *alproto;
12,283✔
950
                }
12,283✔
951
        }
30,923✔
952
        if (*alproto_otherdir == ALPROTO_UNKNOWN) {
30,919✔
953
            if (f->alproto == ALPROTO_UNKNOWN) {
22,936✔
954
                // so as to increase stat about .app_layer.flow.failed_udp
955
                f->alproto = ALPROTO_FAILED;
10,791✔
956
            }
10,791✔
957
            // If the other side is unknown, this is the first packet of the flow
958
            AppLayerIncFlowCounter(tv, f);
22,936✔
959
        }
22,936✔
960

961
        // parse the data if we recognized one protocol
962
        if (f->alproto != ALPROTO_UNKNOWN && f->alproto != ALPROTO_FAILED) {
30,922!
963
            if (reverse_flow) {
18,017✔
964
                SCLogDebug("reversing flow after proto detect told us so");
161!
965
                PacketSwap(p);
161✔
966
                FlowSwap(f);
161✔
967
                SWAP_FLAGS(flags, STREAM_TOSERVER, STREAM_TOCLIENT);
161✔
968
            }
161✔
969

970
            PACKET_PROFILING_APP_START(tctx, f->alproto);
18,017✔
971
            r = AppLayerParserParse(tv, tctx->alp_tctx, f, f->alproto,
18,017✔
972
                                    flags, p->payload, p->payload_len);
18,017✔
973
            PACKET_PROFILING_APP_END(tctx);
18,017✔
974
            if (f->flags & FLOW_NOPAYLOAD_INSPECTION &&
18,017✔
975
                    SCAppLayerParserStateIssetFlag(f->alparser, APP_LAYER_PARSER_BYPASS_READY)) {
18,017✔
976
                PacketBypassCallback(p);
1✔
977
            }
1✔
978
            p->app_update_direction = (uint8_t)UPDATE_DIR_PACKET;
18,017✔
979
        }
18,017✔
980
        PACKET_PROFILING_APP_STORE(tctx, p);
30,919✔
981
        /* we do only inspection in one direction, so flag both
982
         * sides as done here */
983
        FlagPacketFlow(p, f, STREAM_TOSERVER);
30,919✔
984
        FlagPacketFlow(p, f, STREAM_TOCLIENT);
30,919✔
985
    } else {
78,947✔
986
        SCLogDebug("data (len %" PRIu32 " ), alproto "
78,947!
987
                   "%"PRIu16" (flow %p)", p->payload_len, f->alproto, f);
78,947✔
988

989
        /* run the parser */
990
        PACKET_PROFILING_APP_START(tctx, f->alproto);
78,947✔
991
        r = AppLayerParserParse(tv, tctx->alp_tctx, f, f->alproto,
78,947✔
992
                flags, p->payload, p->payload_len);
78,947✔
993
        if (f->flags & FLOW_NOPAYLOAD_INSPECTION &&
78,947✔
994
                SCAppLayerParserStateIssetFlag(f->alparser, APP_LAYER_PARSER_BYPASS_READY)) {
78,947!
995
            PacketBypassCallback(p);
×
996
        }
×
997
        PACKET_PROFILING_APP_END(tctx);
78,947✔
998
        PACKET_PROFILING_APP_STORE(tctx, p);
78,947✔
999
        p->app_update_direction = (uint8_t)UPDATE_DIR_PACKET;
78,947✔
1000
    }
78,947✔
1001
    if (r < 0) {
109,866✔
1002
        ExceptionPolicyApply(p, g_applayerparser_error_policy, PKT_DROP_REASON_APPLAYER_ERROR);
2,936✔
1003
        AppLayerIncrErrorExcPolicyCounter(tv, f, g_applayerparser_error_policy);
2,936✔
1004
        SCReturnInt(-1);
2,936✔
1005
    }
2,936✔
1006

1007
    SCReturnInt(r);
109,866✔
1008
}
109,866✔
1009

1010
/***** Utility *****/
1011

1012
AppProto AppLayerGetProtoByName(const char *alproto_name)
1013
{
1,217,178✔
1014
    SCEnter();
1,217,178✔
1015
    AppProto r = AppLayerProtoDetectGetProtoByName(alproto_name);
1,217,178✔
1016
    SCReturnCT(r, "AppProto");
1,217,178✔
1017
}
1,217,178✔
1018

1019
const char *AppLayerGetProtoName(AppProto alproto)
1020
{
159,541✔
1021
    SCEnter();
159,541✔
1022
    const char * r = AppLayerProtoDetectGetProtoName(alproto);
159,541✔
1023
    SCReturnCT(r, "char *");
159,541✔
1024
}
159,541✔
1025

1026
void AppLayerListSupportedProtocols(void)
1027
{
1✔
1028
    SCEnter();
1✔
1029

1030
    AppProto alproto;
1✔
1031
    AppProto alprotos[g_alproto_max];
1✔
1032

1033
    AppLayerProtoDetectSupportedAppProtocols(alprotos);
1✔
1034

1035
    printf("=========Supported App Layer Protocols=========\n");
1✔
1036
    for (alproto = 0; alproto < g_alproto_max; alproto++) {
42!
1037
        if (alprotos[alproto] == 1)
41!
1038
            printf("%s\n", AppLayerGetProtoName(alproto));
33✔
1039
    }
41✔
1040

1041
    SCReturn;
1✔
1042
}
1✔
1043

1044
/***** Setup/General Registration *****/
1045
static void AppLayerNamesSetup(void)
1046
{
2,196✔
1047
    AppProtoRegisterProtoString(ALPROTO_UNKNOWN, "unknown");
2,196✔
1048
    AppProtoRegisterProtoString(ALPROTO_FAILED, "failed");
2,196✔
1049
    AppProtoRegisterProtoString(ALPROTO_HTTP1, "http1");
2,196✔
1050
    AppProtoRegisterProtoString(ALPROTO_FTP, "ftp");
2,196✔
1051
    AppProtoRegisterProtoString(ALPROTO_SMTP, "smtp");
2,196✔
1052
    AppProtoRegisterProtoString(ALPROTO_TLS, "tls");
2,196✔
1053
    AppProtoRegisterProtoString(ALPROTO_SSH, "ssh");
2,196✔
1054
    AppProtoRegisterProtoString(ALPROTO_IMAP, "imap");
2,196✔
1055
    AppProtoRegisterProtoString(ALPROTO_JABBER, "jabber");
2,196✔
1056
    AppProtoRegisterProtoString(ALPROTO_SMB, "smb");
2,196✔
1057
    AppProtoRegisterProtoString(ALPROTO_DCERPC, "dcerpc");
2,196✔
1058
    AppProtoRegisterProtoString(ALPROTO_IRC, "irc");
2,196✔
1059
    AppProtoRegisterProtoString(ALPROTO_DNS, "dns");
2,196✔
1060
    AppProtoRegisterProtoString(ALPROTO_MODBUS, "modbus");
2,196✔
1061
    AppProtoRegisterProtoString(ALPROTO_ENIP, "enip");
2,196✔
1062
    AppProtoRegisterProtoString(ALPROTO_DNP3, "dnp3");
2,196✔
1063
    AppProtoRegisterProtoString(ALPROTO_NFS, "nfs");
2,196✔
1064
    AppProtoRegisterProtoString(ALPROTO_NTP, "ntp");
2,196✔
1065
    AppProtoRegisterProtoString(ALPROTO_FTPDATA, "ftp-data");
2,196✔
1066
    AppProtoRegisterProtoString(ALPROTO_TFTP, "tftp");
2,196✔
1067
    AppProtoRegisterProtoString(ALPROTO_IKE, "ike");
2,196✔
1068
    AppProtoRegisterProtoString(ALPROTO_KRB5, "krb5");
2,196✔
1069
    AppProtoRegisterProtoString(ALPROTO_QUIC, "quic");
2,196✔
1070
    AppProtoRegisterProtoString(ALPROTO_DHCP, "dhcp");
2,196✔
1071
    AppProtoRegisterProtoString(ALPROTO_SIP, "sip");
2,196✔
1072
    AppProtoRegisterProtoString(ALPROTO_RFB, "rfb");
2,196✔
1073
    AppProtoRegisterProtoString(ALPROTO_MQTT, "mqtt");
2,196✔
1074
    AppProtoRegisterProtoString(ALPROTO_PGSQL, "pgsql");
2,196✔
1075
    AppProtoRegisterProtoString(ALPROTO_TELNET, "telnet");
2,196✔
1076
    AppProtoRegisterProtoString(ALPROTO_WEBSOCKET, "websocket");
2,196✔
1077
    AppProtoRegisterProtoString(ALPROTO_LDAP, "ldap");
2,196✔
1078
    AppProtoRegisterProtoString(ALPROTO_DOH2, "doh2");
2,196✔
1079
    AppProtoRegisterProtoString(ALPROTO_MDNS, "mdns");
2,196✔
1080
    AppProtoRegisterProtoString(ALPROTO_SSLPROXY, "sslproxy");
2,196✔
1081
    AppProtoRegisterProtoString(ALPROTO_TEMPLATE, "template");
2,196✔
1082
    AppProtoRegisterProtoString(ALPROTO_RDP, "rdp");
2,196✔
1083
    AppProtoRegisterProtoString(ALPROTO_HTTP2, "http2");
2,196✔
1084
    AppProtoRegisterProtoString(ALPROTO_BITTORRENT_DHT, "bittorrent-dht");
2,196✔
1085
    AppProtoRegisterProtoString(ALPROTO_POP3, "pop3");
2,196✔
1086
    AppProtoRegisterProtoString(ALPROTO_HTTP, "http");
2,196✔
1087
}
2,196✔
1088

1089
int AppLayerSetup(void)
1090
{
2,196✔
1091
    SCEnter();
2,196✔
1092

1093
    AppLayerNamesSetup();
2,196✔
1094
    AppLayerProtoDetectSetup();
2,196✔
1095
    AppLayerParserSetup();
2,196✔
1096

1097
    AppLayerParserRegisterProtocolParsers();
2,196✔
1098
    AppLayerProtoDetectPrepareState();
2,196✔
1099

1100
    AppLayerSetupCounters();
2,196✔
1101
    FrameConfigInit();
2,196✔
1102

1103
    SCReturnInt(0);
2,196✔
1104
}
2,196✔
1105

1106
int AppLayerDeSetup(void)
1107
{
2,109✔
1108
    SCEnter();
2,109✔
1109

1110
    AppLayerProtoDetectDeSetup();
2,109✔
1111
    AppLayerParserDeSetup();
2,109✔
1112

1113
    AppLayerDeSetupCounters();
2,109✔
1114
    FrameConfigDeInit();
2,109✔
1115

1116
    SCReturnInt(0);
2,109✔
1117
}
2,109✔
1118

1119
AppLayerThreadCtx *AppLayerGetCtxThread(void)
1120
{
30,566✔
1121
    SCEnter();
30,566✔
1122

1123
    AppLayerThreadCtx *app_tctx = SCCalloc(1, sizeof(*app_tctx));
30,566✔
1124
    if (app_tctx == NULL)
30,566!
1125
        goto error;
×
1126

1127
    if ((app_tctx->alpd_tctx = AppLayerProtoDetectGetCtxThread()) == NULL)
30,566!
1128
        goto error;
×
1129
    if ((app_tctx->alp_tctx = AppLayerParserThreadCtxAlloc()) == NULL)
30,566!
1130
        goto error;
×
1131

1132
    goto done;
30,566✔
1133
 error:
30,566✔
1134
    AppLayerDestroyCtxThread(app_tctx);
×
1135
    app_tctx = NULL;
×
1136
 done:
30,566✔
1137
    SCReturnPtr(app_tctx, "void *");
30,566✔
1138
}
×
1139

1140
void AppLayerDestroyCtxThread(AppLayerThreadCtx *app_tctx)
1141
{
30,562✔
1142
    SCEnter();
30,562✔
1143

1144
    if (app_tctx == NULL)
30,562!
1145
        SCReturn;
×
1146

1147
    if (app_tctx->alpd_tctx != NULL)
30,562!
1148
        AppLayerProtoDetectDestroyCtxThread(app_tctx->alpd_tctx);
30,562✔
1149
    if (app_tctx->alp_tctx != NULL)
30,562!
1150
        AppLayerParserThreadCtxFree(app_tctx->alp_tctx);
30,562✔
1151
    SCFree(app_tctx);
30,562✔
1152

1153
    SCReturn;
30,562✔
1154
}
30,562✔
1155

1156
#ifdef PROFILING
1157
void AppLayerProfilingResetInternal(AppLayerThreadCtx *app_tctx)
1158
{
1159
    PACKET_PROFILING_APP_RESET(app_tctx);
1160
}
1161

1162
void AppLayerProfilingStoreInternal(AppLayerThreadCtx *app_tctx, Packet *p)
1163
{
1164
    PACKET_PROFILING_APP_STORE(app_tctx, p);
1165
}
1166
#endif
1167

1168
/** \brief HACK to work around our broken unix manager (re)init loop
1169
 */
1170
void AppLayerRegisterGlobalCounters(void)
1171
{
3,561✔
1172
    StatsRegisterGlobalCounter("http.memuse", HTPMemuseGlobalCounter);
3,561✔
1173
    StatsRegisterGlobalCounter("http.memcap", HTPMemcapGlobalCounter);
3,561✔
1174
    StatsRegisterGlobalCounter("ftp.memuse", FTPMemuseGlobalCounter);
3,561✔
1175
    StatsRegisterGlobalCounter("ftp.memcap", FTPMemcapGlobalCounter);
3,561✔
1176
    StatsRegisterGlobalCounter("app_layer.expectations", ExpectationGetCounter);
3,561✔
1177
    StatsRegisterGlobalCounter("http.byterange.memuse", HTPByteRangeMemuseGlobalCounter);
3,561✔
1178
    StatsRegisterGlobalCounter("http.byterange.memcap", HTPByteRangeMemcapGlobalCounter);
3,561✔
1179
    StatsRegisterGlobalCounter("ippair.memuse", IPPairGetMemuse);
3,561✔
1180
    StatsRegisterGlobalCounter("ippair.memcap", IPPairGetMemcap);
3,561✔
1181
    StatsRegisterGlobalCounter("host.memuse", HostGetMemuse);
3,561✔
1182
    StatsRegisterGlobalCounter("host.memcap", HostGetMemcap);
3,561✔
1183
}
3,561✔
1184

1185
static bool IsAppLayerErrorExceptionPolicyStatsValid(enum ExceptionPolicy policy)
1186
{
21,144✔
1187
    if (EngineModeIsIPS()) {
21,144✔
1188
        return app_layer_error_eps_stats.valid_settings_ips[policy];
21,104✔
1189
    }
21,104✔
1190
    return app_layer_error_eps_stats.valid_settings_ids[policy];
40✔
1191
}
21,144✔
1192

1193
static void AppLayerSetupExceptionPolicyPerProtoCounters(
1194
        uint8_t ipproto_map, AppProto alproto, const char *alproto_str, const char *ipproto_suffix)
1195
{
139,106✔
1196
    if (g_stats_eps_per_app_proto_errors &&
139,106!
1197
            g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
139,106!
1198
        for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1; i < EXCEPTION_POLICY_MAX; i++) {
3,564!
1199
            if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
3,168!
1200
                snprintf(applayer_counter_names[alproto][ipproto_map].eps_name[i],
2,772✔
1201
                        sizeof(applayer_counter_names[alproto][ipproto_map].eps_name[i]),
2,772✔
1202
                        "app_layer.error.%s%s.exception_policy.%s", alproto_str, ipproto_suffix,
2,772✔
1203
                        ExceptionPolicyEnumToString(i, true));
2,772✔
1204
            }
2,772✔
1205
        }
3,168✔
1206
    }
396✔
1207
}
139,106✔
1208

1209
void AppLayerSetupCounters(void)
1210
{
2,196✔
1211
    const uint8_t ipprotos[] = { IPPROTO_TCP, IPPROTO_UDP };
2,196✔
1212
    AppProto alprotos[g_alproto_max];
2,196✔
1213
    const char *str = "app_layer.flow.";
2,196✔
1214
    const char *estr = "app_layer.error.";
2,196✔
1215

1216
    applayer_counter_names =
2,196✔
1217
            SCCalloc(g_alproto_max, sizeof(AppLayerCounterNames[FLOW_PROTO_APPLAYER_MAX]));
2,196✔
1218
    if (unlikely(applayer_counter_names == NULL)) {
2,196!
1219
        FatalError("Unable to alloc applayer_counter_names.");
×
1220
    }
×
1221
    applayer_counters = SCCalloc(g_alproto_max, sizeof(AppLayerCounters[FLOW_PROTO_APPLAYER_MAX]));
2,196✔
1222
    if (unlikely(applayer_counters == NULL)) {
2,196!
1223
        FatalError("Unable to alloc applayer_counters.");
×
1224
    }
×
1225
    /* We don't log stats counters if exception policy is `ignore`/`not set` */
1226
    if (g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
2,196✔
1227
        /* Register global counters for app layer error exception policy summary */
1228
        const char *eps_default_str = "exception_policy.app_layer.error.";
234✔
1229
        for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1; i < EXCEPTION_POLICY_MAX; i++) {
2,106✔
1230
            if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
1,872✔
1231
                snprintf(app_layer_error_eps_stats.eps_name[i],
1,636✔
1232
                        sizeof(app_layer_error_eps_stats.eps_name[i]), "%s%s", eps_default_str,
1,636✔
1233
                        ExceptionPolicyEnumToString(i, true));
1,636✔
1234
            }
1,636✔
1235
        }
1,872✔
1236
    }
234✔
1237

1238
    AppLayerProtoDetectSupportedAppProtocols(alprotos);
2,196✔
1239

1240
    for (uint8_t p = 0; p < FLOW_PROTO_APPLAYER_MAX; p++) {
6,588✔
1241
        const uint8_t ipproto = ipprotos[p];
4,392✔
1242
        const uint8_t ipproto_map = FlowGetProtoMapping(ipproto);
4,392✔
1243
        const char *ipproto_suffix = (ipproto == IPPROTO_TCP) ? "_tcp" : "_udp";
4,392✔
1244
        uint8_t ipprotos_all[256 / 8];
4,392✔
1245

1246
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
184,462✔
1247
            if (alprotos[alproto] == 1) {
180,070✔
1248
                const char *tx_str = "app_layer.tx.";
139,106✔
1249
                const char *alproto_str = AppLayerGetProtoName(alproto);
139,106✔
1250

1251
                memset(ipprotos_all, 0, sizeof(ipprotos_all));
139,106✔
1252
                AppLayerProtoDetectSupportedIpprotos(alproto, ipprotos_all);
139,106✔
1253
                if ((ipprotos_all[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) &&
139,106✔
1254
                        (ipprotos_all[IPPROTO_UDP / 8] & (1 << (IPPROTO_UDP % 8)))) {
139,106✔
1255
                    snprintf(applayer_counter_names[alproto][ipproto_map].name,
27,768✔
1256
                            sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s%s",
27,768✔
1257
                            str, alproto_str, ipproto_suffix);
27,768✔
1258
                    snprintf(applayer_counter_names[alproto][ipproto_map].tx_name,
27,768✔
1259
                            sizeof(applayer_counter_names[alproto][ipproto_map].tx_name), "%s%s%s",
27,768✔
1260
                            tx_str, alproto_str, ipproto_suffix);
27,768✔
1261

1262
                    if (ipproto == IPPROTO_TCP) {
27,768✔
1263
                        snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
13,884✔
1264
                                sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
13,884✔
1265
                                "%s%s%s.gap", estr, alproto_str, ipproto_suffix);
13,884✔
1266
                    }
13,884✔
1267
                    snprintf(applayer_counter_names[alproto][ipproto_map].alloc_error,
27,768✔
1268
                            sizeof(applayer_counter_names[alproto][ipproto_map].alloc_error),
27,768✔
1269
                            "%s%s%s.alloc", estr, alproto_str, ipproto_suffix);
27,768✔
1270
                    snprintf(applayer_counter_names[alproto][ipproto_map].parser_error,
27,768✔
1271
                            sizeof(applayer_counter_names[alproto][ipproto_map].parser_error),
27,768✔
1272
                            "%s%s%s.parser", estr, alproto_str, ipproto_suffix);
27,768✔
1273
                    snprintf(applayer_counter_names[alproto][ipproto_map].internal_error,
27,768✔
1274
                            sizeof(applayer_counter_names[alproto][ipproto_map].internal_error),
27,768✔
1275
                            "%s%s%s.internal", estr, alproto_str, ipproto_suffix);
27,768✔
1276

1277
                    AppLayerSetupExceptionPolicyPerProtoCounters(
27,768✔
1278
                            ipproto_map, alproto, alproto_str, ipproto_suffix);
27,768✔
1279
                } else {
111,338✔
1280
                    snprintf(applayer_counter_names[alproto][ipproto_map].name,
111,338✔
1281
                            sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s", str,
111,338✔
1282
                            alproto_str);
111,338✔
1283
                    snprintf(applayer_counter_names[alproto][ipproto_map].tx_name,
111,338✔
1284
                            sizeof(applayer_counter_names[alproto][ipproto_map].tx_name), "%s%s",
111,338✔
1285
                            tx_str, alproto_str);
111,338✔
1286

1287
                    if (ipproto == IPPROTO_TCP) {
111,338✔
1288
                        snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
55,669✔
1289
                                sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
55,669✔
1290
                                "%s%s.gap", estr, alproto_str);
55,669✔
1291
                    }
55,669✔
1292
                    snprintf(applayer_counter_names[alproto][ipproto_map].alloc_error,
111,338✔
1293
                            sizeof(applayer_counter_names[alproto][ipproto_map].alloc_error),
111,338✔
1294
                            "%s%s.alloc", estr, alproto_str);
111,338✔
1295
                    snprintf(applayer_counter_names[alproto][ipproto_map].parser_error,
111,338✔
1296
                            sizeof(applayer_counter_names[alproto][ipproto_map].parser_error),
111,338✔
1297
                            "%s%s.parser", estr, alproto_str);
111,338✔
1298
                    snprintf(applayer_counter_names[alproto][ipproto_map].internal_error,
111,338✔
1299
                            sizeof(applayer_counter_names[alproto][ipproto_map].internal_error),
111,338✔
1300
                            "%s%s.internal", estr, alproto_str);
111,338✔
1301
                    AppLayerSetupExceptionPolicyPerProtoCounters(
111,338✔
1302
                            ipproto_map, alproto, alproto_str, "");
111,338✔
1303
                }
111,338✔
1304
            } else if (alproto == ALPROTO_FAILED) {
139,106✔
1305
                snprintf(applayer_counter_names[alproto][ipproto_map].name,
4,392✔
1306
                        sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s%s", str,
4,392✔
1307
                        "failed", ipproto_suffix);
4,392✔
1308
                if (ipproto == IPPROTO_TCP) {
4,392✔
1309
                    snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
2,196✔
1310
                            sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
2,196✔
1311
                            "%sfailed%s.gap", estr, ipproto_suffix);
2,196✔
1312
                }
2,196✔
1313
            }
4,392✔
1314
        }
180,070✔
1315
    }
4,392✔
1316
}
2,196✔
1317

1318
void AppLayerRegisterThreadCounters(ThreadVars *tv)
1319
{
13,450✔
1320
    const uint8_t ipprotos[] = { IPPROTO_TCP, IPPROTO_UDP };
13,450✔
1321
    AppProto alprotos[g_alproto_max];
13,450✔
1322
    AppLayerProtoDetectSupportedAppProtocols(alprotos);
13,450✔
1323

1324
    /* We don't log stats counters if exception policy is `ignore`/`not set` */
1325
    if (g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
13,450✔
1326
        /* Register global counters for app layer error exception policy summary */
1327
        for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1; i < EXCEPTION_POLICY_MAX; i++) {
7,938✔
1328
            if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
7,056✔
1329
                eps_error_summary.eps_id[i] =
6,166✔
1330
                        StatsRegisterCounter(app_layer_error_eps_stats.eps_name[i], &tv->stats);
6,166✔
1331
            }
6,166✔
1332
        }
7,056✔
1333
    }
882✔
1334

1335
    for (uint8_t p = 0; p < FLOW_PROTO_APPLAYER_MAX; p++) {
40,350✔
1336
        const uint8_t ipproto = ipprotos[p];
26,900✔
1337
        const uint8_t ipproto_map = FlowGetProtoMapping(ipproto);
26,900✔
1338

1339
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
1,129,792✔
1340
            if (alprotos[alproto] == 1) {
1,102,892✔
1341
                applayer_counters[alproto][ipproto_map].counter_id = StatsRegisterCounter(
843,686✔
1342
                        applayer_counter_names[alproto][ipproto_map].name, &tv->stats);
843,686✔
1343

1344
                if (AppLayerParserProtoIsRegistered(ipproto, alproto) != 1)
843,686✔
1345
                    continue;
352,218✔
1346

1347
                applayer_counters[alproto][ipproto_map].counter_tx_id = StatsRegisterCounter(
491,468✔
1348
                        applayer_counter_names[alproto][ipproto_map].tx_name, &tv->stats);
491,468✔
1349

1350
                if (ipproto == IPPROTO_TCP) {
491,468✔
1351
                    applayer_counters[alproto][ipproto_map].gap_error_id = StatsRegisterCounter(
300,833✔
1352
                            applayer_counter_names[alproto][ipproto_map].gap_error, &tv->stats);
300,833✔
1353
                }
300,833✔
1354
                applayer_counters[alproto][ipproto_map].alloc_error_id = StatsRegisterCounter(
491,468✔
1355
                        applayer_counter_names[alproto][ipproto_map].alloc_error, &tv->stats);
491,468✔
1356
                applayer_counters[alproto][ipproto_map].parser_error_id = StatsRegisterCounter(
491,468✔
1357
                        applayer_counter_names[alproto][ipproto_map].parser_error, &tv->stats);
491,468✔
1358
                applayer_counters[alproto][ipproto_map].internal_error_id = StatsRegisterCounter(
491,468✔
1359
                        applayer_counter_names[alproto][ipproto_map].internal_error, &tv->stats);
491,468✔
1360
                /* We don't log stats counters if exception policy is `ignore`/`not set` */
1361
                if (g_stats_eps_per_app_proto_errors &&
491,468!
1362
                        g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
491,468!
1363
                    for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1;
1,131✔
1364
                            i < EXCEPTION_POLICY_MAX; i++) {
10,179!
1365
                        if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
9,048!
1366
                            applayer_counters[alproto][ipproto_map]
7,917✔
1367
                                    .eps_error.eps_id[i] = StatsRegisterCounter(
7,917✔
1368
                                    applayer_counter_names[alproto][ipproto_map].eps_name[i],
7,917✔
1369
                                    &tv->stats);
7,917✔
1370
                        }
7,917✔
1371
                    }
9,048✔
1372
                }
1,131✔
1373
            } else if (alproto == ALPROTO_FAILED) {
491,468✔
1374
                applayer_counters[alproto][ipproto_map].counter_id = StatsRegisterCounter(
26,900✔
1375
                        applayer_counter_names[alproto][ipproto_map].name, &tv->stats);
26,900✔
1376

1377
                if (ipproto == IPPROTO_TCP) {
26,900✔
1378
                    applayer_counters[alproto][ipproto_map].gap_error_id = StatsRegisterCounter(
13,450✔
1379
                            applayer_counter_names[alproto][ipproto_map].gap_error, &tv->stats);
13,450✔
1380
                }
13,450✔
1381
            }
26,900✔
1382
        }
1,102,892✔
1383
    }
26,900✔
1384
}
13,450✔
1385

1386
void AppLayerDeSetupCounters(void)
1387
{
2,109✔
1388
    SCFree(applayer_counter_names);
2,109✔
1389
    SCFree(applayer_counters);
2,109✔
1390
}
2,109✔
1391

1392
/***** Unittests *****/
1393

1394
#ifdef UNITTESTS
1395
#include "pkt-var.h"
1396
#include "stream-tcp-util.h"
1397
#include "stream.h"
1398
#include "util-unittest.h"
1399

1400
#define TEST_START                                                                                 \
1401
    Packet *p = PacketGetFromAlloc();                                                              \
10✔
1402
    FAIL_IF_NULL(p);                                                                               \
10✔
1403
    Flow f;                                                                                        \
10✔
1404
    ThreadVars tv;                                                                                 \
10✔
1405
    StreamTcpThread *stt = NULL;                                                                   \
10✔
1406
    TCPHdr tcph;                                                                                   \
10✔
1407
    PacketQueueNoLock pq;                                                                          \
10✔
1408
    memset(&pq, 0, sizeof(PacketQueueNoLock));                                                     \
10✔
1409
    memset(&f, 0, sizeof(Flow));                                                                   \
10✔
1410
    memset(&tv, 0, sizeof(ThreadVars));                                                            \
10✔
1411
    StatsThreadInit(&tv.stats);                                                                    \
10✔
1412
    memset(&tcph, 0, sizeof(TCPHdr));                                                              \
10✔
1413
                                                                                                   \
10✔
1414
    FLOW_INITIALIZE(&f);                                                                           \
10✔
1415
    f.flags = FLOW_IPV4;                                                                           \
10✔
1416
    f.proto = IPPROTO_TCP;                                                                         \
10✔
1417
    p->flow = &f;                                                                                  \
10✔
1418
    PacketSetTCP(p, (uint8_t *)&tcph);                                                             \
10✔
1419
                                                                                                   \
10✔
1420
    StreamTcpInitConfig(true);                                                                     \
10✔
1421
    IPPairInitConfig(true);                                                                        \
10✔
1422
    StreamTcpThreadInit(&tv, NULL, (void **)&stt);                                                 \
10✔
1423
                                                                                                   \
10✔
1424
    /* handshake */                                                                                \
10✔
1425
    tcph.th_win = htons(5480);                                                                     \
10✔
1426
    tcph.th_flags = TH_SYN;                                                                        \
10✔
1427
    p->flowflags = FLOW_PKT_TOSERVER;                                                              \
10✔
1428
    p->payload_len = 0;                                                                            \
10✔
1429
    p->payload = NULL;                                                                             \
10✔
1430
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);                                              \
10✔
1431
    TcpSession *ssn = (TcpSession *)f.protoctx;                                                    \
10✔
1432
                                                                                                   \
10✔
1433
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));                     \
10✔
1434
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));                     \
10✔
1435
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);                                                         \
10✔
1436
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);                                                      \
10✔
1437
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);                                                      \
10✔
1438
    FAIL_IF(ssn->flags &STREAMTCP_FLAG_APP_LAYER_DISABLED);                                        \
10✔
1439
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));                                                 \
10✔
1440
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));                                                 \
10✔
1441
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));                                                 \
10✔
1442
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));                                                 \
10✔
1443
    FAIL_IF(ssn->data_first_seen_dir != 0);                                                        \
10✔
1444
                                                                                                   \
10✔
1445
    /* handshake */                                                                                \
10✔
1446
    tcph.th_ack = htonl(1);                                                                        \
10✔
1447
    tcph.th_flags = TH_SYN | TH_ACK;                                                               \
10✔
1448
    p->flowflags = FLOW_PKT_TOCLIENT;                                                              \
10✔
1449
    p->payload_len = 0;                                                                            \
10✔
1450
    p->payload = NULL;                                                                             \
10✔
1451
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);                                              \
10✔
1452
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));                     \
10✔
1453
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));                     \
10✔
1454
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);                                                         \
10✔
1455
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);                                                      \
10✔
1456
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);                                                      \
10✔
1457
    FAIL_IF(ssn->flags &STREAMTCP_FLAG_APP_LAYER_DISABLED);                                        \
10✔
1458
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));                                                 \
10✔
1459
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));                                                 \
10✔
1460
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));                                                 \
10✔
1461
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));                                                 \
10✔
1462
    FAIL_IF(ssn->data_first_seen_dir != 0);                                                        \
10✔
1463
                                                                                                   \
10✔
1464
    /* handshake */                                                                                \
10✔
1465
    tcph.th_ack = htonl(1);                                                                        \
10✔
1466
    tcph.th_seq = htonl(1);                                                                        \
10✔
1467
    tcph.th_flags = TH_ACK;                                                                        \
10✔
1468
    p->flowflags = FLOW_PKT_TOSERVER;                                                              \
10✔
1469
    p->payload_len = 0;                                                                            \
10✔
1470
    p->payload = NULL;                                                                             \
10✔
1471
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);                                              \
10✔
1472
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));                     \
10✔
1473
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));                     \
10✔
1474
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);                                                         \
10✔
1475
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);                                                      \
10✔
1476
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);                                                      \
10✔
1477
    FAIL_IF(ssn->flags &STREAMTCP_FLAG_APP_LAYER_DISABLED);                                        \
10✔
1478
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));                                                 \
10✔
1479
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));                                                 \
10✔
1480
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));                                                 \
10✔
1481
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));                                                 \
10✔
1482
    FAIL_IF(ssn->data_first_seen_dir != 0);
10✔
1483
#define TEST_END                                                                                   \
1484
    StreamTcpSessionClear(p->flow->protoctx);                                                      \
10✔
1485
    StreamTcpThreadDeinit(&tv, (void *)stt);                                                       \
10✔
1486
    StreamTcpFreeConfig(true);                                                                     \
10✔
1487
    PacketFree(p);                                                                                 \
10✔
1488
    FLOW_DESTROY(&f);                                                                              \
10✔
1489
    IPPairShutdown();                                                                              \
10✔
1490
    StatsThreadCleanup(&tv.stats);
10✔
1491

1492
/**
1493
 * \test GET -> HTTP/1.1
1494
 */
1495
static int AppLayerTest01(void)
1496
{
1✔
1497
    TEST_START;
37✔
1498

1499
    /* full request */
1500
    uint8_t request[] = {
37✔
1501
        0x47, 0x45, 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
1502
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
37✔
1503
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
37✔
1504
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
37✔
1505
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
37✔
1506
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
37✔
1507
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
37✔
1508
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
37✔
1509
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
37✔
1510
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
37✔
1511
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
37✔
1512
    tcph.th_ack = htonl(1);
37✔
1513
    tcph.th_seq = htonl(1);
37✔
1514
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
1515
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
1516
    p->payload_len = sizeof(request);
37✔
1517
    p->payload = request;
37✔
1518
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
1519
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1520
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1521
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
1522
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
1523
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1524
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1525
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1526
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1527
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1528
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1529
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
1530

1531
    /* full response - request ack */
1532
    uint8_t response[] = {
1✔
1533
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
1✔
1534
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
1535
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
1536
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
1537
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
1538
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
1539
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
1540
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
1541
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
1542
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
1543
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
1544
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
1545
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
1546
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
1547
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
1548
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
1549
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
1550
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
1551
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
1552
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
1553
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
1554
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
1555
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
1556
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
1557
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
1558
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
1559
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
1560
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
1561
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
1562
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
1563
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
1564
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
1565
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
1566
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
1567
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
1568
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
1569
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
1570
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
1571
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
1572
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
1573
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
1574
    tcph.th_ack = htonl(88);
1✔
1575
    tcph.th_seq = htonl(1);
1✔
1576
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
1577
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
1578
    p->payload_len = sizeof(response);
1✔
1579
    p->payload = response;
1✔
1580
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1581
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1582
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1583
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
1584
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
1585
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1586
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1587
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1588
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1589
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1590
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1591
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
1592

1593
    /* response ack */
1594
    tcph.th_ack = htonl(328);
1✔
1595
    tcph.th_seq = htonl(88);
1✔
1596
    tcph.th_flags = TH_ACK;
1✔
1597
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
1598
    p->payload_len = 0;
1✔
1599
    p->payload = NULL;
1✔
1600
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1601
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1602
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1603
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
1604
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
1605
    FAIL_IF(f.alproto_tc != ALPROTO_HTTP1);
1✔
1606
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1607
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1608
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1609
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1610
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1611
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
1612

1613
    TEST_END;
1✔
1614
    PASS;
1✔
1615
}
1✔
1616

1617
/**
1618
 * \test GE -> T -> HTTP/1.1
1619
 */
1620
static int AppLayerTest02(void)
1621
{
1✔
1622
    TEST_START;
37✔
1623

1624
    /* partial request */
1625
    uint8_t request1[] = { 0x47, 0x45, };
37✔
1626
    tcph.th_ack = htonl(1);
37✔
1627
    tcph.th_seq = htonl(1);
37✔
1628
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
1629
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
1630
    p->payload_len = sizeof(request1);
37✔
1631
    p->payload = request1;
37✔
1632
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
1633
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1634
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1635
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
1636
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
1637
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1638
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1639
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1640
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1641
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1642
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1643
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
1644

1645
    /* response ack against partial request */
1646
    tcph.th_ack = htonl(3);
1✔
1647
    tcph.th_seq = htonl(1);
1✔
1648
    tcph.th_flags = TH_ACK;
1✔
1649
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
1650
    p->payload_len = 0;
1✔
1651
    p->payload = NULL;
1✔
1652
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1653
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1654
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1655
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
1656
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
1657
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1658
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1659
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1660
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1661
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1662
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1663
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
1664

1665
    /* complete partial request */
1666
    uint8_t request2[] = {
1✔
1667
        0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
1✔
1668
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
1✔
1669
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
1✔
1670
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
1✔
1671
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
1✔
1672
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
1✔
1673
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
1✔
1674
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
1✔
1675
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
1✔
1676
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
1✔
1677
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
1✔
1678
    tcph.th_ack = htonl(1);
1✔
1679
    tcph.th_seq = htonl(3);
1✔
1680
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
1681
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
1682
    p->payload_len = sizeof(request2);
1✔
1683
    p->payload = request2;
1✔
1684
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1685
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1686
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1687
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
1688
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
1689
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1690
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1691
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1692
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1693
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1694
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1695
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
1696

1697
    /* response - request ack */
1698
    uint8_t response[] = {
1✔
1699
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
1✔
1700
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
1701
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
1702
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
1703
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
1704
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
1705
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
1706
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
1707
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
1708
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
1709
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
1710
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
1711
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
1712
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
1713
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
1714
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
1715
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
1716
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
1717
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
1718
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
1719
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
1720
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
1721
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
1722
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
1723
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
1724
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
1725
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
1726
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
1727
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
1728
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
1729
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
1730
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
1731
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
1732
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
1733
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
1734
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
1735
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
1736
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
1737
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
1738
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
1739
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
1740
    tcph.th_ack = htonl(88);
1✔
1741
    tcph.th_seq = htonl(1);
1✔
1742
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
1743
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
1744
    p->payload_len = sizeof(response);
1✔
1745
    p->payload = response;
1✔
1746
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1747
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1748
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1749
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
1750
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
1751
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1752
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1753
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1754
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1755
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1756
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1757
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
1758

1759
    /* response ack */
1760
    tcph.th_ack = htonl(328);
1✔
1761
    tcph.th_seq = htonl(88);
1✔
1762
    tcph.th_flags = TH_ACK;
1✔
1763
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
1764
    p->payload_len = 0;
1✔
1765
    p->payload = NULL;
1✔
1766
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1767
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1768
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1769
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
1770
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
1771
    FAIL_IF(f.alproto_tc != ALPROTO_HTTP1);
1✔
1772
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1773
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1774
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1775
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1776
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1777
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
1778

1779
    TEST_END;
1✔
1780
    PASS;
1✔
1781
}
1✔
1782

1783
/**
1784
 * \test GET -> RUBBISH(PM AND PP DONE IN ONE GO)
1785
 */
1786
static int AppLayerTest03(void)
1787
{
1✔
1788
    TEST_START;
37✔
1789

1790
    /* request */
1791
    uint8_t request[] = {
37✔
1792
        0x47, 0x45, 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
1793
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
37✔
1794
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
37✔
1795
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
37✔
1796
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
37✔
1797
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
37✔
1798
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
37✔
1799
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
37✔
1800
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
37✔
1801
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
37✔
1802
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
37✔
1803
    tcph.th_ack = htonl(1);
37✔
1804
    tcph.th_seq = htonl(1);
37✔
1805
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
1806
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
1807
    p->payload_len = sizeof(request);
37✔
1808
    p->payload = request;
37✔
1809
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
1810
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1811
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1812
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
1813
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
1814
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1815
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1816
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1817
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1818
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1819
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1820
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
1821

1822
    /* rubbish response */
1823
    uint8_t response[] = {
1✔
1824
        0x58, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
1✔
1825
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
1826
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
1827
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
1828
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
1829
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
1830
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
1831
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
1832
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
1833
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
1834
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
1835
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
1836
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
1837
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
1838
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
1839
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
1840
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
1841
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
1842
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
1843
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
1844
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
1845
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
1846
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
1847
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
1848
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
1849
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
1850
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
1851
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
1852
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
1853
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
1854
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
1855
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
1856
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
1857
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
1858
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
1859
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
1860
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
1861
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
1862
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
1863
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
1864
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
1865
    tcph.th_ack = htonl(88);
1✔
1866
    tcph.th_seq = htonl(1);
1✔
1867
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
1868
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
1869
    p->payload_len = sizeof(response);
1✔
1870
    p->payload = response;
1✔
1871
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1872
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1873
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1874
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
1875
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
1876
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1877
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1878
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1879
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1880
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1881
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1882
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
1883

1884
    /* response ack */
1885
    tcph.th_ack = htonl(328);
1✔
1886
    tcph.th_seq = htonl(88);
1✔
1887
    tcph.th_flags = TH_ACK;
1✔
1888
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
1889
    p->payload_len = 0;
1✔
1890
    p->payload = NULL;
1✔
1891
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1892
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1893
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1894
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
1895
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
1896
    FAIL_IF(f.alproto_tc != ALPROTO_FAILED);
1✔
1897
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1898
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1899
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1900
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1901
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1902
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
1903

1904
    TEST_END;
1✔
1905
    PASS;
1✔
1906
}
1✔
1907

1908
/**
1909
 * \test GE -> RUBBISH(TC - PM AND PP NOT DONE) -> RUBBISH(TC - PM AND PP DONE).
1910
 */
1911
static int AppLayerTest04(void)
1912
{
1✔
1913
    TEST_START;
37✔
1914

1915
    /* request */
1916
    uint8_t request[] = {
37✔
1917
        0x47, 0x45, 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
1918
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
37✔
1919
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
37✔
1920
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
37✔
1921
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
37✔
1922
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
37✔
1923
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
37✔
1924
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
37✔
1925
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
37✔
1926
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
37✔
1927
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
37✔
1928
    PrintRawDataFp(stdout, request, sizeof(request));
37✔
1929
    tcph.th_ack = htonl(1);
37✔
1930
    tcph.th_seq = htonl(1);
37✔
1931
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
1932
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
1933
    p->payload_len = sizeof(request);
37✔
1934
    p->payload = request;
37✔
1935
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
1936
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1937
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
1938
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
1939
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
1940
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1941
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1942
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1943
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1944
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1945
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1946
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);   // TOSERVER data now seen
1✔
1947

1948
    /* partial response */
1949
    uint8_t response1[] = { 0x58, 0x54, 0x54, 0x50, };
1✔
1950
    PrintRawDataFp(stdout, response1, sizeof(response1));
1✔
1951
    tcph.th_ack = htonl(88);
1✔
1952
    tcph.th_seq = htonl(1);
1✔
1953
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
1954
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
1955
    p->payload_len = sizeof(response1);
1✔
1956
    p->payload = response1;
1✔
1957
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1958
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1959
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client)); // toserver complete
1✔
1960
    FAIL_IF(f.alproto != ALPROTO_HTTP1);                                        // http based on ts
1✔
1961
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);                                     // ts complete
1✔
1962
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1963
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1964
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1965
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1966
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1967
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
1968
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);  // first data sent to applayer
1✔
1969

1970
    /* partial response ack */
1971
    tcph.th_ack = htonl(5);
1✔
1972
    tcph.th_seq = htonl(88);
1✔
1973
    tcph.th_flags = TH_ACK;
1✔
1974
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
1975
    p->payload_len = 0;
1✔
1976
    p->payload = NULL;
1✔
1977
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
1978
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
1979
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client)); // toserver complete
1✔
1980
    FAIL_IF(f.alproto != ALPROTO_HTTP1);                                        // http based on ts
1✔
1981
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);                                     // ts complete
1✔
1982
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
1983
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
1984
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
1985
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
1986
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
1987
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));         // to client pp got nothing
1✔
1988
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);  // first data sent to applayer
1✔
1989

1990
    /* remaining response */
1991
    uint8_t response2[] = {
1✔
1992
        0x2f, 0x31, 0x2e, 0x31,
1✔
1993
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
1994
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
1995
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
1996
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
1997
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
1998
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
1999
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
2000
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
2001
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
2002
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
2003
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
2004
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
2005
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
2006
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
2007
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
2008
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
2009
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
2010
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
2011
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
2012
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
2013
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
2014
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
2015
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
2016
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
2017
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
2018
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
2019
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
2020
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
2021
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
2022
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
2023
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
2024
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
2025
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
2026
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
2027
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
2028
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
2029
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
2030
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
2031
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
2032
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
2033
    PrintRawDataFp(stdout, response2, sizeof(response2));
1✔
2034
    tcph.th_ack = htonl(88);
1✔
2035
    tcph.th_seq = htonl(5);
1✔
2036
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2037
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2038
    p->payload_len = sizeof(response2);
1✔
2039
    p->payload = response2;
1✔
2040
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2041
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2042
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client)); // toserver complete
1✔
2043
    FAIL_IF(f.alproto != ALPROTO_HTTP1);                                        // http based on ts
1✔
2044
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);                                     // ts complete
1✔
2045
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2046
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2047
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2048
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2049
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2050
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));         // to client pp got nothing
1✔
2051
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);  // first data sent to applayer
1✔
2052

2053
    /* response ack */
2054
    tcph.th_ack = htonl(328);
1✔
2055
    tcph.th_seq = htonl(88);
1✔
2056
    tcph.th_flags = TH_ACK;
1✔
2057
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2058
    p->payload_len = 0;
1✔
2059
    p->payload = NULL;
1✔
2060
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2061
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server)); // toclient complete (failed)
1✔
2062
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client)); // toserver complete
1✔
2063
    FAIL_IF(f.alproto != ALPROTO_HTTP1);                                        // http based on ts
1✔
2064
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);                                     // ts complete
1✔
2065
    FAIL_IF(f.alproto_tc != ALPROTO_FAILED);                // tc failed
1✔
2066
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2067
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2068
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2069
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2070
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));         // to client pp got nothing
1✔
2071
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);  // first data sent to applayer
1✔
2072

2073
    TEST_END;
1✔
2074
    PASS;
1✔
2075
}
1✔
2076

2077
/**
2078
 * \test RUBBISH -> HTTP/1.1
2079
 */
2080
static int AppLayerTest05(void)
2081
{
1✔
2082
    TEST_START;
37✔
2083

2084
    /* full request */
2085
    uint8_t request[] = {
37✔
2086
        0x48, 0x45, 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
2087
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
37✔
2088
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
37✔
2089
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
37✔
2090
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
37✔
2091
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
37✔
2092
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
37✔
2093
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
37✔
2094
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
37✔
2095
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
37✔
2096
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
37✔
2097
    PrintRawDataFp(stdout, request, sizeof(request));
37✔
2098
    tcph.th_ack = htonl(1);
37✔
2099
    tcph.th_seq = htonl(1);
37✔
2100
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
2101
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
2102
    p->payload_len = sizeof(request);
37✔
2103
    p->payload = request;
37✔
2104
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
2105
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2106
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2107
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2108
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2109
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2110
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2111
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2112
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2113
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2114
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2115
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2116

2117
    /* full response - request ack */
2118
    uint8_t response[] = {
1✔
2119
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
1✔
2120
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
2121
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
2122
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
2123
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
2124
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
2125
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
2126
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
2127
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
2128
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
2129
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
2130
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
2131
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
2132
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
2133
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
2134
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
2135
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
2136
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
2137
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
2138
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
2139
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
2140
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
2141
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
2142
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
2143
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
2144
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
2145
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
2146
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
2147
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
2148
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
2149
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
2150
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
2151
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
2152
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
2153
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
2154
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
2155
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
2156
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
2157
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
2158
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
2159
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
2160
    PrintRawDataFp(stdout, response, sizeof(response));
1✔
2161
    tcph.th_ack = htonl(88);
1✔
2162
    tcph.th_seq = htonl(1);
1✔
2163
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2164
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2165
    p->payload_len = sizeof(response);
1✔
2166
    p->payload = response;
1✔
2167
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2168
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2169
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2170
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2171
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2172
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2173
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2174
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2175
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2176
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2177
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2178
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2179

2180
    /* response ack */
2181
    tcph.th_ack = htonl(328);
1✔
2182
    tcph.th_seq = htonl(88);
1✔
2183
    tcph.th_flags = TH_ACK;
1✔
2184
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2185
    p->payload_len = 0;
1✔
2186
    p->payload = NULL;
1✔
2187
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2188
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2189
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2190
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
2191
    FAIL_IF(f.alproto_ts != ALPROTO_FAILED);
1✔
2192
    FAIL_IF(f.alproto_tc != ALPROTO_HTTP1);
1✔
2193
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2194
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2195
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2196
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2197
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2198
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2199

2200
    TEST_END;
1✔
2201
    PASS;
1✔
2202
}
1✔
2203

2204
/**
2205
 * \test HTTP/1.1 -> GET
2206
 */
2207
static int AppLayerTest06(void)
2208
{
1✔
2209
    TEST_START;
37✔
2210

2211
    /* full response - request ack */
2212
    uint8_t response[] = {
37✔
2213
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
37✔
2214
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
37✔
2215
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
37✔
2216
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
37✔
2217
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
37✔
2218
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
37✔
2219
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
37✔
2220
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
37✔
2221
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
37✔
2222
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
37✔
2223
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
37✔
2224
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
37✔
2225
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
37✔
2226
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
37✔
2227
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
37✔
2228
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
37✔
2229
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
37✔
2230
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
37✔
2231
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
37✔
2232
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
37✔
2233
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
37✔
2234
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
37✔
2235
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
37✔
2236
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
37✔
2237
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
37✔
2238
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
37✔
2239
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
37✔
2240
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
37✔
2241
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
37✔
2242
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
37✔
2243
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
37✔
2244
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
37✔
2245
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
37✔
2246
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
37✔
2247
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
37✔
2248
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
37✔
2249
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
37✔
2250
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
37✔
2251
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
37✔
2252
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
37✔
2253
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
37✔
2254
    tcph.th_ack = htonl(1);
37✔
2255
    tcph.th_seq = htonl(1);
37✔
2256
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
2257
    p->flowflags = FLOW_PKT_TOCLIENT;
37✔
2258
    p->payload_len = sizeof(response);
37✔
2259
    p->payload = response;
37✔
2260
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
2261
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2262
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2263
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2264
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2265
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2266
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2267
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2268
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2269
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2270
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2271
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOCLIENT);
1✔
2272

2273
    /* full request - response ack*/
2274
    uint8_t request[] = {
1✔
2275
        0x47, 0x45, 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
1✔
2276
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
1✔
2277
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
1✔
2278
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
1✔
2279
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
1✔
2280
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
1✔
2281
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
1✔
2282
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
1✔
2283
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
1✔
2284
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
1✔
2285
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
1✔
2286
    tcph.th_ack = htonl(328);
1✔
2287
    tcph.th_seq = htonl(1);
1✔
2288
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2289
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2290
    p->payload_len = sizeof(request);
1✔
2291
    p->payload = request;
1✔
2292
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2293
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2294
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2295
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
2296
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2297
    FAIL_IF(f.alproto_tc != ALPROTO_HTTP1);
1✔
2298
    FAIL_IF((ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED));
1✔
2299
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2300
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2301
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2302
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2303
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2304

2305
    tcph.th_ack = htonl(1 + sizeof(request));
1✔
2306
    tcph.th_seq = htonl(328);
1✔
2307
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2308
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2309
    p->payload_len = 0;
1✔
2310
    p->payload = NULL;
1✔
2311
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2312
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2313
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2314
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
2315
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
2316
    FAIL_IF(f.alproto_tc != ALPROTO_HTTP1);
1✔
2317
    FAIL_IF((ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED));
1✔
2318
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2319
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2320
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2321
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2322
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2323

2324
    TEST_END;
1✔
2325
    PASS;
1✔
2326
}
1✔
2327

2328
/**
2329
 * \test GET -> DCERPC
2330
 */
2331
static int AppLayerTest07(void)
2332
{
1✔
2333
    TEST_START;
37✔
2334

2335
    /* full request */
2336
    uint8_t request[] = {
37✔
2337
        0x47, 0x45, 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
2338
        0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
37✔
2339
        0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
37✔
2340
        0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
37✔
2341
        0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
37✔
2342
        0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
37✔
2343
        0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
37✔
2344
        0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
37✔
2345
        0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
37✔
2346
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
37✔
2347
        0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
37✔
2348
    tcph.th_ack = htonl(1);
37✔
2349
    tcph.th_seq = htonl(1);
37✔
2350
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
2351
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
2352
    p->payload_len = sizeof(request);
37✔
2353
    p->payload = request;
37✔
2354
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
2355
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2356
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2357
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2358
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2359
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2360
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2361
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2362
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2363
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2364
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2365
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2366

2367
    /* full response - request ack */
2368
    uint8_t response[] = { 0x05, 0x00, 0x4d, 0x42, 0x00, 0x01, 0x2e, 0x31, 0x20, 0x32, 0x30, 0x30,
1✔
2369
        0x20, 0x4f, 0x4b, 0x0d, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46, 0x72, 0x69, 0x2c,
1✔
2370
        0x20, 0x32, 0x33, 0x20, 0x53, 0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20, 0x30, 0x36,
1✔
2371
        0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65, 0x72,
1✔
2372
        0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
2373
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69, 0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f,
1✔
2374
        0x32, 0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65,
1✔
2375
        0x64, 0x3a, 0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34, 0x20, 0x4e, 0x6f, 0x76, 0x20,
1✔
2376
        0x32, 0x30, 0x31, 0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a, 0x34, 0x36, 0x20, 0x47,
1✔
2377
        0x4d, 0x54, 0x0d, 0x0a, 0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61, 0x62, 0x38, 0x39,
1✔
2378
        0x36, 0x35, 0x2d, 0x32, 0x63, 0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61, 0x37, 0x66,
1✔
2379
        0x37, 0x66, 0x38, 0x30, 0x22, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x52,
1✔
2380
        0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20, 0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
2381
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20,
1✔
2382
        0x34, 0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
1✔
2383
        0x20, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
1✔
2384
        0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d,
1✔
2385
        0x6c, 0x0d, 0x0a, 0x58, 0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76, 0x6f, 0x69, 0x64,
1✔
2386
        0x20, 0x62, 0x72, 0x6f, 0x77, 0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d, 0x0a, 0x0d,
1✔
2387
        0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c, 0x68,
1✔
2388
        0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f, 0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
2389
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
2390
    tcph.th_ack = htonl(88);
1✔
2391
    tcph.th_seq = htonl(1);
1✔
2392
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2393
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2394
    p->payload_len = sizeof(response);
1✔
2395
    p->payload = response;
1✔
2396
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2397
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2398
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2399
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
2400
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
2401
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2402
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2403
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2404
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2405
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2406
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2407
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2408

2409
    /* response ack */
2410
    tcph.th_ack = htonl(328);
1✔
2411
    tcph.th_seq = htonl(88);
1✔
2412
    tcph.th_flags = TH_ACK;
1✔
2413
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2414
    p->payload_len = 0;
1✔
2415
    p->payload = NULL;
1✔
2416
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2417
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2418
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2419
    FAIL_IF(f.alproto != ALPROTO_HTTP1);
1✔
2420
    FAIL_IF(f.alproto_ts != ALPROTO_HTTP1);
1✔
2421
    FAIL_IF(f.alproto_tc != ALPROTO_DCERPC);
1✔
2422
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2423
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2424
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2425
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2426
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2427
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2428

2429
    TEST_END;
1✔
2430
    PASS;
1✔
2431
}
1✔
2432

2433
/**
2434
 * \test RUBBISH(TC - PM and PP NOT DONE) ->
2435
 *       RUBBISH(TC - PM and PP DONE) ->
2436
 *       RUBBISH(TS - PM and PP DONE)
2437
 */
2438
static int AppLayerTest09(void)
2439
{
1✔
2440
    TEST_START;
37✔
2441

2442
    /* full request */
2443
    uint8_t request1[] = {
37✔
2444
        0x47, 0x47, 0x49, 0x20, 0x2f, 0x69, 0x6e, 0x64 };
37✔
2445
    tcph.th_ack = htonl(1);
37✔
2446
    tcph.th_seq = htonl(1);
37✔
2447
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
2448
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
2449
    p->payload_len = sizeof(request1);
37✔
2450
    p->payload = request1;
37✔
2451
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
2452
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2453
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2454
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2455
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2456
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2457
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2458
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2459
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2460
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2461
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2462
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2463

2464
    /* response - request ack */
2465
    tcph.th_ack = htonl(9);
1✔
2466
    tcph.th_seq = htonl(1);
1✔
2467
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2468
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2469
    p->payload_len = 0;
1✔
2470
    p->payload = NULL;
1✔
2471
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2472
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2473
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2474
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2475
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2476
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2477
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2478
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2479
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2480
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2481
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2482
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2483

2484
    /* full request */
2485
    uint8_t request2[] = {
1✔
2486
        0x44, 0x44, 0x45, 0x20, 0x2f, 0x69, 0x6e, 0x64, 0xff };
1✔
2487
    tcph.th_ack = htonl(1);
1✔
2488
    tcph.th_seq = htonl(9);
1✔
2489
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2490
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2491
    p->payload_len = sizeof(request2);
1✔
2492
    p->payload = request2;
1✔
2493
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2494
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2495
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2496
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2497
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2498
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2499
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2500
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2501
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2502
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2503
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2504
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2505

2506
    /* full response - request ack */
2507
    uint8_t response[] = {
1✔
2508
        0x55, 0x74, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
1✔
2509
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
2510
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
2511
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
2512
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
2513
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
2514
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
2515
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
2516
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
2517
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
2518
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
2519
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
2520
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
2521
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
2522
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
2523
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
2524
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
2525
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
2526
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
2527
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
2528
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
2529
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
2530
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
2531
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
2532
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
2533
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
2534
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
2535
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
2536
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
2537
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
2538
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
2539
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
2540
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
2541
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
2542
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
2543
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
2544
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
2545
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
2546
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
2547
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
2548
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
2549
    tcph.th_ack = htonl(18);
1✔
2550
    tcph.th_seq = htonl(1);
1✔
2551
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2552
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2553
    p->payload_len = sizeof(response);
1✔
2554
    p->payload = response;
1✔
2555
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2556
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2557
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2558
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2559
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2560
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2561
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2562
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2563
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2564
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2565
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2566
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2567

2568
    /* response ack */
2569
    tcph.th_ack = htonl(328);
1✔
2570
    tcph.th_seq = htonl(18);
1✔
2571
    tcph.th_flags = TH_ACK;
1✔
2572
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2573
    p->payload_len = 0;
1✔
2574
    p->payload = NULL;
1✔
2575
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2576
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2577
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2578
    FAIL_IF(f.alproto != ALPROTO_FAILED);
1✔
2579
    FAIL_IF(f.alproto_ts != ALPROTO_FAILED);
1✔
2580
    FAIL_IF(f.alproto_tc != ALPROTO_FAILED);
1✔
2581
    FAIL_IF(!(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED));
1✔
2582
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2583
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2584
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2585
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2586
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2587

2588
    TEST_END;
1✔
2589
    PASS;
1✔
2590
}
1✔
2591

2592
/**
2593
 * \test RUBBISH(TC - PM and PP DONE) ->
2594
 *       RUBBISH(TS - PM and PP DONE)
2595
 */
2596
static int AppLayerTest10(void)
2597
{
1✔
2598
    TEST_START;
37✔
2599

2600
    /* full request */
2601
    uint8_t request1[] = {
37✔
2602
        0x47, 0x47, 0x49, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
2603
        0x47, 0x47, 0x49, 0x20, 0x2f, 0x69, 0x6e, 0x64, 0xff };
37✔
2604
    tcph.th_ack = htonl(1);
37✔
2605
    tcph.th_seq = htonl(1);
37✔
2606
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
2607
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
2608
    p->payload_len = sizeof(request1);
37✔
2609
    p->payload = request1;
37✔
2610
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
2611
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2612
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2613
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2614
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2615
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2616
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2617
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2618
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2619
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2620
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2621
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2622

2623
    /* response - request ack */
2624
    tcph.th_ack = htonl(18);
1✔
2625
    tcph.th_seq = htonl(1);
1✔
2626
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2627
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2628
    p->payload_len = 0;
1✔
2629
    p->payload = NULL;
1✔
2630
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2631
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2632
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2633
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2634
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2635
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2636
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2637
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2638
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2639
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2640
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2641
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2642

2643
    /* full response - request ack */
2644
    uint8_t response[] = {
1✔
2645
        0x55, 0x74, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
1✔
2646
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
2647
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
2648
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
2649
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
2650
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
2651
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
2652
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
2653
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
2654
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
2655
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
2656
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
2657
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
2658
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
2659
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
2660
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
2661
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
2662
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
2663
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
2664
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
2665
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
2666
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
2667
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
2668
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
2669
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
2670
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
2671
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
2672
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
2673
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
2674
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
2675
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
2676
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
2677
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
2678
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
2679
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
2680
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
2681
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
2682
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
2683
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
2684
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
2685
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
2686
    tcph.th_ack = htonl(18);
1✔
2687
    tcph.th_seq = htonl(1);
1✔
2688
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2689
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2690
    p->payload_len = sizeof(response);
1✔
2691
    p->payload = response;
1✔
2692
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2693
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2694
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2695
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2696
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2697
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2698
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2699
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2700
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2701
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2702
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2703
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2704

2705
    /* response ack */
2706
    tcph.th_ack = htonl(328);
1✔
2707
    tcph.th_seq = htonl(18);
1✔
2708
    tcph.th_flags = TH_ACK;
1✔
2709
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2710
    p->payload_len = 0;
1✔
2711
    p->payload = NULL;
1✔
2712
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2713
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2714
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2715
    FAIL_IF(f.alproto != ALPROTO_FAILED);
1✔
2716
    FAIL_IF(f.alproto_ts != ALPROTO_FAILED);
1✔
2717
    FAIL_IF(f.alproto_tc != ALPROTO_FAILED);
1✔
2718
    FAIL_IF(!(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED));
1✔
2719
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2720
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2721
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2722
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2723
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2724

2725
    TEST_END;
1✔
2726
    PASS;
1✔
2727
}
1✔
2728

2729
/**
2730
 * \test RUBBISH(TC - PM and PP DONE) ->
2731
 *       RUBBISH(TS - PM and PP NOT DONE) ->
2732
 *       RUBBISH(TS - PM and PP DONE)
2733
 */
2734
static int AppLayerTest11(void)
2735
{
1✔
2736
    TEST_START;
37✔
2737

2738
    /* full request */
2739
    uint8_t request1[] = {
37✔
2740
        0x47, 0x47, 0x49, 0x20, 0x2f, 0x69, 0x6e, 0x64,
37✔
2741
        0x47, 0x47, 0x49, 0x20, 0x2f, 0x69, 0x6e, 0x64, 0xff };
37✔
2742
    tcph.th_ack = htonl(1);
37✔
2743
    tcph.th_seq = htonl(1);
37✔
2744
    tcph.th_flags = TH_PUSH | TH_ACK;
37✔
2745
    p->flowflags = FLOW_PKT_TOSERVER;
37✔
2746
    p->payload_len = sizeof(request1);
37✔
2747
    p->payload = request1;
37✔
2748
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
37✔
2749
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2750
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2751
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2752
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2753
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2754
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2755
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2756
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2757
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2758
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2759
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2760

2761
    /* response - request ack */
2762
    tcph.th_ack = htonl(18);
1✔
2763
    tcph.th_seq = htonl(1);
1✔
2764
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2765
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2766
    p->payload_len = 0;
1✔
2767
    p->payload = NULL;
1✔
2768
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2769
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2770
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2771
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2772
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2773
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2774
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2775
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2776
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2777
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2778
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2779
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2780

2781
    /* full response - request ack */
2782
    uint8_t response1[] = {
1✔
2783
        0x55, 0x74, 0x54, 0x50, };
1✔
2784
    tcph.th_ack = htonl(18);
1✔
2785
    tcph.th_seq = htonl(1);
1✔
2786
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2787
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2788
    p->payload_len = sizeof(response1);
1✔
2789
    p->payload = response1;
1✔
2790
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2791
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2792
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2793
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2794
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2795
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2796
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2797
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2798
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2799
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2800
    FAIL_IF(FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2801
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2802

2803
    /* response ack from request */
2804
    tcph.th_ack = htonl(5);
1✔
2805
    tcph.th_seq = htonl(18);
1✔
2806
    tcph.th_flags = TH_ACK;
1✔
2807
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2808
    p->payload_len = 0;
1✔
2809
    p->payload = NULL;
1✔
2810
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2811
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2812
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2813
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2814
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2815
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2816
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2817
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2818
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2819
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2820
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2821
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2822

2823
    uint8_t response2[] = {
1✔
2824
        0x2f, 0x31, 0x2e, 0x31,
1✔
2825
        0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
1✔
2826
        0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
1✔
2827
        0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
1✔
2828
        0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
1✔
2829
        0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
1✔
2830
        0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
1✔
2831
        0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
1✔
2832
        0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
1✔
2833
        0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
1✔
2834
        0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
1✔
2835
        0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
1✔
2836
        0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
1✔
2837
        0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
1✔
2838
        0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
1✔
2839
        0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
1✔
2840
        0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
1✔
2841
        0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
1✔
2842
        0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
1✔
2843
        0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
1✔
2844
        0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
1✔
2845
        0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
1✔
2846
        0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
1✔
2847
        0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
1✔
2848
        0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
1✔
2849
        0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
1✔
2850
        0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
1✔
2851
        0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
1✔
2852
        0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
1✔
2853
        0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
1✔
2854
        0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
1✔
2855
        0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
1✔
2856
        0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
1✔
2857
        0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
1✔
2858
        0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
1✔
2859
        0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
1✔
2860
        0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
1✔
2861
        0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
1✔
2862
        0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
1✔
2863
        0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
1✔
2864
        0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
1✔
2865
    tcph.th_ack = htonl(18);
1✔
2866
    tcph.th_seq = htonl(5);
1✔
2867
    tcph.th_flags = TH_PUSH | TH_ACK;
1✔
2868
    p->flowflags = FLOW_PKT_TOCLIENT;
1✔
2869
    p->payload_len = sizeof(response2);
1✔
2870
    p->payload = response2;
1✔
2871
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2872
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2873
    FAIL_IF(StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2874
    FAIL_IF(f.alproto != ALPROTO_UNKNOWN);
1✔
2875
    FAIL_IF(f.alproto_ts != ALPROTO_UNKNOWN);
1✔
2876
    FAIL_IF(f.alproto_tc != ALPROTO_UNKNOWN);
1✔
2877
    FAIL_IF(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED);
1✔
2878
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2879
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2880
    FAIL_IF(FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2881
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2882
    FAIL_IF(ssn->data_first_seen_dir != STREAM_TOSERVER);
1✔
2883

2884
    /* response ack from request */
2885
    tcph.th_ack = htonl(328);
1✔
2886
    tcph.th_seq = htonl(18);
1✔
2887
    tcph.th_flags = TH_ACK;
1✔
2888
    p->flowflags = FLOW_PKT_TOSERVER;
1✔
2889
    p->payload_len = 0;
1✔
2890
    p->payload = NULL;
1✔
2891
    FAIL_IF(StreamTcpPacket(&tv, p, stt, &pq) == -1);
1✔
2892
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->server));
1✔
2893
    FAIL_IF(!StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(&ssn->client));
1✔
2894
    FAIL_IF(f.alproto != ALPROTO_FAILED);
1✔
2895
    FAIL_IF(f.alproto_ts != ALPROTO_FAILED);
1✔
2896
    FAIL_IF(f.alproto_tc != ALPROTO_FAILED);
1✔
2897
    FAIL_IF(!(ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED));
1✔
2898
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOSERVER));
1✔
2899
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOSERVER));
1✔
2900
    FAIL_IF(!FLOW_IS_PM_DONE(&f, STREAM_TOCLIENT));
1✔
2901
    FAIL_IF(!FLOW_IS_PP_DONE(&f, STREAM_TOCLIENT));
1✔
2902
    FAIL_IF(ssn->data_first_seen_dir != APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER);
1✔
2903

2904
    TEST_END;
1✔
2905
    PASS;
1✔
2906
}
1✔
2907

2908
void AppLayerUnittestsRegister(void)
2909
{
1✔
2910
    SCEnter();
1✔
2911

2912
    UtRegisterTest("AppLayerTest01", AppLayerTest01);
1✔
2913
    UtRegisterTest("AppLayerTest02", AppLayerTest02);
1✔
2914
    UtRegisterTest("AppLayerTest03", AppLayerTest03);
1✔
2915
    UtRegisterTest("AppLayerTest04", AppLayerTest04);
1✔
2916
    UtRegisterTest("AppLayerTest05", AppLayerTest05);
1✔
2917
    UtRegisterTest("AppLayerTest06", AppLayerTest06);
1✔
2918
    UtRegisterTest("AppLayerTest07", AppLayerTest07);
1✔
2919
    UtRegisterTest("AppLayerTest09", AppLayerTest09);
1✔
2920
    UtRegisterTest("AppLayerTest10", AppLayerTest10);
1✔
2921
    UtRegisterTest("AppLayerTest11", AppLayerTest11);
1✔
2922

2923
    SCReturn;
1✔
2924
}
1✔
2925

2926
#endif /* UNITTESTS */
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