about modifying files generated by mib2c

"蔷<Qiang Hu>" <[email protected]>
Newsgroups gmane.network.net-snmp.user
Message-ID <[email protected]>
i generated .c and .h of my test mib by mib2c and also can snmptraslate my
test mib tree under experimental nod as you can see
[littlepi@localhost project]# snmptranslate -IR -Tp jmtest
+--jmtest(72)
   +-- -RW- INTEGER   firstKey(1)
            Range: 0..100

and then i modified the files generated by mib2c and put it in mibgroup/ and
./configure --with-mib-modules="jmtest" , recompiled ,as a result ,generated
.o and .lo files of my test mib code in mibgroup/ .but when i tried this
command,i got the wrong value not correspond to my set in c code as you can
see in the following.and particularly, for restart snmpd each time ,i got
the different value although i didn't do anything else. so is there
something wrong with modifying i did to the files generated by mib2c??
i pasted my modified c code files and my test mib ASN.1 document.

[littlepi@localhost project]# snmpget -v 1 -c public localhost
jmtest.firstKey.0
JM-TEST-1-MIB::firstKey.0 = INTEGER: -1074568504

my test mib is as following:
JM-TEST-1-MIB DEFINITIONS ::= BEGIN

IMPORTS
        MODULE-IDENTITY,
        OBJECT-TYPE,
        INTEGER
                FROM SNMPv2-SMI;

jmtest MODULE-IDENTITY
    LAST-UPDATED "200203210000Z"
    ORGANIZATION "Temple U"
    CONTACT-INFO
        "None yet."
    DESCRIPTION
        "AgentX testing MIB"
    REVISION     "200203210000Z"
    DESCRIPTION
        "None yet."
    ::= { experimental 72}

firstKey OBJECT-TYPE
    SYNTAX      INTEGER (0..100)
    MAX-ACCESS  read-write
    STATUS      current
    DESCRIPTION
        "Value initialized to 0 and on each
         access:
         - Return current val.
         - increment"

    ::= { jmtest 1 }

END

jmtest.c I modified for test:



/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.old-api.conf,v 1.4 2004/07/28 08:04:58 dts12 Exp $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "jmtest.h"

/*
 * jmtest_variables_oid:
 *   this is the top level oid that we want to register under.  This
 *   is essentially a prefix, with the suffix appearing in the
 *   variable below.
 */

oid             jmtest_variables_oid[] = { 1, 3, 6, 1, 3, 72 };

/*
 * variable4 jmtest_variables:
 *   this variable defines function callbacks and type return information
 *   for the  mib section
 */

struct variable4 jmtest_variables[] = {
    /*
     * magic number        , variable type , ro/rw , callback fn  , L,
oidsuffix
     */
#define FIRSTKEY  1
    {FIRSTKEY, ASN_INTEGER, RWRITE, var_jmtest, 1, {1}},

};

/*
 * (L = length of the oidsuffix)
 */


/** Initializes the jmtest module */
void
init_jmtest(void)
{

    DEBUGMSGTL(("jmtest", "Initializing\n"));

    /*
     * register ourselves with the agent to handle our mib tree
     */
    REGISTER_MIB("jmtest", jmtest_variables, variable4,
                 jmtest_variables_oid);

    /*
     * place any other initialization junk you need here
     */
}

/*
 * var_jmtest():
 *   This function is called every time the agent gets a request for
 *   a scalar variable that might be found within your mib section
 *   registered above.  It is up to you to do the right thing and
 *   return the correct value.
 *     You should also correct the value of "var_len" if necessary.
 *
 *   Please see the documentation for more information about writing
 *   module extensions, and check out the examples in the examples
 *   and mibII directories.
 */
unsigned char  *
var_jmtest(struct variable *vp,
           oid * name,
           size_t *length,
           int exact, size_t *var_len, WriteMethod ** write_method)
{
    /*
     * variables we may use later
     */
    static long     long_ret;
    static u_long   ulong_ret;
    static unsigned char string[SPRINT_MAX_LEN];
    static oid      objid[MAX_OID_LEN];
    static struct counter64 c64;
 int   VAR;

    if (header_generic(vp, name, length, exact, var_len, write_method)
        == MATCH_FAILED)
        return NULL;

    /*
     * this is where we do the value assignments for the mib results.
     */
    switch (vp->magic) {
    case FIRSTKEY:
        *write_method = write_firstKey;
        VAR = 1;            /* XXX */
  *var_len =sizeof(VAR);
        return (u_char *) & VAR;
    default:
        ERROR_MSG("");
    }
    return NULL;
}

int
write_firstKey(int action,
               u_char * var_val,
               u_char var_val_type,
               size_t var_val_len,
               u_char * statP, oid * name, size_t name_len)
{
    long            value;
    int             size;

    switch (action) {
    case RESERVE1:
        if (var_val_type != ASN_INTEGER) {
            fprintf(stderr, "write to jmtest not ASN_INTEGER\n");
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(long)) {
            fprintf(stderr, "write to jmtest: bad length\n");
            return SNMP_ERR_WRONGLENGTH;
        }
        break;

    case RESERVE2:
        size = var_val_len;
        value = *(long *) var_val;

        break;

    case FREE:
        /*
         * Release any resources that have been allocated
         */
        break;

    case ACTION:
        /*
         * The variable has been stored in 'value' for you to use,
         * and you have just been asked to do something with it.
         * Note that anything done here must be reversable in the UNDO case
         */
        break;

    case UNDO:
        /*
         * Back out any changes made in the ACTION case
         */
        break;

    case COMMIT:
        /*
         * Things are working well, so it's now safe to make the change
         * permanently.  Make sure that anything done here can't fail!
         */
        break;
    }
    return SNMP_ERR_NOERROR;
}







-- 
蔷

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.