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, const PDU_t * pdu)
216 asn_enc_rval_t error;
217 GetResponse_PDU_t msg;
219 memset(&msg, 0, sizeof(msg));
221 msg.request_id = pdu->request_id;
222 asn_long2INTEGER(&msg.error_status, 0);
223 asn_long2INTEGER(&msg.error_index, 0);
227 memset(&vb, 0, sizeof(vb));
229 if (!String2OI(".1.3.6.1.4.1.9363.1", &vb.name))
231 printfd(__FILE__, "SendSetResponsePDU() - failed to convert string to OBJECT_IDENTIFIER\n");
235 ObjectSyntax_t * objectSyntax = &vb.value;
236 objectSyntax->present = ObjectSyntax_PR_simple;
237 SimpleSyntax_t * simpleSyntax = &objectSyntax->choice.simple;
238 simpleSyntax->present = SimpleSyntax_PR_number;
239 asn_long2INTEGER(&simpleSyntax->choice.number, 1);
242 ASN_SEQUENCE_ADD(&msg.variable_bindings, &vb);
244 /*const VarBindList_t * vbl = &pdu->variable_bindings;
245 for (int i = 0; i < vbl->list.count; ++i)
247 VarBind_t * vb = pdu->variable_bindings.list.array[i];
248 printfd(__FILE__, "OID: %s\n", OI2String(&vb->name).c_str());
249 asn_fprint(stderr, &asn_DEF_ObjectSyntax, &vb->value);
253 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer, sizeof(buffer));
255 if (error.encoded == -1)
257 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
258 error.failed_type ? error.failed_type->name : "unknown");
263 write(fd, buffer, error.encoded);
264 printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n", error.encoded);
269 int SendSetResponsePDU(int fd, const PDU_t * pdu)
271 //int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1};
272 asn_enc_rval_t error;
273 GetResponse_PDU_t msg;
275 memset(&msg, 0, sizeof(msg));
277 msg.request_id = pdu->request_id;
278 asn_long2INTEGER(&msg.error_status, PDU__error_status_readOnly);
279 asn_long2INTEGER(&msg.error_index, 0);
282 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer, sizeof(buffer));
284 if (error.encoded == -1)
286 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
287 error.failed_type ? error.failed_type->name : "unknown");
292 write(fd, buffer, error.encoded);
293 printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n", error.encoded);
298 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
301 SMUX_PDUs_t * pdus = NULL;
303 memset(buffer, 0, sizeof(buffer));
305 size_t length = read(fd, buffer, sizeof(buffer));
308 asn_dec_rval_t error;
309 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
310 if(error.code != RC_OK)
312 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
313 (long)error.consumed);
319 int ParseIntInRange(const std::string & str,
324 if (str2x(str.c_str(), *val))
328 if (*val < min || *val > max)
335 SNMP_AGENT_SETTINGS::SNMP_AGENT_SETTINGS()
340 int SNMP_AGENT_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
343 std::vector<PARAM_VALUE>::const_iterator pvi;
345 ///////////////////////////
347 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
348 if (pvi == s.moduleParams.end())
350 errorStr = "Parameter \'Port\' not found.";
351 printfd(__FILE__, "Parameter 'Port' not found\n");
354 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
356 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
357 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
362 pv.param = "Password";
363 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
364 if (pvi == s.moduleParams.end())
366 errorStr = "Parameter \'Password\' not found.";
367 printfd(__FILE__, "Parameter 'Password' not found\n");
372 password = pvi->value[0];
376 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
377 if (pvi == s.moduleParams.end())
379 errorStr = "Parameter \'Server\' not found.";
380 printfd(__FILE__, "Parameter 'Server' not found\n");
383 ip = inet_strington(pvi->value[0]);
388 SNMP_AGENT::SNMP_AGENT()
394 pthread_mutex_init(&mutex, NULL);
396 smuxHandlers[SMUX_PDUs_PR_close] = &SNMP_AGENT::CloseHandler;
397 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SNMP_AGENT::RegisterResponseHandler;
398 smuxHandlers[SMUX_PDUs_PR_pdus] = &SNMP_AGENT::PDUsHandler;
399 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SNMP_AGENT::CommitOrRollbackHandler;
401 pdusHandlers[PDUs_PR_get_request] = &SNMP_AGENT::GetRequestHandler;
402 pdusHandlers[PDUs_PR_get_next_request] = &SNMP_AGENT::GetNextRequestHandler;
403 pdusHandlers[PDUs_PR_set_request] = &SNMP_AGENT::SetRequestHandler;
406 SNMP_AGENT::~SNMP_AGENT()
408 printfd(__FILE__, "SNMP_AGENT::~SNMP_AGENT()\n");
409 pthread_mutex_destroy(&mutex);
412 int SNMP_AGENT::ParseSettings()
414 return snmpAgentSettings.ParseSettings(settings);
417 int SNMP_AGENT::Start()
424 if (pthread_create(&thread, NULL, Runner, this))
426 errorStr = "Cannot create thread.";
427 printfd(__FILE__, "Cannot create thread\n");
435 int SNMP_AGENT::Stop()
437 printfd(__FILE__, "SNMP_AGENT::Stop() - Before\n");
442 //5 seconds to thread stops itself
443 for (int i = 0; i < 25 && !stopped; i++)
445 struct timespec ts = {0, 200000000};
446 nanosleep(&ts, NULL);
449 //after 5 seconds waiting thread still running. now killing it
452 printfd(__FILE__, "SNMP_AGENT::Stop() - failed to stop thread, killing it\n");
453 if (pthread_kill(thread, SIGINT))
455 errorStr = "Cannot kill thread.";
456 printfd(__FILE__, "SNMP_AGENT::Stop() - Cannot kill thread\n");
459 printfd(__FILE__, "SNMP_AGENT::Stop() - killed Run\n");
463 pthread_join(thread, NULL);
467 printfd(__FILE__, "SNMP_AGENT::Stop() - After\n");
471 void * SNMP_AGENT::Runner(void * d)
473 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
480 void SNMP_AGENT::Run()
486 printfd(__FILE__, "SNMP_AGENT::Run() - Before\n");
489 if (WaitPackets(sock))
491 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
498 printfd(__FILE__, "SNMP_AGENT::Run() - After\n");
503 bool SNMP_AGENT::PrepareNet()
505 sock = socket(AF_INET, SOCK_STREAM, 0);
509 errorStr = "Cannot create socket.";
510 printfd(__FILE__, "Cannot create socket\n");
514 struct sockaddr_in addr;
516 addr.sin_family = AF_INET;
517 addr.sin_port = htons(snmpAgentSettings.GetPort());
518 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
520 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
522 errorStr = "Cannot connect.";
523 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
530 bool WaitPackets(int sd)
540 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
541 if (res == -1) // Error
545 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
550 if (res == 0) // Timeout
558 bool SNMP_AGENT::DispatchPDUs(const SMUX_PDUs_t * pdus)
560 SMUXHandlers::iterator it;
561 it = smuxHandlers.find(pdus->present);
562 if (it != smuxHandlers.end())
564 return (this->*(it->second))(pdus);
568 switch (pdus->present)
570 case SMUX_PDUs_PR_NOTHING:
571 printfd(__FILE__, "PDUs: nothing\n");
573 case SMUX_PDUs_PR_open:
574 printfd(__FILE__, "PDUs: open\n");
576 case SMUX_PDUs_PR_registerRequest:
577 printfd(__FILE__, "PDUs: registerRequest\n");
580 printfd(__FILE__, "PDUs: undefined\n");
582 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
587 bool SNMP_AGENT::CloseHandler(const SMUX_PDUs_t * pdus)
589 printfd(__FILE__, "SNMP_AGENT::CloseHandler()\n");
590 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
594 bool SNMP_AGENT::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
596 printfd(__FILE__, "SNMP_AGENT::RegisterResponseHandler()\n");
597 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
601 bool SNMP_AGENT::PDUsHandler(const SMUX_PDUs_t * pdus)
603 printfd(__FILE__, "SNMP_AGENT::PDUsHandler()\n");
604 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
605 PDUsHandlers::iterator it;
606 it = pdusHandlers.find(pdus->choice.pdus.present);
607 if (it != pdusHandlers.end())
609 return (this->*(it->second))(&pdus->choice.pdus);
613 switch (pdus->present)
615 case PDUs_PR_NOTHING:
616 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - nothing\n");
618 case PDUs_PR_get_response:
619 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - get response\n");
622 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - trap\n");
625 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - undefined\n");
631 bool SNMP_AGENT::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
633 printfd(__FILE__, "SNMP_AGENT::CommitOrRollbackHandler()\n");
634 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
638 bool SNMP_AGENT::GetRequestHandler(const PDUs_t * pdus)
640 printfd(__FILE__, "SNMP_AGENT::GetRequestHandler()\n");
641 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
642 SendGetResponsePDU(sock, &pdus->choice.get_request);
646 bool SNMP_AGENT::GetNextRequestHandler(const PDUs_t * pdus)
648 printfd(__FILE__, "SNMP_AGENT::GetNextRequestHandler()\n");
649 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
653 bool SNMP_AGENT::SetRequestHandler(const PDUs_t * pdus)
655 printfd(__FILE__, "SNMP_AGENT::SetRequestHandler()\n");
656 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
657 SendSetResponsePDU(sock, &pdus->choice.set_request);