+int SendClosePDU(int fd)
+{
+ClosePDU msg = ClosePDU_goingDown;
+
+asn_enc_rval_t error;
+error = der_encode(&asn_DEF_ClosePDU, &msg, output, &fd);
+
+if (error.encoded == -1)
+ {
+ printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
+ error.failed_type ? error.failed_type->name : "unknown");
+ return -1;
+ }
+else
+ {
+ printfd(__FILE__, "ClosePDU encoded successfully");
+ }
+return 0;
+}
+
+int RecvSMUXPDUs(int fd)
+{
+char buffer[8192];
+SMUX_PDUs * pdus;
+
+size_t length = read(fd, buffer, sizeof(buffer));
+asn_dec_rval_t error;
+error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
+if(error.code != RC_OK)
+ {
+ printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
+ (long)error.consumed);
+ return -1;
+ }
+switch (pdus->present)
+ {
+ case SMUX_PDUs_PR_NOTHING:
+ printfd(__FILE__, "PDUs: nothing\n");
+ break;
+ case SMUX_PDUs_PR_open:
+ printfd(__FILE__, "PDUs: open\n");
+ break;
+ case SMUX_PDUs_PR_close:
+ printfd(__FILE__, "PDUs: close\n");
+ break;
+ case SMUX_PDUs_PR_registerRequest:
+ printfd(__FILE__, "PDUs: registerRequest\n");
+ break;
+ case SMUX_PDUs_PR_registerResponse:
+ printfd(__FILE__, "PDUs: registerResponse\n");
+ break;
+ case SMUX_PDUs_PR_pdus:
+ printfd(__FILE__, "PDUs: pdus\n");
+ break;
+ case SMUX_PDUs_PR_commitOrRollback:
+ printfd(__FILE__, "PDUs: commitOrRollback\n");
+ break;
+ default:
+ printfd(__FILE__, "PDUs: default\n");
+ }
+asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
+return 0;
+}
+
+int ParseIntInRange(const std::string & str,
+ int min,
+ int max,
+ int * val)
+{
+if (str2x(str.c_str(), *val))
+ {
+ return -1;
+ }
+if (*val < min || *val > max)
+ {
+ return -1;
+ }
+return 0;
+}
+
+SNMP_AGENT_SETTINGS::SNMP_AGENT_SETTINGS()
+ : ip(0),
+ port(0)
+{}
+
+int SNMP_AGENT_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
+{
+PARAM_VALUE pv;
+std::vector<PARAM_VALUE>::const_iterator pvi;
+int p;
+///////////////////////////
+pv.param = "Port";
+pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+if (pvi == s.moduleParams.end())
+ {
+ errorStr = "Parameter \'Port\' not found.";
+ printfd(__FILE__, "Parameter 'Port' not found\n");
+ return -1;
+ }
+if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
+ {
+ errorStr = "Cannot parse parameter \'Port\': " + errorStr;
+ printfd(__FILE__, "Cannot parse parameter 'Port'\n");
+ return -1;
+ }
+port = p;
+
+pv.param = "Password";
+pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+if (pvi == s.moduleParams.end())
+ {
+ errorStr = "Parameter \'Password\' not found.";
+ printfd(__FILE__, "Parameter 'Password' not found\n");
+ password = "";
+ }
+else
+ {
+ password = pvi->value[0];
+ }
+
+pv.param = "Server";
+pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+if (pvi == s.moduleParams.end())
+ {
+ errorStr = "Parameter \'Server\' not found.";
+ printfd(__FILE__, "Parameter 'Server' not found\n");
+ return -1;
+ }
+ip = inet_strington(pvi->value[0]);
+
+return 0;
+}
+