2 #include <sys/socket.h>
13 #include "asn1/OpenPDU.h"
14 #include "asn1/ClosePDU.h"
15 #include "asn1/RReqPDU.h"
16 #include "asn1/OBJECT_IDENTIFIER.h"
17 #include "asn1/ber_decoder.h"
18 #include "asn1/der_encoder.h"
21 #include "stg/common.h"
23 bool WaitPackets(int sd);
25 class SNMP_AGENT_CREATOR
28 SNMP_AGENT * snmpAgent;
32 : snmpAgent(new SNMP_AGENT())
37 printfd(__FILE__, "SNMP_AGENT_CREATOR::~SNMP_AGENT_CREATOR()\n");
41 SNMP_AGENT * GetPlugin()
47 SNMP_AGENT_CREATOR sac;
51 return sac.GetPlugin();
54 int SendOpenPDU(int fd)
56 const char * description = "Stg SNMP Agent";
57 int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1, 5, 2, 1, 1};
61 memset(&msg, 0, sizeof(msg));
63 msg.present = OpenPDU_PR_simple;
64 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
65 OBJECT_IDENTIFIER_set_arcs(&msg.choice.simple.identity,
69 OCTET_STRING_fromString(&msg.choice.simple.description,
71 OCTET_STRING_fromString(&msg.choice.simple.password,
75 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
77 if (error.encoded == -1)
79 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
80 error.failed_type ? error.failed_type->name : "unknown");
85 write(fd, buffer, error.encoded);
86 printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n", error.encoded);
91 int SendClosePDU(int fd)
95 memset(&msg, 0, sizeof(msg));
97 asn_long2INTEGER(&msg, ClosePDU_goingDown);
100 asn_enc_rval_t error;
101 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
103 if (error.encoded == -1)
105 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
106 error.failed_type ? error.failed_type->name : "unknown");
111 write(fd, buffer, error.encoded);
112 printfd(__FILE__, "ClosePDU encoded successfully\n");
117 int SendRReqPDU(int fd)
119 int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1};
120 asn_enc_rval_t error;
123 memset(&msg, 0, sizeof(msg));
126 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
127 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
133 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
135 if (error.encoded == -1)
137 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
138 error.failed_type ? error.failed_type->name : "unknown");
143 write(fd, buffer, error.encoded);
144 printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n", error.encoded);
149 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
152 SMUX_PDUs_t * pdus = NULL;
154 memset(buffer, 0, sizeof(buffer));
156 size_t length = read(fd, buffer, sizeof(buffer));
159 asn_dec_rval_t error;
160 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
161 if(error.code != RC_OK)
163 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
164 (long)error.consumed);
170 int ParseIntInRange(const std::string & str,
175 if (str2x(str.c_str(), *val))
179 if (*val < min || *val > max)
186 SNMP_AGENT_SETTINGS::SNMP_AGENT_SETTINGS()
191 int SNMP_AGENT_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
194 std::vector<PARAM_VALUE>::const_iterator pvi;
196 ///////////////////////////
198 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
199 if (pvi == s.moduleParams.end())
201 errorStr = "Parameter \'Port\' not found.";
202 printfd(__FILE__, "Parameter 'Port' not found\n");
205 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
207 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
208 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
213 pv.param = "Password";
214 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
215 if (pvi == s.moduleParams.end())
217 errorStr = "Parameter \'Password\' not found.";
218 printfd(__FILE__, "Parameter 'Password' not found\n");
223 password = pvi->value[0];
227 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
228 if (pvi == s.moduleParams.end())
230 errorStr = "Parameter \'Server\' not found.";
231 printfd(__FILE__, "Parameter 'Server' not found\n");
234 ip = inet_strington(pvi->value[0]);
239 SNMP_AGENT::SNMP_AGENT()
245 pthread_mutex_init(&mutex, NULL);
247 smuxHandlers[SMUX_PDUs_PR_close] = &SNMP_AGENT::CloseHandler;
248 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SNMP_AGENT::RegisterResponseHandler;
249 smuxHandlers[SMUX_PDUs_PR_pdus] = &SNMP_AGENT::PDUsHandler;
250 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SNMP_AGENT::CommitOrRollbackHandler;
252 pdusHandlers[PDUs_PR_get_request] = &SNMP_AGENT::GetRequestHandler;
253 pdusHandlers[PDUs_PR_get_next_request] = &SNMP_AGENT::GetNextRequestHandler;
254 pdusHandlers[PDUs_PR_set_request] = &SNMP_AGENT::SetRequestHandler;
257 SNMP_AGENT::~SNMP_AGENT()
259 printfd(__FILE__, "SNMP_AGENT::~SNMP_AGENT()\n");
260 pthread_mutex_destroy(&mutex);
263 int SNMP_AGENT::ParseSettings()
265 return snmpAgentSettings.ParseSettings(settings);
268 int SNMP_AGENT::Start()
275 if (pthread_create(&thread, NULL, Runner, this))
277 errorStr = "Cannot create thread.";
278 printfd(__FILE__, "Cannot create thread\n");
286 int SNMP_AGENT::Stop()
288 printfd(__FILE__, "SNMP_AGENT::Stop() - Before\n");
293 //5 seconds to thread stops itself
294 for (int i = 0; i < 25 && !stopped; i++)
296 struct timespec ts = {0, 200000000};
297 nanosleep(&ts, NULL);
300 //after 5 seconds waiting thread still running. now killing it
303 printfd(__FILE__, "SNMP_AGENT::Stop() - failed to stop thread, killing it\n");
304 if (pthread_kill(thread, SIGINT))
306 errorStr = "Cannot kill thread.";
307 printfd(__FILE__, "SNMP_AGENT::Stop() - Cannot kill thread\n");
310 printfd(__FILE__, "SNMP_AGENT::Stop() - killed Run\n");
314 pthread_join(thread, NULL);
318 printfd(__FILE__, "SNMP_AGENT::Stop() - After\n");
322 void * SNMP_AGENT::Runner(void * d)
324 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
331 void SNMP_AGENT::Run()
337 printfd(__FILE__, "SNMP_AGENT::Run() - Before\n");
340 if (WaitPackets(sock))
342 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
349 printfd(__FILE__, "SNMP_AGENT::Run() - After\n");
354 bool SNMP_AGENT::PrepareNet()
356 sock = socket(AF_INET, SOCK_STREAM, 0);
360 errorStr = "Cannot create socket.";
361 printfd(__FILE__, "Cannot create socket\n");
365 struct sockaddr_in addr;
367 addr.sin_family = AF_INET;
368 addr.sin_port = htons(snmpAgentSettings.GetPort());
369 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
371 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
373 errorStr = "Cannot connect.";
374 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
381 bool WaitPackets(int sd)
391 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
392 if (res == -1) // Error
396 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
401 if (res == 0) // Timeout
409 bool SNMP_AGENT::DispatchPDUs(const SMUX_PDUs_t * pdus)
411 SMUXHandlers::iterator it;
412 it = smuxHandlers.find(pdus->present);
413 if (it != smuxHandlers.end())
415 return (this->*(it->second))(pdus);
419 switch (pdus->present)
421 case SMUX_PDUs_PR_NOTHING:
422 printfd(__FILE__, "PDUs: nothing\n");
424 case SMUX_PDUs_PR_open:
425 printfd(__FILE__, "PDUs: open\n");
427 case SMUX_PDUs_PR_registerRequest:
428 printfd(__FILE__, "PDUs: registerRequest\n");
431 printfd(__FILE__, "PDUs: undefined\n");
433 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
438 bool SNMP_AGENT::CloseHandler(const SMUX_PDUs_t * pdus)
440 printfd(__FILE__, "SNMP_AGENT::CloseHandler()\n");
441 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
445 bool SNMP_AGENT::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
447 printfd(__FILE__, "SNMP_AGENT::RegisterResponseHandler()\n");
448 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
452 bool SNMP_AGENT::PDUsHandler(const SMUX_PDUs_t * pdus)
454 printfd(__FILE__, "SNMP_AGENT::PDUsHandler()\n");
455 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
456 PDUsHandlers::iterator it;
457 it = pdusHandlers.find(pdus->choice.pdus.present);
458 if (it != pdusHandlers.end())
460 return (this->*(it->second))(&pdus->choice.pdus);
464 switch (pdus->present)
466 case PDUs_PR_NOTHING:
467 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - nothing\n");
469 case PDUs_PR_get_response:
470 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - get response\n");
473 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - trap\n");
476 printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - undefined\n");
482 bool SNMP_AGENT::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
484 printfd(__FILE__, "SNMP_AGENT::CommitOrRollbackHandler()\n");
485 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
489 bool SNMP_AGENT::GetRequestHandler(const PDUs_t * pdus)
491 printfd(__FILE__, "SNMP_AGENT::GetRequestHandler()\n");
492 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
496 bool SNMP_AGENT::GetNextRequestHandler(const PDUs_t * pdus)
498 printfd(__FILE__, "SNMP_AGENT::GetNextRequestHandler()\n");
499 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
503 bool SNMP_AGENT::SetRequestHandler(const PDUs_t * pdus)
505 printfd(__FILE__, "SNMP_AGENT::SetRequestHandler()\n");
506 asn_fprint(stderr, &asn_DEF_PDUs, pdus);