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 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
35 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
40 for (int i = 0; i < count; ++i)
44 strprintf(&arc, "%d", arcs[i]);
51 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
53 size_t left = 0, pos = 0, arcPos = 0;
55 pos = str.find_first_of('.', left);
59 pos = str.find_first_of('.', left);
61 while (pos != std::string::npos)
64 if (str2x(str.substr(left, left - pos), arc))
70 pos = str.find_first_of('.', left);
72 if (left < str.length())
75 if (str2x(str.substr(left, left - pos), arc))
81 printfd(__FILE__, "String2OI() - arcPos: %d\n", arcPos);
82 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos);
92 SMUX_CREATOR() : smux(new SMUX()) {}
93 ~SMUX_CREATOR() { delete smux; }
95 SMUX * GetPlugin() { return smux; }
102 return sac.GetPlugin();
105 int SendOpenPDU(int fd)
107 const char * description = "Stg SMUX Plugin";
108 //int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1, 5, 2, 1, 1};
109 asn_enc_rval_t error;
112 memset(&msg, 0, sizeof(msg));
114 msg.present = OpenPDU_PR_simple;
115 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
116 if (!String2OI(".1.3.6.1.4.1.38313", &msg.choice.simple.identity))
119 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
122 OCTET_STRING_fromString(&msg.choice.simple.description, description);
123 OCTET_STRING_fromString(&msg.choice.simple.password, "");
126 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
128 if (error.encoded == -1)
130 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
131 error.failed_type ? error.failed_type->name : "unknown");
136 write(fd, buffer, error.encoded);
137 printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n",
143 int SendClosePDU(int fd)
147 memset(&msg, 0, sizeof(msg));
149 asn_long2INTEGER(&msg, ClosePDU_goingDown);
152 asn_enc_rval_t error;
153 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
155 if (error.encoded == -1)
157 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
158 error.failed_type ? error.failed_type->name : "unknown");
163 write(fd, buffer, error.encoded);
164 printfd(__FILE__, "ClosePDU encoded successfully\n");
169 int SendRReqPDU(int fd)
171 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
172 asn_enc_rval_t error;
175 memset(&msg, 0, sizeof(msg));
178 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
179 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
185 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
187 if (error.encoded == -1)
189 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
190 error.failed_type ? error.failed_type->name : "unknown");
195 write(fd, buffer, error.encoded);
196 printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n",
202 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
204 asn_enc_rval_t error;
207 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
210 if (error.encoded == -1)
212 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
213 error.failed_type ? error.failed_type->name : "unknown");
218 write(fd, buffer, error.encoded);
219 printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n",
225 int SendGetResponseErrorPDU(int fd,
226 const PDU_t * getRequest,
230 asn_enc_rval_t error;
231 GetResponse_PDU_t msg;
233 memset(&msg, 0, sizeof(msg));
235 msg.request_id = getRequest->request_id;
236 asn_long2INTEGER(&msg.error_status, errorStatus);
237 asn_long2INTEGER(&msg.error_index, errorIndex);
240 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
243 if (error.encoded == -1)
245 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
246 error.failed_type ? error.failed_type->name : "unknown");
251 write(fd, buffer, error.encoded);
253 "GetResponsePDU for error encoded successfully to %d bytes\n",
259 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
262 SMUX_PDUs_t * pdus = NULL;
264 memset(buffer, 0, sizeof(buffer));
266 size_t length = read(fd, buffer, sizeof(buffer));
269 asn_dec_rval_t error;
270 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
271 if(error.code != RC_OK)
273 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
274 (long)error.consumed);
280 int ParseIntInRange(const std::string & str,
285 if (str2x(str.c_str(), *val))
289 if (*val < min || *val > max)
296 SMUX_SETTINGS::SMUX_SETTINGS()
301 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
304 std::vector<PARAM_VALUE>::const_iterator pvi;
306 ///////////////////////////
308 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
309 if (pvi == s.moduleParams.end())
311 errorStr = "Parameter \'Port\' not found.";
312 printfd(__FILE__, "Parameter 'Port' not found\n");
315 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
317 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
318 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
323 pv.param = "Password";
324 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
325 if (pvi == s.moduleParams.end())
327 errorStr = "Parameter \'Password\' not found.";
328 printfd(__FILE__, "Parameter 'Password' not found\n");
333 password = pvi->value[0];
337 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
338 if (pvi == s.moduleParams.end())
340 errorStr = "Parameter \'Server\' not found.";
341 printfd(__FILE__, "Parameter 'Server' not found\n");
344 ip = inet_strington(pvi->value[0]);
357 pthread_mutex_init(&mutex, NULL);
359 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
360 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
361 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsHandler;
362 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
364 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
365 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
366 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
371 Sensors::iterator it;
372 for (it = sensors.begin(); it != sensors.end(); ++it)
374 printfd(__FILE__, "SMUX::~SMUX()\n");
375 pthread_mutex_destroy(&mutex);
378 int SMUX::ParseSettings()
380 return smuxSettings.ParseSettings(settings);
389 sensors[".1.3.6.1.4.1.38313.1.1.1"] = new TotalUsersSensor(*users);
390 sensors[".1.3.6.1.4.1.38313.1.1.2"] = new ConnectedUsersSensor(*users);
391 sensors[".1.3.6.1.4.1.38313.1.1.3"] = new AuthorizedUsersSensor(*users);
392 sensors[".1.3.6.1.4.1.38313.1.1.4"] = new AlwaysOnlineUsersSensor(*users);
393 sensors[".1.3.6.1.4.1.38313.1.1.5"] = new NoCashUsersSensor(*users);
394 sensors[".1.3.6.1.4.1.38313.1.1.7"] = new DisabledDetailStatsUsersSensor(*users);
395 sensors[".1.3.6.1.4.1.38313.1.1.8"] = new DisabledUsersSensor(*users);
396 sensors[".1.3.6.1.4.1.38313.1.1.9"] = new PassiveUsersSensor(*users);
397 sensors[".1.3.6.1.4.1.38313.1.1.10"] = new CreditUsersSensor(*users);
398 sensors[".1.3.6.1.4.1.38313.1.1.11"] = new FreeMbUsersSensor(*users);
399 sensors[".1.3.6.1.4.1.38313.1.1.12"] = new TariffChangeUsersSensor(*users);
401 sensors[".1.3.6.1.4.1.38313.1.2.1"] = new TotalTariffsSensor(*tariffs);
405 if (pthread_create(&thread, NULL, Runner, this))
407 errorStr = "Cannot create thread.";
408 printfd(__FILE__, "Cannot create thread\n");
418 printfd(__FILE__, "SMUX::Stop() - Before\n");
423 //5 seconds to thread stops itself
424 for (int i = 0; i < 25 && !stopped; i++)
426 struct timespec ts = {0, 200000000};
427 nanosleep(&ts, NULL);
430 //after 5 seconds waiting thread still running. now killing it
433 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
434 if (pthread_kill(thread, SIGINT))
436 errorStr = "Cannot kill thread.";
437 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
440 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
444 pthread_join(thread, NULL);
448 printfd(__FILE__, "SMUX::Stop() - After\n");
452 void * SMUX::Runner(void * d)
454 SMUX * smux = static_cast<SMUX *>(d);
469 if (WaitPackets(sock))
471 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
482 bool SMUX::PrepareNet()
484 sock = socket(AF_INET, SOCK_STREAM, 0);
488 errorStr = "Cannot create socket.";
489 printfd(__FILE__, "Cannot create socket\n");
493 struct sockaddr_in addr;
495 addr.sin_family = AF_INET;
496 addr.sin_port = htons(smuxSettings.GetPort());
497 addr.sin_addr.s_addr = smuxSettings.GetIP();
499 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
501 errorStr = "Cannot connect.";
502 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
509 bool WaitPackets(int sd)
519 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
520 if (res == -1) // Error
524 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
529 if (res == 0) // Timeout
537 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
539 SMUXHandlers::iterator it;
540 it = smuxHandlers.find(pdus->present);
541 if (it != smuxHandlers.end())
543 return (this->*(it->second))(pdus);
547 switch (pdus->present)
549 case SMUX_PDUs_PR_NOTHING:
550 printfd(__FILE__, "PDUs: nothing\n");
552 case SMUX_PDUs_PR_open:
553 printfd(__FILE__, "PDUs: open\n");
555 case SMUX_PDUs_PR_registerRequest:
556 printfd(__FILE__, "PDUs: registerRequest\n");
559 printfd(__FILE__, "PDUs: undefined\n");
561 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);