void callback( int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *cd) { Vb nextVb; int pdu_error; cout << "reason: " << reason << endl << "msg: " << snmp->error_msg(reason) << endl; pdu_error = pdu.get_error_status(); if (pdu_error) { cout << "Response contains error: " << snmp->error_msg(pdu_error)<< endl; } for (int i=0; i<pdu.get_vb_count(); i++) { pdu.get_vb(nextVb, i); cout << "Oid: " << nextVb.get_printable_oid() << endl << "Val: " << nextVb.get_printable_value() << endl; } if (pdu.get_type() == sNMP_PDU_INFORM) { cout << "pdu type: " << pdu.get_type() << endl; cout << "sending response to inform: " << endl; nextVb.set_value("This is the response."); pdu.set_vb(nextVb, 0); snmp->response(pdu, target); } cout << endl; }
void getapp::result(Snmp *, int rc) { Vb vb; if (rc < 0) { const char *ptr = snmp_.error_string(); cout << "ASNMP:ERROR: get command failed reason: " << ptr << endl; } else { // check to see if there are any errors if (pdu_.get_error_status()) { cout << "ERROR: agent replied as follows\n"; cout << pdu_.agent_error_reason() << endl; } else { VbIter iter(pdu_); while (iter.next(vb)) { cout << "\tOid = " << vb.to_string_oid() << "\n"; cout << "\tValue = " << vb.to_string_value() << "\n"; } } } cout << "\nASNMP:INFO: command completed normally.\n"<< endl; ACE_Reactor::instance()->end_reactor_event_loop(); }
int getapp::run() { //----------[ create a ASNMP session ]----------------------------------- if ( snmp_.valid() != SNMP_CLASS_SUCCESS) { cout << "\nASNMP:ERROR:Create session failed: "<< snmp_.error_string()<< "\n"; return 1; } //--------[ build up ASNMP object needed ]------------------------------- if (address_.get_port() == 0) address_.set_port(DEF_AGENT_PORT); target_.set_address( address_); // make a target using the address //-------[ issue the request, blocked mode ]----------------------------- cout << "\nASNMP:INFO:SNMP Version " << (target_.get_version()+ 1) << \ " GET SAMPLE PROGRAM \nOID: " << oid_.to_string() << "\n"; target_.get_address(address_); // target updates port used int rc; const char *name = address_.resolve_hostname(rc); cout << "Device: " << address_ << " "; //FUZZ: disable check_for_lack_ACE_OS cout << (rc ? "<< did not resolve via gethostbyname() >>" : name) << "\n"; //FUZZ: enable check_for_lack_ACE_OS cout << "[ Retries=" << target_.get_retry() << " \ Timeout=" << target_.get_timeout() <<" ms " << "Community=" << \ community_.to_string() << " ]"<< endl; if (snmp_.get( pdu_, target_) == SNMP_CLASS_SUCCESS) { Vb vb; // check to see if there are any errors if (pdu_.get_error_status()) { cout << "ERROR: agent replied as follows\n"; cout << pdu_.agent_error_reason() << endl; } else { VbIter iter(pdu_); while (iter.next(vb)) { cout << "\tOid = " << vb.to_string_oid() << "\n"; cout << "\tValue = " << vb.to_string_value() << "\n"; } } } else { const char *ptr = snmp_.error_string(); cout << "ASNMP:ERROR: get command failed reason: " << ptr << endl; } cout << "\nASNMP:INFO: command completed normally.\n"<< endl; return 0; }
// return vb of next oid in agent tree, return 1 else return 0, reason set int MibIter::next(Vb& vb, char *& reason) { int rc; if (valid_ == 0) // not valid object return -1; // 1. poll for value if (first_ == 0) { rc = snmp_->get( pdu_, *target_); first_++; } else { rc = snmp_->get_next( pdu_, *target_); } if (rc != SNMP_CLASS_SUCCESS) { reason = const_cast <char*> (snmp_->error_string()); return 0; } // 2. check for problems if (pdu_.get_error_status()) { reason = const_cast <char*> (pdu_.agent_error_reason()); return 0; } // 3. return vb to caller pdu_.get_vb(vb, 0); Oid nextoid; vb.get_oid(nextoid); // and setup next oid to get Vb nextvb(nextoid); pdu_.delete_all_vbs(); pdu_ += nextvb; // can't do set_vb as there are no entries to replace return 1; // ok }
void MainWindow::async_callback(int reason, Snmp * /*snmp*/, Pdu &pdu, SnmpTarget &target) { Vb nextVb; int pdu_error; QString prefix_text; QString notify_text; push_button_get_next->setEnabled(true); // What is the reason for this callback? if (reason == SNMP_CLASS_NOTIFICATION) { prefix_text = "Trap: "; // get the notify id for traps Oid id; pdu.get_notify_id(id); notify_text = QString(" ID: %1 Type %2 -- ").arg(id.get_printable()) .arg(pdu.get_type()); } else if (reason == SNMP_CLASS_ASYNC_RESPONSE) { prefix_text = "Response "; } else if (reason == SNMP_CLASS_TIMEOUT) { prefix_text = "Timeout "; } else { QString err = QString("\nDid not receive async response/trap: (%1) %2\n") .arg(reason).arg(Snmp::error_msg(reason)); text_edit_output->append(err); } // Look at the error status of the Pdu pdu_error = pdu.get_error_status(); if (pdu_error) { QString err = "\nResponse contains error:\n"; err += Snmp::error_msg(pdu_error); text_edit_output->append(err); return; } // The Pdu must contain at least one Vb if (pdu.get_vb_count() == 0) { QString err = "\nPdu is empty\n"; text_edit_output->append(err); return; } for (int i=0; i<pdu.get_vb_count(); i++) { // Get the Vb of the Pdu pdu.get_vb(nextVb, i); // Get Oid and value from the Vb and display it line_edit_obj_id->setText(nextVb.get_printable_oid()); line_edit_value->setText(nextVb.get_printable_value()); text_edit_output->append(prefix_text + target.get_address().get_printable() + " -- " + notify_text + line_edit_obj_id->text() + " = " + line_edit_value->text() + "\n"); } // If we received a inform pdu, we have to send a response if (pdu.get_type() == sNMP_PDU_INFORM) { text_edit_output->append("Sending response to inform.\n"); // just change the value of the first vb pdu.get_vb(nextVb, 0); nextVb.set_value("This is the response."); pdu.set_vb(nextVb, 0); snmp->response(pdu, target); } }
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; }