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;
33 } FeatureEntryType;
34 
35 static SCMutex feature_table_mutex = SCMUTEX_INITIALIZER;
36 static HashListTable *feature_hash_table;
37 
FeatureHashFunc(HashListTable * ht,void * data,uint16_t datalen)38 static uint32_t FeatureHashFunc(HashListTable *ht, void *data,
39                                 uint16_t datalen)
40 {
41     FeatureEntryType *f = (FeatureEntryType *)data;
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 
FeatureHashCompareFunc(void * data1,uint16_t datalen1,void * data2,uint16_t datalen2)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 
FeatureHashFreeFunc(void * data)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 
FeatureInit(void)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 
FeatureAddEntry(const char * feature_name)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 
ProvidesFeature(const char * feature_name)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 
RequiresFeature(const char * feature_name)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 
FeatureTrackingRelease(void)134 void FeatureTrackingRelease(void)
135 {
136     if (feature_hash_table != NULL) {
137         HashListTableFree(feature_hash_table);
138         feature_hash_table = NULL;
139     }
140 }
141 
FeatureDump(void)142 void FeatureDump(void)
143 {
144     HashListTableBucket *hb = HashListTableGetListHead(feature_hash_table);
145     for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
146         FeatureEntryType *f = HashListTableGetListData(hb);
147         printf("provided feature name: %s\n", f->feature);
148     }
149 }
FeatureTrackingRegister(void)150 void FeatureTrackingRegister(void)
151 {
152     FeatureInit();
153 }
154