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 handlers[SMUX_PDUs_PR_close] = &SNMP_AGENT::CloseHandler;
248 handlers[SMUX_PDUs_PR_registerResponse] = &SNMP_AGENT::RegisterResponseHandler;
249 handlers[SMUX_PDUs_PR_pdus] = &SNMP_AGENT::PDUsHandler;
250 handlers[SMUX_PDUs_PR_commitOrRollback] = &SNMP_AGENT::CommitOrRollbackHandler;
253 SNMP_AGENT::~SNMP_AGENT()
255 printfd(__FILE__, "SNMP_AGENT::~SNMP_AGENT()\n");
256 pthread_mutex_destroy(&mutex);
259 int SNMP_AGENT::ParseSettings()
261 return snmpAgentSettings.ParseSettings(settings);
264 int SNMP_AGENT::Start()
271 if (pthread_create(&thread, NULL, Runner, this))
273 errorStr = "Cannot create thread.";
274 printfd(__FILE__, "Cannot create thread\n");
282 int SNMP_AGENT::Stop()
284 printfd(__FILE__, "SNMP_AGENT::Stop() - Before\n");
289 //5 seconds to thread stops itself
290 for (int i = 0; i < 25 && !stopped; i++)
292 struct timespec ts = {0, 200000000};
293 nanosleep(&ts, NULL);
296 //after 5 seconds waiting thread still running. now killing it
299 printfd(__FILE__, "SNMP_AGENT::Stop() - failed to stop thread, killing it\n");
300 if (pthread_kill(thread, SIGINT))
302 errorStr = "Cannot kill thread.";
303 printfd(__FILE__, "SNMP_AGENT::Stop() - Cannot kill thread\n");
306 printfd(__FILE__, "SNMP_AGENT::Stop() - killed Run\n");
310 pthread_join(thread, NULL);
314 printfd(__FILE__, "SNMP_AGENT::Stop() - After\n");
318 void * SNMP_AGENT::Runner(void * d)
320 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
327 void SNMP_AGENT::Run()
333 printfd(__FILE__, "SNMP_AGENT::Run() - Before\n");
336 if (WaitPackets(sock))
338 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
345 printfd(__FILE__, "SNMP_AGENT::Run() - After\n");
350 bool SNMP_AGENT::PrepareNet()
352 sock = socket(AF_INET, SOCK_STREAM, 0);
356 errorStr = "Cannot create socket.";
357 printfd(__FILE__, "Cannot create socket\n");
361 struct sockaddr_in addr;
363 addr.sin_family = AF_INET;
364 addr.sin_port = htons(snmpAgentSettings.GetPort());
365 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
367 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
369 errorStr = "Cannot connect.";
370 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
377 bool WaitPackets(int sd)
387 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
388 if (res == -1) // Error
392 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
397 if (res == 0) // Timeout
405 bool SNMP_AGENT::DispatchPDUs(const SMUX_PDUs_t * pdus)
407 std::map<SMUX_PDUs_PR, SNMPPacketHandler>::iterator it;
408 it = handlers.find(pdus->present);
409 if (it != handlers.end())
411 return (this->*(it->second))(pdus);
415 switch (pdus->present)
417 case SMUX_PDUs_PR_NOTHING:
418 printfd(__FILE__, "PDUs: nothing\n");
420 case SMUX_PDUs_PR_open:
421 printfd(__FILE__, "PDUs: open\n");
423 case SMUX_PDUs_PR_registerRequest:
424 printfd(__FILE__, "PDUs: registerRequest\n");
427 printfd(__FILE__, "PDUs: undefined\n");
429 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
434 bool SNMP_AGENT::CloseHandler(const SMUX_PDUs_t * pdus)
436 printfd(__FILE__, "SNMP_AGENT::CloseHandler()\n");
437 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
441 bool SNMP_AGENT::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
443 printfd(__FILE__, "SNMP_AGENT::RegisterResponseHandler()\n");
444 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
448 bool SNMP_AGENT::PDUsHandler(const SMUX_PDUs_t * pdus)
450 printfd(__FILE__, "SNMP_AGENT::PDUsHandler()\n");
451 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
455 bool SNMP_AGENT::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
457 printfd(__FILE__, "SNMP_AGENT::CommitOrRollbackHandler()\n");
458 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);