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 
33 typedef struct FeatureEntryType {
34  const char *feature;
36 
37 static SCMutex feature_table_mutex = SCMUTEX_INITIALIZER;
38 static HashListTable *feature_hash_table;
39 
40 static 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 
53 static char FeatureHashCompareFunc(void *data1, uint16_t datalen1,
54  void *data2, uint16_t datalen2)
55 {
56  FeatureEntryType *f1 = (FeatureEntryType *)data1;
57  FeatureEntryType *f2 = (FeatureEntryType *)data2;
58 
59  if (f1 == NULL || f2 == NULL)
60  return 0;
61 
62  if (f1->feature == NULL || f2->feature == NULL)
63  return 0;
64 
65  return strcmp(f1->feature, f2->feature) == 0;
66 }
67 
68 static void FeatureHashFreeFunc(void *data)
69 {
70  FeatureEntryType *f = data;
71  if (f->feature) {
72  SCFree((void *)f->feature);
73  }
74  SCFree(data);
75 }
76 
77 static void FeatureInit(void) {
78  feature_hash_table = HashListTableInit(256, FeatureHashFunc,
79  FeatureHashCompareFunc,
80  FeatureHashFreeFunc);
81 
82  if (!feature_hash_table) {
83  FatalError("Unable to allocate feature hash table.");
84  }
85 }
86 
87 static void FeatureAddEntry(const char *feature_name)
88 {
89  int rc;
90 
91  FeatureEntryType *feature = SCCalloc(1, sizeof(*feature));
92  if (!feature) {
93  FatalError("Unable to allocate feature entry memory.");
94  }
95 
96  feature->feature = SCStrdup(feature_name);
97  if (feature->feature) {
98  rc = HashListTableAdd(feature_hash_table, feature, sizeof(*feature));
99  if (rc == 0)
100  return;
101  }
102 
103  FeatureHashFreeFunc(feature);
104 }
105 
106 void ProvidesFeature(const char *feature_name)
107 {
108  FeatureEntryType f = { feature_name };
109 
110  SCMutexLock(&feature_table_mutex);
111 
112  FeatureEntryType *feature = HashListTableLookup(feature_hash_table, &f, sizeof(f));
113 
114  if (!feature) {
115  FeatureAddEntry(feature_name);
116  }
117 
118  SCMutexUnlock(&feature_table_mutex);
119 }
120 
121 bool SCRequiresFeature(const char *feature_name)
122 {
123  FeatureEntryType f = { feature_name };
124 
125  SCMutexLock(&feature_table_mutex);
126  FeatureEntryType *feature = HashListTableLookup(feature_hash_table, &f, sizeof(f));
127  SCMutexUnlock(&feature_table_mutex);
128  return feature != NULL;
129 }
130 
132 {
133  if (feature_hash_table != NULL) {
134  HashListTableFree(feature_hash_table);
135  feature_hash_table = NULL;
136  }
137 }
138 
139 void FeatureDump(void)
140 {
141  HashListTableBucket *hb = HashListTableGetListHead(feature_hash_table);
142  for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
144  printf("provided feature name: %s\n", f->feature);
145  }
146 }
148 {
149  FeatureInit();
150 }
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
len
uint8_t len
Definition: app-layer-dnp3.h:2
util-hashlist.h
threads.h
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
u8_tolower
#define u8_tolower(c)
Definition: suricata-common.h:453
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
FeatureDump
void FeatureDump(void)
Definition: feature.c:139
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:114
HashListTable_::array_size
uint32_t array_size
Definition: util-hashlist.h:41
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
FeatureTrackingRegister
void FeatureTrackingRegister(void)
Definition: feature.c:147
FeatureEntryType
Definition: feature.c:33
HashListTableInit
HashListTable * HashListTableInit(uint32_t size, uint32_t(*Hash)(struct HashListTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hashlist.c:35
feature.h
util-debug.h
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
FeatureEntryType::feature
const char * feature
Definition: feature.c:34
HashListTable_
Definition: util-hashlist.h:37
FeatureEntryType
struct FeatureEntryType FeatureEntryType
FeatureTrackingRelease
void FeatureTrackingRelease(void)
Definition: feature.c:131
suricata-common.h
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:517
SCFree
#define SCFree(p)
Definition: util-mem.h:61
HashListTableBucket_
Definition: util-hashlist.h:28
SCRequiresFeature
bool SCRequiresFeature(const char *feature_name)
Definition: feature.c:121
ProvidesFeature
void ProvidesFeature(const char *feature_name)
Definition: feature.c:106
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCMutex
#define SCMutex
Definition: threads-debug.h:114