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, 38313, 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.38313", &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, 38313, 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.38313.1.1.1"] = new TotalUsersSensor(*users);
394 sensors[".1.3.6.1.4.1.38313.1.1.2"] = new ConnectedUsersSensor(*users);
395 sensors[".1.3.6.1.4.1.38313.1.1.3"] = new AuthorizedUsersSensor(*users);
396 sensors[".1.3.6.1.4.1.38313.1.1.4"] = new AlwaysOnlineUsersSensor(*users);
397 sensors[".1.3.6.1.4.1.38313.1.1.5"] = new NoCashUsersSensor(*users);
398 sensors[".1.3.6.1.4.1.38313.1.1.7"] = new DisabledDetailStatsUsersSensor(*users);
399 sensors[".1.3.6.1.4.1.38313.1.1.8"] = new DisabledUsersSensor(*users);
400 sensors[".1.3.6.1.4.1.38313.1.1.9"] = new PassiveUsersSensor(*users);
401 sensors[".1.3.6.1.4.1.38313.1.1.10"] = new CreditUsersSensor(*users);
402 sensors[".1.3.6.1.4.1.38313.1.1.11"] = new FreeMbUsersSensor(*users);
403 sensors[".1.3.6.1.4.1.38313.1.1.12"] = new TariffChangeUsersSensor(*users);
405 sensors[".1.3.6.1.4.1.38313.1.2.1"] = new TotalTariffsSensor(*tariffs);
409 if (pthread_create(&thread, NULL, Runner, this))
411 errorStr = "Cannot create thread.";
412 printfd(__FILE__, "Cannot create thread\n");
420 int SNMP_AGENT::Stop()
422 printfd(__FILE__, "SNMP_AGENT::Stop() - Before\n");
427 //5 seconds to thread stops itself
428 for (int i = 0; i < 25 && !stopped; i++)
430 struct timespec ts = {0, 200000000};
431 nanosleep(&ts, NULL);
434 //after 5 seconds waiting thread still running. now killing it
437 printfd(__FILE__, "SNMP_AGENT::Stop() - failed to stop thread, killing it\n");
438 if (pthread_kill(thread, SIGINT))
440 errorStr = "Cannot kill thread.";
441 printfd(__FILE__, "SNMP_AGENT::Stop() - Cannot kill thread\n");
444 printfd(__FILE__, "SNMP_AGENT::Stop() - killed Run\n");
448 pthread_join(thread, NULL);
452 printfd(__FILE__, "SNMP_AGENT::Stop() - After\n");
456 void * SNMP_AGENT::Runner(void * d)
458 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
465 void SNMP_AGENT::Run()
471 printfd(__FILE__, "SNMP_AGENT::Run() - Before\n");
474 if (WaitPackets(sock))
476 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
483 printfd(__FILE__, "SNMP_AGENT::Run() - After\n");
488 bool SNMP_AGENT::PrepareNet()
490 sock = socket(AF_INET, SOCK_STREAM, 0);
494 errorStr = "Cannot create socket.";
495 printfd(__FILE__, "Cannot create socket\n");
499 struct sockaddr_in addr;
501 addr.sin_family = AF_INET;
502 addr.sin_port = htons(snmpAgentSettings.GetPort());
503 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
505 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
507 errorStr = "Cannot connect.";
508 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
515 bool WaitPackets(int sd)
525 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
526 if (res == -1) // Error
530 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
535 if (res == 0) // Timeout
543 bool SNMP_AGENT::DispatchPDUs(const SMUX_PDUs_t * pdus)
545 SMUXHandlers::iterator it;
546 it = smuxHandlers.find(pdus->present);
547 if (it != smuxHandlers.end())
549 return (this->*(it->second))(pdus);
553 switch (pdus->present)
555 case SMUX_PDUs_PR_NOTHING:
556 printfd(__FILE__, "PDUs: nothing\n");
558 case SMUX_PDUs_PR_open:
559 printfd(__FILE__, "PDUs: open\n");
561 case SMUX_PDUs_PR_registerRequest:
562 printfd(__FILE__, "PDUs: registerRequest\n");
565 printfd(__FILE__, "PDUs: undefined\n");
567 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
572 bool SNMP_AGENT::CloseHandler(const SMUX_PDUs_t * pdus)
574 printfd(__FILE__, "SNMP_AGENT::CloseHandler()\n");
575 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
579 bool SNMP_AGENT::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
581 printfd(__FILE__, "SNMP_AGENT::RegisterResponseHandler()\n");
582 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
586 bool SNMP_AGENT::PDUsHandler(const SMUX_PDUs_t * pdus)
588 printfd(__FILE__, "SNMP_AGENT::PDUsHandler()\n");
589 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
590 PDUsHandlers::iterator it;
591 it = pdusHandlers.find(pdus->choice.pdus.present);
592 if (it != pdusHandlers.end())
594 return (this->*(it->second))(&pdus->choice.pdus);
598 switch (pdus->present)
600 case PDUs_PR_NOTHING:
601 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - nothing\n");
603 case PDUs_PR_get_response:
604 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - get response\n");
607 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - trap\n");
610 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - undefined\n");
616 bool SNMP_AGENT::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
618 printfd(__FILE__, "SNMP_AGENT::CommitOrRollbackHandler()\n");
619 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
623 bool SNMP_AGENT::GetRequestHandler(const PDUs_t * pdus)
625 printfd(__FILE__, "SNMP_AGENT::GetRequestHandler()\n");
626 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
627 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
628 GetResponse_PDU_t msg;
629 VarBindList_t * varBindList = &msg.variable_bindings;
630 memset(&msg, 0, sizeof(msg));
632 msg.request_id = getRequest->request_id;
633 asn_long2INTEGER(&msg.error_status, 0);
634 asn_long2INTEGER(&msg.error_index, 0);
636 const VarBindList_t * vbl = &getRequest->variable_bindings;
637 for (int i = 0; i < vbl->list.count; ++i)
639 VarBind_t * vb = getRequest->variable_bindings.list.array[i];
640 Sensors::iterator it;
641 it = sensors.find(OI2String(&vb->name));
642 if (it == sensors.end())
644 SendGetResponseErrorPDU(sock, getRequest, PDU__error_status_noSuchName, i);
649 memset(&newVb, 0, sizeof(newVb));
651 newVb.name = vb->name;
652 it->second->GetValue(&newVb.value);
654 ASN_SEQUENCE_ADD(varBindList, &newVb);
657 SendGetResponsePDU(sock, &msg);
658 asn_fprint(stderr, &asn_DEF_PDU, &msg);
662 bool SNMP_AGENT::GetNextRequestHandler(const PDUs_t * pdus)
664 printfd(__FILE__, "SNMP_AGENT::GetNextRequestHandler()\n");
665 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
669 bool SNMP_AGENT::SetRequestHandler(const PDUs_t * pdus)
671 printfd(__FILE__, "SNMP_AGENT::SetRequestHandler()\n");
672 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
673 SendGetResponseErrorPDU(sock, &pdus->choice.set_request, PDU__error_status_readOnly, 0);