Is It planned to use net-snmp in LibEvent, like this, but without calling  :

void fct_listen(evutil_socket_t listener,short event, void *arg)
{
 struct event_base *base = (struct event_base *)arg;
 printf("\n fct_listen=%d\n",event);

 int fds = 0, block = 0;
 fd_set fdset;
 struct timeval timeout; // timeout for select()
 timeout.tv_sec = 0;
 timeout.tv_usec = 10000;
 FD_ZERO (&fdset);
 snmp_select_info (&fds, &fdset, &timeout, &block);
 fds = select (fds, &fdset, NULL, NULL, &timeout);
 if (fds)
 { snmp_read(&fdset);
 }
 else
 {
  snmp_timeout();
  printf("snmp_timeout\n");
  // stop evenet loop
  event_base_loopbreak(base);
 }
}

but only

void fct_listen1(evutil_socket_t listener,short event, void *arg)
{
 struct event_base *base = (struct event_base *)arg;
 printf("\n fct_listen=%d\n",event);
 if (event == 2) snmp_read();
 else  if (event==1)
 {
  snmp_timeout();
  printf("snmp_timeout\n");
  event_base_loopbreak(base);
 }
}


Because the callback function  fct_listen or  fct_listen1 are calling when
socket is up (trame detected)
and don't need the use of "fd_set fdset;"
I added all the code sample.c with the two callback function.

Regards


Didier
#include <sys/types.h>
#include <sys/param.h>

#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>


#include <net-snmp/net-snmp-config.h>

#if USE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/types.h>
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if TIME_WITH_SYS_TIME
# ifdef WIN32
#  include <sys/timeb.h>
# else
#  include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <stdio.h>
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/asn1.h>
#include <net-snmp/library/snmp_api.h>
#include <net-snmp/library/snmp_enum.h>
#include <net-snmp/library/parse.h>


#include "C:\\cygwin\\usr\\local\\include\\event2\\event.h"

using namespace std;


static struct event_base* Sample_EventBase = NULL;
int
snmp_input(int op,
           netsnmp_session * session,
           int reqid, netsnmp_pdu *pdu, void *magic)
{
//    struct get_req_state *state = (struct get_req_state *)magic;
			netsnmp_variable_list *varlist, *vp;
			oid anOID[MAX_OID_LEN];
			size_t anOID_len = MAX_OID_LEN;
			/* Device information variables */
			char ip_addr[1024];
			char model[1024];
			char device[1024];
			/* remote IP detection variables */
			netsnmp_indexed_addr_pair *responder;
			struct sockaddr_in *remote = NULL;
			struct MagicolorCap *cap;
			int i;
				//netsnmp_variable_list_zp8 *vars;

printf(" ****************begin snmp_input ************** op=%d \n ",op);

    if (op == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
			varlist = pdu->variables;
			responder = (netsnmp_indexed_addr_pair *) pdu->transport_data;

			printf("%s: Handling SNMP response \n", __func__);

			if (responder == NULL || pdu->transport_data_length != sizeof(netsnmp_indexed_addr_pair )) {
				printf("%s: Unable to extract IP address from SNMP response.\n",__func__);
				return 0;
			}
			remote = (struct sockaddr_in *) &(responder->remote_addr);
			if (remote == NULL) {
				printf("%s: Unable to extract IP address from SNMP response.\n",__func__);
				return 0;
			}
			snprintf(ip_addr, sizeof(ip_addr), "%s", inet_ntoa(remote->sin_addr));
			printf("%s: IP Address of responder is %s\n", __func__, ip_addr);

			// System Object ID (Unique OID identifying model)
			// This determines whether we really have a magicolor device 

			anOID_len = MAX_OID_LEN;
			read_objid("1.3.6.1.2.1.1.2.0", anOID, &anOID_len);
			printf("\n anOID_len=%d \n",anOID_len);
			vp = find_varbind_in_list (varlist, anOID, anOID_len);
			if (vp) {
			printf("\n ee\n");
				size_t value_len = vp->val_len/sizeof(oid);
				if (vp->type != ASN_OBJECT_ID) {
					printf("%s: SystemObjectID does not return an OID, device is not a magicolor device\n", __func__);
					return 0;
				}
				snprint_objid (device, sizeof(device), vp->val.objid, value_len);
				printf("%s: Device object ID is '%s'\n", __func__, device);
			}

			// Retrieve sysDescr (i.e. model name) 
			anOID_len = MAX_OID_LEN;
			read_objid("1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
			vp = find_varbind_in_list (varlist, anOID, anOID_len);
			if (vp) {
				memcpy(model,vp->val.string,vp->val_len);
				model[vp->val_len] = '\0';
				printf( "%s: Found model: %s\n", __func__, model);
			}


    } else if (op == NETSNMP_CALLBACK_OP_TIMED_OUT) {
		printf("\n NETSNMP_CALLBACK_OP_TIMED_OUT\n");
		return 1;
    }
printf(" **************** end snmp_input **************\n ");

    return 0;

}                               /* end snmp_input() */


netsnmp_session* ConnectionRouter(char *Community,char *sPathMib,char *Error)
{

    /*
     * open an SNMP session 
     */

	netsnmp_session *pSessionOpen = NULL;
	// a voir 
	//static 
	netsnmp_session pSession;

	putenv(strdup("POSIXLY_CORRECT=1"));
    setenv("MIBS", "ALL", 1);
	setenv("MIBDIRS", sPathMib, 1);

	snmp_sess_init((netsnmp_session*)&pSession);
	init_snmp("snmpapp");

	pSession.version = SNMP_VERSION_2c;
	pSession.community= (unsigned char*)Community;
	pSession.community_len = strlen(Community);
	unsigned long ulIp0,ulIp1,ulIp2,ulIp3;
	char szAdrIp[50];
	
	ulIp3 = (ulIpAddr&0xFF000000)>>24;
	ulIp2 = (ulIpAddr&0x00FF0000)>>16;
	ulIp1 = (ulIpAddr&0x0000FF00)>>8;
	ulIp0 = (ulIpAddr&0x000000FF);

	ulIp0 = ulIp1 = ulIp2 = ulIp3 = 255;
	sprintf(szAdrIp,"%d.%d.%d.%d:161", ulIp3, ulIp2, ulIp1, ulIp0);

	pSession.peername = szAdrIp;
	if ((ulIp3==255)&&(ulIp2==255)&&(ulIp1==255)&&(ulIp0==255))
		pSession.flags   |= SNMP_FLAGS_UDP_BROADCAST;
	pSession.callback = snmp_input;
	pSession.callback_magic = NULL;

	SOCK_STARTUP;
	pSessionOpen = snmp_open(&pSession);
    if (pSessionOpen == NULL)
	{
		SOCK_CLEANUP;
		sprintf(Error,"Error can not open Snmp Session!!!");
		return NULL;
	}
	return pSessionOpen;
}

// ferme la session Snmp ouverte
int ClearRouter(netsnmp_session* pSession)
{
	snmp_close(pSession);
	SOCK_CLEANUP;	
	return 0;
}


void fct_listen1(evutil_socket_t listener,short event, void *arg)
{
	struct event_base *base = (struct event_base *)arg;
	printf("\n fct_listen=%d\n",event);		
	if (event == 2) snmp_read();
	else  if (event==1)
	{
		snmp_timeout();
		printf("snmp_timeout\n");
		event_base_loopbreak(base); 		
	}
}


// callback function calling when socket is up (trame detected)
void fct_listen(evutil_socket_t listener,short event, void *arg)
{
	struct event_base *base = (struct event_base *)arg;
	printf("\n fct_listen=%d\n",event);
	
	int fds = 0, block = 0;
	fd_set fdset;
	struct timeval timeout; // timeout for select() 
	timeout.tv_sec = 0;
	timeout.tv_usec = 10000;
	FD_ZERO (&fdset);
	snmp_select_info (&fds, &fdset, &timeout, &block);
	fds = select (fds, &fdset, NULL, NULL, &timeout);
	if (fds)
	{ snmp_read(&fdset);
	}
	else 
	{
		snmp_timeout();
		printf("snmp_timeout\n");
		// stop evenet loop
		event_base_loopbreak(base); 		
	}
}


// main
int main(int argc, char** argv)
{
	netsnmp_session *pSession;
	netsnmp_pdu    *pdu, *response = NULL;
    oid             name[MAX_OID_LEN];
    size_t          name_length;
	char sError[200];
	time_t endtime;
	struct timeval five_seconds = {5,0};
	//struct event_base *base;
	struct event* wait;

	// create Event base
	Sample_EventBase = event_base_new();
	if (!Sample_EventBase)
	{
		cout << "event_base_new() returned NULL" << endl;
		exit(1);
	}
	pSession = ConnectionRouter("public","c:\\mibs",sError);
	if (pSession != NULL)  
	{
		// recover socket
		netsnmp_transport *ptr = snmp_sess_transport(snmp_sess_pointer(pSession));

		// register event callback 
		wait = event_new(Sample_EventBase,ptr->sock,EV_READ|EV_PERSIST,fct_listen,(void*)Sample_EventBase);
		// event timeout timer
		event_add(wait, &five_seconds);
		
		pdu = snmp_pdu_create(SNMP_MSG_GET);
        name_length = MAX_OID_LEN;
        if (!snmp_parse_oid("system.sysDescr.0", name, &name_length)) {
            printf("\nsnmp error");
        } else
            snmp_add_null_var(pdu, name, name_length);

        if (!snmp_parse_oid("system.sysObjectID.0", name, &name_length)) {
            printf("\nsnmp error");
        } else
            snmp_add_null_var(pdu, name, name_length);

		if (!snmp_send(pSession,pdu))
		{
			snmp_free_pdu(pdu);
		}
		else
		{
			//event_base LOOP
			event_base_dispatch(Sample_EventBase);
			cout << "\nExit event loop\n" << endl;
		}
		printf("\n aa\n");
		ClearRouter(pSession);
		printf("\n fin\n");

	}
	// free event base
	printf("\n free\n");
	if(Sample_EventBase)
	{
		event_base_free(Sample_EventBase);
		Sample_EventBase = NULL;
		printf("\n OK\n");
	}
	cout << "Terminated!!!" << endl;
	return 0;

}	
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to