Query regarding ASYNC GET calls
"Rahul Ravikanth Anneboina" <[email protected]>
| Newsgroups | gmane.network.net-snmp.user |
|---|---|
| Message-ID | <CCEE49B9CE065146BA4FE34B2748A53210E6FC8C@CEL-BANGT-M01.celstream-in.com> |
Hi ...
Need help from you'll .....
I have a piece of code (attached below) that sends out SNMP_MSG_GET
requests to an entire subnet.
I am using snmp_async_send() to send out the requests and the responses
are read in the callback function asynch_response().
The OID's correspond to printer details and there are about 10 printers
within that subnet.
Every time the callback is triggered with NETSNMP_CALLBACK_OP_TIMED_OUT
and all the 255 IP's time out. Ideally it should get values for those 10
printers and time out the rest of the IPs.
Wireshark packets show that all 256 requests have been sent and
responses from the 10 printers have been received.
If I break down the IP list to a bunch of 50 at a time, then I'm able to
get accurate results. But when I query all 256 at one go, I have this
problem.
Please point me to where I am going wrong.
Thanks in advance.
Regards,
Rahul
Here is the code ...
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <windows.h>
#include <process.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COMMUNITY "public"
#define SUBNET_STR "10.255.110."
#define SUBNET_COUNT 256
#define MAX_OID_COUNT 4
oid OIDs[MAX_OID_COUNT][MAX_OID_LEN] = {
{1,3,6,1,2,1,25,3,5,1,1,1},
{1,3,6,1,2,1,25,3,2,1,3,1},
{1,3,6,1,2,1,43,5,1,1,17,1},
{1,3,6,1,2,1,1,2,0}
};
unsigned char ucOIDLen[MAX_OID_COUNT] = {12,12,12,9};
netsnmp_session * his[SUBNET_COUNT];
int active_hosts;
void initialize (void)
{
SOCK_STARTUP;
init_snmp("sampleasyncapp");
printf("Initialize success ... \n\n");
}
void deinitialize (void)
{
SOCK_CLEANUP;
printf("DeInitialize success ... \n\n");
}
int print_result (int status, netsnmp_session *sp, netsnmp_pdu *pdu)
{
char buf[1024];
netsnmp_variable_list *vp = NULL;
int ix;
switch (status)
{
case
NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE:
vp = pdu->variables;
if (pdu->errstat ==
SNMP_ERR_NOERROR)
{
printf("Got response from '%s' with SNMP version '%d'.... \n",
sp->peername, pdu->version);
while
(vp)
{
snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
fprintf(stdout, "%s: %s\n", sp->peername, buf);
vp = vp->next_variable;
}
}
else
{
for (ix
= 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++);
if (vp)
snprint_objid(buf, sizeof(buf), vp->name, vp->name_length);
else
strcpy(buf, "(none)");
fprintf(stdout, "%s: %s: %s\n", sp->peername, buf,
snmp_errstring(pdu->errstat));
}
return 1;
case NETSNMP_CALLBACK_OP_TIMED_OUT:
printf("\n");
fprintf(stdout, "No
response from device @'%s' operation timed out.\n", sp->peername);
return 0;
case NETSNMP_CALLBACK_OP_SEND_FAILED:
printf("\n");
fprintf(stdout, "Send
failed for device @ '%s'.\n", sp->peername);
return 0;
case NETSNMP_CALLBACK_OP_CONNECT:
printf("\n");
fprintf(stdout, "Device
@ '%s' is connected.\n", sp->peername);
return 0;
case NETSNMP_CALLBACK_OP_DISCONNECT:
printf("\n");
fprintf(stdout, "Device
@ '%s' is disconnected.\n", sp->peername);
return 0;
}
if (vp)
snmp_free_varbind(vp);
return 0;
}
int asynch_response(int operation, netsnmp_session *sp, int reqid,
netsnmp_pdu *pdu, void *magic)
{
netsnmp_session *host = (netsnmp_session *)magic;
printf("Processing ... %s, Op: %d, ", host->peername,
operation);
print_result(operation, host, pdu);
printf("\n");
active_hosts--;
return 1;
}
int init_snmp_sess (char * ip, int index)
{
int i;
struct snmp_pdu *req;
netsnmp_session sess;
snmp_sess_init(&sess);
sess.version = SNMP_VERSION_1;
sess.peername = strdup(ip);
sess.community = strdup(COMMUNITY);
sess.community_len = strlen(COMMUNITY);
//sess.timeout = 10;
//sess.retries = 2;
if (!(his[index] = snmp_open(&sess)))
{
printf("%s - snmp_open error.\n", ip);
return 0;
}
req = snmp_pdu_create(SNMP_MSG_GET);
if (req)
{
for (i = 0; i < MAX_OID_COUNT; i++)
snmp_add_null_var(req,
OIDs[i], ucOIDLen[i]);
if (snmp_async_send(his[index], req,
asynch_response, his[index]))
active_hosts++;
else
{
int liberr, syserr;
char *errstr;
snmp_error(his[index],
&liberr, &syserr, &errstr);
printf("%s -
snmp_sess_async_send error. Error '%s'.\n", ip, errstr);
snmp_free_pdu(req);
free(errstr);
}
}
else
{
printf("%s - snmp_pdu_create error.\n",
ip);
return 0;
}
return 1;
}
int main (int argc, char **argv)
{
int i;
initialize();
for (i = 0; i < SUBNET_COUNT; i++)
{
char cIp[20];
memset(cIp, 0, 20);
snprintf(cIp, 20, "%s%d", SUBNET_STR,
i);
if (!init_snmp_sess (cIp, i))
continue;
}
printf("Completed sending %d requests. Waiting for
responses .... \n\n", active_hosts);
while (1)
{
int fds = 0, block = 1;
fd_set fdset;
struct timeval timeout;
FD_ZERO(&fdset);
snmp_select_info(&fds, &fdset, &timeout,
&block);
fds = select(fds, &fdset, NULL, NULL,
block ? NULL : &timeout);
if (!active_hosts)
break;
if (fds < 0)
perror("");
else
{
if (fds)
snmp_read(&fdset);
else
snmp_timeout();
}
}
for (i = 0; i < SUBNET_COUNT; i++)
{
if (his[i])
{
snmp_close(his[i]);
his[i] = NULL;
}
}
deinitialize();
return 0;
}
______________________________________________________________________________
DISCLAIMER: This electronic message and any attachments to this electronic
message is intended for the exclusive use of the addressee(s) named herein
and may contain legally privileged and confidential information. It is the
property of Celstream Technologies Pvt Limited. If you are not the intended
recipient, you are hereby strictly notified not to copy, forward, distribute
or use this message or any attachments thereto. If you have received this
message in error, please delete it and all copies thereof, from your system
and notify the sender at Celstream Technologies or
[email protected] immediately.
______________________________________________________________________________
------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users