2017-05-23 36 views
0

net-snmpを使用してネットワークアプリケーションを開発していますが、snmpエージェントから情報を取得するには、私はSIGSEGVを受け取るnet-snmpライブラリから可変構造を処理します。なぜ私は本当にその理由がわかりません。なぜ誰かが私のコードを動作させる方法および/または方法を知っているなら、教えてください。 (私がCodeBlocks 16.01 IDEを開発していれば助かります)Cでnet-snmpライブラリを使用してSNMPエージェントからの応答を処理するときにSIGSEGVを受信

ありがとうございました。

PS:net-snmpバージョン5.7.2.1 + dfsg-1を使用しています。

PPS:私のコンピュータはDebian Jessie(8.6.0)で動作します。

PPPS:初めてフォーラムに投稿したので、やさしくしてください。

ソース:私は私のコードで間違っていたものを見つけたので、私は自分の質問に答える

/* 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; 
     } 
    } 
} 
+1

プログラムのデバッグバージョンをビルドし、デバッガで実行して、 "動作中"のクラッシュをキャッチして、*自分のコードがどこにあるのかを特定します。しかし、その前に、[小さなプログラムのデバッグ方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を読むべきでしょう。 –

+0

デバッガを使用して、segfaultが発生する行を特定し、ここで結果を報告してください。 – Jens

+0

私はすでに起こった行を入れました。snprint_variable関数を呼び出すと、その行のすぐ上にコメントしました。 –

答えて

0

  1. SNMPバージョンが間違っていたので、私が尋ねるエージェントはSNMPv1ではなくSNMPv2cを使用します。

  2. OIDは間違っていました(その名前を付けるのではなく、識別子の方がうまくいく):私はOiDViEWソフトウェアを使ってテスト用のエージェントのMIBを解析し、その1つを選択しました(OID)。

コードが正常に動作し、これらの変更の後、私はsession.version = SNMP_VERSION_2c;

によってsnmp_parse_oid(".1.3.6.1.2.1.1.5.0", anOID, &anOID_len);session.version = SNMP_VERSION_1;get_node("sysDescr.0", anOID, &anOID_len);を置き換えコードを動作させるために。

関連する問題