SNMP Broadcast
Martin Townsend <[email protected]>
| Newsgroups | gmane.network.net-snmp.user |
|---|---|
| Organization | Power Oasis |
| Message-ID | <[email protected]> |
Hi,
I'm trying to use SNMP broadcast packets to perform device discovery.
I'm using version 5.5 which has limited support for broadcast. Has
anyone managed to get this working as my initial attempt doesn't see any
replies when I know there should be at least 3 devices that should reply.
Here's the code which creates a session with the peername set to
"255.255.255.255" and the flags set to SNMP_FLAGS_UDP_BROADCAST. It
then creates a GET PDU with the sysDescr.0 OID. This is then sent
asynchronously. it sleeps for 3 seconds and then uses snmp_select_info
and select to read the replies. When I run this code, count always
returns as 0. Am I missing something in the code? Wireshark sees the
UDP Broadcast packet and 1 reply from our cisco switch but not from the
2 printers that I would also expect. I've also tried using the single
session API with the same result.
struct snmp_session session;
netsnmp_session * netsnmp_session_p;
/* Setup the session */
snmp_sess_init( &session );
session.peername = (char *)"255.255.255.255";
session.version = SNMP_VERSION_1;
session.community = "public";
session.community_len = 6;
session.callback = ssmd_device_mgr_discovery_input;
session.callback_magic = NULL;
session.flags |= SNMP_FLAGS_UDP_BROADCAST;
session.authenticator = NULL;
session.isAuthoritative = SNMP_SESS_UNKNOWNAUTH;
netsnmp_session_p = snmp_open(&session);
if(netsnmp_session_p == NULL) {
SSMD_LOG(LOG_ERR, SSMD_LOG_GROUP_DEVICE_MGR,
"Failed to open net-snmp session (%s)",
snmp_api_errstring(snmp_errno));
return FALSE;
} else {
netsnmp_pdu * pdu_p;
int status;
size_t oid_name_length = MAX_OID_LEN;
oid oid_name[oid_name_length];
pdu_p = snmp_pdu_create(SNMP_MSG_GET);
read_objid(".1.3.6.1.2.1.1.1.0", oid_name, &oid_name_length);
snmp_add_null_var(pdu_p, oid_name, oid_name_length);
status = snmp_send(netsnmp_session_p, pdu_p);
if(status == 0) {
SSMD_LOG(LOG_ERR, SSMD_LOG_GROUP_DEVICE_MGR, "Failed to
send discovery PDU (%s)",
snmp_api_errstring(snmp_errno));
} else {
int num_fds;
int block;
int count;
fd_set rd_fdset;
struct timeval timeout, * timeout_p;
SSMD_LOG(LOG_ERR, SSMD_LOG_GROUP_DEVICE_MGR, "SNMP
Broadcast packet sent (%d)", status);
num_fds = 0;
FD_ZERO(&rd_fdset);
block = 0; /* note: will be set by snmp_sess_select_info */
timeout_p = &timeout;
if (block) {
timerclear(timeout_p);
} else {
timeout_p->tv_sec = 1;
timeout_p->tv_usec = 0;
}
snmp_select_info(&num_fds, &rd_fdset, timeout_p, &block);
SSMD_LOG(LOG_DEBUG, SSMD_LOG_GROUP_DEVICE_MGR, "num_fds(%d)
block(%d) tv(%d)",
num_fds, block, timeout_p->tv_sec);
if (block == 1) {
/* @todo (mjt): problem, abort */
SSMD_LOG(LOG_ERR, SSMD_LOG_GROUP_DEVICE_MGR, "Select
will block");
timeout_p = NULL;
}
/* Check for read readiness for the required timeout */
SSMD_LOG(LOG_DEBUG, SSMD_LOG_GROUP_DEVICE_MGR, "Discovery
sleeping for 3 secs");
sleep(3);
SSMD_LOG(LOG_DEBUG, SSMD_LOG_GROUP_DEVICE_MGR, "Discovery
performing select");
int ok = 1;
while(ok) {
count = select(num_fds, &rd_fdset, 0, 0, timeout_p);
SSMD_LOG(LOG_DEBUG, SSMD_LOG_GROUP_DEVICE_MGR,
"Discovery select finished, count = %d", count);
if (count > 0) {
snmp_read(&rd_fdset);
} else {
/* Check select return value. */
switch (count) {
case 0 :
/* It timed out, call snmp_timeout to check
other sessions. */
snmp_timeout();
ok = 0;
break;
case -1 :
/* There was an error */
SSMD_LOG(LOG_ERR, SSMD_LOG_GROUP_DEVICE_MGR,
"Select failed %d:(%s)",
errno, strerror(errno));
ok=0;
break;
default:
SSMD_LOG(LOG_ERR, SSMD_LOG_GROUP_DEVICE_MGR,
"select returned %d\n", count);
}
}
}
}
snmp_close(netsnmp_session_p);
return TRUE;
}
gives the following output
[Error] ssmd_device_mgr_discovery(116): SNMP Broadcast packet sent
(1179738193)
[Debug] ssmd_device_mgr_discovery(131): num_fds(5) block(0) tv(0)
[Debug] ssmd_device_mgr_discovery(139): Discovery sleeping for 3 secs
[Debug] ssmd_device_mgr_discovery(141): Discovery performing select
[Debug] ssmd_device_mgr_discovery(145): Discovery select finished, count = 0
Best Regards,
Martin.
------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a
Billion" shares his insights and actions to help propel your
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
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