1 /* Copyright (C) 2007-2010 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 Victor Julien <victor@inliniac.net>
22  *
23  * Thread Module functions
24  */
25 
26 #include "suricata-common.h"
27 #include "packet-queue.h"
28 #include "tm-threads.h"
29 #include "util-debug.h"
30 #include "threads.h"
31 #include "util-logopenfile.h"
32 
33 TmModule tmm_modules[TMM_SIZE];
34 
TmModuleDebugList(void)35 void TmModuleDebugList(void)
36 {
37     TmModule *t;
38     uint16_t i;
39 
40     for (i = 0; i < TMM_SIZE; i++) {
41         t = &tmm_modules[i];
42 
43         if (t->name == NULL)
44             continue;
45 
46         SCLogDebug("%s:%p", t->name, t->Func);
47     }
48 }
49 
50 /** \brief get a tm module ptr by name
51  *  \param name name string
52  *  \retval ptr to the module or NULL */
TmModuleGetByName(const char * name)53 TmModule *TmModuleGetByName(const char *name)
54 {
55     TmModule *t;
56     uint16_t i;
57 
58     for (i = 0; i < TMM_SIZE; i++) {
59         t = &tmm_modules[i];
60 
61         if (t->name == NULL)
62             continue;
63 
64         if (strcmp(t->name, name) == 0)
65             return t;
66     }
67 
68     return NULL;
69 }
70 
71 /** \brief get the id of a module from it's name
72  *  \param name registered name of the module
73  *  \retval id the id or -1 in case of error */
TmModuleGetIdByName(const char * name)74 int TmModuleGetIdByName(const char *name)
75 {
76     TmModule *tm = TmModuleGetByName(name);
77     if (tm == NULL)
78         return -1;;
79     return TmModuleGetIDForTM(tm);
80 }
81 
82 /**
83  * \brief Returns a TM Module by its id.
84  *
85  * \param id Id of the TM Module to return.
86  *
87  * \retval Pointer of the module to be returned if available;
88  *         NULL if unavailable.
89  */
TmModuleGetById(int id)90 TmModule *TmModuleGetById(int id)
91 {
92 
93     if (id < 0 || id >= TMM_SIZE) {
94         SCLogError(SC_ERR_TM_MODULES_ERROR, "Threading module with the id "
95                    "\"%d\" doesn't exist", id);
96         return NULL;
97     }
98 
99     return &tmm_modules[id];
100 }
101 
102 /**
103  * \brief Given a TM Module, returns its id.
104  *
105  * \param tm Pointer to the TM Module.
106  *
107  * \retval id of the TM Module if available; -1 if unavailable.
108  */
TmModuleGetIDForTM(TmModule * tm)109 int TmModuleGetIDForTM(TmModule *tm)
110 {
111     TmModule *t;
112     int i;
113 
114     for (i = 0; i < TMM_SIZE; i++) {
115         t = &tmm_modules[i];
116 
117         if (t->name == NULL)
118             continue;
119 
120         if (strcmp(t->name, tm->name) == 0)
121             return i;
122     }
123 
124     return -1;
125 }
126 
127 
TmModuleRunInit(void)128 void TmModuleRunInit(void)
129 {
130     TmModule *t;
131     uint16_t i;
132 
133     for (i = 0; i < TMM_SIZE; i++) {
134         t = &tmm_modules[i];
135 
136         if (t->name == NULL)
137             continue;
138 
139         if (t->Init == NULL)
140             continue;
141 
142         t->Init();
143     }
144 }
145 
TmModuleRunDeInit(void)146 void TmModuleRunDeInit(void)
147 {
148     TmModule *t;
149     uint16_t i;
150 
151     for (i = 0; i < TMM_SIZE; i++) {
152         t = &tmm_modules[i];
153 
154         if (t->name == NULL)
155             continue;
156 
157         if (t->DeInit == NULL)
158             continue;
159 
160         t->DeInit();
161     }
162 }
163 
164 /** \brief register all unittests for the tm modules */
TmModuleRegisterTests(void)165 void TmModuleRegisterTests(void)
166 {
167 #ifdef UNITTESTS
168     TmModule *t;
169     uint16_t i;
170 
171     for (i = 0; i < TMM_SIZE; i++) {
172         t = &tmm_modules[i];
173 
174         if (t->name == NULL)
175             continue;
176 
177         g_ut_modules++;
178 
179 
180         if (t->RegisterTests == NULL) {
181             if (coverage_unittests)
182                 SCLogWarning(SC_WARN_NO_UNITTESTS, "threading module %s has no unittest "
183                         "registration function.", t->name);
184         } else {
185             t->RegisterTests();
186             g_ut_covered++;
187         }
188     }
189 #endif /* UNITTESTS */
190 }
191 
192 #define CASE_CODE(E)  case E: return #E
193 
194 /**
195  * \brief Maps the TmmId, to its string equivalent
196  *
197  * \param id tmm id
198  *
199  * \retval string equivalent for the tmm id
200  */
TmModuleTmmIdToString(TmmId id)201 const char * TmModuleTmmIdToString(TmmId id)
202 {
203     switch (id) {
204         CASE_CODE (TMM_FLOWWORKER);
205         CASE_CODE (TMM_RECEIVENFLOG);
206         CASE_CODE (TMM_DECODENFLOG);
207         CASE_CODE (TMM_DECODENFQ);
208         CASE_CODE (TMM_VERDICTNFQ);
209         CASE_CODE (TMM_RECEIVENFQ);
210         CASE_CODE (TMM_RECEIVEPCAP);
211         CASE_CODE (TMM_RECEIVEPCAPFILE);
212         CASE_CODE (TMM_DECODEPCAP);
213         CASE_CODE (TMM_DECODEPCAPFILE);
214         CASE_CODE (TMM_RECEIVEPFRING);
215         CASE_CODE (TMM_DECODEPFRING);
216         CASE_CODE (TMM_RECEIVEPLUGIN);
217         CASE_CODE (TMM_DECODEPLUGIN);
218         CASE_CODE (TMM_RESPONDREJECT);
219         CASE_CODE (TMM_DECODEIPFW);
220         CASE_CODE (TMM_VERDICTIPFW);
221         CASE_CODE (TMM_RECEIVEIPFW);
222         CASE_CODE (TMM_RECEIVEERFFILE);
223         CASE_CODE (TMM_DECODEERFFILE);
224         CASE_CODE (TMM_RECEIVEERFDAG);
225         CASE_CODE (TMM_DECODEERFDAG);
226         CASE_CODE (TMM_RECEIVENAPATECH);
227         CASE_CODE (TMM_DECODENAPATECH);
228         CASE_CODE (TMM_RECEIVEAFP);
229         CASE_CODE (TMM_ALERTPCAPINFO);
230         CASE_CODE (TMM_DECODEAFP);
231         CASE_CODE (TMM_STATSLOGGER);
232         CASE_CODE (TMM_FLOWMANAGER);
233         CASE_CODE (TMM_FLOWRECYCLER);
234         CASE_CODE (TMM_BYPASSEDFLOWMANAGER);
235         CASE_CODE (TMM_UNIXMANAGER);
236         CASE_CODE (TMM_DETECTLOADER);
237         CASE_CODE (TMM_RECEIVENETMAP);
238         CASE_CODE (TMM_DECODENETMAP);
239         CASE_CODE (TMM_RECEIVEWINDIVERT);
240         CASE_CODE (TMM_VERDICTWINDIVERT);
241         CASE_CODE (TMM_DECODEWINDIVERT);
242 
243         CASE_CODE (TMM_SIZE);
244     }
245     return "<unknown>";
246 }
247