首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用c中的net库处理来自SNMP代理的响应时收到的SIGSEGV。

使用c中的net库处理来自SNMP代理的响应时收到的SIGSEGV。
EN

Stack Overflow用户
提问于 2017-05-23 14:04:12
回答 1查看 304关注 0票数 0

我正在使用network开发一个网络应用程序,通过在C中编程snmp管理器从snmp代理获取信息。我的代码应该可以工作,但是每次我从network库处理可变结构时,我都会收到一个SIGSEGV,我真的不知道为什么。如果有人知道为什么和/或如何使我的代码工作,请告诉我。(我正在使用CodeBlocks 16.01IDE开发,如果这有帮助的话)

谢谢你的帮助。

PS :我使用的是net版本5.7.2.1+dfsg-1.

PPS :我的电脑和Debian一起工作(8.6.0)。

PPPS :这是我第一次在论坛上发帖寻求帮助,所以请温柔一点。

资料来源:

代码语言:javascript
复制
/* 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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-24 08:21:14

我回答我自己的问题,因为我发现我的代码有什么问题。

  1. SNMP版本错了,我问的代理程序使用的是SNMPv2c而不是SNMPv1。
  2. OID是错误的(不是给出它的名称,而是给出它的标识符):要知道给出哪个标识符,我使用了OiDViEW软件来解析用于测试的代理的MIB,并选择了其中之一(OID)。

为了使代码工作,我用snmp_parse_oid(".1.3.6.1.2.1.1.5.0", anOID, &anOID_len);代替了snmp_parse_oid(".1.3.6.1.2.1.1.5.0", anOID, &anOID_len);,用session.version = SNMP_VERSION_2c;代替了session.version = SNMP_VERSION_1;

在这些更改之后,代码可以正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44137045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档