]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
Rules counter added to the SMUX plugin
[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 {
107 pthread_mutex_init(&mutex, NULL);
108
109 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
110 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
111 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
112 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
113
114 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
115 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
116 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
117 }
118
119 SMUX::~SMUX()
120 {
121     {
122     Sensors::iterator it;
123     for (it = sensors.begin(); it != sensors.end(); ++it)
124         delete it->second;
125     }
126     {
127     Tables::iterator it;
128     for (it = tables.begin(); it != tables.end(); ++it)
129         delete it->second;
130     }
131 printfd(__FILE__, "SMUX::~SMUX()\n");
132 pthread_mutex_destroy(&mutex);
133 }
134
135 int SMUX::ParseSettings()
136 {
137 return smuxSettings.ParseSettings(settings);
138 }
139
140 int SMUX::Start()
141 {
142 assert(users != NULL && "users must not be NULL");
143 assert(tariffs != NULL && "tariffs must not be NULL");
144 assert(admins != NULL && "admins must not be NULL");
145 assert(services != NULL && "services must not be NULL");
146 assert(corporations != NULL && "corporations must not be NULL");
147 assert(traffcounter != NULL && "traffcounter must not be NULL");
148
149 if (PrepareNet())
150     return -1;
151
152 // Users
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
154 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
157 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
164 // Tariffs
165 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
166 // Admins
167 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
168 // Services
169 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
170 // Corporations
171 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
172 // Traffcounter
173 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
174
175 // Table data
176 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users);
177
178 UpdateTables();
179
180 #ifdef DEBUG
181 Sensors::const_iterator it(sensors.begin());
182 while (it != sensors.end())
183     {
184     printfd(__FILE__, "%s = %s\n",
185             it->first.ToString().c_str(),
186             it->second->ToString().c_str());
187     ++it;
188     }
189 #endif
190
191 if (!running)
192     {
193     if (pthread_create(&thread, NULL, Runner, this))
194         {
195         errorStr = "Cannot create thread.";
196         printfd(__FILE__, "Cannot create thread\n");
197         return -1;
198         }
199     }
200
201 return 0;
202 }
203
204 int SMUX::Stop()
205 {
206 printfd(__FILE__, "SMUX::Stop() - Before\n");
207 running = false;
208
209 if (!stopped)
210     {
211     //5 seconds to thread stops itself
212     for (int i = 0; i < 25 && !stopped; i++)
213         {
214         struct timespec ts = {0, 200000000};
215         nanosleep(&ts, NULL);
216         }
217
218     //after 5 seconds waiting thread still running. now killing it
219     if (!stopped)
220         {
221         printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
222         if (pthread_kill(thread, SIGINT))
223             {
224             errorStr = "Cannot kill thread.";
225             printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
226             return -1;
227             }
228         printfd(__FILE__, "SMUX::Stop() -  killed Run\n");
229         }
230     }
231
232 pthread_join(thread, NULL);
233
234 close(sock);
235
236 printfd(__FILE__, "SMUX::Stop() - After\n");
237 return 0;
238 }
239
240 void * SMUX::Runner(void * d)
241 {
242 SMUX * smux = static_cast<SMUX *>(d);
243
244 smux->Run();
245
246 return NULL;
247 }
248
249 void SMUX::Run()
250 {
251 SendOpenPDU(sock);
252 SendRReqPDU(sock);
253 running = true;
254 stopped = false;
255 while(running)
256     {
257     if (WaitPackets(sock))
258         {
259         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
260         if (pdus)
261             DispatchPDUs(pdus);
262         }
263     if (!running)
264         break;
265     }
266 SendClosePDU(sock);
267 stopped = true;
268 }
269
270 bool SMUX::PrepareNet()
271 {
272 sock = socket(AF_INET, SOCK_STREAM, 0);
273
274 if (sock < 0)
275     {
276     errorStr = "Cannot create socket.";
277     printfd(__FILE__, "Cannot create socket\n");
278     return true;
279     }
280
281 struct sockaddr_in addr;
282
283 addr.sin_family = AF_INET;
284 addr.sin_port = htons(smuxSettings.GetPort());
285 addr.sin_addr.s_addr = smuxSettings.GetIP();
286
287 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
288     {
289     errorStr = "Cannot connect.";
290     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
291     return true;
292     }
293
294 return false;
295 }
296
297 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
298 {
299 SMUXHandlers::iterator it;
300 it = smuxHandlers.find(pdus->present);
301 if (it != smuxHandlers.end())
302     {
303     return (this->*(it->second))(pdus);
304     }
305 else
306     {
307     switch (pdus->present)
308         {
309         case SMUX_PDUs_PR_NOTHING:
310             printfd(__FILE__, "PDUs: nothing\n");
311             break;
312         case SMUX_PDUs_PR_open:
313             printfd(__FILE__, "PDUs: open\n");
314             break;
315         case SMUX_PDUs_PR_registerRequest:
316             printfd(__FILE__, "PDUs: registerRequest\n");
317             break;
318         default:
319             printfd(__FILE__, "PDUs: undefined\n");
320         }
321     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
322     }
323 return false;
324 }
325
326 bool SMUX::UpdateTables()
327 {
328 Sensors newSensors;
329 bool done = true;
330 Tables::iterator it(tables.begin());
331 while (it != tables.end())
332     {
333     try
334         {
335         it->second->UpdateSensors(newSensors);
336         }
337     catch (const std::runtime_error & ex)
338         {
339         printfd(__FILE__,
340                 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
341                 it->first.c_str(), ex.what());
342         done = false;
343         break;
344         }
345     ++it;
346     }
347 if (!done)
348     {
349     Sensors::iterator it(newSensors.begin());
350     while (it != newSensors.end())
351         {
352         delete it->second;
353         ++it;
354         }
355     return false;
356     }
357
358 it = tables.begin();
359 while (it != tables.end())
360     {
361     std::pair<Sensors::iterator, Sensors::iterator> res;
362     res = std::equal_range(sensors.begin(),
363                            sensors.end(),
364                            std::pair<OID, Sensor *>(OID(it->first), NULL),
365                            SPrefixLess);
366     Sensors::iterator sit(res.first);
367     while (sit != res.second)
368         {
369         delete sit->second;
370         ++sit;
371         }
372     sensors.erase(res.first, res.second);
373     ++it;
374     }
375
376 sensors.insert(newSensors.begin(), newSensors.end());
377
378 return true;
379 }