C: Cómo acceder al valor devuelto por Net-SNMP GET


Me disculpo por la pregunta ingenua, soy nuevo en Net-SNMP. He intentado usar esta sencilla aplicación de demostración de SNMP que se proporciona en el sitio web de Net-SNMP.

Este código realiza un SNMP-GET y manipula la respuesta para comprobar si el valor devuelto es ASN_OCTET_STRING y, en caso afirmativo, acceder a la cadena mediante vars->val.string y asignarla a un puntero de carácter sp.

Pero no puedo averiguar cómo acceder a este valor si el tipo es diferente a ASN_OCTET_STRING. por ejemplo comotomo este valor y, digamos, lo asigno a una variable si es del tipo 'ASN_INTEGER' o 'ASN_OBJECT_ID'.

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <string.h>
#define DEMO_USE_SNMP_VERSION_3

#ifdef DEMO_USE_SNMP_VERSION_3
const char *our_v3_passphrase = "MD5Password";
#endif

int main(int argc, char ** argv)
{
    netsnmp_session session, *ss;
    netsnmp_pdu *pdu;
    netsnmp_pdu *response;

    oid anOID[MAX_OID_LEN];
    size_t anOID_len;

    netsnmp_variable_list *vars;
    int status;
    int count=1;

    init_snmp("snmpdemoapp");

    snmp_sess_init( &session );                   
    session.peername = strdup("localhost:161");


#ifdef DEMO_USE_SNMP_VERSION_3

    session.version=SNMP_VERSION_3;

    session.securityName = strdup("user2");
    session.securityNameLen = strlen(session.securityName);

    session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

    session.securityAuthProto = usmHMACMD5AuthProtocol;
    session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
    session.securityAuthKeyLen = USM_AUTH_KU_LEN;

    if (generate_Ku(session.securityAuthProto,
                    session.securityAuthProtoLen,
                    (u_char *) our_v3_passphrase, strlen(our_v3_passphrase),
                    session.securityAuthKey,
                    &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
        snmp_perror(argv[0]);
        snmp_log(LOG_ERR,
                 "Error generating Ku from authentication pass phrase. \n");
        exit(1);
    }

#else /* we'll use the insecure (but simplier) SNMPv1 */

    session.version = SNMP_VERSION_1;

    session.community = "demopublic";
    session.community_len = strlen(session.community);

#endif /* SNMPv1 */

    SOCK_STARTUP;
    ss = snmp_open(&session);                     
    if (!ss) {
      snmp_sess_perror("ack", &session);
      SOCK_CLEANUP;
      exit(1);
    }

    pdu = snmp_pdu_create(SNMP_MSG_GET);
    anOID_len = MAX_OID_LEN;
    if (!snmp_parse_oid("ip.21.1.8.xx.xx.xx.xx", anOID, &anOID_len)) {
      snmp_perror("ip.21.1.8.xx.xx.xx.xx");
      SOCK_CLEANUP;
      exit(1);
    }

    snmp_add_null_var(pdu, anOID, anOID_len);

    status = snmp_synch_response(ss, pdu, &response);

    if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {

      for(vars = response->variables; vars; vars = vars->next_variable)
        print_variable(vars->name, vars->name_length, vars);

      /* manipuate 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] = '
HOST-RESOURCES-MIB::hrSWInstalledDate.1953 = STRING: 0-1-1,0:0:0.0
'; printf("value #%d is a string: %s\n", count++, sp); //Here sp now has the string - But this doesnt work when the string is for eg."HOST-RESOURCES-MIB::hrSWInstalledDate.1953 = STRING: 0-1-1,0:0:0.0" free(sp); } else if(vars->type == ASN_INTEGER) { printf("value is an Integer\n"); int ObjVal; // How do I get the Integer value and assign it to 'ObjVal' } else if(vars->type == ASN_OBJECT_ID) { printf("value is an OID\n"); // How do I get the OID and assign it to some variable } else if(vars->type == ASN_TIMETICKS) { printf("value is in Timeticks\n"); // How do I get the Timeticks and assign it to some variable for further processing } } } else { if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); else if (status == STAT_TIMEOUT) fprintf(stderr, "Timeout: No response from %s.\n", session.peername); else snmp_sess_perror("snmpdemoapp", ss); } if (response) snmp_free_pdu(response); snmp_close(ss); SOCK_CLEANUP; return (0); }

Intenté vars->val.integer o vars->val.object_id, pero eso no contiene el valor. ¿Qué me estoy perdiendo aquí?

Mi otra pregunta es, incluso cuando es del tipo ASN_OCTET_STRING, cuando la respuesta GET es algo como esto,

typedef union {
   long           *integer;
   u_char         *string;
   oid            *objid;
   u_char         *bitstring;
   struct counter64 *counter64;
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
   float          *floatVal;
   double         *doubleVal;
   /*
    * t_union *unionVal; 
    */
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
} netsnmp_vardata;

entonces vars->val.string no tiene "0-1-1,0:0:0.0" como cadena.

Básicamente, mi pregunta es ¿Cómo se almacena el valor?en la estructura de respuesta de la que puedo recuperar los valores?

Gracias de antemano!!

P.S: enlace Makefile del sitio web de Net-SNMP.

Editar1: Para enteros, puedo leer usando *vars->val->string como lo indica immibis. ¿Alguna idea sobre cómo acceder a otros tipos de datos?



------------Respuesta------------

Como puede ver en el archivo /usr/include/net-snmp/types.h o similar en su sistema, net-snmp vars->val tiene el siguiente tipo de unión:

1008611

también*vars tiene el campo val_len, donde se almacena la longitud de los datos. Entonces puede acceder a enteros como *vars->val.integer, string como puntero a u_char vars->val.string con vars->val_len chars, oid como puntero a oid vars->val.objid con vars- >val_len/sizeof(oid) oid elementos y así sucesivamente.

Etiquetas: net-snmp snmp c

Artículos relacionados:

wordpress: he seleccionado un tema html en el bosque de temas que satisface todas mis necesidades comerciales. ¿Con qué CMS debo ir?

Error de C# MySQL: palabra clave no admitida