2 #include <sys/socket.h>
17 #include "stg/common.h"
25 bool SPrefixLess(const Sensors::value_type & a,
26 const Sensors::value_type & b)
28 return a.first.PrefixLess(b.first);
33 extern "C" STG::Plugin* GetPlugin()
39 SMUX_SETTINGS::SMUX_SETTINGS()
44 int SMUX_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
50 auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
51 if (pvi == s.moduleParams.end() || pvi->value.empty())
53 errorStr = "Parameter \'Port\' not found.";
54 printfd(__FILE__, "Parameter 'Port' not found\n");
57 if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0)
59 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
60 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
63 port = static_cast<uint16_t>(p);
65 pv.param = "Password";
66 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end() || pvi->value.empty())
69 errorStr = "Parameter \'Password\' not found.";
70 printfd(__FILE__, "Parameter 'Password' not found\n");
75 password = pvi->value[0];
79 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end() || pvi->value.empty())
82 errorStr = "Parameter \'Server\' not found.";
83 printfd(__FILE__, "Parameter 'Server' not found\n");
86 ip = inet_strington(pvi->value[0]);
96 corporations(nullptr),
97 traffcounter(nullptr),
103 addDelTariffNotifier(*this),
104 logger(STG::PluginLogger::get("smux"))
106 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
107 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
108 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
109 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
111 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
112 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
113 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
118 for (auto& kv : sensors)
120 for (auto& kv : tables)
122 printfd(__FILE__, "SMUX::~SMUX()\n");
125 int SMUX::ParseSettings()
127 return smuxSettings.ParseSettings(settings);
132 assert(users != nullptr && "users must not be NULL");
133 assert(tariffs != nullptr && "tariffs must not be NULL");
134 assert(admins != nullptr && "admins must not be NULL");
135 assert(services != nullptr && "services must not be NULL");
136 assert(corporations != nullptr && "corporations must not be NULL");
137 assert(traffcounter != nullptr && "traffcounter must not be NULL");
140 needReconnect = true;
143 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
144 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
145 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
146 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
147 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
148 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
149 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
150 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
151 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
152 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
154 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
158 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
160 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
162 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
164 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
167 tables[".1.3.6.1.4.1.38313.1.2.2"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.2.2", *tariffs, *users);
173 auto it = sensors.begin();
174 while (it != sensors.end())
176 printfd(__FILE__, "%s = %s\n",
177 it->first.ToString().c_str(),
178 it->second->ToString().c_str());
183 if (!m_thread.joinable())
184 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
191 printfd(__FILE__, "SMUX::Stop() - Before\n");
192 m_thread.request_stop();
196 //5 seconds to thread stops itself
197 for (int i = 0; i < 25 && !stopped; i++)
199 struct timespec ts = {0, 200000000};
200 nanosleep(&ts, nullptr);
211 for (auto& kv : sensors)
213 for (auto& kv : tables)
216 tables.erase(tables.begin(), tables.end());
217 sensors.erase(sensors.begin(), sensors.end());
226 printfd(__FILE__, "SMUX::Stop() - After\n");
230 int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
238 printfd(__FILE__, "SMUX reconnected succesfully.\n");
239 logger("Reconnected successfully.");
244 void SMUX::Run(std::stop_token token)
247 if (!SendOpenPDU(sock))
248 needReconnect = true;
249 if (!SendRReqPDU(sock))
250 needReconnect = true;
253 while (!token.stop_requested())
255 if (WaitPackets(sock) && !needReconnect)
257 auto* pdus = RecvSMUXPDUs(sock);
261 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
263 else if (!token.stop_requested())
266 else if (!token.stop_requested() && needReconnect)
268 if (token.stop_requested())
275 bool SMUX::PrepareNet()
277 sock = socket(AF_INET, SOCK_STREAM, 0);
281 errorStr = "Cannot create socket.";
282 logger("Cannot create a socket: %s", strerror(errno));
283 printfd(__FILE__, "Cannot create socket\n");
287 struct sockaddr_in addr;
289 addr.sin_family = AF_INET;
290 addr.sin_port = htons(smuxSettings.GetPort());
291 addr.sin_addr.s_addr = smuxSettings.GetIP();
293 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) != 0)
295 errorStr = "Cannot connect.";
296 logger("Cannot connect the socket: %s", strerror(errno));
297 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
304 bool SMUX::Reconnect()
306 if (needReconnect && difftime(time(nullptr), lastReconnectTry) < reconnectTimeout)
309 time(&lastReconnectTry);
313 if (SendOpenPDU(sock))
314 if (SendRReqPDU(sock))
316 reconnectTimeout = 1;
317 needReconnect = false;
318 logger("Connected successfully");
319 printfd(__FILE__, "Connected successfully\n");
324 if (reconnectTimeout < 60)
325 reconnectTimeout *= 2;
327 needReconnect = true;
331 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
333 auto it = smuxHandlers.find(pdus->present);
334 if (it != smuxHandlers.end())
335 return (this->*(it->second))(pdus);
339 switch (pdus->present)
341 case SMUX_PDUs_PR_NOTHING:
342 printfd(__FILE__, "PDUs: nothing\n");
344 case SMUX_PDUs_PR_open:
345 printfd(__FILE__, "PDUs: open\n");
347 case SMUX_PDUs_PR_registerRequest:
348 printfd(__FILE__, "PDUs: registerRequest\n");
351 printfd(__FILE__, "PDUs: undefined\n");
353 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
359 bool SMUX::UpdateTables()
363 auto it = tables.begin();
364 while (it != tables.end())
368 it->second->UpdateSensors(newSensors);
370 catch (const std::runtime_error & ex)
373 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
374 it->first.c_str(), ex.what());
382 auto sit = newSensors.begin();
383 while (sit != newSensors.end())
392 while (it != tables.end())
394 auto res = std::equal_range(sensors.begin(),
396 std::pair<OID, Sensor *>(OID(it->first), nullptr),
398 auto sit = res.first;
399 while (sit != res.second)
404 sensors.erase(res.first, res.second);
408 sensors.insert(newSensors.begin(), newSensors.end());
413 void SMUX::SetNotifier(UserPtr userPtr)
415 notifiers.emplace_back(*this, userPtr);
416 userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back());
419 void SMUX::UnsetNotifier(UserPtr userPtr)
421 auto it = notifiers.begin();
422 while (it != notifiers.end())
424 if (it->GetUserPtr() == userPtr)
426 userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
434 void SMUX::SetNotifiers()
436 int h = users->OpenSearch();
437 assert(h && "USERS::OpenSearch is always correct");
440 while (users->SearchNext(h, &u) == 0)
443 users->CloseSearch(h);
445 m_onAddUserConn = users->onUserAdd([this](auto user){
449 m_onDelUserConn = users->onUserDel([this](auto user){
454 tariffs->AddNotifierAdd(&addDelTariffNotifier);
455 tariffs->AddNotifierDel(&addDelTariffNotifier);
458 void SMUX::ResetNotifiers()
460 tariffs->DelNotifierDel(&addDelTariffNotifier);
461 tariffs->DelNotifierAdd(&addDelTariffNotifier);
463 m_onAddUserConn.disconnect();
464 m_onDelUserConn.disconnect();
466 auto it = notifiers.begin();
467 while (it != notifiers.end())
469 it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));