~ubuntu-branches/ubuntu/precise/suricata/precise-proposed

« back to all changes in this revision

Viewing changes to src/detect-engine-siggroup.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2010-06-19 17:39:14 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100619173914-5vkjfgz24mbia29z
Tags: 0.9.2-1
ImportedĀ UpstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "suricata-common.h"
27
27
#include "decode.h"
28
 
#include "detect.h"
 
28
 
29
29
#include "flow-var.h"
30
30
 
31
 
#include "util-cidr.h"
32
 
#include "util-unittest.h"
 
31
#include "app-layer-protos.h"
33
32
 
34
33
#include "detect.h"
35
34
#include "detect-parse.h"
46
45
 
47
46
#include "util-error.h"
48
47
#include "util-debug.h"
 
48
#include "util-cidr.h"
 
49
#include "util-unittest.h"
 
50
 
49
51
 
50
52
/* prototypes */
51
53
int SigGroupHeadClearSigs(SigGroupHead *);
63
65
static uint32_t detect_siggroup_matcharray_init_cnt = 0;
64
66
static uint32_t detect_siggroup_matcharray_free_cnt = 0;
65
67
 
66
 
static SigGroupHeadInitData *SigGroupHeadInitDataAlloc(uint32_t size) {
67
 
    SigGroupHeadInitData *sghid = SCMalloc(sizeof(SigGroupHeadInitData));
68
 
    if (sghid == NULL)
69
 
        return NULL;
70
 
 
71
 
    memset(sghid, 0x00, sizeof(SigGroupHeadInitData));
72
 
 
73
 
    detect_siggroup_head_initdata_init_cnt++;
74
 
    detect_siggroup_head_initdata_memory += sizeof(SigGroupHeadInitData);
75
 
    return sghid;
76
 
}
77
 
 
78
68
void SigGroupHeadInitDataFree(SigGroupHeadInitData *sghid) {
79
69
    if (sghid->content_array != NULL) {
80
70
        SCFree(sghid->content_array);
86
76
        sghid->uri_content_array = NULL;
87
77
        sghid->uri_content_size = 0;
88
78
    }
 
79
    if (sghid->sig_array != NULL) {
 
80
        SCFree(sghid->sig_array);
 
81
        sghid->sig_array = NULL;
 
82
 
 
83
        detect_siggroup_sigarray_free_cnt++;
 
84
        detect_siggroup_sigarray_memory -= sghid->sig_size;
 
85
    }
89
86
    SCFree(sghid);
90
87
 
91
88
    detect_siggroup_head_initdata_free_cnt++;
92
89
    detect_siggroup_head_initdata_memory -= sizeof(SigGroupHeadInitData);
93
90
}
94
91
 
 
92
static SigGroupHeadInitData *SigGroupHeadInitDataAlloc(uint32_t size) {
 
93
    SigGroupHeadInitData *sghid = SCMalloc(sizeof(SigGroupHeadInitData));
 
94
    if (sghid == NULL)
 
95
        return NULL;
 
96
 
 
97
    memset(sghid, 0x00, sizeof(SigGroupHeadInitData));
 
98
 
 
99
    detect_siggroup_head_initdata_init_cnt++;
 
100
    detect_siggroup_head_initdata_memory += sizeof(SigGroupHeadInitData);
 
101
 
 
102
    /* initialize the signature bitarray */
 
103
    sghid->sig_size = size;
 
104
    if ( (sghid->sig_array = SCMalloc(sghid->sig_size)) == NULL)
 
105
        goto error;
 
106
 
 
107
    memset(sghid->sig_array, 0, sghid->sig_size);
 
108
 
 
109
    detect_siggroup_sigarray_init_cnt++;
 
110
    detect_siggroup_sigarray_memory += sghid->sig_size;
 
111
 
 
112
    return sghid;
 
113
error:
 
114
    SigGroupHeadInitDataFree(sghid);
 
115
    return NULL;
 
116
}
 
117
 
 
118
void SigGroupHeadStore(DetectEngineCtx *de_ctx, SigGroupHead *sgh) {
 
119
    //printf("de_ctx->sgh_array_cnt %u, de_ctx->sgh_array_size %u, de_ctx->sgh_array %p\n", de_ctx->sgh_array_cnt, de_ctx->sgh_array_size, de_ctx->sgh_array);
 
120
    if (de_ctx->sgh_array_cnt < de_ctx->sgh_array_size) {
 
121
        de_ctx->sgh_array[de_ctx->sgh_array_cnt] = sgh;
 
122
    } else {
 
123
        de_ctx->sgh_array = SCRealloc(de_ctx->sgh_array, sizeof(SigGroupHead *) * (16 + de_ctx->sgh_array_size));
 
124
        if (de_ctx->sgh_array == NULL)
 
125
            return;
 
126
        de_ctx->sgh_array_size += 10;
 
127
        de_ctx->sgh_array[de_ctx->sgh_array_cnt] = sgh;
 
128
    }
 
129
    de_ctx->sgh_array_cnt++;
 
130
}
 
131
 
95
132
/**
96
133
 * \brief Alloc a SigGroupHead and its signature bit_array.
97
134
 *
101
138
 * \retval sgh Pointer to the newly init SigGroupHead on success; or NULL in
102
139
 *             case of error.
103
140
 */
104
 
static SigGroupHead *SigGroupHeadAlloc(uint32_t size)
 
141
static SigGroupHead *SigGroupHeadAlloc(DetectEngineCtx *de_ctx, uint32_t size)
105
142
{
106
143
    SigGroupHead *sgh = SCMalloc(sizeof(SigGroupHead));
107
144
    if (sgh == NULL)
115
152
    detect_siggroup_head_init_cnt++;
116
153
    detect_siggroup_head_memory += sizeof(SigGroupHead);
117
154
 
118
 
    /* initialize the signature bitarray */
119
 
    sgh->sig_size = size;
120
 
    if ( (sgh->sig_array = SCMalloc(sgh->sig_size)) == NULL)
121
 
        goto error;
122
 
    memset(sgh->sig_array, 0, sgh->sig_size);
123
 
 
124
 
    detect_siggroup_sigarray_init_cnt++;
125
 
    detect_siggroup_sigarray_memory += sgh->sig_size;
126
 
 
127
155
    return sgh;
128
156
 
129
157
error:
146
174
 
147
175
    PatternMatchDestroyGroup(sgh);
148
176
 
149
 
    if (sgh->sig_array != NULL) {
150
 
        SCFree(sgh->sig_array);
151
 
        sgh->sig_array = NULL;
152
 
 
153
 
        detect_siggroup_sigarray_free_cnt++;
154
 
        detect_siggroup_sigarray_memory -= sgh->sig_size;
155
 
    }
156
 
 
157
177
    if (sgh->match_array != NULL) {
158
178
        detect_siggroup_matcharray_free_cnt++;
159
 
        detect_siggroup_matcharray_memory -= (sgh->sig_cnt * sizeof(SigIntId));
 
179
        detect_siggroup_matcharray_memory -= (sgh->sig_cnt * sizeof(Signature *));
160
180
        SCFree(sgh->match_array);
161
181
        sgh->match_array = NULL;
162
 
        sgh->sig_cnt = 0;
163
182
    }
164
183
 
 
184
    sgh->sig_cnt = 0;
 
185
 
165
186
    if (sgh->init != NULL) {
166
187
        SigGroupHeadInitDataFree(sgh->init);
167
188
        sgh->init = NULL;
430
451
}
431
452
 
432
453
/**
 
454
 * \brief The hash function to be the used by the mpm uri SigGroupHead hash
 
455
 *        table - DetectEngineCtx->sgh_mpm_uri_hash_table.
 
456
 *
 
457
 * \param ht      Pointer to the hash table.
 
458
 * \param data    Pointer to the SigGroupHead.
 
459
 * \param datalen Not used in our case.
 
460
 *
 
461
 * \retval hash The generated hash value.
 
462
 */
 
463
uint32_t SigGroupHeadMpmStreamHashFunc(HashListTable *ht, void *data, uint16_t datalen)
 
464
{
 
465
    SigGroupHead *sgh = (SigGroupHead *)data;
 
466
    uint32_t hash = 0;
 
467
    uint32_t b = 0;
 
468
 
 
469
    for (b = 0; b < sgh->init->stream_content_size; b++)
 
470
        hash += sgh->init->stream_content_array[b];
 
471
 
 
472
    return hash % ht->array_size;
 
473
}
 
474
 
 
475
/**
 
476
 * \brief The Compare function to be used by the mpm uri SigGroupHead hash
 
477
 *        table - DetectEngineCtx->sgh_mpm_uri_hash_table.
 
478
 *
 
479
 * \param data1 Pointer to the first SigGroupHead.
 
480
 * \param len1  Not used.
 
481
 * \param data2 Pointer to the second SigGroupHead.
 
482
 * \param len2  Not used.
 
483
 *
 
484
 * \retval 1 If the 2 SigGroupHeads sent as args match.
 
485
 * \retval 0 If the 2 SigGroupHeads sent as args do not match.
 
486
 */
 
487
char SigGroupHeadMpmStreamCompareFunc(void *data1, uint16_t len1, void *data2,
 
488
                                   uint16_t len2)
 
489
{
 
490
    SigGroupHead *sgh1 = (SigGroupHead *)data1;
 
491
    SigGroupHead *sgh2 = (SigGroupHead *)data2;
 
492
 
 
493
    if (sgh1->init->stream_content_size != sgh2->init->stream_content_size)
 
494
        return 0;
 
495
 
 
496
    if (memcmp(sgh1->init->stream_content_array, sgh2->init->stream_content_array,
 
497
               sgh1->init->stream_content_size) != 0) {
 
498
        return 0;
 
499
    }
 
500
 
 
501
    return 1;
 
502
}
 
503
 
 
504
/**
 
505
 * \brief Initializes the mpm uri hash table to be used by the detection engine
 
506
 *        context.
 
507
 *
 
508
 * \param de_ctx Pointer to the detection engine context.
 
509
 *
 
510
 * \retval  0 On success.
 
511
 * \retval -1 On failure.
 
512
 */
 
513
int SigGroupHeadMpmStreamHashInit(DetectEngineCtx *de_ctx)
 
514
{
 
515
    de_ctx->sgh_mpm_stream_hash_table = HashListTableInit(4096,
 
516
            SigGroupHeadMpmStreamHashFunc, SigGroupHeadMpmStreamCompareFunc, NULL);
 
517
    if (de_ctx->sgh_mpm_stream_hash_table == NULL)
 
518
        goto error;
 
519
 
 
520
    return 0;
 
521
 
 
522
error:
 
523
    return -1;
 
524
}
 
525
 
 
526
/**
 
527
 * \brief Adds a SigGroupHead to the detection engine context SigGroupHead
 
528
 *        mpm uri hash table.
 
529
 *
 
530
 * \param de_ctx Pointer to the detection engine context.
 
531
 * \param sgh    Pointer to the SigGroupHead.
 
532
 *
 
533
 * \retval ret 0 on Successfully adding the argument sgh and -1 on failure.
 
534
 */
 
535
int SigGroupHeadMpmStreamHashAdd(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
 
536
{
 
537
    int ret = HashListTableAdd(de_ctx->sgh_mpm_stream_hash_table, (void *)sgh, 0);
 
538
 
 
539
    return ret;
 
540
}
 
541
 
 
542
/**
 
543
 * \brief Used to lookup a SigGroupHead from the detection engine context
 
544
 *        SigGroupHead mpm uri hash table.
 
545
 *
 
546
 * \param de_ctx Pointer to the detection engine context.
 
547
 * \param sgh    Pointer to the SigGroupHead.
 
548
 *
 
549
 * \retval rsgh On success a pointer to the SigGroupHead if the SigGroupHead is
 
550
 *              found in the hash table; NULL on failure.
 
551
 */
 
552
SigGroupHead *SigGroupHeadMpmStreamHashLookup(DetectEngineCtx *de_ctx,
 
553
                                           SigGroupHead *sgh)
 
554
{
 
555
    SigGroupHead *rsgh = HashListTableLookup(de_ctx->sgh_mpm_stream_hash_table,
 
556
                                             (void *)sgh, 0);
 
557
 
 
558
    return rsgh;
 
559
}
 
560
 
 
561
/**
 
562
 * \brief Frees the hash table - DetectEngineCtx->sgh_mpm_uri_hash_table,
 
563
 *        allocated by SigGroupHeadMpmUriHashInit() function.
 
564
 *
 
565
 * \param de_ctx Pointer to the detection engine context.
 
566
 */
 
567
void SigGroupHeadMpmStreamHashFree(DetectEngineCtx *de_ctx)
 
568
{
 
569
    if (de_ctx->sgh_mpm_stream_hash_table == NULL)
 
570
        return;
 
571
 
 
572
    HashListTableFree(de_ctx->sgh_mpm_stream_hash_table);
 
573
    de_ctx->sgh_mpm_stream_hash_table = NULL;
 
574
 
 
575
    return;
 
576
}
 
577
 
 
578
/**
433
579
 * \brief The hash function to be the used by the hash table -
434
580
 *        DetectEngineCtx->sgh_hash_table.
435
581
 *
447
593
 
448
594
    SCLogDebug("hashing sgh %p (mpm_content_maxlen %u)", sgh, sgh->mpm_content_maxlen);
449
595
 
450
 
    for (b = 0; b < sgh->sig_size; b++)
451
 
        hash += sgh->sig_array[b];
 
596
    for (b = 0; b < sgh->init->sig_size; b++)
 
597
        hash += sgh->init->sig_array[b];
452
598
 
453
599
    hash %= ht->array_size;
454
 
    SCLogDebug("hash %"PRIu32" (sig_size %"PRIu32")", hash, sgh->sig_size);
 
600
    SCLogDebug("hash %"PRIu32" (sig_size %"PRIu32")", hash, sgh->init->sig_size);
455
601
    return hash;
456
602
}
457
603
 
476
622
    if (data1 == NULL || data2 == NULL)
477
623
        return 0;
478
624
 
479
 
    if (sgh1->sig_size != sgh2->sig_size)
 
625
    if (sgh1->init->sig_size != sgh2->init->sig_size)
480
626
        return 0;
481
627
 
482
 
    if (memcmp(sgh1->sig_array, sgh2->sig_array, sgh1->sig_size) != 0)
 
628
    if (memcmp(sgh1->init->sig_array, sgh2->init->sig_array, sgh1->init->sig_size) != 0)
483
629
        return 0;
484
630
 
485
631
    return 1;
738
884
 
739
885
    for (htb = HashListTableGetListHead(ht);
740
886
         htb != NULL;
741
 
         htb = HashListTableGetListNext(htb)) {
 
887
         htb = HashListTableGetListNext(htb))
 
888
    {
742
889
        sgh = (SigGroupHead *)HashListTableGetListData(htb);
743
890
 
744
 
        if (sgh->sig_array != NULL) {
 
891
        if (sgh->init->sig_array != NULL) {
745
892
            detect_siggroup_sigarray_free_cnt++;
746
 
            detect_siggroup_sigarray_memory -= sgh->sig_size;
 
893
            detect_siggroup_sigarray_memory -= sgh->init->sig_size;
747
894
 
748
 
            SCFree(sgh->sig_array);
749
 
            sgh->sig_array = NULL;
750
 
            sgh->sig_size = 0;
 
895
            SCFree(sgh->init->sig_array);
 
896
            sgh->init->sig_array = NULL;
 
897
            sgh->init->sig_size = 0;
751
898
        }
752
899
 
753
900
        if (sgh->init != NULL) {
777
924
         htb = HashListTableGetListNext(htb)) {
778
925
        sgh = (SigGroupHead *)HashListTableGetListData(htb);
779
926
 
780
 
        if (sgh->sig_array != NULL) {
781
 
            detect_siggroup_sigarray_free_cnt++;
782
 
            detect_siggroup_sigarray_memory -= sgh->sig_size;
783
 
 
784
 
            SCFree(sgh->sig_array);
785
 
            sgh->sig_array = NULL;
786
 
            sgh->sig_size = 0;
787
 
        }
788
 
 
789
927
        if (sgh->init != NULL) {
790
928
            SigGroupHeadInitDataFree(sgh->init);
791
929
            sgh->init = NULL;
859
997
 
860
998
    /* see if we have a head already */
861
999
    if (*sgh == NULL) {
862
 
        *sgh = SigGroupHeadAlloc(DetectEngineGetMaxSigId(de_ctx) / 8 + 1);
 
1000
        *sgh = SigGroupHeadAlloc(de_ctx, DetectEngineGetMaxSigId(de_ctx) / 8 + 1);
863
1001
        if (*sgh == NULL)
864
1002
            goto error;
865
1003
    }
866
1004
 
867
1005
    /* enable the sig in the bitarray */
868
 
    (*sgh)->sig_array[s->num / 8] |= 1 << (s->num % 8);
 
1006
    (*sgh)->init->sig_array[s->num / 8] |= 1 << (s->num % 8);
869
1007
 
870
1008
    /* update maxlen for mpm */
871
1009
    if (s->flags & SIG_FLAG_MPM) {
880
1018
            SCLogDebug("(%p)->mpm_content_maxlen %u", *sgh, (*sgh)->mpm_content_maxlen);
881
1019
        }
882
1020
    }
883
 
    if (s->flags & SIG_FLAG_MPM) {
 
1021
    if (s->flags & SIG_FLAG_MPM_URI) {
884
1022
        if (s->mpm_uricontent_maxlen > 0) {
885
1023
            if ((*sgh)->mpm_uricontent_maxlen == 0)
886
1024
                (*sgh)->mpm_uricontent_maxlen = s->mpm_uricontent_maxlen;
909
1047
    if (sgh == NULL)
910
1048
        return 0;
911
1049
 
912
 
    if (sgh->sig_array != NULL)
913
 
        memset(sgh->sig_array, 0, sgh->sig_size);
 
1050
    if (sgh->init->sig_array != NULL)
 
1051
        memset(sgh->init->sig_array, 0, sgh->init->sig_size);
914
1052
 
915
1053
    sgh->sig_cnt = 0;
916
1054
 
936
1074
        return 0;
937
1075
 
938
1076
    if (*dst == NULL) {
939
 
        *dst = SigGroupHeadAlloc(DetectEngineGetMaxSigId(de_ctx) / 8 + 1);
 
1077
        *dst = SigGroupHeadAlloc(de_ctx, DetectEngineGetMaxSigId(de_ctx) / 8 + 1);
940
1078
        if (*dst == NULL)
941
1079
            goto error;
942
1080
    }
943
1081
 
944
1082
    /* do the copy */
945
 
    for (idx = 0; idx < src->sig_size; idx++)
946
 
        (*dst)->sig_array[idx] = (*dst)->sig_array[idx] | src->sig_array[idx];
 
1083
    for (idx = 0; idx < src->init->sig_size; idx++)
 
1084
        (*dst)->init->sig_array[idx] = (*dst)->init->sig_array[idx] | src->init->sig_array[idx];
947
1085
 
948
1086
    if (src->mpm_content_maxlen != 0) {
949
1087
        if ((*dst)->mpm_content_maxlen == 0)
983
1121
 
984
1122
    sgh->sig_cnt = 0;
985
1123
    for (sig = 0; sig < max_idx + 1; sig++) {
986
 
        if (sgh->sig_array[sig / 8] & (1 << (sig % 8)))
 
1124
        if (sgh->init->sig_array[sig / 8] & (1 << (sig % 8)))
987
1125
            sgh->sig_cnt++;
988
1126
    }
989
1127
 
1064
1202
    uint32_t u;
1065
1203
 
1066
1204
    SCLogDebug("The Signatures present in this SigGroupHead are: ");
1067
 
    for (u = 0; u < (sgh->sig_size * 8); u++) {
1068
 
        if (sgh->sig_array[u / 8] & (1 << (u % 8))) {
 
1205
    for (u = 0; u < (sgh->init->sig_size * 8); u++) {
 
1206
        if (sgh->init->sig_array[u / 8] & (1 << (u % 8))) {
1069
1207
            SCLogDebug("%" PRIu32, u);
1070
1208
            printf("s->num %"PRIu16" ", u);
1071
1209
        }
1140
1278
    Signature *s = NULL;
1141
1279
    SigMatch *sm = NULL;
1142
1280
    uint32_t sig = 0;
1143
 
    SigIntId num = 0;
1144
1281
    DetectContentData *co = NULL;
1145
1282
 
1146
1283
    if (sgh == NULL)
1159
1296
    memset(sgh->init->content_array,0, sgh->init->content_size);
1160
1297
 
1161
1298
    for (sig = 0; sig < sgh->sig_cnt; sig++) {
1162
 
        num = sgh->match_array[sig];
1163
 
 
1164
 
        s = de_ctx->sig_array[num];
 
1299
        s = sgh->match_array[sig];
1165
1300
        if (s == NULL)
1166
1301
            continue;
1167
1302
 
1168
1303
        if (!(s->flags & SIG_FLAG_MPM))
1169
1304
            continue;
1170
1305
 
 
1306
        if (s->alproto != ALPROTO_UNKNOWN)
 
1307
            continue;
 
1308
 
1171
1309
        sm = s->pmatch;
1172
1310
        if (sm == NULL)
1173
1311
            continue;
1223
1361
    Signature *s = NULL;
1224
1362
    SigMatch *sm = NULL;
1225
1363
    uint32_t sig = 0;
1226
 
    SigIntId num = 0;
1227
1364
    DetectUricontentData *co = NULL;
1228
1365
 
1229
1366
    if (sgh == NULL)
1242
1379
    memset(sgh->init->uri_content_array, 0, sgh->init->uri_content_size);
1243
1380
 
1244
1381
    for (sig = 0; sig < sgh->sig_cnt; sig++) {
1245
 
        num = sgh->match_array[sig];
 
1382
        s = sgh->match_array[sig];
1246
1383
 
1247
 
        s = de_ctx->sig_array[num];
1248
1384
        if (s == NULL)
1249
1385
            continue;
1250
1386
 
1291
1427
}
1292
1428
 
1293
1429
/**
 
1430
 * \brief Loads all the content ids from all the contents belonging to all the
 
1431
 *        Signatures in this SigGroupHead, into a bitarray.  A fast and an
 
1432
 *        efficient way of comparing pattern sets.
 
1433
 *
 
1434
 * \param de_ctx Pointer to the detection engine context.
 
1435
 * \param sgh    Pointer to the SigGroupHead.
 
1436
 *
 
1437
 * \retval  0 On success, i.e. on either the detection engine context being NULL
 
1438
 *            or on succesfully allocating memory and updating it with relevant
 
1439
 *            data.
 
1440
 * \retval -1 On failure.
 
1441
 */
 
1442
int SigGroupHeadLoadStreamContent(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
 
1443
{
 
1444
    Signature *s = NULL;
 
1445
    SigMatch *sm = NULL;
 
1446
    uint32_t sig = 0;
 
1447
    DetectContentData *co = NULL;
 
1448
 
 
1449
    if (sgh == NULL)
 
1450
        return 0;
 
1451
 
 
1452
    if (DetectContentMaxId(de_ctx) == 0)
 
1453
        return 0;
 
1454
 
 
1455
    BUG_ON(sgh->init == NULL);
 
1456
 
 
1457
    sgh->init->stream_content_size = (DetectContentMaxId(de_ctx) / 8) + 1;
 
1458
    sgh->init->stream_content_array = SCMalloc(sgh->init->stream_content_size);
 
1459
    if (sgh->init->stream_content_array == NULL)
 
1460
        return -1;
 
1461
 
 
1462
    memset(sgh->init->stream_content_array,0, sgh->init->stream_content_size);
 
1463
 
 
1464
    for (sig = 0; sig < sgh->sig_cnt; sig++) {
 
1465
        s = sgh->match_array[sig];
 
1466
        if (s == NULL)
 
1467
            continue;
 
1468
 
 
1469
        if (!(s->flags & SIG_FLAG_MPM))
 
1470
            continue;
 
1471
 
 
1472
        if (s->flags & SIG_FLAG_DSIZE)
 
1473
            continue;
 
1474
 
 
1475
        sm = s->pmatch;
 
1476
        if (sm == NULL)
 
1477
            continue;
 
1478
 
 
1479
        for ( ;sm != NULL; sm = sm->next) {
 
1480
            if (sm->type == DETECT_CONTENT) {
 
1481
                co = (DetectContentData *)sm->ctx;
 
1482
 
 
1483
                sgh->init->stream_content_array[co->id / 8] |= 1 << (co->id % 8);
 
1484
            }
 
1485
        }
 
1486
    }
 
1487
 
 
1488
    return 0;
 
1489
}
 
1490
 
 
1491
/**
 
1492
 * \brief Clears the memory allocated by SigGroupHeadLoadContent() for the
 
1493
 *        bitarray to hold the content ids for a SigGroupHead.
 
1494
 *
 
1495
 * \param Pointer to the SigGroupHead whose content_array would to be cleared.
 
1496
 *
 
1497
 * \ret 0 Always.
 
1498
 */
 
1499
int SigGroupHeadClearStreamContent(SigGroupHead *sh)
 
1500
{
 
1501
    if (sh == NULL)
 
1502
        return 0;
 
1503
 
 
1504
    if (sh->init->stream_content_array != NULL) {
 
1505
        SCFree(sh->init->stream_content_array);
 
1506
        sh->init->stream_content_array = NULL;
 
1507
        sh->init->stream_content_size = 0;
 
1508
    }
 
1509
    return 0;
 
1510
}
 
1511
 
 
1512
/**
1294
1513
 * \brief Create an array with all the internal ids of the sigs that this
1295
1514
 *        sig group head will check for.
1296
1515
 *
1313
1532
 
1314
1533
    BUG_ON(sgh->match_array != NULL);
1315
1534
 
1316
 
    sgh->match_array = SCMalloc(sgh->sig_cnt * sizeof(SigIntId));
 
1535
    sgh->match_array = SCMalloc(sgh->sig_cnt * sizeof(Signature *));
1317
1536
    if (sgh->match_array == NULL)
1318
1537
        return -1;
1319
1538
 
1320
 
    memset(sgh->match_array,0, sgh->sig_cnt * sizeof(SigIntId));
 
1539
    memset(sgh->match_array,0, sgh->sig_cnt * sizeof(Signature *));
1321
1540
 
1322
1541
    detect_siggroup_matcharray_init_cnt++;
1323
 
    detect_siggroup_matcharray_memory += (sgh->sig_cnt * sizeof(SigIntId));
 
1542
    detect_siggroup_matcharray_memory += (sgh->sig_cnt * sizeof(Signature *));
1324
1543
 
1325
1544
    for (sig = 0; sig < max_idx + 1; sig++) {
1326
 
        if ( !(sgh->sig_array[(sig / 8)] & (1 << (sig % 8))) )
 
1545
        if (!(sgh->init->sig_array[(sig / 8)] & (1 << (sig % 8))) )
1327
1546
            continue;
1328
1547
 
1329
1548
        s = de_ctx->sig_array[sig];
1330
1549
        if (s == NULL)
1331
1550
            continue;
1332
1551
 
1333
 
        sgh->match_array[idx] = s->num;
 
1552
        sgh->match_array[idx] = s;
 
1553
        idx++;
 
1554
    }
 
1555
 
 
1556
    return 0;
 
1557
}
 
1558
 
 
1559
int SigGroupHeadBuildHeadArray(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
 
1560
{
 
1561
    Signature *s = NULL;
 
1562
    uint32_t idx = 0;
 
1563
    uint32_t sig = 0;
 
1564
 
 
1565
    if (sgh == NULL)
 
1566
        return 0;
 
1567
 
 
1568
    BUG_ON(sgh->head_array != NULL);
 
1569
 
 
1570
    sgh->head_array = SCMalloc(sgh->sig_cnt * sizeof(SignatureHeader));
 
1571
    if (sgh->head_array == NULL)
 
1572
        return -1;
 
1573
 
 
1574
    memset(sgh->head_array, 0, sgh->sig_cnt * sizeof(SignatureHeader));
 
1575
 
 
1576
    detect_siggroup_matcharray_init_cnt++;
 
1577
    detect_siggroup_matcharray_memory += (sgh->sig_cnt * sizeof(SignatureHeader *));
 
1578
 
 
1579
    for (sig = 0; sig < sgh->sig_cnt; sig++) {
 
1580
        s = sgh->match_array[sig];
 
1581
        if (s == NULL)
 
1582
            continue;
 
1583
 
 
1584
        sgh->head_array[idx].flags = s->flags;
 
1585
        sgh->head_array[idx].mpm_pattern_id = s->mpm_pattern_id;
 
1586
        sgh->head_array[idx].alproto = s->alproto;
 
1587
        sgh->head_array[idx].num = s->num;
 
1588
        sgh->head_array[idx].full_sig = s;
 
1589
 
 
1590
        BUG_ON(s->flags != sgh->head_array[idx].flags);
 
1591
        BUG_ON(s->alproto != sgh->head_array[idx].alproto);
 
1592
        BUG_ON(s->mpm_pattern_id != sgh->head_array[idx].mpm_pattern_id);
 
1593
        BUG_ON(s->num != sgh->head_array[idx].num);
 
1594
        BUG_ON(s != sgh->head_array[idx].full_sig);
 
1595
 
1334
1596
        idx++;
1335
1597
    }
1336
1598
 
1363
1625
    }
1364
1626
 
1365
1627
    for (sig = 0; sig < max_sid; sig++) {
1366
 
        if (sgh->sig_array == NULL) {
 
1628
        if (sgh->init->sig_array == NULL) {
1367
1629
            SCReturnInt(0);
1368
1630
        }
1369
1631
 
1370
1632
        /* Check if the SigGroupHead has an entry for the sid */
1371
 
        if ( !(sgh->sig_array[sig / 8] & (1 << (sig % 8))) )
 
1633
        if ( !(sgh->init->sig_array[sig / 8] & (1 << (sig % 8))) )
1372
1634
            continue;
1373
1635
 
1374
1636
        /* If we have reached here, we have an entry for sid in the SigGrouHead.
1841
2103
    SigGroupHeadSetSigCnt(sh, 4);
1842
2104
    SigGroupHeadBuildMatchArray(de_ctx, sh, 4);
1843
2105
 
1844
 
    result &= (sh->match_array[0] == 0);
1845
 
    result &= (sh->match_array[1] == 2);
1846
 
    result &= (sh->match_array[2] == 4);
 
2106
    result &= (sh->match_array[0] == de_ctx->sig_list);
 
2107
    result &= (sh->match_array[1] == de_ctx->sig_list->next->next);
 
2108
    result &= (sh->match_array[2] == de_ctx->sig_list->next->next->next->next);
1847
2109
 
1848
2110
    SigGroupHeadFree(sh);
1849
2111
 
1892
2154
 
1893
2155
    AddressDebugPrint(&p.dst);
1894
2156
 
1895
 
    SigGroupHead *sgh = SigMatchSignaturesGetSgh(&th_v, de_ctx, det_ctx, &p);
 
2157
    SigGroupHead *sgh = SigMatchSignaturesGetSgh(de_ctx, det_ctx, &p);
1896
2158
    if (sgh == NULL) {
1897
2159
        goto end;
1898
2160
    }