int trapapp::run()
{ 
   if ( snmp_.valid() != SNMP_CLASS_SUCCESS) {
      cout << "\nASNMP:ERROR:Create session failed: "<< 
          snmp_.error_string()<< "\n";
      return 1;
   }

   if (address_.get_port() == 0)
     address_.set_port(DEF_TRAP_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) << \
       " TRAP GENERATOR 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_ << " ";
   cout << (rc ? "<< did not resolve via gethostbyname() >>" : name) << "\n"; 
   cout << "[ Community=" <<  community_.to_string() << " ]"<< endl;

   if (snmp_.trap( pdu_, target_) == SNMP_CLASS_SUCCESS) {
     cout << "Trap was written to network...\n";
   }
   else {
    const char *ptr = snmp_.error_string();
    cout << "ASNMP:ERROR: trap command failed reason: " << ptr << endl;
  }

  cout << "ASNMP:INFO:command completed normally.\n"<< endl;
  return 0;
} 
Exemple #2
0
void Snmp::check_default_port(UdpTarget& target, unsigned short port)
{
  UdpAddress tmp;
  target.get_address(tmp);
  if (tmp.get_port() == 0) {
    tmp.set_port(port);
    target.set_address(tmp);
  }
}
Exemple #3
0
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;
}
Exemple #4
0
int walkapp::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) << \
       " WALK 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;

   MibIter iter(&snmp_, pdu_, &target_);
   char *err_str = 0;
   Vb vb;
   unsigned ctr = 0;
   while (iter.next(vb, err_str))  {
     cout << "\tOid = " << vb.to_string_oid() << "\n";
     cout << "\tValue = " << vb.to_string_value() << "\n";
     ctr++;
   }

   if (!err_str) {
     cout << "ERROR: walk: " << err_str << endl;
     return 0;
   }

  cout << "ASNMP:INFO:command completed normally. ACE Rocks...\n"<< endl;
  return 0;
}
Exemple #5
0
// callback : have received a Pdu from the target host with given read comm str
// this is really simplistic, but gives the general idea
int agent_impl::handle_get( Pdu &pdu, UdpTarget &target)
{
  ACE_TRACE("agent_impl::handle_get");
  OctetStr mgr_rd_str, agent_rd_str;
  target.get_read_community(mgr_rd_str); // requster's read community string
  tgt_.get_read_community(agent_rd_str); // this agent's read community string

  //  1. verify we have a valid read string else drop pdu (no response to caller)
  if (mgr_rd_str != agent_rd_str) {
     ACE_DEBUG((LM_DEBUG, "agent_impl::handle_get: invalid read community recvd\n"));
     return 0;
  }

  // 2. iterate over each varbind in the pdu, filling providing responses
  int fdone = 0;
  for (int i = 0; (i < pdu.get_vb_count()) && !fdone; i++) {
    Vb vb;
    pdu.get_vb(vb, i);
    if (get_response(vb)) { // set a value for the oid if we can else
      set_error_status(&pdu, SNMP_ERROR_NO_SUCH_NAME); // these ought to be member
      set_error_index(&pdu, i); // functions but are not yet...
      fdone++; // trigger flag to exit loop early
    }
    else // failed, return noSuch error
      pdu.set_vb(vb, i);
  }

  // 3. lastly, return the pkt to the caller
  return respond(pdu, target);
}
Exemple #6
0
int Snmp::validate_args(const Pdu& pdu, const UdpTarget& target) const
{
  // 0. check object status
  if (construct_status_ != SNMP_CLASS_SUCCESS)
     return construct_status_;

  // 1. check args passed
  if ( !pdu.valid() || !target.valid() )
     return SNMP_INVALID_ARGS;
  return 0;
}
Exemple #7
0
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_, this) != SNMP_CLASS_SUCCESS) {
    const char *ptr = snmp_.error_string();
    cout << "ASNMP:ERROR: get command failed reason: " << ptr << endl;
   } else {
       ACE_Reactor::instance()->run_reactor_event_loop();
   }
   return 0;
}
transaction::transaction(const Pdu& pdu, const UdpTarget& target,
                         ACE_SOCK_Dgram& io):
                         result_(0),
                         wp_(pdu,target), params_(target), session_(io)
{
  // last step, convert address (get ride of this once we have merged address
  UdpAddress udp;
  target.get_address(udp);
  // via string conversion "dotted-quad:port"
  ACE_INET_Addr tmp(udp);
  addr_ = tmp;
  reset_receive_buffer(receive_iovec_);
}
Exemple #9
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;
}