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