All pastes #275980 Raw Edit

snmp-coder

public c v1 · immutable
·published 2006-12-11 22:36 UTC
rendered paste body
rstory; still doesn't work..:-(GLvoid snmp_walk(char *ip_addr) {    struct snmp_session session, *ss;    struct snmp_pdu *pdu;    struct snmp_pdu *response;	struct hostent *he;    oid anOID[MAX_OID_LEN];    size_t anOID_len = MAX_OID_LEN;    struct variable_list *vars;    int status;    int count=1;		char *oids[OIDS];	/* For variable OID instances */	char var_buffer[MAX_MSG];	database *data;	int i;		/* SNMPv2-MIB */	oids[0]="sysDescr.0";	oids[1]="sysUpTime.0";	oids[2]="sysContact.0";	oids[3]="sysName.0";	oids[4]="sysLocation.0";	/* IP-MIB */	oids[5]="ipForwarding.0";	snprintf(var_buffer,sizeof(var_buffer),"ipAdEntNetMask.%s",ip_addr);	oids[6]=var_buffer;	/* IF-MIB */	oids[7]="ifNumber.0";		data=malloc(sizeof *data);        /* Initialize the SNMP library */    init_snmp("snmpapp");        /* Initialize a "session" that defines who we're going to talk to */    snmp_sess_init(&session);                   /* set up defaults */    session.peername=strdup(ip_addr);    /* set the SNMP version number */    session.version = SNMP_VERSION_1;    /* set the SNMPv1 community name used for authentication */    session.community = "public";    session.community_len = strlen(session.community);	/* Open the session */	 	/* We'll keep this in for windows */    SOCK_STARTUP;	/* Establish session */    ss = snmp_open(&session);                  	    if (!ss) {	snmp_perror("ack");    snmp_log(LOG_ERR, "Could not setup SNMP session to host\n");	return 0;    }    	/* Create the PDU for the data for our request. */	for(i=0;i<OIDS;i++) {     pdu = snmp_pdu_create(SNMP_MSG_GET);		get_node(oids[i],anOID,&anOID_len);    snmp_add_null_var(pdu,anOID,anOID_len);  	/* Send the request out */    status=snmp_synch_response(ss,pdu,&response);    /* Process the response */    if(status==STAT_SUCCESS&&response->errstat==SNMP_ERR_NOERROR) {	/* Print the variables */    for(vars = response->variables; vars; vars = vars->next_variable)	print_variable(vars->name, vars->name_length, vars);	/* Fill in first part of database struct */		if((he=gethostbyaddr((char *)&ip_addr,sizeof(ip_addr),AF_INET))==(struct hostent *)NULL) {	/* Dang. Let's just use ip address then */	strncpy(data->hostname,ip_addr,sizeof(data->ip_addr));	strncpy(data->ip_addr,ip_addr,sizeof(data->ip_addr));}	else {	strncpy(data->hostname,he->h_name,sizeof(data->hostname));	strncpy(data->ip_addr,ip_addr,sizeof(data->ip_addr));	}	    /* Manipulate the information ourselves */    for(vars=response->variables;vars;vars=vars->next_variable) {	if(vars->type==ASN_OCTET_STR) {		char *sp=(char *)malloc(1+vars->val_len);		memcpy(sp,vars->val.string,vars->val_len);		sp[vars->val_len]='\0';	  		/* Fill in second part of database struct */		if(i==0) strncpy(data->sysDescr,sp,sizeof(data->sysDescr));		if(i==2) strncpy(data->sysContact,sp,sizeof(data->sysContact));		if(i==3) strncpy(data->sysName,sp,sizeof(data->sysName));		if(i==4) strncpy(data->sysLocation,sp,sizeof(data->sysLocation));			free(sp);		}	if(vars->type==ASN_TIMETICKS) {	if(i==1) {	//data->sysUpTime=malloc(sizeof(long));	data->sysUpTime=*vars->val.integer;	}	}		if(vars->type==ASN_INTEGER) {	if(i==5) { 	//data->ipForwarding=malloc(sizeof(long)); 	data->ipForwarding=*vars->val.integer;	}	if(i==7) {	//data->ifNumber=malloc(sizeof(long));	data->ifNumber=*vars->val.integer;	}	}				}	} 	else {	    /* Something went wrong. Close thread and exit */	if(status==STAT_SUCCESS) {    fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat));	if(response)	snmp_free_pdu(response);	snmp_close(ss);	pthread_exit(1);} /* Return right away */	  	else {	snmp_sess_perror("snmpget", ss);	if(response)	snmp_free_pdu(response);	snmp_close(ss);	pthread_exit(1);} /* Return right away */    }	} /* for */	/* Add record to database */	add_database(data);	/* Save database */	save_database();     /* Clean up: free the response and close the session. */    if (response)	snmp_free_pdu(response);    snmp_close(ss);	/* We'll keep this in for windows */    SOCK_CLEANUP;	pthread_exit(0);}