0

I am working on my own SNMP agent and am having trouble processing strings. I am pretty new to SNMP as well.

I've referred to the following links for some of implementing my own agent :

http://www.net-snmp.org/dev/agent/ucdDemoPublic_8c_source.html

http://www.net-snmp.org/dev/agent/example_8h_source.html

The second link shows exactly how to handle when a user attempts to set an integer type MIB object :

line 657 shows :

intval = *((long *) var_val);

My Question : How would I go about it with a string? I've tried casting it, strncpy, snprintf, etc.

My work :

I know, or at least think, that the following is legal :

int
setString(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)
{
    unsigned char publicString[10];
    static long intval;
    char *cmd_string = NULL;

    /*
     * Define an arbitrary maximum permissible value
     */
    switch (action) {
    case RESERVE1:

        //intval = *((long *) var_val);

        /*
         *  Check that the value being set is acceptable
         */
        if (var_val_type != ASN_OCTET_STR) {
            DEBUGMSGTL(("setString", "%x not string type", var_val_type));
            return SNMP_ERR_WRONGTYPE;
        }

        if (var_val_len > 1 ) {
            DEBUGMSGTL(("setString", "wrong length %" NETSNMP_PRIz "u",
                        var_val_len));
            return SNMP_ERR_WRONGLENGTH;
        }

        if ( !(var_val[0] == '1' || var_val[0] == '0') )
        {
            DEBUGMSGTL(("setString", "wrong value %s", var_val));
            return SNMP_ERR_WRONGVALUE;
        }

I know it is somewhat working because when I invoke

# snmpset -v 2c -c xxx 10.20.30.40 1.3.6.1.4.1.54321.3.0 s 3
Error in packet.
Reason: wrongValue (The set value is illegal or unsupported in some way)
Failed object: MY-TEST-MIB::testSnmp.3.0

AND

snmpset -v 2c -c xxx 10.20.30.40 1.3.6.1.4.1.54321.3.0 s 1
MY-TEST-MIB::testSnmp.3.0 = STRING: "1"

This proves to me at least the last collection of code is working.

Here is the action part :

   case ACTION:
        /*
         *  Set the variable as requested.
         *   Note that this may need to be reversed,
         *   so save any information needed to do this.
         */

        if ( var_val[0] == '1' )
        {
            //do stuff - there realy is a script call here that does something
        }

        if ( var_val[0] == '0' )
        {
            //do stuff - there realy is a script call here that does something
        }
        break;

The above section I cannot get to work.

I've been able to get what I want by making the object an INTEGER (ASN.1) type but I can't do that because when reading this object it returns a STRING (ASN.1).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
jtor
  • 133
  • 1
  • 4
  • 13
  • 1
    Have you tried running your program in a debugger, setting a breakpoint in the function and see what the values actually are? – Some programmer dude May 29 '15 at 20:30
  • After reading up on the debugging and experimenting, I finally was able to look at a log file for what exactly was going on. Thanks for the hint! – jtor Jun 02 '15 at 19:20
  • @jtor, what was the root cause you figured here? can you please share? – Pawan Jul 16 '15 at 16:23
  • The best way to figure out what EXACTLY is going on is to enable debugging on the snmpd agent. To do this, I edited /etc/default/snmpd to run as : `SNMPDOPTS='-Dall -Lsd -Lf /var/log/snmpd.log -u root -g root -I -smux,mteTrigger,mteTriggerConf -p /var/run/snmpd.pid' ` I then put some unique parameters into DEBUGMSGTL. The first parameter is more of a "LABEL" and the second is the actual message. Search for these inside your specified logfile and you'll see what I mean. Printing out what is actually there will help tremendously. – jtor Jul 17 '15 at 17:24
  • @Pawan , secondly, when debugging, I highly recommend putting your messages in RESERVE1 inside your "WriteMethod". RESERVE1 is the first place the code will go before actually setting the specified value from the user. RESERVE1 is meant for data type/value/etc. checking before it goes off and does the modification. – jtor Jul 17 '15 at 17:36

0 Answers0