C - 从SNMP SET解析字符串(奇怪)

时间:2015-05-29 20:27:15

标签: c snmp net-snmp mib snmpd

我正在使用自己的SNMP代理,但在处理字符串时遇到问题。我也很熟悉SNMP。

我已经参考了以下链接来实现我自己的代理:

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

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

第二个链接显示了用户尝试设置整数类型MIB对象时的确切处理方式:

第657行显示:

intval = *((long *) var_val);

我的问题:我怎么用字符串来解决它?我尝试过它,strncpy,snprintf等

我的工作:

我知道,或者至少认为以下是合法的:

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;
        }

我知道它有点起作用,因为当我调用

# 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

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"

这向我证明至少最后一批代码正在运作。

以下是行动部分:

   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;

以上部分我无法上班。

我已经能够通过使对象成为INTEGER(ASN.1)类型来获得我想要的东西,但我无法做到这一点,因为在读取此对象时它会返回STRING(ASN.1)

0 个答案:

没有答案