]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
Patch from SpiderX <spiderx@spiderx.dp.ua> - path to FreeRADIUS plugins.
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
1 #include <cstdio>
2 #include <cassert>
3 #include <csignal>
4 #include <ctime>
5 #include <algorithm>
6
7 #include "stg/user.h"
8 #include "stg/locker.h"
9 #include "stg/user_property.h"
10 #include "stg/plugin_creator.h"
11 #include "ping.h"
12
13 PLUGIN_CREATOR<PING> pc;
14 //-----------------------------------------------------------------------------
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 PLUGIN * GetPlugin()
18 {
19 return pc.GetPlugin();
20 }
21 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
24 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
25 template <typename varType>
26 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
27 {
28 public:
29     IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
30     bool operator()(varType notifier) const
31         {
32         return notifier.GetUser() == user;
33         };
34 private:
35     const USER_PTR & user;
36 };
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
41 {
42 PARAM_VALUE pv;
43 vector<PARAM_VALUE>::const_iterator pvi;
44
45 pv.param = "PingDelay";
46 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
47 if (pvi == s.moduleParams.end())
48     {
49     errorStr = "Parameter \'PingDelay\' not found.";
50     printfd(__FILE__, "Parameter 'PingDelay' not found\n");
51     return -1;
52     }
53 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
54     {
55     errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
56     printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
57     return -1;
58     }
59
60 return 0;
61 }
62 //-----------------------------------------------------------------------------
63 PING::PING()
64     : errorStr(),
65       pingSettings(),
66       settings(),
67       users(NULL),
68       usersList(),
69       thread(),
70       mutex(),
71       nonstop(false),
72       isRunning(false),
73       pinger(),
74       ChgCurrIPNotifierList(),
75       ChgIPNotifierList(),
76       onAddUserNotifier(*this),
77       onDelUserNotifier(*this),
78       logger(GetPluginLogger(GetStgLogger(), "ping"))
79 {
80 pthread_mutex_init(&mutex, NULL);
81 }
82 //-----------------------------------------------------------------------------
83 PING::~PING()
84 {
85 pthread_mutex_destroy(&mutex);
86 }
87 //-----------------------------------------------------------------------------
88 int PING::ParseSettings()
89 {
90 int ret = pingSettings.ParseSettings(settings);
91 if (ret)
92     errorStr = pingSettings.GetStrError();
93 return ret;
94 }
95 //-----------------------------------------------------------------------------
96 int PING::Start()
97 {
98 GetUsers();
99
100 users->AddNotifierUserAdd(&onAddUserNotifier);
101 users->AddNotifierUserDel(&onDelUserNotifier);
102
103 nonstop = true;
104
105 pinger.SetDelayTime(pingSettings.GetPingDelay());
106 pinger.Start();
107
108 if (pthread_create(&thread, NULL, Run, this))
109     {
110     errorStr = "Cannot start thread.";
111     logger("Cannot create thread.");
112     printfd(__FILE__, "Cannot start thread\n");
113     return -1;
114     }
115
116 return 0;
117 }
118 //-----------------------------------------------------------------------------
119 int PING::Stop()
120 {
121 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
122
123 if (!isRunning)
124     return 0;
125
126 pinger.Stop();
127 nonstop = false;
128 //5 seconds to thread stops itself
129 struct timespec ts = {0, 200000000};
130 for (int i = 0; i < 25; i++)
131     {
132     if (!isRunning)
133         break;
134
135     nanosleep(&ts, NULL);
136     }
137
138 users->DelNotifierUserAdd(&onAddUserNotifier);
139 users->DelNotifierUserDel(&onDelUserNotifier);
140
141 list<USER_PTR>::iterator users_iter;
142 users_iter = usersList.begin();
143 while (users_iter != usersList.end())
144     {
145     UnSetUserNotifiers(*users_iter);
146     ++users_iter;
147     }
148
149 if (isRunning)
150     return -1;
151
152 return 0;
153 }
154 //-----------------------------------------------------------------------------
155 bool PING::IsRunning()
156 {
157 return isRunning;
158 }
159 //-----------------------------------------------------------------------------
160 void * PING::Run(void * d)
161 {
162 sigset_t signalSet;
163 sigfillset(&signalSet);
164 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
165
166 PING * ping = static_cast<PING *>(d);
167 ping->isRunning = true;
168
169 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
170  
171 while (ping->nonstop)
172     {
173     list<USER_PTR>::iterator iter = ping->usersList.begin();
174         {
175         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
176         while (iter != ping->usersList.end())
177             {
178             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
179                 {
180                 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
181                 time_t t;
182                 if (ping->pinger.GetIPTime(ip, &t) == 0)
183                     {
184                     if (t)
185                         (*iter)->UpdatePingTime(t);
186                     }
187                 }
188             else
189                 {
190                 uint32_t ip = (*iter)->GetCurrIP();
191                 if (ip)
192                     {
193                     time_t t;
194                     if (ping->pinger.GetIPTime(ip, &t) == 0)
195                         {
196                         if (t)
197                             (*iter)->UpdatePingTime(t);
198                         }
199                     }
200                 }
201             ++iter;
202             }
203         }
204     struct timespec ts = {delay / 1000000000, delay % 1000000000};
205     for (int i = 0; i < 100; i++)
206         {
207         if (ping->nonstop)
208             {
209             nanosleep(&ts, NULL);
210             }
211         }
212     }
213
214 ping->isRunning = false;
215 return NULL;
216 }
217 //-----------------------------------------------------------------------------
218 void PING::SetUserNotifiers(USER_PTR u)
219 {
220 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
221 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
222
223 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
224 ChgIPNotifierList.push_front(ChgIPNotifier);
225
226 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
227 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
228 }
229 //-----------------------------------------------------------------------------
230 void PING::UnSetUserNotifiers(USER_PTR u)
231 {
232 // ---          CurrIP              ---
233 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
234 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
235
236 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
237 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
238
239 currIPter = find_if(ChgCurrIPNotifierList.begin(),
240                     ChgCurrIPNotifierList.end(),
241                     IsContainsUserCurrIP);
242
243 if (currIPter != ChgCurrIPNotifierList.end())
244     {
245     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
246     ChgCurrIPNotifierList.erase(currIPter);
247     }
248 // ---         CurrIP end          ---
249
250 // ---          IP              ---
251 IPIter = find_if(ChgIPNotifierList.begin(),
252                  ChgIPNotifierList.end(),
253                  IsContainsUserIP);
254
255 if (IPIter != ChgIPNotifierList.end())
256     {
257     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
258     ChgIPNotifierList.erase(IPIter);
259     }
260 // ---          IP end          ---
261 }
262 //-----------------------------------------------------------------------------
263 void PING::GetUsers()
264 {
265 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
266
267 USER_PTR u;
268 int h = users->OpenSearch();
269 assert(h && "USERS::OpenSearch is always correct");
270
271 while (users->SearchNext(h, &u) == 0)
272     {
273     usersList.push_back(u);
274     SetUserNotifiers(u);
275     if (u->GetProperty().ips.ConstData().OnlyOneIP())
276         {
277         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
278         }
279     else
280         {
281         uint32_t ip = u->GetCurrIP();
282         if (ip)
283             {
284             pinger.AddIP(ip);
285             }
286         }
287     }
288
289 users->CloseSearch(h);
290 }
291 //-----------------------------------------------------------------------------
292 void PING::AddUser(USER_PTR u)
293 {
294 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
295
296 SetUserNotifiers(u);
297 usersList.push_back(u);
298 }
299 //-----------------------------------------------------------------------------
300 void PING::DelUser(USER_PTR u)
301 {
302 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
303
304 UnSetUserNotifiers(u);
305
306 list<USER_PTR>::iterator users_iter;
307 users_iter = usersList.begin();
308
309 while (users_iter != usersList.end())
310     {
311     if (u == *users_iter)
312         {
313         usersList.erase(users_iter);
314         break;
315         }
316     ++users_iter;
317     }
318 }
319 //-----------------------------------------------------------------------------
320 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
321 {
322 ping.pinger.DelIP(oldIP);
323 if (newIP)
324     {
325     ping.pinger.AddIP(newIP);
326     }
327 }
328 //-----------------------------------------------------------------------------
329 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
330 {
331 if (oldIPS.OnlyOneIP())
332     {
333     ping.pinger.DelIP(oldIPS[0].ip);
334     }
335
336 if (newIPS.OnlyOneIP())
337     {
338     ping.pinger.AddIP(newIPS[0].ip);
339     }
340 }
341 //-----------------------------------------------------------------------------
342 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
343 {
344 ping.AddUser(user);
345 }
346 //-----------------------------------------------------------------------------
347 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
348 {
349 ping.DelUser(user);
350 }
351 //-----------------------------------------------------------------------------