]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
Prevent memory leak in 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             {
262             DispatchPDUs(pdus);
263             ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
264             }
265         }
266     if (!running)
267         break;
268     }
269 SendClosePDU(sock);
270 stopped = true;
271 }
272
273 bool SMUX::PrepareNet()
274 {
275 sock = socket(AF_INET, SOCK_STREAM, 0);
276
277 if (sock < 0)
278     {
279     errorStr = "Cannot create socket.";
280     printfd(__FILE__, "Cannot create socket\n");
281     return true;
282     }
283
284 struct sockaddr_in addr;
285
286 addr.sin_family = AF_INET;
287 addr.sin_port = htons(smuxSettings.GetPort());
288 addr.sin_addr.s_addr = smuxSettings.GetIP();
289
290 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
291     {
292     errorStr = "Cannot connect.";
293     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
294     return true;
295     }
296
297 return false;
298 }
299
300 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
301 {
302 SMUXHandlers::iterator it;
303 it = smuxHandlers.find(pdus->present);
304 if (it != smuxHandlers.end())
305     {
306     return (this->*(it->second))(pdus);
307     }
308 else
309     {
310     switch (pdus->present)
311         {
312         case SMUX_PDUs_PR_NOTHING:
313             printfd(__FILE__, "PDUs: nothing\n");
314             break;
315         case SMUX_PDUs_PR_open:
316             printfd(__FILE__, "PDUs: open\n");
317             break;
318         case SMUX_PDUs_PR_registerRequest:
319             printfd(__FILE__, "PDUs: registerRequest\n");
320             break;
321         default:
322             printfd(__FILE__, "PDUs: undefined\n");
323         }
324     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
325     }
326 return false;
327 }
328
329 bool SMUX::UpdateTables()
330 {
331 Sensors newSensors;
332 bool done = true;
333 Tables::iterator it(tables.begin());
334 while (it != tables.end())
335     {
336     try
337         {
338         it->second->UpdateSensors(newSensors);
339         }
340     catch (const std::runtime_error & ex)
341         {
342         printfd(__FILE__,
343                 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
344                 it->first.c_str(), ex.what());
345         done = false;
346         break;
347         }
348     ++it;
349     }
350 if (!done)
351     {
352     Sensors::iterator it(newSensors.begin());
353     while (it != newSensors.end())
354         {
355         delete it->second;
356         ++it;
357         }
358     return false;
359     }
360
361 it = tables.begin();
362 while (it != tables.end())
363     {
364     std::pair<Sensors::iterator, Sensors::iterator> res;
365     res = std::equal_range(sensors.begin(),
366                            sensors.end(),
367                            std::pair<OID, Sensor *>(OID(it->first), NULL),
368                            SPrefixLess);
369     Sensors::iterator sit(res.first);
370     while (sit != res.second)
371         {
372         delete sit->second;
373         ++sit;
374         }
375     sensors.erase(res.first, res.second);
376     ++it;
377     }
378
379 sensors.insert(newSensors.begin(), newSensors.end());
380
381 return true;
382 }