]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/stress/stress.cpp
Code deduplication (plugin creation via template PLUGIN_CREATOR)
[stg.git] / projects / stargazer / plugins / authorization / stress / stress.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
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  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.5 $
23  $Date: 2009/06/19 12:50:32 $
24  $Author: faust $
25  */
26
27 #include <stdio.h>
28 #include <unistd.h>
29
30 #include <csignal>
31 #include <algorithm>
32
33 #include "stg/user.h"
34 #include "stg/common.h"
35 #include "stg/user_property.h"
36 #include "stg/plugin_creator.h"
37
38 #include "stress.h"
39
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 PLUGIN_CREATOR<AUTH_STRESS> stressc;
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
48 template <typename varType>
49 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
50 {
51 public:
52     bool operator()(varType notifier, USER_PTR user) const
53         {
54         return notifier.GetUser() == user;
55         };
56 };
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 PLUGIN * GetPlugin()
61 {
62 //printf("BASE_CAPTURER * GetCapturer()\n");
63 return stressc.GetPlugin();
64 }
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS()
69     : averageOnlineTime(0)
70 {
71 }
72 //-----------------------------------------------------------------------------
73 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
74 {
75 PARAM_VALUE pv;
76 vector<PARAM_VALUE>::const_iterator pvi;
77
78 pv.param = "AverageOnlineTime";
79 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
81     {
82     errorStr = "Parameter \'" + pv.param + "\' not found.";
83     return -1;
84     }
85
86 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
87     {
88     errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
89     return -1;
90     }
91
92 return 0;
93 }
94 //-----------------------------------------------------------------------------
95 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
96 {
97 return averageOnlineTime;
98 }
99 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 const string AUTH_STRESS::GetVersion() const
103 {
104 return "Stress authorizator v.0.1";
105 }
106 //-----------------------------------------------------------------------------
107 AUTH_STRESS::AUTH_STRESS()
108 {
109 pthread_mutex_init(&mutex, NULL);
110 isRunning = false;
111 }
112 //-----------------------------------------------------------------------------
113 void AUTH_STRESS::SetUsers(USERS * u)
114 {
115 users = u;
116 }
117 //-----------------------------------------------------------------------------
118 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
119 {
120 settings = s;
121 }
122 //-----------------------------------------------------------------------------
123 int AUTH_STRESS::ParseSettings()
124 {
125 int ret = stressSettings.ParseSettings(settings);
126 if (ret)
127     errorStr = stressSettings.GetStrError();
128 return ret;
129 }
130 //-----------------------------------------------------------------------------
131 const string & AUTH_STRESS::GetStrError() const
132 {
133 return errorStr;
134 }
135 //-----------------------------------------------------------------------------
136 int AUTH_STRESS::Start()
137 {
138 GetUsers();
139 nonstop = true;
140
141 list<USER_PTR>::iterator users_iter;
142
143 onAddUserNotifier.SetAuthorizator(this);
144 onDelUserNotifier.SetAuthorizator(this);
145 users->AddNotifierUserAdd(&onAddUserNotifier);
146 users->AddNotifierUserDel(&onDelUserNotifier);
147
148 if (!isRunning)
149     {
150     if (pthread_create(&thread, NULL, Run, this))
151         {
152         errorStr = "Cannot create thread.";
153         return -1;
154         }
155     }
156
157 users_iter = usersList.begin();
158 while (users_iter != usersList.end())
159     {
160     Authorize(*users_iter);
161     users_iter++;
162     }
163
164 //isRunning = true;
165 return 0;
166 }
167 //-----------------------------------------------------------------------------
168 int AUTH_STRESS::Stop()
169 {
170 nonstop = false;
171 if (isRunning)
172     {
173     //5 seconds to thread stops itself
174     int i;
175     for (i = 0; i < 25; i++)
176         {
177         if (!isRunning)
178             break;
179         usleep(200000);
180         }
181
182     //after 5 seconds waiting thread still running. now killing it
183     if (isRunning)
184         {
185         if (pthread_kill(thread, SIGINT))
186             {
187             errorStr = "Cannot kill thread.";
188             return -1;
189             }
190         printfd(__FILE__, "AUTH_STRESS killed Run\n");
191         }
192     }
193
194 users->DelNotifierUserAdd(&onAddUserNotifier);
195 users->DelNotifierUserDel(&onDelUserNotifier);
196
197 return 0;
198 }
199 //-----------------------------------------------------------------------------
200 bool AUTH_STRESS::IsRunning()
201 {
202 return isRunning;
203 }
204 //-----------------------------------------------------------------------------
205 uint16_t AUTH_STRESS::GetStartPosition() const
206 {
207 return 70;
208 }
209 //-----------------------------------------------------------------------------
210 uint16_t AUTH_STRESS::GetStopPosition() const
211 {
212 return 70;
213 }
214 //-----------------------------------------------------------------------------
215 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
216 {
217 // ---------- IP -------------------
218 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
219 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier;
220
221 BeforeChgIPNotifier.SetAuthorizator(this);
222 BeforeChgIPNotifier.SetUser(u);
223 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
224
225 AfterChgIPNotifier.SetAuthorizator(this);
226 AfterChgIPNotifier.SetUser(u);
227 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
228
229 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
230 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
231 // ---------- IP end ---------------
232 }
233 //-----------------------------------------------------------------------------
234 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
235 {
236 // ---          IP              ---
237 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
238 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
239
240 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
241 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
242
243 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
244                   BeforeChgIPNotifierList.end(),
245                   bind2nd(IsContainsUserIPB, u));
246
247 if (ipBIter != BeforeChgIPNotifierList.end())
248     {
249     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
250     BeforeChgIPNotifierList.erase(ipBIter);
251     }
252
253 ipAIter = find_if(AfterChgIPNotifierList.begin(),
254                   AfterChgIPNotifierList.end(),
255                   bind2nd(IsContainsUserIPA, u));
256
257 if (ipAIter != AfterChgIPNotifierList.end())
258     {
259     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
260     AfterChgIPNotifierList.erase(ipAIter);
261     }
262 // ---          IP end          ---
263 }
264 //-----------------------------------------------------------------------------
265 void AUTH_STRESS::GetUsers()
266 {
267 USER_PTR u;
268 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
269 int h = users->OpenSearch();
270 if (!h)
271     {
272     printfd(__FILE__, "users->OpenSearch() error\n");
273     return;
274     }
275
276 while (1)
277     {
278     if (users->SearchNext(h, &u))
279         {
280         break;
281         }
282     usersList.push_back(u);
283     SetUserNotifiers(u);
284     }
285
286 users->CloseSearch(h);
287 }
288 //-----------------------------------------------------------------------------
289 void AUTH_STRESS::Unauthorize(USER_PTR u) const
290 {
291 if (!u->IsAuthorizedBy(this))
292     return;
293
294 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
295 u->Unauthorize(this);
296 }
297 //-----------------------------------------------------------------------------
298 void AUTH_STRESS::Authorize(USER_PTR u) const
299 {
300 USER_IPS ips = u->GetProperty().ips;
301 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
302     {
303     if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
304         {
305         printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
306         }
307     }
308 }
309 //-----------------------------------------------------------------------------
310 void AUTH_STRESS::AddUser(USER_PTR u)
311 {
312 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
313 SetUserNotifiers(u);
314 usersList.push_back(u);
315 }
316 //-----------------------------------------------------------------------------
317 void AUTH_STRESS::DelUser(USER_PTR u)
318 {
319 Unauthorize(u);
320 UnSetUserNotifiers(u);
321
322 list<USER_PTR>::iterator users_iter;
323 users_iter = usersList.begin();
324
325 while (users_iter != usersList.end())
326     {
327     if (u == *users_iter)
328         {
329         usersList.erase(users_iter);
330         printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
331         break;
332         }
333     users_iter++;
334     }
335 }
336 //-----------------------------------------------------------------------------
337 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
338 {
339 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
340 return -1;
341 }
342 //-----------------------------------------------------------------------------
343 void * AUTH_STRESS::Run(void * d)
344 {
345 AUTH_STRESS * ia;
346 ia = (AUTH_STRESS *)d;
347
348 ia->isRunning = true;
349
350 while (ia->nonstop)
351     {
352     printfd(__FILE__, "AUTH_STRESS::Run - averageTime: %d\n", random() % (2*ia->stressSettings.GetAverageOnlineTime()));
353
354     list<USER_PTR>::iterator users_iter;
355     users_iter = ia->usersList.begin();
356     while (users_iter != ia->usersList.end())
357         {
358         if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 1)
359             {
360             ia->Authorize(*users_iter);
361             printfd(__FILE__, "AUTH_STRESS::Authorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
362             }
363         if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 2)
364             {
365             ia->Unauthorize(*users_iter);
366             printfd(__FILE__, "AUTH_STRESS::Unauthorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
367             }
368
369         users_iter++;
370         }
371
372     sleep(1);
373     }
374
375 ia->isRunning = false;
376 return NULL;
377 }
378 //-----------------------------------------------------------------------------
379 template <typename varParamType>
380 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
381 {
382 auth->Unauthorize(user);
383 }
384 //-----------------------------------------------------------------------------
385 template <typename varParamType>
386 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
387 {
388 auth->Authorize(user);
389 }
390 //-----------------------------------------------------------------------------