2 #include <sys/socket.h>
13 #include "asn1/OpenPDU.h"
14 #include "asn1/ClosePDU.h"
15 #include "asn1/SMUX-PDUs.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 class SNMP_AGENT_CREATOR
26 SNMP_AGENT * snmpAgent;
30 : snmpAgent(new SNMP_AGENT())
38 SNMP_AGENT * GetPlugin()
44 SNMP_AGENT_CREATOR sac;
48 return sac.GetPlugin();
51 int output(const void * buffer, size_t size, void * data)
53 int * fd = static_cast<int *>(data);
54 return write(*fd, buffer, size);
57 int SendOpenPDU(int fd)
59 const char * description = "Stg SNMP Agent";
60 int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1, 5, 2, 1, 1};
64 msg.present = OpenPDU_PR_simple;
65 asn_long2INTEGER(&msg.choice.simple.version, 1);
66 OBJECT_IDENTIFIER_set_arcs(&msg.choice.simple.identity,
70 OCTET_STRING_fromBuf(&msg.choice.simple.description,
73 OCTET_STRING_fromBuf(&msg.choice.simple.password,
77 error = der_encode(&asn_DEF_OpenPDU, &msg, output, &fd);
79 if (error.encoded == -1)
81 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
82 error.failed_type ? error.failed_type->name : "unknown");
87 printfd(__FILE__, "OpenPDU encoded successfully");
92 int SendClosePDU(int fd)
94 ClosePDU msg = ClosePDU_goingDown;
97 error = der_encode(&asn_DEF_ClosePDU, &msg, output, &fd);
99 if (error.encoded == -1)
101 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
102 error.failed_type ? error.failed_type->name : "unknown");
107 printfd(__FILE__, "ClosePDU encoded successfully");
112 int RecvSMUXPDUs(int fd)
117 size_t length = read(fd, buffer, sizeof(buffer));
118 asn_dec_rval_t error;
119 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
120 if(error.code != RC_OK)
122 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
123 (long)error.consumed);
126 switch (pdus->present)
128 case SMUX_PDUs_PR_NOTHING:
129 printfd(__FILE__, "PDUs: nothing\n");
131 case SMUX_PDUs_PR_open:
132 printfd(__FILE__, "PDUs: open\n");
134 case SMUX_PDUs_PR_close:
135 printfd(__FILE__, "PDUs: close\n");
137 case SMUX_PDUs_PR_registerRequest:
138 printfd(__FILE__, "PDUs: registerRequest\n");
140 case SMUX_PDUs_PR_registerResponse:
141 printfd(__FILE__, "PDUs: registerResponse\n");
143 case SMUX_PDUs_PR_pdus:
144 printfd(__FILE__, "PDUs: pdus\n");
146 case SMUX_PDUs_PR_commitOrRollback:
147 printfd(__FILE__, "PDUs: commitOrRollback\n");
150 printfd(__FILE__, "PDUs: default\n");
152 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
156 int ParseIntInRange(const std::string & str,
161 if (str2x(str.c_str(), *val))
165 if (*val < min || *val > max)
172 SNMP_AGENT_SETTINGS::SNMP_AGENT_SETTINGS()
177 int SNMP_AGENT_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
180 std::vector<PARAM_VALUE>::const_iterator pvi;
182 ///////////////////////////
184 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
185 if (pvi == s.moduleParams.end())
187 errorStr = "Parameter \'Port\' not found.";
188 printfd(__FILE__, "Parameter 'Port' not found\n");
191 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
193 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
194 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
199 pv.param = "Password";
200 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
201 if (pvi == s.moduleParams.end())
203 errorStr = "Parameter \'Password\' not found.";
204 printfd(__FILE__, "Parameter 'Password' not found\n");
209 password = pvi->value[0];
213 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
214 if (pvi == s.moduleParams.end())
216 errorStr = "Parameter \'Server\' not found.";
217 printfd(__FILE__, "Parameter 'Server' not found\n");
220 ip = inet_strington(pvi->value[0]);
225 SNMP_AGENT::SNMP_AGENT()
231 pthread_mutex_init(&mutex, NULL);
234 SNMP_AGENT::~SNMP_AGENT()
236 pthread_mutex_destroy(&mutex);
239 int SNMP_AGENT::ParseSettings()
241 return snmpAgentSettings.ParseSettings(settings);
244 int SNMP_AGENT::Start()
251 if (pthread_create(&thread, NULL, Runner, this))
253 errorStr = "Cannot create thread.";
254 printfd(__FILE__, "Cannot create thread\n");
262 int SNMP_AGENT::Stop()
270 //5 seconds to thread stops itself
271 for (int i = 0; i < 25 && !stopped; i++)
273 struct timespec ts = {0, 200000000};
274 nanosleep(&ts, NULL);
277 //after 5 seconds waiting thread still running. now killing it
280 if (pthread_kill(thread, SIGINT))
282 errorStr = "Cannot kill thread.";
283 printfd(__FILE__, "Cannot kill thread\n");
286 printfd(__FILE__, "SNMP_AGENT killed Run\n");
293 void * SNMP_AGENT::Runner(void * d)
295 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
302 void SNMP_AGENT::Run()
309 struct timespec ts = {1, 0};
310 nanosleep(&ts, NULL);
316 bool SNMP_AGENT::PrepareNet()
318 sock = socket(AF_INET, SOCK_STREAM, 0);
322 errorStr = "Cannot create socket.";
323 printfd(__FILE__, "Cannot create socket\n");
327 struct sockaddr_in addr;
329 addr.sin_family = AF_INET;
330 addr.sin_port = htons(snmpAgentSettings.GetPort());
331 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
333 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
335 errorStr = "Cannot connect.";
336 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));