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

jasonish / suricata / 22598483541

01 Mar 2026 06:45AM UTC coverage: 73.677% (+30.4%) from 43.31%
22598483541

push

github

victorjulien
detect/transforms: update gunzip / zlib_deflate syntax

Use standard space separated syntax.

38322 of 77530 branches covered (49.43%)

Branch coverage included in aggregate %.

6 of 6 new or added lines in 1 file covered. (100.0%)

501 existing lines in 30 files now uncovered.

265690 of 335097 relevant lines covered (79.29%)

4921680.05 hits per line

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

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

18
/**
19
 * \file
20
 *
21
 * \author Victor Julien <victor@inliniac.net>
22
 *
23
 * Implements per host bits. Actually, not a bit,
24
 * but called that way because of Snort's flowbits.
25
 * It's a binary storage.
26
 *
27
 * \todo move away from a linked list implementation
28
 * \todo use different datatypes, such as string, int, etc.
29
 */
30

31
#include "suricata-common.h"
32
#include "threads.h"
33
#include "host-bit.h"
34
#include "host.h"
35
#include "detect.h"
36
#include "util-var.h"
37
#include "util-debug.h"
38
#include "util-unittest.h"
39
#include "host-storage.h"
40

41
static HostStorageId host_bit_id = { .id = -1 }; /**< Host storage id for bits */
42

43
static void HostBitFreeAll(void *store)
44
{
14✔
45
    GenericVar *gv = store;
14✔
46
    SCGenericVarFree(gv);
14✔
47
}
14✔
48

49
void HostBitInitCtx(void)
50
{
2,209✔
51
    host_bit_id = HostStorageRegister("bit", sizeof(void *), NULL, HostBitFreeAll);
2,209✔
52
    if (host_bit_id.id == -1) {
2,209!
53
        FatalError("Can't initiate host storage for bits");
×
54
    }
×
55
}
2,209✔
56

57
/* lock before using this */
58
int HostHasHostBits(Host *host)
59
{
108✔
60
    if (host == NULL)
108!
61
        return 0;
×
62
    return HostGetStorageById(host, host_bit_id) ? 1 : 0;
108✔
63
}
108✔
64

65
/** \retval 1 host timed out wrt xbits
66
  * \retval 0 host still has active (non-expired) xbits */
67
int HostBitsTimedoutCheck(Host *h, SCTime_t ts)
68
{
93✔
69
    GenericVar *gv = HostGetStorageById(h, host_bit_id);
93✔
70
    for ( ; gv != NULL; gv = gv->next) {
93!
71
        if (gv->type == DETECT_XBITS) {
93!
72
            XBit *xb = (XBit *)gv;
93✔
73
            if (SCTIME_CMP_GT(xb->expire, ts))
93✔
74
                return 0;
93✔
75
        }
93✔
76
    }
93✔
77
    return 1;
×
78
}
93✔
79

80
/* get the bit with idx from the host */
81
static XBit *HostBitGet(Host *h, uint32_t idx)
82
{
589✔
83
    GenericVar *gv = HostGetStorageById(h, host_bit_id);
589✔
84
    for ( ; gv != NULL; gv = gv->next) {
743✔
85
        if (gv->type == DETECT_XBITS && gv->idx == idx) {
553!
86
            return (XBit *)gv;
399✔
87
        }
399✔
88
    }
553✔
89

90
    return NULL;
190✔
91
}
589✔
92

93
/* add a flowbit to the flow */
94
static void HostBitAdd(Host *h, uint32_t idx, SCTime_t expire)
95
{
106✔
96
    XBit *fb = HostBitGet(h, idx);
106✔
97
    if (fb == NULL) {
106!
98
        fb = SCMalloc(sizeof(XBit));
106✔
99
        if (unlikely(fb == NULL))
106!
100
            return;
×
101

102
        fb->type = DETECT_XBITS;
106✔
103
        fb->idx = idx;
106✔
104
        fb->next = NULL;
106✔
105
        fb->expire = expire;
106✔
106

107
        GenericVar *gv = HostGetStorageById(h, host_bit_id);
106✔
108
        GenericVarAppend(&gv, (GenericVar *)fb);
106✔
109
        HostSetStorageById(h, host_bit_id, gv);
106✔
110

111
    // bit already set, lets update it's time
112
    } else {
106✔
113
        fb->expire = expire;
×
114
    }
×
115
}
106✔
116

117
static void HostBitRemove(Host *h, uint32_t idx)
118
{
12✔
119
    XBit *fb = HostBitGet(h, idx);
12✔
120
    if (fb == NULL)
12!
121
        return;
×
122

123
    GenericVar *gv = HostGetStorageById(h, host_bit_id);
12✔
124
    if (gv) {
12!
125
        GenericVarRemove(&gv, (GenericVar *)fb);
12✔
126
        XBitFree(fb);
12✔
127
        HostSetStorageById(h, host_bit_id, gv);
12✔
128
    }
12✔
129
}
12✔
130

131
void HostBitSet(Host *h, uint32_t idx, SCTime_t expire)
132
{
351✔
133
    XBit *fb = HostBitGet(h, idx);
351✔
134
    if (fb == NULL) {
351!
135
        HostBitAdd(h, idx, expire);
71✔
136
    }
71✔
137
}
351✔
138

139
void HostBitUnset(Host *h, uint32_t idx)
140
{
7✔
141
    XBit *fb = HostBitGet(h, idx);
7✔
142
    if (fb != NULL) {
7!
143
        HostBitRemove(h, idx);
6✔
144
    }
6✔
145
}
7✔
146

147
void HostBitToggle(Host *h, uint32_t idx, SCTime_t expire)
148
{
2✔
149
    XBit *fb = HostBitGet(h, idx);
2✔
150
    if (fb != NULL) {
2✔
151
        HostBitRemove(h, idx);
1✔
152
    } else {
1✔
153
        HostBitAdd(h, idx, expire);
1✔
154
    }
1✔
155
}
2✔
156

157
int HostBitIsset(Host *h, uint32_t idx, SCTime_t ts)
158
{
95✔
159
    XBit *fb = HostBitGet(h, idx);
95✔
160
    if (fb != NULL) {
95✔
161
        if (SCTIME_CMP_LT(fb->expire, ts)) {
90!
162
            HostBitRemove(h,idx);
×
163
            return 0;
×
164
        }
×
165
        return 1;
90✔
166
    }
90✔
167
    return 0;
5✔
168
}
95✔
169

170
int HostBitIsnotset(Host *h, uint32_t idx, SCTime_t ts)
UNCOV
171
{
×
UNCOV
172
    XBit *fb = HostBitGet(h, idx);
×
UNCOV
173
    if (fb == NULL) {
×
UNCOV
174
        return 1;
×
UNCOV
175
    }
×
176

177
    if (SCTIME_CMP_LT(fb->expire, ts)) {
×
178
        HostBitRemove(h,idx);
×
179
        return 1;
×
180
    }
×
181
    return 0;
×
182
}
×
183

184
int HostBitList(Host *h, XBit **iter)
185
{
12✔
186
    GenericVar *gv = (GenericVar *)*iter;
12✔
187
    if (gv == NULL) {
12!
188
        gv = HostGetStorageById(h, host_bit_id);
6✔
189
    } else {
6✔
190
        gv = gv->next;
6✔
191
    }
6✔
192

193
    for ( ; gv != NULL; gv = gv->next) {
12!
194
        if (gv->type == DETECT_XBITS) {
6!
195
            *iter = (XBit *)gv;
6✔
196
            return 1;
6✔
197
        }
6✔
198
    }
6✔
199
    *iter = NULL;
6✔
200
    return 0;
6✔
201
}
12✔
202

203
/* TESTS */
204
#ifdef UNITTESTS
205
static int HostBitTest01 (void)
206
{
1✔
207
    int ret = 0;
1✔
208

209
    StorageCleanup();
1✔
210
    StorageInit();
1✔
211
    HostBitInitCtx();
1✔
212
    StorageFinalize();
1✔
213

214
    HostInitConfig(true);
1✔
215
    Host *h = HostAlloc();
1✔
216
    if (h == NULL)
1✔
217
        goto end;
218

219
    HostBitAdd(h, 0, SCTIME_FROM_SECS(0));
1✔
220

221
    XBit *fb = HostBitGet(h,0);
1✔
222
    if (fb != NULL)
1✔
223
        ret = 1;
1✔
224

225
    HostFree(h);
1✔
226
end:
1✔
227
    HostShutdown();
1✔
228
    StorageCleanup();
1✔
229
    return ret;
1✔
230
}
1✔
231

232
static int HostBitTest02 (void)
233
{
1✔
234
    int ret = 0;
1✔
235

236
    StorageCleanup();
1✔
237
    StorageInit();
1✔
238
    HostBitInitCtx();
1✔
239
    StorageFinalize();
1✔
240

241
    HostInitConfig(true);
1✔
242
    Host *h = HostAlloc();
1✔
243
    if (h == NULL)
1✔
244
        goto end;
245

246
    XBit *fb = HostBitGet(h,0);
1✔
247
    if (fb == NULL)
1✔
248
        ret = 1;
1✔
249

250
    HostFree(h);
1✔
251
end:
1✔
252
    HostShutdown();
1✔
253
    StorageCleanup();
1✔
254
    return ret;
1✔
255
}
1✔
256

257
static int HostBitTest03 (void)
258
{
1✔
259
    int ret = 0;
1✔
260

261
    StorageCleanup();
1✔
262
    StorageInit();
1✔
263
    HostBitInitCtx();
1✔
264
    StorageFinalize();
1✔
265

266
    HostInitConfig(true);
1✔
267
    Host *h = HostAlloc();
1✔
268
    if (h == NULL)
1✔
269
        goto end;
270

271
    HostBitAdd(h, 0, SCTIME_FROM_SECS(30));
1✔
272

273
    XBit *fb = HostBitGet(h,0);
1✔
274
    if (fb == NULL) {
1✔
275
        printf("fb == NULL although it was just added: ");
276
        goto end;
277
    }
278

279
    HostBitRemove(h, 0);
1✔
280

281
    fb = HostBitGet(h,0);
1✔
282
    if (fb != NULL) {
1✔
283
        printf("fb != NULL although it was just removed: ");
284
        goto end;
285
    } else {
1✔
286
        ret = 1;
1✔
287
    }
1✔
288

289
    HostFree(h);
1✔
290
end:
1✔
291
    HostShutdown();
1✔
292
    StorageCleanup();
1✔
293
    return ret;
1✔
294
}
1✔
295

296
static int HostBitTest04 (void)
297
{
1✔
298
    int ret = 0;
1✔
299

300
    StorageCleanup();
1✔
301
    StorageInit();
1✔
302
    HostBitInitCtx();
1✔
303
    StorageFinalize();
1✔
304

305
    HostInitConfig(true);
1✔
306
    Host *h = HostAlloc();
1✔
307
    if (h == NULL)
1✔
308
        goto end;
309

310
    HostBitAdd(h, 0, SCTIME_FROM_SECS(30));
1✔
311
    HostBitAdd(h, 1, SCTIME_FROM_SECS(30));
1✔
312
    HostBitAdd(h, 2, SCTIME_FROM_SECS(30));
1✔
313
    HostBitAdd(h, 3, SCTIME_FROM_SECS(30));
1✔
314

315
    XBit *fb = HostBitGet(h,0);
1✔
316
    if (fb != NULL)
1✔
317
        ret = 1;
1✔
318

319
    HostFree(h);
1✔
320
end:
1✔
321
    HostShutdown();
1✔
322
    StorageCleanup();
1✔
323
    return ret;
1✔
324
}
1✔
325

326
static int HostBitTest05 (void)
327
{
1✔
328
    int ret = 0;
1✔
329

330
    StorageCleanup();
1✔
331
    StorageInit();
1✔
332
    HostBitInitCtx();
1✔
333
    StorageFinalize();
1✔
334

335
    HostInitConfig(true);
1✔
336
    Host *h = HostAlloc();
1✔
337
    if (h == NULL)
1✔
338
        goto end;
339

340
    HostBitAdd(h, 0, SCTIME_FROM_SECS(30));
1✔
341
    HostBitAdd(h, 1, SCTIME_FROM_SECS(30));
1✔
342
    HostBitAdd(h, 2, SCTIME_FROM_SECS(30));
1✔
343
    HostBitAdd(h, 3, SCTIME_FROM_SECS(30));
1✔
344

345
    XBit *fb = HostBitGet(h,1);
1✔
346
    if (fb != NULL)
1✔
347
        ret = 1;
1✔
348

349
    HostFree(h);
1✔
350
end:
1✔
351
    HostShutdown();
1✔
352
    StorageCleanup();
1✔
353
    return ret;
1✔
354
}
1✔
355

356
static int HostBitTest06 (void)
357
{
1✔
358
    int ret = 0;
1✔
359

360
    StorageCleanup();
1✔
361
    StorageInit();
1✔
362
    HostBitInitCtx();
1✔
363
    StorageFinalize();
1✔
364

365
    HostInitConfig(true);
1✔
366
    Host *h = HostAlloc();
1✔
367
    if (h == NULL)
1✔
368
        goto end;
369

370
    HostBitAdd(h, 0, SCTIME_FROM_SECS(90));
1✔
371
    HostBitAdd(h, 1, SCTIME_FROM_SECS(90));
1✔
372
    HostBitAdd(h, 2, SCTIME_FROM_SECS(90));
1✔
373
    HostBitAdd(h, 3, SCTIME_FROM_SECS(90));
1✔
374

375
    XBit *fb = HostBitGet(h,2);
1✔
376
    if (fb != NULL)
1✔
377
        ret = 1;
1✔
378

379
    HostFree(h);
1✔
380
end:
1✔
381
    HostShutdown();
1✔
382
    StorageCleanup();
1✔
383
    return ret;
1✔
384
}
1✔
385

386
static int HostBitTest07 (void)
387
{
1✔
388
    int ret = 0;
1✔
389

390
    StorageCleanup();
1✔
391
    StorageInit();
1✔
392
    HostBitInitCtx();
1✔
393
    StorageFinalize();
1✔
394

395
    HostInitConfig(true);
1✔
396
    Host *h = HostAlloc();
1✔
397
    if (h == NULL)
1✔
398
        goto end;
399

400
    HostBitAdd(h, 0, SCTIME_FROM_SECS(90));
1✔
401
    HostBitAdd(h, 1, SCTIME_FROM_SECS(90));
1✔
402
    HostBitAdd(h, 2, SCTIME_FROM_SECS(90));
1✔
403
    HostBitAdd(h, 3, SCTIME_FROM_SECS(90));
1✔
404

405
    XBit *fb = HostBitGet(h,3);
1✔
406
    if (fb != NULL)
1✔
407
        ret = 1;
1✔
408

409
    HostFree(h);
1✔
410
end:
1✔
411
    HostShutdown();
1✔
412
    StorageCleanup();
1✔
413
    return ret;
1✔
414
}
1✔
415

416
static int HostBitTest08 (void)
417
{
1✔
418
    int ret = 0;
1✔
419

420
    StorageCleanup();
1✔
421
    StorageInit();
1✔
422
    HostBitInitCtx();
1✔
423
    StorageFinalize();
1✔
424

425
    HostInitConfig(true);
1✔
426
    Host *h = HostAlloc();
1✔
427
    if (h == NULL)
1✔
428
        goto end;
429

430
    HostBitAdd(h, 0, SCTIME_FROM_SECS(90));
1✔
431
    HostBitAdd(h, 1, SCTIME_FROM_SECS(90));
1✔
432
    HostBitAdd(h, 2, SCTIME_FROM_SECS(90));
1✔
433
    HostBitAdd(h, 3, SCTIME_FROM_SECS(90));
1✔
434

435
    XBit *fb = HostBitGet(h,0);
1✔
436
    if (fb == NULL)
1✔
437
        goto end;
438

439
    HostBitRemove(h,0);
1✔
440

441
    fb = HostBitGet(h,0);
1✔
442
    if (fb != NULL) {
1✔
443
        printf("fb != NULL even though it was removed: ");
444
        goto end;
445
    }
446

447
    ret = 1;
1✔
448
    HostFree(h);
1✔
449
end:
1✔
450
    HostShutdown();
1✔
451
    StorageCleanup();
1✔
452
    return ret;
1✔
453
}
1✔
454

455
static int HostBitTest09 (void)
456
{
1✔
457
    int ret = 0;
1✔
458

459
    StorageCleanup();
1✔
460
    StorageInit();
1✔
461
    HostBitInitCtx();
1✔
462
    StorageFinalize();
1✔
463

464
    HostInitConfig(true);
1✔
465
    Host *h = HostAlloc();
1✔
466
    if (h == NULL)
1✔
467
        goto end;
468

469
    HostBitAdd(h, 0, SCTIME_FROM_SECS(90));
1✔
470
    HostBitAdd(h, 1, SCTIME_FROM_SECS(90));
1✔
471
    HostBitAdd(h, 2, SCTIME_FROM_SECS(90));
1✔
472
    HostBitAdd(h, 3, SCTIME_FROM_SECS(90));
1✔
473

474
    XBit *fb = HostBitGet(h,1);
1✔
475
    if (fb == NULL)
1✔
476
        goto end;
477

478
    HostBitRemove(h,1);
1✔
479

480
    fb = HostBitGet(h,1);
1✔
481
    if (fb != NULL) {
1✔
482
        printf("fb != NULL even though it was removed: ");
483
        goto end;
484
    }
485

486
    ret = 1;
1✔
487
    HostFree(h);
1✔
488
end:
1✔
489
    HostShutdown();
1✔
490
    StorageCleanup();
1✔
491
    return ret;
1✔
492
}
1✔
493

494
static int HostBitTest10 (void)
495
{
1✔
496
    int ret = 0;
1✔
497

498
    StorageCleanup();
1✔
499
    StorageInit();
1✔
500
    HostBitInitCtx();
1✔
501
    StorageFinalize();
1✔
502

503
    HostInitConfig(true);
1✔
504
    Host *h = HostAlloc();
1✔
505
    if (h == NULL)
1✔
506
        goto end;
507

508
    HostBitAdd(h, 0, SCTIME_FROM_SECS(90));
1✔
509
    HostBitAdd(h, 1, SCTIME_FROM_SECS(90));
1✔
510
    HostBitAdd(h, 2, SCTIME_FROM_SECS(90));
1✔
511
    HostBitAdd(h, 3, SCTIME_FROM_SECS(90));
1✔
512

513
    XBit *fb = HostBitGet(h,2);
1✔
514
    if (fb == NULL)
1✔
515
        goto end;
516

517
    HostBitRemove(h,2);
1✔
518

519
    fb = HostBitGet(h,2);
1✔
520
    if (fb != NULL) {
1✔
521
        printf("fb != NULL even though it was removed: ");
522
        goto end;
523
    }
524

525
    ret = 1;
1✔
526
    HostFree(h);
1✔
527
end:
1✔
528
    HostShutdown();
1✔
529
    StorageCleanup();
1✔
530
    return ret;
1✔
531
}
1✔
532

533
static int HostBitTest11 (void)
534
{
1✔
535
    int ret = 0;
1✔
536

537
    StorageCleanup();
1✔
538
    StorageInit();
1✔
539
    HostBitInitCtx();
1✔
540
    StorageFinalize();
1✔
541

542
    HostInitConfig(true);
1✔
543
    Host *h = HostAlloc();
1✔
544
    if (h == NULL)
1✔
545
        goto end;
546

547
    HostBitAdd(h, 0, SCTIME_FROM_SECS(90));
1✔
548
    HostBitAdd(h, 1, SCTIME_FROM_SECS(90));
1✔
549
    HostBitAdd(h, 2, SCTIME_FROM_SECS(90));
1✔
550
    HostBitAdd(h, 3, SCTIME_FROM_SECS(90));
1✔
551

552
    XBit *fb = HostBitGet(h,3);
1✔
553
    if (fb == NULL)
1✔
554
        goto end;
555

556
    HostBitRemove(h,3);
1✔
557

558
    fb = HostBitGet(h,3);
1✔
559
    if (fb != NULL) {
1✔
560
        printf("fb != NULL even though it was removed: ");
561
        goto end;
562
    }
563

564
    ret = 1;
1✔
565
    HostFree(h);
1✔
566
end:
1✔
567
    HostShutdown();
1✔
568
    StorageCleanup();
1✔
569
    return ret;
1✔
570
}
1✔
571

572
#endif /* UNITTESTS */
573

574
void HostBitRegisterTests(void)
575
{
1✔
576
#ifdef UNITTESTS
1✔
577
    UtRegisterTest("HostBitTest01", HostBitTest01);
1✔
578
    UtRegisterTest("HostBitTest02", HostBitTest02);
1✔
579
    UtRegisterTest("HostBitTest03", HostBitTest03);
1✔
580
    UtRegisterTest("HostBitTest04", HostBitTest04);
1✔
581
    UtRegisterTest("HostBitTest05", HostBitTest05);
1✔
582
    UtRegisterTest("HostBitTest06", HostBitTest06);
1✔
583
    UtRegisterTest("HostBitTest07", HostBitTest07);
1✔
584
    UtRegisterTest("HostBitTest08", HostBitTest08);
1✔
585
    UtRegisterTest("HostBitTest09", HostBitTest09);
1✔
586
    UtRegisterTest("HostBitTest10", HostBitTest10);
1✔
587
    UtRegisterTest("HostBitTest11", HostBitTest11);
1✔
588
#endif /* UNITTESTS */
1✔
589
}
1✔
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