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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2009/06/19 12:50:32 $
34 #include "stg/common.h"
35 #include "stg/user_property.h"
47 printfd(__FILE__, "constructor STRESS_CREATOR\n");
48 dc = new AUTH_STRESS();
52 printfd(__FILE__, "destructor STRESS_CREATOR\n");
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>
73 bool operator()(varType notifier, USER_PTR user) const
75 return notifier.GetUser() == user;
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
83 //printf("BASE_CAPTURER * GetCapturer()\n");
84 return stressc.GetPlugin();
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS()
90 : averageOnlineTime(0)
93 //-----------------------------------------------------------------------------
94 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
97 vector<PARAM_VALUE>::const_iterator pvi;
99 pv.param = "AverageOnlineTime";
100 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
101 if (pvi == s.moduleParams.end())
103 errorStr = "Parameter \'" + pv.param + "\' not found.";
107 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
109 errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
115 //-----------------------------------------------------------------------------
116 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
118 return averageOnlineTime;
120 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
123 const string AUTH_STRESS::GetVersion() const
125 return "Stress authorizator v.0.1";
127 //-----------------------------------------------------------------------------
128 AUTH_STRESS::AUTH_STRESS()
130 pthread_mutex_init(&mutex, NULL);
133 //-----------------------------------------------------------------------------
134 void AUTH_STRESS::SetUsers(USERS * u)
138 //-----------------------------------------------------------------------------
139 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
143 //-----------------------------------------------------------------------------
144 int AUTH_STRESS::ParseSettings()
146 int ret = stressSettings.ParseSettings(settings);
148 errorStr = stressSettings.GetStrError();
151 //-----------------------------------------------------------------------------
152 const string & AUTH_STRESS::GetStrError() const
156 //-----------------------------------------------------------------------------
157 int AUTH_STRESS::Start()
162 list<USER_PTR>::iterator users_iter;
164 onAddUserNotifier.SetAuthorizator(this);
165 onDelUserNotifier.SetAuthorizator(this);
166 users->AddNotifierUserAdd(&onAddUserNotifier);
167 users->AddNotifierUserDel(&onDelUserNotifier);
171 if (pthread_create(&thread, NULL, Run, this))
173 errorStr = "Cannot create thread.";
178 users_iter = usersList.begin();
179 while (users_iter != usersList.end())
181 Authorize(*users_iter);
188 //-----------------------------------------------------------------------------
189 int AUTH_STRESS::Stop()
194 //5 seconds to thread stops itself
196 for (i = 0; i < 25; i++)
203 //after 5 seconds waiting thread still running. now killing it
206 if (pthread_kill(thread, SIGINT))
208 errorStr = "Cannot kill thread.";
211 printfd(__FILE__, "AUTH_STRESS killed Run\n");
215 users->DelNotifierUserAdd(&onAddUserNotifier);
216 users->DelNotifierUserDel(&onDelUserNotifier);
220 //-----------------------------------------------------------------------------
221 bool AUTH_STRESS::IsRunning()
225 //-----------------------------------------------------------------------------
226 uint16_t AUTH_STRESS::GetStartPosition() const
230 //-----------------------------------------------------------------------------
231 uint16_t AUTH_STRESS::GetStopPosition() const
235 //-----------------------------------------------------------------------------
236 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
238 // ---------- IP -------------------
239 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
240 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier;
242 BeforeChgIPNotifier.SetAuthorizator(this);
243 BeforeChgIPNotifier.SetUser(u);
244 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
246 AfterChgIPNotifier.SetAuthorizator(this);
247 AfterChgIPNotifier.SetUser(u);
248 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
250 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
251 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
252 // ---------- IP end ---------------
254 //-----------------------------------------------------------------------------
255 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
258 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
259 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
261 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
262 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
264 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
265 BeforeChgIPNotifierList.end(),
266 bind2nd(IsContainsUserIPB, u));
268 if (ipBIter != BeforeChgIPNotifierList.end())
270 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
271 BeforeChgIPNotifierList.erase(ipBIter);
274 ipAIter = find_if(AfterChgIPNotifierList.begin(),
275 AfterChgIPNotifierList.end(),
276 bind2nd(IsContainsUserIPA, u));
278 if (ipAIter != AfterChgIPNotifierList.end())
280 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
281 AfterChgIPNotifierList.erase(ipAIter);
285 //-----------------------------------------------------------------------------
286 void AUTH_STRESS::GetUsers()
289 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
290 int h = users->OpenSearch();
293 printfd(__FILE__, "users->OpenSearch() error\n");
299 if (users->SearchNext(h, &u))
303 usersList.push_back(u);
307 users->CloseSearch(h);
309 //-----------------------------------------------------------------------------
310 void AUTH_STRESS::Unauthorize(USER_PTR u) const
312 if (!u->IsAuthorizedBy(this))
315 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
316 u->Unauthorize(this);
318 //-----------------------------------------------------------------------------
319 void AUTH_STRESS::Authorize(USER_PTR u) const
321 USER_IPS ips = u->GetProperty().ips;
322 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
324 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
326 printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
330 //-----------------------------------------------------------------------------
331 void AUTH_STRESS::AddUser(USER_PTR u)
333 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
335 usersList.push_back(u);
337 //-----------------------------------------------------------------------------
338 void AUTH_STRESS::DelUser(USER_PTR u)
341 UnSetUserNotifiers(u);
343 list<USER_PTR>::iterator users_iter;
344 users_iter = usersList.begin();
346 while (users_iter != usersList.end())
348 if (u == *users_iter)
350 usersList.erase(users_iter);
351 printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
357 //-----------------------------------------------------------------------------
358 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
360 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
363 //-----------------------------------------------------------------------------
364 void * AUTH_STRESS::Run(void * d)
367 ia = (AUTH_STRESS *)d;
369 ia->isRunning = true;
373 printfd(__FILE__, "AUTH_STRESS::Run - averageTime: %d\n", random() % (2*ia->stressSettings.GetAverageOnlineTime()));
375 list<USER_PTR>::iterator users_iter;
376 users_iter = ia->usersList.begin();
377 while (users_iter != ia->usersList.end())
379 if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 1)
381 ia->Authorize(*users_iter);
382 printfd(__FILE__, "AUTH_STRESS::Authorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
384 if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 2)
386 ia->Unauthorize(*users_iter);
387 printfd(__FILE__, "AUTH_STRESS::Unauthorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
396 ia->isRunning = false;
399 //-----------------------------------------------------------------------------
400 template <typename varParamType>
401 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
403 auth->Unauthorize(user);
405 //-----------------------------------------------------------------------------
406 template <typename varParamType>
407 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
409 auth->Authorize(user);
411 //-----------------------------------------------------------------------------