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 addUserNotifier(*this),
104 delUserNotifier(*this),
105 addDelTariffNotifier(*this),
106 logger(STG::PluginLogger::get("smux"))
108 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
109 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
110 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
111 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
113 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
114 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
115 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
120 for (auto& kv : sensors)
122 for (auto& kv : tables)
124 printfd(__FILE__, "SMUX::~SMUX()\n");
127 int SMUX::ParseSettings()
129 return smuxSettings.ParseSettings(settings);
134 assert(users != nullptr && "users must not be NULL");
135 assert(tariffs != nullptr && "tariffs must not be NULL");
136 assert(admins != nullptr && "admins must not be NULL");
137 assert(services != nullptr && "services must not be NULL");
138 assert(corporations != nullptr && "corporations must not be NULL");
139 assert(traffcounter != nullptr && "traffcounter must not be NULL");
142 needReconnect = true;
145 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
146 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
147 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
148 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
149 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
150 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
151 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
152 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
154 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
160 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
162 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
164 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
166 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
169 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);
175 auto it = sensors.begin();
176 while (it != sensors.end())
178 printfd(__FILE__, "%s = %s\n",
179 it->first.ToString().c_str(),
180 it->second->ToString().c_str());
185 if (!m_thread.joinable())
186 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
193 printfd(__FILE__, "SMUX::Stop() - Before\n");
194 m_thread.request_stop();
198 //5 seconds to thread stops itself
199 for (int i = 0; i < 25 && !stopped; i++)
201 struct timespec ts = {0, 200000000};
202 nanosleep(&ts, nullptr);
213 for (auto& kv : sensors)
215 for (auto& kv : tables)
218 tables.erase(tables.begin(), tables.end());
219 sensors.erase(sensors.begin(), sensors.end());
228 printfd(__FILE__, "SMUX::Stop() - After\n");
232 int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
240 printfd(__FILE__, "SMUX reconnected succesfully.\n");
241 logger("Reconnected successfully.");
246 void SMUX::Run(std::stop_token token)
249 if (!SendOpenPDU(sock))
250 needReconnect = true;
251 if (!SendRReqPDU(sock))
252 needReconnect = true;
255 while (!token.stop_requested())
257 if (WaitPackets(sock) && !needReconnect)
259 auto* pdus = RecvSMUXPDUs(sock);
263 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
265 else if (!token.stop_requested())
268 else if (!token.stop_requested() && needReconnect)
270 if (token.stop_requested())
277 bool SMUX::PrepareNet()
279 sock = socket(AF_INET, SOCK_STREAM, 0);
283 errorStr = "Cannot create socket.";
284 logger("Cannot create a socket: %s", strerror(errno));
285 printfd(__FILE__, "Cannot create socket\n");
289 struct sockaddr_in addr;
291 addr.sin_family = AF_INET;
292 addr.sin_port = htons(smuxSettings.GetPort());
293 addr.sin_addr.s_addr = smuxSettings.GetIP();
295 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) != 0)
297 errorStr = "Cannot connect.";
298 logger("Cannot connect the socket: %s", strerror(errno));
299 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
306 bool SMUX::Reconnect()
308 if (needReconnect && difftime(time(nullptr), lastReconnectTry) < reconnectTimeout)
311 time(&lastReconnectTry);
315 if (SendOpenPDU(sock))
316 if (SendRReqPDU(sock))
318 reconnectTimeout = 1;
319 needReconnect = false;
320 logger("Connected successfully");
321 printfd(__FILE__, "Connected successfully\n");
326 if (reconnectTimeout < 60)
327 reconnectTimeout *= 2;
329 needReconnect = true;
333 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
335 auto it = smuxHandlers.find(pdus->present);
336 if (it != smuxHandlers.end())
337 return (this->*(it->second))(pdus);
341 switch (pdus->present)
343 case SMUX_PDUs_PR_NOTHING:
344 printfd(__FILE__, "PDUs: nothing\n");
346 case SMUX_PDUs_PR_open:
347 printfd(__FILE__, "PDUs: open\n");
349 case SMUX_PDUs_PR_registerRequest:
350 printfd(__FILE__, "PDUs: registerRequest\n");
353 printfd(__FILE__, "PDUs: undefined\n");
355 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
361 bool SMUX::UpdateTables()
365 auto it = tables.begin();
366 while (it != tables.end())
370 it->second->UpdateSensors(newSensors);
372 catch (const std::runtime_error & ex)
375 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
376 it->first.c_str(), ex.what());
384 auto sit = newSensors.begin();
385 while (sit != newSensors.end())
394 while (it != tables.end())
396 auto res = std::equal_range(sensors.begin(),
398 std::pair<OID, Sensor *>(OID(it->first), nullptr),
400 auto sit = res.first;
401 while (sit != res.second)
406 sensors.erase(res.first, res.second);
410 sensors.insert(newSensors.begin(), newSensors.end());
415 void SMUX::SetNotifier(UserPtr userPtr)
417 notifiers.emplace_back(*this, userPtr);
418 userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back());
421 void SMUX::UnsetNotifier(UserPtr userPtr)
423 auto it = notifiers.begin();
424 while (it != notifiers.end())
426 if (it->GetUserPtr() == userPtr)
428 userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
436 void SMUX::SetNotifiers()
438 int h = users->OpenSearch();
439 assert(h && "USERS::OpenSearch is always correct");
442 while (users->SearchNext(h, &u) == 0)
445 users->CloseSearch(h);
447 users->AddNotifierUserAdd(&addUserNotifier);
448 users->AddNotifierUserDel(&delUserNotifier);
450 tariffs->AddNotifierAdd(&addDelTariffNotifier);
451 tariffs->AddNotifierDel(&addDelTariffNotifier);
454 void SMUX::ResetNotifiers()
456 tariffs->DelNotifierDel(&addDelTariffNotifier);
457 tariffs->DelNotifierAdd(&addDelTariffNotifier);
459 users->DelNotifierUserDel(&delUserNotifier);
460 users->DelNotifierUserAdd(&addUserNotifier);
462 auto it = notifiers.begin();
463 while (it != notifiers.end())
465 it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));