suricata
tm-modules.c
Go to the documentation of this file.
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 
34 {
35  TmModule *t;
36  uint16_t i;
37 
38  for (i = 0; i < TMM_SIZE; i++) {
39  t = &tmm_modules[i];
40 
41  if (t->name == NULL)
42  continue;
43 
44  SCLogDebug("%s:%p", t->name, t->Func);
45  }
46 }
47 
48 /** \brief get a tm module ptr by name
49  * \param name name string
50  * \retval ptr to the module or NULL */
52 {
53  TmModule *t;
54  uint16_t i;
55 
56  for (i = 0; i < TMM_SIZE; i++) {
57  t = &tmm_modules[i];
58 
59  if (t->name == NULL)
60  continue;
61 
62  if (strcmp(t->name, name) == 0)
63  return t;
64  }
65 
66  return NULL;
67 }
68 
69 /** \brief get the id of a module from it's name
70  * \param name registered name of the module
71  * \retval id the id or -1 in case of error */
72 int TmModuleGetIdByName(const char *name)
73 {
74  TmModule *tm = TmModuleGetByName(name);
75  if (tm == NULL)
76  return -1;;
77  return TmModuleGetIDForTM(tm);
78 }
79 
80 /**
81  * \brief Returns a TM Module by its id.
82  *
83  * \param id Id of the TM Module to return.
84  *
85  * \retval Pointer of the module to be returned if available;
86  * NULL if unavailable.
87  */
89 {
90 
91  if (id < 0 || id >= TMM_SIZE) {
92  SCLogError(SC_ERR_TM_MODULES_ERROR, "Threading module with the id "
93  "\"%d\" doesn't exist", id);
94  return NULL;
95  }
96 
97  return &tmm_modules[id];
98 }
99 
100 /**
101  * \brief Given a TM Module, returns its id.
102  *
103  * \param tm Pointer to the TM Module.
104  *
105  * \retval id of the TM Module if available; -1 if unavailable.
106  */
108 {
109  TmModule *t;
110  int i;
111 
112  for (i = 0; i < TMM_SIZE; i++) {
113  t = &tmm_modules[i];
114 
115  if (t->name == NULL)
116  continue;
117 
118  if (strcmp(t->name, tm->name) == 0)
119  return i;
120  }
121 
122  return -1;
123 }
124 
125 
126 void TmModuleRunInit(void)
127 {
128  TmModule *t;
129  uint16_t i;
130 
131  for (i = 0; i < TMM_SIZE; i++) {
132  t = &tmm_modules[i];
133 
134  if (t->name == NULL)
135  continue;
136 
137  if (t->Init == NULL)
138  continue;
139 
140  t->Init();
141  }
142 }
143 
145 {
146  TmModule *t;
147  uint16_t i;
148 
149  for (i = 0; i < TMM_SIZE; i++) {
150  t = &tmm_modules[i];
151 
152  if (t->name == NULL)
153  continue;
154 
155  if (t->DeInit == NULL)
156  continue;
157 
158  t->DeInit();
159  }
160 }
161 
162 /** \brief register all unittests for the tm modules */
164 {
165 #ifdef UNITTESTS
166  TmModule *t;
167  uint16_t i;
168 
169  for (i = 0; i < TMM_SIZE; i++) {
170  t = &tmm_modules[i];
171 
172  if (t->name == NULL)
173  continue;
174 
175  g_ut_modules++;
176 
177 
178  if (t->RegisterTests == NULL) {
179  if (coverage_unittests)
180  SCLogWarning(SC_WARN_NO_UNITTESTS, "threading module %s has no unittest "
181  "registration function.", t->name);
182  } else {
183  t->RegisterTests();
184  g_ut_covered++;
185  }
186  }
187 #endif /* UNITTESTS */
188 }
189 
190 #define CASE_CODE(E) case E: return #E
191 
192 /**
193  * \brief Maps the TmmId, to its string equivalent
194  *
195  * \param id tmm id
196  *
197  * \retval string equivalent for the tmm id
198  */
199 const char * TmModuleTmmIdToString(TmmId id)
200 {
201  switch (id) {
238 
240  }
241  return "<unknown>";
242 }
TmModule * TmModuleGetById(int id)
Returns a TM Module by its id.
Definition: tm-modules.c:88
#define SCLogDebug(...)
Definition: util-debug.h:335
TmEcode(* Func)(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *)
Definition: tm-modules.h:52
pthread_t t
Definition: threadvars.h:58
int g_ut_covered
Definition: suricata.c:861
void TmModuleRunInit(void)
Definition: tm-modules.c:126
TmmId
Thread Model Module id&#39;s.
const char * TmModuleTmmIdToString(TmmId id)
Maps the TmmId, to its string equivalent.
Definition: tm-modules.c:199
#define CASE_CODE(E)
Definition: tm-modules.c:190
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void(* RegisterTests)(void)
Definition: tm-modules.h:65
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
TmEcode(* Init)(void)
Definition: tm-modules.h:62
const char * name
Definition: tm-modules.h:44
int coverage_unittests
Definition: suricata.c:859
int g_ut_modules
Definition: suricata.c:860
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.h:73
int TmModuleGetIDForTM(TmModule *tm)
Given a TM Module, returns its id.
Definition: tm-modules.c:107
void TmModuleRunDeInit(void)
Definition: tm-modules.c:144
int TmModuleGetIdByName(const char *name)
get the id of a module from it&#39;s name
Definition: tm-modules.c:72
char name[16]
Definition: threadvars.h:59
TmEcode(* DeInit)(void)
Definition: tm-modules.h:63
TmModule * TmModuleGetByName(const char *name)
get a tm module ptr by name
Definition: tm-modules.c:51
void TmModuleRegisterTests(void)
register all unittests for the tm modules
Definition: tm-modules.c:163
void TmModuleDebugList(void)
Definition: tm-modules.c:33