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