snmp_sess_synch_response and thread safety
"Robert Caldecott" <[email protected]>
| Newsgroups | gmane.network.net-snmp.user |
|---|---|
| Message-ID | <[email protected]> |
I am struggling to get snmp_sess_synch_response to work correctly from
multiple threads and am experiencing STAT_TIMEOUT errors when
attempting to perform an SNMP GET to multiple IP addresses
simultaneously. I am using v5.4 of Net-SNMP on Linux (Ubuntu 6.10).
Here is the code, stripped of error checking for brevity:
...
// Calls _init_snmp, which won't then be called again
init_snmp("snmpapp");
// Create the threads
for (int i = 1; i <= 255; i++)
create_thread();
...
Here is the thread function. Assume that 'host' is a unique IP
address and oid_name the OID:
void fn(const char* ip, const char* oid_name)
{
netsnmp_session session;
snmp_sess_init(&session_);
session.peername = ip;
session.community = "public";
session.community_len = strlen(session.community);
void* ssp = snmp_sess_open(&session);
netsnmp_session* ss = snmp_sess_session(ssp);
ss->version = SNMP_VERSION_1;
struct snmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_GET);
oid anOID[MAX_OID_LEN];
size_t anOID_len = MAX_OID_LEN;
read_objid(oid_name, anOID, &anOID_len);
snmp_add_null_var(pdu, anOID, anOID_len);
struct snmp_pdu *response;
int status = snmp_sess_synch_response(ssp, pdu, &response);
// Always returns STAT_TIMEOUT!
...
snmp_free_pdu(response);
snmp_sess_close(ssp);
}
The problem seems to be with the call to 'snmp_sess_synch_response'.
If I wrap this call with a lock (boost::mutex), then the code works
fine (although very slowly obviously). I have looked at the source
but cannot see any obvious shared resources that could be getting
reset by each call.
This code forms part of an snmpscan function which trawls a specified
subnet looking for printers. I have tried different timeouts, but
with no luck. Note that I am expecting some IPs to timeout, as they
won't be printers, but it seems to be that as soon as one thread has
caused snmp_sess_synch_response to timeout, all the other threads
follow suit.
I know I could give the async_response call a try, but it is overkill
for my needs so I really want to the get the sync call to work.
Anyone have any ideas? FWIW the same code works OK on Windows.
Regards.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
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