2 #include <sys/socket.h>
13 #include "asn1/OpenPDU.h"
14 #include "asn1/ClosePDU.h"
15 #include "asn1/RReqPDU.h"
16 #include "asn1/GetRequest-PDU.h"
17 #include "asn1/GetResponse-PDU.h"
18 #include "asn1/VarBindList.h"
19 #include "asn1/VarBind.h"
20 #include "asn1/OBJECT_IDENTIFIER.h"
21 #include "asn1/ber_decoder.h"
22 #include "asn1/der_encoder.h"
24 #include "stg/common.h"
28 bool WaitPackets(int sd);
30 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
32 size_t left = 0, pos = 0, arcPos = 0;
34 pos = str.find_first_of('.', left);
38 pos = str.find_first_of('.', left);
40 while (pos != std::string::npos)
43 if (str2x(str.substr(left, left - pos), arc))
49 pos = str.find_first_of('.', left);
51 if (left < str.length())
54 if (str2x(str.substr(left, left - pos), arc))
60 printfd(__FILE__, "String2OI() - arcPos: %d\n", arcPos);
61 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos);
71 SMUX_CREATOR() : smux(new SMUX()) {}
72 ~SMUX_CREATOR() { delete smux; }
74 SMUX * GetPlugin() { return smux; }
81 return sac.GetPlugin();
84 int SendOpenPDU(int fd)
86 const char * description = "Stg SMUX Plugin";
87 //int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1, 5, 2, 1, 1};
91 memset(&msg, 0, sizeof(msg));
93 msg.present = OpenPDU_PR_simple;
94 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
95 if (!String2OI(".1.3.6.1.4.1.38313", &msg.choice.simple.identity))
98 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
101 OCTET_STRING_fromString(&msg.choice.simple.description, description);
102 OCTET_STRING_fromString(&msg.choice.simple.password, "");
105 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
107 if (error.encoded == -1)
109 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
110 error.failed_type ? error.failed_type->name : "unknown");
115 write(fd, buffer, error.encoded);
116 printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n",
122 int SendClosePDU(int fd)
126 memset(&msg, 0, sizeof(msg));
128 asn_long2INTEGER(&msg, ClosePDU_goingDown);
131 asn_enc_rval_t error;
132 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
134 if (error.encoded == -1)
136 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
137 error.failed_type ? error.failed_type->name : "unknown");
142 write(fd, buffer, error.encoded);
143 printfd(__FILE__, "ClosePDU encoded successfully\n");
148 int SendRReqPDU(int fd)
150 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
151 asn_enc_rval_t error;
154 memset(&msg, 0, sizeof(msg));
157 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
158 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
164 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
166 if (error.encoded == -1)
168 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
169 error.failed_type ? error.failed_type->name : "unknown");
174 write(fd, buffer, error.encoded);
175 printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n",
181 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
184 SMUX_PDUs_t * pdus = NULL;
186 memset(buffer, 0, sizeof(buffer));
188 size_t length = read(fd, buffer, sizeof(buffer));
191 asn_dec_rval_t error;
192 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
193 if(error.code != RC_OK)
195 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
196 (long)error.consumed);
202 int ParseIntInRange(const std::string & str,
207 if (str2x(str.c_str(), *val))
211 if (*val < min || *val > max)
218 SMUX_SETTINGS::SMUX_SETTINGS()
223 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
226 std::vector<PARAM_VALUE>::const_iterator pvi;
228 ///////////////////////////
230 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
231 if (pvi == s.moduleParams.end())
233 errorStr = "Parameter \'Port\' not found.";
234 printfd(__FILE__, "Parameter 'Port' not found\n");
237 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
239 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
240 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
245 pv.param = "Password";
246 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
247 if (pvi == s.moduleParams.end())
249 errorStr = "Parameter \'Password\' not found.";
250 printfd(__FILE__, "Parameter 'Password' not found\n");
255 password = pvi->value[0];
259 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
260 if (pvi == s.moduleParams.end())
262 errorStr = "Parameter \'Server\' not found.";
263 printfd(__FILE__, "Parameter 'Server' not found\n");
266 ip = inet_strington(pvi->value[0]);
279 pthread_mutex_init(&mutex, NULL);
281 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
282 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
283 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
284 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
286 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
287 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
288 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
293 Sensors::iterator it;
294 for (it = sensors.begin(); it != sensors.end(); ++it)
296 printfd(__FILE__, "SMUX::~SMUX()\n");
297 pthread_mutex_destroy(&mutex);
300 int SMUX::ParseSettings()
302 return smuxSettings.ParseSettings(settings);
311 sensors[".1.3.6.1.4.1.38313.1.1.1"] = new TotalUsersSensor(*users);
312 sensors[".1.3.6.1.4.1.38313.1.1.2"] = new ConnectedUsersSensor(*users);
313 sensors[".1.3.6.1.4.1.38313.1.1.3"] = new AuthorizedUsersSensor(*users);
314 sensors[".1.3.6.1.4.1.38313.1.1.4"] = new AlwaysOnlineUsersSensor(*users);
315 sensors[".1.3.6.1.4.1.38313.1.1.5"] = new NoCashUsersSensor(*users);
316 sensors[".1.3.6.1.4.1.38313.1.1.7"] = new DisabledDetailStatsUsersSensor(*users);
317 sensors[".1.3.6.1.4.1.38313.1.1.8"] = new DisabledUsersSensor(*users);
318 sensors[".1.3.6.1.4.1.38313.1.1.9"] = new PassiveUsersSensor(*users);
319 sensors[".1.3.6.1.4.1.38313.1.1.10"] = new CreditUsersSensor(*users);
320 sensors[".1.3.6.1.4.1.38313.1.1.11"] = new FreeMbUsersSensor(*users);
321 sensors[".1.3.6.1.4.1.38313.1.1.12"] = new TariffChangeUsersSensor(*users);
323 sensors[".1.3.6.1.4.1.38313.1.2.1"] = new TotalTariffsSensor(*tariffs);
327 if (pthread_create(&thread, NULL, Runner, this))
329 errorStr = "Cannot create thread.";
330 printfd(__FILE__, "Cannot create thread\n");
340 printfd(__FILE__, "SMUX::Stop() - Before\n");
345 //5 seconds to thread stops itself
346 for (int i = 0; i < 25 && !stopped; i++)
348 struct timespec ts = {0, 200000000};
349 nanosleep(&ts, NULL);
352 //after 5 seconds waiting thread still running. now killing it
355 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
356 if (pthread_kill(thread, SIGINT))
358 errorStr = "Cannot kill thread.";
359 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
362 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
366 pthread_join(thread, NULL);
370 printfd(__FILE__, "SMUX::Stop() - After\n");
374 void * SMUX::Runner(void * d)
376 SMUX * smux = static_cast<SMUX *>(d);
391 if (WaitPackets(sock))
393 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
404 bool SMUX::PrepareNet()
406 sock = socket(AF_INET, SOCK_STREAM, 0);
410 errorStr = "Cannot create socket.";
411 printfd(__FILE__, "Cannot create socket\n");
415 struct sockaddr_in addr;
417 addr.sin_family = AF_INET;
418 addr.sin_port = htons(smuxSettings.GetPort());
419 addr.sin_addr.s_addr = smuxSettings.GetIP();
421 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
423 errorStr = "Cannot connect.";
424 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
431 bool WaitPackets(int sd)
441 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
442 if (res == -1) // Error
446 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
451 if (res == 0) // Timeout
459 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
461 SMUXHandlers::iterator it;
462 it = smuxHandlers.find(pdus->present);
463 if (it != smuxHandlers.end())
465 return (this->*(it->second))(pdus);
469 switch (pdus->present)
471 case SMUX_PDUs_PR_NOTHING:
472 printfd(__FILE__, "PDUs: nothing\n");
474 case SMUX_PDUs_PR_open:
475 printfd(__FILE__, "PDUs: open\n");
477 case SMUX_PDUs_PR_registerRequest:
478 printfd(__FILE__, "PDUs: registerRequest\n");
481 printfd(__FILE__, "PDUs: undefined\n");
483 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);