suricata
feature.c
Go to the documentation of this file.
1/* Copyright (C) 2019 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 Jeff Lucovsky <jeff@lucovsky.org>
22 *
23 * Implements feature tracking
24 */
25
26#include "suricata-common.h"
27#include "feature.h"
28#include "threads.h"
29
30#include "util-debug.h"
31#include "util-hashlist.h"
32
33typedef struct FeatureEntryType {
34 const char *feature;
36
37static SCMutex feature_table_mutex = SCMUTEX_INITIALIZER;
38static HashListTable *feature_hash_table;
39
40static uint32_t FeatureHashFunc(HashListTable *ht, void *data,
41 uint16_t datalen)
42{
44 uint32_t hash = 0;
45 size_t len = strlen(f->feature);
46
47 for (size_t i = 0; i < len; i++)
48 hash += u8_tolower((unsigned char)f->feature[i]);
49
50 return (hash % ht->array_size);
51}
52
53static char FeatureHashCompareFunc(void *data1, uint16_t datalen1,
54 void *data2, uint16_t datalen2)
55{
58 size_t len1 = 0;
59 size_t len2 = 0;
60
61 if (f1 == NULL || f2 == NULL)
62 return 0;
63
64 if (f1->feature == NULL || f2->feature == NULL)
65 return 0;
66
67 len1 = strlen(f1->feature);
68 len2 = strlen(f2->feature);
69
70 return (len1 == len2 && memcmp(f1->feature, f2->feature, len1) == 0);
71}
72
73static void FeatureHashFreeFunc(void *data)
74{
75 FeatureEntryType *f = data;
76 if (f->feature) {
77 SCFree((void *)f->feature);
78 }
79 SCFree(data);
80}
81
82static void FeatureInit(void) {
83 feature_hash_table = HashListTableInit(256, FeatureHashFunc,
84 FeatureHashCompareFunc,
85 FeatureHashFreeFunc);
86
87 if (!feature_hash_table) {
88 FatalError("Unable to allocate feature hash table.");
89 }
90}
91
92static void FeatureAddEntry(const char *feature_name)
93{
94 int rc;
95
96 FeatureEntryType *feature = SCCalloc(1, sizeof(*feature));
97 if (!feature) {
98 FatalError("Unable to allocate feature entry memory.");
99 }
100
101 feature->feature = SCStrdup(feature_name);
102 if (feature->feature) {
103 rc = HashListTableAdd(feature_hash_table, feature, sizeof(*feature));
104 if (rc == 0)
105 return;
106 }
107
108 FeatureHashFreeFunc(feature);
109}
110
111void ProvidesFeature(const char *feature_name)
112{
113 FeatureEntryType f = { feature_name };
114
115 SCMutexLock(&feature_table_mutex);
116
117 FeatureEntryType *feature = HashListTableLookup(feature_hash_table, &f, sizeof(f));
118
119 if (!feature) {
120 FeatureAddEntry(feature_name);
121 }
122
123 SCMutexUnlock(&feature_table_mutex);
124}
125
126bool RequiresFeature(const char *feature_name)
127{
128 FeatureEntryType f = { feature_name };
129
130 SCMutexLock(&feature_table_mutex);
131 FeatureEntryType *feature = HashListTableLookup(feature_hash_table, &f, sizeof(f));
132 SCMutexUnlock(&feature_table_mutex);
133 return feature != NULL;
134}
135
137{
138 if (feature_hash_table != NULL) {
139 HashListTableFree(feature_hash_table);
140 feature_hash_table = NULL;
141 }
142}
143
144void FeatureDump(void)
145{
146 HashListTableBucket *hb = HashListTableGetListHead(feature_hash_table);
147 for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
149 printf("provided feature name: %s\n", f->feature);
150 }
151}
153{
154 FeatureInit();
155}
uint8_t len
void FeatureDump(void)
Definition feature.c:144
void ProvidesFeature(const char *feature_name)
Definition feature.c:111
void FeatureTrackingRegister(void)
Definition feature.c:152
bool RequiresFeature(const char *feature_name)
Definition feature.c:126
void FeatureTrackingRelease(void)
Definition feature.c:136
const char * feature
Definition feature.c:34
uint32_t array_size
#define u8_tolower(c)
#define SCMUTEX_INITIALIZER
#define SCMutex
#define SCMutexUnlock(mut)
#define SCMutexLock(mut)
#define FatalError(...)
Definition util-debug.h:510
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
HashListTable * HashListTableInit(uint32_t size, uint32_t(*Hash)(struct HashListTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
void HashListTableFree(HashListTable *ht)
#define HashListTableGetListData(hb)
#define HashListTableGetListNext(hb)
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define SCStrdup(s)
Definition util-mem.h:56