int Snmp::run_transaction(Pdu& pdu, UdpTarget& target) { int rc, done = 0; // 1. set unique id to match this packet on return size_t hold_req_id = req_id_++; set_request_id(&pdu, hold_req_id); // 2. write request to agent transaction trans(pdu, target, iv_snmp_session_); // this call blocks while it attempts to retrieve agent response while (!done) { if ((rc = trans.run()) < 0) { last_transaction_status_ = rc; return rc; } else { trans.result(pdu); // verify this is the pdu we are after if (pdu.get_request_id() == hold_req_id) done = 1 ; } } return 0; }
wpdu::wpdu(const Pdu& pdu, const UdpTarget& target): valid_flag_(SNMP_CLASS_INVALID ), comm_len(MAX_COMM_STR_LEN) { reset_iov(iovec_); version_ = target.get_version(); int status; OctetStr comm_str; community_name[0] = 0; snmp_pdu *raw_pdu; // create a raw pdu raw_pdu = cmu_snmp::pdu_create( (int) pdu.get_type()); if (!raw_pdu) { valid_flag_ = SNMP_CLASS_RESOURCE_UNAVAIL; return; } raw_pdu->reqid = pdu.get_request_id(); raw_pdu->errstat= (unsigned long) pdu.get_error_status(); raw_pdu->errindex= (unsigned long) pdu.get_error_index(); switch (raw_pdu->command) { case sNMP_PDU_GET: case sNMP_PDU_GETNEXT: target.get_read_community(comm_str); break; case sNMP_PDU_SET: target.get_write_community(comm_str); break; case sNMP_PDU_V1TRAP: target.get_read_community(comm_str); if (set_trap_info(raw_pdu, pdu)) // will free raw_pdu return; break; case sNMP_PDU_RESPONSE: break; default: ACE_ASSERT(0); return; } if (load_vbs(raw_pdu, pdu)) { cmu_snmp::free_pdu( raw_pdu); valid_flag_ = SNMP_CLASS_RESOURCE_UNAVAIL; return; } // TODO: determine how big raw_pdu serializes out to iovec_.iov_len = target.get_max_pdu_size(); ACE_NEW(iovec_.iov_base, char [iovec_.iov_len]); // create raw byte stream // The intermediate integer is to avoid type-punned pointer // dereferencing. int out_length = iovec_.iov_len; status = cmu_snmp::build( raw_pdu, (unsigned char *)iovec_.iov_base, &out_length, target.get_version(), comm_str.data(), comm_str.length()); iovec_.iov_len = out_length; if ( status != 0) { valid_flag_ = SNMP_ERROR_WRONG_ENCODING; cmu_snmp::free_pdu( raw_pdu); return; } cmu_snmp::free_pdu( raw_pdu); valid_flag_ = SNMP_CLASS_SUCCESS; }
int CSNMPMessage::SetPdu(const int reason, const Pdu &pdu, const UdpAddress &fromaddress) { if (Pdu::match_type(m_pdu.get_type(), pdu.get_type()) == false) { LOG_BEGIN(INFO_LOG | 1); LOG("MsgQueue: Response pdu type does not match, pdu is ignored: (id) (type1) (type2)"); LOG(m_uniqueId); LOG(m_pdu.get_type()); LOG(pdu.get_type()); LOG_END; return -1; } unsigned short orig_type = m_pdu.get_type(); if (m_received) { LOG_BEGIN(WARNING_LOG | 1); LOG("MsgQueue: Message is already marked as received (id) (reason) (new reason)"); LOG(m_uniqueId); LOG(reason); LOG(m_reason); LOG_END; // TODO: better check based on error codes if (reason || !m_reason) { LOG_BEGIN(WARNING_LOG | 1); LOG("MsgQueue: ignoring the second pdu"); LOG_END; return 0; } } m_received = 1; m_pdu = pdu; m_reason = reason; LOG_BEGIN(DEBUG_LOG | 10); LOG("MsgQueue: Response received (req id) (status) (msg id)"); LOG(pdu.get_request_id()); LOG(reason); #ifdef _SNMPv3 LOG(pdu.get_message_id()); #endif LOG_END; if ((orig_type == sNMP_PDU_INFORM) && (m_pdu.get_type() == sNMP_PDU_RESPONSE)) { // remove the first two vbs of the pdu if sysUpTime and notify_id if (m_pdu.get_vb_count() < 2) return 0; const Vb &vb1 = m_pdu.get_vb(0); if (vb1.get_syntax() != sNMP_SYNTAX_TIMETICKS) return 0; if (vb1.get_oid() != SNMP_MSG_OID_SYSUPTIME) return 0; const Vb &vb2 = m_pdu.get_vb(1); if (vb2.get_syntax() != sNMP_SYNTAX_OID) return 0; if (vb2.get_oid() != SNMP_MSG_OID_TRAPID) return 0; TimeTicks timeticks; Oid oid; vb1.get_value(timeticks); m_pdu.set_notify_timestamp(timeticks); vb2.get_value(oid); m_pdu.set_notify_id(oid); m_pdu.delete_vb(1); m_pdu.delete_vb(0); } return 0; }