]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
Fix tariff and user subscription in SMUX
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4
5 #include <cstring>
6 #include <cerrno>
7 #include <ctime>
8 #include <csignal>
9 #include <cassert>
10
11 #include <vector>
12 #include <algorithm>
13 #include <iterator>
14 #include <stdexcept>
15 #include <utility>
16
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
19 /*#include "stg/users.h"
20 #include "stg/tariffs.h"
21 #include "stg/admins.h"
22 #include "stg/services.h"
23 #include "stg/corporations.h"
24 #include "stg/traffcounter.h"*/
25
26 #include "smux.h"
27 #include "utils.h"
28
29 PLUGIN_CREATOR<SMUX> smc;
30
31 PLUGIN * GetPlugin()
32 {
33 return smc.GetPlugin();
34 }
35
36 bool SPrefixLess(const Sensors::value_type & a,
37                  const Sensors::value_type & b)
38 {
39 return a.first.PrefixLess(b.first);
40 }
41
42 SMUX_SETTINGS::SMUX_SETTINGS()
43     : ip(0),
44       port(0)
45 {}
46
47 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
48 {
49 PARAM_VALUE pv;
50 std::vector<PARAM_VALUE>::const_iterator pvi;
51 int p;
52
53 pv.param = "Port";
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
56     {
57     errorStr = "Parameter \'Port\' not found.";
58     printfd(__FILE__, "Parameter 'Port' not found\n");
59     return -1;
60     }
61 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
62     {
63     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
64     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
65     return -1;
66     }
67 port = p;
68
69 pv.param = "Password";
70 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
71 if (pvi == s.moduleParams.end())
72     {
73     errorStr = "Parameter \'Password\' not found.";
74     printfd(__FILE__, "Parameter 'Password' not found\n");
75     password = "";
76     }
77 else
78     {
79     password = pvi->value[0];
80     }
81
82 pv.param = "Server";
83 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
84 if (pvi == s.moduleParams.end())
85     {
86     errorStr = "Parameter \'Server\' not found.";
87     printfd(__FILE__, "Parameter 'Server' not found\n");
88     return -1;
89     }
90 ip = inet_strington(pvi->value[0]);
91
92 return 0;
93 }
94
95 SMUX::SMUX()
96     : PLUGIN(),
97       users(NULL),
98       tariffs(NULL),
99       admins(NULL),
100       services(NULL),
101       corporations(NULL),
102       traffcounter(NULL),
103       running(false),
104       stopped(true),
105       sock(-1),
106       smuxHandlers(),
107       pdusHandlers(),
108       sensors(),
109       tables(),
110       notifiers(),
111       addUserNotifier(*this),
112       delUserNotifier(*this),
113       addDelTariffNotifier(*this)
114 {
115 pthread_mutex_init(&mutex, NULL);
116
117 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
118 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
119 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
120 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
121
122 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
123 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
124 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
125 }
126
127 SMUX::~SMUX()
128 {
129     {
130     Sensors::iterator it;
131     for (it = sensors.begin(); it != sensors.end(); ++it)
132         delete it->second;
133     }
134     {
135     Tables::iterator it;
136     for (it = tables.begin(); it != tables.end(); ++it)
137         delete it->second;
138     }
139 printfd(__FILE__, "SMUX::~SMUX()\n");
140 pthread_mutex_destroy(&mutex);
141 }
142
143 int SMUX::ParseSettings()
144 {
145 return smuxSettings.ParseSettings(settings);
146 }
147
148 int SMUX::Start()
149 {
150 assert(users != NULL && "users must not be NULL");
151 assert(tariffs != NULL && "tariffs must not be NULL");
152 assert(admins != NULL && "admins must not be NULL");
153 assert(services != NULL && "services must not be NULL");
154 assert(corporations != NULL && "corporations must not be NULL");
155 assert(traffcounter != NULL && "traffcounter must not be NULL");
156
157 if (PrepareNet())
158     return -1;
159
160 // Users
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
170 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
172 // Tariffs
173 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
174 // Admins
175 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
176 // Services
177 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
178 // Corporations
179 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
180 // Traffcounter
181 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
182
183 // Table data
184 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *tariffs, *users);
185
186 UpdateTables();
187 SetNotifiers();
188
189 #ifdef DEBUG
190 Sensors::const_iterator it(sensors.begin());
191 while (it != sensors.end())
192     {
193     printfd(__FILE__, "%s = %s\n",
194             it->first.ToString().c_str(),
195             it->second->ToString().c_str());
196     ++it;
197     }
198 #endif
199
200 if (!running)
201     {
202     if (pthread_create(&thread, NULL, Runner, this))
203         {
204         errorStr = "Cannot create thread.";
205         printfd(__FILE__, "Cannot create thread\n");
206         return -1;
207         }
208     }
209
210 return 0;
211 }
212
213 int SMUX::Stop()
214 {
215 printfd(__FILE__, "SMUX::Stop() - Before\n");
216 running = false;
217
218 ResetNotifiers();
219
220 if (!stopped)
221     {
222     //5 seconds to thread stops itself
223     for (int i = 0; i < 25 && !stopped; i++)
224         {
225         struct timespec ts = {0, 200000000};
226         nanosleep(&ts, NULL);
227         }
228
229     //after 5 seconds waiting thread still running. now killing it
230     if (!stopped)
231         {
232         printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
233         if (pthread_kill(thread, SIGINT))
234             {
235             errorStr = "Cannot kill thread.";
236             printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
237             return -1;
238             }
239         printfd(__FILE__, "SMUX::Stop() -  killed Run\n");
240         }
241     }
242
243 pthread_join(thread, NULL);
244
245 close(sock);
246
247 printfd(__FILE__, "SMUX::Stop() - After\n");
248 return 0;
249 }
250
251 void * SMUX::Runner(void * d)
252 {
253 SMUX * smux = static_cast<SMUX *>(d);
254
255 smux->Run();
256
257 return NULL;
258 }
259
260 void SMUX::Run()
261 {
262 SendOpenPDU(sock);
263 SendRReqPDU(sock);
264 running = true;
265 stopped = false;
266
267 while(running)
268     {
269     if (WaitPackets(sock))
270         {
271         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
272         if (pdus)
273             {
274             DispatchPDUs(pdus);
275             ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
276             }
277         }
278     if (!running)
279         break;
280     }
281 SendClosePDU(sock);
282 stopped = true;
283 }
284
285 bool SMUX::PrepareNet()
286 {
287 sock = socket(AF_INET, SOCK_STREAM, 0);
288
289 if (sock < 0)
290     {
291     errorStr = "Cannot create socket.";
292     printfd(__FILE__, "Cannot create socket\n");
293     return true;
294     }
295
296 struct sockaddr_in addr;
297
298 addr.sin_family = AF_INET;
299 addr.sin_port = htons(smuxSettings.GetPort());
300 addr.sin_addr.s_addr = smuxSettings.GetIP();
301
302 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
303     {
304     errorStr = "Cannot connect.";
305     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
306     return true;
307     }
308
309 return false;
310 }
311
312 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
313 {
314 SMUXHandlers::iterator it;
315 it = smuxHandlers.find(pdus->present);
316 if (it != smuxHandlers.end())
317     {
318     return (this->*(it->second))(pdus);
319     }
320 else
321     {
322     switch (pdus->present)
323         {
324         case SMUX_PDUs_PR_NOTHING:
325             printfd(__FILE__, "PDUs: nothing\n");
326             break;
327         case SMUX_PDUs_PR_open:
328             printfd(__FILE__, "PDUs: open\n");
329             break;
330         case SMUX_PDUs_PR_registerRequest:
331             printfd(__FILE__, "PDUs: registerRequest\n");
332             break;
333         default:
334             printfd(__FILE__, "PDUs: undefined\n");
335         }
336     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
337     }
338 return false;
339 }
340
341 bool SMUX::UpdateTables()
342 {
343 Sensors newSensors;
344 bool done = true;
345 Tables::iterator it(tables.begin());
346 while (it != tables.end())
347     {
348     try
349         {
350         it->second->UpdateSensors(newSensors);
351         }
352     catch (const std::runtime_error & ex)
353         {
354         printfd(__FILE__,
355                 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
356                 it->first.c_str(), ex.what());
357         done = false;
358         break;
359         }
360     ++it;
361     }
362 if (!done)
363     {
364     Sensors::iterator it(newSensors.begin());
365     while (it != newSensors.end())
366         {
367         delete it->second;
368         ++it;
369         }
370     return false;
371     }
372
373 it = tables.begin();
374 while (it != tables.end())
375     {
376     std::pair<Sensors::iterator, Sensors::iterator> res;
377     res = std::equal_range(sensors.begin(),
378                            sensors.end(),
379                            std::pair<OID, Sensor *>(OID(it->first), NULL),
380                            SPrefixLess);
381     Sensors::iterator sit(res.first);
382     while (sit != res.second)
383         {
384         delete sit->second;
385         ++sit;
386         }
387     sensors.erase(res.first, res.second);
388     ++it;
389     }
390
391 sensors.insert(newSensors.begin(), newSensors.end());
392
393 return true;
394 }
395
396 void SMUX::SetNotifier(USER_PTR userPtr)
397 {
398 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
399 userPtr->GetProperty().tariffName.AddAfterNotifier(&notifiers.back());
400 }
401
402 void SMUX::UnsetNotifier(USER_PTR userPtr)
403 {
404 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
405 while (it != notifiers.end())
406     {
407     if (it->GetUserPtr() == userPtr)
408         {
409         userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
410         notifiers.erase(it);
411         break;
412         }
413     ++it;
414     }
415 }
416
417 void SMUX::SetNotifiers()
418 {
419 int h = users->OpenSearch();
420 assert(h && "USERS::OpenSearch is always correct");
421
422 USER_PTR u;
423 while (!users->SearchNext(h, &u))
424     SetNotifier(u);
425
426 users->CloseSearch(h);
427
428 users->AddNotifierUserAdd(&addUserNotifier);
429 users->AddNotifierUserDel(&delUserNotifier);
430
431 tariffs->AddNotifierAdd(&addDelTariffNotifier);
432 tariffs->AddNotifierDel(&addDelTariffNotifier);
433 }
434
435 void SMUX::ResetNotifiers()
436 {
437 tariffs->DelNotifierDel(&addDelTariffNotifier);
438 tariffs->DelNotifierAdd(&addDelTariffNotifier);
439
440 users->DelNotifierUserDel(&delUserNotifier);
441 users->DelNotifierUserAdd(&addUserNotifier);
442
443 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
444 while (it != notifiers.end())
445     {
446     it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
447     ++it;
448     }
449 }
450
451 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
452 {
453 smux.UpdateTables();
454 }