+ write(fd, buffer, error.encoded);
+ printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n", error.encoded);
+ }
+return 0;
+}
+
+int SendClosePDU(int fd)
+{
+ClosePDU_t msg;
+
+memset(&msg, 0, sizeof(msg));
+
+asn_long2INTEGER(&msg, ClosePDU_goingDown);
+
+char buffer[1024];
+asn_enc_rval_t error;
+error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
+
+if (error.encoded == -1)
+ {
+ printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
+ error.failed_type ? error.failed_type->name : "unknown");
+ return -1;
+ }
+else
+ {
+ write(fd, buffer, error.encoded);
+ printfd(__FILE__, "ClosePDU encoded successfully\n");
+ }
+return 0;
+}
+
+int SendRReqPDU(int fd)
+{
+int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
+asn_enc_rval_t error;
+RReqPDU_t msg;
+
+memset(&msg, 0, sizeof(msg));
+
+msg.priority = 0;
+asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
+OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
+ oid,
+ sizeof(oid[0]),
+ 8);
+
+char buffer[1024];
+error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
+
+if (error.encoded == -1)
+ {
+ printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
+ error.failed_type ? error.failed_type->name : "unknown");
+ return -1;
+ }
+else
+ {
+ write(fd, buffer, error.encoded);
+ printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n", error.encoded);
+ }
+return 0;
+}
+
+int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
+{
+asn_enc_rval_t error;
+
+char buffer[1024];
+error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer, sizeof(buffer));
+
+if (error.encoded == -1)
+ {
+ printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
+ error.failed_type ? error.failed_type->name : "unknown");
+ return -1;
+ }
+else
+ {
+ write(fd, buffer, error.encoded);
+ printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n", error.encoded);
+ }
+return 0;
+}
+
+int SendGetResponseErrorPDU(int fd, const PDU_t * getRequest, int errorStatus, int errorIndex)
+{
+asn_enc_rval_t error;
+GetResponse_PDU_t msg;
+
+memset(&msg, 0, sizeof(msg));
+
+msg.request_id = getRequest->request_id;
+asn_long2INTEGER(&msg.error_status, errorStatus);
+asn_long2INTEGER(&msg.error_index, errorIndex);
+
+char buffer[1024];
+error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer, sizeof(buffer));
+
+if (error.encoded == -1)
+ {
+ printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
+ error.failed_type ? error.failed_type->name : "unknown");
+ return -1;
+ }
+else
+ {
+ write(fd, buffer, error.encoded);
+ printfd(__FILE__, "GetResponsePDU for error encoded successfully to %d bytes\n", error.encoded);
+ }
+return 0;
+}
+
+SMUX_PDUs_t * RecvSMUXPDUs(int fd)
+{
+char buffer[1024];
+SMUX_PDUs_t * pdus = NULL;
+
+memset(buffer, 0, sizeof(buffer));
+
+size_t length = read(fd, buffer, sizeof(buffer));
+if (length < 1)
+ return NULL;
+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 NULL;
+ }
+return pdus;
+}
+
+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];