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"
25 #include "stg/common.h"
27 bool WaitPackets(int sd);
29 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
34 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
39 for (int i = 0; i < count; ++i)
43 strprintf(&arc, "%d", arcs[i]);
50 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
52 size_t left = 0, pos = 0, arcPos = 0;
54 pos = str.find_first_of('.', left);
58 pos = str.find_first_of('.', left);
60 while (pos != std::string::npos)
63 if (str2x(str.substr(left, left - pos), arc))
69 pos = str.find_first_of('.', left);
71 if (left < str.length())
74 if (str2x(str.substr(left, left - pos), arc))
80 printfd(__FILE__, "String2OI() - arcPos: %d\n", arcPos);
81 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos);
85 class SNMP_AGENT_CREATOR
88 SNMP_AGENT * snmpAgent;
92 : snmpAgent(new SNMP_AGENT())
97 printfd(__FILE__, "SNMP_AGENT_CREATOR::~SNMP_AGENT_CREATOR()\n");
101 SNMP_AGENT * GetPlugin()
107 SNMP_AGENT_CREATOR sac;
111 return sac.GetPlugin();
114 int SendOpenPDU(int fd)
116 const char * description = "Stg SNMP Agent";
117 //int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1, 5, 2, 1, 1};
118 asn_enc_rval_t error;
121 memset(&msg, 0, sizeof(msg));
123 msg.present = OpenPDU_PR_simple;
124 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
125 /*OBJECT_IDENTIFIER_set_arcs(&msg.choice.simple.identity,
129 if (!String2OI(".1.3.6.1.4.1.9363", &msg.choice.simple.identity))
131 printfd(__FILE__, "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
134 OCTET_STRING_fromString(&msg.choice.simple.description,
136 OCTET_STRING_fromString(&msg.choice.simple.password,
140 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
142 if (error.encoded == -1)
144 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
145 error.failed_type ? error.failed_type->name : "unknown");
150 write(fd, buffer, error.encoded);
151 printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n", error.encoded);
156 int SendClosePDU(int fd)
160 memset(&msg, 0, sizeof(msg));
162 asn_long2INTEGER(&msg, ClosePDU_goingDown);
165 asn_enc_rval_t error;
166 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
168 if (error.encoded == -1)
170 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
171 error.failed_type ? error.failed_type->name : "unknown");
176 write(fd, buffer, error.encoded);
177 printfd(__FILE__, "ClosePDU encoded successfully\n");
182 int SendRReqPDU(int fd)
184 int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1};
185 asn_enc_rval_t error;
188 memset(&msg, 0, sizeof(msg));
191 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
192 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
198 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
200 if (error.encoded == -1)
202 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
203 error.failed_type ? error.failed_type->name : "unknown");
208 write(fd, buffer, error.encoded);
209 printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n", error.encoded);
214 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
216 asn_enc_rval_t error;
219 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer, sizeof(buffer));
221 if (error.encoded == -1)
223 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
224 error.failed_type ? error.failed_type->name : "unknown");
229 write(fd, buffer, error.encoded);
230 printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n", error.encoded);
235 int SendGetResponseErrorPDU(int fd, const PDU_t * getRequest, int errorStatus, int errorIndex)
237 asn_enc_rval_t error;
238 GetResponse_PDU_t msg;
240 memset(&msg, 0, sizeof(msg));
242 msg.request_id = getRequest->request_id;
243 asn_long2INTEGER(&msg.error_status, errorStatus);
244 asn_long2INTEGER(&msg.error_index, errorIndex);
247 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer, sizeof(buffer));
249 if (error.encoded == -1)
251 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
252 error.failed_type ? error.failed_type->name : "unknown");
257 write(fd, buffer, error.encoded);
258 printfd(__FILE__, "GetResponsePDU for error encoded successfully to %d bytes\n", error.encoded);
263 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
266 SMUX_PDUs_t * pdus = NULL;
268 memset(buffer, 0, sizeof(buffer));
270 size_t length = read(fd, buffer, sizeof(buffer));
273 asn_dec_rval_t error;
274 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
275 if(error.code != RC_OK)
277 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
278 (long)error.consumed);
284 int ParseIntInRange(const std::string & str,
289 if (str2x(str.c_str(), *val))
293 if (*val < min || *val > max)
300 SNMP_AGENT_SETTINGS::SNMP_AGENT_SETTINGS()
305 int SNMP_AGENT_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
308 std::vector<PARAM_VALUE>::const_iterator pvi;
310 ///////////////////////////
312 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
313 if (pvi == s.moduleParams.end())
315 errorStr = "Parameter \'Port\' not found.";
316 printfd(__FILE__, "Parameter 'Port' not found\n");
319 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
321 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
322 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
327 pv.param = "Password";
328 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
329 if (pvi == s.moduleParams.end())
331 errorStr = "Parameter \'Password\' not found.";
332 printfd(__FILE__, "Parameter 'Password' not found\n");
337 password = pvi->value[0];
341 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
342 if (pvi == s.moduleParams.end())
344 errorStr = "Parameter \'Server\' not found.";
345 printfd(__FILE__, "Parameter 'Server' not found\n");
348 ip = inet_strington(pvi->value[0]);
353 SNMP_AGENT::SNMP_AGENT()
361 pthread_mutex_init(&mutex, NULL);
363 smuxHandlers[SMUX_PDUs_PR_close] = &SNMP_AGENT::CloseHandler;
364 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SNMP_AGENT::RegisterResponseHandler;
365 smuxHandlers[SMUX_PDUs_PR_pdus] = &SNMP_AGENT::PDUsHandler;
366 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SNMP_AGENT::CommitOrRollbackHandler;
368 pdusHandlers[PDUs_PR_get_request] = &SNMP_AGENT::GetRequestHandler;
369 pdusHandlers[PDUs_PR_get_next_request] = &SNMP_AGENT::GetNextRequestHandler;
370 pdusHandlers[PDUs_PR_set_request] = &SNMP_AGENT::SetRequestHandler;
373 SNMP_AGENT::~SNMP_AGENT()
375 Sensors::iterator it;
376 for (it = sensors.begin(); it != sensors.end(); ++it)
378 printfd(__FILE__, "SNMP_AGENT::~SNMP_AGENT()\n");
379 pthread_mutex_destroy(&mutex);
382 int SNMP_AGENT::ParseSettings()
384 return snmpAgentSettings.ParseSettings(settings);
387 int SNMP_AGENT::Start()
393 sensors[".1.3.6.1.4.1.9363.1.1.1"] = new TotalUsersSensor(*users);
394 sensors[".1.3.6.1.4.1.9363.1.1.2"] = new ConnectedUsersSensor(*users);
395 sensors[".1.3.6.1.4.1.9363.1.1.3"] = new AuthorizedUsersSensor(*users);
396 sensors[".1.3.6.1.4.1.9363.1.1.4"] = new AlwaysOnlineUsersSensor(*users);
397 sensors[".1.3.6.1.4.1.9363.1.1.5"] = new NoCashUsersSensor(*users);
398 sensors[".1.3.6.1.4.1.9363.1.1.7"] = new DisabledDetailStatsUsersSensor(*users);
399 sensors[".1.3.6.1.4.1.9363.1.1.8"] = new DisabledUsersSensor(*users);
400 sensors[".1.3.6.1.4.1.9363.1.1.9"] = new PassiveUsersSensor(*users);
401 sensors[".1.3.6.1.4.1.9363.1.1.10"] = new CreditUsersSensor(*users);
402 sensors[".1.3.6.1.4.1.9363.1.1.11"] = new FreeMbUsersSensor(*users);
404 sensors[".1.3.6.1.4.1.9363.1.2.1"] = new TotalTariffsSensor(*tariffs);
408 if (pthread_create(&thread, NULL, Runner, this))
410 errorStr = "Cannot create thread.";
411 printfd(__FILE__, "Cannot create thread\n");
419 int SNMP_AGENT::Stop()
421 printfd(__FILE__, "SNMP_AGENT::Stop() - Before\n");
426 //5 seconds to thread stops itself
427 for (int i = 0; i < 25 && !stopped; i++)
429 struct timespec ts = {0, 200000000};
430 nanosleep(&ts, NULL);
433 //after 5 seconds waiting thread still running. now killing it
436 printfd(__FILE__, "SNMP_AGENT::Stop() - failed to stop thread, killing it\n");
437 if (pthread_kill(thread, SIGINT))
439 errorStr = "Cannot kill thread.";
440 printfd(__FILE__, "SNMP_AGENT::Stop() - Cannot kill thread\n");
443 printfd(__FILE__, "SNMP_AGENT::Stop() - killed Run\n");
447 pthread_join(thread, NULL);
451 printfd(__FILE__, "SNMP_AGENT::Stop() - After\n");
455 void * SNMP_AGENT::Runner(void * d)
457 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
464 void SNMP_AGENT::Run()
470 printfd(__FILE__, "SNMP_AGENT::Run() - Before\n");
473 if (WaitPackets(sock))
475 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
482 printfd(__FILE__, "SNMP_AGENT::Run() - After\n");
487 bool SNMP_AGENT::PrepareNet()
489 sock = socket(AF_INET, SOCK_STREAM, 0);
493 errorStr = "Cannot create socket.";
494 printfd(__FILE__, "Cannot create socket\n");
498 struct sockaddr_in addr;
500 addr.sin_family = AF_INET;
501 addr.sin_port = htons(snmpAgentSettings.GetPort());
502 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
504 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
506 errorStr = "Cannot connect.";
507 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
514 bool WaitPackets(int sd)
524 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
525 if (res == -1) // Error
529 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
534 if (res == 0) // Timeout
542 bool SNMP_AGENT::DispatchPDUs(const SMUX_PDUs_t * pdus)
544 SMUXHandlers::iterator it;
545 it = smuxHandlers.find(pdus->present);
546 if (it != smuxHandlers.end())
548 return (this->*(it->second))(pdus);
552 switch (pdus->present)
554 case SMUX_PDUs_PR_NOTHING:
555 printfd(__FILE__, "PDUs: nothing\n");
557 case SMUX_PDUs_PR_open:
558 printfd(__FILE__, "PDUs: open\n");
560 case SMUX_PDUs_PR_registerRequest:
561 printfd(__FILE__, "PDUs: registerRequest\n");
564 printfd(__FILE__, "PDUs: undefined\n");
566 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
571 bool SNMP_AGENT::CloseHandler(const SMUX_PDUs_t * pdus)
573 printfd(__FILE__, "SNMP_AGENT::CloseHandler()\n");
574 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
578 bool SNMP_AGENT::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
580 printfd(__FILE__, "SNMP_AGENT::RegisterResponseHandler()\n");
581 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
585 bool SNMP_AGENT::PDUsHandler(const SMUX_PDUs_t * pdus)
587 printfd(__FILE__, "SNMP_AGENT::PDUsHandler()\n");
588 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
589 PDUsHandlers::iterator it;
590 it = pdusHandlers.find(pdus->choice.pdus.present);
591 if (it != pdusHandlers.end())
593 return (this->*(it->second))(&pdus->choice.pdus);
597 switch (pdus->present)
599 case PDUs_PR_NOTHING:
600 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - nothing\n");
602 case PDUs_PR_get_response:
603 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - get response\n");
606 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - trap\n");
609 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - undefined\n");
615 bool SNMP_AGENT::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
617 printfd(__FILE__, "SNMP_AGENT::CommitOrRollbackHandler()\n");
618 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
622 bool SNMP_AGENT::GetRequestHandler(const PDUs_t * pdus)
624 printfd(__FILE__, "SNMP_AGENT::GetRequestHandler()\n");
625 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
626 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
627 GetResponse_PDU_t msg;
628 VarBindList_t * varBindList = &msg.variable_bindings;
629 memset(&msg, 0, sizeof(msg));
631 msg.request_id = getRequest->request_id;
632 asn_long2INTEGER(&msg.error_status, 0);
633 asn_long2INTEGER(&msg.error_index, 0);
635 const VarBindList_t * vbl = &getRequest->variable_bindings;
636 for (int i = 0; i < vbl->list.count; ++i)
638 VarBind_t * vb = getRequest->variable_bindings.list.array[i];
639 Sensors::iterator it;
640 it = sensors.find(OI2String(&vb->name));
641 if (it == sensors.end())
643 SendGetResponseErrorPDU(sock, getRequest, PDU__error_status_noSuchName, i);
648 memset(&newVb, 0, sizeof(newVb));
650 newVb.name = vb->name;
651 it->second->GetValue(&newVb.value);
653 ASN_SEQUENCE_ADD(varBindList, &newVb);
656 SendGetResponsePDU(sock, &msg);
657 asn_fprint(stderr, &asn_DEF_PDU, &msg);
661 bool SNMP_AGENT::GetNextRequestHandler(const PDUs_t * pdus)
663 printfd(__FILE__, "SNMP_AGENT::GetNextRequestHandler()\n");
664 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
668 bool SNMP_AGENT::SetRequestHandler(const PDUs_t * pdus)
670 printfd(__FILE__, "SNMP_AGENT::SetRequestHandler()\n");
671 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
672 SendGetResponseErrorPDU(sock, &pdus->choice.set_request, PDU__error_status_readOnly, 0);