Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
BroadcastCommunity.qnx.com will be offline from May 31 6:00pm until June 2 12:00AM for upcoming system upgrades. For more information please go to https://community.qnx.com/sf/discussion/do/listPosts/projects.bazaar/discussion.bazaar.topc28418
Forum Topic - using SNMP API to read data (OID) from an SNMP agent: Page 1 of 3 (3 Items)
   
using SNMP API to read data (OID) from an SNMP agent  
Hi,

is there any help, or example for using the snmp api functions like snmp_open(), snmp_send()...? I'm lost with the 
description in the library reference.

I use a QNX 6.4.1 system and would like to read out OIDs from another SNMP agent. 
The snmp_open() succeeds, but the snmp_send() with my snmp-get-pdu failes with error -1 (general error). Is this the 
right way to read from a SNMP agent?

Here is my source code.
Thanks for any help!

Carsten

---------------------------------------------

extern int snmp_errno;

snmp_session* m_Session;


int SnmpCallback (int operation, struct snmp_session* session, int reqid, struct snmp_pdu* pdu, void* magic)
{
  printf("SnmpCallback\n");
  // TODO
  return 1; // successful completion
}

int main(int argc, char *argv[]) {

    oid   BaseOid[] = {1,3,6,1,4,1,4329,1};
    char  AgentIpAddress[] = "192.168.184.200";

    snmp_session snmp_session_settings;
    snmp_session_settings.community = 0; // default
    snmp_session_settings.community_len = SNMP_DEFAULT_COMMUNITY_LEN; // default
    snmp_session_settings.retries = 0; // no retries
    snmp_session_settings.timeout = SNMP_DEFAULT_TIMEOUT;
    snmp_session_settings.peername = AgentIpAddress; // agent ip address 192.168.184.200
    snmp_session_settings.remote_port = 161; // UDP Port at agent
    snmp_session_settings.local_port = 162; // local UDP port for traps
    snmp_session_settings.authenticator = NULL; // not authentication used
     snmp_session_settings.callback = (int (*)())(SnmpCallback); // callback for async answers and traps
    snmp_session_settings.callback_magic = NULL;
    snmp_session_settings.version = SNMP_VERSION_2;
    snmp_session_settings.srcParty = BaseOid;
    snmp_session_settings.srcPartyLen = sizeof(BaseOid)/sizeof(oid);
    snmp_session_settings.dstParty = BaseOid;
    snmp_session_settings.dstPartyLen  = sizeof(BaseOid)/sizeof(oid);
    snmp_session_settings.context = BaseOid;
    snmp_session_settings.contextLen = sizeof(BaseOid)/sizeof(oid);

    m_Session = snmp_open(&snmp_session_settings);
    if(m_Session == NULL)
    {
      printf("Error %d in snmp_open\n", snmp_errno);
    }
    else
    {
      printf("snmp_open done!\n");
    }


    long Value = 0;
    oid   AnyOID[] = {1,3,6,1,4,1,4329,1,2,4};
    variable_list new_variable;
    new_variable.name = &AnyOID[0];
    new_variable.name_length = sizeof(AnyOID);
    new_variable.next_variable = NULL;
    new_variable.type = ASN_INTEGER;
    new_variable.val.integer = &Value;
    new_variable.val_len = sizeof(Value);

    ipaddr theIpAddr;
    memset(&theIpAddr, 0, sizeof(theIpAddr));
    theIpAddr.sin_len = sizeof(theIpAddr);
    theIpAddr.sin_family = AF_INET;
    theIpAddr.sin_port = htons((uint16_t)162);
    theIpAddr.sin_addr.s_addr = __IPADDR(0xC0A4B801); //192.168.184.1

    // create a new pdu struct
    struct snmp_pdu * new_snmp_pdu = snmp_pdu_create (GET_REQ_MSG);

    // fill the pdu struct
    new_snmp_pdu->version = m_Session->version;
    new_snmp_pdu->address = theIpAddr;
    new_snmp_pdu->srcParty = m_Session->srcParty;
    new_snmp_pdu->srcPartyLen = m_Session->srcPartyLen;
    new_snmp_pdu->dstParty = m_Session->dstParty;
    new_snmp_pdu->dstPartyLen = m_Session->dstPartyLen;
    new_snmp_pdu->context = m_Session->context;
    new_snmp_pdu->contextLen = m_Session->contextLen;
    new_snmp_pdu->community = m_Session->community;
    new_snmp_pdu->community_len = m_Session->community_len;
//  new_snmp_pdu->command =  allready filled
    new_snmp_pdu->reqid = 1;
    new_snmp_pdu->agent_addr = theIpAddr;
//  new_snmp_pdu->trap_type :don't know
//  new_snmp_pdu->specific_type :don't know
//  new_snmp_pdu->time  :use default
    new_snmp_pdu->variables  = &new_variable;

    int returnvalue = snmp_send( m_Session,
                               new_snmp_pdu...
View Full Message
Attachment: Text SNMP_Test.cc 4.05 KB