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::ParseIntInRange(const string & str, int min, int max, int * val)
96 if (str2x(str.c_str(), *val))
98 errorStr = "Incorrect value \'" + str + "\'.";
101 if (*val < min || *val > max)
103 errorStr = "Value \'" + str + "\' out of range.";
108 //-----------------------------------------------------------------------------
109 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
112 vector<PARAM_VALUE>::const_iterator pvi;
114 pv.param = "AverageOnlineTime";
115 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
116 if (pvi == s.moduleParams.end())
118 errorStr = "Parameter \'" + pv.param + "\' not found.";
122 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
124 errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
130 //-----------------------------------------------------------------------------
131 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
133 return averageOnlineTime;
135 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
138 const string AUTH_STRESS::GetVersion() const
140 return "Stress authorizator v.0.1";
142 //-----------------------------------------------------------------------------
143 AUTH_STRESS::AUTH_STRESS()
145 pthread_mutex_init(&mutex, NULL);
148 //-----------------------------------------------------------------------------
149 void AUTH_STRESS::SetUsers(USERS * u)
153 //-----------------------------------------------------------------------------
154 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
158 //-----------------------------------------------------------------------------
159 int AUTH_STRESS::ParseSettings()
161 int ret = stressSettings.ParseSettings(settings);
163 errorStr = stressSettings.GetStrError();
166 //-----------------------------------------------------------------------------
167 const string & AUTH_STRESS::GetStrError() const
171 //-----------------------------------------------------------------------------
172 int AUTH_STRESS::Start()
177 list<USER_PTR>::iterator users_iter;
179 onAddUserNotifier.SetAuthorizator(this);
180 onDelUserNotifier.SetAuthorizator(this);
181 users->AddNotifierUserAdd(&onAddUserNotifier);
182 users->AddNotifierUserDel(&onDelUserNotifier);
186 if (pthread_create(&thread, NULL, Run, this))
188 errorStr = "Cannot create thread.";
193 users_iter = usersList.begin();
194 while (users_iter != usersList.end())
196 Authorize(*users_iter);
203 //-----------------------------------------------------------------------------
204 int AUTH_STRESS::Stop()
209 //5 seconds to thread stops itself
211 for (i = 0; i < 25; i++)
218 //after 5 seconds waiting thread still running. now killing it
221 if (pthread_kill(thread, SIGINT))
223 errorStr = "Cannot kill thread.";
226 printfd(__FILE__, "AUTH_STRESS killed Run\n");
230 users->DelNotifierUserAdd(&onAddUserNotifier);
231 users->DelNotifierUserDel(&onDelUserNotifier);
235 //-----------------------------------------------------------------------------
236 bool AUTH_STRESS::IsRunning()
240 //-----------------------------------------------------------------------------
241 uint16_t AUTH_STRESS::GetStartPosition() const
245 //-----------------------------------------------------------------------------
246 uint16_t AUTH_STRESS::GetStopPosition() const
250 //-----------------------------------------------------------------------------
251 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
253 // ---------- IP -------------------
254 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
255 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier;
257 BeforeChgIPNotifier.SetAuthorizator(this);
258 BeforeChgIPNotifier.SetUser(u);
259 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
261 AfterChgIPNotifier.SetAuthorizator(this);
262 AfterChgIPNotifier.SetUser(u);
263 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
265 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
266 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
267 // ---------- IP end ---------------
269 //-----------------------------------------------------------------------------
270 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
273 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
274 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
276 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
277 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
279 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
280 BeforeChgIPNotifierList.end(),
281 bind2nd(IsContainsUserIPB, u));
283 if (ipBIter != BeforeChgIPNotifierList.end())
285 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
286 BeforeChgIPNotifierList.erase(ipBIter);
289 ipAIter = find_if(AfterChgIPNotifierList.begin(),
290 AfterChgIPNotifierList.end(),
291 bind2nd(IsContainsUserIPA, u));
293 if (ipAIter != AfterChgIPNotifierList.end())
295 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
296 AfterChgIPNotifierList.erase(ipAIter);
300 //-----------------------------------------------------------------------------
301 void AUTH_STRESS::GetUsers()
304 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
305 int h = users->OpenSearch();
308 printfd(__FILE__, "users->OpenSearch() error\n");
314 if (users->SearchNext(h, &u))
318 usersList.push_back(u);
322 users->CloseSearch(h);
324 //-----------------------------------------------------------------------------
325 void AUTH_STRESS::Unauthorize(USER_PTR u) const
327 if (!u->IsAuthorizedBy(this))
330 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
331 u->Unauthorize(this);
333 //-----------------------------------------------------------------------------
334 void AUTH_STRESS::Authorize(USER_PTR u) const
336 USER_IPS ips = u->GetProperty().ips;
337 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
339 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
341 printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
345 //-----------------------------------------------------------------------------
346 void AUTH_STRESS::AddUser(USER_PTR u)
348 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
350 usersList.push_back(u);
352 //-----------------------------------------------------------------------------
353 void AUTH_STRESS::DelUser(USER_PTR u)
356 UnSetUserNotifiers(u);
358 list<USER_PTR>::iterator users_iter;
359 users_iter = usersList.begin();
361 while (users_iter != usersList.end())
363 if (u == *users_iter)
365 usersList.erase(users_iter);
366 printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
372 //-----------------------------------------------------------------------------
373 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
375 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
378 //-----------------------------------------------------------------------------
379 void * AUTH_STRESS::Run(void * d)
382 ia = (AUTH_STRESS *)d;
384 ia->isRunning = true;
388 printfd(__FILE__, "AUTH_STRESS::Run - averageTime: %d\n", random() % (2*ia->stressSettings.GetAverageOnlineTime()));
390 list<USER_PTR>::iterator users_iter;
391 users_iter = ia->usersList.begin();
392 while (users_iter != ia->usersList.end())
394 if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 1)
396 ia->Authorize(*users_iter);
397 printfd(__FILE__, "AUTH_STRESS::Authorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
399 if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 2)
401 ia->Unauthorize(*users_iter);
402 printfd(__FILE__, "AUTH_STRESS::Unauthorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
411 ia->isRunning = false;
414 //-----------------------------------------------------------------------------
415 template <typename varParamType>
416 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
418 auth->Unauthorize(user);
420 //-----------------------------------------------------------------------------
421 template <typename varParamType>
422 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
424 auth->Authorize(user);
426 //-----------------------------------------------------------------------------