I'm working on a network application using net-snmp to get informations from snmp agents by programming an snmp manager in C. My code should work but everytime I process the variable structure from the net-snmp library I receive a SIGSEGV and I really don't know why. If anyone knows why and/or how to make my code work, please tell me. (I'm developping with CodeBlocks 16.01 IDE, if that helps)
Thank you for your help.
PS : I'm using net-snmp version 5.7.2.1+dfsg-1.
PPS : My computer works with Debian Jessie (8.6.0).
PPPS : It's my first time posting on a forum for help so please be gentle.
source :
/* Include zone, some may not be useful in this application but I used them for testings */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <string.h>
/* function prototypes */
int print_result(int status, struct snmp_session *sp, struct snmp_pdu *pdu);
/* Main program */
int main(void)
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
oid anOID[MAX_OID_LEN];
size_t anOID_len;
int status;
long int *buf[256];
/* Variables init */
status = 0;
response = 0;
/* SNMP init */
init_snmp("test");
/* Session init */
snmp_sess_init(&session);
session.peername = "192.168.50.29";
/* SNMP session version */
session.version = SNMP_VERSION_1;
/* SNMP session community */
session.community = "public";
session.community_len = strlen(session.community);
/* Session open */
SOCK_STARTUP;
ss = snmp_open(&session);
if (!ss)
{
snmp_perror("ack");
exit(2);
}
/* Create PDU */
pdu = snmp_pdu_create(SNMP_MSG_GET);
/* Get OID */
get_node("sysDescr.0", anOID, &anOID_len);
snmp_add_null_var(pdu, anOID, anOID_len);
/* Send PDU request and stock response in &response */
status = snmp_synch_response(ss, pdu, &response);
/* Response processing */
print_result(status, ss, response);
if(response)
{
snmp_free_pdu(response);
}
/* Closing */
snmp_close(ss);
SOCK_CLEANUP;
return (0);
}
/* Response processing program, where the error I get is */
int print_result(int status, struct snmp_session *sp, struct snmp_pdu *pdu)
{
char buf[1024];
struct variable_list *vp;
int ix;
struct timeval now;
struct timezone tz;
struct tm *tm;
/* Get the hour */
gettimeofday(&now, &tz);
tm = localtime(&now.tv_sec);
printf("Heure = %.2d:%.2d:%.2d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
/* Print SNMP response */
switch (status)
{
/* If no error in snmp_synch_response(); */
case STAT_SUCCESS :
{
vp = pdu->variables;
/* If no error in response */
if (pdu->errstat == SNMP_ERR_NOERROR)
{
while(vp) //Possibly error here, but not the point of this question
{
/* The error I get is here : everytime I manipulate the vp structure I get the SIGSEGV signal */
snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
printf("%s: %s\n", sp->peername, buf);
vp = vp->next_variable;
}
return 0;
}
/* If error in response */
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)");
}
printf("%s: %s: %s\n",sp->peername, buf, snmp_errstring(pdu->errstat));
}
return -1;
}
}break;
/* If timeout in snmp_synch_response(); */
case STAT_TIMEOUT :
{
fprintf(stdout, "%s: Timeout\n", sp->peername);
return -1;
}break;
/* If error snmp_synch_response(); */
case STAT_ERROR :
{
snmp_perror(sp->peername);
return -1;
}break;
default :
{
return -1;
}
}
}
I answer to my own question because I found what was wrong with my code.
The SNMP version was wrong, the agent I ask uses SNMPv2c not SNMPv1.
The OID was wrong (instead of giving its name, giving it's identifier works better) : to know which one to give I used the OiDViEW software to parse the MIB of the agent I use for testing and selected one of them (OIDs).
To make the code work I replaced get_node("sysDescr.0", anOID, &anOID_len);
by snmp_parse_oid(".1.3.6.1.2.1.1.5.0", anOID, &anOID_len);
and session.version = SNMP_VERSION_1;
by session.version = SNMP_VERSION_2c;
After these changes the code works fine.