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

OISF / suricata / 22731000775

05 Mar 2026 06:16PM UTC coverage: 79.035% (-0.2%) from 79.283%
22731000775

Pull #14956

github

web-flow
Merge 0d6798ed1 into 7e97dfd52
Pull Request #14956: Draft: windows-pe keyword

1312 of 1773 new or added lines in 11 files covered. (74.0%)

684 existing lines in 56 files now uncovered.

266003 of 336563 relevant lines covered (79.04%)

3075360.67 hits per line

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

96.7
/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,056✔
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) {
29,289✔
140
    const TcpStream *stream = (direction & STREAM_TOSERVER) ? &ssn->client : &ssn->server;
29,289✔
141
    return ((stream->flags & STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_COMPLETED) ||
29,289✔
142
            (FLOW_IS_PM_DONE(f, direction) && FLOW_IS_PP_DONE(f, direction)));
29,296✔
143
}
29,289✔
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
{
52,437✔
151
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].counter_id;
52,437✔
152
    if (likely(tv && id.id > 0)) {
52,437✔
153
        StatsCounterIncr(&tv->stats, id);
52,422✔
154
    }
52,422✔
155
}
52,437✔
156

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

165
void AppLayerIncGapErrorCounter(ThreadVars *tv, Flow *f)
166
{
1,275✔
167
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].gap_error_id;
1,275✔
168
    if (likely(tv && id.id > 0)) {
1,275✔
169
        StatsCounterIncr(&tv->stats, id);
1,275✔
170
    }
1,275✔
171
}
1,275✔
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,291✔
183
    const StatsCounterId id = applayer_counters[f->alproto][f->protomap].parser_error_id;
5,291✔
184
    if (likely(tv && id.id > 0)) {
5,291✔
185
        StatsCounterIncr(&tv->stats, id);
4,657✔
186
    }
4,657✔
187
}
5,291✔
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
{
5,932✔
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];
5,932✔
205
    /* for the summary values */
206
    StatsCounterId g_id = eps_error_summary.eps_id[policy];
5,932✔
207

208
    if (likely(id.id > 0)) {
5,932✔
209
        StatsCounterIncr(&tv->stats, id);
×
210
    }
×
211
    if (likely(g_id.id > 0)) {
5,932✔
212
        StatsCounterIncr(&tv->stats, g_id);
2✔
213
    }
2✔
214
}
5,932✔
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
{
116,418✔
224
    if (p->proto != IPPROTO_TCP || EngineModeIsIPS()) {
116,418✔
225
        if (flags & STREAM_TOSERVER) {
58,834✔
226
            if (p->flowflags & FLOW_PKT_TOSERVER) {
29,435✔
227
                p->flags |= PKT_PROTO_DETECT_TS_DONE;
21,840✔
228
                f->flags |= FLOW_PROTO_DETECT_TS_DONE;
21,840✔
229
            } else {
21,845✔
230
                f->flags |= FLOW_PROTO_DETECT_TS_DONE;
7,595✔
231
            }
7,595✔
232
        } else {
29,435✔
233
            if (p->flowflags & FLOW_PKT_TOCLIENT) {
29,399✔
234
                p->flags |= PKT_PROTO_DETECT_TC_DONE;
7,710✔
235
                f->flags |= FLOW_PROTO_DETECT_TC_DONE;
7,710✔
236
            } else {
21,689✔
237
                f->flags |= FLOW_PROTO_DETECT_TC_DONE;
21,689✔
238
            }
21,689✔
239
        }
29,399✔
240
    } else {
62,329✔
241
        if (flags & STREAM_TOSERVER) {
57,584✔
242
            f->flags |= FLOW_PROTO_DETECT_TS_DONE;
31,452✔
243
        } else {
31,454✔
244
            f->flags |= FLOW_PROTO_DETECT_TC_DONE;
26,132✔
245
        }
26,132✔
246
    }
57,584✔
247
}
116,418✔
248

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

260
    if (f->alproto_tc != ALPROTO_FAILED) {
1,890✔
261
        if (f->alproto_tc == ALPROTO_UNKNOWN) {
1,890✔
262
            f->alproto_tc = ALPROTO_FAILED;
1,653✔
263
        }
1,653✔
264
        FlagPacketFlow(p, f, STREAM_TOCLIENT);
1,890✔
265
    }
1,890✔
266
    if (f->alproto_ts != ALPROTO_FAILED) {
1,890✔
267
        if (f->alproto_ts == ALPROTO_UNKNOWN) {
1,890✔
268
            f->alproto_ts = ALPROTO_FAILED;
1,872✔
269
        }
1,872✔
270
        FlagPacketFlow(p, f, STREAM_TOSERVER);
1,890✔
271
    }
1,890✔
272
    SCLogDebug("disabled app layer for flow %p alproto %u ts %u tc %u",
1,890✔
273
            f, f->alproto, f->alproto_ts, f->alproto_tc);
1,890✔
274
}
1,890✔
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
{
25,666✔
298
    if (ssn->state < TCP_ESTABLISHED) {
25,666✔
299
        SCLogDebug("skip as long as TCP is not ESTABLISHED (TCP fast open)");
405✔
300
        return;
405✔
301
    }
405✔
302

303
    const uint32_t size_ts = StreamDataAvailableForProtoDetect(&ssn->client);
25,261✔
304
    const uint32_t size_tc = StreamDataAvailableForProtoDetect(&ssn->server);
25,261✔
305
    SCLogDebug("size_ts %" PRIu32 ", size_tc %" PRIu32, size_ts, size_tc);
25,261✔
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 =
25,261✔
310
            MAX(100000, MIN(ssn->client.window, stream_config.reassembly_depth));
25,261✔
311
    const uint32_t size_ts_limit =
25,261✔
312
            MAX(100000, MIN(ssn->server.window, stream_config.reassembly_depth));
25,261✔
313

314
    if (ProtoDetectDone(f, ssn, STREAM_TOSERVER) &&
25,261✔
315
        ProtoDetectDone(f, ssn, STREAM_TOCLIENT))
25,261✔
316
    {
145✔
317
        goto failure;
145✔
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) {
25,116✔
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) &&
25,116✔
326
               size_ts > size_ts_limit && size_tc == 0) {
25,116✔
327
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
1✔
328
        goto failure;
1✔
329

330
    } else if (FLOW_IS_PM_DONE(f, STREAM_TOCLIENT) && FLOW_IS_PP_DONE(f, STREAM_TOCLIENT) &&
25,115✔
331
               size_tc > size_tc_limit && size_ts == 0) {
25,115✔
332
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
×
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) &&
25,115✔
339
               !(FLOW_IS_PM_DONE(f, STREAM_TOSERVER)) && FLOW_IS_PM_DONE(f, STREAM_TOCLIENT) &&
25,115✔
340
               FLOW_IS_PP_DONE(f, STREAM_TOCLIENT)) {
25,115✔
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) &&
25,115✔
348
               !(FLOW_IS_PM_DONE(f, STREAM_TOCLIENT)) && FLOW_IS_PM_DONE(f, STREAM_TOSERVER) &&
25,115✔
349
               FLOW_IS_PP_DONE(f, STREAM_TOSERVER)) {
25,115✔
350
        SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_PROTO_DETECTION_SKIPPED);
×
351
        goto failure;
×
352
    }
×
353
    return;
25,115✔
354

355
failure:
25,115✔
356
    DisableAppLayer(tv, f, p);
146✔
357
}
146✔
358

359
static int TCPProtoDetectTriggerOpposingSide(ThreadVars *tv, TcpReassemblyThreadCtx *ra_ctx,
360
        Packet *p, TcpSession *ssn, const TcpStream *stream)
361
{
1,159✔
362
    TcpStream *opposing_stream = NULL;
1,159✔
363
    if (stream == &ssn->client) {
1,159✔
364
        opposing_stream = &ssn->server;
411✔
365
    } else {
753✔
366
        opposing_stream = &ssn->client;
748✔
367
    }
748✔
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,159✔
372
        SCLogDebug("opposing dir has STREAMTCP_STREAM_FLAG_NOREASSEMBLY set");
11✔
373
        return -1;
11✔
374
    }
11✔
375

376
    enum StreamUpdateDir dir = StreamTcpInlineMode() ?
1,148✔
377
                                                UPDATE_DIR_OPPOSING :
12✔
378
                                                UPDATE_DIR_PACKET;
1,148✔
379
    int ret = StreamTcpReassembleAppLayer(tv, ra_ctx, ssn,
1,148✔
380
            opposing_stream, p, dir);
1,148✔
381
    return ret;
1,148✔
382
}
1,159✔
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
        uint8_t *data, uint32_t data_len, uint8_t flags, enum StreamUpdateDir app_update_dir)
393
{
85,704✔
394
    AppProto *alproto;
85,704✔
395
    AppProto *alproto_otherdir;
85,704✔
396
    uint8_t direction = (flags & STREAM_TOSERVER) ? 0 : 1;
85,704✔
397

398
    if (flags & STREAM_TOSERVER) {
85,704✔
399
        alproto = &f->alproto_ts;
44,358✔
400
        alproto_otherdir = &f->alproto_tc;
44,358✔
401
    } else {
44,358✔
402
        alproto = &f->alproto_tc;
41,346✔
403
        alproto_otherdir = &f->alproto_ts;
41,346✔
404
    }
41,346✔
405

406
    SCLogDebug("Stream initializer (len %" PRIu32 ")", data_len);
85,704✔
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;
85,704✔
418
    DEBUG_VALIDATE_BUG_ON(data == NULL && data_len > 0);
85,704✔
419
    PACKET_PROFILING_APP_PD_START(app_tctx);
85,704✔
420
    *alproto = AppLayerProtoDetectGetProto(app_tctx->alpd_tctx,
85,704✔
421
            f, data, data_len,
85,704✔
422
            IPPROTO_TCP, flags, &reverse_flow);
85,704✔
423
    PACKET_PROFILING_APP_PD_END(app_tctx);
85,704✔
424
    SCLogDebug("alproto %u rev %s", *alproto, reverse_flow ? "true" : "false");
85,704✔
425

426
    if (*alproto != ALPROTO_UNKNOWN) {
85,704✔
427
        if (*alproto_otherdir != ALPROTO_UNKNOWN && *alproto_otherdir != *alproto) {
53,273✔
428
            SCAppLayerDecoderEventsSetEventRaw(
45✔
429
                    &p->app_layer_events, APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS);
45✔
430

431
            if (ssn->data_first_seen_dir == APP_LAYER_DATA_ALREADY_SENT_TO_APP_LAYER) {
45✔
432
                /* if we already invoked the parser, we go with that proto */
433
                f->alproto = *alproto_otherdir;
45✔
434
            } else {
45✔
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 {
53,228✔
443
            f->alproto = *alproto;
53,228✔
444
        }
53,228✔
445

446
        StreamTcpSetStreamFlagAppProtoDetectionCompleted(*stream);
53,273✔
447
        TcpSessionSetReassemblyDepth(ssn,
53,273✔
448
                AppLayerParserGetStreamDepth(f));
53,273✔
449
        FlagPacketFlow(p, f, flags);
53,273✔
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 &&
53,273✔
455
                ((ssn->flags & (STREAMTCP_FLAG_MIDSTREAM | STREAMTCP_FLAG_MIDSTREAM_SYNACK)) ==
53,273✔
456
                        STREAMTCP_FLAG_MIDSTREAM)) {
2,011✔
457
            /* but only if we didn't already detect it on the other side. */
458
            if (*alproto_otherdir == ALPROTO_UNKNOWN) {
1,532✔
459
                SCLogDebug("reversing flow after proto detect told us so");
1,500✔
460
                PacketSwap(p);
1,500✔
461
                FlowSwap(f);
1,500✔
462
                // Will reset signature groups in DetectRunSetup
463
                f->de_ctx_version = UINT32_MAX;
1,500✔
464
                SWAP_FLAGS(flags, STREAM_TOSERVER, STREAM_TOCLIENT);
1,500✔
465
                if (*stream == &ssn->client) {
1,500✔
466
                    *stream = &ssn->server;
1,378✔
467
                } else {
1,378✔
468
                    *stream = &ssn->client;
122✔
469
                }
122✔
470
                direction = 1 - direction;
1,500✔
471
            } else {
1,500✔
472
                // TODO event, error?
473
            }
32✔
474
        }
1,532✔
475

476
        /* account flow if we have both sides */
477
        if (*alproto_otherdir != ALPROTO_UNKNOWN) {
53,273✔
478
            AppLayerIncFlowCounter(tv, f);
23,809✔
479
        }
23,809✔
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)) &&
53,273✔
489
                !(flags & ssn->data_first_seen_dir))
53,273✔
490
        {
1,159✔
491
            SCLogDebug("protocol %s needs first data in other direction",
1,159✔
492
                    AppProtoToString(*alproto));
1,159✔
493

494
            if (TCPProtoDetectTriggerOpposingSide(tv, ra_ctx,
1,159✔
495
                        p, ssn, *stream) != 0)
1,159✔
496
            {
11✔
497
                goto detect_error;
11✔
498
            }
11✔
499
            if (FlowChangeProto(f)) {
1,148✔
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,148✔
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) {
53,262✔
527
            uint8_t first_data_dir;
28,674✔
528
            first_data_dir = AppLayerParserGetFirstDataDir(f->proto, f->alproto);
28,674✔
529

530
            if (first_data_dir && !(first_data_dir & ssn->data_first_seen_dir)) {
28,674✔
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)) {
28,459✔
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
        }
28,459✔
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;
53,034✔
554

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

607
        if (*alproto_otherdir != ALPROTO_UNKNOWN) {
31,419✔
608
            uint8_t first_data_dir;
4,723✔
609
            first_data_dir = AppLayerParserGetFirstDataDir(f->proto, *alproto_otherdir);
4,723✔
610

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

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

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

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

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

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

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

716
    DEBUG_ASSERT_FLOW_LOCKED(f);
692,569✔
717
    DEBUG_VALIDATE_BUG_ON(data_len > (uint32_t)INT_MAX);
692,569✔
718

719
    AppLayerThreadCtx *app_tctx = ra_ctx->app_tctx;
692,569✔
720
    AppProto alproto;
692,569✔
721
    int r = 0;
692,569✔
722

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

729
    const uint8_t direction = (flags & STREAM_TOSERVER) ? 0 : 1;
692,562✔
730

731
    if (flags & STREAM_TOSERVER) {
692,562✔
732
        alproto = f->alproto_ts;
365,370✔
733
    } else {
373,447✔
734
        alproto = f->alproto_tc;
327,192✔
735
    }
327,192✔
736

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

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

815
        if (f->alproto_expect != ALPROTO_UNKNOWN && f->alproto != ALPROTO_UNKNOWN &&
3,059✔
816
                f->alproto != f->alproto_expect) {
3,059✔
817
            SCAppLayerDecoderEventsSetEventRaw(&p->app_layer_events, APPLAYER_UNEXPECTED_PROTOCOL);
568✔
818

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

856
    goto end;
676,851✔
857
 failure:
676,851✔
858
    r = -1;
5,530✔
859
 end:
690,845✔
860
    SCReturnInt(r);
690,845✔
861
}
5,530✔
862

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

881
    if (f->alproto_ts == ALPROTO_FAILED && f->alproto_tc == ALPROTO_FAILED) {
96,988✔
882
        SCReturnInt(0);
2,941✔
883
    }
2,941✔
884

885
    int r = 0;
94,047✔
886
    uint8_t flags = 0;
94,047✔
887
    if (p->flowflags & FLOW_PKT_TOSERVER) {
94,047✔
888
        flags |= STREAM_TOSERVER;
77,465✔
889
        alproto = &f->alproto_ts;
77,465✔
890
        alproto_otherdir = &f->alproto_tc;
77,465✔
891
    } else {
77,465✔
892
        flags |= STREAM_TOCLIENT;
16,582✔
893
        alproto = &f->alproto_tc;
16,582✔
894
        alproto_otherdir = &f->alproto_ts;
16,582✔
895
    }
16,582✔
896

897
    AppLayerProfilingReset(tctx);
94,047✔
898

899
    /* if the protocol is still unknown, run detection */
900
    if (*alproto == ALPROTO_UNKNOWN) {
94,047✔
901
        SCLogDebug("Detecting AL proto on udp mesg (len %" PRIu32 ")",
29,255✔
902
                   p->payload_len);
29,255✔
903

904
        bool reverse_flow = false;
29,255✔
905
        PACKET_PROFILING_APP_PD_START(tctx);
29,255✔
906
        *alproto = AppLayerProtoDetectGetProto(
29,255✔
907
                tctx->alpd_tctx, f, p->payload, p->payload_len, IPPROTO_UDP, flags, &reverse_flow);
29,255✔
908
        PACKET_PROFILING_APP_PD_END(tctx);
29,255✔
909

910
        switch (*alproto) {
29,255✔
911
            case ALPROTO_UNKNOWN:
12,667✔
912
                if (*alproto_otherdir != ALPROTO_UNKNOWN) {
12,667✔
913
                    // Use recognized side
914
                    f->alproto = *alproto_otherdir;
2,480✔
915
                    // do not keep ALPROTO_UNKNOWN for this side so as not to loop
916
                    *alproto = *alproto_otherdir;
2,480✔
917
                    if (*alproto_otherdir == ALPROTO_FAILED) {
2,480✔
918
                        SCLogDebug("ALPROTO_UNKNOWN flow %p", f);
1,892✔
919
                    }
1,892✔
920
                } else {
10,187✔
921
                    // First side of protocol is unknown
922
                    *alproto = ALPROTO_FAILED;
10,187✔
923
                }
10,187✔
924
                break;
12,667✔
925
            case ALPROTO_FAILED:
×
926
                if (*alproto_otherdir != ALPROTO_UNKNOWN) {
×
927
                    // Use recognized side
928
                    f->alproto = *alproto_otherdir;
×
929
                    if (*alproto_otherdir == ALPROTO_FAILED) {
×
930
                        SCLogDebug("ALPROTO_UNKNOWN flow %p", f);
×
931
                    }
×
932
                }
×
933
                // else wait for second side of protocol
934
                break;
×
935
            default:
16,587✔
936
                if (*alproto_otherdir != ALPROTO_UNKNOWN && *alproto_otherdir != ALPROTO_FAILED) {
16,587✔
937
                    if (*alproto_otherdir != *alproto) {
4,835✔
938
                        SCAppLayerDecoderEventsSetEventRaw(
1✔
939
                                &p->app_layer_events, APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS);
1✔
940
                        // data already sent to parser, we cannot change the protocol to use the one
941
                        // of the server
942
                    }
1✔
943
                } else {
11,752✔
944
                    f->alproto = *alproto;
11,752✔
945
                }
11,752✔
946
        }
29,255✔
947
        if (*alproto_otherdir == ALPROTO_UNKNOWN) {
29,253✔
948
            if (f->alproto == ALPROTO_UNKNOWN) {
21,803✔
949
                // so as to increase stat about .app_layer.flow.failed_udp
950
                f->alproto = ALPROTO_FAILED;
10,186✔
951
            }
10,186✔
952
            // If the other side is unknown, this is the first packet of the flow
953
            AppLayerIncFlowCounter(tv, f);
21,803✔
954
        }
21,803✔
955

956
        // parse the data if we recognized one protocol
957
        if (f->alproto != ALPROTO_UNKNOWN && f->alproto != ALPROTO_FAILED) {
29,254✔
958
            if (reverse_flow) {
17,175✔
959
                SCLogDebug("reversing flow after proto detect told us so");
161✔
960
                PacketSwap(p);
161✔
961
                FlowSwap(f);
161✔
962
                SWAP_FLAGS(flags, STREAM_TOSERVER, STREAM_TOCLIENT);
161✔
963
            }
161✔
964

965
            PACKET_PROFILING_APP_START(tctx, f->alproto);
17,175✔
966
            r = AppLayerParserParse(tv, tctx->alp_tctx, f, f->alproto,
17,175✔
967
                                    flags, p->payload, p->payload_len);
17,175✔
968
            PACKET_PROFILING_APP_END(tctx);
17,175✔
969
            if (f->flags & FLOW_NOPAYLOAD_INSPECTION &&
17,175✔
970
                    SCAppLayerParserStateIssetFlag(f->alparser, APP_LAYER_PARSER_BYPASS_READY)) {
17,175✔
971
                PacketBypassCallback(p);
1✔
972
            }
1✔
973
            p->app_update_direction = (uint8_t)UPDATE_DIR_PACKET;
17,175✔
974
        }
17,175✔
975
        PACKET_PROFILING_APP_STORE(tctx, p);
29,253✔
976
        /* we do only inspection in one direction, so flag both
977
         * sides as done here */
978
        FlagPacketFlow(p, f, STREAM_TOSERVER);
29,253✔
979
        FlagPacketFlow(p, f, STREAM_TOCLIENT);
29,253✔
980
    } else {
64,792✔
981
        SCLogDebug("data (len %" PRIu32 " ), alproto "
64,792✔
982
                   "%"PRIu16" (flow %p)", p->payload_len, f->alproto, f);
64,792✔
983

984
        /* run the parser */
985
        PACKET_PROFILING_APP_START(tctx, f->alproto);
64,792✔
986
        r = AppLayerParserParse(tv, tctx->alp_tctx, f, f->alproto,
64,792✔
987
                flags, p->payload, p->payload_len);
64,792✔
988
        if (f->flags & FLOW_NOPAYLOAD_INSPECTION &&
64,792✔
989
                SCAppLayerParserStateIssetFlag(f->alparser, APP_LAYER_PARSER_BYPASS_READY)) {
64,792✔
990
            PacketBypassCallback(p);
×
991
        }
×
992
        PACKET_PROFILING_APP_END(tctx);
64,792✔
993
        PACKET_PROFILING_APP_STORE(tctx, p);
64,792✔
994
        p->app_update_direction = (uint8_t)UPDATE_DIR_PACKET;
64,792✔
995
    }
64,792✔
996
    if (r < 0) {
94,045✔
997
        ExceptionPolicyApply(p, g_applayerparser_error_policy, PKT_DROP_REASON_APPLAYER_ERROR);
2,887✔
998
        AppLayerIncrErrorExcPolicyCounter(tv, f, g_applayerparser_error_policy);
2,887✔
999
        SCReturnInt(-1);
2,887✔
1000
    }
2,887✔
1001

1002
    SCReturnInt(r);
94,045✔
1003
}
94,045✔
1004

1005
/***** Utility *****/
1006

1007
AppProto AppLayerGetProtoByName(const char *alproto_name)
1008
{
1,185,599✔
1009
    SCEnter();
1,185,599✔
1010
    AppProto r = AppLayerProtoDetectGetProtoByName(alproto_name);
1,185,599✔
1011
    SCReturnCT(r, "AppProto");
1,185,599✔
1012
}
1,185,599✔
1013

1014
const char *AppLayerGetProtoName(AppProto alproto)
1015
{
155,015✔
1016
    SCEnter();
155,015✔
1017
    const char * r = AppLayerProtoDetectGetProtoName(alproto);
155,015✔
1018
    SCReturnCT(r, "char *");
155,015✔
1019
}
155,015✔
1020

1021
void AppLayerListSupportedProtocols(void)
1022
{
1✔
1023
    SCEnter();
1✔
1024

1025
    AppProto alproto;
1✔
1026
    AppProto alprotos[g_alproto_max];
1✔
1027

1028
    AppLayerProtoDetectSupportedAppProtocols(alprotos);
1✔
1029

1030
    printf("=========Supported App Layer Protocols=========\n");
1✔
1031
    for (alproto = 0; alproto < g_alproto_max; alproto++) {
41✔
1032
        if (alprotos[alproto] == 1)
40✔
1033
            printf("%s\n", AppLayerGetProtoName(alproto));
32✔
1034
    }
40✔
1035

1036
    SCReturn;
1✔
1037
}
1✔
1038

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

1083
int AppLayerSetup(void)
1084
{
2,207✔
1085
    SCEnter();
2,207✔
1086

1087
    AppLayerNamesSetup();
2,207✔
1088
    AppLayerProtoDetectSetup();
2,207✔
1089
    AppLayerParserSetup();
2,207✔
1090

1091
    AppLayerParserRegisterProtocolParsers();
2,207✔
1092
    AppLayerProtoDetectPrepareState();
2,207✔
1093

1094
    AppLayerSetupCounters();
2,207✔
1095
    FrameConfigInit();
2,207✔
1096

1097
    SCReturnInt(0);
2,207✔
1098
}
2,207✔
1099

1100
int AppLayerDeSetup(void)
1101
{
2,120✔
1102
    SCEnter();
2,120✔
1103

1104
    AppLayerProtoDetectDeSetup();
2,120✔
1105
    AppLayerParserDeSetup();
2,120✔
1106

1107
    AppLayerDeSetupCounters();
2,120✔
1108
    FrameConfigDeInit();
2,120✔
1109

1110
    SCReturnInt(0);
2,120✔
1111
}
2,120✔
1112

1113
AppLayerThreadCtx *AppLayerGetCtxThread(void)
1114
{
18,134✔
1115
    SCEnter();
18,134✔
1116

1117
    AppLayerThreadCtx *app_tctx = SCCalloc(1, sizeof(*app_tctx));
18,134✔
1118
    if (app_tctx == NULL)
18,134✔
1119
        goto error;
×
1120

1121
    if ((app_tctx->alpd_tctx = AppLayerProtoDetectGetCtxThread()) == NULL)
18,134✔
1122
        goto error;
×
1123
    if ((app_tctx->alp_tctx = AppLayerParserThreadCtxAlloc()) == NULL)
18,134✔
1124
        goto error;
×
1125

1126
    goto done;
18,134✔
1127
 error:
18,134✔
1128
    AppLayerDestroyCtxThread(app_tctx);
×
1129
    app_tctx = NULL;
×
1130
 done:
18,134✔
1131
    SCReturnPtr(app_tctx, "void *");
18,134✔
1132
}
×
1133

1134
void AppLayerDestroyCtxThread(AppLayerThreadCtx *app_tctx)
1135
{
18,130✔
1136
    SCEnter();
18,130✔
1137

1138
    if (app_tctx == NULL)
18,130✔
1139
        SCReturn;
×
1140

1141
    if (app_tctx->alpd_tctx != NULL)
18,130✔
1142
        AppLayerProtoDetectDestroyCtxThread(app_tctx->alpd_tctx);
18,130✔
1143
    if (app_tctx->alp_tctx != NULL)
18,130✔
1144
        AppLayerParserThreadCtxFree(app_tctx->alp_tctx);
18,130✔
1145
    SCFree(app_tctx);
18,130✔
1146

1147
    SCReturn;
18,130✔
1148
}
18,130✔
1149

1150
#ifdef PROFILING
1151
void AppLayerProfilingResetInternal(AppLayerThreadCtx *app_tctx)
1152
{
1153
    PACKET_PROFILING_APP_RESET(app_tctx);
1154
}
1155

1156
void AppLayerProfilingStoreInternal(AppLayerThreadCtx *app_tctx, Packet *p)
1157
{
1158
    PACKET_PROFILING_APP_STORE(app_tctx, p);
1159
}
1160
#endif
1161

1162
/** \brief HACK to work around our broken unix manager (re)init loop
1163
 */
1164
void AppLayerRegisterGlobalCounters(void)
1165
{
2,183✔
1166
    StatsRegisterGlobalCounter("http.memuse", HTPMemuseGlobalCounter);
2,183✔
1167
    StatsRegisterGlobalCounter("http.memcap", HTPMemcapGlobalCounter);
2,183✔
1168
    StatsRegisterGlobalCounter("ftp.memuse", FTPMemuseGlobalCounter);
2,183✔
1169
    StatsRegisterGlobalCounter("ftp.memcap", FTPMemcapGlobalCounter);
2,183✔
1170
    StatsRegisterGlobalCounter("app_layer.expectations", ExpectationGetCounter);
2,183✔
1171
    StatsRegisterGlobalCounter("http.byterange.memuse", HTPByteRangeMemuseGlobalCounter);
2,183✔
1172
    StatsRegisterGlobalCounter("http.byterange.memcap", HTPByteRangeMemcapGlobalCounter);
2,183✔
1173
    StatsRegisterGlobalCounter("ippair.memuse", IPPairGetMemuse);
2,183✔
1174
    StatsRegisterGlobalCounter("ippair.memcap", IPPairGetMemcap);
2,183✔
1175
    StatsRegisterGlobalCounter("host.memuse", HostGetMemuse);
2,183✔
1176
    StatsRegisterGlobalCounter("host.memcap", HostGetMemcap);
2,183✔
1177
}
2,183✔
1178

1179
static bool IsAppLayerErrorExceptionPolicyStatsValid(enum ExceptionPolicy policy)
1180
{
8,648✔
1181
    if (EngineModeIsIPS()) {
8,648✔
1182
        return app_layer_error_eps_stats.valid_settings_ips[policy];
8,608✔
1183
    }
8,608✔
1184
    return app_layer_error_eps_stats.valid_settings_ids[policy];
40✔
1185
}
8,648✔
1186

1187
static void AppLayerSetupExceptionPolicyPerProtoCounters(
1188
        uint8_t ipproto_map, AppProto alproto, const char *alproto_str, const char *ipproto_suffix)
1189
{
135,406✔
1190
    if (g_stats_eps_per_app_proto_errors &&
135,406✔
1191
            g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
135,406✔
UNCOV
1192
        for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1; i < EXCEPTION_POLICY_MAX; i++) {
×
UNCOV
1193
            if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
×
UNCOV
1194
                snprintf(applayer_counter_names[alproto][ipproto_map].eps_name[i],
×
UNCOV
1195
                        sizeof(applayer_counter_names[alproto][ipproto_map].eps_name[i]),
×
UNCOV
1196
                        "app_layer.error.%s%s.exception_policy.%s", alproto_str, ipproto_suffix,
×
UNCOV
1197
                        ExceptionPolicyEnumToString(i, true));
×
UNCOV
1198
            }
×
UNCOV
1199
        }
×
UNCOV
1200
    }
×
1201
}
135,406✔
1202

1203
void AppLayerSetupCounters(void)
1204
{
2,207✔
1205
    const uint8_t ipprotos[] = { IPPROTO_TCP, IPPROTO_UDP };
2,207✔
1206
    AppProto alprotos[g_alproto_max];
2,207✔
1207
    const char *str = "app_layer.flow.";
2,207✔
1208
    const char *estr = "app_layer.error.";
2,207✔
1209

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

1232
    AppLayerProtoDetectSupportedAppProtocols(alprotos);
2,207✔
1233

1234
    for (uint8_t p = 0; p < FLOW_PROTO_APPLAYER_MAX; p++) {
6,621✔
1235
        const uint8_t ipproto = ipprotos[p];
4,414✔
1236
        const uint8_t ipproto_map = FlowGetProtoMapping(ipproto);
4,414✔
1237
        const char *ipproto_suffix = (ipproto == IPPROTO_TCP) ? "_tcp" : "_udp";
4,414✔
1238
        uint8_t ipprotos_all[256 / 8];
4,414✔
1239

1240
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
180,972✔
1241
            if (alprotos[alproto] == 1) {
176,558✔
1242
                const char *tx_str = "app_layer.tx.";
135,406✔
1243
                const char *alproto_str = AppLayerGetProtoName(alproto);
135,406✔
1244

1245
                memset(ipprotos_all, 0, sizeof(ipprotos_all));
135,406✔
1246
                AppLayerProtoDetectSupportedIpprotos(alproto, ipprotos_all);
135,406✔
1247
                if ((ipprotos_all[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) &&
135,406✔
1248
                        (ipprotos_all[IPPROTO_UDP / 8] & (1 << (IPPROTO_UDP % 8)))) {
135,406✔
1249
                    snprintf(applayer_counter_names[alproto][ipproto_map].name,
27,904✔
1250
                            sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s%s",
27,904✔
1251
                            str, alproto_str, ipproto_suffix);
27,904✔
1252
                    snprintf(applayer_counter_names[alproto][ipproto_map].tx_name,
27,904✔
1253
                            sizeof(applayer_counter_names[alproto][ipproto_map].tx_name), "%s%s%s",
27,904✔
1254
                            tx_str, alproto_str, ipproto_suffix);
27,904✔
1255

1256
                    if (ipproto == IPPROTO_TCP) {
27,904✔
1257
                        snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
13,952✔
1258
                                sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
13,952✔
1259
                                "%s%s%s.gap", estr, alproto_str, ipproto_suffix);
13,952✔
1260
                    }
13,952✔
1261
                    snprintf(applayer_counter_names[alproto][ipproto_map].alloc_error,
27,904✔
1262
                            sizeof(applayer_counter_names[alproto][ipproto_map].alloc_error),
27,904✔
1263
                            "%s%s%s.alloc", estr, alproto_str, ipproto_suffix);
27,904✔
1264
                    snprintf(applayer_counter_names[alproto][ipproto_map].parser_error,
27,904✔
1265
                            sizeof(applayer_counter_names[alproto][ipproto_map].parser_error),
27,904✔
1266
                            "%s%s%s.parser", estr, alproto_str, ipproto_suffix);
27,904✔
1267
                    snprintf(applayer_counter_names[alproto][ipproto_map].internal_error,
27,904✔
1268
                            sizeof(applayer_counter_names[alproto][ipproto_map].internal_error),
27,904✔
1269
                            "%s%s%s.internal", estr, alproto_str, ipproto_suffix);
27,904✔
1270

1271
                    AppLayerSetupExceptionPolicyPerProtoCounters(
27,904✔
1272
                            ipproto_map, alproto, alproto_str, ipproto_suffix);
27,904✔
1273
                } else {
107,502✔
1274
                    snprintf(applayer_counter_names[alproto][ipproto_map].name,
107,502✔
1275
                            sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s", str,
107,502✔
1276
                            alproto_str);
107,502✔
1277
                    snprintf(applayer_counter_names[alproto][ipproto_map].tx_name,
107,502✔
1278
                            sizeof(applayer_counter_names[alproto][ipproto_map].tx_name), "%s%s",
107,502✔
1279
                            tx_str, alproto_str);
107,502✔
1280

1281
                    if (ipproto == IPPROTO_TCP) {
107,502✔
1282
                        snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
53,751✔
1283
                                sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
53,751✔
1284
                                "%s%s.gap", estr, alproto_str);
53,751✔
1285
                    }
53,751✔
1286
                    snprintf(applayer_counter_names[alproto][ipproto_map].alloc_error,
107,502✔
1287
                            sizeof(applayer_counter_names[alproto][ipproto_map].alloc_error),
107,502✔
1288
                            "%s%s.alloc", estr, alproto_str);
107,502✔
1289
                    snprintf(applayer_counter_names[alproto][ipproto_map].parser_error,
107,502✔
1290
                            sizeof(applayer_counter_names[alproto][ipproto_map].parser_error),
107,502✔
1291
                            "%s%s.parser", estr, alproto_str);
107,502✔
1292
                    snprintf(applayer_counter_names[alproto][ipproto_map].internal_error,
107,502✔
1293
                            sizeof(applayer_counter_names[alproto][ipproto_map].internal_error),
107,502✔
1294
                            "%s%s.internal", estr, alproto_str);
107,502✔
1295
                    AppLayerSetupExceptionPolicyPerProtoCounters(
107,502✔
1296
                            ipproto_map, alproto, alproto_str, "");
107,502✔
1297
                }
107,502✔
1298
            } else if (alproto == ALPROTO_FAILED) {
135,406✔
1299
                snprintf(applayer_counter_names[alproto][ipproto_map].name,
4,414✔
1300
                        sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s%s", str,
4,414✔
1301
                        "failed", ipproto_suffix);
4,414✔
1302
                if (ipproto == IPPROTO_TCP) {
4,414✔
1303
                    snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
2,207✔
1304
                            sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
2,207✔
1305
                            "%sfailed%s.gap", estr, ipproto_suffix);
2,207✔
1306
                }
2,207✔
1307
            }
4,414✔
1308
        }
176,558✔
1309
    }
4,414✔
1310
}
2,207✔
1311

1312
void AppLayerRegisterThreadCounters(ThreadVars *tv)
1313
{
7,933✔
1314
    const uint8_t ipprotos[] = { IPPROTO_TCP, IPPROTO_UDP };
7,933✔
1315
    AppProto alprotos[g_alproto_max];
7,933✔
1316
    AppLayerProtoDetectSupportedAppProtocols(alprotos);
7,933✔
1317

1318
    /* We don't log stats counters if exception policy is `ignore`/`not set` */
1319
    if (g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
7,933✔
1320
        /* Register global counters for app layer error exception policy summary */
1321
        for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1; i < EXCEPTION_POLICY_MAX; i++) {
7,677✔
1322
            if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
6,824✔
1323
                eps_error_summary.eps_id[i] =
5,963✔
1324
                        StatsRegisterCounter(app_layer_error_eps_stats.eps_name[i], &tv->stats);
5,963✔
1325
            }
5,963✔
1326
        }
6,824✔
1327
    }
853✔
1328

1329
    for (uint8_t p = 0; p < FLOW_PROTO_APPLAYER_MAX; p++) {
23,799✔
1330
        const uint8_t ipproto = ipprotos[p];
15,866✔
1331
        const uint8_t ipproto_map = FlowGetProtoMapping(ipproto);
15,866✔
1332

1333
        for (AppProto alproto = 0; alproto < g_alproto_max; alproto++) {
650,498✔
1334
            if (alprotos[alproto] == 1) {
634,632✔
1335
                applayer_counters[alproto][ipproto_map].counter_id = StatsRegisterCounter(
485,874✔
1336
                        applayer_counter_names[alproto][ipproto_map].name, &tv->stats);
485,874✔
1337

1338
                if (AppLayerParserProtoIsRegistered(ipproto, alproto) != 1)
485,874✔
1339
                    continue;
200,894✔
1340

1341
                applayer_counters[alproto][ipproto_map].counter_tx_id = StatsRegisterCounter(
284,980✔
1342
                        applayer_counter_names[alproto][ipproto_map].tx_name, &tv->stats);
284,980✔
1343

1344
                if (ipproto == IPPROTO_TCP) {
284,980✔
1345
                    applayer_counters[alproto][ipproto_map].gap_error_id = StatsRegisterCounter(
171,568✔
1346
                            applayer_counter_names[alproto][ipproto_map].gap_error, &tv->stats);
171,568✔
1347
                }
171,568✔
1348
                applayer_counters[alproto][ipproto_map].alloc_error_id = StatsRegisterCounter(
284,980✔
1349
                        applayer_counter_names[alproto][ipproto_map].alloc_error, &tv->stats);
284,980✔
1350
                applayer_counters[alproto][ipproto_map].parser_error_id = StatsRegisterCounter(
284,980✔
1351
                        applayer_counter_names[alproto][ipproto_map].parser_error, &tv->stats);
284,980✔
1352
                applayer_counters[alproto][ipproto_map].internal_error_id = StatsRegisterCounter(
284,980✔
1353
                        applayer_counter_names[alproto][ipproto_map].internal_error, &tv->stats);
284,980✔
1354
                /* We don't log stats counters if exception policy is `ignore`/`not set` */
1355
                if (g_stats_eps_per_app_proto_errors &&
284,980✔
1356
                        g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
284,980✔
UNCOV
1357
                    for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1;
×
UNCOV
1358
                            i < EXCEPTION_POLICY_MAX; i++) {
×
UNCOV
1359
                        if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
×
UNCOV
1360
                            applayer_counters[alproto][ipproto_map]
×
UNCOV
1361
                                    .eps_error.eps_id[i] = StatsRegisterCounter(
×
UNCOV
1362
                                    applayer_counter_names[alproto][ipproto_map].eps_name[i],
×
UNCOV
1363
                                    &tv->stats);
×
UNCOV
1364
                        }
×
UNCOV
1365
                    }
×
UNCOV
1366
                }
×
1367
            } else if (alproto == ALPROTO_FAILED) {
284,980✔
1368
                applayer_counters[alproto][ipproto_map].counter_id = StatsRegisterCounter(
15,866✔
1369
                        applayer_counter_names[alproto][ipproto_map].name, &tv->stats);
15,866✔
1370

1371
                if (ipproto == IPPROTO_TCP) {
15,866✔
1372
                    applayer_counters[alproto][ipproto_map].gap_error_id = StatsRegisterCounter(
7,933✔
1373
                            applayer_counter_names[alproto][ipproto_map].gap_error, &tv->stats);
7,933✔
1374
                }
7,933✔
1375
            }
15,866✔
1376
        }
634,632✔
1377
    }
15,866✔
1378
}
7,933✔
1379

1380
void AppLayerDeSetupCounters(void)
1381
{
2,120✔
1382
    SCFree(applayer_counter_names);
2,120✔
1383
    SCFree(applayer_counters);
2,120✔
1384
}
2,120✔
1385

1386
/***** Unittests *****/
1387

1388
#ifdef UNITTESTS
1389
#include "pkt-var.h"
1390
#include "stream-tcp-util.h"
1391
#include "stream.h"
1392
#include "util-unittest.h"
1393

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

1486
/**
1487
 * \test GET -> HTTP/1.1
1488
 */
1489
static int AppLayerTest01(void)
1490
{
1✔
1491
    TEST_START;
37✔
1492

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

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

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

1607
    TEST_END;
1✔
1608
    PASS;
1✔
1609
}
1✔
1610

1611
/**
1612
 * \test GE -> T -> HTTP/1.1
1613
 */
1614
static int AppLayerTest02(void)
1615
{
1✔
1616
    TEST_START;
37✔
1617

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

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

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

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

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

1773
    TEST_END;
1✔
1774
    PASS;
1✔
1775
}
1✔
1776

1777
/**
1778
 * \test GET -> RUBBISH(PM AND PP DONE IN ONE GO)
1779
 */
1780
static int AppLayerTest03(void)
1781
{
1✔
1782
    TEST_START;
37✔
1783

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

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

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

1898
    TEST_END;
1✔
1899
    PASS;
1✔
1900
}
1✔
1901

1902
/**
1903
 * \test GE -> RUBBISH(TC - PM AND PP NOT DONE) -> RUBBISH(TC - PM AND PP DONE).
1904
 */
1905
static int AppLayerTest04(void)
1906
{
1✔
1907
    TEST_START;
37✔
1908

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

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

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

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

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

2067
    TEST_END;
1✔
2068
    PASS;
1✔
2069
}
1✔
2070

2071
/**
2072
 * \test RUBBISH -> HTTP/1.1
2073
 */
2074
static int AppLayerTest05(void)
2075
{
1✔
2076
    TEST_START;
37✔
2077

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

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

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

2194
    TEST_END;
1✔
2195
    PASS;
1✔
2196
}
1✔
2197

2198
/**
2199
 * \test HTTP/1.1 -> GET
2200
 */
2201
static int AppLayerTest06(void)
2202
{
1✔
2203
    TEST_START;
37✔
2204

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

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

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

2318
    TEST_END;
1✔
2319
    PASS;
1✔
2320
}
1✔
2321

2322
/**
2323
 * \test GET -> DCERPC
2324
 */
2325
static int AppLayerTest07(void)
2326
{
1✔
2327
    TEST_START;
37✔
2328

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

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

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

2423
    TEST_END;
1✔
2424
    PASS;
1✔
2425
}
1✔
2426

2427
/**
2428
 * \test RUBBISH(TC - PM and PP NOT DONE) ->
2429
 *       RUBBISH(TC - PM and PP DONE) ->
2430
 *       RUBBISH(TS - PM and PP DONE)
2431
 */
2432
static int AppLayerTest09(void)
2433
{
1✔
2434
    TEST_START;
37✔
2435

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

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

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

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

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

2582
    TEST_END;
1✔
2583
    PASS;
1✔
2584
}
1✔
2585

2586
/**
2587
 * \test RUBBISH(TC - PM and PP DONE) ->
2588
 *       RUBBISH(TS - PM and PP DONE)
2589
 */
2590
static int AppLayerTest10(void)
2591
{
1✔
2592
    TEST_START;
37✔
2593

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

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

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

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

2719
    TEST_END;
1✔
2720
    PASS;
1✔
2721
}
1✔
2722

2723
/**
2724
 * \test RUBBISH(TC - PM and PP DONE) ->
2725
 *       RUBBISH(TS - PM and PP NOT DONE) ->
2726
 *       RUBBISH(TS - PM and PP DONE)
2727
 */
2728
static int AppLayerTest11(void)
2729
{
1✔
2730
    TEST_START;
37✔
2731

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

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

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

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

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

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

2898
    TEST_END;
1✔
2899
    PASS;
1✔
2900
}
1✔
2901

2902
void AppLayerUnittestsRegister(void)
2903
{
1✔
2904
    SCEnter();
1✔
2905

2906
    UtRegisterTest("AppLayerTest01", AppLayerTest01);
1✔
2907
    UtRegisterTest("AppLayerTest02", AppLayerTest02);
1✔
2908
    UtRegisterTest("AppLayerTest03", AppLayerTest03);
1✔
2909
    UtRegisterTest("AppLayerTest04", AppLayerTest04);
1✔
2910
    UtRegisterTest("AppLayerTest05", AppLayerTest05);
1✔
2911
    UtRegisterTest("AppLayerTest06", AppLayerTest06);
1✔
2912
    UtRegisterTest("AppLayerTest07", AppLayerTest07);
1✔
2913
    UtRegisterTest("AppLayerTest09", AppLayerTest09);
1✔
2914
    UtRegisterTest("AppLayerTest10", AppLayerTest10);
1✔
2915
    UtRegisterTest("AppLayerTest11", AppLayerTest11);
1✔
2916

2917
    SCReturn;
1✔
2918
}
1✔
2919

2920
#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