Hi all,

I'm running into a little issue here. I've created a test application that 
utilizes both synchronous and asynchronous snmp get's.

My synchronous app works ok, however I cannot get my async app working. I 
believe I have everything set up correctly. I'm launching my application 
through gtest. Here is my code:


#include "SnmpTestApp.h"
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>

int asynch_response(int operation, struct snmp_session *sp, int reqid,
    struct snmp_pdu *pdu, void *magic) {

  std::cout << "Response received!" << std::endl;

  return 1;
}

namespace testing {
    namespace util {

      SnmpTestApp::SnmpTestApp() {

      }

      SnmpTestApp::~SnmpTestApp() {
      }

      netsnmp_session SnmpTestApp::initialize() {

        netsnmp_session session;
        netsnmp_pdu *pdu;
        netsnmp_pdu *response;

        init_snmp("SnmpTestApp");

        snmp_sess_init(&session);
        session.peername = strdup("oster");
        session.version = SNMP_VERSION_1;
        session.community = (u_char*) strdup("public");
        session.community_len = strlen("public");
        return session;

      }

      void SnmpTestApp::sync(netsnmp_session session) {

        netsnmp_session *ss;
        netsnmp_pdu *pdu, *resp;
        oid anOID[MAX_OID_LEN];
        std::string oid = ".1.3.6.1.2.1.1.1.0";
        size_t anOidLen = MAX_OID_LEN;
        int status;

        ss = snmp_open(&session);

        if (!ss) {
          snmp_sess_perror("ack", &session);
        }

        pdu = snmp_pdu_create(SNMP_MSG_GET);

        if (read_objid(oid.c_str(), anOID, &anOidLen) == 0) {
          snmp_sess_perror("read_objid", &session);
        }

        snmp_add_null_var(pdu, anOID, anOidLen);

        status = snmp_synch_response(ss, pdu, &resp);

        std::cout
            << "Status (Success: 1 Failure: 0): "
                + std::to_string(status == STAT_SUCCESS ? true : false)
            << std::endl;
        std::cout
            << "Resp status Success: 1 Failure: 0 :"
                + std::to_string(
                    resp->errstat == SNMP_ERR_NOERROR ? true : false)
            << std::endl;

        for (struct variable_list *vars = resp->variables; vars;
            vars = vars->next_variable) {
          print_variable(vars->name, vars->name_length, vars);
        }

        if (resp) {
          snmp_free_pdu(resp);
        }
        snmp_close(ss);

      }

      void SnmpTestApp::async(netsnmp_session session) {

        netsnmp_session *ss;
        netsnmp_pdu *pdu;
        oid anOID[MAX_OID_LEN];
        std::string oid = ".1.3.6.1.2.1.1.1.0";
        size_t anOidLen = MAX_OID_LEN;
        int status;

        session.callback = asynch_response;
        //session.callback_magic = ss;

        ss = snmp_open(&session);

        if (!ss) {
          snmp_sess_perror("ack", &session);
        }

        pdu = snmp_pdu_create(SNMP_MSG_GET);

        if (read_objid(oid.c_str(), anOID, &anOidLen) == 0) {
          snmp_sess_perror("read_objid", &session);
        }

        snmp_add_null_var(pdu, anOID, anOidLen);

        if (snmp_async_send(ss, pdu, ss->callback, ss->callback_magic) == 0) {
          std::cout << "Error sending async" << std::endl;
          std::cout << std::to_string(session.s_snmp_errno) << std::endl;
          std::cout << std::to_string(session.s_errno) << std::endl;

        }

      }

    }
  }

Is there anything I'm missing here? This app is in c++, so the callback is the 
only method that is declared with extern "C". If this is incorrect please let 
me know. I am not setting the void*, because I don't need to send any data to 
the callback. As a starting point I have a debug point on the callback method 
that doesn't get hit. The snmp_async_send does not show any errors being sent.

Thanks!
Corey



Corey Pentasuglia
Software Engineer
Sensor Systems
Saab Defense and Security USA LLC

Direct +1 315 445 5018
corey.pentasug...@saabusa.com<mailto:firstname.lastn...@saabusa.com>

5717 Enterprise Pkwy
Syracuse, Ny, 13057 USA
www.saabusa.com<http://www.saabusa.com/>

[SAAB_Defense&Security_CMYK]

This message is intended only for the addressee and may contain information 
that is company confidential or privileged. Any technical data in this message 
may be exported only in accordance with the U.S. International Traffic in Arms 
Regulations (22 CFR Parts 120-130) or the Export Administration Regulations (15 
CFR Parts 730-774). Unauthorized use is strictly prohibited and may be 
unlawful. If you are not the intended recipient, or the person responsible for 
delivering to the intended recipient, you should not read, copy, disclose or 
otherwise use this message. If you have received this email in error, please 
delete it, and advise the sender immediately.

<<inline: image001.jpg>>

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to