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  int len = strlen(f->feature);
46 
47  for (int 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  int len1 = 0;
59  int 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 
73 static void FeatureHashFreeFunc(void *data)
74 {
75  FeatureEntryType *f = data;
76  if (f->feature) {
77  SCFree((void *)f->feature);
78  }
79  SCFree(data);
80 }
81 
82 static 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 
92 static 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 
111 void 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 
126 bool 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 
144 void 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 }
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:436
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
FeatureDump
void FeatureDump(void)
Definition: feature.c:144
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:152
FeatureEntryType
Definition: feature.c:33
RequiresFeature
bool RequiresFeature(const char *feature_name)
Definition: feature.c:126
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:119
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:136
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:502
SCFree
#define SCFree(p)
Definition: util-mem.h:61
HashListTableBucket_
Definition: util-hashlist.h:28
ProvidesFeature
void ProvidesFeature(const char *feature_name)
Definition: feature.c:111
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCMutex
#define SCMutex
Definition: threads-debug.h:114